Use innermost_loop_behavior for outer loop vectorisation
[official-gcc.git] / gcc / predict.c
blob4d01bf357efd4eb6ae971601144ce9337b32c63d
1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* References:
22 [1] "Branch Prediction for Free"
23 Ball and Larus; PLDI '93.
24 [2] "Static Branch Frequency and Program Profile Analysis"
25 Wu and Larus; MICRO-27.
26 [3] "Corpus-based Static Branch Prediction"
27 Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "backend.h"
34 #include "rtl.h"
35 #include "tree.h"
36 #include "gimple.h"
37 #include "cfghooks.h"
38 #include "tree-pass.h"
39 #include "ssa.h"
40 #include "memmodel.h"
41 #include "emit-rtl.h"
42 #include "cgraph.h"
43 #include "coverage.h"
44 #include "diagnostic-core.h"
45 #include "gimple-predict.h"
46 #include "fold-const.h"
47 #include "calls.h"
48 #include "cfganal.h"
49 #include "profile.h"
50 #include "sreal.h"
51 #include "params.h"
52 #include "cfgloop.h"
53 #include "gimple-iterator.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa-loop-niter.h"
56 #include "tree-ssa-loop.h"
57 #include "tree-scalar-evolution.h"
58 #include "ipa-utils.h"
59 #include "gimple-pretty-print.h"
60 #include "selftest.h"
61 #include "cfgrtl.h"
63 /* Enum with reasons why a predictor is ignored. */
65 enum predictor_reason
67 REASON_NONE,
68 REASON_IGNORED,
69 REASON_SINGLE_EDGE_DUPLICATE,
70 REASON_EDGE_PAIR_DUPLICATE
73 /* String messages for the aforementioned enum. */
75 static const char *reason_messages[] = {"", " (ignored)",
76 " (single edge duplicate)", " (edge pair duplicate)"};
78 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
79 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */
80 static sreal real_almost_one, real_br_prob_base,
81 real_inv_br_prob_base, real_one_half, real_bb_freq_max;
83 static void combine_predictions_for_insn (rtx_insn *, basic_block);
84 static void dump_prediction (FILE *, enum br_predictor, int, basic_block,
85 enum predictor_reason, edge);
86 static void predict_paths_leading_to (basic_block, enum br_predictor,
87 enum prediction,
88 struct loop *in_loop = NULL);
89 static void predict_paths_leading_to_edge (edge, enum br_predictor,
90 enum prediction,
91 struct loop *in_loop = NULL);
92 static bool can_predict_insn_p (const rtx_insn *);
94 /* Information we hold about each branch predictor.
95 Filled using information from predict.def. */
97 struct predictor_info
99 const char *const name; /* Name used in the debugging dumps. */
100 const int hitrate; /* Expected hitrate used by
101 predict_insn_def call. */
102 const int flags;
105 /* Use given predictor without Dempster-Shaffer theory if it matches
106 using first_match heuristics. */
107 #define PRED_FLAG_FIRST_MATCH 1
109 /* Recompute hitrate in percent to our representation. */
111 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
113 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
114 static const struct predictor_info predictor_info[]= {
115 #include "predict.def"
117 /* Upper bound on predictors. */
118 {NULL, 0, 0}
120 #undef DEF_PREDICTOR
122 /* Return TRUE if frequency FREQ is considered to be hot. */
124 static inline bool
125 maybe_hot_frequency_p (struct function *fun, int freq)
127 struct cgraph_node *node = cgraph_node::get (fun->decl);
128 if (!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
130 if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
131 return false;
132 if (node->frequency == NODE_FREQUENCY_HOT)
133 return true;
135 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
136 return true;
137 if (node->frequency == NODE_FREQUENCY_EXECUTED_ONCE
138 && freq < (ENTRY_BLOCK_PTR_FOR_FN (fun)->frequency * 2 / 3))
139 return false;
140 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0)
141 return false;
142 if (freq * PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)
143 < ENTRY_BLOCK_PTR_FOR_FN (fun)->frequency)
144 return false;
145 return true;
148 static gcov_type min_count = -1;
150 /* Determine the threshold for hot BB counts. */
152 gcov_type
153 get_hot_bb_threshold ()
155 gcov_working_set_t *ws;
156 if (min_count == -1)
158 ws = find_working_set (PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE));
159 gcc_assert (ws);
160 min_count = ws->min_counter;
162 return min_count;
165 /* Set the threshold for hot BB counts. */
167 void
168 set_hot_bb_threshold (gcov_type min)
170 min_count = min;
173 /* Return TRUE if frequency FREQ is considered to be hot. */
175 bool
176 maybe_hot_count_p (struct function *, profile_count count)
178 if (!count.initialized_p ())
179 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.to_gcov_type () >= get_hot_bb_threshold ());
186 /* Return true in case BB can be CPU intensive and should be optimized
187 for maximal performance. */
189 bool
190 maybe_hot_bb_p (struct function *fun, const_basic_block bb)
192 gcc_checking_assert (fun);
193 if (!maybe_hot_count_p (fun, bb->count))
194 return false;
195 return maybe_hot_frequency_p (fun, bb->frequency);
198 /* Return true in case BB can be CPU intensive and should be optimized
199 for maximal performance. */
201 bool
202 maybe_hot_edge_p (edge e)
204 if (!maybe_hot_count_p (cfun, e->count))
205 return false;
206 return maybe_hot_frequency_p (cfun, EDGE_FREQUENCY (e));
209 /* Return true if profile COUNT and FREQUENCY, or function FUN static
210 node frequency reflects never being executed. */
212 static bool
213 probably_never_executed (struct function *fun,
214 profile_count count, int)
216 gcc_checking_assert (fun);
217 if (count == profile_count::zero ())
218 return true;
219 if (count.initialized_p () && profile_status_for_fn (fun) == PROFILE_READ)
221 int unlikely_count_fraction = PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION);
222 if (count.apply_scale (unlikely_count_fraction, 1) >= profile_info->runs)
223 return false;
224 return true;
226 if ((!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
227 && (cgraph_node::get (fun->decl)->frequency
228 == NODE_FREQUENCY_UNLIKELY_EXECUTED))
229 return true;
230 return false;
234 /* Return true in case BB is probably never executed. */
236 bool
237 probably_never_executed_bb_p (struct function *fun, const_basic_block bb)
239 return probably_never_executed (fun, bb->count, bb->frequency);
243 /* Return true if E is unlikely executed for obvious reasons. */
245 static bool
246 unlikely_executed_edge_p (edge e)
248 return (e->count == profile_count::zero ()
249 || e->probability == profile_probability::never ())
250 || (e->flags & (EDGE_EH | EDGE_FAKE));
253 /* Return true in case edge E is probably never executed. */
255 bool
256 probably_never_executed_edge_p (struct function *fun, edge e)
258 if (unlikely_executed_edge_p (e))
259 return true;
260 return probably_never_executed (fun, e->count, EDGE_FREQUENCY (e));
263 /* Return true when current function should always be optimized for size. */
265 bool
266 optimize_function_for_size_p (struct function *fun)
268 if (!fun || !fun->decl)
269 return optimize_size;
270 cgraph_node *n = cgraph_node::get (fun->decl);
271 return n && n->optimize_for_size_p ();
274 /* Return true when current function 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 the 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 when BB should be optimized for size. */
294 bool
295 optimize_bb_for_size_p (const_basic_block bb)
297 return (optimize_function_for_size_p (cfun)
298 || (bb && !maybe_hot_bb_p (cfun, bb)));
301 /* Return TRUE when BB should be optimized for speed. */
303 bool
304 optimize_bb_for_speed_p (const_basic_block bb)
306 return !optimize_bb_for_size_p (bb);
309 /* Return the optimization type that should be used for block BB. */
311 optimization_type
312 bb_optimization_type (const_basic_block bb)
314 return (optimize_bb_for_speed_p (bb)
315 ? OPTIMIZE_FOR_SPEED
316 : OPTIMIZE_FOR_SIZE);
319 /* Return TRUE when BB should be optimized for size. */
321 bool
322 optimize_edge_for_size_p (edge e)
324 return optimize_function_for_size_p (cfun) || !maybe_hot_edge_p (e);
327 /* Return TRUE when BB should be optimized for speed. */
329 bool
330 optimize_edge_for_speed_p (edge e)
332 return !optimize_edge_for_size_p (e);
335 /* Return TRUE when BB should be optimized for size. */
337 bool
338 optimize_insn_for_size_p (void)
340 return optimize_function_for_size_p (cfun) || !crtl->maybe_hot_insn_p;
343 /* Return TRUE when BB should be optimized for speed. */
345 bool
346 optimize_insn_for_speed_p (void)
348 return !optimize_insn_for_size_p ();
351 /* Return TRUE when LOOP should be optimized for size. */
353 bool
354 optimize_loop_for_size_p (struct loop *loop)
356 return optimize_bb_for_size_p (loop->header);
359 /* Return TRUE when LOOP should be optimized for speed. */
361 bool
362 optimize_loop_for_speed_p (struct loop *loop)
364 return optimize_bb_for_speed_p (loop->header);
367 /* Return TRUE when LOOP nest should be optimized for speed. */
369 bool
370 optimize_loop_nest_for_speed_p (struct loop *loop)
372 struct loop *l = loop;
373 if (optimize_loop_for_speed_p (loop))
374 return true;
375 l = loop->inner;
376 while (l && l != loop)
378 if (optimize_loop_for_speed_p (l))
379 return true;
380 if (l->inner)
381 l = l->inner;
382 else if (l->next)
383 l = l->next;
384 else
386 while (l != loop && !l->next)
387 l = loop_outer (l);
388 if (l != loop)
389 l = l->next;
392 return false;
395 /* Return TRUE when LOOP nest should be optimized for size. */
397 bool
398 optimize_loop_nest_for_size_p (struct loop *loop)
400 return !optimize_loop_nest_for_speed_p (loop);
403 /* Return true when edge E is likely to be well predictable by branch
404 predictor. */
406 bool
407 predictable_edge_p (edge e)
409 if (!e->probability.initialized_p ())
410 return false;
411 if ((e->probability.to_reg_br_prob_base ()
412 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100)
413 || (REG_BR_PROB_BASE - e->probability.to_reg_br_prob_base ()
414 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100))
415 return true;
416 return false;
420 /* Set RTL expansion for BB profile. */
422 void
423 rtl_profile_for_bb (basic_block bb)
425 crtl->maybe_hot_insn_p = maybe_hot_bb_p (cfun, bb);
428 /* Set RTL expansion for edge profile. */
430 void
431 rtl_profile_for_edge (edge e)
433 crtl->maybe_hot_insn_p = maybe_hot_edge_p (e);
436 /* Set RTL expansion to default mode (i.e. when profile info is not known). */
437 void
438 default_rtl_profile (void)
440 crtl->maybe_hot_insn_p = true;
443 /* Return true if the one of outgoing edges is already predicted by
444 PREDICTOR. */
446 bool
447 rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
449 rtx note;
450 if (!INSN_P (BB_END (bb)))
451 return false;
452 for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
453 if (REG_NOTE_KIND (note) == REG_BR_PRED
454 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
455 return true;
456 return false;
459 /* Structure representing predictions in tree level. */
461 struct edge_prediction {
462 struct edge_prediction *ep_next;
463 edge ep_edge;
464 enum br_predictor ep_predictor;
465 int ep_probability;
468 /* This map contains for a basic block the list of predictions for the
469 outgoing edges. */
471 static hash_map<const_basic_block, edge_prediction *> *bb_predictions;
473 /* Return true if the one of outgoing edges is already predicted by
474 PREDICTOR. */
476 bool
477 gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
479 struct edge_prediction *i;
480 edge_prediction **preds = bb_predictions->get (bb);
482 if (!preds)
483 return false;
485 for (i = *preds; i; i = i->ep_next)
486 if (i->ep_predictor == predictor)
487 return true;
488 return false;
491 /* Return true if the one of outgoing edges is already predicted by
492 PREDICTOR for edge E predicted as TAKEN. */
494 bool
495 edge_predicted_by_p (edge e, enum br_predictor predictor, bool taken)
497 struct edge_prediction *i;
498 basic_block bb = e->src;
499 edge_prediction **preds = bb_predictions->get (bb);
500 if (!preds)
501 return false;
503 int probability = predictor_info[(int) predictor].hitrate;
505 if (taken != TAKEN)
506 probability = REG_BR_PROB_BASE - probability;
508 for (i = *preds; i; i = i->ep_next)
509 if (i->ep_predictor == predictor
510 && i->ep_edge == e
511 && i->ep_probability == probability)
512 return true;
513 return false;
516 /* Return true when the probability of edge is reliable.
518 The profile guessing code is good at predicting branch outcome (ie.
519 taken/not taken), that is predicted right slightly over 75% of time.
520 It is however notoriously poor on predicting the probability itself.
521 In general the profile appear a lot flatter (with probabilities closer
522 to 50%) than the reality so it is bad idea to use it to drive optimization
523 such as those disabling dynamic branch prediction for well predictable
524 branches.
526 There are two exceptions - edges leading to noreturn edges and edges
527 predicted by number of iterations heuristics are predicted well. This macro
528 should be able to distinguish those, but at the moment it simply check for
529 noreturn heuristic that is only one giving probability over 99% or bellow
530 1%. In future we might want to propagate reliability information across the
531 CFG if we find this information useful on multiple places. */
532 static bool
533 probability_reliable_p (int prob)
535 return (profile_status_for_fn (cfun) == PROFILE_READ
536 || (profile_status_for_fn (cfun) == PROFILE_GUESSED
537 && (prob <= HITRATE (1) || prob >= HITRATE (99))));
540 /* Same predicate as above, working on edges. */
541 bool
542 edge_probability_reliable_p (const_edge e)
544 return e->probability.reliable_p ();
547 /* Same predicate as edge_probability_reliable_p, working on notes. */
548 bool
549 br_prob_note_reliable_p (const_rtx note)
551 gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB);
552 return probability_reliable_p (XINT (note, 0));
555 static void
556 predict_insn (rtx_insn *insn, enum br_predictor predictor, int probability)
558 gcc_assert (any_condjump_p (insn));
559 if (!flag_guess_branch_prob)
560 return;
562 add_reg_note (insn, REG_BR_PRED,
563 gen_rtx_CONCAT (VOIDmode,
564 GEN_INT ((int) predictor),
565 GEN_INT ((int) probability)));
568 /* Predict insn by given predictor. */
570 void
571 predict_insn_def (rtx_insn *insn, enum br_predictor predictor,
572 enum prediction taken)
574 int probability = predictor_info[(int) predictor].hitrate;
576 if (taken != TAKEN)
577 probability = REG_BR_PROB_BASE - probability;
579 predict_insn (insn, predictor, probability);
582 /* Predict edge E with given probability if possible. */
584 void
585 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
587 rtx_insn *last_insn;
588 last_insn = BB_END (e->src);
590 /* We can store the branch prediction information only about
591 conditional jumps. */
592 if (!any_condjump_p (last_insn))
593 return;
595 /* We always store probability of branching. */
596 if (e->flags & EDGE_FALLTHRU)
597 probability = REG_BR_PROB_BASE - probability;
599 predict_insn (last_insn, predictor, probability);
602 /* Predict edge E with the given PROBABILITY. */
603 void
604 gimple_predict_edge (edge e, enum br_predictor predictor, int probability)
606 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
607 && EDGE_COUNT (e->src->succs) > 1
608 && flag_guess_branch_prob
609 && optimize)
611 struct edge_prediction *i = XNEW (struct edge_prediction);
612 edge_prediction *&preds = bb_predictions->get_or_insert (e->src);
614 i->ep_next = preds;
615 preds = i;
616 i->ep_probability = probability;
617 i->ep_predictor = predictor;
618 i->ep_edge = e;
622 /* Filter edge predictions PREDS by a function FILTER. DATA are passed
623 to the filter function. */
625 void
626 filter_predictions (edge_prediction **preds,
627 bool (*filter) (edge_prediction *, void *), void *data)
629 if (!bb_predictions)
630 return;
632 if (preds)
634 struct edge_prediction **prediction = preds;
635 struct edge_prediction *next;
637 while (*prediction)
639 if ((*filter) (*prediction, data))
640 prediction = &((*prediction)->ep_next);
641 else
643 next = (*prediction)->ep_next;
644 free (*prediction);
645 *prediction = next;
651 /* Filter function predicate that returns true for a edge predicate P
652 if its edge is equal to DATA. */
654 bool
655 equal_edge_p (edge_prediction *p, void *data)
657 return p->ep_edge == (edge)data;
660 /* Remove all predictions on given basic block that are attached
661 to edge E. */
662 void
663 remove_predictions_associated_with_edge (edge e)
665 if (!bb_predictions)
666 return;
668 edge_prediction **preds = bb_predictions->get (e->src);
669 filter_predictions (preds, equal_edge_p, e);
672 /* Clears the list of predictions stored for BB. */
674 static void
675 clear_bb_predictions (basic_block bb)
677 edge_prediction **preds = bb_predictions->get (bb);
678 struct edge_prediction *pred, *next;
680 if (!preds)
681 return;
683 for (pred = *preds; pred; pred = next)
685 next = pred->ep_next;
686 free (pred);
688 *preds = NULL;
691 /* Return true when we can store prediction on insn INSN.
692 At the moment we represent predictions only on conditional
693 jumps, not at computed jump or other complicated cases. */
694 static bool
695 can_predict_insn_p (const rtx_insn *insn)
697 return (JUMP_P (insn)
698 && any_condjump_p (insn)
699 && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2);
702 /* Predict edge E by given predictor if possible. */
704 void
705 predict_edge_def (edge e, enum br_predictor predictor,
706 enum prediction taken)
708 int probability = predictor_info[(int) predictor].hitrate;
710 if (taken != TAKEN)
711 probability = REG_BR_PROB_BASE - probability;
713 predict_edge (e, predictor, probability);
716 /* Invert all branch predictions or probability notes in the INSN. This needs
717 to be done each time we invert the condition used by the jump. */
719 void
720 invert_br_probabilities (rtx insn)
722 rtx note;
724 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
725 if (REG_NOTE_KIND (note) == REG_BR_PROB)
726 XINT (note, 0) = REG_BR_PROB_BASE - XINT (note, 0);
727 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
728 XEXP (XEXP (note, 0), 1)
729 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
732 /* Dump information about the branch prediction to the output file. */
734 static void
735 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
736 basic_block bb, enum predictor_reason reason = REASON_NONE,
737 edge ep_edge = NULL)
739 edge e = ep_edge;
740 edge_iterator ei;
742 if (!file)
743 return;
745 if (e == NULL)
746 FOR_EACH_EDGE (e, ei, bb->succs)
747 if (! (e->flags & EDGE_FALLTHRU))
748 break;
750 char edge_info_str[128];
751 if (ep_edge)
752 sprintf (edge_info_str, " of edge %d->%d", ep_edge->src->index,
753 ep_edge->dest->index);
754 else
755 edge_info_str[0] = '\0';
757 fprintf (file, " %s heuristics%s%s: %.1f%%",
758 predictor_info[predictor].name,
759 edge_info_str, reason_messages[reason],
760 probability * 100.0 / REG_BR_PROB_BASE);
762 if (bb->count.initialized_p ())
764 fprintf (file, " exec ");
765 bb->count.dump (file);
766 if (e)
768 fprintf (file, " hit ");
769 e->count.dump (file);
770 fprintf (file, " (%.1f%%)", e->count.to_gcov_type() * 100.0
771 / bb->count.to_gcov_type ());
775 fprintf (file, "\n");
778 /* Return true if STMT is known to be unlikely executed. */
780 static bool
781 unlikely_executed_stmt_p (gimple *stmt)
783 if (!is_gimple_call (stmt))
784 return false;
785 /* NORETURN attribute alone is not strong enough: exit() may be quite
786 likely executed once during program run. */
787 if (gimple_call_fntype (stmt)
788 && lookup_attribute ("cold",
789 TYPE_ATTRIBUTES (gimple_call_fntype (stmt)))
790 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
791 return true;
792 tree decl = gimple_call_fndecl (stmt);
793 if (!decl)
794 return false;
795 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl))
796 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
797 return true;
799 cgraph_node *n = cgraph_node::get (decl);
800 if (!n)
801 return false;
803 availability avail;
804 n = n->ultimate_alias_target (&avail);
805 if (avail < AVAIL_AVAILABLE)
806 return false;
807 if (!n->analyzed
808 || n->decl == current_function_decl)
809 return false;
810 return n->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED;
813 /* Return true if BB is unlikely executed. */
815 static bool
816 unlikely_executed_bb_p (basic_block bb)
818 if (bb->count == profile_count::zero ())
819 return true;
820 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
821 return false;
822 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
823 !gsi_end_p (gsi); gsi_next (&gsi))
825 if (unlikely_executed_stmt_p (gsi_stmt (gsi)))
826 return true;
827 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
828 return false;
830 return false;
833 /* We can not predict the probabilities of outgoing edges of bb. Set them
834 evenly and hope for the best. If UNLIKELY_EDGES is not null, distribute
835 even probability for all edges not mentioned in the set. These edges
836 are given PROB_VERY_UNLIKELY probability. */
838 static void
839 set_even_probabilities (basic_block bb,
840 hash_set<edge> *unlikely_edges = NULL)
842 unsigned nedges = 0;
843 edge e = NULL;
844 edge_iterator ei;
846 FOR_EACH_EDGE (e, ei, bb->succs)
847 if (!unlikely_executed_edge_p (e))
848 nedges ++;
850 /* Make the distribution even if all edges are unlikely. */
851 unsigned unlikely_count = unlikely_edges ? unlikely_edges->elements () : 0;
852 if (unlikely_count == nedges)
854 unlikely_edges = NULL;
855 unlikely_count = 0;
858 unsigned c = nedges - unlikely_count;
860 FOR_EACH_EDGE (e, ei, bb->succs)
861 if (!unlikely_executed_edge_p (e))
863 if (unlikely_edges != NULL && unlikely_edges->contains (e))
864 e->probability = profile_probability::very_unlikely ();
865 else
866 e->probability = profile_probability::guessed_always ()
867 .apply_scale (1, c);
869 else
870 e->probability = profile_probability::never ();
873 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
874 note if not already present. Remove now useless REG_BR_PRED notes. */
876 static void
877 combine_predictions_for_insn (rtx_insn *insn, basic_block bb)
879 rtx prob_note;
880 rtx *pnote;
881 rtx note;
882 int best_probability = PROB_EVEN;
883 enum br_predictor best_predictor = END_PREDICTORS;
884 int combined_probability = REG_BR_PROB_BASE / 2;
885 int d;
886 bool first_match = false;
887 bool found = false;
889 if (!can_predict_insn_p (insn))
891 set_even_probabilities (bb);
892 return;
895 prob_note = find_reg_note (insn, REG_BR_PROB, 0);
896 pnote = &REG_NOTES (insn);
897 if (dump_file)
898 fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
899 bb->index);
901 /* We implement "first match" heuristics and use probability guessed
902 by predictor with smallest index. */
903 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
904 if (REG_NOTE_KIND (note) == REG_BR_PRED)
906 enum br_predictor predictor = ((enum br_predictor)
907 INTVAL (XEXP (XEXP (note, 0), 0)));
908 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
910 found = true;
911 if (best_predictor > predictor
912 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
913 best_probability = probability, best_predictor = predictor;
915 d = (combined_probability * probability
916 + (REG_BR_PROB_BASE - combined_probability)
917 * (REG_BR_PROB_BASE - probability));
919 /* Use FP math to avoid overflows of 32bit integers. */
920 if (d == 0)
921 /* If one probability is 0% and one 100%, avoid division by zero. */
922 combined_probability = REG_BR_PROB_BASE / 2;
923 else
924 combined_probability = (((double) combined_probability) * probability
925 * REG_BR_PROB_BASE / d + 0.5);
928 /* Decide which heuristic to use. In case we didn't match anything,
929 use no_prediction heuristic, in case we did match, use either
930 first match or Dempster-Shaffer theory depending on the flags. */
932 if (best_predictor != END_PREDICTORS)
933 first_match = true;
935 if (!found)
936 dump_prediction (dump_file, PRED_NO_PREDICTION,
937 combined_probability, bb);
938 else
940 if (!first_match)
941 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
942 bb, !first_match ? REASON_NONE : REASON_IGNORED);
943 else
944 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
945 bb, first_match ? REASON_NONE : REASON_IGNORED);
948 if (first_match)
949 combined_probability = best_probability;
950 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
952 while (*pnote)
954 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
956 enum br_predictor predictor = ((enum br_predictor)
957 INTVAL (XEXP (XEXP (*pnote, 0), 0)));
958 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
960 dump_prediction (dump_file, predictor, probability, bb,
961 (!first_match || best_predictor == predictor)
962 ? REASON_NONE : REASON_IGNORED);
963 *pnote = XEXP (*pnote, 1);
965 else
966 pnote = &XEXP (*pnote, 1);
969 if (!prob_note)
971 add_int_reg_note (insn, REG_BR_PROB, combined_probability);
973 /* Save the prediction into CFG in case we are seeing non-degenerated
974 conditional jump. */
975 if (!single_succ_p (bb))
977 BRANCH_EDGE (bb)->probability
978 = profile_probability::from_reg_br_prob_base (combined_probability);
979 FALLTHRU_EDGE (bb)->probability
980 = BRANCH_EDGE (bb)->probability.invert ();
983 else if (!single_succ_p (bb))
985 int prob = XINT (prob_note, 0);
987 BRANCH_EDGE (bb)->probability
988 = profile_probability::from_reg_br_prob_base (prob);
989 FALLTHRU_EDGE (bb)->probability
990 = BRANCH_EDGE (bb)->probability.invert ();
992 else
993 single_succ_edge (bb)->probability = profile_probability::always ();
996 /* Edge prediction hash traits. */
998 struct predictor_hash: pointer_hash <edge_prediction>
1001 static inline hashval_t hash (const edge_prediction *);
1002 static inline bool equal (const edge_prediction *, const edge_prediction *);
1005 /* Calculate hash value of an edge prediction P based on predictor and
1006 normalized probability. */
1008 inline hashval_t
1009 predictor_hash::hash (const edge_prediction *p)
1011 inchash::hash hstate;
1012 hstate.add_int (p->ep_predictor);
1014 int prob = p->ep_probability;
1015 if (prob > REG_BR_PROB_BASE / 2)
1016 prob = REG_BR_PROB_BASE - prob;
1018 hstate.add_int (prob);
1020 return hstate.end ();
1023 /* Return true whether edge predictions P1 and P2 use the same predictor and
1024 have equal (or opposed probability). */
1026 inline bool
1027 predictor_hash::equal (const edge_prediction *p1, const edge_prediction *p2)
1029 return (p1->ep_predictor == p2->ep_predictor
1030 && (p1->ep_probability == p2->ep_probability
1031 || p1->ep_probability == REG_BR_PROB_BASE - p2->ep_probability));
1034 struct predictor_hash_traits: predictor_hash,
1035 typed_noop_remove <edge_prediction *> {};
1037 /* Return true if edge prediction P is not in DATA hash set. */
1039 static bool
1040 not_removed_prediction_p (edge_prediction *p, void *data)
1042 hash_set<edge_prediction *> *remove = (hash_set<edge_prediction *> *) data;
1043 return !remove->contains (p);
1046 /* Prune predictions for a basic block BB. Currently we do following
1047 clean-up steps:
1049 1) remove duplicate prediction that is guessed with the same probability
1050 (different than 1/2) to both edge
1051 2) remove duplicates for a prediction that belongs with the same probability
1052 to a single edge
1056 static void
1057 prune_predictions_for_bb (basic_block bb)
1059 edge_prediction **preds = bb_predictions->get (bb);
1061 if (preds)
1063 hash_table <predictor_hash_traits> s (13);
1064 hash_set <edge_prediction *> remove;
1066 /* Step 1: identify predictors that should be removed. */
1067 for (edge_prediction *pred = *preds; pred; pred = pred->ep_next)
1069 edge_prediction *existing = s.find (pred);
1070 if (existing)
1072 if (pred->ep_edge == existing->ep_edge
1073 && pred->ep_probability == existing->ep_probability)
1075 /* Remove a duplicate predictor. */
1076 dump_prediction (dump_file, pred->ep_predictor,
1077 pred->ep_probability, bb,
1078 REASON_SINGLE_EDGE_DUPLICATE, pred->ep_edge);
1080 remove.add (pred);
1082 else if (pred->ep_edge != existing->ep_edge
1083 && pred->ep_probability == existing->ep_probability
1084 && pred->ep_probability != REG_BR_PROB_BASE / 2)
1086 /* Remove both predictors as they predict the same
1087 for both edges. */
1088 dump_prediction (dump_file, existing->ep_predictor,
1089 pred->ep_probability, bb,
1090 REASON_EDGE_PAIR_DUPLICATE,
1091 existing->ep_edge);
1092 dump_prediction (dump_file, pred->ep_predictor,
1093 pred->ep_probability, bb,
1094 REASON_EDGE_PAIR_DUPLICATE,
1095 pred->ep_edge);
1097 remove.add (existing);
1098 remove.add (pred);
1102 edge_prediction **slot2 = s.find_slot (pred, INSERT);
1103 *slot2 = pred;
1106 /* Step 2: Remove predictors. */
1107 filter_predictions (preds, not_removed_prediction_p, &remove);
1111 /* Combine predictions into single probability and store them into CFG.
1112 Remove now useless prediction entries.
1113 If DRY_RUN is set, only produce dumps and do not modify profile. */
1115 static void
1116 combine_predictions_for_bb (basic_block bb, bool dry_run)
1118 int best_probability = PROB_EVEN;
1119 enum br_predictor best_predictor = END_PREDICTORS;
1120 int combined_probability = REG_BR_PROB_BASE / 2;
1121 int d;
1122 bool first_match = false;
1123 bool found = false;
1124 struct edge_prediction *pred;
1125 int nedges = 0;
1126 edge e, first = NULL, second = NULL;
1127 edge_iterator ei;
1129 FOR_EACH_EDGE (e, ei, bb->succs)
1130 if (!unlikely_executed_edge_p (e))
1132 nedges ++;
1133 if (first && !second)
1134 second = e;
1135 if (!first)
1136 first = e;
1138 else if (!e->probability.initialized_p ())
1139 e->probability = profile_probability::never ();
1141 /* When there is no successor or only one choice, prediction is easy.
1143 When we have a basic block with more than 2 successors, the situation
1144 is more complicated as DS theory cannot be used literally.
1145 More precisely, let's assume we predicted edge e1 with probability p1,
1146 thus: m1({b1}) = p1. As we're going to combine more than 2 edges, we
1147 need to find probability of e.g. m1({b2}), which we don't know.
1148 The only approximation is to equally distribute 1-p1 to all edges
1149 different from b1.
1151 According to numbers we've got from SPEC2006 benchark, there's only
1152 one interesting reliable predictor (noreturn call), which can be
1153 handled with a bit easier approach. */
1154 if (nedges != 2)
1156 hash_set<edge> unlikely_edges (4);
1158 /* Identify all edges that have a probability close to very unlikely.
1159 Doing the approach for very unlikely doesn't worth for doing as
1160 there's no such probability in SPEC2006 benchmark. */
1161 edge_prediction **preds = bb_predictions->get (bb);
1162 if (preds)
1163 for (pred = *preds; pred; pred = pred->ep_next)
1164 if (pred->ep_probability <= PROB_VERY_UNLIKELY)
1165 unlikely_edges.add (pred->ep_edge);
1167 if (!bb->count.initialized_p () && !dry_run)
1168 set_even_probabilities (bb, &unlikely_edges);
1169 clear_bb_predictions (bb);
1170 if (dump_file)
1172 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1173 if (unlikely_edges.elements () == 0)
1174 fprintf (dump_file,
1175 "%i edges in bb %i predicted to even probabilities\n",
1176 nedges, bb->index);
1177 else
1179 fprintf (dump_file,
1180 "%i edges in bb %i predicted with some unlikely edges\n",
1181 nedges, bb->index);
1182 FOR_EACH_EDGE (e, ei, bb->succs)
1183 if (!unlikely_executed_edge_p (e))
1184 dump_prediction (dump_file, PRED_COMBINED,
1185 e->probability.to_reg_br_prob_base (), bb, REASON_NONE, e);
1188 return;
1191 if (dump_file)
1192 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1194 prune_predictions_for_bb (bb);
1196 edge_prediction **preds = bb_predictions->get (bb);
1198 if (preds)
1200 /* We implement "first match" heuristics and use probability guessed
1201 by predictor with smallest index. */
1202 for (pred = *preds; pred; pred = pred->ep_next)
1204 enum br_predictor predictor = pred->ep_predictor;
1205 int probability = pred->ep_probability;
1207 if (pred->ep_edge != first)
1208 probability = REG_BR_PROB_BASE - probability;
1210 found = true;
1211 /* First match heuristics would be widly confused if we predicted
1212 both directions. */
1213 if (best_predictor > predictor
1214 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
1216 struct edge_prediction *pred2;
1217 int prob = probability;
1219 for (pred2 = (struct edge_prediction *) *preds;
1220 pred2; pred2 = pred2->ep_next)
1221 if (pred2 != pred && pred2->ep_predictor == pred->ep_predictor)
1223 int probability2 = pred2->ep_probability;
1225 if (pred2->ep_edge != first)
1226 probability2 = REG_BR_PROB_BASE - probability2;
1228 if ((probability < REG_BR_PROB_BASE / 2) !=
1229 (probability2 < REG_BR_PROB_BASE / 2))
1230 break;
1232 /* If the same predictor later gave better result, go for it! */
1233 if ((probability >= REG_BR_PROB_BASE / 2 && (probability2 > probability))
1234 || (probability <= REG_BR_PROB_BASE / 2 && (probability2 < probability)))
1235 prob = probability2;
1237 if (!pred2)
1238 best_probability = prob, best_predictor = predictor;
1241 d = (combined_probability * probability
1242 + (REG_BR_PROB_BASE - combined_probability)
1243 * (REG_BR_PROB_BASE - probability));
1245 /* Use FP math to avoid overflows of 32bit integers. */
1246 if (d == 0)
1247 /* If one probability is 0% and one 100%, avoid division by zero. */
1248 combined_probability = REG_BR_PROB_BASE / 2;
1249 else
1250 combined_probability = (((double) combined_probability)
1251 * probability
1252 * REG_BR_PROB_BASE / d + 0.5);
1256 /* Decide which heuristic to use. In case we didn't match anything,
1257 use no_prediction heuristic, in case we did match, use either
1258 first match or Dempster-Shaffer theory depending on the flags. */
1260 if (best_predictor != END_PREDICTORS)
1261 first_match = true;
1263 if (!found)
1264 dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb);
1265 else
1267 if (!first_match)
1268 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb,
1269 !first_match ? REASON_NONE : REASON_IGNORED);
1270 else
1271 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb,
1272 first_match ? REASON_NONE : REASON_IGNORED);
1275 if (first_match)
1276 combined_probability = best_probability;
1277 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
1279 if (preds)
1281 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next)
1283 enum br_predictor predictor = pred->ep_predictor;
1284 int probability = pred->ep_probability;
1286 dump_prediction (dump_file, predictor, probability, bb,
1287 (!first_match || best_predictor == predictor)
1288 ? REASON_NONE : REASON_IGNORED, pred->ep_edge);
1291 clear_bb_predictions (bb);
1293 if (!bb->count.initialized_p () && !dry_run)
1295 first->probability
1296 = profile_probability::from_reg_br_prob_base (combined_probability);
1297 second->probability = first->probability.invert ();
1301 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1302 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1304 T1 and T2 should be one of the following cases:
1305 1. T1 is SSA_NAME, T2 is NULL
1306 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1307 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1309 static tree
1310 strips_small_constant (tree t1, tree t2)
1312 tree ret = NULL;
1313 int value = 0;
1315 if (!t1)
1316 return NULL;
1317 else if (TREE_CODE (t1) == SSA_NAME)
1318 ret = t1;
1319 else if (tree_fits_shwi_p (t1))
1320 value = tree_to_shwi (t1);
1321 else
1322 return NULL;
1324 if (!t2)
1325 return ret;
1326 else if (tree_fits_shwi_p (t2))
1327 value = tree_to_shwi (t2);
1328 else if (TREE_CODE (t2) == SSA_NAME)
1330 if (ret)
1331 return NULL;
1332 else
1333 ret = t2;
1336 if (value <= 4 && value >= -4)
1337 return ret;
1338 else
1339 return NULL;
1342 /* Return the SSA_NAME in T or T's operands.
1343 Return NULL if SSA_NAME cannot be found. */
1345 static tree
1346 get_base_value (tree t)
1348 if (TREE_CODE (t) == SSA_NAME)
1349 return t;
1351 if (!BINARY_CLASS_P (t))
1352 return NULL;
1354 switch (TREE_OPERAND_LENGTH (t))
1356 case 1:
1357 return strips_small_constant (TREE_OPERAND (t, 0), NULL);
1358 case 2:
1359 return strips_small_constant (TREE_OPERAND (t, 0),
1360 TREE_OPERAND (t, 1));
1361 default:
1362 return NULL;
1366 /* Check the compare STMT in LOOP. If it compares an induction
1367 variable to a loop invariant, return true, and save
1368 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1369 Otherwise return false and set LOOP_INVAIANT to NULL. */
1371 static bool
1372 is_comparison_with_loop_invariant_p (gcond *stmt, struct loop *loop,
1373 tree *loop_invariant,
1374 enum tree_code *compare_code,
1375 tree *loop_step,
1376 tree *loop_iv_base)
1378 tree op0, op1, bound, base;
1379 affine_iv iv0, iv1;
1380 enum tree_code code;
1381 tree step;
1383 code = gimple_cond_code (stmt);
1384 *loop_invariant = NULL;
1386 switch (code)
1388 case GT_EXPR:
1389 case GE_EXPR:
1390 case NE_EXPR:
1391 case LT_EXPR:
1392 case LE_EXPR:
1393 case EQ_EXPR:
1394 break;
1396 default:
1397 return false;
1400 op0 = gimple_cond_lhs (stmt);
1401 op1 = gimple_cond_rhs (stmt);
1403 if ((TREE_CODE (op0) != SSA_NAME && TREE_CODE (op0) != INTEGER_CST)
1404 || (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op1) != INTEGER_CST))
1405 return false;
1406 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, true))
1407 return false;
1408 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, true))
1409 return false;
1410 if (TREE_CODE (iv0.step) != INTEGER_CST
1411 || TREE_CODE (iv1.step) != INTEGER_CST)
1412 return false;
1413 if ((integer_zerop (iv0.step) && integer_zerop (iv1.step))
1414 || (!integer_zerop (iv0.step) && !integer_zerop (iv1.step)))
1415 return false;
1417 if (integer_zerop (iv0.step))
1419 if (code != NE_EXPR && code != EQ_EXPR)
1420 code = invert_tree_comparison (code, false);
1421 bound = iv0.base;
1422 base = iv1.base;
1423 if (tree_fits_shwi_p (iv1.step))
1424 step = iv1.step;
1425 else
1426 return false;
1428 else
1430 bound = iv1.base;
1431 base = iv0.base;
1432 if (tree_fits_shwi_p (iv0.step))
1433 step = iv0.step;
1434 else
1435 return false;
1438 if (TREE_CODE (bound) != INTEGER_CST)
1439 bound = get_base_value (bound);
1440 if (!bound)
1441 return false;
1442 if (TREE_CODE (base) != INTEGER_CST)
1443 base = get_base_value (base);
1444 if (!base)
1445 return false;
1447 *loop_invariant = bound;
1448 *compare_code = code;
1449 *loop_step = step;
1450 *loop_iv_base = base;
1451 return true;
1454 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1456 static bool
1457 expr_coherent_p (tree t1, tree t2)
1459 gimple *stmt;
1460 tree ssa_name_1 = NULL;
1461 tree ssa_name_2 = NULL;
1463 gcc_assert (TREE_CODE (t1) == SSA_NAME || TREE_CODE (t1) == INTEGER_CST);
1464 gcc_assert (TREE_CODE (t2) == SSA_NAME || TREE_CODE (t2) == INTEGER_CST);
1466 if (t1 == t2)
1467 return true;
1469 if (TREE_CODE (t1) == INTEGER_CST && TREE_CODE (t2) == INTEGER_CST)
1470 return true;
1471 if (TREE_CODE (t1) == INTEGER_CST || TREE_CODE (t2) == INTEGER_CST)
1472 return false;
1474 /* Check to see if t1 is expressed/defined with t2. */
1475 stmt = SSA_NAME_DEF_STMT (t1);
1476 gcc_assert (stmt != NULL);
1477 if (is_gimple_assign (stmt))
1479 ssa_name_1 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1480 if (ssa_name_1 && ssa_name_1 == t2)
1481 return true;
1484 /* Check to see if t2 is expressed/defined with t1. */
1485 stmt = SSA_NAME_DEF_STMT (t2);
1486 gcc_assert (stmt != NULL);
1487 if (is_gimple_assign (stmt))
1489 ssa_name_2 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1490 if (ssa_name_2 && ssa_name_2 == t1)
1491 return true;
1494 /* Compare if t1 and t2's def_stmts are identical. */
1495 if (ssa_name_2 != NULL && ssa_name_1 == ssa_name_2)
1496 return true;
1497 else
1498 return false;
1501 /* Return true if E is predicted by one of loop heuristics. */
1503 static bool
1504 predicted_by_loop_heuristics_p (basic_block bb)
1506 struct edge_prediction *i;
1507 edge_prediction **preds = bb_predictions->get (bb);
1509 if (!preds)
1510 return false;
1512 for (i = *preds; i; i = i->ep_next)
1513 if (i->ep_predictor == PRED_LOOP_ITERATIONS_GUESSED
1514 || i->ep_predictor == PRED_LOOP_ITERATIONS_MAX
1515 || i->ep_predictor == PRED_LOOP_ITERATIONS
1516 || i->ep_predictor == PRED_LOOP_EXIT
1517 || i->ep_predictor == PRED_LOOP_EXIT_WITH_RECURSION
1518 || i->ep_predictor == PRED_LOOP_EXTRA_EXIT)
1519 return true;
1520 return false;
1523 /* Predict branch probability of BB when BB contains a branch that compares
1524 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1525 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1527 E.g.
1528 for (int i = 0; i < bound; i++) {
1529 if (i < bound - 2)
1530 computation_1();
1531 else
1532 computation_2();
1535 In this loop, we will predict the branch inside the loop to be taken. */
1537 static void
1538 predict_iv_comparison (struct loop *loop, basic_block bb,
1539 tree loop_bound_var,
1540 tree loop_iv_base_var,
1541 enum tree_code loop_bound_code,
1542 int loop_bound_step)
1544 gimple *stmt;
1545 tree compare_var, compare_base;
1546 enum tree_code compare_code;
1547 tree compare_step_var;
1548 edge then_edge;
1549 edge_iterator ei;
1551 if (predicted_by_loop_heuristics_p (bb))
1552 return;
1554 stmt = last_stmt (bb);
1555 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1556 return;
1557 if (!is_comparison_with_loop_invariant_p (as_a <gcond *> (stmt),
1558 loop, &compare_var,
1559 &compare_code,
1560 &compare_step_var,
1561 &compare_base))
1562 return;
1564 /* Find the taken edge. */
1565 FOR_EACH_EDGE (then_edge, ei, bb->succs)
1566 if (then_edge->flags & EDGE_TRUE_VALUE)
1567 break;
1569 /* When comparing an IV to a loop invariant, NE is more likely to be
1570 taken while EQ is more likely to be not-taken. */
1571 if (compare_code == NE_EXPR)
1573 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1574 return;
1576 else if (compare_code == EQ_EXPR)
1578 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1579 return;
1582 if (!expr_coherent_p (loop_iv_base_var, compare_base))
1583 return;
1585 /* If loop bound, base and compare bound are all constants, we can
1586 calculate the probability directly. */
1587 if (tree_fits_shwi_p (loop_bound_var)
1588 && tree_fits_shwi_p (compare_var)
1589 && tree_fits_shwi_p (compare_base))
1591 int probability;
1592 bool overflow, overall_overflow = false;
1593 widest_int compare_count, tem;
1595 /* (loop_bound - base) / compare_step */
1596 tem = wi::sub (wi::to_widest (loop_bound_var),
1597 wi::to_widest (compare_base), SIGNED, &overflow);
1598 overall_overflow |= overflow;
1599 widest_int loop_count = wi::div_trunc (tem,
1600 wi::to_widest (compare_step_var),
1601 SIGNED, &overflow);
1602 overall_overflow |= overflow;
1604 if (!wi::neg_p (wi::to_widest (compare_step_var))
1605 ^ (compare_code == LT_EXPR || compare_code == LE_EXPR))
1607 /* (loop_bound - compare_bound) / compare_step */
1608 tem = wi::sub (wi::to_widest (loop_bound_var),
1609 wi::to_widest (compare_var), SIGNED, &overflow);
1610 overall_overflow |= overflow;
1611 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1612 SIGNED, &overflow);
1613 overall_overflow |= overflow;
1615 else
1617 /* (compare_bound - base) / compare_step */
1618 tem = wi::sub (wi::to_widest (compare_var),
1619 wi::to_widest (compare_base), SIGNED, &overflow);
1620 overall_overflow |= overflow;
1621 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1622 SIGNED, &overflow);
1623 overall_overflow |= overflow;
1625 if (compare_code == LE_EXPR || compare_code == GE_EXPR)
1626 ++compare_count;
1627 if (loop_bound_code == LE_EXPR || loop_bound_code == GE_EXPR)
1628 ++loop_count;
1629 if (wi::neg_p (compare_count))
1630 compare_count = 0;
1631 if (wi::neg_p (loop_count))
1632 loop_count = 0;
1633 if (loop_count == 0)
1634 probability = 0;
1635 else if (wi::cmps (compare_count, loop_count) == 1)
1636 probability = REG_BR_PROB_BASE;
1637 else
1639 tem = compare_count * REG_BR_PROB_BASE;
1640 tem = wi::udiv_trunc (tem, loop_count);
1641 probability = tem.to_uhwi ();
1644 /* FIXME: The branch prediction seems broken. It has only 20% hitrate. */
1645 if (!overall_overflow)
1646 predict_edge (then_edge, PRED_LOOP_IV_COMPARE, probability);
1648 return;
1651 if (expr_coherent_p (loop_bound_var, compare_var))
1653 if ((loop_bound_code == LT_EXPR || loop_bound_code == LE_EXPR)
1654 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1655 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1656 else if ((loop_bound_code == GT_EXPR || loop_bound_code == GE_EXPR)
1657 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1658 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1659 else if (loop_bound_code == NE_EXPR)
1661 /* If the loop backedge condition is "(i != bound)", we do
1662 the comparison based on the step of IV:
1663 * step < 0 : backedge condition is like (i > bound)
1664 * step > 0 : backedge condition is like (i < bound) */
1665 gcc_assert (loop_bound_step != 0);
1666 if (loop_bound_step > 0
1667 && (compare_code == LT_EXPR
1668 || compare_code == LE_EXPR))
1669 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1670 else if (loop_bound_step < 0
1671 && (compare_code == GT_EXPR
1672 || compare_code == GE_EXPR))
1673 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1674 else
1675 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1677 else
1678 /* The branch is predicted not-taken if loop_bound_code is
1679 opposite with compare_code. */
1680 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1682 else if (expr_coherent_p (loop_iv_base_var, compare_var))
1684 /* For cases like:
1685 for (i = s; i < h; i++)
1686 if (i > s + 2) ....
1687 The branch should be predicted taken. */
1688 if (loop_bound_step > 0
1689 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1690 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1691 else if (loop_bound_step < 0
1692 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1693 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1694 else
1695 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1699 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1700 exits are resulted from short-circuit conditions that will generate an
1701 if_tmp. E.g.:
1703 if (foo() || global > 10)
1704 break;
1706 This will be translated into:
1708 BB3:
1709 loop header...
1710 BB4:
1711 if foo() goto BB6 else goto BB5
1712 BB5:
1713 if global > 10 goto BB6 else goto BB7
1714 BB6:
1715 goto BB7
1716 BB7:
1717 iftmp = (PHI 0(BB5), 1(BB6))
1718 if iftmp == 1 goto BB8 else goto BB3
1719 BB8:
1720 outside of the loop...
1722 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1723 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1724 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1725 exits to predict them using PRED_LOOP_EXTRA_EXIT. */
1727 static void
1728 predict_extra_loop_exits (edge exit_edge)
1730 unsigned i;
1731 bool check_value_one;
1732 gimple *lhs_def_stmt;
1733 gphi *phi_stmt;
1734 tree cmp_rhs, cmp_lhs;
1735 gimple *last;
1736 gcond *cmp_stmt;
1738 last = last_stmt (exit_edge->src);
1739 if (!last)
1740 return;
1741 cmp_stmt = dyn_cast <gcond *> (last);
1742 if (!cmp_stmt)
1743 return;
1745 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1746 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1747 if (!TREE_CONSTANT (cmp_rhs)
1748 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1749 return;
1750 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1751 return;
1753 /* If check_value_one is true, only the phi_args with value '1' will lead
1754 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1755 loop exit. */
1756 check_value_one = (((integer_onep (cmp_rhs))
1757 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1758 ^ ((exit_edge->flags & EDGE_TRUE_VALUE) != 0));
1760 lhs_def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1761 if (!lhs_def_stmt)
1762 return;
1764 phi_stmt = dyn_cast <gphi *> (lhs_def_stmt);
1765 if (!phi_stmt)
1766 return;
1768 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1770 edge e1;
1771 edge_iterator ei;
1772 tree val = gimple_phi_arg_def (phi_stmt, i);
1773 edge e = gimple_phi_arg_edge (phi_stmt, i);
1775 if (!TREE_CONSTANT (val) || !(integer_zerop (val) || integer_onep (val)))
1776 continue;
1777 if ((check_value_one ^ integer_onep (val)) == 1)
1778 continue;
1779 if (EDGE_COUNT (e->src->succs) != 1)
1781 predict_paths_leading_to_edge (e, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1782 continue;
1785 FOR_EACH_EDGE (e1, ei, e->src->preds)
1786 predict_paths_leading_to_edge (e1, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1791 /* Predict edge probabilities by exploiting loop structure. */
1793 static void
1794 predict_loops (void)
1796 struct loop *loop;
1797 basic_block bb;
1798 hash_set <struct loop *> with_recursion(10);
1800 FOR_EACH_BB_FN (bb, cfun)
1802 gimple_stmt_iterator gsi;
1803 tree decl;
1805 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1806 if (is_gimple_call (gsi_stmt (gsi))
1807 && (decl = gimple_call_fndecl (gsi_stmt (gsi))) != NULL
1808 && recursive_call_p (current_function_decl, decl))
1810 loop = bb->loop_father;
1811 while (loop && !with_recursion.add (loop))
1812 loop = loop_outer (loop);
1816 /* Try to predict out blocks in a loop that are not part of a
1817 natural loop. */
1818 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1820 basic_block bb, *bbs;
1821 unsigned j, n_exits = 0;
1822 vec<edge> exits;
1823 struct tree_niter_desc niter_desc;
1824 edge ex;
1825 struct nb_iter_bound *nb_iter;
1826 enum tree_code loop_bound_code = ERROR_MARK;
1827 tree loop_bound_step = NULL;
1828 tree loop_bound_var = NULL;
1829 tree loop_iv_base = NULL;
1830 gcond *stmt = NULL;
1831 bool recursion = with_recursion.contains (loop);
1833 exits = get_loop_exit_edges (loop);
1834 FOR_EACH_VEC_ELT (exits, j, ex)
1835 if (!unlikely_executed_edge_p (ex) && !(ex->flags & EDGE_ABNORMAL_CALL))
1836 n_exits ++;
1837 if (!n_exits)
1839 exits.release ();
1840 continue;
1843 if (dump_file && (dump_flags & TDF_DETAILS))
1844 fprintf (dump_file, "Predicting loop %i%s with %i exits.\n",
1845 loop->num, recursion ? " (with recursion)":"", n_exits);
1846 if (dump_file && (dump_flags & TDF_DETAILS)
1847 && max_loop_iterations_int (loop) >= 0)
1849 fprintf (dump_file,
1850 "Loop %d iterates at most %i times.\n", loop->num,
1851 (int)max_loop_iterations_int (loop));
1853 if (dump_file && (dump_flags & TDF_DETAILS)
1854 && likely_max_loop_iterations_int (loop) >= 0)
1856 fprintf (dump_file, "Loop %d likely iterates at most %i times.\n",
1857 loop->num, (int)likely_max_loop_iterations_int (loop));
1860 FOR_EACH_VEC_ELT (exits, j, ex)
1862 tree niter = NULL;
1863 HOST_WIDE_INT nitercst;
1864 int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS);
1865 int probability;
1866 enum br_predictor predictor;
1867 widest_int nit;
1869 if (unlikely_executed_edge_p (ex)
1870 || (ex->flags & EDGE_ABNORMAL_CALL))
1871 continue;
1872 /* Loop heuristics do not expect exit conditional to be inside
1873 inner loop. We predict from innermost to outermost loop. */
1874 if (predicted_by_loop_heuristics_p (ex->src))
1876 if (dump_file && (dump_flags & TDF_DETAILS))
1877 fprintf (dump_file, "Skipping exit %i->%i because "
1878 "it is already predicted.\n",
1879 ex->src->index, ex->dest->index);
1880 continue;
1882 predict_extra_loop_exits (ex);
1884 if (number_of_iterations_exit (loop, ex, &niter_desc, false, false))
1885 niter = niter_desc.niter;
1886 if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST)
1887 niter = loop_niter_by_eval (loop, ex);
1888 if (dump_file && (dump_flags & TDF_DETAILS)
1889 && TREE_CODE (niter) == INTEGER_CST)
1891 fprintf (dump_file, "Exit %i->%i %d iterates ",
1892 ex->src->index, ex->dest->index,
1893 loop->num);
1894 print_generic_expr (dump_file, niter, TDF_SLIM);
1895 fprintf (dump_file, " times.\n");
1898 if (TREE_CODE (niter) == INTEGER_CST)
1900 if (tree_fits_uhwi_p (niter)
1901 && max
1902 && compare_tree_int (niter, max - 1) == -1)
1903 nitercst = tree_to_uhwi (niter) + 1;
1904 else
1905 nitercst = max;
1906 predictor = PRED_LOOP_ITERATIONS;
1908 /* If we have just one exit and we can derive some information about
1909 the number of iterations of the loop from the statements inside
1910 the loop, use it to predict this exit. */
1911 else if (n_exits == 1
1912 && estimated_stmt_executions (loop, &nit))
1914 if (wi::gtu_p (nit, max))
1915 nitercst = max;
1916 else
1917 nitercst = nit.to_shwi ();
1918 predictor = PRED_LOOP_ITERATIONS_GUESSED;
1920 /* If we have likely upper bound, trust it for very small iteration
1921 counts. Such loops would otherwise get mispredicted by standard
1922 LOOP_EXIT heuristics. */
1923 else if (n_exits == 1
1924 && likely_max_stmt_executions (loop, &nit)
1925 && wi::ltu_p (nit,
1926 RDIV (REG_BR_PROB_BASE,
1927 REG_BR_PROB_BASE
1928 - predictor_info
1929 [recursion
1930 ? PRED_LOOP_EXIT_WITH_RECURSION
1931 : PRED_LOOP_EXIT].hitrate)))
1933 nitercst = nit.to_shwi ();
1934 predictor = PRED_LOOP_ITERATIONS_MAX;
1936 else
1938 if (dump_file && (dump_flags & TDF_DETAILS))
1939 fprintf (dump_file, "Nothing known about exit %i->%i.\n",
1940 ex->src->index, ex->dest->index);
1941 continue;
1944 if (dump_file && (dump_flags & TDF_DETAILS))
1945 fprintf (dump_file, "Recording prediction to %i iterations by %s.\n",
1946 (int)nitercst, predictor_info[predictor].name);
1947 /* If the prediction for number of iterations is zero, do not
1948 predict the exit edges. */
1949 if (nitercst == 0)
1950 continue;
1952 probability = RDIV (REG_BR_PROB_BASE, nitercst);
1953 predict_edge (ex, predictor, probability);
1955 exits.release ();
1957 /* Find information about loop bound variables. */
1958 for (nb_iter = loop->bounds; nb_iter;
1959 nb_iter = nb_iter->next)
1960 if (nb_iter->stmt
1961 && gimple_code (nb_iter->stmt) == GIMPLE_COND)
1963 stmt = as_a <gcond *> (nb_iter->stmt);
1964 break;
1966 if (!stmt && last_stmt (loop->header)
1967 && gimple_code (last_stmt (loop->header)) == GIMPLE_COND)
1968 stmt = as_a <gcond *> (last_stmt (loop->header));
1969 if (stmt)
1970 is_comparison_with_loop_invariant_p (stmt, loop,
1971 &loop_bound_var,
1972 &loop_bound_code,
1973 &loop_bound_step,
1974 &loop_iv_base);
1976 bbs = get_loop_body (loop);
1978 for (j = 0; j < loop->num_nodes; j++)
1980 edge e;
1981 edge_iterator ei;
1983 bb = bbs[j];
1985 /* Bypass loop heuristics on continue statement. These
1986 statements construct loops via "non-loop" constructs
1987 in the source language and are better to be handled
1988 separately. */
1989 if (predicted_by_p (bb, PRED_CONTINUE))
1991 if (dump_file && (dump_flags & TDF_DETAILS))
1992 fprintf (dump_file, "BB %i predicted by continue.\n",
1993 bb->index);
1994 continue;
1997 /* If we already used more reliable loop exit predictors, do not
1998 bother with PRED_LOOP_EXIT. */
1999 if (!predicted_by_loop_heuristics_p (bb))
2001 /* For loop with many exits we don't want to predict all exits
2002 with the pretty large probability, because if all exits are
2003 considered in row, the loop would be predicted to iterate
2004 almost never. The code to divide probability by number of
2005 exits is very rough. It should compute the number of exits
2006 taken in each patch through function (not the overall number
2007 of exits that might be a lot higher for loops with wide switch
2008 statements in them) and compute n-th square root.
2010 We limit the minimal probability by 2% to avoid
2011 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
2012 as this was causing regression in perl benchmark containing such
2013 a wide loop. */
2015 int probability = ((REG_BR_PROB_BASE
2016 - predictor_info
2017 [recursion
2018 ? PRED_LOOP_EXIT_WITH_RECURSION
2019 : PRED_LOOP_EXIT].hitrate)
2020 / n_exits);
2021 if (probability < HITRATE (2))
2022 probability = HITRATE (2);
2023 FOR_EACH_EDGE (e, ei, bb->succs)
2024 if (e->dest->index < NUM_FIXED_BLOCKS
2025 || !flow_bb_inside_loop_p (loop, e->dest))
2027 if (dump_file && (dump_flags & TDF_DETAILS))
2028 fprintf (dump_file,
2029 "Predicting exit %i->%i with prob %i.\n",
2030 e->src->index, e->dest->index, probability);
2031 predict_edge (e,
2032 recursion ? PRED_LOOP_EXIT_WITH_RECURSION
2033 : PRED_LOOP_EXIT, probability);
2036 if (loop_bound_var)
2037 predict_iv_comparison (loop, bb, loop_bound_var, loop_iv_base,
2038 loop_bound_code,
2039 tree_to_shwi (loop_bound_step));
2042 /* In the following code
2043 for (loop1)
2044 if (cond)
2045 for (loop2)
2046 body;
2047 guess that cond is unlikely. */
2048 if (loop_outer (loop)->num)
2050 basic_block bb = NULL;
2051 edge preheader_edge = loop_preheader_edge (loop);
2053 if (single_pred_p (preheader_edge->src)
2054 && single_succ_p (preheader_edge->src))
2055 preheader_edge = single_pred_edge (preheader_edge->src);
2057 gimple *stmt = last_stmt (preheader_edge->src);
2058 /* Pattern match fortran loop preheader:
2059 _16 = BUILTIN_EXPECT (_15, 1, PRED_FORTRAN_LOOP_PREHEADER);
2060 _17 = (logical(kind=4)) _16;
2061 if (_17 != 0)
2062 goto <bb 11>;
2063 else
2064 goto <bb 13>;
2066 Loop guard branch prediction says nothing about duplicated loop
2067 headers produced by fortran frontend and in this case we want
2068 to predict paths leading to this preheader. */
2070 if (stmt
2071 && gimple_code (stmt) == GIMPLE_COND
2072 && gimple_cond_code (stmt) == NE_EXPR
2073 && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
2074 && integer_zerop (gimple_cond_rhs (stmt)))
2076 gimple *call_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
2077 if (gimple_code (call_stmt) == GIMPLE_ASSIGN
2078 && gimple_expr_code (call_stmt) == NOP_EXPR
2079 && TREE_CODE (gimple_assign_rhs1 (call_stmt)) == SSA_NAME)
2080 call_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (call_stmt));
2081 if (gimple_call_internal_p (call_stmt, IFN_BUILTIN_EXPECT)
2082 && TREE_CODE (gimple_call_arg (call_stmt, 2)) == INTEGER_CST
2083 && tree_fits_uhwi_p (gimple_call_arg (call_stmt, 2))
2084 && tree_to_uhwi (gimple_call_arg (call_stmt, 2))
2085 == PRED_FORTRAN_LOOP_PREHEADER)
2086 bb = preheader_edge->src;
2088 if (!bb)
2090 if (!dominated_by_p (CDI_DOMINATORS,
2091 loop_outer (loop)->latch, loop->header))
2092 predict_paths_leading_to_edge (loop_preheader_edge (loop),
2093 recursion
2094 ? PRED_LOOP_GUARD_WITH_RECURSION
2095 : PRED_LOOP_GUARD,
2096 NOT_TAKEN,
2097 loop_outer (loop));
2099 else
2101 if (!dominated_by_p (CDI_DOMINATORS,
2102 loop_outer (loop)->latch, bb))
2103 predict_paths_leading_to (bb,
2104 recursion
2105 ? PRED_LOOP_GUARD_WITH_RECURSION
2106 : PRED_LOOP_GUARD,
2107 NOT_TAKEN,
2108 loop_outer (loop));
2112 /* Free basic blocks from get_loop_body. */
2113 free (bbs);
2117 /* Attempt to predict probabilities of BB outgoing edges using local
2118 properties. */
2119 static void
2120 bb_estimate_probability_locally (basic_block bb)
2122 rtx_insn *last_insn = BB_END (bb);
2123 rtx cond;
2125 if (! can_predict_insn_p (last_insn))
2126 return;
2127 cond = get_condition (last_insn, NULL, false, false);
2128 if (! cond)
2129 return;
2131 /* Try "pointer heuristic."
2132 A comparison ptr == 0 is predicted as false.
2133 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2134 if (COMPARISON_P (cond)
2135 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
2136 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
2138 if (GET_CODE (cond) == EQ)
2139 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
2140 else if (GET_CODE (cond) == NE)
2141 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
2143 else
2145 /* Try "opcode heuristic."
2146 EQ tests are usually false and NE tests are usually true. Also,
2147 most quantities are positive, so we can make the appropriate guesses
2148 about signed comparisons against zero. */
2149 switch (GET_CODE (cond))
2151 case CONST_INT:
2152 /* Unconditional branch. */
2153 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
2154 cond == const0_rtx ? NOT_TAKEN : TAKEN);
2155 break;
2157 case EQ:
2158 case UNEQ:
2159 /* Floating point comparisons appears to behave in a very
2160 unpredictable way because of special role of = tests in
2161 FP code. */
2162 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2164 /* Comparisons with 0 are often used for booleans and there is
2165 nothing useful to predict about them. */
2166 else if (XEXP (cond, 1) == const0_rtx
2167 || XEXP (cond, 0) == const0_rtx)
2169 else
2170 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
2171 break;
2173 case NE:
2174 case LTGT:
2175 /* Floating point comparisons appears to behave in a very
2176 unpredictable way because of special role of = tests in
2177 FP code. */
2178 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2180 /* Comparisons with 0 are often used for booleans and there is
2181 nothing useful to predict about them. */
2182 else if (XEXP (cond, 1) == const0_rtx
2183 || XEXP (cond, 0) == const0_rtx)
2185 else
2186 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
2187 break;
2189 case ORDERED:
2190 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
2191 break;
2193 case UNORDERED:
2194 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
2195 break;
2197 case LE:
2198 case LT:
2199 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2200 || XEXP (cond, 1) == constm1_rtx)
2201 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
2202 break;
2204 case GE:
2205 case GT:
2206 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2207 || XEXP (cond, 1) == constm1_rtx)
2208 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
2209 break;
2211 default:
2212 break;
2216 /* Set edge->probability for each successor edge of BB. */
2217 void
2218 guess_outgoing_edge_probabilities (basic_block bb)
2220 bb_estimate_probability_locally (bb);
2221 combine_predictions_for_insn (BB_END (bb), bb);
2224 static tree expr_expected_value (tree, bitmap, enum br_predictor *predictor);
2226 /* Helper function for expr_expected_value. */
2228 static tree
2229 expr_expected_value_1 (tree type, tree op0, enum tree_code code,
2230 tree op1, bitmap visited, enum br_predictor *predictor)
2232 gimple *def;
2234 if (predictor)
2235 *predictor = PRED_UNCONDITIONAL;
2237 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
2239 if (TREE_CONSTANT (op0))
2240 return op0;
2242 if (code == IMAGPART_EXPR)
2244 if (TREE_CODE (TREE_OPERAND (op0, 0)) == SSA_NAME)
2246 def = SSA_NAME_DEF_STMT (TREE_OPERAND (op0, 0));
2247 if (is_gimple_call (def)
2248 && gimple_call_internal_p (def)
2249 && (gimple_call_internal_fn (def)
2250 == IFN_ATOMIC_COMPARE_EXCHANGE))
2252 /* Assume that any given atomic operation has low contention,
2253 and thus the compare-and-swap operation succeeds. */
2254 if (predictor)
2255 *predictor = PRED_COMPARE_AND_SWAP;
2256 return build_one_cst (TREE_TYPE (op0));
2261 if (code != SSA_NAME)
2262 return NULL_TREE;
2264 def = SSA_NAME_DEF_STMT (op0);
2266 /* If we were already here, break the infinite cycle. */
2267 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (op0)))
2268 return NULL;
2270 if (gimple_code (def) == GIMPLE_PHI)
2272 /* All the arguments of the PHI node must have the same constant
2273 length. */
2274 int i, n = gimple_phi_num_args (def);
2275 tree val = NULL, new_val;
2277 for (i = 0; i < n; i++)
2279 tree arg = PHI_ARG_DEF (def, i);
2280 enum br_predictor predictor2;
2282 /* If this PHI has itself as an argument, we cannot
2283 determine the string length of this argument. However,
2284 if we can find an expected constant value for the other
2285 PHI args then we can still be sure that this is
2286 likely a constant. So be optimistic and just
2287 continue with the next argument. */
2288 if (arg == PHI_RESULT (def))
2289 continue;
2291 new_val = expr_expected_value (arg, visited, &predictor2);
2293 /* It is difficult to combine value predictors. Simply assume
2294 that later predictor is weaker and take its prediction. */
2295 if (predictor && *predictor < predictor2)
2296 *predictor = predictor2;
2297 if (!new_val)
2298 return NULL;
2299 if (!val)
2300 val = new_val;
2301 else if (!operand_equal_p (val, new_val, false))
2302 return NULL;
2304 return val;
2306 if (is_gimple_assign (def))
2308 if (gimple_assign_lhs (def) != op0)
2309 return NULL;
2311 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)),
2312 gimple_assign_rhs1 (def),
2313 gimple_assign_rhs_code (def),
2314 gimple_assign_rhs2 (def),
2315 visited, predictor);
2318 if (is_gimple_call (def))
2320 tree decl = gimple_call_fndecl (def);
2321 if (!decl)
2323 if (gimple_call_internal_p (def)
2324 && gimple_call_internal_fn (def) == IFN_BUILTIN_EXPECT)
2326 gcc_assert (gimple_call_num_args (def) == 3);
2327 tree val = gimple_call_arg (def, 0);
2328 if (TREE_CONSTANT (val))
2329 return val;
2330 if (predictor)
2332 tree val2 = gimple_call_arg (def, 2);
2333 gcc_assert (TREE_CODE (val2) == INTEGER_CST
2334 && tree_fits_uhwi_p (val2)
2335 && tree_to_uhwi (val2) < END_PREDICTORS);
2336 *predictor = (enum br_predictor) tree_to_uhwi (val2);
2338 return gimple_call_arg (def, 1);
2340 return NULL;
2342 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2343 switch (DECL_FUNCTION_CODE (decl))
2345 case BUILT_IN_EXPECT:
2347 tree val;
2348 if (gimple_call_num_args (def) != 2)
2349 return NULL;
2350 val = gimple_call_arg (def, 0);
2351 if (TREE_CONSTANT (val))
2352 return val;
2353 if (predictor)
2354 *predictor = PRED_BUILTIN_EXPECT;
2355 return gimple_call_arg (def, 1);
2358 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
2359 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
2360 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
2361 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
2362 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
2363 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
2364 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
2365 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
2366 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
2367 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
2368 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
2369 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
2370 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
2371 /* Assume that any given atomic operation has low contention,
2372 and thus the compare-and-swap operation succeeds. */
2373 if (predictor)
2374 *predictor = PRED_COMPARE_AND_SWAP;
2375 return boolean_true_node;
2376 default:
2377 break;
2381 return NULL;
2384 if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
2386 tree res;
2387 enum br_predictor predictor2;
2388 op0 = expr_expected_value (op0, visited, predictor);
2389 if (!op0)
2390 return NULL;
2391 op1 = expr_expected_value (op1, visited, &predictor2);
2392 if (predictor && *predictor < predictor2)
2393 *predictor = predictor2;
2394 if (!op1)
2395 return NULL;
2396 res = fold_build2 (code, type, op0, op1);
2397 if (TREE_CONSTANT (res))
2398 return res;
2399 return NULL;
2401 if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
2403 tree res;
2404 op0 = expr_expected_value (op0, visited, predictor);
2405 if (!op0)
2406 return NULL;
2407 res = fold_build1 (code, type, op0);
2408 if (TREE_CONSTANT (res))
2409 return res;
2410 return NULL;
2412 return NULL;
2415 /* Return constant EXPR will likely have at execution time, NULL if unknown.
2416 The function is used by builtin_expect branch predictor so the evidence
2417 must come from this construct and additional possible constant folding.
2419 We may want to implement more involved value guess (such as value range
2420 propagation based prediction), but such tricks shall go to new
2421 implementation. */
2423 static tree
2424 expr_expected_value (tree expr, bitmap visited,
2425 enum br_predictor *predictor)
2427 enum tree_code code;
2428 tree op0, op1;
2430 if (TREE_CONSTANT (expr))
2432 if (predictor)
2433 *predictor = PRED_UNCONDITIONAL;
2434 return expr;
2437 extract_ops_from_tree (expr, &code, &op0, &op1);
2438 return expr_expected_value_1 (TREE_TYPE (expr),
2439 op0, code, op1, visited, predictor);
2442 /* Predict using opcode of the last statement in basic block. */
2443 static void
2444 tree_predict_by_opcode (basic_block bb)
2446 gimple *stmt = last_stmt (bb);
2447 edge then_edge;
2448 tree op0, op1;
2449 tree type;
2450 tree val;
2451 enum tree_code cmp;
2452 edge_iterator ei;
2453 enum br_predictor predictor;
2455 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
2456 return;
2457 FOR_EACH_EDGE (then_edge, ei, bb->succs)
2458 if (then_edge->flags & EDGE_TRUE_VALUE)
2459 break;
2460 op0 = gimple_cond_lhs (stmt);
2461 op1 = gimple_cond_rhs (stmt);
2462 cmp = gimple_cond_code (stmt);
2463 type = TREE_TYPE (op0);
2464 val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, auto_bitmap (),
2465 &predictor);
2466 if (val && TREE_CODE (val) == INTEGER_CST)
2468 if (predictor == PRED_BUILTIN_EXPECT)
2470 int percent = PARAM_VALUE (BUILTIN_EXPECT_PROBABILITY);
2472 gcc_assert (percent >= 0 && percent <= 100);
2473 if (integer_zerop (val))
2474 percent = 100 - percent;
2475 predict_edge (then_edge, PRED_BUILTIN_EXPECT, HITRATE (percent));
2477 else
2478 predict_edge_def (then_edge, predictor,
2479 integer_zerop (val) ? NOT_TAKEN : TAKEN);
2481 /* Try "pointer heuristic."
2482 A comparison ptr == 0 is predicted as false.
2483 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2484 if (POINTER_TYPE_P (type))
2486 if (cmp == EQ_EXPR)
2487 predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
2488 else if (cmp == NE_EXPR)
2489 predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
2491 else
2493 /* Try "opcode heuristic."
2494 EQ tests are usually false and NE tests are usually true. Also,
2495 most quantities are positive, so we can make the appropriate guesses
2496 about signed comparisons against zero. */
2497 switch (cmp)
2499 case EQ_EXPR:
2500 case UNEQ_EXPR:
2501 /* Floating point comparisons appears to behave in a very
2502 unpredictable way because of special role of = tests in
2503 FP code. */
2504 if (FLOAT_TYPE_P (type))
2506 /* Comparisons with 0 are often used for booleans and there is
2507 nothing useful to predict about them. */
2508 else if (integer_zerop (op0) || integer_zerop (op1))
2510 else
2511 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
2512 break;
2514 case NE_EXPR:
2515 case LTGT_EXPR:
2516 /* Floating point comparisons appears to behave in a very
2517 unpredictable way because of special role of = tests in
2518 FP code. */
2519 if (FLOAT_TYPE_P (type))
2521 /* Comparisons with 0 are often used for booleans and there is
2522 nothing useful to predict about them. */
2523 else if (integer_zerop (op0)
2524 || integer_zerop (op1))
2526 else
2527 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
2528 break;
2530 case ORDERED_EXPR:
2531 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
2532 break;
2534 case UNORDERED_EXPR:
2535 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
2536 break;
2538 case LE_EXPR:
2539 case LT_EXPR:
2540 if (integer_zerop (op1)
2541 || integer_onep (op1)
2542 || integer_all_onesp (op1)
2543 || real_zerop (op1)
2544 || real_onep (op1)
2545 || real_minus_onep (op1))
2546 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
2547 break;
2549 case GE_EXPR:
2550 case GT_EXPR:
2551 if (integer_zerop (op1)
2552 || integer_onep (op1)
2553 || integer_all_onesp (op1)
2554 || real_zerop (op1)
2555 || real_onep (op1)
2556 || real_minus_onep (op1))
2557 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
2558 break;
2560 default:
2561 break;
2565 /* Returns TRUE if the STMT is exit(0) like statement. */
2567 static bool
2568 is_exit_with_zero_arg (const gimple *stmt)
2570 /* This is not exit, _exit or _Exit. */
2571 if (!gimple_call_builtin_p (stmt, BUILT_IN_EXIT)
2572 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT)
2573 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT2))
2574 return false;
2576 /* Argument is an interger zero. */
2577 return integer_zerop (gimple_call_arg (stmt, 0));
2580 /* Try to guess whether the value of return means error code. */
2582 static enum br_predictor
2583 return_prediction (tree val, enum prediction *prediction)
2585 /* VOID. */
2586 if (!val)
2587 return PRED_NO_PREDICTION;
2588 /* Different heuristics for pointers and scalars. */
2589 if (POINTER_TYPE_P (TREE_TYPE (val)))
2591 /* NULL is usually not returned. */
2592 if (integer_zerop (val))
2594 *prediction = NOT_TAKEN;
2595 return PRED_NULL_RETURN;
2598 else if (INTEGRAL_TYPE_P (TREE_TYPE (val)))
2600 /* Negative return values are often used to indicate
2601 errors. */
2602 if (TREE_CODE (val) == INTEGER_CST
2603 && tree_int_cst_sgn (val) < 0)
2605 *prediction = NOT_TAKEN;
2606 return PRED_NEGATIVE_RETURN;
2608 /* Constant return values seems to be commonly taken.
2609 Zero/one often represent booleans so exclude them from the
2610 heuristics. */
2611 if (TREE_CONSTANT (val)
2612 && (!integer_zerop (val) && !integer_onep (val)))
2614 *prediction = NOT_TAKEN;
2615 return PRED_CONST_RETURN;
2618 return PRED_NO_PREDICTION;
2621 /* Find the basic block with return expression and look up for possible
2622 return value trying to apply RETURN_PREDICTION heuristics. */
2623 static void
2624 apply_return_prediction (void)
2626 greturn *return_stmt = NULL;
2627 tree return_val;
2628 edge e;
2629 gphi *phi;
2630 int phi_num_args, i;
2631 enum br_predictor pred;
2632 enum prediction direction;
2633 edge_iterator ei;
2635 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2637 gimple *last = last_stmt (e->src);
2638 if (last
2639 && gimple_code (last) == GIMPLE_RETURN)
2641 return_stmt = as_a <greturn *> (last);
2642 break;
2645 if (!e)
2646 return;
2647 return_val = gimple_return_retval (return_stmt);
2648 if (!return_val)
2649 return;
2650 if (TREE_CODE (return_val) != SSA_NAME
2651 || !SSA_NAME_DEF_STMT (return_val)
2652 || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI)
2653 return;
2654 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (return_val));
2655 phi_num_args = gimple_phi_num_args (phi);
2656 pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction);
2658 /* Avoid the degenerate case where all return values form the function
2659 belongs to same category (ie they are all positive constants)
2660 so we can hardly say something about them. */
2661 for (i = 1; i < phi_num_args; i++)
2662 if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction))
2663 break;
2664 if (i != phi_num_args)
2665 for (i = 0; i < phi_num_args; i++)
2667 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
2668 if (pred != PRED_NO_PREDICTION)
2669 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi, i), pred,
2670 direction);
2674 /* Look for basic block that contains unlikely to happen events
2675 (such as noreturn calls) and mark all paths leading to execution
2676 of this basic blocks as unlikely. */
2678 static void
2679 tree_bb_level_predictions (void)
2681 basic_block bb;
2682 bool has_return_edges = false;
2683 edge e;
2684 edge_iterator ei;
2686 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2687 if (!unlikely_executed_edge_p (e) && !(e->flags & EDGE_ABNORMAL_CALL))
2689 has_return_edges = true;
2690 break;
2693 apply_return_prediction ();
2695 FOR_EACH_BB_FN (bb, cfun)
2697 gimple_stmt_iterator gsi;
2699 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2701 gimple *stmt = gsi_stmt (gsi);
2702 tree decl;
2704 if (is_gimple_call (stmt))
2706 if (gimple_call_noreturn_p (stmt)
2707 && has_return_edges
2708 && !is_exit_with_zero_arg (stmt))
2709 predict_paths_leading_to (bb, PRED_NORETURN,
2710 NOT_TAKEN);
2711 decl = gimple_call_fndecl (stmt);
2712 if (decl
2713 && lookup_attribute ("cold",
2714 DECL_ATTRIBUTES (decl)))
2715 predict_paths_leading_to (bb, PRED_COLD_FUNCTION,
2716 NOT_TAKEN);
2717 if (decl && recursive_call_p (current_function_decl, decl))
2718 predict_paths_leading_to (bb, PRED_RECURSIVE_CALL,
2719 NOT_TAKEN);
2721 else if (gimple_code (stmt) == GIMPLE_PREDICT)
2723 predict_paths_leading_to (bb, gimple_predict_predictor (stmt),
2724 gimple_predict_outcome (stmt));
2725 /* Keep GIMPLE_PREDICT around so early inlining will propagate
2726 hints to callers. */
2732 /* Callback for hash_map::traverse, asserts that the pointer map is
2733 empty. */
2735 bool
2736 assert_is_empty (const_basic_block const &, edge_prediction *const &value,
2737 void *)
2739 gcc_assert (!value);
2740 return false;
2743 /* Predict branch probabilities and estimate profile for basic block BB.
2744 When LOCAL_ONLY is set do not use any global properties of CFG. */
2746 static void
2747 tree_estimate_probability_bb (basic_block bb, bool local_only)
2749 edge e;
2750 edge_iterator ei;
2752 FOR_EACH_EDGE (e, ei, bb->succs)
2754 /* Look for block we are guarding (ie we dominate it,
2755 but it doesn't postdominate us). */
2756 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest != bb
2757 && !local_only
2758 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
2759 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
2761 gimple_stmt_iterator bi;
2763 /* The call heuristic claims that a guarded function call
2764 is improbable. This is because such calls are often used
2765 to signal exceptional situations such as printing error
2766 messages. */
2767 for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi);
2768 gsi_next (&bi))
2770 gimple *stmt = gsi_stmt (bi);
2771 if (is_gimple_call (stmt)
2772 && !gimple_inexpensive_call_p (as_a <gcall *> (stmt))
2773 /* Constant and pure calls are hardly used to signalize
2774 something exceptional. */
2775 && gimple_has_side_effects (stmt))
2777 if (gimple_call_fndecl (stmt))
2778 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
2779 else if (virtual_method_call_p (gimple_call_fn (stmt)))
2780 predict_edge_def (e, PRED_POLYMORPHIC_CALL, NOT_TAKEN);
2781 else
2782 predict_edge_def (e, PRED_INDIR_CALL, TAKEN);
2783 break;
2788 tree_predict_by_opcode (bb);
2791 /* Predict branch probabilities and estimate profile of the tree CFG.
2792 This function can be called from the loop optimizers to recompute
2793 the profile information.
2794 If DRY_RUN is set, do not modify CFG and only produce dump files. */
2796 void
2797 tree_estimate_probability (bool dry_run)
2799 basic_block bb;
2801 add_noreturn_fake_exit_edges ();
2802 connect_infinite_loops_to_exit ();
2803 /* We use loop_niter_by_eval, which requires that the loops have
2804 preheaders. */
2805 create_preheaders (CP_SIMPLE_PREHEADERS);
2806 calculate_dominance_info (CDI_POST_DOMINATORS);
2808 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
2809 tree_bb_level_predictions ();
2810 record_loop_exits ();
2812 if (number_of_loops (cfun) > 1)
2813 predict_loops ();
2815 FOR_EACH_BB_FN (bb, cfun)
2816 tree_estimate_probability_bb (bb, false);
2818 FOR_EACH_BB_FN (bb, cfun)
2819 combine_predictions_for_bb (bb, dry_run);
2821 if (flag_checking)
2822 bb_predictions->traverse<void *, assert_is_empty> (NULL);
2824 delete bb_predictions;
2825 bb_predictions = NULL;
2827 if (!dry_run)
2828 estimate_bb_frequencies (false);
2829 free_dominance_info (CDI_POST_DOMINATORS);
2830 remove_fake_exit_edges ();
2833 /* Set edge->probability for each successor edge of BB. */
2834 void
2835 tree_guess_outgoing_edge_probabilities (basic_block bb)
2837 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
2838 tree_estimate_probability_bb (bb, true);
2839 combine_predictions_for_bb (bb, false);
2840 if (flag_checking)
2841 bb_predictions->traverse<void *, assert_is_empty> (NULL);
2842 delete bb_predictions;
2843 bb_predictions = NULL;
2846 /* Predict edges to successors of CUR whose sources are not postdominated by
2847 BB by PRED and recurse to all postdominators. */
2849 static void
2850 predict_paths_for_bb (basic_block cur, basic_block bb,
2851 enum br_predictor pred,
2852 enum prediction taken,
2853 bitmap visited, struct loop *in_loop = NULL)
2855 edge e;
2856 edge_iterator ei;
2857 basic_block son;
2859 /* If we exited the loop or CUR is unconditional in the loop, there is
2860 nothing to do. */
2861 if (in_loop
2862 && (!flow_bb_inside_loop_p (in_loop, cur)
2863 || dominated_by_p (CDI_DOMINATORS, in_loop->latch, cur)))
2864 return;
2866 /* We are looking for all edges forming edge cut induced by
2867 set of all blocks postdominated by BB. */
2868 FOR_EACH_EDGE (e, ei, cur->preds)
2869 if (e->src->index >= NUM_FIXED_BLOCKS
2870 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb))
2872 edge e2;
2873 edge_iterator ei2;
2874 bool found = false;
2876 /* Ignore fake edges and eh, we predict them as not taken anyway. */
2877 if (unlikely_executed_edge_p (e))
2878 continue;
2879 gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb));
2881 /* See if there is an edge from e->src that is not abnormal
2882 and does not lead to BB and does not exit the loop. */
2883 FOR_EACH_EDGE (e2, ei2, e->src->succs)
2884 if (e2 != e
2885 && !unlikely_executed_edge_p (e2)
2886 && !dominated_by_p (CDI_POST_DOMINATORS, e2->dest, bb)
2887 && (!in_loop || !loop_exit_edge_p (in_loop, e2)))
2889 found = true;
2890 break;
2893 /* If there is non-abnormal path leaving e->src, predict edge
2894 using predictor. Otherwise we need to look for paths
2895 leading to e->src.
2897 The second may lead to infinite loop in the case we are predicitng
2898 regions that are only reachable by abnormal edges. We simply
2899 prevent visiting given BB twice. */
2900 if (found)
2902 if (!edge_predicted_by_p (e, pred, taken))
2903 predict_edge_def (e, pred, taken);
2905 else if (bitmap_set_bit (visited, e->src->index))
2906 predict_paths_for_bb (e->src, e->src, pred, taken, visited, in_loop);
2908 for (son = first_dom_son (CDI_POST_DOMINATORS, cur);
2909 son;
2910 son = next_dom_son (CDI_POST_DOMINATORS, son))
2911 predict_paths_for_bb (son, bb, pred, taken, visited, in_loop);
2914 /* Sets branch probabilities according to PREDiction and
2915 FLAGS. */
2917 static void
2918 predict_paths_leading_to (basic_block bb, enum br_predictor pred,
2919 enum prediction taken, struct loop *in_loop)
2921 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
2924 /* Like predict_paths_leading_to but take edge instead of basic block. */
2926 static void
2927 predict_paths_leading_to_edge (edge e, enum br_predictor pred,
2928 enum prediction taken, struct loop *in_loop)
2930 bool has_nonloop_edge = false;
2931 edge_iterator ei;
2932 edge e2;
2934 basic_block bb = e->src;
2935 FOR_EACH_EDGE (e2, ei, bb->succs)
2936 if (e2->dest != e->src && e2->dest != e->dest
2937 && !unlikely_executed_edge_p (e)
2938 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->dest))
2940 has_nonloop_edge = true;
2941 break;
2943 if (!has_nonloop_edge)
2945 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
2947 else
2948 predict_edge_def (e, pred, taken);
2951 /* This is used to carry information about basic blocks. It is
2952 attached to the AUX field of the standard CFG block. */
2954 struct block_info
2956 /* Estimated frequency of execution of basic_block. */
2957 sreal frequency;
2959 /* To keep queue of basic blocks to process. */
2960 basic_block next;
2962 /* Number of predecessors we need to visit first. */
2963 int npredecessors;
2966 /* Similar information for edges. */
2967 struct edge_prob_info
2969 /* In case edge is a loopback edge, the probability edge will be reached
2970 in case header is. Estimated number of iterations of the loop can be
2971 then computed as 1 / (1 - back_edge_prob). */
2972 sreal back_edge_prob;
2973 /* True if the edge is a loopback edge in the natural loop. */
2974 unsigned int back_edge:1;
2977 #define BLOCK_INFO(B) ((block_info *) (B)->aux)
2978 #undef EDGE_INFO
2979 #define EDGE_INFO(E) ((edge_prob_info *) (E)->aux)
2981 /* Helper function for estimate_bb_frequencies.
2982 Propagate the frequencies in blocks marked in
2983 TOVISIT, starting in HEAD. */
2985 static void
2986 propagate_freq (basic_block head, bitmap tovisit)
2988 basic_block bb;
2989 basic_block last;
2990 unsigned i;
2991 edge e;
2992 basic_block nextbb;
2993 bitmap_iterator bi;
2995 /* For each basic block we need to visit count number of his predecessors
2996 we need to visit first. */
2997 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
2999 edge_iterator ei;
3000 int count = 0;
3002 bb = BASIC_BLOCK_FOR_FN (cfun, i);
3004 FOR_EACH_EDGE (e, ei, bb->preds)
3006 bool visit = bitmap_bit_p (tovisit, e->src->index);
3008 if (visit && !(e->flags & EDGE_DFS_BACK))
3009 count++;
3010 else if (visit && dump_file && !EDGE_INFO (e)->back_edge)
3011 fprintf (dump_file,
3012 "Irreducible region hit, ignoring edge to %i->%i\n",
3013 e->src->index, bb->index);
3015 BLOCK_INFO (bb)->npredecessors = count;
3016 /* When function never returns, we will never process exit block. */
3017 if (!count && bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3019 bb->count = profile_count::zero ();
3020 bb->frequency = 0;
3024 BLOCK_INFO (head)->frequency = 1;
3025 last = head;
3026 for (bb = head; bb; bb = nextbb)
3028 edge_iterator ei;
3029 sreal cyclic_probability = 0;
3030 sreal frequency = 0;
3032 nextbb = BLOCK_INFO (bb)->next;
3033 BLOCK_INFO (bb)->next = NULL;
3035 /* Compute frequency of basic block. */
3036 if (bb != head)
3038 if (flag_checking)
3039 FOR_EACH_EDGE (e, ei, bb->preds)
3040 gcc_assert (!bitmap_bit_p (tovisit, e->src->index)
3041 || (e->flags & EDGE_DFS_BACK));
3043 FOR_EACH_EDGE (e, ei, bb->preds)
3044 if (EDGE_INFO (e)->back_edge)
3046 cyclic_probability += EDGE_INFO (e)->back_edge_prob;
3048 else if (!(e->flags & EDGE_DFS_BACK))
3050 /* frequency += (e->probability
3051 * BLOCK_INFO (e->src)->frequency /
3052 REG_BR_PROB_BASE); */
3054 sreal tmp = e->probability.to_reg_br_prob_base ();
3055 tmp *= BLOCK_INFO (e->src)->frequency;
3056 tmp *= real_inv_br_prob_base;
3057 frequency += tmp;
3060 if (cyclic_probability == 0)
3062 BLOCK_INFO (bb)->frequency = frequency;
3064 else
3066 if (cyclic_probability > real_almost_one)
3067 cyclic_probability = real_almost_one;
3069 /* BLOCK_INFO (bb)->frequency = frequency
3070 / (1 - cyclic_probability) */
3072 cyclic_probability = sreal (1) - cyclic_probability;
3073 BLOCK_INFO (bb)->frequency = frequency / cyclic_probability;
3077 bitmap_clear_bit (tovisit, bb->index);
3079 e = find_edge (bb, head);
3080 if (e)
3082 /* EDGE_INFO (e)->back_edge_prob
3083 = ((e->probability * BLOCK_INFO (bb)->frequency)
3084 / REG_BR_PROB_BASE); */
3086 sreal tmp = e->probability.to_reg_br_prob_base ();
3087 tmp *= BLOCK_INFO (bb)->frequency;
3088 EDGE_INFO (e)->back_edge_prob = tmp * real_inv_br_prob_base;
3091 /* Propagate to successor blocks. */
3092 FOR_EACH_EDGE (e, ei, bb->succs)
3093 if (!(e->flags & EDGE_DFS_BACK)
3094 && BLOCK_INFO (e->dest)->npredecessors)
3096 BLOCK_INFO (e->dest)->npredecessors--;
3097 if (!BLOCK_INFO (e->dest)->npredecessors)
3099 if (!nextbb)
3100 nextbb = e->dest;
3101 else
3102 BLOCK_INFO (last)->next = e->dest;
3104 last = e->dest;
3110 /* Estimate frequencies in loops at same nest level. */
3112 static void
3113 estimate_loops_at_level (struct loop *first_loop)
3115 struct loop *loop;
3117 for (loop = first_loop; loop; loop = loop->next)
3119 edge e;
3120 basic_block *bbs;
3121 unsigned i;
3122 auto_bitmap tovisit;
3124 estimate_loops_at_level (loop->inner);
3126 /* Find current loop back edge and mark it. */
3127 e = loop_latch_edge (loop);
3128 EDGE_INFO (e)->back_edge = 1;
3130 bbs = get_loop_body (loop);
3131 for (i = 0; i < loop->num_nodes; i++)
3132 bitmap_set_bit (tovisit, bbs[i]->index);
3133 free (bbs);
3134 propagate_freq (loop->header, tovisit);
3138 /* Propagates frequencies through structure of loops. */
3140 static void
3141 estimate_loops (void)
3143 auto_bitmap tovisit;
3144 basic_block bb;
3146 /* Start by estimating the frequencies in the loops. */
3147 if (number_of_loops (cfun) > 1)
3148 estimate_loops_at_level (current_loops->tree_root->inner);
3150 /* Now propagate the frequencies through all the blocks. */
3151 FOR_ALL_BB_FN (bb, cfun)
3153 bitmap_set_bit (tovisit, bb->index);
3155 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun), tovisit);
3158 /* Drop the profile for NODE to guessed, and update its frequency based on
3159 whether it is expected to be hot given the CALL_COUNT. */
3161 static void
3162 drop_profile (struct cgraph_node *node, profile_count call_count)
3164 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3165 /* In the case where this was called by another function with a
3166 dropped profile, call_count will be 0. Since there are no
3167 non-zero call counts to this function, we don't know for sure
3168 whether it is hot, and therefore it will be marked normal below. */
3169 bool hot = maybe_hot_count_p (NULL, call_count);
3171 if (dump_file)
3172 fprintf (dump_file,
3173 "Dropping 0 profile for %s. %s based on calls.\n",
3174 node->dump_name (),
3175 hot ? "Function is hot" : "Function is normal");
3176 /* We only expect to miss profiles for functions that are reached
3177 via non-zero call edges in cases where the function may have
3178 been linked from another module or library (COMDATs and extern
3179 templates). See the comments below for handle_missing_profiles.
3180 Also, only warn in cases where the missing counts exceed the
3181 number of training runs. In certain cases with an execv followed
3182 by a no-return call the profile for the no-return call is not
3183 dumped and there can be a mismatch. */
3184 if (!DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl)
3185 && call_count > profile_info->runs)
3187 if (flag_profile_correction)
3189 if (dump_file)
3190 fprintf (dump_file,
3191 "Missing counts for called function %s\n",
3192 node->dump_name ());
3194 else
3195 warning (0, "Missing counts for called function %s",
3196 node->dump_name ());
3199 basic_block bb;
3200 FOR_ALL_BB_FN (bb, fn)
3202 bb->count = profile_count::uninitialized ();
3204 edge_iterator ei;
3205 edge e;
3206 FOR_EACH_EDGE (e, ei, bb->preds)
3207 e->count = profile_count::uninitialized ();
3210 struct cgraph_edge *e;
3211 for (e = node->callees; e; e = e->next_caller)
3213 e->count = profile_count::uninitialized ();
3214 e->frequency = compute_call_stmt_bb_frequency (e->caller->decl,
3215 gimple_bb (e->call_stmt));
3217 node->count = profile_count::uninitialized ();
3219 profile_status_for_fn (fn)
3220 = (flag_guess_branch_prob ? PROFILE_GUESSED : PROFILE_ABSENT);
3221 node->frequency
3222 = hot ? NODE_FREQUENCY_HOT : NODE_FREQUENCY_NORMAL;
3225 /* In the case of COMDAT routines, multiple object files will contain the same
3226 function and the linker will select one for the binary. In that case
3227 all the other copies from the profile instrument binary will be missing
3228 profile counts. Look for cases where this happened, due to non-zero
3229 call counts going to 0-count functions, and drop the profile to guessed
3230 so that we can use the estimated probabilities and avoid optimizing only
3231 for size.
3233 The other case where the profile may be missing is when the routine
3234 is not going to be emitted to the object file, e.g. for "extern template"
3235 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
3236 all other cases of non-zero calls to 0-count functions. */
3238 void
3239 handle_missing_profiles (void)
3241 struct cgraph_node *node;
3242 int unlikely_count_fraction = PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION);
3243 auto_vec<struct cgraph_node *, 64> worklist;
3245 /* See if 0 count function has non-0 count callers. In this case we
3246 lost some profile. Drop its function profile to PROFILE_GUESSED. */
3247 FOR_EACH_DEFINED_FUNCTION (node)
3249 struct cgraph_edge *e;
3250 profile_count call_count = profile_count::zero ();
3251 gcov_type max_tp_first_run = 0;
3252 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3254 if (!(node->count == profile_count::zero ()))
3255 continue;
3256 for (e = node->callers; e; e = e->next_caller)
3257 if (e->count.initialized_p () && e->count > 0)
3259 call_count = call_count + e->count;
3261 if (e->caller->tp_first_run > max_tp_first_run)
3262 max_tp_first_run = e->caller->tp_first_run;
3265 /* If time profile is missing, let assign the maximum that comes from
3266 caller functions. */
3267 if (!node->tp_first_run && max_tp_first_run)
3268 node->tp_first_run = max_tp_first_run + 1;
3270 if (call_count > 0
3271 && fn && fn->cfg
3272 && (call_count.apply_scale (unlikely_count_fraction, 1)
3273 >= profile_info->runs))
3275 drop_profile (node, call_count);
3276 worklist.safe_push (node);
3280 /* Propagate the profile dropping to other 0-count COMDATs that are
3281 potentially called by COMDATs we already dropped the profile on. */
3282 while (worklist.length () > 0)
3284 struct cgraph_edge *e;
3286 node = worklist.pop ();
3287 for (e = node->callees; e; e = e->next_caller)
3289 struct cgraph_node *callee = e->callee;
3290 struct function *fn = DECL_STRUCT_FUNCTION (callee->decl);
3292 if (callee->count > 0)
3293 continue;
3294 if ((DECL_COMDAT (callee->decl) || DECL_EXTERNAL (callee->decl))
3295 && fn && fn->cfg
3296 && profile_status_for_fn (fn) == PROFILE_READ)
3298 drop_profile (node, profile_count::zero ());
3299 worklist.safe_push (callee);
3305 /* Convert counts measured by profile driven feedback to frequencies.
3306 Return nonzero iff there was any nonzero execution count. */
3308 bool
3309 counts_to_freqs (void)
3311 gcov_type count_max;
3312 profile_count true_count_max = profile_count::zero ();
3313 basic_block bb;
3315 /* Don't overwrite the estimated frequencies when the profile for
3316 the function is missing. We may drop this function PROFILE_GUESSED
3317 later in drop_profile (). */
3318 if (!ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.initialized_p ()
3319 || ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ())
3320 return false;
3322 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3323 if (bb->count > true_count_max)
3324 true_count_max = bb->count;
3326 /* If we have no counts to base frequencies on, keep those that are
3327 already there. */
3328 if (!(true_count_max > 0))
3329 return false;
3331 count_max = true_count_max.to_gcov_type ();
3333 FOR_ALL_BB_FN (bb, cfun)
3334 if (bb->count.initialized_p ())
3335 bb->frequency = RDIV (bb->count.to_gcov_type () * BB_FREQ_MAX, count_max);
3337 return true;
3340 /* Return true if function is likely to be expensive, so there is no point to
3341 optimize performance of prologue, epilogue or do inlining at the expense
3342 of code size growth. THRESHOLD is the limit of number of instructions
3343 function can execute at average to be still considered not expensive. */
3345 bool
3346 expensive_function_p (int threshold)
3348 unsigned int sum = 0;
3349 basic_block bb;
3350 unsigned int limit;
3352 /* We can not compute accurately for large thresholds due to scaled
3353 frequencies. */
3354 gcc_assert (threshold <= BB_FREQ_MAX);
3356 /* Frequencies are out of range. This either means that function contains
3357 internal loop executing more than BB_FREQ_MAX times or profile feedback
3358 is available and function has not been executed at all. */
3359 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency == 0)
3360 return true;
3362 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
3363 limit = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency * threshold;
3364 FOR_EACH_BB_FN (bb, cfun)
3366 rtx_insn *insn;
3368 FOR_BB_INSNS (bb, insn)
3369 if (active_insn_p (insn))
3371 sum += bb->frequency;
3372 if (sum > limit)
3373 return true;
3377 return false;
3380 /* Determine basic blocks/edges that are known to be unlikely executed and set
3381 their counters to zero.
3382 This is done with first identifying obviously unlikely BBs/edges and then
3383 propagating in both directions. */
3385 static void
3386 determine_unlikely_bbs ()
3388 basic_block bb;
3389 auto_vec<basic_block, 64> worklist;
3390 edge_iterator ei;
3391 edge e;
3393 FOR_EACH_BB_FN (bb, cfun)
3395 if (!(bb->count == profile_count::zero ())
3396 && unlikely_executed_bb_p (bb))
3398 if (dump_file && (dump_flags & TDF_DETAILS))
3399 fprintf (dump_file, "Basic block %i is locally unlikely\n",
3400 bb->index);
3401 bb->count = profile_count::zero ();
3404 if (bb->count == profile_count::zero ())
3406 bb->frequency = 0;
3407 FOR_EACH_EDGE (e, ei, bb->preds)
3408 e->count = profile_count::zero ();
3411 FOR_EACH_EDGE (e, ei, bb->succs)
3412 if (!(e->count == profile_count::zero ())
3413 && unlikely_executed_edge_p (e))
3415 if (dump_file && (dump_flags & TDF_DETAILS))
3416 fprintf (dump_file, "Edge %i->%i is locally unlikely\n",
3417 bb->index, e->dest->index);
3418 e->count = profile_count::zero ();
3421 gcc_checking_assert (!bb->aux);
3424 if (!(ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ()))
3426 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(size_t) 1;
3427 worklist.safe_push (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3429 while (worklist.length () > 0)
3431 bb = worklist.pop ();
3432 FOR_EACH_EDGE (e, ei, bb->succs)
3433 if (!(e->count == profile_count::zero ())
3434 && !(e->dest->count == profile_count::zero ())
3435 && !e->dest->aux)
3437 e->dest->aux = (void *)(size_t) 1;
3438 worklist.safe_push (e->dest);
3443 FOR_ALL_BB_FN (bb, cfun)
3445 if (!bb->aux)
3447 if (!(bb->count == profile_count::zero ())
3448 && (dump_file && (dump_flags & TDF_DETAILS)))
3449 fprintf (dump_file,
3450 "Basic block %i is marked unlikely by forward prop\n",
3451 bb->index);
3452 bb->count = profile_count::zero ();
3453 bb->frequency = 0;
3454 FOR_EACH_EDGE (e, ei, bb->succs)
3455 e->count = profile_count::zero ();
3457 else
3458 bb->aux = NULL;
3461 auto_vec<int, 64> nsuccs;
3462 nsuccs.safe_grow_cleared (last_basic_block_for_fn (cfun));
3463 FOR_ALL_BB_FN (bb, cfun)
3464 if (!(bb->count == profile_count::zero ())
3465 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3467 nsuccs[bb->index] = 0;
3468 FOR_EACH_EDGE (e, ei, bb->succs)
3469 if (!(e->count == profile_count::zero ()))
3470 nsuccs[bb->index]++;
3471 if (!nsuccs[bb->index])
3472 worklist.safe_push (bb);
3474 while (worklist.length () > 0)
3476 bb = worklist.pop ();
3477 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3479 bool found = false;
3480 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
3481 !gsi_end_p (gsi); gsi_next (&gsi))
3482 if (stmt_can_terminate_bb_p (gsi_stmt (gsi))
3483 /* stmt_can_terminate_bb_p special cases noreturns because it
3484 assumes that fake edges are created. We want to know that
3485 noreturn alone does not imply BB to be unlikely. */
3486 || (is_gimple_call (gsi_stmt (gsi))
3487 && (gimple_call_flags (gsi_stmt (gsi)) & ECF_NORETURN)))
3489 found = true;
3490 break;
3492 if (found)
3493 continue;
3495 if (!(bb->count == profile_count::zero ())
3496 && (dump_file && (dump_flags & TDF_DETAILS)))
3497 fprintf (dump_file,
3498 "Basic block %i is marked unlikely by backward prop\n",
3499 bb->index);
3500 bb->count = profile_count::zero ();
3501 bb->frequency = 0;
3502 FOR_EACH_EDGE (e, ei, bb->preds)
3503 if (!(e->count == profile_count::zero ()))
3505 e->count = profile_count::zero ();
3506 if (!(e->src->count == profile_count::zero ()))
3508 nsuccs[e->src->index]--;
3509 if (!nsuccs[e->src->index])
3510 worklist.safe_push (e->src);
3516 /* Estimate and propagate basic block frequencies using the given branch
3517 probabilities. If FORCE is true, the frequencies are used to estimate
3518 the counts even when there are already non-zero profile counts. */
3520 void
3521 estimate_bb_frequencies (bool force)
3523 basic_block bb;
3524 sreal freq_max;
3526 determine_unlikely_bbs ();
3528 if (force || profile_status_for_fn (cfun) != PROFILE_READ
3529 || !counts_to_freqs ())
3531 static int real_values_initialized = 0;
3533 if (!real_values_initialized)
3535 real_values_initialized = 1;
3536 real_br_prob_base = REG_BR_PROB_BASE;
3537 real_bb_freq_max = BB_FREQ_MAX;
3538 real_one_half = sreal (1, -1);
3539 real_inv_br_prob_base = sreal (1) / real_br_prob_base;
3540 real_almost_one = sreal (1) - real_inv_br_prob_base;
3543 mark_dfs_back_edges ();
3545 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->probability =
3546 profile_probability::always ();
3548 /* Set up block info for each basic block. */
3549 alloc_aux_for_blocks (sizeof (block_info));
3550 alloc_aux_for_edges (sizeof (edge_prob_info));
3551 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3553 edge e;
3554 edge_iterator ei;
3556 FOR_EACH_EDGE (e, ei, bb->succs)
3558 EDGE_INFO (e)->back_edge_prob
3559 = e->probability.to_reg_br_prob_base ();
3560 EDGE_INFO (e)->back_edge_prob *= real_inv_br_prob_base;
3564 /* First compute frequencies locally for each loop from innermost
3565 to outermost to examine frequencies for back edges. */
3566 estimate_loops ();
3568 freq_max = 0;
3569 FOR_EACH_BB_FN (bb, cfun)
3570 if (freq_max < BLOCK_INFO (bb)->frequency)
3571 freq_max = BLOCK_INFO (bb)->frequency;
3573 freq_max = real_bb_freq_max / freq_max;
3574 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3576 sreal tmp = BLOCK_INFO (bb)->frequency * freq_max + real_one_half;
3577 bb->frequency = tmp.to_int ();
3580 free_aux_for_blocks ();
3581 free_aux_for_edges ();
3583 compute_function_frequency ();
3586 /* Decide whether function is hot, cold or unlikely executed. */
3587 void
3588 compute_function_frequency (void)
3590 basic_block bb;
3591 struct cgraph_node *node = cgraph_node::get (current_function_decl);
3593 if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3594 || MAIN_NAME_P (DECL_NAME (current_function_decl)))
3595 node->only_called_at_startup = true;
3596 if (DECL_STATIC_DESTRUCTOR (current_function_decl))
3597 node->only_called_at_exit = true;
3599 if (profile_status_for_fn (cfun) != PROFILE_READ)
3601 int flags = flags_from_decl_or_type (current_function_decl);
3602 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ()
3603 || lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
3604 != NULL)
3605 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3606 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl))
3607 != NULL)
3608 node->frequency = NODE_FREQUENCY_HOT;
3609 else if (flags & ECF_NORETURN)
3610 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3611 else if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
3612 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3613 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3614 || DECL_STATIC_DESTRUCTOR (current_function_decl))
3615 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3616 return;
3619 /* Only first time try to drop function into unlikely executed.
3620 After inlining the roundoff errors may confuse us.
3621 Ipa-profile pass will drop functions only called from unlikely
3622 functions to unlikely and that is most of what we care about. */
3623 if (!cfun->after_inlining)
3624 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3625 FOR_EACH_BB_FN (bb, cfun)
3627 if (maybe_hot_bb_p (cfun, bb))
3629 node->frequency = NODE_FREQUENCY_HOT;
3630 return;
3632 if (!probably_never_executed_bb_p (cfun, bb))
3633 node->frequency = NODE_FREQUENCY_NORMAL;
3637 /* Build PREDICT_EXPR. */
3638 tree
3639 build_predict_expr (enum br_predictor predictor, enum prediction taken)
3641 tree t = build1 (PREDICT_EXPR, void_type_node,
3642 build_int_cst (integer_type_node, predictor));
3643 SET_PREDICT_EXPR_OUTCOME (t, taken);
3644 return t;
3647 const char *
3648 predictor_name (enum br_predictor predictor)
3650 return predictor_info[predictor].name;
3653 /* Predict branch probabilities and estimate profile of the tree CFG. */
3655 namespace {
3657 const pass_data pass_data_profile =
3659 GIMPLE_PASS, /* type */
3660 "profile_estimate", /* name */
3661 OPTGROUP_NONE, /* optinfo_flags */
3662 TV_BRANCH_PROB, /* tv_id */
3663 PROP_cfg, /* properties_required */
3664 0, /* properties_provided */
3665 0, /* properties_destroyed */
3666 0, /* todo_flags_start */
3667 0, /* todo_flags_finish */
3670 class pass_profile : public gimple_opt_pass
3672 public:
3673 pass_profile (gcc::context *ctxt)
3674 : gimple_opt_pass (pass_data_profile, ctxt)
3677 /* opt_pass methods: */
3678 virtual bool gate (function *) { return flag_guess_branch_prob; }
3679 virtual unsigned int execute (function *);
3681 }; // class pass_profile
3683 unsigned int
3684 pass_profile::execute (function *fun)
3686 unsigned nb_loops;
3688 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
3689 return 0;
3691 loop_optimizer_init (LOOPS_NORMAL);
3692 if (dump_file && (dump_flags & TDF_DETAILS))
3693 flow_loops_dump (dump_file, NULL, 0);
3695 mark_irreducible_loops ();
3697 nb_loops = number_of_loops (fun);
3698 if (nb_loops > 1)
3699 scev_initialize ();
3701 tree_estimate_probability (false);
3703 if (nb_loops > 1)
3704 scev_finalize ();
3706 loop_optimizer_finalize ();
3707 if (dump_file && (dump_flags & TDF_DETAILS))
3708 gimple_dump_cfg (dump_file, dump_flags);
3709 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
3710 profile_status_for_fn (fun) = PROFILE_GUESSED;
3711 if (dump_file && (dump_flags & TDF_DETAILS))
3713 struct loop *loop;
3714 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
3715 if (loop->header->frequency)
3716 fprintf (dump_file, "Loop got predicted %d to iterate %i times.\n",
3717 loop->num,
3718 (int)expected_loop_iterations_unbounded (loop));
3720 return 0;
3723 } // anon namespace
3725 gimple_opt_pass *
3726 make_pass_profile (gcc::context *ctxt)
3728 return new pass_profile (ctxt);
3731 namespace {
3733 const pass_data pass_data_strip_predict_hints =
3735 GIMPLE_PASS, /* type */
3736 "*strip_predict_hints", /* name */
3737 OPTGROUP_NONE, /* optinfo_flags */
3738 TV_BRANCH_PROB, /* tv_id */
3739 PROP_cfg, /* properties_required */
3740 0, /* properties_provided */
3741 0, /* properties_destroyed */
3742 0, /* todo_flags_start */
3743 0, /* todo_flags_finish */
3746 class pass_strip_predict_hints : public gimple_opt_pass
3748 public:
3749 pass_strip_predict_hints (gcc::context *ctxt)
3750 : gimple_opt_pass (pass_data_strip_predict_hints, ctxt)
3753 /* opt_pass methods: */
3754 opt_pass * clone () { return new pass_strip_predict_hints (m_ctxt); }
3755 virtual unsigned int execute (function *);
3757 }; // class pass_strip_predict_hints
3759 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
3760 we no longer need. */
3761 unsigned int
3762 pass_strip_predict_hints::execute (function *fun)
3764 basic_block bb;
3765 gimple *ass_stmt;
3766 tree var;
3767 bool changed = false;
3769 FOR_EACH_BB_FN (bb, fun)
3771 gimple_stmt_iterator bi;
3772 for (bi = gsi_start_bb (bb); !gsi_end_p (bi);)
3774 gimple *stmt = gsi_stmt (bi);
3776 if (gimple_code (stmt) == GIMPLE_PREDICT)
3778 gsi_remove (&bi, true);
3779 changed = true;
3780 continue;
3782 else if (is_gimple_call (stmt))
3784 tree fndecl = gimple_call_fndecl (stmt);
3786 if ((fndecl
3787 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
3788 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
3789 && gimple_call_num_args (stmt) == 2)
3790 || (gimple_call_internal_p (stmt)
3791 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
3793 var = gimple_call_lhs (stmt);
3794 changed = true;
3795 if (var)
3797 ass_stmt
3798 = gimple_build_assign (var, gimple_call_arg (stmt, 0));
3799 gsi_replace (&bi, ass_stmt, true);
3801 else
3803 gsi_remove (&bi, true);
3804 continue;
3808 gsi_next (&bi);
3811 return changed ? TODO_cleanup_cfg : 0;
3814 } // anon namespace
3816 gimple_opt_pass *
3817 make_pass_strip_predict_hints (gcc::context *ctxt)
3819 return new pass_strip_predict_hints (ctxt);
3822 /* Rebuild function frequencies. Passes are in general expected to
3823 maintain profile by hand, however in some cases this is not possible:
3824 for example when inlining several functions with loops freuqencies might run
3825 out of scale and thus needs to be recomputed. */
3827 void
3828 rebuild_frequencies (void)
3830 timevar_push (TV_REBUILD_FREQUENCIES);
3832 /* When the max bb count in the function is small, there is a higher
3833 chance that there were truncation errors in the integer scaling
3834 of counts by inlining and other optimizations. This could lead
3835 to incorrect classification of code as being cold when it isn't.
3836 In that case, force the estimation of bb counts/frequencies from the
3837 branch probabilities, rather than computing frequencies from counts,
3838 which may also lead to frequencies incorrectly reduced to 0. There
3839 is less precision in the probabilities, so we only do this for small
3840 max counts. */
3841 profile_count count_max = profile_count::zero ();
3842 basic_block bb;
3843 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3844 if (bb->count > count_max)
3845 count_max = bb->count;
3847 if (profile_status_for_fn (cfun) == PROFILE_GUESSED
3848 || (!flag_auto_profile && profile_status_for_fn (cfun) == PROFILE_READ
3849 && count_max < REG_BR_PROB_BASE / 10))
3851 loop_optimizer_init (0);
3852 add_noreturn_fake_exit_edges ();
3853 mark_irreducible_loops ();
3854 connect_infinite_loops_to_exit ();
3855 estimate_bb_frequencies (true);
3856 remove_fake_exit_edges ();
3857 loop_optimizer_finalize ();
3859 else if (profile_status_for_fn (cfun) == PROFILE_READ)
3860 counts_to_freqs ();
3861 else
3862 gcc_unreachable ();
3863 timevar_pop (TV_REBUILD_FREQUENCIES);
3866 /* Perform a dry run of the branch prediction pass and report comparsion of
3867 the predicted and real profile into the dump file. */
3869 void
3870 report_predictor_hitrates (void)
3872 unsigned nb_loops;
3874 loop_optimizer_init (LOOPS_NORMAL);
3875 if (dump_file && (dump_flags & TDF_DETAILS))
3876 flow_loops_dump (dump_file, NULL, 0);
3878 mark_irreducible_loops ();
3880 nb_loops = number_of_loops (cfun);
3881 if (nb_loops > 1)
3882 scev_initialize ();
3884 tree_estimate_probability (true);
3886 if (nb_loops > 1)
3887 scev_finalize ();
3889 loop_optimizer_finalize ();
3892 /* Force edge E to be cold.
3893 If IMPOSSIBLE is true, for edge to have count and probability 0 otherwise
3894 keep low probability to represent possible error in a guess. This is used
3895 i.e. in case we predict loop to likely iterate given number of times but
3896 we are not 100% sure.
3898 This function locally updates profile without attempt to keep global
3899 consistency which can not be reached in full generality without full profile
3900 rebuild from probabilities alone. Doing so is not necessarily a good idea
3901 because frequencies and counts may be more realistic then probabilities.
3903 In some cases (such as for elimination of early exits during full loop
3904 unrolling) the caller can ensure that profile will get consistent
3905 afterwards. */
3907 void
3908 force_edge_cold (edge e, bool impossible)
3910 profile_count count_sum = profile_count::zero ();
3911 profile_probability prob_sum = profile_probability::never ();
3912 edge_iterator ei;
3913 edge e2;
3914 profile_count old_count = e->count;
3915 profile_probability old_probability = e->probability;
3916 bool uninitialized_exit = false;
3918 profile_probability goal = (impossible ? profile_probability::never ()
3919 : profile_probability::very_unlikely ());
3921 /* If edge is already improbably or cold, just return. */
3922 if (e->probability <= goal
3923 && (!impossible || e->count == profile_count::zero ()))
3924 return;
3925 FOR_EACH_EDGE (e2, ei, e->src->succs)
3926 if (e2 != e)
3928 if (e2->count.initialized_p ())
3929 count_sum += e2->count;
3930 else
3931 uninitialized_exit = true;
3932 if (e2->probability.initialized_p ())
3933 prob_sum += e2->probability;
3936 /* If there are other edges out of e->src, redistribute probabilitity
3937 there. */
3938 if (prob_sum > profile_probability::never ())
3940 if (!(e->probability < goal))
3941 e->probability = goal;
3942 if (impossible)
3943 e->count = profile_count::zero ();
3944 else if (old_probability > profile_probability::never ())
3945 e->count = e->count.apply_probability (e->probability
3946 / old_probability);
3947 else
3948 e->count = e->count.apply_scale (1, REG_BR_PROB_BASE);
3950 profile_probability prob_comp = prob_sum / e->probability.invert ();
3952 if (dump_file && (dump_flags & TDF_DETAILS))
3953 fprintf (dump_file, "Making edge %i->%i %s by redistributing "
3954 "probability to other edges.\n",
3955 e->src->index, e->dest->index,
3956 impossible ? "impossible" : "cold");
3957 profile_count count_sum2 = count_sum + old_count - e->count;
3958 FOR_EACH_EDGE (e2, ei, e->src->succs)
3959 if (e2 != e)
3961 if (count_sum > 0)
3962 e2->count.apply_scale (count_sum2, count_sum);
3963 e2->probability /= prob_comp;
3965 if (current_ir_type () != IR_GIMPLE)
3966 update_br_prob_note (e->src);
3968 /* If all edges out of e->src are unlikely, the basic block itself
3969 is unlikely. */
3970 else
3972 e->probability = profile_probability::always ();
3973 if (current_ir_type () != IR_GIMPLE)
3974 update_br_prob_note (e->src);
3975 if (e->src->count == profile_count::zero ())
3976 return;
3977 if (count_sum == profile_count::zero () && !uninitialized_exit
3978 && impossible)
3980 bool found = false;
3981 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
3983 else if (current_ir_type () == IR_GIMPLE)
3984 for (gimple_stmt_iterator gsi = gsi_start_bb (e->src);
3985 !gsi_end_p (gsi); gsi_next (&gsi))
3987 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
3989 found = true;
3990 break;
3993 /* FIXME: Implement RTL path. */
3994 else
3995 found = true;
3996 if (!found)
3998 if (dump_file && (dump_flags & TDF_DETAILS))
3999 fprintf (dump_file,
4000 "Making bb %i impossible and dropping count to 0.\n",
4001 e->src->index);
4002 e->count = profile_count::zero ();
4003 e->src->count = profile_count::zero ();
4004 FOR_EACH_EDGE (e2, ei, e->src->preds)
4005 force_edge_cold (e2, impossible);
4006 return;
4010 /* If we did not adjusting, the source basic block has no likely edeges
4011 leaving other direction. In that case force that bb cold, too.
4012 This in general is difficult task to do, but handle special case when
4013 BB has only one predecestor. This is common case when we are updating
4014 after loop transforms. */
4015 if (!(prob_sum > profile_probability::never ())
4016 && count_sum == profile_count::zero ()
4017 && single_pred_p (e->src) && e->src->frequency > (impossible ? 0 : 1))
4019 int old_frequency = e->src->frequency;
4020 if (dump_file && (dump_flags & TDF_DETAILS))
4021 fprintf (dump_file, "Making bb %i %s.\n", e->src->index,
4022 impossible ? "impossible" : "cold");
4023 e->src->frequency = MIN (e->src->frequency, impossible ? 0 : 1);
4024 if (impossible)
4025 e->src->count = e->count = profile_count::zero ();
4026 else
4027 e->src->count = e->count = e->count.apply_scale (e->src->frequency,
4028 old_frequency);
4029 force_edge_cold (single_pred_edge (e->src), impossible);
4031 else if (dump_file && (dump_flags & TDF_DETAILS)
4032 && maybe_hot_bb_p (cfun, e->src))
4033 fprintf (dump_file, "Giving up on making bb %i %s.\n", e->src->index,
4034 impossible ? "impossible" : "cold");
4038 #if CHECKING_P
4040 namespace selftest {
4042 /* Test that value range of predictor values defined in predict.def is
4043 within range (50, 100]. */
4045 struct branch_predictor
4047 const char *name;
4048 unsigned probability;
4051 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) { NAME, HITRATE },
4053 static void
4054 test_prediction_value_range ()
4056 branch_predictor predictors[] = {
4057 #include "predict.def"
4058 {NULL, -1U}
4061 for (unsigned i = 0; predictors[i].name != NULL; i++)
4063 unsigned p = 100 * predictors[i].probability / REG_BR_PROB_BASE;
4064 ASSERT_TRUE (p > 50 && p <= 100);
4068 #undef DEF_PREDICTOR
4070 /* Run all of the selfests within this file. */
4072 void
4073 predict_c_tests ()
4075 test_prediction_value_range ();
4078 } // namespace selftest
4079 #endif /* CHECKING_P. */