* tree-vect-loop.c (vectorizable_live_operation): Support handling
[official-gcc.git] / gcc / predict.c
blob463fd0b610e5f9b0d750939a6398ee551777c461
1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000-2016 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 "emit-rtl.h"
41 #include "cgraph.h"
42 #include "coverage.h"
43 #include "diagnostic-core.h"
44 #include "gimple-predict.h"
45 #include "fold-const.h"
46 #include "calls.h"
47 #include "cfganal.h"
48 #include "profile.h"
49 #include "sreal.h"
50 #include "params.h"
51 #include "cfgloop.h"
52 #include "gimple-iterator.h"
53 #include "tree-cfg.h"
54 #include "tree-ssa-loop-niter.h"
55 #include "tree-ssa-loop.h"
56 #include "tree-scalar-evolution.h"
57 #include "ipa-utils.h"
58 #include "gimple-pretty-print.h"
60 /* Enum with reasons why a predictor is ignored. */
62 enum predictor_reason
64 REASON_NONE,
65 REASON_IGNORED,
66 REASON_SINGLE_EDGE_DUPLICATE,
67 REASON_EDGE_PAIR_DUPLICATE
70 /* String messages for the aforementioned enum. */
72 static const char *reason_messages[] = {"", " (ignored)",
73 " (single edge duplicate)", " (edge pair duplicate)"};
75 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
76 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */
77 static sreal real_almost_one, real_br_prob_base,
78 real_inv_br_prob_base, real_one_half, real_bb_freq_max;
80 static void combine_predictions_for_insn (rtx_insn *, basic_block);
81 static void dump_prediction (FILE *, enum br_predictor, int, basic_block,
82 enum predictor_reason, edge);
83 static void predict_paths_leading_to (basic_block, enum br_predictor,
84 enum prediction,
85 struct loop *in_loop = NULL);
86 static void predict_paths_leading_to_edge (edge, enum br_predictor,
87 enum prediction,
88 struct loop *in_loop = NULL);
89 static bool can_predict_insn_p (const rtx_insn *);
91 /* Information we hold about each branch predictor.
92 Filled using information from predict.def. */
94 struct predictor_info
96 const char *const name; /* Name used in the debugging dumps. */
97 const int hitrate; /* Expected hitrate used by
98 predict_insn_def call. */
99 const int flags;
102 /* Use given predictor without Dempster-Shaffer theory if it matches
103 using first_match heuristics. */
104 #define PRED_FLAG_FIRST_MATCH 1
106 /* Recompute hitrate in percent to our representation. */
108 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
110 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
111 static const struct predictor_info predictor_info[]= {
112 #include "predict.def"
114 /* Upper bound on predictors. */
115 {NULL, 0, 0}
117 #undef DEF_PREDICTOR
119 /* Return TRUE if frequency FREQ is considered to be hot. */
121 static inline bool
122 maybe_hot_frequency_p (struct function *fun, int freq)
124 struct cgraph_node *node = cgraph_node::get (fun->decl);
125 if (!profile_info
126 || !opt_for_fn (fun->decl, flag_branch_probabilities))
128 if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
129 return false;
130 if (node->frequency == NODE_FREQUENCY_HOT)
131 return true;
133 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
134 return true;
135 if (node->frequency == NODE_FREQUENCY_EXECUTED_ONCE
136 && freq < (ENTRY_BLOCK_PTR_FOR_FN (fun)->frequency * 2 / 3))
137 return false;
138 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0)
139 return false;
140 if (freq * PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)
141 < ENTRY_BLOCK_PTR_FOR_FN (fun)->frequency)
142 return false;
143 return true;
146 static gcov_type min_count = -1;
148 /* Determine the threshold for hot BB counts. */
150 gcov_type
151 get_hot_bb_threshold ()
153 gcov_working_set_t *ws;
154 if (min_count == -1)
156 ws = find_working_set (PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE));
157 gcc_assert (ws);
158 min_count = ws->min_counter;
160 return min_count;
163 /* Set the threshold for hot BB counts. */
165 void
166 set_hot_bb_threshold (gcov_type min)
168 min_count = min;
171 /* Return TRUE if frequency FREQ is considered to be hot. */
173 bool
174 maybe_hot_count_p (struct function *fun, gcov_type count)
176 if (fun && profile_status_for_fn (fun) != PROFILE_READ)
177 return true;
178 /* Code executed at most once is not hot. */
179 if (profile_info->runs >= count)
180 return false;
181 return (count >= get_hot_bb_threshold ());
184 /* Return true in case BB can be CPU intensive and should be optimized
185 for maximal performance. */
187 bool
188 maybe_hot_bb_p (struct function *fun, const_basic_block bb)
190 gcc_checking_assert (fun);
191 if (profile_status_for_fn (fun) == PROFILE_READ)
192 return maybe_hot_count_p (fun, bb->count);
193 return maybe_hot_frequency_p (fun, bb->frequency);
196 /* Return true in case BB can be CPU intensive and should be optimized
197 for maximal performance. */
199 bool
200 maybe_hot_edge_p (edge e)
202 if (profile_status_for_fn (cfun) == PROFILE_READ)
203 return maybe_hot_count_p (cfun, e->count);
204 return maybe_hot_frequency_p (cfun, EDGE_FREQUENCY (e));
207 /* Return true if profile COUNT and FREQUENCY, or function FUN static
208 node frequency reflects never being executed. */
210 static bool
211 probably_never_executed (struct function *fun,
212 gcov_type count, int frequency)
214 gcc_checking_assert (fun);
215 if (profile_status_for_fn (fun) == PROFILE_READ)
217 int unlikely_count_fraction = PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION);
218 if (count * unlikely_count_fraction >= profile_info->runs)
219 return false;
220 if (!frequency)
221 return true;
222 if (!ENTRY_BLOCK_PTR_FOR_FN (fun)->frequency)
223 return false;
224 if (ENTRY_BLOCK_PTR_FOR_FN (fun)->count)
226 gcov_type computed_count;
227 /* Check for possibility of overflow, in which case entry bb count
228 is large enough to do the division first without losing much
229 precision. */
230 if (ENTRY_BLOCK_PTR_FOR_FN (fun)->count < REG_BR_PROB_BASE *
231 REG_BR_PROB_BASE)
233 gcov_type scaled_count
234 = frequency * ENTRY_BLOCK_PTR_FOR_FN (fun)->count *
235 unlikely_count_fraction;
236 computed_count = RDIV (scaled_count,
237 ENTRY_BLOCK_PTR_FOR_FN (fun)->frequency);
239 else
241 computed_count = RDIV (ENTRY_BLOCK_PTR_FOR_FN (fun)->count,
242 ENTRY_BLOCK_PTR_FOR_FN (fun)->frequency);
243 computed_count *= frequency * unlikely_count_fraction;
245 if (computed_count >= profile_info->runs)
246 return false;
248 return true;
250 if ((!profile_info || !(opt_for_fn (fun->decl, flag_branch_probabilities)))
251 && (cgraph_node::get (fun->decl)->frequency
252 == NODE_FREQUENCY_UNLIKELY_EXECUTED))
253 return true;
254 return false;
258 /* Return true in case BB is probably never executed. */
260 bool
261 probably_never_executed_bb_p (struct function *fun, const_basic_block bb)
263 return probably_never_executed (fun, bb->count, bb->frequency);
267 /* Return true in case edge E is probably never executed. */
269 bool
270 probably_never_executed_edge_p (struct function *fun, edge e)
272 return probably_never_executed (fun, e->count, EDGE_FREQUENCY (e));
275 /* Return true when current function should always be optimized for size. */
277 bool
278 optimize_function_for_size_p (struct function *fun)
280 if (!fun || !fun->decl)
281 return optimize_size;
282 cgraph_node *n = cgraph_node::get (fun->decl);
283 return n && n->optimize_for_size_p ();
286 /* Return true when current function should always be optimized for speed. */
288 bool
289 optimize_function_for_speed_p (struct function *fun)
291 return !optimize_function_for_size_p (fun);
294 /* Return the optimization type that should be used for the function FUN. */
296 optimization_type
297 function_optimization_type (struct function *fun)
299 return (optimize_function_for_speed_p (fun)
300 ? OPTIMIZE_FOR_SPEED
301 : OPTIMIZE_FOR_SIZE);
304 /* Return TRUE when BB should be optimized for size. */
306 bool
307 optimize_bb_for_size_p (const_basic_block bb)
309 return (optimize_function_for_size_p (cfun)
310 || (bb && !maybe_hot_bb_p (cfun, bb)));
313 /* Return TRUE when BB should be optimized for speed. */
315 bool
316 optimize_bb_for_speed_p (const_basic_block bb)
318 return !optimize_bb_for_size_p (bb);
321 /* Return the optimization type that should be used for block BB. */
323 optimization_type
324 bb_optimization_type (const_basic_block bb)
326 return (optimize_bb_for_speed_p (bb)
327 ? OPTIMIZE_FOR_SPEED
328 : OPTIMIZE_FOR_SIZE);
331 /* Return TRUE when BB should be optimized for size. */
333 bool
334 optimize_edge_for_size_p (edge e)
336 return optimize_function_for_size_p (cfun) || !maybe_hot_edge_p (e);
339 /* Return TRUE when BB should be optimized for speed. */
341 bool
342 optimize_edge_for_speed_p (edge e)
344 return !optimize_edge_for_size_p (e);
347 /* Return TRUE when BB should be optimized for size. */
349 bool
350 optimize_insn_for_size_p (void)
352 return optimize_function_for_size_p (cfun) || !crtl->maybe_hot_insn_p;
355 /* Return TRUE when BB should be optimized for speed. */
357 bool
358 optimize_insn_for_speed_p (void)
360 return !optimize_insn_for_size_p ();
363 /* Return TRUE when LOOP should be optimized for size. */
365 bool
366 optimize_loop_for_size_p (struct loop *loop)
368 return optimize_bb_for_size_p (loop->header);
371 /* Return TRUE when LOOP should be optimized for speed. */
373 bool
374 optimize_loop_for_speed_p (struct loop *loop)
376 return optimize_bb_for_speed_p (loop->header);
379 /* Return TRUE when LOOP nest should be optimized for speed. */
381 bool
382 optimize_loop_nest_for_speed_p (struct loop *loop)
384 struct loop *l = loop;
385 if (optimize_loop_for_speed_p (loop))
386 return true;
387 l = loop->inner;
388 while (l && l != loop)
390 if (optimize_loop_for_speed_p (l))
391 return true;
392 if (l->inner)
393 l = l->inner;
394 else if (l->next)
395 l = l->next;
396 else
398 while (l != loop && !l->next)
399 l = loop_outer (l);
400 if (l != loop)
401 l = l->next;
404 return false;
407 /* Return TRUE when LOOP nest should be optimized for size. */
409 bool
410 optimize_loop_nest_for_size_p (struct loop *loop)
412 return !optimize_loop_nest_for_speed_p (loop);
415 /* Return true when edge E is likely to be well predictable by branch
416 predictor. */
418 bool
419 predictable_edge_p (edge e)
421 if (profile_status_for_fn (cfun) == PROFILE_ABSENT)
422 return false;
423 if ((e->probability
424 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100)
425 || (REG_BR_PROB_BASE - e->probability
426 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100))
427 return true;
428 return false;
432 /* Set RTL expansion for BB profile. */
434 void
435 rtl_profile_for_bb (basic_block bb)
437 crtl->maybe_hot_insn_p = maybe_hot_bb_p (cfun, bb);
440 /* Set RTL expansion for edge profile. */
442 void
443 rtl_profile_for_edge (edge e)
445 crtl->maybe_hot_insn_p = maybe_hot_edge_p (e);
448 /* Set RTL expansion to default mode (i.e. when profile info is not known). */
449 void
450 default_rtl_profile (void)
452 crtl->maybe_hot_insn_p = true;
455 /* Return true if the one of outgoing edges is already predicted by
456 PREDICTOR. */
458 bool
459 rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
461 rtx note;
462 if (!INSN_P (BB_END (bb)))
463 return false;
464 for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
465 if (REG_NOTE_KIND (note) == REG_BR_PRED
466 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
467 return true;
468 return false;
471 /* Structure representing predictions in tree level. */
473 struct edge_prediction {
474 struct edge_prediction *ep_next;
475 edge ep_edge;
476 enum br_predictor ep_predictor;
477 int ep_probability;
480 /* This map contains for a basic block the list of predictions for the
481 outgoing edges. */
483 static hash_map<const_basic_block, edge_prediction *> *bb_predictions;
485 /* Return true if the one of outgoing edges is already predicted by
486 PREDICTOR. */
488 bool
489 gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
491 struct edge_prediction *i;
492 edge_prediction **preds = bb_predictions->get (bb);
494 if (!preds)
495 return false;
497 for (i = *preds; i; i = i->ep_next)
498 if (i->ep_predictor == predictor)
499 return true;
500 return false;
503 /* Return true if the one of outgoing edges is already predicted by
504 PREDICTOR for edge E predicted as TAKEN. */
506 bool
507 edge_predicted_by_p (edge e, enum br_predictor predictor, bool taken)
509 struct edge_prediction *i;
510 basic_block bb = e->src;
511 edge_prediction **preds = bb_predictions->get (bb);
512 if (!preds)
513 return false;
515 int probability = predictor_info[(int) predictor].hitrate;
517 if (taken != TAKEN)
518 probability = REG_BR_PROB_BASE - probability;
520 for (i = *preds; i; i = i->ep_next)
521 if (i->ep_predictor == predictor
522 && i->ep_edge == e
523 && i->ep_probability == probability)
524 return true;
525 return false;
528 /* Return true when the probability of edge is reliable.
530 The profile guessing code is good at predicting branch outcome (ie.
531 taken/not taken), that is predicted right slightly over 75% of time.
532 It is however notoriously poor on predicting the probability itself.
533 In general the profile appear a lot flatter (with probabilities closer
534 to 50%) than the reality so it is bad idea to use it to drive optimization
535 such as those disabling dynamic branch prediction for well predictable
536 branches.
538 There are two exceptions - edges leading to noreturn edges and edges
539 predicted by number of iterations heuristics are predicted well. This macro
540 should be able to distinguish those, but at the moment it simply check for
541 noreturn heuristic that is only one giving probability over 99% or bellow
542 1%. In future we might want to propagate reliability information across the
543 CFG if we find this information useful on multiple places. */
544 static bool
545 probability_reliable_p (int prob)
547 return (profile_status_for_fn (cfun) == PROFILE_READ
548 || (profile_status_for_fn (cfun) == PROFILE_GUESSED
549 && (prob <= HITRATE (1) || prob >= HITRATE (99))));
552 /* Same predicate as above, working on edges. */
553 bool
554 edge_probability_reliable_p (const_edge e)
556 return probability_reliable_p (e->probability);
559 /* Same predicate as edge_probability_reliable_p, working on notes. */
560 bool
561 br_prob_note_reliable_p (const_rtx note)
563 gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB);
564 return probability_reliable_p (XINT (note, 0));
567 static void
568 predict_insn (rtx_insn *insn, enum br_predictor predictor, int probability)
570 gcc_assert (any_condjump_p (insn));
571 if (!flag_guess_branch_prob)
572 return;
574 add_reg_note (insn, REG_BR_PRED,
575 gen_rtx_CONCAT (VOIDmode,
576 GEN_INT ((int) predictor),
577 GEN_INT ((int) probability)));
580 /* Predict insn by given predictor. */
582 void
583 predict_insn_def (rtx_insn *insn, enum br_predictor predictor,
584 enum prediction taken)
586 int probability = predictor_info[(int) predictor].hitrate;
588 if (taken != TAKEN)
589 probability = REG_BR_PROB_BASE - probability;
591 predict_insn (insn, predictor, probability);
594 /* Predict edge E with given probability if possible. */
596 void
597 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
599 rtx_insn *last_insn;
600 last_insn = BB_END (e->src);
602 /* We can store the branch prediction information only about
603 conditional jumps. */
604 if (!any_condjump_p (last_insn))
605 return;
607 /* We always store probability of branching. */
608 if (e->flags & EDGE_FALLTHRU)
609 probability = REG_BR_PROB_BASE - probability;
611 predict_insn (last_insn, predictor, probability);
614 /* Predict edge E with the given PROBABILITY. */
615 void
616 gimple_predict_edge (edge e, enum br_predictor predictor, int probability)
618 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
619 && EDGE_COUNT (e->src->succs) > 1
620 && flag_guess_branch_prob
621 && optimize)
623 struct edge_prediction *i = XNEW (struct edge_prediction);
624 edge_prediction *&preds = bb_predictions->get_or_insert (e->src);
626 i->ep_next = preds;
627 preds = i;
628 i->ep_probability = probability;
629 i->ep_predictor = predictor;
630 i->ep_edge = e;
634 /* Filter edge predictions PREDS by a function FILTER. DATA are passed
635 to the filter function. */
637 void
638 filter_predictions (edge_prediction **preds,
639 bool (*filter) (edge_prediction *, void *), void *data)
641 if (!bb_predictions)
642 return;
644 if (preds)
646 struct edge_prediction **prediction = preds;
647 struct edge_prediction *next;
649 while (*prediction)
651 if ((*filter) (*prediction, data))
652 prediction = &((*prediction)->ep_next);
653 else
655 next = (*prediction)->ep_next;
656 free (*prediction);
657 *prediction = next;
663 /* Filter function predicate that returns true for a edge predicate P
664 if its edge is equal to DATA. */
666 bool
667 equal_edge_p (edge_prediction *p, void *data)
669 return p->ep_edge == (edge)data;
672 /* Remove all predictions on given basic block that are attached
673 to edge E. */
674 void
675 remove_predictions_associated_with_edge (edge e)
677 if (!bb_predictions)
678 return;
680 edge_prediction **preds = bb_predictions->get (e->src);
681 filter_predictions (preds, equal_edge_p, e);
684 /* Clears the list of predictions stored for BB. */
686 static void
687 clear_bb_predictions (basic_block bb)
689 edge_prediction **preds = bb_predictions->get (bb);
690 struct edge_prediction *pred, *next;
692 if (!preds)
693 return;
695 for (pred = *preds; pred; pred = next)
697 next = pred->ep_next;
698 free (pred);
700 *preds = NULL;
703 /* Return true when we can store prediction on insn INSN.
704 At the moment we represent predictions only on conditional
705 jumps, not at computed jump or other complicated cases. */
706 static bool
707 can_predict_insn_p (const rtx_insn *insn)
709 return (JUMP_P (insn)
710 && any_condjump_p (insn)
711 && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2);
714 /* Predict edge E by given predictor if possible. */
716 void
717 predict_edge_def (edge e, enum br_predictor predictor,
718 enum prediction taken)
720 int probability = predictor_info[(int) predictor].hitrate;
722 if (taken != TAKEN)
723 probability = REG_BR_PROB_BASE - probability;
725 predict_edge (e, predictor, probability);
728 /* Invert all branch predictions or probability notes in the INSN. This needs
729 to be done each time we invert the condition used by the jump. */
731 void
732 invert_br_probabilities (rtx insn)
734 rtx note;
736 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
737 if (REG_NOTE_KIND (note) == REG_BR_PROB)
738 XINT (note, 0) = REG_BR_PROB_BASE - XINT (note, 0);
739 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
740 XEXP (XEXP (note, 0), 1)
741 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
744 /* Dump information about the branch prediction to the output file. */
746 static void
747 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
748 basic_block bb, enum predictor_reason reason = REASON_NONE,
749 edge ep_edge = NULL)
751 edge e = ep_edge;
752 edge_iterator ei;
754 if (!file)
755 return;
757 if (e == NULL)
758 FOR_EACH_EDGE (e, ei, bb->succs)
759 if (! (e->flags & EDGE_FALLTHRU))
760 break;
762 char edge_info_str[128];
763 if (ep_edge)
764 sprintf (edge_info_str, " of edge %d->%d", ep_edge->src->index,
765 ep_edge->dest->index);
766 else
767 edge_info_str[0] = '\0';
769 fprintf (file, " %s heuristics%s%s: %.1f%%",
770 predictor_info[predictor].name,
771 edge_info_str, reason_messages[reason],
772 probability * 100.0 / REG_BR_PROB_BASE);
774 if (bb->count)
776 fprintf (file, " exec %" PRId64, bb->count);
777 if (e)
779 fprintf (file, " hit %" PRId64, e->count);
780 fprintf (file, " (%.1f%%)", e->count * 100.0 / bb->count);
784 fprintf (file, "\n");
787 /* We can not predict the probabilities of outgoing edges of bb. Set them
788 evenly and hope for the best. If UNLIKELY_EDGES is not null, distribute
789 even probability for all edges not mentioned in the set. These edges
790 are given PROB_VERY_UNLIKELY probability. */
792 static void
793 set_even_probabilities (basic_block bb,
794 hash_set<edge> *unlikely_edges = NULL)
796 unsigned nedges = 0;
797 edge e;
798 edge_iterator ei;
800 FOR_EACH_EDGE (e, ei, bb->succs)
801 if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
802 nedges ++;
804 /* Make the distribution even if all edges are unlikely. */
805 unsigned unlikely_count = unlikely_edges ? unlikely_edges->elements () : 0;
806 if (unlikely_count == nedges)
808 unlikely_edges = NULL;
809 unlikely_count = 0;
812 unsigned c = nedges - unlikely_count;
814 FOR_EACH_EDGE (e, ei, bb->succs)
815 if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
817 if (unlikely_edges != NULL && unlikely_edges->contains (e))
818 e->probability = PROB_VERY_UNLIKELY;
819 else
820 e->probability = (REG_BR_PROB_BASE + c / 2) / c;
822 else
823 e->probability = 0;
826 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
827 note if not already present. Remove now useless REG_BR_PRED notes. */
829 static void
830 combine_predictions_for_insn (rtx_insn *insn, basic_block bb)
832 rtx prob_note;
833 rtx *pnote;
834 rtx note;
835 int best_probability = PROB_EVEN;
836 enum br_predictor best_predictor = END_PREDICTORS;
837 int combined_probability = REG_BR_PROB_BASE / 2;
838 int d;
839 bool first_match = false;
840 bool found = false;
842 if (!can_predict_insn_p (insn))
844 set_even_probabilities (bb);
845 return;
848 prob_note = find_reg_note (insn, REG_BR_PROB, 0);
849 pnote = &REG_NOTES (insn);
850 if (dump_file)
851 fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
852 bb->index);
854 /* We implement "first match" heuristics and use probability guessed
855 by predictor with smallest index. */
856 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
857 if (REG_NOTE_KIND (note) == REG_BR_PRED)
859 enum br_predictor predictor = ((enum br_predictor)
860 INTVAL (XEXP (XEXP (note, 0), 0)));
861 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
863 found = true;
864 if (best_predictor > predictor
865 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
866 best_probability = probability, best_predictor = predictor;
868 d = (combined_probability * probability
869 + (REG_BR_PROB_BASE - combined_probability)
870 * (REG_BR_PROB_BASE - probability));
872 /* Use FP math to avoid overflows of 32bit integers. */
873 if (d == 0)
874 /* If one probability is 0% and one 100%, avoid division by zero. */
875 combined_probability = REG_BR_PROB_BASE / 2;
876 else
877 combined_probability = (((double) combined_probability) * probability
878 * REG_BR_PROB_BASE / d + 0.5);
881 /* Decide which heuristic to use. In case we didn't match anything,
882 use no_prediction heuristic, in case we did match, use either
883 first match or Dempster-Shaffer theory depending on the flags. */
885 if (best_predictor != END_PREDICTORS)
886 first_match = true;
888 if (!found)
889 dump_prediction (dump_file, PRED_NO_PREDICTION,
890 combined_probability, bb);
891 else
893 if (!first_match)
894 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
895 bb, !first_match ? REASON_NONE : REASON_IGNORED);
896 else
897 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
898 bb, first_match ? REASON_NONE : REASON_IGNORED);
901 if (first_match)
902 combined_probability = best_probability;
903 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
905 while (*pnote)
907 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
909 enum br_predictor predictor = ((enum br_predictor)
910 INTVAL (XEXP (XEXP (*pnote, 0), 0)));
911 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
913 dump_prediction (dump_file, predictor, probability, bb,
914 (!first_match || best_predictor == predictor)
915 ? REASON_NONE : REASON_IGNORED);
916 *pnote = XEXP (*pnote, 1);
918 else
919 pnote = &XEXP (*pnote, 1);
922 if (!prob_note)
924 add_int_reg_note (insn, REG_BR_PROB, combined_probability);
926 /* Save the prediction into CFG in case we are seeing non-degenerated
927 conditional jump. */
928 if (!single_succ_p (bb))
930 BRANCH_EDGE (bb)->probability = combined_probability;
931 FALLTHRU_EDGE (bb)->probability
932 = REG_BR_PROB_BASE - combined_probability;
935 else if (!single_succ_p (bb))
937 int prob = XINT (prob_note, 0);
939 BRANCH_EDGE (bb)->probability = prob;
940 FALLTHRU_EDGE (bb)->probability = REG_BR_PROB_BASE - prob;
942 else
943 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
946 /* Edge prediction hash traits. */
948 struct predictor_hash: pointer_hash <edge_prediction>
951 static inline hashval_t hash (const edge_prediction *);
952 static inline bool equal (const edge_prediction *, const edge_prediction *);
955 /* Calculate hash value of an edge prediction P based on predictor and
956 normalized probability. */
958 inline hashval_t
959 predictor_hash::hash (const edge_prediction *p)
961 inchash::hash hstate;
962 hstate.add_int (p->ep_predictor);
964 int prob = p->ep_probability;
965 if (prob > REG_BR_PROB_BASE / 2)
966 prob = REG_BR_PROB_BASE - prob;
968 hstate.add_int (prob);
970 return hstate.end ();
973 /* Return true whether edge predictions P1 and P2 use the same predictor and
974 have equal (or opposed probability). */
976 inline bool
977 predictor_hash::equal (const edge_prediction *p1, const edge_prediction *p2)
979 return (p1->ep_predictor == p2->ep_predictor
980 && (p1->ep_probability == p2->ep_probability
981 || p1->ep_probability == REG_BR_PROB_BASE - p2->ep_probability));
984 struct predictor_hash_traits: predictor_hash,
985 typed_noop_remove <edge_prediction *> {};
987 /* Return true if edge prediction P is not in DATA hash set. */
989 static bool
990 not_removed_prediction_p (edge_prediction *p, void *data)
992 hash_set<edge_prediction *> *remove = (hash_set<edge_prediction *> *) data;
993 return !remove->contains (p);
996 /* Prune predictions for a basic block BB. Currently we do following
997 clean-up steps:
999 1) remove duplicate prediction that is guessed with the same probability
1000 (different than 1/2) to both edge
1001 2) remove duplicates for a prediction that belongs with the same probability
1002 to a single edge
1006 static void
1007 prune_predictions_for_bb (basic_block bb)
1009 edge_prediction **preds = bb_predictions->get (bb);
1011 if (preds)
1013 hash_table <predictor_hash_traits> s (13);
1014 hash_set <edge_prediction *> remove;
1016 /* Step 1: identify predictors that should be removed. */
1017 for (edge_prediction *pred = *preds; pred; pred = pred->ep_next)
1019 edge_prediction *existing = s.find (pred);
1020 if (existing)
1022 if (pred->ep_edge == existing->ep_edge
1023 && pred->ep_probability == existing->ep_probability)
1025 /* Remove a duplicate predictor. */
1026 dump_prediction (dump_file, pred->ep_predictor,
1027 pred->ep_probability, bb,
1028 REASON_SINGLE_EDGE_DUPLICATE, pred->ep_edge);
1030 remove.add (pred);
1032 else if (pred->ep_edge != existing->ep_edge
1033 && pred->ep_probability == existing->ep_probability
1034 && pred->ep_probability != REG_BR_PROB_BASE / 2)
1036 /* Remove both predictors as they predict the same
1037 for both edges. */
1038 dump_prediction (dump_file, existing->ep_predictor,
1039 pred->ep_probability, bb,
1040 REASON_EDGE_PAIR_DUPLICATE,
1041 existing->ep_edge);
1042 dump_prediction (dump_file, pred->ep_predictor,
1043 pred->ep_probability, bb,
1044 REASON_EDGE_PAIR_DUPLICATE,
1045 pred->ep_edge);
1047 remove.add (existing);
1048 remove.add (pred);
1052 edge_prediction **slot2 = s.find_slot (pred, INSERT);
1053 *slot2 = pred;
1056 /* Step 2: Remove predictors. */
1057 filter_predictions (preds, not_removed_prediction_p, &remove);
1061 /* Combine predictions into single probability and store them into CFG.
1062 Remove now useless prediction entries.
1063 If DRY_RUN is set, only produce dumps and do not modify profile. */
1065 static void
1066 combine_predictions_for_bb (basic_block bb, bool dry_run)
1068 int best_probability = PROB_EVEN;
1069 enum br_predictor best_predictor = END_PREDICTORS;
1070 int combined_probability = REG_BR_PROB_BASE / 2;
1071 int d;
1072 bool first_match = false;
1073 bool found = false;
1074 struct edge_prediction *pred;
1075 int nedges = 0;
1076 edge e, first = NULL, second = NULL;
1077 edge_iterator ei;
1079 FOR_EACH_EDGE (e, ei, bb->succs)
1080 if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
1082 nedges ++;
1083 if (first && !second)
1084 second = e;
1085 if (!first)
1086 first = e;
1089 /* When there is no successor or only one choice, prediction is easy.
1091 When we have a basic block with more than 2 successors, the situation
1092 is more complicated as DS theory cannot be used literally.
1093 More precisely, let's assume we predicted edge e1 with probability p1,
1094 thus: m1({b1}) = p1. As we're going to combine more than 2 edges, we
1095 need to find probability of e.g. m1({b2}), which we don't know.
1096 The only approximation is to equally distribute 1-p1 to all edges
1097 different from b1.
1099 According to numbers we've got from SPEC2006 benchark, there's only
1100 one interesting reliable predictor (noreturn call), which can be
1101 handled with a bit easier approach. */
1102 if (nedges != 2)
1104 hash_set<edge> unlikely_edges (4);
1106 /* Identify all edges that have a probability close to very unlikely.
1107 Doing the approach for very unlikely doesn't worth for doing as
1108 there's no such probability in SPEC2006 benchmark. */
1109 edge_prediction **preds = bb_predictions->get (bb);
1110 if (preds)
1111 for (pred = *preds; pred; pred = pred->ep_next)
1112 if (pred->ep_probability <= PROB_VERY_UNLIKELY)
1113 unlikely_edges.add (pred->ep_edge);
1115 if (!bb->count && !dry_run)
1116 set_even_probabilities (bb, &unlikely_edges);
1117 clear_bb_predictions (bb);
1118 if (dump_file)
1120 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1121 if (unlikely_edges.elements () == 0)
1122 fprintf (dump_file,
1123 "%i edges in bb %i predicted to even probabilities\n",
1124 nedges, bb->index);
1125 else
1127 fprintf (dump_file,
1128 "%i edges in bb %i predicted with some unlikely edges\n",
1129 nedges, bb->index);
1130 FOR_EACH_EDGE (e, ei, bb->succs)
1131 if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
1132 dump_prediction (dump_file, PRED_COMBINED, e->probability,
1133 bb, REASON_NONE, e);
1136 return;
1139 if (dump_file)
1140 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1142 prune_predictions_for_bb (bb);
1144 edge_prediction **preds = bb_predictions->get (bb);
1146 if (preds)
1148 /* We implement "first match" heuristics and use probability guessed
1149 by predictor with smallest index. */
1150 for (pred = *preds; pred; pred = pred->ep_next)
1152 enum br_predictor predictor = pred->ep_predictor;
1153 int probability = pred->ep_probability;
1155 if (pred->ep_edge != first)
1156 probability = REG_BR_PROB_BASE - probability;
1158 found = true;
1159 /* First match heuristics would be widly confused if we predicted
1160 both directions. */
1161 if (best_predictor > predictor
1162 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
1164 struct edge_prediction *pred2;
1165 int prob = probability;
1167 for (pred2 = (struct edge_prediction *) *preds;
1168 pred2; pred2 = pred2->ep_next)
1169 if (pred2 != pred && pred2->ep_predictor == pred->ep_predictor)
1171 int probability2 = pred2->ep_probability;
1173 if (pred2->ep_edge != first)
1174 probability2 = REG_BR_PROB_BASE - probability2;
1176 if ((probability < REG_BR_PROB_BASE / 2) !=
1177 (probability2 < REG_BR_PROB_BASE / 2))
1178 break;
1180 /* If the same predictor later gave better result, go for it! */
1181 if ((probability >= REG_BR_PROB_BASE / 2 && (probability2 > probability))
1182 || (probability <= REG_BR_PROB_BASE / 2 && (probability2 < probability)))
1183 prob = probability2;
1185 if (!pred2)
1186 best_probability = prob, best_predictor = predictor;
1189 d = (combined_probability * probability
1190 + (REG_BR_PROB_BASE - combined_probability)
1191 * (REG_BR_PROB_BASE - probability));
1193 /* Use FP math to avoid overflows of 32bit integers. */
1194 if (d == 0)
1195 /* If one probability is 0% and one 100%, avoid division by zero. */
1196 combined_probability = REG_BR_PROB_BASE / 2;
1197 else
1198 combined_probability = (((double) combined_probability)
1199 * probability
1200 * REG_BR_PROB_BASE / d + 0.5);
1204 /* Decide which heuristic to use. In case we didn't match anything,
1205 use no_prediction heuristic, in case we did match, use either
1206 first match or Dempster-Shaffer theory depending on the flags. */
1208 if (best_predictor != END_PREDICTORS)
1209 first_match = true;
1211 if (!found)
1212 dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb);
1213 else
1215 if (!first_match)
1216 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb,
1217 !first_match ? REASON_NONE : REASON_IGNORED);
1218 else
1219 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb,
1220 first_match ? REASON_NONE : REASON_IGNORED);
1223 if (first_match)
1224 combined_probability = best_probability;
1225 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
1227 if (preds)
1229 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next)
1231 enum br_predictor predictor = pred->ep_predictor;
1232 int probability = pred->ep_probability;
1234 dump_prediction (dump_file, predictor, probability, bb,
1235 (!first_match || best_predictor == predictor)
1236 ? REASON_NONE : REASON_IGNORED, pred->ep_edge);
1239 clear_bb_predictions (bb);
1241 if (!bb->count && !dry_run)
1243 first->probability = combined_probability;
1244 second->probability = REG_BR_PROB_BASE - combined_probability;
1248 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1249 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1251 T1 and T2 should be one of the following cases:
1252 1. T1 is SSA_NAME, T2 is NULL
1253 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1254 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1256 static tree
1257 strips_small_constant (tree t1, tree t2)
1259 tree ret = NULL;
1260 int value = 0;
1262 if (!t1)
1263 return NULL;
1264 else if (TREE_CODE (t1) == SSA_NAME)
1265 ret = t1;
1266 else if (tree_fits_shwi_p (t1))
1267 value = tree_to_shwi (t1);
1268 else
1269 return NULL;
1271 if (!t2)
1272 return ret;
1273 else if (tree_fits_shwi_p (t2))
1274 value = tree_to_shwi (t2);
1275 else if (TREE_CODE (t2) == SSA_NAME)
1277 if (ret)
1278 return NULL;
1279 else
1280 ret = t2;
1283 if (value <= 4 && value >= -4)
1284 return ret;
1285 else
1286 return NULL;
1289 /* Return the SSA_NAME in T or T's operands.
1290 Return NULL if SSA_NAME cannot be found. */
1292 static tree
1293 get_base_value (tree t)
1295 if (TREE_CODE (t) == SSA_NAME)
1296 return t;
1298 if (!BINARY_CLASS_P (t))
1299 return NULL;
1301 switch (TREE_OPERAND_LENGTH (t))
1303 case 1:
1304 return strips_small_constant (TREE_OPERAND (t, 0), NULL);
1305 case 2:
1306 return strips_small_constant (TREE_OPERAND (t, 0),
1307 TREE_OPERAND (t, 1));
1308 default:
1309 return NULL;
1313 /* Check the compare STMT in LOOP. If it compares an induction
1314 variable to a loop invariant, return true, and save
1315 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1316 Otherwise return false and set LOOP_INVAIANT to NULL. */
1318 static bool
1319 is_comparison_with_loop_invariant_p (gcond *stmt, struct loop *loop,
1320 tree *loop_invariant,
1321 enum tree_code *compare_code,
1322 tree *loop_step,
1323 tree *loop_iv_base)
1325 tree op0, op1, bound, base;
1326 affine_iv iv0, iv1;
1327 enum tree_code code;
1328 tree step;
1330 code = gimple_cond_code (stmt);
1331 *loop_invariant = NULL;
1333 switch (code)
1335 case GT_EXPR:
1336 case GE_EXPR:
1337 case NE_EXPR:
1338 case LT_EXPR:
1339 case LE_EXPR:
1340 case EQ_EXPR:
1341 break;
1343 default:
1344 return false;
1347 op0 = gimple_cond_lhs (stmt);
1348 op1 = gimple_cond_rhs (stmt);
1350 if ((TREE_CODE (op0) != SSA_NAME && TREE_CODE (op0) != INTEGER_CST)
1351 || (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op1) != INTEGER_CST))
1352 return false;
1353 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, true))
1354 return false;
1355 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, true))
1356 return false;
1357 if (TREE_CODE (iv0.step) != INTEGER_CST
1358 || TREE_CODE (iv1.step) != INTEGER_CST)
1359 return false;
1360 if ((integer_zerop (iv0.step) && integer_zerop (iv1.step))
1361 || (!integer_zerop (iv0.step) && !integer_zerop (iv1.step)))
1362 return false;
1364 if (integer_zerop (iv0.step))
1366 if (code != NE_EXPR && code != EQ_EXPR)
1367 code = invert_tree_comparison (code, false);
1368 bound = iv0.base;
1369 base = iv1.base;
1370 if (tree_fits_shwi_p (iv1.step))
1371 step = iv1.step;
1372 else
1373 return false;
1375 else
1377 bound = iv1.base;
1378 base = iv0.base;
1379 if (tree_fits_shwi_p (iv0.step))
1380 step = iv0.step;
1381 else
1382 return false;
1385 if (TREE_CODE (bound) != INTEGER_CST)
1386 bound = get_base_value (bound);
1387 if (!bound)
1388 return false;
1389 if (TREE_CODE (base) != INTEGER_CST)
1390 base = get_base_value (base);
1391 if (!base)
1392 return false;
1394 *loop_invariant = bound;
1395 *compare_code = code;
1396 *loop_step = step;
1397 *loop_iv_base = base;
1398 return true;
1401 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1403 static bool
1404 expr_coherent_p (tree t1, tree t2)
1406 gimple *stmt;
1407 tree ssa_name_1 = NULL;
1408 tree ssa_name_2 = NULL;
1410 gcc_assert (TREE_CODE (t1) == SSA_NAME || TREE_CODE (t1) == INTEGER_CST);
1411 gcc_assert (TREE_CODE (t2) == SSA_NAME || TREE_CODE (t2) == INTEGER_CST);
1413 if (t1 == t2)
1414 return true;
1416 if (TREE_CODE (t1) == INTEGER_CST && TREE_CODE (t2) == INTEGER_CST)
1417 return true;
1418 if (TREE_CODE (t1) == INTEGER_CST || TREE_CODE (t2) == INTEGER_CST)
1419 return false;
1421 /* Check to see if t1 is expressed/defined with t2. */
1422 stmt = SSA_NAME_DEF_STMT (t1);
1423 gcc_assert (stmt != NULL);
1424 if (is_gimple_assign (stmt))
1426 ssa_name_1 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1427 if (ssa_name_1 && ssa_name_1 == t2)
1428 return true;
1431 /* Check to see if t2 is expressed/defined with t1. */
1432 stmt = SSA_NAME_DEF_STMT (t2);
1433 gcc_assert (stmt != NULL);
1434 if (is_gimple_assign (stmt))
1436 ssa_name_2 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1437 if (ssa_name_2 && ssa_name_2 == t1)
1438 return true;
1441 /* Compare if t1 and t2's def_stmts are identical. */
1442 if (ssa_name_2 != NULL && ssa_name_1 == ssa_name_2)
1443 return true;
1444 else
1445 return false;
1448 /* Return true if E is predicted by one of loop heuristics. */
1450 static bool
1451 predicted_by_loop_heuristics_p (basic_block bb)
1453 struct edge_prediction *i;
1454 edge_prediction **preds = bb_predictions->get (bb);
1456 if (!preds)
1457 return false;
1459 for (i = *preds; i; i = i->ep_next)
1460 if (i->ep_predictor == PRED_LOOP_ITERATIONS_GUESSED
1461 || i->ep_predictor == PRED_LOOP_ITERATIONS_MAX
1462 || i->ep_predictor == PRED_LOOP_ITERATIONS
1463 || i->ep_predictor == PRED_LOOP_EXIT
1464 || i->ep_predictor == PRED_LOOP_EXIT_WITH_RECURSION
1465 || i->ep_predictor == PRED_LOOP_EXTRA_EXIT)
1466 return true;
1467 return false;
1470 /* Predict branch probability of BB when BB contains a branch that compares
1471 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1472 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1474 E.g.
1475 for (int i = 0; i < bound; i++) {
1476 if (i < bound - 2)
1477 computation_1();
1478 else
1479 computation_2();
1482 In this loop, we will predict the branch inside the loop to be taken. */
1484 static void
1485 predict_iv_comparison (struct loop *loop, basic_block bb,
1486 tree loop_bound_var,
1487 tree loop_iv_base_var,
1488 enum tree_code loop_bound_code,
1489 int loop_bound_step)
1491 gimple *stmt;
1492 tree compare_var, compare_base;
1493 enum tree_code compare_code;
1494 tree compare_step_var;
1495 edge then_edge;
1496 edge_iterator ei;
1498 if (predicted_by_loop_heuristics_p (bb))
1499 return;
1501 stmt = last_stmt (bb);
1502 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1503 return;
1504 if (!is_comparison_with_loop_invariant_p (as_a <gcond *> (stmt),
1505 loop, &compare_var,
1506 &compare_code,
1507 &compare_step_var,
1508 &compare_base))
1509 return;
1511 /* Find the taken edge. */
1512 FOR_EACH_EDGE (then_edge, ei, bb->succs)
1513 if (then_edge->flags & EDGE_TRUE_VALUE)
1514 break;
1516 /* When comparing an IV to a loop invariant, NE is more likely to be
1517 taken while EQ is more likely to be not-taken. */
1518 if (compare_code == NE_EXPR)
1520 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1521 return;
1523 else if (compare_code == EQ_EXPR)
1525 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1526 return;
1529 if (!expr_coherent_p (loop_iv_base_var, compare_base))
1530 return;
1532 /* If loop bound, base and compare bound are all constants, we can
1533 calculate the probability directly. */
1534 if (tree_fits_shwi_p (loop_bound_var)
1535 && tree_fits_shwi_p (compare_var)
1536 && tree_fits_shwi_p (compare_base))
1538 int probability;
1539 bool overflow, overall_overflow = false;
1540 widest_int compare_count, tem;
1542 /* (loop_bound - base) / compare_step */
1543 tem = wi::sub (wi::to_widest (loop_bound_var),
1544 wi::to_widest (compare_base), SIGNED, &overflow);
1545 overall_overflow |= overflow;
1546 widest_int loop_count = wi::div_trunc (tem,
1547 wi::to_widest (compare_step_var),
1548 SIGNED, &overflow);
1549 overall_overflow |= overflow;
1551 if (!wi::neg_p (wi::to_widest (compare_step_var))
1552 ^ (compare_code == LT_EXPR || compare_code == LE_EXPR))
1554 /* (loop_bound - compare_bound) / compare_step */
1555 tem = wi::sub (wi::to_widest (loop_bound_var),
1556 wi::to_widest (compare_var), SIGNED, &overflow);
1557 overall_overflow |= overflow;
1558 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1559 SIGNED, &overflow);
1560 overall_overflow |= overflow;
1562 else
1564 /* (compare_bound - base) / compare_step */
1565 tem = wi::sub (wi::to_widest (compare_var),
1566 wi::to_widest (compare_base), SIGNED, &overflow);
1567 overall_overflow |= overflow;
1568 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1569 SIGNED, &overflow);
1570 overall_overflow |= overflow;
1572 if (compare_code == LE_EXPR || compare_code == GE_EXPR)
1573 ++compare_count;
1574 if (loop_bound_code == LE_EXPR || loop_bound_code == GE_EXPR)
1575 ++loop_count;
1576 if (wi::neg_p (compare_count))
1577 compare_count = 0;
1578 if (wi::neg_p (loop_count))
1579 loop_count = 0;
1580 if (loop_count == 0)
1581 probability = 0;
1582 else if (wi::cmps (compare_count, loop_count) == 1)
1583 probability = REG_BR_PROB_BASE;
1584 else
1586 tem = compare_count * REG_BR_PROB_BASE;
1587 tem = wi::udiv_trunc (tem, loop_count);
1588 probability = tem.to_uhwi ();
1591 /* FIXME: The branch prediction seems broken. It has only 20% hitrate. */
1592 if (!overall_overflow)
1593 predict_edge (then_edge, PRED_LOOP_IV_COMPARE, probability);
1595 return;
1598 if (expr_coherent_p (loop_bound_var, compare_var))
1600 if ((loop_bound_code == LT_EXPR || loop_bound_code == LE_EXPR)
1601 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1602 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1603 else if ((loop_bound_code == GT_EXPR || loop_bound_code == GE_EXPR)
1604 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1605 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1606 else if (loop_bound_code == NE_EXPR)
1608 /* If the loop backedge condition is "(i != bound)", we do
1609 the comparison based on the step of IV:
1610 * step < 0 : backedge condition is like (i > bound)
1611 * step > 0 : backedge condition is like (i < bound) */
1612 gcc_assert (loop_bound_step != 0);
1613 if (loop_bound_step > 0
1614 && (compare_code == LT_EXPR
1615 || compare_code == LE_EXPR))
1616 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1617 else if (loop_bound_step < 0
1618 && (compare_code == GT_EXPR
1619 || compare_code == GE_EXPR))
1620 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1621 else
1622 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1624 else
1625 /* The branch is predicted not-taken if loop_bound_code is
1626 opposite with compare_code. */
1627 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1629 else if (expr_coherent_p (loop_iv_base_var, compare_var))
1631 /* For cases like:
1632 for (i = s; i < h; i++)
1633 if (i > s + 2) ....
1634 The branch should be predicted taken. */
1635 if (loop_bound_step > 0
1636 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1637 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1638 else if (loop_bound_step < 0
1639 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1640 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1641 else
1642 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1646 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1647 exits are resulted from short-circuit conditions that will generate an
1648 if_tmp. E.g.:
1650 if (foo() || global > 10)
1651 break;
1653 This will be translated into:
1655 BB3:
1656 loop header...
1657 BB4:
1658 if foo() goto BB6 else goto BB5
1659 BB5:
1660 if global > 10 goto BB6 else goto BB7
1661 BB6:
1662 goto BB7
1663 BB7:
1664 iftmp = (PHI 0(BB5), 1(BB6))
1665 if iftmp == 1 goto BB8 else goto BB3
1666 BB8:
1667 outside of the loop...
1669 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1670 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1671 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1672 exits to predict them using PRED_LOOP_EXTRA_EXIT. */
1674 static void
1675 predict_extra_loop_exits (edge exit_edge)
1677 unsigned i;
1678 bool check_value_one;
1679 gimple *lhs_def_stmt;
1680 gphi *phi_stmt;
1681 tree cmp_rhs, cmp_lhs;
1682 gimple *last;
1683 gcond *cmp_stmt;
1685 last = last_stmt (exit_edge->src);
1686 if (!last)
1687 return;
1688 cmp_stmt = dyn_cast <gcond *> (last);
1689 if (!cmp_stmt)
1690 return;
1692 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1693 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1694 if (!TREE_CONSTANT (cmp_rhs)
1695 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1696 return;
1697 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1698 return;
1700 /* If check_value_one is true, only the phi_args with value '1' will lead
1701 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1702 loop exit. */
1703 check_value_one = (((integer_onep (cmp_rhs))
1704 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1705 ^ ((exit_edge->flags & EDGE_TRUE_VALUE) != 0));
1707 lhs_def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1708 if (!lhs_def_stmt)
1709 return;
1711 phi_stmt = dyn_cast <gphi *> (lhs_def_stmt);
1712 if (!phi_stmt)
1713 return;
1715 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1717 edge e1;
1718 edge_iterator ei;
1719 tree val = gimple_phi_arg_def (phi_stmt, i);
1720 edge e = gimple_phi_arg_edge (phi_stmt, i);
1722 if (!TREE_CONSTANT (val) || !(integer_zerop (val) || integer_onep (val)))
1723 continue;
1724 if ((check_value_one ^ integer_onep (val)) == 1)
1725 continue;
1726 if (EDGE_COUNT (e->src->succs) != 1)
1728 predict_paths_leading_to_edge (e, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1729 continue;
1732 FOR_EACH_EDGE (e1, ei, e->src->preds)
1733 predict_paths_leading_to_edge (e1, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1738 /* Predict edge probabilities by exploiting loop structure. */
1740 static void
1741 predict_loops (void)
1743 struct loop *loop;
1744 basic_block bb;
1745 hash_set <struct loop *> with_recursion(10);
1747 FOR_EACH_BB_FN (bb, cfun)
1749 gimple_stmt_iterator gsi;
1750 tree decl;
1752 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1753 if (is_gimple_call (gsi_stmt (gsi))
1754 && (decl = gimple_call_fndecl (gsi_stmt (gsi))) != NULL
1755 && recursive_call_p (current_function_decl, decl))
1757 loop = bb->loop_father;
1758 while (loop && !with_recursion.add (loop))
1759 loop = loop_outer (loop);
1763 /* Try to predict out blocks in a loop that are not part of a
1764 natural loop. */
1765 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1767 basic_block bb, *bbs;
1768 unsigned j, n_exits = 0;
1769 vec<edge> exits;
1770 struct tree_niter_desc niter_desc;
1771 edge ex;
1772 struct nb_iter_bound *nb_iter;
1773 enum tree_code loop_bound_code = ERROR_MARK;
1774 tree loop_bound_step = NULL;
1775 tree loop_bound_var = NULL;
1776 tree loop_iv_base = NULL;
1777 gcond *stmt = NULL;
1778 bool recursion = with_recursion.contains (loop);
1780 exits = get_loop_exit_edges (loop);
1781 FOR_EACH_VEC_ELT (exits, j, ex)
1782 if (!(ex->flags & (EDGE_EH | EDGE_ABNORMAL_CALL | EDGE_FAKE)))
1783 n_exits ++;
1784 if (!n_exits)
1786 exits.release ();
1787 continue;
1790 if (dump_file && (dump_flags & TDF_DETAILS))
1791 fprintf (dump_file, "Predicting loop %i%s with %i exits.\n",
1792 loop->num, recursion ? " (with recursion)":"", n_exits);
1793 if (dump_file && (dump_flags & TDF_DETAILS)
1794 && max_loop_iterations_int (loop) >= 0)
1796 fprintf (dump_file,
1797 "Loop %d iterates at most %i times.\n", loop->num,
1798 (int)max_loop_iterations_int (loop));
1800 if (dump_file && (dump_flags & TDF_DETAILS)
1801 && likely_max_loop_iterations_int (loop) >= 0)
1803 fprintf (dump_file, "Loop %d likely iterates at most %i times.\n",
1804 loop->num, (int)likely_max_loop_iterations_int (loop));
1807 FOR_EACH_VEC_ELT (exits, j, ex)
1809 tree niter = NULL;
1810 HOST_WIDE_INT nitercst;
1811 int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS);
1812 int probability;
1813 enum br_predictor predictor;
1814 widest_int nit;
1816 if (ex->flags & (EDGE_EH | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1817 continue;
1818 /* Loop heuristics do not expect exit conditional to be inside
1819 inner loop. We predict from innermost to outermost loop. */
1820 if (predicted_by_loop_heuristics_p (ex->src))
1822 if (dump_file && (dump_flags & TDF_DETAILS))
1823 fprintf (dump_file, "Skipping exit %i->%i because "
1824 "it is already predicted.\n",
1825 ex->src->index, ex->dest->index);
1826 continue;
1828 predict_extra_loop_exits (ex);
1830 if (number_of_iterations_exit (loop, ex, &niter_desc, false, false))
1831 niter = niter_desc.niter;
1832 if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST)
1833 niter = loop_niter_by_eval (loop, ex);
1834 if (dump_file && (dump_flags & TDF_DETAILS)
1835 && TREE_CODE (niter) == INTEGER_CST)
1837 fprintf (dump_file, "Exit %i->%i %d iterates ",
1838 ex->src->index, ex->dest->index,
1839 loop->num);
1840 print_generic_expr (dump_file, niter, TDF_SLIM);
1841 fprintf (dump_file, " times.\n");
1844 if (TREE_CODE (niter) == INTEGER_CST)
1846 if (tree_fits_uhwi_p (niter)
1847 && max
1848 && compare_tree_int (niter, max - 1) == -1)
1849 nitercst = tree_to_uhwi (niter) + 1;
1850 else
1851 nitercst = max;
1852 predictor = PRED_LOOP_ITERATIONS;
1854 /* If we have just one exit and we can derive some information about
1855 the number of iterations of the loop from the statements inside
1856 the loop, use it to predict this exit. */
1857 else if (n_exits == 1
1858 && estimated_stmt_executions (loop, &nit))
1860 if (wi::gtu_p (nit, max))
1861 nitercst = max;
1862 else
1863 nitercst = nit.to_shwi ();
1864 predictor = PRED_LOOP_ITERATIONS_GUESSED;
1866 /* If we have likely upper bound, trust it for very small iteration
1867 counts. Such loops would otherwise get mispredicted by standard
1868 LOOP_EXIT heuristics. */
1869 else if (n_exits == 1
1870 && likely_max_stmt_executions (loop, &nit)
1871 && wi::ltu_p (nit,
1872 RDIV (REG_BR_PROB_BASE,
1873 REG_BR_PROB_BASE
1874 - predictor_info
1875 [recursion
1876 ? PRED_LOOP_EXIT_WITH_RECURSION
1877 : PRED_LOOP_EXIT].hitrate)))
1879 nitercst = nit.to_shwi ();
1880 predictor = PRED_LOOP_ITERATIONS_MAX;
1882 else
1884 if (dump_file && (dump_flags & TDF_DETAILS))
1885 fprintf (dump_file, "Nothing known about exit %i->%i.\n",
1886 ex->src->index, ex->dest->index);
1887 continue;
1890 if (dump_file && (dump_flags & TDF_DETAILS))
1891 fprintf (dump_file, "Recording prediction to %i iterations by %s.\n",
1892 (int)nitercst, predictor_info[predictor].name);
1893 /* If the prediction for number of iterations is zero, do not
1894 predict the exit edges. */
1895 if (nitercst == 0)
1896 continue;
1898 probability = RDIV (REG_BR_PROB_BASE, nitercst);
1899 predict_edge (ex, predictor, probability);
1901 exits.release ();
1903 /* Find information about loop bound variables. */
1904 for (nb_iter = loop->bounds; nb_iter;
1905 nb_iter = nb_iter->next)
1906 if (nb_iter->stmt
1907 && gimple_code (nb_iter->stmt) == GIMPLE_COND)
1909 stmt = as_a <gcond *> (nb_iter->stmt);
1910 break;
1912 if (!stmt && last_stmt (loop->header)
1913 && gimple_code (last_stmt (loop->header)) == GIMPLE_COND)
1914 stmt = as_a <gcond *> (last_stmt (loop->header));
1915 if (stmt)
1916 is_comparison_with_loop_invariant_p (stmt, loop,
1917 &loop_bound_var,
1918 &loop_bound_code,
1919 &loop_bound_step,
1920 &loop_iv_base);
1922 bbs = get_loop_body (loop);
1924 for (j = 0; j < loop->num_nodes; j++)
1926 edge e;
1927 edge_iterator ei;
1929 bb = bbs[j];
1931 /* Bypass loop heuristics on continue statement. These
1932 statements construct loops via "non-loop" constructs
1933 in the source language and are better to be handled
1934 separately. */
1935 if (predicted_by_p (bb, PRED_CONTINUE))
1937 if (dump_file && (dump_flags & TDF_DETAILS))
1938 fprintf (dump_file, "BB %i predicted by continue.\n",
1939 bb->index);
1940 continue;
1943 /* If we already used more reliable loop exit predictors, do not
1944 bother with PRED_LOOP_EXIT. */
1945 if (!predicted_by_loop_heuristics_p (bb))
1947 /* For loop with many exits we don't want to predict all exits
1948 with the pretty large probability, because if all exits are
1949 considered in row, the loop would be predicted to iterate
1950 almost never. The code to divide probability by number of
1951 exits is very rough. It should compute the number of exits
1952 taken in each patch through function (not the overall number
1953 of exits that might be a lot higher for loops with wide switch
1954 statements in them) and compute n-th square root.
1956 We limit the minimal probability by 2% to avoid
1957 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
1958 as this was causing regression in perl benchmark containing such
1959 a wide loop. */
1961 int probability = ((REG_BR_PROB_BASE
1962 - predictor_info
1963 [recursion
1964 ? PRED_LOOP_EXIT_WITH_RECURSION
1965 : PRED_LOOP_EXIT].hitrate)
1966 / n_exits);
1967 if (probability < HITRATE (2))
1968 probability = HITRATE (2);
1969 FOR_EACH_EDGE (e, ei, bb->succs)
1970 if (e->dest->index < NUM_FIXED_BLOCKS
1971 || !flow_bb_inside_loop_p (loop, e->dest))
1973 if (dump_file && (dump_flags & TDF_DETAILS))
1974 fprintf (dump_file,
1975 "Predicting exit %i->%i with prob %i.\n",
1976 e->src->index, e->dest->index, probability);
1977 predict_edge (e,
1978 recursion ? PRED_LOOP_EXIT_WITH_RECURSION
1979 : PRED_LOOP_EXIT, probability);
1982 if (loop_bound_var)
1983 predict_iv_comparison (loop, bb, loop_bound_var, loop_iv_base,
1984 loop_bound_code,
1985 tree_to_shwi (loop_bound_step));
1988 /* In the following code
1989 for (loop1)
1990 if (cond)
1991 for (loop2)
1992 body;
1993 guess that cond is unlikely. */
1994 if (loop_outer (loop)->num)
1996 basic_block bb = NULL;
1997 edge preheader_edge = loop_preheader_edge (loop);
1999 if (single_pred_p (preheader_edge->src)
2000 && single_succ_p (preheader_edge->src))
2001 preheader_edge = single_pred_edge (preheader_edge->src);
2003 gimple *stmt = last_stmt (preheader_edge->src);
2004 /* Pattern match fortran loop preheader:
2005 _16 = BUILTIN_EXPECT (_15, 1, PRED_FORTRAN_LOOP_PREHEADER);
2006 _17 = (logical(kind=4)) _16;
2007 if (_17 != 0)
2008 goto <bb 11>;
2009 else
2010 goto <bb 13>;
2012 Loop guard branch prediction says nothing about duplicated loop
2013 headers produced by fortran frontend and in this case we want
2014 to predict paths leading to this preheader. */
2016 if (stmt
2017 && gimple_code (stmt) == GIMPLE_COND
2018 && gimple_cond_code (stmt) == NE_EXPR
2019 && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
2020 && integer_zerop (gimple_cond_rhs (stmt)))
2022 gimple *call_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
2023 if (gimple_code (call_stmt) == GIMPLE_ASSIGN
2024 && gimple_expr_code (call_stmt) == NOP_EXPR
2025 && TREE_CODE (gimple_assign_rhs1 (call_stmt)) == SSA_NAME)
2026 call_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (call_stmt));
2027 if (gimple_call_internal_p (call_stmt, IFN_BUILTIN_EXPECT)
2028 && TREE_CODE (gimple_call_arg (call_stmt, 2)) == INTEGER_CST
2029 && tree_fits_uhwi_p (gimple_call_arg (call_stmt, 2))
2030 && tree_to_uhwi (gimple_call_arg (call_stmt, 2))
2031 == PRED_FORTRAN_LOOP_PREHEADER)
2032 bb = preheader_edge->src;
2034 if (!bb)
2036 if (!dominated_by_p (CDI_DOMINATORS,
2037 loop_outer (loop)->latch, loop->header))
2038 predict_paths_leading_to_edge (loop_preheader_edge (loop),
2039 recursion
2040 ? PRED_LOOP_GUARD_WITH_RECURSION
2041 : PRED_LOOP_GUARD,
2042 NOT_TAKEN,
2043 loop_outer (loop));
2045 else
2047 if (!dominated_by_p (CDI_DOMINATORS,
2048 loop_outer (loop)->latch, bb))
2049 predict_paths_leading_to (bb,
2050 recursion
2051 ? PRED_LOOP_GUARD_WITH_RECURSION
2052 : PRED_LOOP_GUARD,
2053 NOT_TAKEN,
2054 loop_outer (loop));
2058 /* Free basic blocks from get_loop_body. */
2059 free (bbs);
2063 /* Attempt to predict probabilities of BB outgoing edges using local
2064 properties. */
2065 static void
2066 bb_estimate_probability_locally (basic_block bb)
2068 rtx_insn *last_insn = BB_END (bb);
2069 rtx cond;
2071 if (! can_predict_insn_p (last_insn))
2072 return;
2073 cond = get_condition (last_insn, NULL, false, false);
2074 if (! cond)
2075 return;
2077 /* Try "pointer heuristic."
2078 A comparison ptr == 0 is predicted as false.
2079 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2080 if (COMPARISON_P (cond)
2081 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
2082 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
2084 if (GET_CODE (cond) == EQ)
2085 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
2086 else if (GET_CODE (cond) == NE)
2087 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
2089 else
2091 /* Try "opcode heuristic."
2092 EQ tests are usually false and NE tests are usually true. Also,
2093 most quantities are positive, so we can make the appropriate guesses
2094 about signed comparisons against zero. */
2095 switch (GET_CODE (cond))
2097 case CONST_INT:
2098 /* Unconditional branch. */
2099 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
2100 cond == const0_rtx ? NOT_TAKEN : TAKEN);
2101 break;
2103 case EQ:
2104 case UNEQ:
2105 /* Floating point comparisons appears to behave in a very
2106 unpredictable way because of special role of = tests in
2107 FP code. */
2108 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2110 /* Comparisons with 0 are often used for booleans and there is
2111 nothing useful to predict about them. */
2112 else if (XEXP (cond, 1) == const0_rtx
2113 || XEXP (cond, 0) == const0_rtx)
2115 else
2116 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
2117 break;
2119 case NE:
2120 case LTGT:
2121 /* Floating point comparisons appears to behave in a very
2122 unpredictable way because of special role of = tests in
2123 FP code. */
2124 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2126 /* Comparisons with 0 are often used for booleans and there is
2127 nothing useful to predict about them. */
2128 else if (XEXP (cond, 1) == const0_rtx
2129 || XEXP (cond, 0) == const0_rtx)
2131 else
2132 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
2133 break;
2135 case ORDERED:
2136 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
2137 break;
2139 case UNORDERED:
2140 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
2141 break;
2143 case LE:
2144 case LT:
2145 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2146 || XEXP (cond, 1) == constm1_rtx)
2147 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
2148 break;
2150 case GE:
2151 case GT:
2152 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2153 || XEXP (cond, 1) == constm1_rtx)
2154 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
2155 break;
2157 default:
2158 break;
2162 /* Set edge->probability for each successor edge of BB. */
2163 void
2164 guess_outgoing_edge_probabilities (basic_block bb)
2166 bb_estimate_probability_locally (bb);
2167 combine_predictions_for_insn (BB_END (bb), bb);
2170 static tree expr_expected_value (tree, bitmap, enum br_predictor *predictor);
2172 /* Helper function for expr_expected_value. */
2174 static tree
2175 expr_expected_value_1 (tree type, tree op0, enum tree_code code,
2176 tree op1, bitmap visited, enum br_predictor *predictor)
2178 gimple *def;
2180 if (predictor)
2181 *predictor = PRED_UNCONDITIONAL;
2183 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
2185 if (TREE_CONSTANT (op0))
2186 return op0;
2188 if (code == IMAGPART_EXPR)
2190 if (TREE_CODE (TREE_OPERAND (op0, 0)) == SSA_NAME)
2192 def = SSA_NAME_DEF_STMT (TREE_OPERAND (op0, 0));
2193 if (is_gimple_call (def)
2194 && gimple_call_internal_p (def)
2195 && (gimple_call_internal_fn (def)
2196 == IFN_ATOMIC_COMPARE_EXCHANGE))
2198 /* Assume that any given atomic operation has low contention,
2199 and thus the compare-and-swap operation succeeds. */
2200 if (predictor)
2201 *predictor = PRED_COMPARE_AND_SWAP;
2202 return build_one_cst (TREE_TYPE (op0));
2207 if (code != SSA_NAME)
2208 return NULL_TREE;
2210 def = SSA_NAME_DEF_STMT (op0);
2212 /* If we were already here, break the infinite cycle. */
2213 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (op0)))
2214 return NULL;
2216 if (gimple_code (def) == GIMPLE_PHI)
2218 /* All the arguments of the PHI node must have the same constant
2219 length. */
2220 int i, n = gimple_phi_num_args (def);
2221 tree val = NULL, new_val;
2223 for (i = 0; i < n; i++)
2225 tree arg = PHI_ARG_DEF (def, i);
2226 enum br_predictor predictor2;
2228 /* If this PHI has itself as an argument, we cannot
2229 determine the string length of this argument. However,
2230 if we can find an expected constant value for the other
2231 PHI args then we can still be sure that this is
2232 likely a constant. So be optimistic and just
2233 continue with the next argument. */
2234 if (arg == PHI_RESULT (def))
2235 continue;
2237 new_val = expr_expected_value (arg, visited, &predictor2);
2239 /* It is difficult to combine value predictors. Simply assume
2240 that later predictor is weaker and take its prediction. */
2241 if (predictor && *predictor < predictor2)
2242 *predictor = predictor2;
2243 if (!new_val)
2244 return NULL;
2245 if (!val)
2246 val = new_val;
2247 else if (!operand_equal_p (val, new_val, false))
2248 return NULL;
2250 return val;
2252 if (is_gimple_assign (def))
2254 if (gimple_assign_lhs (def) != op0)
2255 return NULL;
2257 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)),
2258 gimple_assign_rhs1 (def),
2259 gimple_assign_rhs_code (def),
2260 gimple_assign_rhs2 (def),
2261 visited, predictor);
2264 if (is_gimple_call (def))
2266 tree decl = gimple_call_fndecl (def);
2267 if (!decl)
2269 if (gimple_call_internal_p (def)
2270 && gimple_call_internal_fn (def) == IFN_BUILTIN_EXPECT)
2272 gcc_assert (gimple_call_num_args (def) == 3);
2273 tree val = gimple_call_arg (def, 0);
2274 if (TREE_CONSTANT (val))
2275 return val;
2276 if (predictor)
2278 tree val2 = gimple_call_arg (def, 2);
2279 gcc_assert (TREE_CODE (val2) == INTEGER_CST
2280 && tree_fits_uhwi_p (val2)
2281 && tree_to_uhwi (val2) < END_PREDICTORS);
2282 *predictor = (enum br_predictor) tree_to_uhwi (val2);
2284 return gimple_call_arg (def, 1);
2286 return NULL;
2288 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2289 switch (DECL_FUNCTION_CODE (decl))
2291 case BUILT_IN_EXPECT:
2293 tree val;
2294 if (gimple_call_num_args (def) != 2)
2295 return NULL;
2296 val = gimple_call_arg (def, 0);
2297 if (TREE_CONSTANT (val))
2298 return val;
2299 if (predictor)
2300 *predictor = PRED_BUILTIN_EXPECT;
2301 return gimple_call_arg (def, 1);
2304 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
2305 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
2306 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
2307 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
2308 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
2309 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
2310 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
2311 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
2312 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
2313 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
2314 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
2315 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
2316 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
2317 /* Assume that any given atomic operation has low contention,
2318 and thus the compare-and-swap operation succeeds. */
2319 if (predictor)
2320 *predictor = PRED_COMPARE_AND_SWAP;
2321 return boolean_true_node;
2322 default:
2323 break;
2327 return NULL;
2330 if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
2332 tree res;
2333 enum br_predictor predictor2;
2334 op0 = expr_expected_value (op0, visited, predictor);
2335 if (!op0)
2336 return NULL;
2337 op1 = expr_expected_value (op1, visited, &predictor2);
2338 if (predictor && *predictor < predictor2)
2339 *predictor = predictor2;
2340 if (!op1)
2341 return NULL;
2342 res = fold_build2 (code, type, op0, op1);
2343 if (TREE_CONSTANT (res))
2344 return res;
2345 return NULL;
2347 if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
2349 tree res;
2350 op0 = expr_expected_value (op0, visited, predictor);
2351 if (!op0)
2352 return NULL;
2353 res = fold_build1 (code, type, op0);
2354 if (TREE_CONSTANT (res))
2355 return res;
2356 return NULL;
2358 return NULL;
2361 /* Return constant EXPR will likely have at execution time, NULL if unknown.
2362 The function is used by builtin_expect branch predictor so the evidence
2363 must come from this construct and additional possible constant folding.
2365 We may want to implement more involved value guess (such as value range
2366 propagation based prediction), but such tricks shall go to new
2367 implementation. */
2369 static tree
2370 expr_expected_value (tree expr, bitmap visited,
2371 enum br_predictor *predictor)
2373 enum tree_code code;
2374 tree op0, op1;
2376 if (TREE_CONSTANT (expr))
2378 if (predictor)
2379 *predictor = PRED_UNCONDITIONAL;
2380 return expr;
2383 extract_ops_from_tree (expr, &code, &op0, &op1);
2384 return expr_expected_value_1 (TREE_TYPE (expr),
2385 op0, code, op1, visited, predictor);
2388 /* Predict using opcode of the last statement in basic block. */
2389 static void
2390 tree_predict_by_opcode (basic_block bb)
2392 gimple *stmt = last_stmt (bb);
2393 edge then_edge;
2394 tree op0, op1;
2395 tree type;
2396 tree val;
2397 enum tree_code cmp;
2398 bitmap visited;
2399 edge_iterator ei;
2400 enum br_predictor predictor;
2402 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
2403 return;
2404 FOR_EACH_EDGE (then_edge, ei, bb->succs)
2405 if (then_edge->flags & EDGE_TRUE_VALUE)
2406 break;
2407 op0 = gimple_cond_lhs (stmt);
2408 op1 = gimple_cond_rhs (stmt);
2409 cmp = gimple_cond_code (stmt);
2410 type = TREE_TYPE (op0);
2411 visited = BITMAP_ALLOC (NULL);
2412 val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, visited,
2413 &predictor);
2414 BITMAP_FREE (visited);
2415 if (val && TREE_CODE (val) == INTEGER_CST)
2417 if (predictor == PRED_BUILTIN_EXPECT)
2419 int percent = PARAM_VALUE (BUILTIN_EXPECT_PROBABILITY);
2421 gcc_assert (percent >= 0 && percent <= 100);
2422 if (integer_zerop (val))
2423 percent = 100 - percent;
2424 predict_edge (then_edge, PRED_BUILTIN_EXPECT, HITRATE (percent));
2426 else
2427 predict_edge_def (then_edge, predictor,
2428 integer_zerop (val) ? NOT_TAKEN : TAKEN);
2430 /* Try "pointer heuristic."
2431 A comparison ptr == 0 is predicted as false.
2432 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2433 if (POINTER_TYPE_P (type))
2435 if (cmp == EQ_EXPR)
2436 predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
2437 else if (cmp == NE_EXPR)
2438 predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
2440 else
2442 /* Try "opcode heuristic."
2443 EQ tests are usually false and NE tests are usually true. Also,
2444 most quantities are positive, so we can make the appropriate guesses
2445 about signed comparisons against zero. */
2446 switch (cmp)
2448 case EQ_EXPR:
2449 case UNEQ_EXPR:
2450 /* Floating point comparisons appears to behave in a very
2451 unpredictable way because of special role of = tests in
2452 FP code. */
2453 if (FLOAT_TYPE_P (type))
2455 /* Comparisons with 0 are often used for booleans and there is
2456 nothing useful to predict about them. */
2457 else if (integer_zerop (op0) || integer_zerop (op1))
2459 else
2460 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
2461 break;
2463 case NE_EXPR:
2464 case LTGT_EXPR:
2465 /* Floating point comparisons appears to behave in a very
2466 unpredictable way because of special role of = tests in
2467 FP code. */
2468 if (FLOAT_TYPE_P (type))
2470 /* Comparisons with 0 are often used for booleans and there is
2471 nothing useful to predict about them. */
2472 else if (integer_zerop (op0)
2473 || integer_zerop (op1))
2475 else
2476 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
2477 break;
2479 case ORDERED_EXPR:
2480 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
2481 break;
2483 case UNORDERED_EXPR:
2484 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
2485 break;
2487 case LE_EXPR:
2488 case LT_EXPR:
2489 if (integer_zerop (op1)
2490 || integer_onep (op1)
2491 || integer_all_onesp (op1)
2492 || real_zerop (op1)
2493 || real_onep (op1)
2494 || real_minus_onep (op1))
2495 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
2496 break;
2498 case GE_EXPR:
2499 case GT_EXPR:
2500 if (integer_zerop (op1)
2501 || integer_onep (op1)
2502 || integer_all_onesp (op1)
2503 || real_zerop (op1)
2504 || real_onep (op1)
2505 || real_minus_onep (op1))
2506 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
2507 break;
2509 default:
2510 break;
2514 /* Try to guess whether the value of return means error code. */
2516 static enum br_predictor
2517 return_prediction (tree val, enum prediction *prediction)
2519 /* VOID. */
2520 if (!val)
2521 return PRED_NO_PREDICTION;
2522 /* Different heuristics for pointers and scalars. */
2523 if (POINTER_TYPE_P (TREE_TYPE (val)))
2525 /* NULL is usually not returned. */
2526 if (integer_zerop (val))
2528 *prediction = NOT_TAKEN;
2529 return PRED_NULL_RETURN;
2532 else if (INTEGRAL_TYPE_P (TREE_TYPE (val)))
2534 /* Negative return values are often used to indicate
2535 errors. */
2536 if (TREE_CODE (val) == INTEGER_CST
2537 && tree_int_cst_sgn (val) < 0)
2539 *prediction = NOT_TAKEN;
2540 return PRED_NEGATIVE_RETURN;
2542 /* Constant return values seems to be commonly taken.
2543 Zero/one often represent booleans so exclude them from the
2544 heuristics. */
2545 if (TREE_CONSTANT (val)
2546 && (!integer_zerop (val) && !integer_onep (val)))
2548 *prediction = NOT_TAKEN;
2549 return PRED_CONST_RETURN;
2552 return PRED_NO_PREDICTION;
2555 /* Find the basic block with return expression and look up for possible
2556 return value trying to apply RETURN_PREDICTION heuristics. */
2557 static void
2558 apply_return_prediction (void)
2560 greturn *return_stmt = NULL;
2561 tree return_val;
2562 edge e;
2563 gphi *phi;
2564 int phi_num_args, i;
2565 enum br_predictor pred;
2566 enum prediction direction;
2567 edge_iterator ei;
2569 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2571 gimple *last = last_stmt (e->src);
2572 if (last
2573 && gimple_code (last) == GIMPLE_RETURN)
2575 return_stmt = as_a <greturn *> (last);
2576 break;
2579 if (!e)
2580 return;
2581 return_val = gimple_return_retval (return_stmt);
2582 if (!return_val)
2583 return;
2584 if (TREE_CODE (return_val) != SSA_NAME
2585 || !SSA_NAME_DEF_STMT (return_val)
2586 || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI)
2587 return;
2588 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (return_val));
2589 phi_num_args = gimple_phi_num_args (phi);
2590 pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction);
2592 /* Avoid the degenerate case where all return values form the function
2593 belongs to same category (ie they are all positive constants)
2594 so we can hardly say something about them. */
2595 for (i = 1; i < phi_num_args; i++)
2596 if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction))
2597 break;
2598 if (i != phi_num_args)
2599 for (i = 0; i < phi_num_args; i++)
2601 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
2602 if (pred != PRED_NO_PREDICTION)
2603 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi, i), pred,
2604 direction);
2608 /* Look for basic block that contains unlikely to happen events
2609 (such as noreturn calls) and mark all paths leading to execution
2610 of this basic blocks as unlikely. */
2612 static void
2613 tree_bb_level_predictions (void)
2615 basic_block bb;
2616 bool has_return_edges = false;
2617 edge e;
2618 edge_iterator ei;
2620 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2621 if (!(e->flags & (EDGE_ABNORMAL | EDGE_FAKE | EDGE_EH)))
2623 has_return_edges = true;
2624 break;
2627 apply_return_prediction ();
2629 FOR_EACH_BB_FN (bb, cfun)
2631 gimple_stmt_iterator gsi;
2633 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2635 gimple *stmt = gsi_stmt (gsi);
2636 tree decl;
2638 if (is_gimple_call (stmt))
2640 if (gimple_call_noreturn_p (stmt) && has_return_edges)
2641 predict_paths_leading_to (bb, PRED_NORETURN,
2642 NOT_TAKEN);
2643 decl = gimple_call_fndecl (stmt);
2644 if (decl
2645 && lookup_attribute ("cold",
2646 DECL_ATTRIBUTES (decl)))
2647 predict_paths_leading_to (bb, PRED_COLD_FUNCTION,
2648 NOT_TAKEN);
2649 if (decl && recursive_call_p (current_function_decl, decl))
2650 predict_paths_leading_to (bb, PRED_RECURSIVE_CALL,
2651 NOT_TAKEN);
2653 else if (gimple_code (stmt) == GIMPLE_PREDICT)
2655 predict_paths_leading_to (bb, gimple_predict_predictor (stmt),
2656 gimple_predict_outcome (stmt));
2657 /* Keep GIMPLE_PREDICT around so early inlining will propagate
2658 hints to callers. */
2664 /* Callback for hash_map::traverse, asserts that the pointer map is
2665 empty. */
2667 bool
2668 assert_is_empty (const_basic_block const &, edge_prediction *const &value,
2669 void *)
2671 gcc_assert (!value);
2672 return false;
2675 /* Predict branch probabilities and estimate profile for basic block BB. */
2677 static void
2678 tree_estimate_probability_bb (basic_block bb)
2680 edge e;
2681 edge_iterator ei;
2682 gimple *last;
2684 FOR_EACH_EDGE (e, ei, bb->succs)
2686 /* Predict edges to user labels with attributes. */
2687 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2689 gimple_stmt_iterator gi;
2690 for (gi = gsi_start_bb (e->dest); !gsi_end_p (gi); gsi_next (&gi))
2692 glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gi));
2693 tree decl;
2695 if (!label_stmt)
2696 break;
2697 decl = gimple_label_label (label_stmt);
2698 if (DECL_ARTIFICIAL (decl))
2699 continue;
2701 /* Finally, we have a user-defined label. */
2702 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl)))
2703 predict_edge_def (e, PRED_COLD_LABEL, NOT_TAKEN);
2704 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (decl)))
2705 predict_edge_def (e, PRED_HOT_LABEL, TAKEN);
2709 /* Predict early returns to be probable, as we've already taken
2710 care for error returns and other cases are often used for
2711 fast paths through function.
2713 Since we've already removed the return statements, we are
2714 looking for CFG like:
2716 if (conditional)
2719 goto return_block
2721 some other blocks
2722 return_block:
2723 return_stmt. */
2724 if (e->dest != bb->next_bb
2725 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
2726 && single_succ_p (e->dest)
2727 && single_succ_edge (e->dest)->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
2728 && (last = last_stmt (e->dest)) != NULL
2729 && gimple_code (last) == GIMPLE_RETURN)
2731 edge e1;
2732 edge_iterator ei1;
2734 if (single_succ_p (bb))
2736 FOR_EACH_EDGE (e1, ei1, bb->preds)
2737 if (!predicted_by_p (e1->src, PRED_NULL_RETURN)
2738 && !predicted_by_p (e1->src, PRED_CONST_RETURN)
2739 && !predicted_by_p (e1->src, PRED_NEGATIVE_RETURN))
2740 predict_edge_def (e1, PRED_TREE_EARLY_RETURN, NOT_TAKEN);
2742 else
2743 if (!predicted_by_p (e->src, PRED_NULL_RETURN)
2744 && !predicted_by_p (e->src, PRED_CONST_RETURN)
2745 && !predicted_by_p (e->src, PRED_NEGATIVE_RETURN))
2746 predict_edge_def (e, PRED_TREE_EARLY_RETURN, NOT_TAKEN);
2749 /* Look for block we are guarding (ie we dominate it,
2750 but it doesn't postdominate us). */
2751 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest != bb
2752 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
2753 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
2755 gimple_stmt_iterator bi;
2757 /* The call heuristic claims that a guarded function call
2758 is improbable. This is because such calls are often used
2759 to signal exceptional situations such as printing error
2760 messages. */
2761 for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi);
2762 gsi_next (&bi))
2764 gimple *stmt = gsi_stmt (bi);
2765 if (is_gimple_call (stmt)
2766 && !gimple_inexpensive_call_p (as_a <gcall *> (stmt))
2767 /* Constant and pure calls are hardly used to signalize
2768 something exceptional. */
2769 && gimple_has_side_effects (stmt))
2771 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
2772 break;
2777 tree_predict_by_opcode (bb);
2780 /* Predict branch probabilities and estimate profile of the tree CFG.
2781 This function can be called from the loop optimizers to recompute
2782 the profile information.
2783 If DRY_RUN is set, do not modify CFG and only produce dump files. */
2785 void
2786 tree_estimate_probability (bool dry_run)
2788 basic_block bb;
2790 add_noreturn_fake_exit_edges ();
2791 connect_infinite_loops_to_exit ();
2792 /* We use loop_niter_by_eval, which requires that the loops have
2793 preheaders. */
2794 create_preheaders (CP_SIMPLE_PREHEADERS);
2795 calculate_dominance_info (CDI_POST_DOMINATORS);
2797 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
2798 tree_bb_level_predictions ();
2799 record_loop_exits ();
2801 if (number_of_loops (cfun) > 1)
2802 predict_loops ();
2804 FOR_EACH_BB_FN (bb, cfun)
2805 tree_estimate_probability_bb (bb);
2807 FOR_EACH_BB_FN (bb, cfun)
2808 combine_predictions_for_bb (bb, dry_run);
2810 if (flag_checking)
2811 bb_predictions->traverse<void *, assert_is_empty> (NULL);
2813 delete bb_predictions;
2814 bb_predictions = NULL;
2816 if (!dry_run)
2817 estimate_bb_frequencies (false);
2818 free_dominance_info (CDI_POST_DOMINATORS);
2819 remove_fake_exit_edges ();
2822 /* Predict edges to successors of CUR whose sources are not postdominated by
2823 BB by PRED and recurse to all postdominators. */
2825 static void
2826 predict_paths_for_bb (basic_block cur, basic_block bb,
2827 enum br_predictor pred,
2828 enum prediction taken,
2829 bitmap visited, struct loop *in_loop = NULL)
2831 edge e;
2832 edge_iterator ei;
2833 basic_block son;
2835 /* If we exited the loop or CUR is unconditional in the loop, there is
2836 nothing to do. */
2837 if (in_loop
2838 && (!flow_bb_inside_loop_p (in_loop, cur)
2839 || dominated_by_p (CDI_DOMINATORS, in_loop->latch, cur)))
2840 return;
2842 /* We are looking for all edges forming edge cut induced by
2843 set of all blocks postdominated by BB. */
2844 FOR_EACH_EDGE (e, ei, cur->preds)
2845 if (e->src->index >= NUM_FIXED_BLOCKS
2846 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb))
2848 edge e2;
2849 edge_iterator ei2;
2850 bool found = false;
2852 /* Ignore fake edges and eh, we predict them as not taken anyway. */
2853 if (e->flags & (EDGE_EH | EDGE_FAKE))
2854 continue;
2855 gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb));
2857 /* See if there is an edge from e->src that is not abnormal
2858 and does not lead to BB and does not exit the loop. */
2859 FOR_EACH_EDGE (e2, ei2, e->src->succs)
2860 if (e2 != e
2861 && !(e2->flags & (EDGE_EH | EDGE_FAKE))
2862 && !dominated_by_p (CDI_POST_DOMINATORS, e2->dest, bb)
2863 && (!in_loop || !loop_exit_edge_p (in_loop, e2)))
2865 found = true;
2866 break;
2869 /* If there is non-abnormal path leaving e->src, predict edge
2870 using predictor. Otherwise we need to look for paths
2871 leading to e->src.
2873 The second may lead to infinite loop in the case we are predicitng
2874 regions that are only reachable by abnormal edges. We simply
2875 prevent visiting given BB twice. */
2876 if (found)
2878 if (!edge_predicted_by_p (e, pred, taken))
2879 predict_edge_def (e, pred, taken);
2881 else if (bitmap_set_bit (visited, e->src->index))
2882 predict_paths_for_bb (e->src, e->src, pred, taken, visited, in_loop);
2884 for (son = first_dom_son (CDI_POST_DOMINATORS, cur);
2885 son;
2886 son = next_dom_son (CDI_POST_DOMINATORS, son))
2887 predict_paths_for_bb (son, bb, pred, taken, visited, in_loop);
2890 /* Sets branch probabilities according to PREDiction and
2891 FLAGS. */
2893 static void
2894 predict_paths_leading_to (basic_block bb, enum br_predictor pred,
2895 enum prediction taken, struct loop *in_loop)
2897 bitmap visited = BITMAP_ALLOC (NULL);
2898 predict_paths_for_bb (bb, bb, pred, taken, visited, in_loop);
2899 BITMAP_FREE (visited);
2902 /* Like predict_paths_leading_to but take edge instead of basic block. */
2904 static void
2905 predict_paths_leading_to_edge (edge e, enum br_predictor pred,
2906 enum prediction taken, struct loop *in_loop)
2908 bool has_nonloop_edge = false;
2909 edge_iterator ei;
2910 edge e2;
2912 basic_block bb = e->src;
2913 FOR_EACH_EDGE (e2, ei, bb->succs)
2914 if (e2->dest != e->src && e2->dest != e->dest
2915 && !(e->flags & (EDGE_EH | EDGE_FAKE))
2916 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->dest))
2918 has_nonloop_edge = true;
2919 break;
2921 if (!has_nonloop_edge)
2923 bitmap visited = BITMAP_ALLOC (NULL);
2924 predict_paths_for_bb (bb, bb, pred, taken, visited, in_loop);
2925 BITMAP_FREE (visited);
2927 else
2928 predict_edge_def (e, pred, taken);
2931 /* This is used to carry information about basic blocks. It is
2932 attached to the AUX field of the standard CFG block. */
2934 struct block_info
2936 /* Estimated frequency of execution of basic_block. */
2937 sreal frequency;
2939 /* To keep queue of basic blocks to process. */
2940 basic_block next;
2942 /* Number of predecessors we need to visit first. */
2943 int npredecessors;
2946 /* Similar information for edges. */
2947 struct edge_prob_info
2949 /* In case edge is a loopback edge, the probability edge will be reached
2950 in case header is. Estimated number of iterations of the loop can be
2951 then computed as 1 / (1 - back_edge_prob). */
2952 sreal back_edge_prob;
2953 /* True if the edge is a loopback edge in the natural loop. */
2954 unsigned int back_edge:1;
2957 #define BLOCK_INFO(B) ((block_info *) (B)->aux)
2958 #undef EDGE_INFO
2959 #define EDGE_INFO(E) ((edge_prob_info *) (E)->aux)
2961 /* Helper function for estimate_bb_frequencies.
2962 Propagate the frequencies in blocks marked in
2963 TOVISIT, starting in HEAD. */
2965 static void
2966 propagate_freq (basic_block head, bitmap tovisit)
2968 basic_block bb;
2969 basic_block last;
2970 unsigned i;
2971 edge e;
2972 basic_block nextbb;
2973 bitmap_iterator bi;
2975 /* For each basic block we need to visit count number of his predecessors
2976 we need to visit first. */
2977 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
2979 edge_iterator ei;
2980 int count = 0;
2982 bb = BASIC_BLOCK_FOR_FN (cfun, i);
2984 FOR_EACH_EDGE (e, ei, bb->preds)
2986 bool visit = bitmap_bit_p (tovisit, e->src->index);
2988 if (visit && !(e->flags & EDGE_DFS_BACK))
2989 count++;
2990 else if (visit && dump_file && !EDGE_INFO (e)->back_edge)
2991 fprintf (dump_file,
2992 "Irreducible region hit, ignoring edge to %i->%i\n",
2993 e->src->index, bb->index);
2995 BLOCK_INFO (bb)->npredecessors = count;
2996 /* When function never returns, we will never process exit block. */
2997 if (!count && bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
2998 bb->count = bb->frequency = 0;
3001 BLOCK_INFO (head)->frequency = 1;
3002 last = head;
3003 for (bb = head; bb; bb = nextbb)
3005 edge_iterator ei;
3006 sreal cyclic_probability = 0;
3007 sreal frequency = 0;
3009 nextbb = BLOCK_INFO (bb)->next;
3010 BLOCK_INFO (bb)->next = NULL;
3012 /* Compute frequency of basic block. */
3013 if (bb != head)
3015 if (flag_checking)
3016 FOR_EACH_EDGE (e, ei, bb->preds)
3017 gcc_assert (!bitmap_bit_p (tovisit, e->src->index)
3018 || (e->flags & EDGE_DFS_BACK));
3020 FOR_EACH_EDGE (e, ei, bb->preds)
3021 if (EDGE_INFO (e)->back_edge)
3023 cyclic_probability += EDGE_INFO (e)->back_edge_prob;
3025 else if (!(e->flags & EDGE_DFS_BACK))
3027 /* frequency += (e->probability
3028 * BLOCK_INFO (e->src)->frequency /
3029 REG_BR_PROB_BASE); */
3031 sreal tmp = e->probability;
3032 tmp *= BLOCK_INFO (e->src)->frequency;
3033 tmp *= real_inv_br_prob_base;
3034 frequency += tmp;
3037 if (cyclic_probability == 0)
3039 BLOCK_INFO (bb)->frequency = frequency;
3041 else
3043 if (cyclic_probability > real_almost_one)
3044 cyclic_probability = real_almost_one;
3046 /* BLOCK_INFO (bb)->frequency = frequency
3047 / (1 - cyclic_probability) */
3049 cyclic_probability = sreal (1) - cyclic_probability;
3050 BLOCK_INFO (bb)->frequency = frequency / cyclic_probability;
3054 bitmap_clear_bit (tovisit, bb->index);
3056 e = find_edge (bb, head);
3057 if (e)
3059 /* EDGE_INFO (e)->back_edge_prob
3060 = ((e->probability * BLOCK_INFO (bb)->frequency)
3061 / REG_BR_PROB_BASE); */
3063 sreal tmp = e->probability;
3064 tmp *= BLOCK_INFO (bb)->frequency;
3065 EDGE_INFO (e)->back_edge_prob = tmp * real_inv_br_prob_base;
3068 /* Propagate to successor blocks. */
3069 FOR_EACH_EDGE (e, ei, bb->succs)
3070 if (!(e->flags & EDGE_DFS_BACK)
3071 && BLOCK_INFO (e->dest)->npredecessors)
3073 BLOCK_INFO (e->dest)->npredecessors--;
3074 if (!BLOCK_INFO (e->dest)->npredecessors)
3076 if (!nextbb)
3077 nextbb = e->dest;
3078 else
3079 BLOCK_INFO (last)->next = e->dest;
3081 last = e->dest;
3087 /* Estimate frequencies in loops at same nest level. */
3089 static void
3090 estimate_loops_at_level (struct loop *first_loop)
3092 struct loop *loop;
3094 for (loop = first_loop; loop; loop = loop->next)
3096 edge e;
3097 basic_block *bbs;
3098 unsigned i;
3099 bitmap tovisit = BITMAP_ALLOC (NULL);
3101 estimate_loops_at_level (loop->inner);
3103 /* Find current loop back edge and mark it. */
3104 e = loop_latch_edge (loop);
3105 EDGE_INFO (e)->back_edge = 1;
3107 bbs = get_loop_body (loop);
3108 for (i = 0; i < loop->num_nodes; i++)
3109 bitmap_set_bit (tovisit, bbs[i]->index);
3110 free (bbs);
3111 propagate_freq (loop->header, tovisit);
3112 BITMAP_FREE (tovisit);
3116 /* Propagates frequencies through structure of loops. */
3118 static void
3119 estimate_loops (void)
3121 bitmap tovisit = BITMAP_ALLOC (NULL);
3122 basic_block bb;
3124 /* Start by estimating the frequencies in the loops. */
3125 if (number_of_loops (cfun) > 1)
3126 estimate_loops_at_level (current_loops->tree_root->inner);
3128 /* Now propagate the frequencies through all the blocks. */
3129 FOR_ALL_BB_FN (bb, cfun)
3131 bitmap_set_bit (tovisit, bb->index);
3133 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun), tovisit);
3134 BITMAP_FREE (tovisit);
3137 /* Drop the profile for NODE to guessed, and update its frequency based on
3138 whether it is expected to be hot given the CALL_COUNT. */
3140 static void
3141 drop_profile (struct cgraph_node *node, gcov_type call_count)
3143 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3144 /* In the case where this was called by another function with a
3145 dropped profile, call_count will be 0. Since there are no
3146 non-zero call counts to this function, we don't know for sure
3147 whether it is hot, and therefore it will be marked normal below. */
3148 bool hot = maybe_hot_count_p (NULL, call_count);
3150 if (dump_file)
3151 fprintf (dump_file,
3152 "Dropping 0 profile for %s/%i. %s based on calls.\n",
3153 node->name (), node->order,
3154 hot ? "Function is hot" : "Function is normal");
3155 /* We only expect to miss profiles for functions that are reached
3156 via non-zero call edges in cases where the function may have
3157 been linked from another module or library (COMDATs and extern
3158 templates). See the comments below for handle_missing_profiles.
3159 Also, only warn in cases where the missing counts exceed the
3160 number of training runs. In certain cases with an execv followed
3161 by a no-return call the profile for the no-return call is not
3162 dumped and there can be a mismatch. */
3163 if (!DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl)
3164 && call_count > profile_info->runs)
3166 if (flag_profile_correction)
3168 if (dump_file)
3169 fprintf (dump_file,
3170 "Missing counts for called function %s/%i\n",
3171 node->name (), node->order);
3173 else
3174 warning (0, "Missing counts for called function %s/%i",
3175 node->name (), node->order);
3178 profile_status_for_fn (fn)
3179 = (flag_guess_branch_prob ? PROFILE_GUESSED : PROFILE_ABSENT);
3180 node->frequency
3181 = hot ? NODE_FREQUENCY_HOT : NODE_FREQUENCY_NORMAL;
3184 /* In the case of COMDAT routines, multiple object files will contain the same
3185 function and the linker will select one for the binary. In that case
3186 all the other copies from the profile instrument binary will be missing
3187 profile counts. Look for cases where this happened, due to non-zero
3188 call counts going to 0-count functions, and drop the profile to guessed
3189 so that we can use the estimated probabilities and avoid optimizing only
3190 for size.
3192 The other case where the profile may be missing is when the routine
3193 is not going to be emitted to the object file, e.g. for "extern template"
3194 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
3195 all other cases of non-zero calls to 0-count functions. */
3197 void
3198 handle_missing_profiles (void)
3200 struct cgraph_node *node;
3201 int unlikely_count_fraction = PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION);
3202 auto_vec<struct cgraph_node *, 64> worklist;
3204 /* See if 0 count function has non-0 count callers. In this case we
3205 lost some profile. Drop its function profile to PROFILE_GUESSED. */
3206 FOR_EACH_DEFINED_FUNCTION (node)
3208 struct cgraph_edge *e;
3209 gcov_type call_count = 0;
3210 gcov_type max_tp_first_run = 0;
3211 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3213 if (node->count)
3214 continue;
3215 for (e = node->callers; e; e = e->next_caller)
3217 call_count += e->count;
3219 if (e->caller->tp_first_run > max_tp_first_run)
3220 max_tp_first_run = e->caller->tp_first_run;
3223 /* If time profile is missing, let assign the maximum that comes from
3224 caller functions. */
3225 if (!node->tp_first_run && max_tp_first_run)
3226 node->tp_first_run = max_tp_first_run + 1;
3228 if (call_count
3229 && fn && fn->cfg
3230 && (call_count * unlikely_count_fraction >= profile_info->runs))
3232 drop_profile (node, call_count);
3233 worklist.safe_push (node);
3237 /* Propagate the profile dropping to other 0-count COMDATs that are
3238 potentially called by COMDATs we already dropped the profile on. */
3239 while (worklist.length () > 0)
3241 struct cgraph_edge *e;
3243 node = worklist.pop ();
3244 for (e = node->callees; e; e = e->next_caller)
3246 struct cgraph_node *callee = e->callee;
3247 struct function *fn = DECL_STRUCT_FUNCTION (callee->decl);
3249 if (callee->count > 0)
3250 continue;
3251 if (DECL_COMDAT (callee->decl) && fn && fn->cfg
3252 && profile_status_for_fn (fn) == PROFILE_READ)
3254 drop_profile (node, 0);
3255 worklist.safe_push (callee);
3261 /* Convert counts measured by profile driven feedback to frequencies.
3262 Return nonzero iff there was any nonzero execution count. */
3265 counts_to_freqs (void)
3267 gcov_type count_max, true_count_max = 0;
3268 basic_block bb;
3270 /* Don't overwrite the estimated frequencies when the profile for
3271 the function is missing. We may drop this function PROFILE_GUESSED
3272 later in drop_profile (). */
3273 if (!flag_auto_profile && !ENTRY_BLOCK_PTR_FOR_FN (cfun)->count)
3274 return 0;
3276 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3277 true_count_max = MAX (bb->count, true_count_max);
3279 count_max = MAX (true_count_max, 1);
3280 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3281 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
3283 return true_count_max;
3286 /* Return true if function is likely to be expensive, so there is no point to
3287 optimize performance of prologue, epilogue or do inlining at the expense
3288 of code size growth. THRESHOLD is the limit of number of instructions
3289 function can execute at average to be still considered not expensive. */
3291 bool
3292 expensive_function_p (int threshold)
3294 unsigned int sum = 0;
3295 basic_block bb;
3296 unsigned int limit;
3298 /* We can not compute accurately for large thresholds due to scaled
3299 frequencies. */
3300 gcc_assert (threshold <= BB_FREQ_MAX);
3302 /* Frequencies are out of range. This either means that function contains
3303 internal loop executing more than BB_FREQ_MAX times or profile feedback
3304 is available and function has not been executed at all. */
3305 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency == 0)
3306 return true;
3308 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
3309 limit = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency * threshold;
3310 FOR_EACH_BB_FN (bb, cfun)
3312 rtx_insn *insn;
3314 FOR_BB_INSNS (bb, insn)
3315 if (active_insn_p (insn))
3317 sum += bb->frequency;
3318 if (sum > limit)
3319 return true;
3323 return false;
3326 /* Estimate and propagate basic block frequencies using the given branch
3327 probabilities. If FORCE is true, the frequencies are used to estimate
3328 the counts even when there are already non-zero profile counts. */
3330 void
3331 estimate_bb_frequencies (bool force)
3333 basic_block bb;
3334 sreal freq_max;
3336 if (force || profile_status_for_fn (cfun) != PROFILE_READ || !counts_to_freqs ())
3338 static int real_values_initialized = 0;
3340 if (!real_values_initialized)
3342 real_values_initialized = 1;
3343 real_br_prob_base = REG_BR_PROB_BASE;
3344 real_bb_freq_max = BB_FREQ_MAX;
3345 real_one_half = sreal (1, -1);
3346 real_inv_br_prob_base = sreal (1) / real_br_prob_base;
3347 real_almost_one = sreal (1) - real_inv_br_prob_base;
3350 mark_dfs_back_edges ();
3352 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->probability =
3353 REG_BR_PROB_BASE;
3355 /* Set up block info for each basic block. */
3356 alloc_aux_for_blocks (sizeof (block_info));
3357 alloc_aux_for_edges (sizeof (edge_prob_info));
3358 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3360 edge e;
3361 edge_iterator ei;
3363 FOR_EACH_EDGE (e, ei, bb->succs)
3365 EDGE_INFO (e)->back_edge_prob = e->probability;
3366 EDGE_INFO (e)->back_edge_prob *= real_inv_br_prob_base;
3370 /* First compute frequencies locally for each loop from innermost
3371 to outermost to examine frequencies for back edges. */
3372 estimate_loops ();
3374 freq_max = 0;
3375 FOR_EACH_BB_FN (bb, cfun)
3376 if (freq_max < BLOCK_INFO (bb)->frequency)
3377 freq_max = BLOCK_INFO (bb)->frequency;
3379 freq_max = real_bb_freq_max / freq_max;
3380 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3382 sreal tmp = BLOCK_INFO (bb)->frequency * freq_max + real_one_half;
3383 bb->frequency = tmp.to_int ();
3386 free_aux_for_blocks ();
3387 free_aux_for_edges ();
3389 compute_function_frequency ();
3392 /* Decide whether function is hot, cold or unlikely executed. */
3393 void
3394 compute_function_frequency (void)
3396 basic_block bb;
3397 struct cgraph_node *node = cgraph_node::get (current_function_decl);
3399 if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3400 || MAIN_NAME_P (DECL_NAME (current_function_decl)))
3401 node->only_called_at_startup = true;
3402 if (DECL_STATIC_DESTRUCTOR (current_function_decl))
3403 node->only_called_at_exit = true;
3405 if (profile_status_for_fn (cfun) != PROFILE_READ)
3407 int flags = flags_from_decl_or_type (current_function_decl);
3408 if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
3409 != NULL)
3410 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3411 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl))
3412 != NULL)
3413 node->frequency = NODE_FREQUENCY_HOT;
3414 else if (flags & ECF_NORETURN)
3415 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3416 else if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
3417 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3418 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3419 || DECL_STATIC_DESTRUCTOR (current_function_decl))
3420 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3421 return;
3424 /* Only first time try to drop function into unlikely executed.
3425 After inlining the roundoff errors may confuse us.
3426 Ipa-profile pass will drop functions only called from unlikely
3427 functions to unlikely and that is most of what we care about. */
3428 if (!cfun->after_inlining)
3429 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3430 FOR_EACH_BB_FN (bb, cfun)
3432 if (maybe_hot_bb_p (cfun, bb))
3434 node->frequency = NODE_FREQUENCY_HOT;
3435 return;
3437 if (!probably_never_executed_bb_p (cfun, bb))
3438 node->frequency = NODE_FREQUENCY_NORMAL;
3442 /* Build PREDICT_EXPR. */
3443 tree
3444 build_predict_expr (enum br_predictor predictor, enum prediction taken)
3446 tree t = build1 (PREDICT_EXPR, void_type_node,
3447 build_int_cst (integer_type_node, predictor));
3448 SET_PREDICT_EXPR_OUTCOME (t, taken);
3449 return t;
3452 const char *
3453 predictor_name (enum br_predictor predictor)
3455 return predictor_info[predictor].name;
3458 /* Predict branch probabilities and estimate profile of the tree CFG. */
3460 namespace {
3462 const pass_data pass_data_profile =
3464 GIMPLE_PASS, /* type */
3465 "profile_estimate", /* name */
3466 OPTGROUP_NONE, /* optinfo_flags */
3467 TV_BRANCH_PROB, /* tv_id */
3468 PROP_cfg, /* properties_required */
3469 0, /* properties_provided */
3470 0, /* properties_destroyed */
3471 0, /* todo_flags_start */
3472 0, /* todo_flags_finish */
3475 class pass_profile : public gimple_opt_pass
3477 public:
3478 pass_profile (gcc::context *ctxt)
3479 : gimple_opt_pass (pass_data_profile, ctxt)
3482 /* opt_pass methods: */
3483 virtual bool gate (function *) { return flag_guess_branch_prob; }
3484 virtual unsigned int execute (function *);
3486 }; // class pass_profile
3488 unsigned int
3489 pass_profile::execute (function *fun)
3491 unsigned nb_loops;
3493 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
3494 return 0;
3496 loop_optimizer_init (LOOPS_NORMAL);
3497 if (dump_file && (dump_flags & TDF_DETAILS))
3498 flow_loops_dump (dump_file, NULL, 0);
3500 mark_irreducible_loops ();
3502 nb_loops = number_of_loops (fun);
3503 if (nb_loops > 1)
3504 scev_initialize ();
3506 tree_estimate_probability (false);
3508 if (nb_loops > 1)
3509 scev_finalize ();
3511 loop_optimizer_finalize ();
3512 if (dump_file && (dump_flags & TDF_DETAILS))
3513 gimple_dump_cfg (dump_file, dump_flags);
3514 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
3515 profile_status_for_fn (fun) = PROFILE_GUESSED;
3516 if (dump_file && (dump_flags & TDF_DETAILS))
3518 struct loop *loop;
3519 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
3520 if (loop->header->frequency)
3521 fprintf (dump_file, "Loop got predicted %d to iterate %i times.\n",
3522 loop->num,
3523 (int)expected_loop_iterations_unbounded (loop));
3525 return 0;
3528 } // anon namespace
3530 gimple_opt_pass *
3531 make_pass_profile (gcc::context *ctxt)
3533 return new pass_profile (ctxt);
3536 namespace {
3538 const pass_data pass_data_strip_predict_hints =
3540 GIMPLE_PASS, /* type */
3541 "*strip_predict_hints", /* name */
3542 OPTGROUP_NONE, /* optinfo_flags */
3543 TV_BRANCH_PROB, /* tv_id */
3544 PROP_cfg, /* properties_required */
3545 0, /* properties_provided */
3546 0, /* properties_destroyed */
3547 0, /* todo_flags_start */
3548 0, /* todo_flags_finish */
3551 class pass_strip_predict_hints : public gimple_opt_pass
3553 public:
3554 pass_strip_predict_hints (gcc::context *ctxt)
3555 : gimple_opt_pass (pass_data_strip_predict_hints, ctxt)
3558 /* opt_pass methods: */
3559 opt_pass * clone () { return new pass_strip_predict_hints (m_ctxt); }
3560 virtual unsigned int execute (function *);
3562 }; // class pass_strip_predict_hints
3564 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
3565 we no longer need. */
3566 unsigned int
3567 pass_strip_predict_hints::execute (function *fun)
3569 basic_block bb;
3570 gimple *ass_stmt;
3571 tree var;
3572 bool changed = false;
3574 FOR_EACH_BB_FN (bb, fun)
3576 gimple_stmt_iterator bi;
3577 for (bi = gsi_start_bb (bb); !gsi_end_p (bi);)
3579 gimple *stmt = gsi_stmt (bi);
3581 if (gimple_code (stmt) == GIMPLE_PREDICT)
3583 gsi_remove (&bi, true);
3584 changed = true;
3585 continue;
3587 else if (is_gimple_call (stmt))
3589 tree fndecl = gimple_call_fndecl (stmt);
3591 if ((fndecl
3592 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
3593 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
3594 && gimple_call_num_args (stmt) == 2)
3595 || (gimple_call_internal_p (stmt)
3596 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
3598 var = gimple_call_lhs (stmt);
3599 changed = true;
3600 if (var)
3602 ass_stmt
3603 = gimple_build_assign (var, gimple_call_arg (stmt, 0));
3604 gsi_replace (&bi, ass_stmt, true);
3606 else
3608 gsi_remove (&bi, true);
3609 continue;
3613 gsi_next (&bi);
3616 return changed ? TODO_cleanup_cfg : 0;
3619 } // anon namespace
3621 gimple_opt_pass *
3622 make_pass_strip_predict_hints (gcc::context *ctxt)
3624 return new pass_strip_predict_hints (ctxt);
3627 /* Rebuild function frequencies. Passes are in general expected to
3628 maintain profile by hand, however in some cases this is not possible:
3629 for example when inlining several functions with loops freuqencies might run
3630 out of scale and thus needs to be recomputed. */
3632 void
3633 rebuild_frequencies (void)
3635 timevar_push (TV_REBUILD_FREQUENCIES);
3637 /* When the max bb count in the function is small, there is a higher
3638 chance that there were truncation errors in the integer scaling
3639 of counts by inlining and other optimizations. This could lead
3640 to incorrect classification of code as being cold when it isn't.
3641 In that case, force the estimation of bb counts/frequencies from the
3642 branch probabilities, rather than computing frequencies from counts,
3643 which may also lead to frequencies incorrectly reduced to 0. There
3644 is less precision in the probabilities, so we only do this for small
3645 max counts. */
3646 gcov_type count_max = 0;
3647 basic_block bb;
3648 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3649 count_max = MAX (bb->count, count_max);
3651 if (profile_status_for_fn (cfun) == PROFILE_GUESSED
3652 || (!flag_auto_profile && profile_status_for_fn (cfun) == PROFILE_READ
3653 && count_max < REG_BR_PROB_BASE/10))
3655 loop_optimizer_init (0);
3656 add_noreturn_fake_exit_edges ();
3657 mark_irreducible_loops ();
3658 connect_infinite_loops_to_exit ();
3659 estimate_bb_frequencies (true);
3660 remove_fake_exit_edges ();
3661 loop_optimizer_finalize ();
3663 else if (profile_status_for_fn (cfun) == PROFILE_READ)
3664 counts_to_freqs ();
3665 else
3666 gcc_unreachable ();
3667 timevar_pop (TV_REBUILD_FREQUENCIES);
3670 /* Perform a dry run of the branch prediction pass and report comparsion of
3671 the predicted and real profile into the dump file. */
3673 void
3674 report_predictor_hitrates (void)
3676 unsigned nb_loops;
3678 loop_optimizer_init (LOOPS_NORMAL);
3679 if (dump_file && (dump_flags & TDF_DETAILS))
3680 flow_loops_dump (dump_file, NULL, 0);
3682 mark_irreducible_loops ();
3684 nb_loops = number_of_loops (cfun);
3685 if (nb_loops > 1)
3686 scev_initialize ();
3688 tree_estimate_probability (true);
3690 if (nb_loops > 1)
3691 scev_finalize ();
3693 loop_optimizer_finalize ();
3696 /* Force edge E to be cold.
3697 If IMPOSSIBLE is true, for edge to have count and probability 0 otherwise
3698 keep low probability to represent possible error in a guess. This is used
3699 i.e. in case we predict loop to likely iterate given number of times but
3700 we are not 100% sure.
3702 This function locally updates profile without attempt to keep global
3703 consistency which can not be reached in full generality without full profile
3704 rebuild from probabilities alone. Doing so is not necessarily a good idea
3705 because frequencies and counts may be more realistic then probabilities.
3707 In some cases (such as for elimination of early exits during full loop
3708 unrolling) the caller can ensure that profile will get consistent
3709 afterwards. */
3711 void
3712 force_edge_cold (edge e, bool impossible)
3714 gcov_type count_sum = 0;
3715 int prob_sum = 0;
3716 edge_iterator ei;
3717 edge e2;
3718 gcov_type old_count = e->count;
3719 int old_probability = e->probability;
3720 gcov_type gcov_scale = REG_BR_PROB_BASE;
3721 int prob_scale = REG_BR_PROB_BASE;
3723 /* If edge is already improbably or cold, just return. */
3724 if (e->probability <= (impossible ? PROB_VERY_UNLIKELY : 0)
3725 && (!impossible || !e->count))
3726 return;
3727 FOR_EACH_EDGE (e2, ei, e->src->succs)
3728 if (e2 != e)
3730 count_sum += e2->count;
3731 prob_sum += e2->probability;
3734 /* If there are other edges out of e->src, redistribute probabilitity
3735 there. */
3736 if (prob_sum)
3738 e->probability
3739 = MIN (e->probability, impossible ? 0 : PROB_VERY_UNLIKELY);
3740 if (old_probability)
3741 e->count = RDIV (e->count * e->probability, old_probability);
3742 else
3743 e->count = MIN (e->count, impossible ? 0 : 1);
3745 if (count_sum)
3746 gcov_scale = RDIV ((count_sum + old_count - e->count) * REG_BR_PROB_BASE,
3747 count_sum);
3748 prob_scale = RDIV ((REG_BR_PROB_BASE - e->probability) * REG_BR_PROB_BASE,
3749 prob_sum);
3750 if (dump_file && (dump_flags & TDF_DETAILS))
3751 fprintf (dump_file, "Making edge %i->%i %s by redistributing "
3752 "probability to other edges.\n",
3753 e->src->index, e->dest->index,
3754 impossible ? "impossible" : "cold");
3755 FOR_EACH_EDGE (e2, ei, e->src->succs)
3756 if (e2 != e)
3758 e2->count = RDIV (e2->count * gcov_scale, REG_BR_PROB_BASE);
3759 e2->probability = RDIV (e2->probability * prob_scale,
3760 REG_BR_PROB_BASE);
3763 /* If all edges out of e->src are unlikely, the basic block itself
3764 is unlikely. */
3765 else
3767 e->probability = REG_BR_PROB_BASE;
3769 /* If we did not adjusting, the source basic block has no likely edeges
3770 leaving other direction. In that case force that bb cold, too.
3771 This in general is difficult task to do, but handle special case when
3772 BB has only one predecestor. This is common case when we are updating
3773 after loop transforms. */
3774 if (!prob_sum && !count_sum && single_pred_p (e->src)
3775 && e->src->frequency > (impossible ? 0 : 1))
3777 int old_frequency = e->src->frequency;
3778 if (dump_file && (dump_flags & TDF_DETAILS))
3779 fprintf (dump_file, "Making bb %i %s.\n", e->src->index,
3780 impossible ? "impossible" : "cold");
3781 e->src->frequency = MIN (e->src->frequency, impossible ? 0 : 1);
3782 e->src->count = e->count = RDIV (e->src->count * e->src->frequency,
3783 old_frequency);
3784 force_edge_cold (single_pred_edge (e->src), impossible);
3786 else if (dump_file && (dump_flags & TDF_DETAILS)
3787 && maybe_hot_bb_p (cfun, e->src))
3788 fprintf (dump_file, "Giving up on making bb %i %s.\n", e->src->index,
3789 impossible ? "impossible" : "cold");