* cgraph.c (cgraph_edge::maybe_hot_p): Do not check
[official-gcc.git] / gcc / predict.c
blobb460a6f26ee6e024ea188d2c9532745283c514d5
1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* References:
22 [1] "Branch Prediction for Free"
23 Ball and Larus; PLDI '93.
24 [2] "Static Branch Frequency and Program Profile Analysis"
25 Wu and Larus; MICRO-27.
26 [3] "Corpus-based Static Branch Prediction"
27 Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "backend.h"
34 #include "rtl.h"
35 #include "tree.h"
36 #include "gimple.h"
37 #include "cfghooks.h"
38 #include "tree-pass.h"
39 #include "ssa.h"
40 #include "memmodel.h"
41 #include "emit-rtl.h"
42 #include "cgraph.h"
43 #include "coverage.h"
44 #include "diagnostic-core.h"
45 #include "gimple-predict.h"
46 #include "fold-const.h"
47 #include "calls.h"
48 #include "cfganal.h"
49 #include "profile.h"
50 #include "sreal.h"
51 #include "params.h"
52 #include "cfgloop.h"
53 #include "gimple-iterator.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa-loop-niter.h"
56 #include "tree-ssa-loop.h"
57 #include "tree-scalar-evolution.h"
58 #include "ipa-utils.h"
59 #include "gimple-pretty-print.h"
61 /* Enum with reasons why a predictor is ignored. */
63 enum predictor_reason
65 REASON_NONE,
66 REASON_IGNORED,
67 REASON_SINGLE_EDGE_DUPLICATE,
68 REASON_EDGE_PAIR_DUPLICATE
71 /* String messages for the aforementioned enum. */
73 static const char *reason_messages[] = {"", " (ignored)",
74 " (single edge duplicate)", " (edge pair duplicate)"};
76 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
77 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */
78 static sreal real_almost_one, real_br_prob_base,
79 real_inv_br_prob_base, real_one_half, real_bb_freq_max;
81 static void combine_predictions_for_insn (rtx_insn *, basic_block);
82 static void dump_prediction (FILE *, enum br_predictor, int, basic_block,
83 enum predictor_reason, edge);
84 static void predict_paths_leading_to (basic_block, enum br_predictor,
85 enum prediction,
86 struct loop *in_loop = NULL);
87 static void predict_paths_leading_to_edge (edge, enum br_predictor,
88 enum prediction,
89 struct loop *in_loop = NULL);
90 static bool can_predict_insn_p (const rtx_insn *);
92 /* Information we hold about each branch predictor.
93 Filled using information from predict.def. */
95 struct predictor_info
97 const char *const name; /* Name used in the debugging dumps. */
98 const int hitrate; /* Expected hitrate used by
99 predict_insn_def call. */
100 const int flags;
103 /* Use given predictor without Dempster-Shaffer theory if it matches
104 using first_match heuristics. */
105 #define PRED_FLAG_FIRST_MATCH 1
107 /* Recompute hitrate in percent to our representation. */
109 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
111 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
112 static const struct predictor_info predictor_info[]= {
113 #include "predict.def"
115 /* Upper bound on predictors. */
116 {NULL, 0, 0}
118 #undef DEF_PREDICTOR
120 /* Return TRUE if frequency FREQ is considered to be hot. */
122 static inline bool
123 maybe_hot_frequency_p (struct function *fun, int freq)
125 struct cgraph_node *node = cgraph_node::get (fun->decl);
126 if (!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
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 *, profile_count count)
176 if (!count.initialized_p ())
177 return true;
178 /* Code executed at most once is not hot. */
179 if (count <= MAX (profile_info ? profile_info->runs : 1, 1))
180 return false;
181 return (count.to_gcov_type () >= 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 (!maybe_hot_count_p (fun, bb->count))
192 return false;
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 (!maybe_hot_count_p (cfun, e->count))
203 return false;
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 profile_count count, int)
214 gcc_checking_assert (fun);
215 if (count == profile_count::zero ())
216 return true;
217 if (count.initialized_p () && profile_status_for_fn (fun) == PROFILE_READ)
219 int unlikely_count_fraction = PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION);
220 if (count.apply_scale (unlikely_count_fraction, 1) >= profile_info->runs)
221 return false;
222 return true;
224 if ((!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
225 && (cgraph_node::get (fun->decl)->frequency
226 == NODE_FREQUENCY_UNLIKELY_EXECUTED))
227 return true;
228 return false;
232 /* Return true in case BB is probably never executed. */
234 bool
235 probably_never_executed_bb_p (struct function *fun, const_basic_block bb)
237 return probably_never_executed (fun, bb->count, bb->frequency);
241 /* Return true in case edge E is probably never executed. */
243 bool
244 probably_never_executed_edge_p (struct function *fun, edge e)
246 return probably_never_executed (fun, e->count, EDGE_FREQUENCY (e));
249 /* Return true when current function should always be optimized for size. */
251 bool
252 optimize_function_for_size_p (struct function *fun)
254 if (!fun || !fun->decl)
255 return optimize_size;
256 cgraph_node *n = cgraph_node::get (fun->decl);
257 return n && n->optimize_for_size_p ();
260 /* Return true when current function should always be optimized for speed. */
262 bool
263 optimize_function_for_speed_p (struct function *fun)
265 return !optimize_function_for_size_p (fun);
268 /* Return the optimization type that should be used for the function FUN. */
270 optimization_type
271 function_optimization_type (struct function *fun)
273 return (optimize_function_for_speed_p (fun)
274 ? OPTIMIZE_FOR_SPEED
275 : OPTIMIZE_FOR_SIZE);
278 /* Return TRUE when BB should be optimized for size. */
280 bool
281 optimize_bb_for_size_p (const_basic_block bb)
283 return (optimize_function_for_size_p (cfun)
284 || (bb && !maybe_hot_bb_p (cfun, bb)));
287 /* Return TRUE when BB should be optimized for speed. */
289 bool
290 optimize_bb_for_speed_p (const_basic_block bb)
292 return !optimize_bb_for_size_p (bb);
295 /* Return the optimization type that should be used for block BB. */
297 optimization_type
298 bb_optimization_type (const_basic_block bb)
300 return (optimize_bb_for_speed_p (bb)
301 ? OPTIMIZE_FOR_SPEED
302 : OPTIMIZE_FOR_SIZE);
305 /* Return TRUE when BB should be optimized for size. */
307 bool
308 optimize_edge_for_size_p (edge e)
310 return optimize_function_for_size_p (cfun) || !maybe_hot_edge_p (e);
313 /* Return TRUE when BB should be optimized for speed. */
315 bool
316 optimize_edge_for_speed_p (edge e)
318 return !optimize_edge_for_size_p (e);
321 /* Return TRUE when BB should be optimized for size. */
323 bool
324 optimize_insn_for_size_p (void)
326 return optimize_function_for_size_p (cfun) || !crtl->maybe_hot_insn_p;
329 /* Return TRUE when BB should be optimized for speed. */
331 bool
332 optimize_insn_for_speed_p (void)
334 return !optimize_insn_for_size_p ();
337 /* Return TRUE when LOOP should be optimized for size. */
339 bool
340 optimize_loop_for_size_p (struct loop *loop)
342 return optimize_bb_for_size_p (loop->header);
345 /* Return TRUE when LOOP should be optimized for speed. */
347 bool
348 optimize_loop_for_speed_p (struct loop *loop)
350 return optimize_bb_for_speed_p (loop->header);
353 /* Return TRUE when LOOP nest should be optimized for speed. */
355 bool
356 optimize_loop_nest_for_speed_p (struct loop *loop)
358 struct loop *l = loop;
359 if (optimize_loop_for_speed_p (loop))
360 return true;
361 l = loop->inner;
362 while (l && l != loop)
364 if (optimize_loop_for_speed_p (l))
365 return true;
366 if (l->inner)
367 l = l->inner;
368 else if (l->next)
369 l = l->next;
370 else
372 while (l != loop && !l->next)
373 l = loop_outer (l);
374 if (l != loop)
375 l = l->next;
378 return false;
381 /* Return TRUE when LOOP nest should be optimized for size. */
383 bool
384 optimize_loop_nest_for_size_p (struct loop *loop)
386 return !optimize_loop_nest_for_speed_p (loop);
389 /* Return true when edge E is likely to be well predictable by branch
390 predictor. */
392 bool
393 predictable_edge_p (edge e)
395 if (profile_status_for_fn (cfun) == PROFILE_ABSENT)
396 return false;
397 if ((e->probability
398 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100)
399 || (REG_BR_PROB_BASE - e->probability
400 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100))
401 return true;
402 return false;
406 /* Set RTL expansion for BB profile. */
408 void
409 rtl_profile_for_bb (basic_block bb)
411 crtl->maybe_hot_insn_p = maybe_hot_bb_p (cfun, bb);
414 /* Set RTL expansion for edge profile. */
416 void
417 rtl_profile_for_edge (edge e)
419 crtl->maybe_hot_insn_p = maybe_hot_edge_p (e);
422 /* Set RTL expansion to default mode (i.e. when profile info is not known). */
423 void
424 default_rtl_profile (void)
426 crtl->maybe_hot_insn_p = true;
429 /* Return true if the one of outgoing edges is already predicted by
430 PREDICTOR. */
432 bool
433 rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
435 rtx note;
436 if (!INSN_P (BB_END (bb)))
437 return false;
438 for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
439 if (REG_NOTE_KIND (note) == REG_BR_PRED
440 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
441 return true;
442 return false;
445 /* Structure representing predictions in tree level. */
447 struct edge_prediction {
448 struct edge_prediction *ep_next;
449 edge ep_edge;
450 enum br_predictor ep_predictor;
451 int ep_probability;
454 /* This map contains for a basic block the list of predictions for the
455 outgoing edges. */
457 static hash_map<const_basic_block, edge_prediction *> *bb_predictions;
459 /* Return true if the one of outgoing edges is already predicted by
460 PREDICTOR. */
462 bool
463 gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
465 struct edge_prediction *i;
466 edge_prediction **preds = bb_predictions->get (bb);
468 if (!preds)
469 return false;
471 for (i = *preds; i; i = i->ep_next)
472 if (i->ep_predictor == predictor)
473 return true;
474 return false;
477 /* Return true if the one of outgoing edges is already predicted by
478 PREDICTOR for edge E predicted as TAKEN. */
480 bool
481 edge_predicted_by_p (edge e, enum br_predictor predictor, bool taken)
483 struct edge_prediction *i;
484 basic_block bb = e->src;
485 edge_prediction **preds = bb_predictions->get (bb);
486 if (!preds)
487 return false;
489 int probability = predictor_info[(int) predictor].hitrate;
491 if (taken != TAKEN)
492 probability = REG_BR_PROB_BASE - probability;
494 for (i = *preds; i; i = i->ep_next)
495 if (i->ep_predictor == predictor
496 && i->ep_edge == e
497 && i->ep_probability == probability)
498 return true;
499 return false;
502 /* Return true when the probability of edge is reliable.
504 The profile guessing code is good at predicting branch outcome (ie.
505 taken/not taken), that is predicted right slightly over 75% of time.
506 It is however notoriously poor on predicting the probability itself.
507 In general the profile appear a lot flatter (with probabilities closer
508 to 50%) than the reality so it is bad idea to use it to drive optimization
509 such as those disabling dynamic branch prediction for well predictable
510 branches.
512 There are two exceptions - edges leading to noreturn edges and edges
513 predicted by number of iterations heuristics are predicted well. This macro
514 should be able to distinguish those, but at the moment it simply check for
515 noreturn heuristic that is only one giving probability over 99% or bellow
516 1%. In future we might want to propagate reliability information across the
517 CFG if we find this information useful on multiple places. */
518 static bool
519 probability_reliable_p (int prob)
521 return (profile_status_for_fn (cfun) == PROFILE_READ
522 || (profile_status_for_fn (cfun) == PROFILE_GUESSED
523 && (prob <= HITRATE (1) || prob >= HITRATE (99))));
526 /* Same predicate as above, working on edges. */
527 bool
528 edge_probability_reliable_p (const_edge e)
530 return probability_reliable_p (e->probability);
533 /* Same predicate as edge_probability_reliable_p, working on notes. */
534 bool
535 br_prob_note_reliable_p (const_rtx note)
537 gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB);
538 return probability_reliable_p (XINT (note, 0));
541 static void
542 predict_insn (rtx_insn *insn, enum br_predictor predictor, int probability)
544 gcc_assert (any_condjump_p (insn));
545 if (!flag_guess_branch_prob)
546 return;
548 add_reg_note (insn, REG_BR_PRED,
549 gen_rtx_CONCAT (VOIDmode,
550 GEN_INT ((int) predictor),
551 GEN_INT ((int) probability)));
554 /* Predict insn by given predictor. */
556 void
557 predict_insn_def (rtx_insn *insn, enum br_predictor predictor,
558 enum prediction taken)
560 int probability = predictor_info[(int) predictor].hitrate;
562 if (taken != TAKEN)
563 probability = REG_BR_PROB_BASE - probability;
565 predict_insn (insn, predictor, probability);
568 /* Predict edge E with given probability if possible. */
570 void
571 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
573 rtx_insn *last_insn;
574 last_insn = BB_END (e->src);
576 /* We can store the branch prediction information only about
577 conditional jumps. */
578 if (!any_condjump_p (last_insn))
579 return;
581 /* We always store probability of branching. */
582 if (e->flags & EDGE_FALLTHRU)
583 probability = REG_BR_PROB_BASE - probability;
585 predict_insn (last_insn, predictor, probability);
588 /* Predict edge E with the given PROBABILITY. */
589 void
590 gimple_predict_edge (edge e, enum br_predictor predictor, int probability)
592 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
593 && EDGE_COUNT (e->src->succs) > 1
594 && flag_guess_branch_prob
595 && optimize)
597 struct edge_prediction *i = XNEW (struct edge_prediction);
598 edge_prediction *&preds = bb_predictions->get_or_insert (e->src);
600 i->ep_next = preds;
601 preds = i;
602 i->ep_probability = probability;
603 i->ep_predictor = predictor;
604 i->ep_edge = e;
608 /* Filter edge predictions PREDS by a function FILTER. DATA are passed
609 to the filter function. */
611 void
612 filter_predictions (edge_prediction **preds,
613 bool (*filter) (edge_prediction *, void *), void *data)
615 if (!bb_predictions)
616 return;
618 if (preds)
620 struct edge_prediction **prediction = preds;
621 struct edge_prediction *next;
623 while (*prediction)
625 if ((*filter) (*prediction, data))
626 prediction = &((*prediction)->ep_next);
627 else
629 next = (*prediction)->ep_next;
630 free (*prediction);
631 *prediction = next;
637 /* Filter function predicate that returns true for a edge predicate P
638 if its edge is equal to DATA. */
640 bool
641 equal_edge_p (edge_prediction *p, void *data)
643 return p->ep_edge == (edge)data;
646 /* Remove all predictions on given basic block that are attached
647 to edge E. */
648 void
649 remove_predictions_associated_with_edge (edge e)
651 if (!bb_predictions)
652 return;
654 edge_prediction **preds = bb_predictions->get (e->src);
655 filter_predictions (preds, equal_edge_p, e);
658 /* Clears the list of predictions stored for BB. */
660 static void
661 clear_bb_predictions (basic_block bb)
663 edge_prediction **preds = bb_predictions->get (bb);
664 struct edge_prediction *pred, *next;
666 if (!preds)
667 return;
669 for (pred = *preds; pred; pred = next)
671 next = pred->ep_next;
672 free (pred);
674 *preds = NULL;
677 /* Return true when we can store prediction on insn INSN.
678 At the moment we represent predictions only on conditional
679 jumps, not at computed jump or other complicated cases. */
680 static bool
681 can_predict_insn_p (const rtx_insn *insn)
683 return (JUMP_P (insn)
684 && any_condjump_p (insn)
685 && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2);
688 /* Predict edge E by given predictor if possible. */
690 void
691 predict_edge_def (edge e, enum br_predictor predictor,
692 enum prediction taken)
694 int probability = predictor_info[(int) predictor].hitrate;
696 if (taken != TAKEN)
697 probability = REG_BR_PROB_BASE - probability;
699 predict_edge (e, predictor, probability);
702 /* Invert all branch predictions or probability notes in the INSN. This needs
703 to be done each time we invert the condition used by the jump. */
705 void
706 invert_br_probabilities (rtx insn)
708 rtx note;
710 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
711 if (REG_NOTE_KIND (note) == REG_BR_PROB)
712 XINT (note, 0) = REG_BR_PROB_BASE - XINT (note, 0);
713 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
714 XEXP (XEXP (note, 0), 1)
715 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
718 /* Dump information about the branch prediction to the output file. */
720 static void
721 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
722 basic_block bb, enum predictor_reason reason = REASON_NONE,
723 edge ep_edge = NULL)
725 edge e = ep_edge;
726 edge_iterator ei;
728 if (!file)
729 return;
731 if (e == NULL)
732 FOR_EACH_EDGE (e, ei, bb->succs)
733 if (! (e->flags & EDGE_FALLTHRU))
734 break;
736 char edge_info_str[128];
737 if (ep_edge)
738 sprintf (edge_info_str, " of edge %d->%d", ep_edge->src->index,
739 ep_edge->dest->index);
740 else
741 edge_info_str[0] = '\0';
743 fprintf (file, " %s heuristics%s%s: %.1f%%",
744 predictor_info[predictor].name,
745 edge_info_str, reason_messages[reason],
746 probability * 100.0 / REG_BR_PROB_BASE);
748 if (bb->count.initialized_p ())
750 fprintf (file, " exec ");
751 bb->count.dump (file);
752 if (e)
754 fprintf (file, " hit ");
755 e->count.dump (file);
756 fprintf (file, " (%.1f%%)", e->count.to_gcov_type() * 100.0
757 / bb->count.to_gcov_type ());
761 fprintf (file, "\n");
764 /* Return true if E is unlikely executed. */
766 static bool
767 unlikely_executed_edge_p (edge e)
769 return e->count == profile_count::zero ()
770 || (e->flags & (EDGE_EH | EDGE_FAKE));
773 /* Return true if STMT is known to be unlikely executed. */
775 static bool
776 unlikely_executed_stmt_p (gimple *stmt)
778 if (!is_gimple_call (stmt))
779 return false;
780 /* NORETURN attribute enough is not strong enough: exit() may be quite
781 likely executed once during program run. */
782 if (gimple_call_fntype (stmt)
783 && lookup_attribute ("cold",
784 TYPE_ATTRIBUTES (gimple_call_fntype (stmt)))
785 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
786 return true;
787 tree decl = gimple_call_fndecl (stmt);
788 if (!decl)
789 return false;
790 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl))
791 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
792 return true;
794 cgraph_node *n = cgraph_node::get (decl);
795 if (!n)
796 return false;
797 enum availability avail;
798 n = n->ultimate_alias_target (&avail);
799 if (avail < AVAIL_AVAILABLE)
800 return NULL;
801 if (!n->analyzed
802 || n->decl == current_function_decl)
803 return false;
804 return n->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED;
807 /* Return true if BB is unlikely executed. */
809 static bool
810 unlikely_executed_bb_p (basic_block bb)
812 if (bb->count == profile_count::zero ())
813 return true;
814 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
815 return false;
816 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
817 !gsi_end_p (gsi); gsi_next (&gsi))
819 if (unlikely_executed_stmt_p (gsi_stmt (gsi)))
820 return true;
821 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
822 return false;
824 return false;
827 /* We can not predict the probabilities of outgoing edges of bb. Set them
828 evenly and hope for the best. If UNLIKELY_EDGES is not null, distribute
829 even probability for all edges not mentioned in the set. These edges
830 are given PROB_VERY_UNLIKELY probability. */
832 static void
833 set_even_probabilities (basic_block bb,
834 hash_set<edge> *unlikely_edges = NULL)
836 unsigned nedges = 0;
837 edge e = NULL;
838 edge_iterator ei;
840 FOR_EACH_EDGE (e, ei, bb->succs)
841 if (!unlikely_executed_edge_p (e))
842 nedges ++;
844 /* Make the distribution even if all edges are unlikely. */
845 unsigned unlikely_count = unlikely_edges ? unlikely_edges->elements () : 0;
846 if (unlikely_count == nedges)
848 unlikely_edges = NULL;
849 unlikely_count = 0;
852 unsigned c = nedges - unlikely_count;
854 FOR_EACH_EDGE (e, ei, bb->succs)
855 if (!unlikely_executed_edge_p (e))
857 if (unlikely_edges != NULL && unlikely_edges->contains (e))
858 e->probability = PROB_VERY_UNLIKELY;
859 else
860 e->probability = (REG_BR_PROB_BASE + c / 2) / c;
862 else
863 e->probability = 0;
866 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
867 note if not already present. Remove now useless REG_BR_PRED notes. */
869 static void
870 combine_predictions_for_insn (rtx_insn *insn, basic_block bb)
872 rtx prob_note;
873 rtx *pnote;
874 rtx note;
875 int best_probability = PROB_EVEN;
876 enum br_predictor best_predictor = END_PREDICTORS;
877 int combined_probability = REG_BR_PROB_BASE / 2;
878 int d;
879 bool first_match = false;
880 bool found = false;
882 if (!can_predict_insn_p (insn))
884 set_even_probabilities (bb);
885 return;
888 prob_note = find_reg_note (insn, REG_BR_PROB, 0);
889 pnote = &REG_NOTES (insn);
890 if (dump_file)
891 fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
892 bb->index);
894 /* We implement "first match" heuristics and use probability guessed
895 by predictor with smallest index. */
896 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
897 if (REG_NOTE_KIND (note) == REG_BR_PRED)
899 enum br_predictor predictor = ((enum br_predictor)
900 INTVAL (XEXP (XEXP (note, 0), 0)));
901 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
903 found = true;
904 if (best_predictor > predictor
905 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
906 best_probability = probability, best_predictor = predictor;
908 d = (combined_probability * probability
909 + (REG_BR_PROB_BASE - combined_probability)
910 * (REG_BR_PROB_BASE - probability));
912 /* Use FP math to avoid overflows of 32bit integers. */
913 if (d == 0)
914 /* If one probability is 0% and one 100%, avoid division by zero. */
915 combined_probability = REG_BR_PROB_BASE / 2;
916 else
917 combined_probability = (((double) combined_probability) * probability
918 * REG_BR_PROB_BASE / d + 0.5);
921 /* Decide which heuristic to use. In case we didn't match anything,
922 use no_prediction heuristic, in case we did match, use either
923 first match or Dempster-Shaffer theory depending on the flags. */
925 if (best_predictor != END_PREDICTORS)
926 first_match = true;
928 if (!found)
929 dump_prediction (dump_file, PRED_NO_PREDICTION,
930 combined_probability, bb);
931 else
933 if (!first_match)
934 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
935 bb, !first_match ? REASON_NONE : REASON_IGNORED);
936 else
937 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
938 bb, first_match ? REASON_NONE : REASON_IGNORED);
941 if (first_match)
942 combined_probability = best_probability;
943 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
945 while (*pnote)
947 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
949 enum br_predictor predictor = ((enum br_predictor)
950 INTVAL (XEXP (XEXP (*pnote, 0), 0)));
951 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
953 dump_prediction (dump_file, predictor, probability, bb,
954 (!first_match || best_predictor == predictor)
955 ? REASON_NONE : REASON_IGNORED);
956 *pnote = XEXP (*pnote, 1);
958 else
959 pnote = &XEXP (*pnote, 1);
962 if (!prob_note)
964 add_int_reg_note (insn, REG_BR_PROB, combined_probability);
966 /* Save the prediction into CFG in case we are seeing non-degenerated
967 conditional jump. */
968 if (!single_succ_p (bb))
970 BRANCH_EDGE (bb)->probability = combined_probability;
971 FALLTHRU_EDGE (bb)->probability
972 = REG_BR_PROB_BASE - combined_probability;
975 else if (!single_succ_p (bb))
977 int prob = XINT (prob_note, 0);
979 BRANCH_EDGE (bb)->probability = prob;
980 FALLTHRU_EDGE (bb)->probability = REG_BR_PROB_BASE - prob;
982 else
983 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
986 /* Edge prediction hash traits. */
988 struct predictor_hash: pointer_hash <edge_prediction>
991 static inline hashval_t hash (const edge_prediction *);
992 static inline bool equal (const edge_prediction *, const edge_prediction *);
995 /* Calculate hash value of an edge prediction P based on predictor and
996 normalized probability. */
998 inline hashval_t
999 predictor_hash::hash (const edge_prediction *p)
1001 inchash::hash hstate;
1002 hstate.add_int (p->ep_predictor);
1004 int prob = p->ep_probability;
1005 if (prob > REG_BR_PROB_BASE / 2)
1006 prob = REG_BR_PROB_BASE - prob;
1008 hstate.add_int (prob);
1010 return hstate.end ();
1013 /* Return true whether edge predictions P1 and P2 use the same predictor and
1014 have equal (or opposed probability). */
1016 inline bool
1017 predictor_hash::equal (const edge_prediction *p1, const edge_prediction *p2)
1019 return (p1->ep_predictor == p2->ep_predictor
1020 && (p1->ep_probability == p2->ep_probability
1021 || p1->ep_probability == REG_BR_PROB_BASE - p2->ep_probability));
1024 struct predictor_hash_traits: predictor_hash,
1025 typed_noop_remove <edge_prediction *> {};
1027 /* Return true if edge prediction P is not in DATA hash set. */
1029 static bool
1030 not_removed_prediction_p (edge_prediction *p, void *data)
1032 hash_set<edge_prediction *> *remove = (hash_set<edge_prediction *> *) data;
1033 return !remove->contains (p);
1036 /* Prune predictions for a basic block BB. Currently we do following
1037 clean-up steps:
1039 1) remove duplicate prediction that is guessed with the same probability
1040 (different than 1/2) to both edge
1041 2) remove duplicates for a prediction that belongs with the same probability
1042 to a single edge
1046 static void
1047 prune_predictions_for_bb (basic_block bb)
1049 edge_prediction **preds = bb_predictions->get (bb);
1051 if (preds)
1053 hash_table <predictor_hash_traits> s (13);
1054 hash_set <edge_prediction *> remove;
1056 /* Step 1: identify predictors that should be removed. */
1057 for (edge_prediction *pred = *preds; pred; pred = pred->ep_next)
1059 edge_prediction *existing = s.find (pred);
1060 if (existing)
1062 if (pred->ep_edge == existing->ep_edge
1063 && pred->ep_probability == existing->ep_probability)
1065 /* Remove a duplicate predictor. */
1066 dump_prediction (dump_file, pred->ep_predictor,
1067 pred->ep_probability, bb,
1068 REASON_SINGLE_EDGE_DUPLICATE, pred->ep_edge);
1070 remove.add (pred);
1072 else if (pred->ep_edge != existing->ep_edge
1073 && pred->ep_probability == existing->ep_probability
1074 && pred->ep_probability != REG_BR_PROB_BASE / 2)
1076 /* Remove both predictors as they predict the same
1077 for both edges. */
1078 dump_prediction (dump_file, existing->ep_predictor,
1079 pred->ep_probability, bb,
1080 REASON_EDGE_PAIR_DUPLICATE,
1081 existing->ep_edge);
1082 dump_prediction (dump_file, pred->ep_predictor,
1083 pred->ep_probability, bb,
1084 REASON_EDGE_PAIR_DUPLICATE,
1085 pred->ep_edge);
1087 remove.add (existing);
1088 remove.add (pred);
1092 edge_prediction **slot2 = s.find_slot (pred, INSERT);
1093 *slot2 = pred;
1096 /* Step 2: Remove predictors. */
1097 filter_predictions (preds, not_removed_prediction_p, &remove);
1101 /* Combine predictions into single probability and store them into CFG.
1102 Remove now useless prediction entries.
1103 If DRY_RUN is set, only produce dumps and do not modify profile. */
1105 static void
1106 combine_predictions_for_bb (basic_block bb, bool dry_run)
1108 int best_probability = PROB_EVEN;
1109 enum br_predictor best_predictor = END_PREDICTORS;
1110 int combined_probability = REG_BR_PROB_BASE / 2;
1111 int d;
1112 bool first_match = false;
1113 bool found = false;
1114 struct edge_prediction *pred;
1115 int nedges = 0;
1116 edge e, first = NULL, second = NULL;
1117 edge_iterator ei;
1119 FOR_EACH_EDGE (e, ei, bb->succs)
1120 if (!unlikely_executed_edge_p (e))
1122 nedges ++;
1123 if (first && !second)
1124 second = e;
1125 if (!first)
1126 first = e;
1129 /* When there is no successor or only one choice, prediction is easy.
1131 When we have a basic block with more than 2 successors, the situation
1132 is more complicated as DS theory cannot be used literally.
1133 More precisely, let's assume we predicted edge e1 with probability p1,
1134 thus: m1({b1}) = p1. As we're going to combine more than 2 edges, we
1135 need to find probability of e.g. m1({b2}), which we don't know.
1136 The only approximation is to equally distribute 1-p1 to all edges
1137 different from b1.
1139 According to numbers we've got from SPEC2006 benchark, there's only
1140 one interesting reliable predictor (noreturn call), which can be
1141 handled with a bit easier approach. */
1142 if (nedges != 2)
1144 hash_set<edge> unlikely_edges (4);
1146 /* Identify all edges that have a probability close to very unlikely.
1147 Doing the approach for very unlikely doesn't worth for doing as
1148 there's no such probability in SPEC2006 benchmark. */
1149 edge_prediction **preds = bb_predictions->get (bb);
1150 if (preds)
1151 for (pred = *preds; pred; pred = pred->ep_next)
1152 if (pred->ep_probability <= PROB_VERY_UNLIKELY)
1153 unlikely_edges.add (pred->ep_edge);
1155 if (!bb->count.initialized_p () && !dry_run)
1156 set_even_probabilities (bb, &unlikely_edges);
1157 clear_bb_predictions (bb);
1158 if (dump_file)
1160 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1161 if (unlikely_edges.elements () == 0)
1162 fprintf (dump_file,
1163 "%i edges in bb %i predicted to even probabilities\n",
1164 nedges, bb->index);
1165 else
1167 fprintf (dump_file,
1168 "%i edges in bb %i predicted with some unlikely edges\n",
1169 nedges, bb->index);
1170 FOR_EACH_EDGE (e, ei, bb->succs)
1171 if (!unlikely_executed_edge_p (e))
1172 dump_prediction (dump_file, PRED_COMBINED, e->probability,
1173 bb, REASON_NONE, e);
1176 return;
1179 if (dump_file)
1180 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1182 prune_predictions_for_bb (bb);
1184 edge_prediction **preds = bb_predictions->get (bb);
1186 if (preds)
1188 /* We implement "first match" heuristics and use probability guessed
1189 by predictor with smallest index. */
1190 for (pred = *preds; pred; pred = pred->ep_next)
1192 enum br_predictor predictor = pred->ep_predictor;
1193 int probability = pred->ep_probability;
1195 if (pred->ep_edge != first)
1196 probability = REG_BR_PROB_BASE - probability;
1198 found = true;
1199 /* First match heuristics would be widly confused if we predicted
1200 both directions. */
1201 if (best_predictor > predictor
1202 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
1204 struct edge_prediction *pred2;
1205 int prob = probability;
1207 for (pred2 = (struct edge_prediction *) *preds;
1208 pred2; pred2 = pred2->ep_next)
1209 if (pred2 != pred && pred2->ep_predictor == pred->ep_predictor)
1211 int probability2 = pred2->ep_probability;
1213 if (pred2->ep_edge != first)
1214 probability2 = REG_BR_PROB_BASE - probability2;
1216 if ((probability < REG_BR_PROB_BASE / 2) !=
1217 (probability2 < REG_BR_PROB_BASE / 2))
1218 break;
1220 /* If the same predictor later gave better result, go for it! */
1221 if ((probability >= REG_BR_PROB_BASE / 2 && (probability2 > probability))
1222 || (probability <= REG_BR_PROB_BASE / 2 && (probability2 < probability)))
1223 prob = probability2;
1225 if (!pred2)
1226 best_probability = prob, best_predictor = predictor;
1229 d = (combined_probability * probability
1230 + (REG_BR_PROB_BASE - combined_probability)
1231 * (REG_BR_PROB_BASE - probability));
1233 /* Use FP math to avoid overflows of 32bit integers. */
1234 if (d == 0)
1235 /* If one probability is 0% and one 100%, avoid division by zero. */
1236 combined_probability = REG_BR_PROB_BASE / 2;
1237 else
1238 combined_probability = (((double) combined_probability)
1239 * probability
1240 * REG_BR_PROB_BASE / d + 0.5);
1244 /* Decide which heuristic to use. In case we didn't match anything,
1245 use no_prediction heuristic, in case we did match, use either
1246 first match or Dempster-Shaffer theory depending on the flags. */
1248 if (best_predictor != END_PREDICTORS)
1249 first_match = true;
1251 if (!found)
1252 dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb);
1253 else
1255 if (!first_match)
1256 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb,
1257 !first_match ? REASON_NONE : REASON_IGNORED);
1258 else
1259 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb,
1260 first_match ? REASON_NONE : REASON_IGNORED);
1263 if (first_match)
1264 combined_probability = best_probability;
1265 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
1267 if (preds)
1269 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next)
1271 enum br_predictor predictor = pred->ep_predictor;
1272 int probability = pred->ep_probability;
1274 dump_prediction (dump_file, predictor, probability, bb,
1275 (!first_match || best_predictor == predictor)
1276 ? REASON_NONE : REASON_IGNORED, pred->ep_edge);
1279 clear_bb_predictions (bb);
1281 if (!bb->count.initialized_p () && !dry_run)
1283 first->probability = combined_probability;
1284 second->probability = REG_BR_PROB_BASE - combined_probability;
1288 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1289 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1291 T1 and T2 should be one of the following cases:
1292 1. T1 is SSA_NAME, T2 is NULL
1293 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1294 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1296 static tree
1297 strips_small_constant (tree t1, tree t2)
1299 tree ret = NULL;
1300 int value = 0;
1302 if (!t1)
1303 return NULL;
1304 else if (TREE_CODE (t1) == SSA_NAME)
1305 ret = t1;
1306 else if (tree_fits_shwi_p (t1))
1307 value = tree_to_shwi (t1);
1308 else
1309 return NULL;
1311 if (!t2)
1312 return ret;
1313 else if (tree_fits_shwi_p (t2))
1314 value = tree_to_shwi (t2);
1315 else if (TREE_CODE (t2) == SSA_NAME)
1317 if (ret)
1318 return NULL;
1319 else
1320 ret = t2;
1323 if (value <= 4 && value >= -4)
1324 return ret;
1325 else
1326 return NULL;
1329 /* Return the SSA_NAME in T or T's operands.
1330 Return NULL if SSA_NAME cannot be found. */
1332 static tree
1333 get_base_value (tree t)
1335 if (TREE_CODE (t) == SSA_NAME)
1336 return t;
1338 if (!BINARY_CLASS_P (t))
1339 return NULL;
1341 switch (TREE_OPERAND_LENGTH (t))
1343 case 1:
1344 return strips_small_constant (TREE_OPERAND (t, 0), NULL);
1345 case 2:
1346 return strips_small_constant (TREE_OPERAND (t, 0),
1347 TREE_OPERAND (t, 1));
1348 default:
1349 return NULL;
1353 /* Check the compare STMT in LOOP. If it compares an induction
1354 variable to a loop invariant, return true, and save
1355 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1356 Otherwise return false and set LOOP_INVAIANT to NULL. */
1358 static bool
1359 is_comparison_with_loop_invariant_p (gcond *stmt, struct loop *loop,
1360 tree *loop_invariant,
1361 enum tree_code *compare_code,
1362 tree *loop_step,
1363 tree *loop_iv_base)
1365 tree op0, op1, bound, base;
1366 affine_iv iv0, iv1;
1367 enum tree_code code;
1368 tree step;
1370 code = gimple_cond_code (stmt);
1371 *loop_invariant = NULL;
1373 switch (code)
1375 case GT_EXPR:
1376 case GE_EXPR:
1377 case NE_EXPR:
1378 case LT_EXPR:
1379 case LE_EXPR:
1380 case EQ_EXPR:
1381 break;
1383 default:
1384 return false;
1387 op0 = gimple_cond_lhs (stmt);
1388 op1 = gimple_cond_rhs (stmt);
1390 if ((TREE_CODE (op0) != SSA_NAME && TREE_CODE (op0) != INTEGER_CST)
1391 || (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op1) != INTEGER_CST))
1392 return false;
1393 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, true))
1394 return false;
1395 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, true))
1396 return false;
1397 if (TREE_CODE (iv0.step) != INTEGER_CST
1398 || TREE_CODE (iv1.step) != INTEGER_CST)
1399 return false;
1400 if ((integer_zerop (iv0.step) && integer_zerop (iv1.step))
1401 || (!integer_zerop (iv0.step) && !integer_zerop (iv1.step)))
1402 return false;
1404 if (integer_zerop (iv0.step))
1406 if (code != NE_EXPR && code != EQ_EXPR)
1407 code = invert_tree_comparison (code, false);
1408 bound = iv0.base;
1409 base = iv1.base;
1410 if (tree_fits_shwi_p (iv1.step))
1411 step = iv1.step;
1412 else
1413 return false;
1415 else
1417 bound = iv1.base;
1418 base = iv0.base;
1419 if (tree_fits_shwi_p (iv0.step))
1420 step = iv0.step;
1421 else
1422 return false;
1425 if (TREE_CODE (bound) != INTEGER_CST)
1426 bound = get_base_value (bound);
1427 if (!bound)
1428 return false;
1429 if (TREE_CODE (base) != INTEGER_CST)
1430 base = get_base_value (base);
1431 if (!base)
1432 return false;
1434 *loop_invariant = bound;
1435 *compare_code = code;
1436 *loop_step = step;
1437 *loop_iv_base = base;
1438 return true;
1441 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1443 static bool
1444 expr_coherent_p (tree t1, tree t2)
1446 gimple *stmt;
1447 tree ssa_name_1 = NULL;
1448 tree ssa_name_2 = NULL;
1450 gcc_assert (TREE_CODE (t1) == SSA_NAME || TREE_CODE (t1) == INTEGER_CST);
1451 gcc_assert (TREE_CODE (t2) == SSA_NAME || TREE_CODE (t2) == INTEGER_CST);
1453 if (t1 == t2)
1454 return true;
1456 if (TREE_CODE (t1) == INTEGER_CST && TREE_CODE (t2) == INTEGER_CST)
1457 return true;
1458 if (TREE_CODE (t1) == INTEGER_CST || TREE_CODE (t2) == INTEGER_CST)
1459 return false;
1461 /* Check to see if t1 is expressed/defined with t2. */
1462 stmt = SSA_NAME_DEF_STMT (t1);
1463 gcc_assert (stmt != NULL);
1464 if (is_gimple_assign (stmt))
1466 ssa_name_1 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1467 if (ssa_name_1 && ssa_name_1 == t2)
1468 return true;
1471 /* Check to see if t2 is expressed/defined with t1. */
1472 stmt = SSA_NAME_DEF_STMT (t2);
1473 gcc_assert (stmt != NULL);
1474 if (is_gimple_assign (stmt))
1476 ssa_name_2 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1477 if (ssa_name_2 && ssa_name_2 == t1)
1478 return true;
1481 /* Compare if t1 and t2's def_stmts are identical. */
1482 if (ssa_name_2 != NULL && ssa_name_1 == ssa_name_2)
1483 return true;
1484 else
1485 return false;
1488 /* Return true if E is predicted by one of loop heuristics. */
1490 static bool
1491 predicted_by_loop_heuristics_p (basic_block bb)
1493 struct edge_prediction *i;
1494 edge_prediction **preds = bb_predictions->get (bb);
1496 if (!preds)
1497 return false;
1499 for (i = *preds; i; i = i->ep_next)
1500 if (i->ep_predictor == PRED_LOOP_ITERATIONS_GUESSED
1501 || i->ep_predictor == PRED_LOOP_ITERATIONS_MAX
1502 || i->ep_predictor == PRED_LOOP_ITERATIONS
1503 || i->ep_predictor == PRED_LOOP_EXIT
1504 || i->ep_predictor == PRED_LOOP_EXIT_WITH_RECURSION
1505 || i->ep_predictor == PRED_LOOP_EXTRA_EXIT)
1506 return true;
1507 return false;
1510 /* Predict branch probability of BB when BB contains a branch that compares
1511 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1512 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1514 E.g.
1515 for (int i = 0; i < bound; i++) {
1516 if (i < bound - 2)
1517 computation_1();
1518 else
1519 computation_2();
1522 In this loop, we will predict the branch inside the loop to be taken. */
1524 static void
1525 predict_iv_comparison (struct loop *loop, basic_block bb,
1526 tree loop_bound_var,
1527 tree loop_iv_base_var,
1528 enum tree_code loop_bound_code,
1529 int loop_bound_step)
1531 gimple *stmt;
1532 tree compare_var, compare_base;
1533 enum tree_code compare_code;
1534 tree compare_step_var;
1535 edge then_edge;
1536 edge_iterator ei;
1538 if (predicted_by_loop_heuristics_p (bb))
1539 return;
1541 stmt = last_stmt (bb);
1542 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1543 return;
1544 if (!is_comparison_with_loop_invariant_p (as_a <gcond *> (stmt),
1545 loop, &compare_var,
1546 &compare_code,
1547 &compare_step_var,
1548 &compare_base))
1549 return;
1551 /* Find the taken edge. */
1552 FOR_EACH_EDGE (then_edge, ei, bb->succs)
1553 if (then_edge->flags & EDGE_TRUE_VALUE)
1554 break;
1556 /* When comparing an IV to a loop invariant, NE is more likely to be
1557 taken while EQ is more likely to be not-taken. */
1558 if (compare_code == NE_EXPR)
1560 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1561 return;
1563 else if (compare_code == EQ_EXPR)
1565 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1566 return;
1569 if (!expr_coherent_p (loop_iv_base_var, compare_base))
1570 return;
1572 /* If loop bound, base and compare bound are all constants, we can
1573 calculate the probability directly. */
1574 if (tree_fits_shwi_p (loop_bound_var)
1575 && tree_fits_shwi_p (compare_var)
1576 && tree_fits_shwi_p (compare_base))
1578 int probability;
1579 bool overflow, overall_overflow = false;
1580 widest_int compare_count, tem;
1582 /* (loop_bound - base) / compare_step */
1583 tem = wi::sub (wi::to_widest (loop_bound_var),
1584 wi::to_widest (compare_base), SIGNED, &overflow);
1585 overall_overflow |= overflow;
1586 widest_int loop_count = wi::div_trunc (tem,
1587 wi::to_widest (compare_step_var),
1588 SIGNED, &overflow);
1589 overall_overflow |= overflow;
1591 if (!wi::neg_p (wi::to_widest (compare_step_var))
1592 ^ (compare_code == LT_EXPR || compare_code == LE_EXPR))
1594 /* (loop_bound - compare_bound) / compare_step */
1595 tem = wi::sub (wi::to_widest (loop_bound_var),
1596 wi::to_widest (compare_var), SIGNED, &overflow);
1597 overall_overflow |= overflow;
1598 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1599 SIGNED, &overflow);
1600 overall_overflow |= overflow;
1602 else
1604 /* (compare_bound - base) / compare_step */
1605 tem = wi::sub (wi::to_widest (compare_var),
1606 wi::to_widest (compare_base), SIGNED, &overflow);
1607 overall_overflow |= overflow;
1608 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1609 SIGNED, &overflow);
1610 overall_overflow |= overflow;
1612 if (compare_code == LE_EXPR || compare_code == GE_EXPR)
1613 ++compare_count;
1614 if (loop_bound_code == LE_EXPR || loop_bound_code == GE_EXPR)
1615 ++loop_count;
1616 if (wi::neg_p (compare_count))
1617 compare_count = 0;
1618 if (wi::neg_p (loop_count))
1619 loop_count = 0;
1620 if (loop_count == 0)
1621 probability = 0;
1622 else if (wi::cmps (compare_count, loop_count) == 1)
1623 probability = REG_BR_PROB_BASE;
1624 else
1626 tem = compare_count * REG_BR_PROB_BASE;
1627 tem = wi::udiv_trunc (tem, loop_count);
1628 probability = tem.to_uhwi ();
1631 /* FIXME: The branch prediction seems broken. It has only 20% hitrate. */
1632 if (!overall_overflow)
1633 predict_edge (then_edge, PRED_LOOP_IV_COMPARE, probability);
1635 return;
1638 if (expr_coherent_p (loop_bound_var, compare_var))
1640 if ((loop_bound_code == LT_EXPR || loop_bound_code == LE_EXPR)
1641 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1642 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1643 else if ((loop_bound_code == GT_EXPR || loop_bound_code == GE_EXPR)
1644 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1645 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1646 else if (loop_bound_code == NE_EXPR)
1648 /* If the loop backedge condition is "(i != bound)", we do
1649 the comparison based on the step of IV:
1650 * step < 0 : backedge condition is like (i > bound)
1651 * step > 0 : backedge condition is like (i < bound) */
1652 gcc_assert (loop_bound_step != 0);
1653 if (loop_bound_step > 0
1654 && (compare_code == LT_EXPR
1655 || compare_code == LE_EXPR))
1656 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1657 else if (loop_bound_step < 0
1658 && (compare_code == GT_EXPR
1659 || compare_code == GE_EXPR))
1660 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1661 else
1662 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1664 else
1665 /* The branch is predicted not-taken if loop_bound_code is
1666 opposite with compare_code. */
1667 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1669 else if (expr_coherent_p (loop_iv_base_var, compare_var))
1671 /* For cases like:
1672 for (i = s; i < h; i++)
1673 if (i > s + 2) ....
1674 The branch should be predicted taken. */
1675 if (loop_bound_step > 0
1676 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1677 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1678 else if (loop_bound_step < 0
1679 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1680 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1681 else
1682 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1686 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1687 exits are resulted from short-circuit conditions that will generate an
1688 if_tmp. E.g.:
1690 if (foo() || global > 10)
1691 break;
1693 This will be translated into:
1695 BB3:
1696 loop header...
1697 BB4:
1698 if foo() goto BB6 else goto BB5
1699 BB5:
1700 if global > 10 goto BB6 else goto BB7
1701 BB6:
1702 goto BB7
1703 BB7:
1704 iftmp = (PHI 0(BB5), 1(BB6))
1705 if iftmp == 1 goto BB8 else goto BB3
1706 BB8:
1707 outside of the loop...
1709 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1710 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1711 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1712 exits to predict them using PRED_LOOP_EXTRA_EXIT. */
1714 static void
1715 predict_extra_loop_exits (edge exit_edge)
1717 unsigned i;
1718 bool check_value_one;
1719 gimple *lhs_def_stmt;
1720 gphi *phi_stmt;
1721 tree cmp_rhs, cmp_lhs;
1722 gimple *last;
1723 gcond *cmp_stmt;
1725 last = last_stmt (exit_edge->src);
1726 if (!last)
1727 return;
1728 cmp_stmt = dyn_cast <gcond *> (last);
1729 if (!cmp_stmt)
1730 return;
1732 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1733 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1734 if (!TREE_CONSTANT (cmp_rhs)
1735 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1736 return;
1737 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1738 return;
1740 /* If check_value_one is true, only the phi_args with value '1' will lead
1741 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1742 loop exit. */
1743 check_value_one = (((integer_onep (cmp_rhs))
1744 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1745 ^ ((exit_edge->flags & EDGE_TRUE_VALUE) != 0));
1747 lhs_def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1748 if (!lhs_def_stmt)
1749 return;
1751 phi_stmt = dyn_cast <gphi *> (lhs_def_stmt);
1752 if (!phi_stmt)
1753 return;
1755 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1757 edge e1;
1758 edge_iterator ei;
1759 tree val = gimple_phi_arg_def (phi_stmt, i);
1760 edge e = gimple_phi_arg_edge (phi_stmt, i);
1762 if (!TREE_CONSTANT (val) || !(integer_zerop (val) || integer_onep (val)))
1763 continue;
1764 if ((check_value_one ^ integer_onep (val)) == 1)
1765 continue;
1766 if (EDGE_COUNT (e->src->succs) != 1)
1768 predict_paths_leading_to_edge (e, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1769 continue;
1772 FOR_EACH_EDGE (e1, ei, e->src->preds)
1773 predict_paths_leading_to_edge (e1, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1778 /* Predict edge probabilities by exploiting loop structure. */
1780 static void
1781 predict_loops (void)
1783 struct loop *loop;
1784 basic_block bb;
1785 hash_set <struct loop *> with_recursion(10);
1787 FOR_EACH_BB_FN (bb, cfun)
1789 gimple_stmt_iterator gsi;
1790 tree decl;
1792 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1793 if (is_gimple_call (gsi_stmt (gsi))
1794 && (decl = gimple_call_fndecl (gsi_stmt (gsi))) != NULL
1795 && recursive_call_p (current_function_decl, decl))
1797 loop = bb->loop_father;
1798 while (loop && !with_recursion.add (loop))
1799 loop = loop_outer (loop);
1803 /* Try to predict out blocks in a loop that are not part of a
1804 natural loop. */
1805 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1807 basic_block bb, *bbs;
1808 unsigned j, n_exits = 0;
1809 vec<edge> exits;
1810 struct tree_niter_desc niter_desc;
1811 edge ex;
1812 struct nb_iter_bound *nb_iter;
1813 enum tree_code loop_bound_code = ERROR_MARK;
1814 tree loop_bound_step = NULL;
1815 tree loop_bound_var = NULL;
1816 tree loop_iv_base = NULL;
1817 gcond *stmt = NULL;
1818 bool recursion = with_recursion.contains (loop);
1820 exits = get_loop_exit_edges (loop);
1821 FOR_EACH_VEC_ELT (exits, j, ex)
1822 if (!unlikely_executed_edge_p (ex) && !(ex->flags & EDGE_ABNORMAL_CALL))
1823 n_exits ++;
1824 if (!n_exits)
1826 exits.release ();
1827 continue;
1830 if (dump_file && (dump_flags & TDF_DETAILS))
1831 fprintf (dump_file, "Predicting loop %i%s with %i exits.\n",
1832 loop->num, recursion ? " (with recursion)":"", n_exits);
1833 if (dump_file && (dump_flags & TDF_DETAILS)
1834 && max_loop_iterations_int (loop) >= 0)
1836 fprintf (dump_file,
1837 "Loop %d iterates at most %i times.\n", loop->num,
1838 (int)max_loop_iterations_int (loop));
1840 if (dump_file && (dump_flags & TDF_DETAILS)
1841 && likely_max_loop_iterations_int (loop) >= 0)
1843 fprintf (dump_file, "Loop %d likely iterates at most %i times.\n",
1844 loop->num, (int)likely_max_loop_iterations_int (loop));
1847 FOR_EACH_VEC_ELT (exits, j, ex)
1849 tree niter = NULL;
1850 HOST_WIDE_INT nitercst;
1851 int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS);
1852 int probability;
1853 enum br_predictor predictor;
1854 widest_int nit;
1856 if (unlikely_executed_edge_p (ex)
1857 || (ex->flags & EDGE_ABNORMAL_CALL))
1858 continue;
1859 /* Loop heuristics do not expect exit conditional to be inside
1860 inner loop. We predict from innermost to outermost loop. */
1861 if (predicted_by_loop_heuristics_p (ex->src))
1863 if (dump_file && (dump_flags & TDF_DETAILS))
1864 fprintf (dump_file, "Skipping exit %i->%i because "
1865 "it is already predicted.\n",
1866 ex->src->index, ex->dest->index);
1867 continue;
1869 predict_extra_loop_exits (ex);
1871 if (number_of_iterations_exit (loop, ex, &niter_desc, false, false))
1872 niter = niter_desc.niter;
1873 if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST)
1874 niter = loop_niter_by_eval (loop, ex);
1875 if (dump_file && (dump_flags & TDF_DETAILS)
1876 && TREE_CODE (niter) == INTEGER_CST)
1878 fprintf (dump_file, "Exit %i->%i %d iterates ",
1879 ex->src->index, ex->dest->index,
1880 loop->num);
1881 print_generic_expr (dump_file, niter, TDF_SLIM);
1882 fprintf (dump_file, " times.\n");
1885 if (TREE_CODE (niter) == INTEGER_CST)
1887 if (tree_fits_uhwi_p (niter)
1888 && max
1889 && compare_tree_int (niter, max - 1) == -1)
1890 nitercst = tree_to_uhwi (niter) + 1;
1891 else
1892 nitercst = max;
1893 predictor = PRED_LOOP_ITERATIONS;
1895 /* If we have just one exit and we can derive some information about
1896 the number of iterations of the loop from the statements inside
1897 the loop, use it to predict this exit. */
1898 else if (n_exits == 1
1899 && estimated_stmt_executions (loop, &nit))
1901 if (wi::gtu_p (nit, max))
1902 nitercst = max;
1903 else
1904 nitercst = nit.to_shwi ();
1905 predictor = PRED_LOOP_ITERATIONS_GUESSED;
1907 /* If we have likely upper bound, trust it for very small iteration
1908 counts. Such loops would otherwise get mispredicted by standard
1909 LOOP_EXIT heuristics. */
1910 else if (n_exits == 1
1911 && likely_max_stmt_executions (loop, &nit)
1912 && wi::ltu_p (nit,
1913 RDIV (REG_BR_PROB_BASE,
1914 REG_BR_PROB_BASE
1915 - predictor_info
1916 [recursion
1917 ? PRED_LOOP_EXIT_WITH_RECURSION
1918 : PRED_LOOP_EXIT].hitrate)))
1920 nitercst = nit.to_shwi ();
1921 predictor = PRED_LOOP_ITERATIONS_MAX;
1923 else
1925 if (dump_file && (dump_flags & TDF_DETAILS))
1926 fprintf (dump_file, "Nothing known about exit %i->%i.\n",
1927 ex->src->index, ex->dest->index);
1928 continue;
1931 if (dump_file && (dump_flags & TDF_DETAILS))
1932 fprintf (dump_file, "Recording prediction to %i iterations by %s.\n",
1933 (int)nitercst, predictor_info[predictor].name);
1934 /* If the prediction for number of iterations is zero, do not
1935 predict the exit edges. */
1936 if (nitercst == 0)
1937 continue;
1939 probability = RDIV (REG_BR_PROB_BASE, nitercst);
1940 predict_edge (ex, predictor, probability);
1942 exits.release ();
1944 /* Find information about loop bound variables. */
1945 for (nb_iter = loop->bounds; nb_iter;
1946 nb_iter = nb_iter->next)
1947 if (nb_iter->stmt
1948 && gimple_code (nb_iter->stmt) == GIMPLE_COND)
1950 stmt = as_a <gcond *> (nb_iter->stmt);
1951 break;
1953 if (!stmt && last_stmt (loop->header)
1954 && gimple_code (last_stmt (loop->header)) == GIMPLE_COND)
1955 stmt = as_a <gcond *> (last_stmt (loop->header));
1956 if (stmt)
1957 is_comparison_with_loop_invariant_p (stmt, loop,
1958 &loop_bound_var,
1959 &loop_bound_code,
1960 &loop_bound_step,
1961 &loop_iv_base);
1963 bbs = get_loop_body (loop);
1965 for (j = 0; j < loop->num_nodes; j++)
1967 edge e;
1968 edge_iterator ei;
1970 bb = bbs[j];
1972 /* Bypass loop heuristics on continue statement. These
1973 statements construct loops via "non-loop" constructs
1974 in the source language and are better to be handled
1975 separately. */
1976 if (predicted_by_p (bb, PRED_CONTINUE))
1978 if (dump_file && (dump_flags & TDF_DETAILS))
1979 fprintf (dump_file, "BB %i predicted by continue.\n",
1980 bb->index);
1981 continue;
1984 /* If we already used more reliable loop exit predictors, do not
1985 bother with PRED_LOOP_EXIT. */
1986 if (!predicted_by_loop_heuristics_p (bb))
1988 /* For loop with many exits we don't want to predict all exits
1989 with the pretty large probability, because if all exits are
1990 considered in row, the loop would be predicted to iterate
1991 almost never. The code to divide probability by number of
1992 exits is very rough. It should compute the number of exits
1993 taken in each patch through function (not the overall number
1994 of exits that might be a lot higher for loops with wide switch
1995 statements in them) and compute n-th square root.
1997 We limit the minimal probability by 2% to avoid
1998 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
1999 as this was causing regression in perl benchmark containing such
2000 a wide loop. */
2002 int probability = ((REG_BR_PROB_BASE
2003 - predictor_info
2004 [recursion
2005 ? PRED_LOOP_EXIT_WITH_RECURSION
2006 : PRED_LOOP_EXIT].hitrate)
2007 / n_exits);
2008 if (probability < HITRATE (2))
2009 probability = HITRATE (2);
2010 FOR_EACH_EDGE (e, ei, bb->succs)
2011 if (e->dest->index < NUM_FIXED_BLOCKS
2012 || !flow_bb_inside_loop_p (loop, e->dest))
2014 if (dump_file && (dump_flags & TDF_DETAILS))
2015 fprintf (dump_file,
2016 "Predicting exit %i->%i with prob %i.\n",
2017 e->src->index, e->dest->index, probability);
2018 predict_edge (e,
2019 recursion ? PRED_LOOP_EXIT_WITH_RECURSION
2020 : PRED_LOOP_EXIT, probability);
2023 if (loop_bound_var)
2024 predict_iv_comparison (loop, bb, loop_bound_var, loop_iv_base,
2025 loop_bound_code,
2026 tree_to_shwi (loop_bound_step));
2029 /* In the following code
2030 for (loop1)
2031 if (cond)
2032 for (loop2)
2033 body;
2034 guess that cond is unlikely. */
2035 if (loop_outer (loop)->num)
2037 basic_block bb = NULL;
2038 edge preheader_edge = loop_preheader_edge (loop);
2040 if (single_pred_p (preheader_edge->src)
2041 && single_succ_p (preheader_edge->src))
2042 preheader_edge = single_pred_edge (preheader_edge->src);
2044 gimple *stmt = last_stmt (preheader_edge->src);
2045 /* Pattern match fortran loop preheader:
2046 _16 = BUILTIN_EXPECT (_15, 1, PRED_FORTRAN_LOOP_PREHEADER);
2047 _17 = (logical(kind=4)) _16;
2048 if (_17 != 0)
2049 goto <bb 11>;
2050 else
2051 goto <bb 13>;
2053 Loop guard branch prediction says nothing about duplicated loop
2054 headers produced by fortran frontend and in this case we want
2055 to predict paths leading to this preheader. */
2057 if (stmt
2058 && gimple_code (stmt) == GIMPLE_COND
2059 && gimple_cond_code (stmt) == NE_EXPR
2060 && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
2061 && integer_zerop (gimple_cond_rhs (stmt)))
2063 gimple *call_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
2064 if (gimple_code (call_stmt) == GIMPLE_ASSIGN
2065 && gimple_expr_code (call_stmt) == NOP_EXPR
2066 && TREE_CODE (gimple_assign_rhs1 (call_stmt)) == SSA_NAME)
2067 call_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (call_stmt));
2068 if (gimple_call_internal_p (call_stmt, IFN_BUILTIN_EXPECT)
2069 && TREE_CODE (gimple_call_arg (call_stmt, 2)) == INTEGER_CST
2070 && tree_fits_uhwi_p (gimple_call_arg (call_stmt, 2))
2071 && tree_to_uhwi (gimple_call_arg (call_stmt, 2))
2072 == PRED_FORTRAN_LOOP_PREHEADER)
2073 bb = preheader_edge->src;
2075 if (!bb)
2077 if (!dominated_by_p (CDI_DOMINATORS,
2078 loop_outer (loop)->latch, loop->header))
2079 predict_paths_leading_to_edge (loop_preheader_edge (loop),
2080 recursion
2081 ? PRED_LOOP_GUARD_WITH_RECURSION
2082 : PRED_LOOP_GUARD,
2083 NOT_TAKEN,
2084 loop_outer (loop));
2086 else
2088 if (!dominated_by_p (CDI_DOMINATORS,
2089 loop_outer (loop)->latch, bb))
2090 predict_paths_leading_to (bb,
2091 recursion
2092 ? PRED_LOOP_GUARD_WITH_RECURSION
2093 : PRED_LOOP_GUARD,
2094 NOT_TAKEN,
2095 loop_outer (loop));
2099 /* Free basic blocks from get_loop_body. */
2100 free (bbs);
2104 /* Attempt to predict probabilities of BB outgoing edges using local
2105 properties. */
2106 static void
2107 bb_estimate_probability_locally (basic_block bb)
2109 rtx_insn *last_insn = BB_END (bb);
2110 rtx cond;
2112 if (! can_predict_insn_p (last_insn))
2113 return;
2114 cond = get_condition (last_insn, NULL, false, false);
2115 if (! cond)
2116 return;
2118 /* Try "pointer heuristic."
2119 A comparison ptr == 0 is predicted as false.
2120 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2121 if (COMPARISON_P (cond)
2122 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
2123 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
2125 if (GET_CODE (cond) == EQ)
2126 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
2127 else if (GET_CODE (cond) == NE)
2128 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
2130 else
2132 /* Try "opcode heuristic."
2133 EQ tests are usually false and NE tests are usually true. Also,
2134 most quantities are positive, so we can make the appropriate guesses
2135 about signed comparisons against zero. */
2136 switch (GET_CODE (cond))
2138 case CONST_INT:
2139 /* Unconditional branch. */
2140 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
2141 cond == const0_rtx ? NOT_TAKEN : TAKEN);
2142 break;
2144 case EQ:
2145 case UNEQ:
2146 /* Floating point comparisons appears to behave in a very
2147 unpredictable way because of special role of = tests in
2148 FP code. */
2149 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2151 /* Comparisons with 0 are often used for booleans and there is
2152 nothing useful to predict about them. */
2153 else if (XEXP (cond, 1) == const0_rtx
2154 || XEXP (cond, 0) == const0_rtx)
2156 else
2157 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
2158 break;
2160 case NE:
2161 case LTGT:
2162 /* Floating point comparisons appears to behave in a very
2163 unpredictable way because of special role of = tests in
2164 FP code. */
2165 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2167 /* Comparisons with 0 are often used for booleans and there is
2168 nothing useful to predict about them. */
2169 else if (XEXP (cond, 1) == const0_rtx
2170 || XEXP (cond, 0) == const0_rtx)
2172 else
2173 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
2174 break;
2176 case ORDERED:
2177 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
2178 break;
2180 case UNORDERED:
2181 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
2182 break;
2184 case LE:
2185 case LT:
2186 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2187 || XEXP (cond, 1) == constm1_rtx)
2188 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
2189 break;
2191 case GE:
2192 case GT:
2193 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2194 || XEXP (cond, 1) == constm1_rtx)
2195 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
2196 break;
2198 default:
2199 break;
2203 /* Set edge->probability for each successor edge of BB. */
2204 void
2205 guess_outgoing_edge_probabilities (basic_block bb)
2207 bb_estimate_probability_locally (bb);
2208 combine_predictions_for_insn (BB_END (bb), bb);
2211 static tree expr_expected_value (tree, bitmap, enum br_predictor *predictor);
2213 /* Helper function for expr_expected_value. */
2215 static tree
2216 expr_expected_value_1 (tree type, tree op0, enum tree_code code,
2217 tree op1, bitmap visited, enum br_predictor *predictor)
2219 gimple *def;
2221 if (predictor)
2222 *predictor = PRED_UNCONDITIONAL;
2224 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
2226 if (TREE_CONSTANT (op0))
2227 return op0;
2229 if (code == IMAGPART_EXPR)
2231 if (TREE_CODE (TREE_OPERAND (op0, 0)) == SSA_NAME)
2233 def = SSA_NAME_DEF_STMT (TREE_OPERAND (op0, 0));
2234 if (is_gimple_call (def)
2235 && gimple_call_internal_p (def)
2236 && (gimple_call_internal_fn (def)
2237 == IFN_ATOMIC_COMPARE_EXCHANGE))
2239 /* Assume that any given atomic operation has low contention,
2240 and thus the compare-and-swap operation succeeds. */
2241 if (predictor)
2242 *predictor = PRED_COMPARE_AND_SWAP;
2243 return build_one_cst (TREE_TYPE (op0));
2248 if (code != SSA_NAME)
2249 return NULL_TREE;
2251 def = SSA_NAME_DEF_STMT (op0);
2253 /* If we were already here, break the infinite cycle. */
2254 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (op0)))
2255 return NULL;
2257 if (gimple_code (def) == GIMPLE_PHI)
2259 /* All the arguments of the PHI node must have the same constant
2260 length. */
2261 int i, n = gimple_phi_num_args (def);
2262 tree val = NULL, new_val;
2264 for (i = 0; i < n; i++)
2266 tree arg = PHI_ARG_DEF (def, i);
2267 enum br_predictor predictor2;
2269 /* If this PHI has itself as an argument, we cannot
2270 determine the string length of this argument. However,
2271 if we can find an expected constant value for the other
2272 PHI args then we can still be sure that this is
2273 likely a constant. So be optimistic and just
2274 continue with the next argument. */
2275 if (arg == PHI_RESULT (def))
2276 continue;
2278 new_val = expr_expected_value (arg, visited, &predictor2);
2280 /* It is difficult to combine value predictors. Simply assume
2281 that later predictor is weaker and take its prediction. */
2282 if (predictor && *predictor < predictor2)
2283 *predictor = predictor2;
2284 if (!new_val)
2285 return NULL;
2286 if (!val)
2287 val = new_val;
2288 else if (!operand_equal_p (val, new_val, false))
2289 return NULL;
2291 return val;
2293 if (is_gimple_assign (def))
2295 if (gimple_assign_lhs (def) != op0)
2296 return NULL;
2298 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)),
2299 gimple_assign_rhs1 (def),
2300 gimple_assign_rhs_code (def),
2301 gimple_assign_rhs2 (def),
2302 visited, predictor);
2305 if (is_gimple_call (def))
2307 tree decl = gimple_call_fndecl (def);
2308 if (!decl)
2310 if (gimple_call_internal_p (def)
2311 && gimple_call_internal_fn (def) == IFN_BUILTIN_EXPECT)
2313 gcc_assert (gimple_call_num_args (def) == 3);
2314 tree val = gimple_call_arg (def, 0);
2315 if (TREE_CONSTANT (val))
2316 return val;
2317 if (predictor)
2319 tree val2 = gimple_call_arg (def, 2);
2320 gcc_assert (TREE_CODE (val2) == INTEGER_CST
2321 && tree_fits_uhwi_p (val2)
2322 && tree_to_uhwi (val2) < END_PREDICTORS);
2323 *predictor = (enum br_predictor) tree_to_uhwi (val2);
2325 return gimple_call_arg (def, 1);
2327 return NULL;
2329 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2330 switch (DECL_FUNCTION_CODE (decl))
2332 case BUILT_IN_EXPECT:
2334 tree val;
2335 if (gimple_call_num_args (def) != 2)
2336 return NULL;
2337 val = gimple_call_arg (def, 0);
2338 if (TREE_CONSTANT (val))
2339 return val;
2340 if (predictor)
2341 *predictor = PRED_BUILTIN_EXPECT;
2342 return gimple_call_arg (def, 1);
2345 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
2346 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
2347 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
2348 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
2349 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
2350 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
2351 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
2352 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
2353 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
2354 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
2355 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
2356 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
2357 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
2358 /* Assume that any given atomic operation has low contention,
2359 and thus the compare-and-swap operation succeeds. */
2360 if (predictor)
2361 *predictor = PRED_COMPARE_AND_SWAP;
2362 return boolean_true_node;
2363 default:
2364 break;
2368 return NULL;
2371 if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
2373 tree res;
2374 enum br_predictor predictor2;
2375 op0 = expr_expected_value (op0, visited, predictor);
2376 if (!op0)
2377 return NULL;
2378 op1 = expr_expected_value (op1, visited, &predictor2);
2379 if (predictor && *predictor < predictor2)
2380 *predictor = predictor2;
2381 if (!op1)
2382 return NULL;
2383 res = fold_build2 (code, type, op0, op1);
2384 if (TREE_CONSTANT (res))
2385 return res;
2386 return NULL;
2388 if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
2390 tree res;
2391 op0 = expr_expected_value (op0, visited, predictor);
2392 if (!op0)
2393 return NULL;
2394 res = fold_build1 (code, type, op0);
2395 if (TREE_CONSTANT (res))
2396 return res;
2397 return NULL;
2399 return NULL;
2402 /* Return constant EXPR will likely have at execution time, NULL if unknown.
2403 The function is used by builtin_expect branch predictor so the evidence
2404 must come from this construct and additional possible constant folding.
2406 We may want to implement more involved value guess (such as value range
2407 propagation based prediction), but such tricks shall go to new
2408 implementation. */
2410 static tree
2411 expr_expected_value (tree expr, bitmap visited,
2412 enum br_predictor *predictor)
2414 enum tree_code code;
2415 tree op0, op1;
2417 if (TREE_CONSTANT (expr))
2419 if (predictor)
2420 *predictor = PRED_UNCONDITIONAL;
2421 return expr;
2424 extract_ops_from_tree (expr, &code, &op0, &op1);
2425 return expr_expected_value_1 (TREE_TYPE (expr),
2426 op0, code, op1, visited, predictor);
2429 /* Predict using opcode of the last statement in basic block. */
2430 static void
2431 tree_predict_by_opcode (basic_block bb)
2433 gimple *stmt = last_stmt (bb);
2434 edge then_edge;
2435 tree op0, op1;
2436 tree type;
2437 tree val;
2438 enum tree_code cmp;
2439 edge_iterator ei;
2440 enum br_predictor predictor;
2442 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
2443 return;
2444 FOR_EACH_EDGE (then_edge, ei, bb->succs)
2445 if (then_edge->flags & EDGE_TRUE_VALUE)
2446 break;
2447 op0 = gimple_cond_lhs (stmt);
2448 op1 = gimple_cond_rhs (stmt);
2449 cmp = gimple_cond_code (stmt);
2450 type = TREE_TYPE (op0);
2451 val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, auto_bitmap (),
2452 &predictor);
2453 if (val && TREE_CODE (val) == INTEGER_CST)
2455 if (predictor == PRED_BUILTIN_EXPECT)
2457 int percent = PARAM_VALUE (BUILTIN_EXPECT_PROBABILITY);
2459 gcc_assert (percent >= 0 && percent <= 100);
2460 if (integer_zerop (val))
2461 percent = 100 - percent;
2462 predict_edge (then_edge, PRED_BUILTIN_EXPECT, HITRATE (percent));
2464 else
2465 predict_edge_def (then_edge, predictor,
2466 integer_zerop (val) ? NOT_TAKEN : TAKEN);
2468 /* Try "pointer heuristic."
2469 A comparison ptr == 0 is predicted as false.
2470 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2471 if (POINTER_TYPE_P (type))
2473 if (cmp == EQ_EXPR)
2474 predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
2475 else if (cmp == NE_EXPR)
2476 predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
2478 else
2480 /* Try "opcode heuristic."
2481 EQ tests are usually false and NE tests are usually true. Also,
2482 most quantities are positive, so we can make the appropriate guesses
2483 about signed comparisons against zero. */
2484 switch (cmp)
2486 case EQ_EXPR:
2487 case UNEQ_EXPR:
2488 /* Floating point comparisons appears to behave in a very
2489 unpredictable way because of special role of = tests in
2490 FP code. */
2491 if (FLOAT_TYPE_P (type))
2493 /* Comparisons with 0 are often used for booleans and there is
2494 nothing useful to predict about them. */
2495 else if (integer_zerop (op0) || integer_zerop (op1))
2497 else
2498 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
2499 break;
2501 case NE_EXPR:
2502 case LTGT_EXPR:
2503 /* Floating point comparisons appears to behave in a very
2504 unpredictable way because of special role of = tests in
2505 FP code. */
2506 if (FLOAT_TYPE_P (type))
2508 /* Comparisons with 0 are often used for booleans and there is
2509 nothing useful to predict about them. */
2510 else if (integer_zerop (op0)
2511 || integer_zerop (op1))
2513 else
2514 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
2515 break;
2517 case ORDERED_EXPR:
2518 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
2519 break;
2521 case UNORDERED_EXPR:
2522 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
2523 break;
2525 case LE_EXPR:
2526 case LT_EXPR:
2527 if (integer_zerop (op1)
2528 || integer_onep (op1)
2529 || integer_all_onesp (op1)
2530 || real_zerop (op1)
2531 || real_onep (op1)
2532 || real_minus_onep (op1))
2533 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
2534 break;
2536 case GE_EXPR:
2537 case GT_EXPR:
2538 if (integer_zerop (op1)
2539 || integer_onep (op1)
2540 || integer_all_onesp (op1)
2541 || real_zerop (op1)
2542 || real_onep (op1)
2543 || real_minus_onep (op1))
2544 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
2545 break;
2547 default:
2548 break;
2552 /* Returns TRUE if the STMT is exit(0) like statement. */
2554 static bool
2555 is_exit_with_zero_arg (const gimple *stmt)
2557 /* This is not exit, _exit or _Exit. */
2558 if (!gimple_call_builtin_p (stmt, BUILT_IN_EXIT)
2559 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT)
2560 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT2))
2561 return false;
2563 /* Argument is an interger zero. */
2564 return integer_zerop (gimple_call_arg (stmt, 0));
2567 /* Try to guess whether the value of return means error code. */
2569 static enum br_predictor
2570 return_prediction (tree val, enum prediction *prediction)
2572 /* VOID. */
2573 if (!val)
2574 return PRED_NO_PREDICTION;
2575 /* Different heuristics for pointers and scalars. */
2576 if (POINTER_TYPE_P (TREE_TYPE (val)))
2578 /* NULL is usually not returned. */
2579 if (integer_zerop (val))
2581 *prediction = NOT_TAKEN;
2582 return PRED_NULL_RETURN;
2585 else if (INTEGRAL_TYPE_P (TREE_TYPE (val)))
2587 /* Negative return values are often used to indicate
2588 errors. */
2589 if (TREE_CODE (val) == INTEGER_CST
2590 && tree_int_cst_sgn (val) < 0)
2592 *prediction = NOT_TAKEN;
2593 return PRED_NEGATIVE_RETURN;
2595 /* Constant return values seems to be commonly taken.
2596 Zero/one often represent booleans so exclude them from the
2597 heuristics. */
2598 if (TREE_CONSTANT (val)
2599 && (!integer_zerop (val) && !integer_onep (val)))
2601 *prediction = NOT_TAKEN;
2602 return PRED_CONST_RETURN;
2605 return PRED_NO_PREDICTION;
2608 /* Find the basic block with return expression and look up for possible
2609 return value trying to apply RETURN_PREDICTION heuristics. */
2610 static void
2611 apply_return_prediction (void)
2613 greturn *return_stmt = NULL;
2614 tree return_val;
2615 edge e;
2616 gphi *phi;
2617 int phi_num_args, i;
2618 enum br_predictor pred;
2619 enum prediction direction;
2620 edge_iterator ei;
2622 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2624 gimple *last = last_stmt (e->src);
2625 if (last
2626 && gimple_code (last) == GIMPLE_RETURN)
2628 return_stmt = as_a <greturn *> (last);
2629 break;
2632 if (!e)
2633 return;
2634 return_val = gimple_return_retval (return_stmt);
2635 if (!return_val)
2636 return;
2637 if (TREE_CODE (return_val) != SSA_NAME
2638 || !SSA_NAME_DEF_STMT (return_val)
2639 || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI)
2640 return;
2641 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (return_val));
2642 phi_num_args = gimple_phi_num_args (phi);
2643 pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction);
2645 /* Avoid the degenerate case where all return values form the function
2646 belongs to same category (ie they are all positive constants)
2647 so we can hardly say something about them. */
2648 for (i = 1; i < phi_num_args; i++)
2649 if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction))
2650 break;
2651 if (i != phi_num_args)
2652 for (i = 0; i < phi_num_args; i++)
2654 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
2655 if (pred != PRED_NO_PREDICTION)
2656 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi, i), pred,
2657 direction);
2661 /* Look for basic block that contains unlikely to happen events
2662 (such as noreturn calls) and mark all paths leading to execution
2663 of this basic blocks as unlikely. */
2665 static void
2666 tree_bb_level_predictions (void)
2668 basic_block bb;
2669 bool has_return_edges = false;
2670 edge e;
2671 edge_iterator ei;
2673 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2674 if (!unlikely_executed_edge_p (e) && !(e->flags & EDGE_ABNORMAL_CALL))
2676 has_return_edges = true;
2677 break;
2680 apply_return_prediction ();
2682 FOR_EACH_BB_FN (bb, cfun)
2684 gimple_stmt_iterator gsi;
2686 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2688 gimple *stmt = gsi_stmt (gsi);
2689 tree decl;
2691 if (is_gimple_call (stmt))
2693 if (gimple_call_noreturn_p (stmt)
2694 && has_return_edges
2695 && !is_exit_with_zero_arg (stmt))
2696 predict_paths_leading_to (bb, PRED_NORETURN,
2697 NOT_TAKEN);
2698 decl = gimple_call_fndecl (stmt);
2699 if (decl
2700 && lookup_attribute ("cold",
2701 DECL_ATTRIBUTES (decl)))
2702 predict_paths_leading_to (bb, PRED_COLD_FUNCTION,
2703 NOT_TAKEN);
2704 if (decl && recursive_call_p (current_function_decl, decl))
2705 predict_paths_leading_to (bb, PRED_RECURSIVE_CALL,
2706 NOT_TAKEN);
2708 else if (gimple_code (stmt) == GIMPLE_PREDICT)
2710 predict_paths_leading_to (bb, gimple_predict_predictor (stmt),
2711 gimple_predict_outcome (stmt));
2712 /* Keep GIMPLE_PREDICT around so early inlining will propagate
2713 hints to callers. */
2719 /* Callback for hash_map::traverse, asserts that the pointer map is
2720 empty. */
2722 bool
2723 assert_is_empty (const_basic_block const &, edge_prediction *const &value,
2724 void *)
2726 gcc_assert (!value);
2727 return false;
2730 /* Predict branch probabilities and estimate profile for basic block BB.
2731 When LOCAL_ONLY is set do not use any global properties of CFG. */
2733 static void
2734 tree_estimate_probability_bb (basic_block bb, bool local_only)
2736 edge e;
2737 edge_iterator ei;
2738 gimple *last;
2740 FOR_EACH_EDGE (e, ei, bb->succs)
2742 /* Predict edges to user labels with attributes. */
2743 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2745 gimple_stmt_iterator gi;
2746 for (gi = gsi_start_bb (e->dest); !gsi_end_p (gi); gsi_next (&gi))
2748 glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gi));
2749 tree decl;
2751 if (!label_stmt)
2752 break;
2753 decl = gimple_label_label (label_stmt);
2754 if (DECL_ARTIFICIAL (decl))
2755 continue;
2757 /* Finally, we have a user-defined label. */
2758 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl)))
2759 predict_edge_def (e, PRED_COLD_LABEL, NOT_TAKEN);
2760 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (decl)))
2761 predict_edge_def (e, PRED_HOT_LABEL, TAKEN);
2765 /* Predict early returns to be probable, as we've already taken
2766 care for error returns and other cases are often used for
2767 fast paths through function.
2769 Since we've already removed the return statements, we are
2770 looking for CFG like:
2772 if (conditional)
2775 goto return_block
2777 some other blocks
2778 return_block:
2779 return_stmt. */
2780 if (e->dest != bb->next_bb
2781 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
2782 && single_succ_p (e->dest)
2783 && single_succ_edge (e->dest)->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
2784 && (last = last_stmt (e->dest)) != NULL
2785 && gimple_code (last) == GIMPLE_RETURN)
2787 edge e1;
2788 edge_iterator ei1;
2790 if (single_succ_p (bb))
2792 FOR_EACH_EDGE (e1, ei1, bb->preds)
2793 if (!predicted_by_p (e1->src, PRED_NULL_RETURN)
2794 && !predicted_by_p (e1->src, PRED_CONST_RETURN)
2795 && !predicted_by_p (e1->src, PRED_NEGATIVE_RETURN))
2796 predict_edge_def (e1, PRED_TREE_EARLY_RETURN, NOT_TAKEN);
2798 else
2799 if (!predicted_by_p (e->src, PRED_NULL_RETURN)
2800 && !predicted_by_p (e->src, PRED_CONST_RETURN)
2801 && !predicted_by_p (e->src, PRED_NEGATIVE_RETURN))
2802 predict_edge_def (e, PRED_TREE_EARLY_RETURN, NOT_TAKEN);
2805 /* Look for block we are guarding (ie we dominate it,
2806 but it doesn't postdominate us). */
2807 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest != bb
2808 && !local_only
2809 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
2810 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
2812 gimple_stmt_iterator bi;
2814 /* The call heuristic claims that a guarded function call
2815 is improbable. This is because such calls are often used
2816 to signal exceptional situations such as printing error
2817 messages. */
2818 for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi);
2819 gsi_next (&bi))
2821 gimple *stmt = gsi_stmt (bi);
2822 if (is_gimple_call (stmt)
2823 && !gimple_inexpensive_call_p (as_a <gcall *> (stmt))
2824 /* Constant and pure calls are hardly used to signalize
2825 something exceptional. */
2826 && gimple_has_side_effects (stmt))
2828 if (gimple_call_fndecl (stmt))
2829 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
2830 else if (virtual_method_call_p (gimple_call_fn (stmt)))
2831 predict_edge_def (e, PRED_POLYMORPHIC_CALL, NOT_TAKEN);
2832 else
2833 predict_edge_def (e, PRED_INDIR_CALL, TAKEN);
2834 break;
2839 tree_predict_by_opcode (bb);
2842 /* Predict branch probabilities and estimate profile of the tree CFG.
2843 This function can be called from the loop optimizers to recompute
2844 the profile information.
2845 If DRY_RUN is set, do not modify CFG and only produce dump files. */
2847 void
2848 tree_estimate_probability (bool dry_run)
2850 basic_block bb;
2852 add_noreturn_fake_exit_edges ();
2853 connect_infinite_loops_to_exit ();
2854 /* We use loop_niter_by_eval, which requires that the loops have
2855 preheaders. */
2856 create_preheaders (CP_SIMPLE_PREHEADERS);
2857 calculate_dominance_info (CDI_POST_DOMINATORS);
2859 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
2860 tree_bb_level_predictions ();
2861 record_loop_exits ();
2863 if (number_of_loops (cfun) > 1)
2864 predict_loops ();
2866 FOR_EACH_BB_FN (bb, cfun)
2867 tree_estimate_probability_bb (bb, false);
2869 FOR_EACH_BB_FN (bb, cfun)
2870 combine_predictions_for_bb (bb, dry_run);
2872 if (flag_checking)
2873 bb_predictions->traverse<void *, assert_is_empty> (NULL);
2875 delete bb_predictions;
2876 bb_predictions = NULL;
2878 if (!dry_run)
2879 estimate_bb_frequencies (false);
2880 free_dominance_info (CDI_POST_DOMINATORS);
2881 remove_fake_exit_edges ();
2884 /* Set edge->probability for each successor edge of BB. */
2885 void
2886 tree_guess_outgoing_edge_probabilities (basic_block bb)
2888 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
2889 tree_estimate_probability_bb (bb, true);
2890 combine_predictions_for_bb (bb, false);
2891 if (flag_checking)
2892 bb_predictions->traverse<void *, assert_is_empty> (NULL);
2893 delete bb_predictions;
2894 bb_predictions = NULL;
2897 /* Predict edges to successors of CUR whose sources are not postdominated by
2898 BB by PRED and recurse to all postdominators. */
2900 static void
2901 predict_paths_for_bb (basic_block cur, basic_block bb,
2902 enum br_predictor pred,
2903 enum prediction taken,
2904 bitmap visited, struct loop *in_loop = NULL)
2906 edge e;
2907 edge_iterator ei;
2908 basic_block son;
2910 /* If we exited the loop or CUR is unconditional in the loop, there is
2911 nothing to do. */
2912 if (in_loop
2913 && (!flow_bb_inside_loop_p (in_loop, cur)
2914 || dominated_by_p (CDI_DOMINATORS, in_loop->latch, cur)))
2915 return;
2917 /* We are looking for all edges forming edge cut induced by
2918 set of all blocks postdominated by BB. */
2919 FOR_EACH_EDGE (e, ei, cur->preds)
2920 if (e->src->index >= NUM_FIXED_BLOCKS
2921 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb))
2923 edge e2;
2924 edge_iterator ei2;
2925 bool found = false;
2927 /* Ignore fake edges and eh, we predict them as not taken anyway. */
2928 if (unlikely_executed_edge_p (e))
2929 continue;
2930 gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb));
2932 /* See if there is an edge from e->src that is not abnormal
2933 and does not lead to BB and does not exit the loop. */
2934 FOR_EACH_EDGE (e2, ei2, e->src->succs)
2935 if (e2 != e
2936 && !unlikely_executed_edge_p (e2)
2937 && !dominated_by_p (CDI_POST_DOMINATORS, e2->dest, bb)
2938 && (!in_loop || !loop_exit_edge_p (in_loop, e2)))
2940 found = true;
2941 break;
2944 /* If there is non-abnormal path leaving e->src, predict edge
2945 using predictor. Otherwise we need to look for paths
2946 leading to e->src.
2948 The second may lead to infinite loop in the case we are predicitng
2949 regions that are only reachable by abnormal edges. We simply
2950 prevent visiting given BB twice. */
2951 if (found)
2953 if (!edge_predicted_by_p (e, pred, taken))
2954 predict_edge_def (e, pred, taken);
2956 else if (bitmap_set_bit (visited, e->src->index))
2957 predict_paths_for_bb (e->src, e->src, pred, taken, visited, in_loop);
2959 for (son = first_dom_son (CDI_POST_DOMINATORS, cur);
2960 son;
2961 son = next_dom_son (CDI_POST_DOMINATORS, son))
2962 predict_paths_for_bb (son, bb, pred, taken, visited, in_loop);
2965 /* Sets branch probabilities according to PREDiction and
2966 FLAGS. */
2968 static void
2969 predict_paths_leading_to (basic_block bb, enum br_predictor pred,
2970 enum prediction taken, struct loop *in_loop)
2972 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
2975 /* Like predict_paths_leading_to but take edge instead of basic block. */
2977 static void
2978 predict_paths_leading_to_edge (edge e, enum br_predictor pred,
2979 enum prediction taken, struct loop *in_loop)
2981 bool has_nonloop_edge = false;
2982 edge_iterator ei;
2983 edge e2;
2985 basic_block bb = e->src;
2986 FOR_EACH_EDGE (e2, ei, bb->succs)
2987 if (e2->dest != e->src && e2->dest != e->dest
2988 && !unlikely_executed_edge_p (e)
2989 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->dest))
2991 has_nonloop_edge = true;
2992 break;
2994 if (!has_nonloop_edge)
2996 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
2998 else
2999 predict_edge_def (e, pred, taken);
3002 /* This is used to carry information about basic blocks. It is
3003 attached to the AUX field of the standard CFG block. */
3005 struct block_info
3007 /* Estimated frequency of execution of basic_block. */
3008 sreal frequency;
3010 /* To keep queue of basic blocks to process. */
3011 basic_block next;
3013 /* Number of predecessors we need to visit first. */
3014 int npredecessors;
3017 /* Similar information for edges. */
3018 struct edge_prob_info
3020 /* In case edge is a loopback edge, the probability edge will be reached
3021 in case header is. Estimated number of iterations of the loop can be
3022 then computed as 1 / (1 - back_edge_prob). */
3023 sreal back_edge_prob;
3024 /* True if the edge is a loopback edge in the natural loop. */
3025 unsigned int back_edge:1;
3028 #define BLOCK_INFO(B) ((block_info *) (B)->aux)
3029 #undef EDGE_INFO
3030 #define EDGE_INFO(E) ((edge_prob_info *) (E)->aux)
3032 /* Helper function for estimate_bb_frequencies.
3033 Propagate the frequencies in blocks marked in
3034 TOVISIT, starting in HEAD. */
3036 static void
3037 propagate_freq (basic_block head, bitmap tovisit)
3039 basic_block bb;
3040 basic_block last;
3041 unsigned i;
3042 edge e;
3043 basic_block nextbb;
3044 bitmap_iterator bi;
3046 /* For each basic block we need to visit count number of his predecessors
3047 we need to visit first. */
3048 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
3050 edge_iterator ei;
3051 int count = 0;
3053 bb = BASIC_BLOCK_FOR_FN (cfun, i);
3055 FOR_EACH_EDGE (e, ei, bb->preds)
3057 bool visit = bitmap_bit_p (tovisit, e->src->index);
3059 if (visit && !(e->flags & EDGE_DFS_BACK))
3060 count++;
3061 else if (visit && dump_file && !EDGE_INFO (e)->back_edge)
3062 fprintf (dump_file,
3063 "Irreducible region hit, ignoring edge to %i->%i\n",
3064 e->src->index, bb->index);
3066 BLOCK_INFO (bb)->npredecessors = count;
3067 /* When function never returns, we will never process exit block. */
3068 if (!count && bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3070 bb->count = profile_count::zero ();
3071 bb->frequency = 0;
3075 BLOCK_INFO (head)->frequency = 1;
3076 last = head;
3077 for (bb = head; bb; bb = nextbb)
3079 edge_iterator ei;
3080 sreal cyclic_probability = 0;
3081 sreal frequency = 0;
3083 nextbb = BLOCK_INFO (bb)->next;
3084 BLOCK_INFO (bb)->next = NULL;
3086 /* Compute frequency of basic block. */
3087 if (bb != head)
3089 if (flag_checking)
3090 FOR_EACH_EDGE (e, ei, bb->preds)
3091 gcc_assert (!bitmap_bit_p (tovisit, e->src->index)
3092 || (e->flags & EDGE_DFS_BACK));
3094 FOR_EACH_EDGE (e, ei, bb->preds)
3095 if (EDGE_INFO (e)->back_edge)
3097 cyclic_probability += EDGE_INFO (e)->back_edge_prob;
3099 else if (!(e->flags & EDGE_DFS_BACK))
3101 /* frequency += (e->probability
3102 * BLOCK_INFO (e->src)->frequency /
3103 REG_BR_PROB_BASE); */
3105 sreal tmp = e->probability;
3106 tmp *= BLOCK_INFO (e->src)->frequency;
3107 tmp *= real_inv_br_prob_base;
3108 frequency += tmp;
3111 if (cyclic_probability == 0)
3113 BLOCK_INFO (bb)->frequency = frequency;
3115 else
3117 if (cyclic_probability > real_almost_one)
3118 cyclic_probability = real_almost_one;
3120 /* BLOCK_INFO (bb)->frequency = frequency
3121 / (1 - cyclic_probability) */
3123 cyclic_probability = sreal (1) - cyclic_probability;
3124 BLOCK_INFO (bb)->frequency = frequency / cyclic_probability;
3128 bitmap_clear_bit (tovisit, bb->index);
3130 e = find_edge (bb, head);
3131 if (e)
3133 /* EDGE_INFO (e)->back_edge_prob
3134 = ((e->probability * BLOCK_INFO (bb)->frequency)
3135 / REG_BR_PROB_BASE); */
3137 sreal tmp = e->probability;
3138 tmp *= BLOCK_INFO (bb)->frequency;
3139 EDGE_INFO (e)->back_edge_prob = tmp * real_inv_br_prob_base;
3142 /* Propagate to successor blocks. */
3143 FOR_EACH_EDGE (e, ei, bb->succs)
3144 if (!(e->flags & EDGE_DFS_BACK)
3145 && BLOCK_INFO (e->dest)->npredecessors)
3147 BLOCK_INFO (e->dest)->npredecessors--;
3148 if (!BLOCK_INFO (e->dest)->npredecessors)
3150 if (!nextbb)
3151 nextbb = e->dest;
3152 else
3153 BLOCK_INFO (last)->next = e->dest;
3155 last = e->dest;
3161 /* Estimate frequencies in loops at same nest level. */
3163 static void
3164 estimate_loops_at_level (struct loop *first_loop)
3166 struct loop *loop;
3168 for (loop = first_loop; loop; loop = loop->next)
3170 edge e;
3171 basic_block *bbs;
3172 unsigned i;
3173 auto_bitmap tovisit;
3175 estimate_loops_at_level (loop->inner);
3177 /* Find current loop back edge and mark it. */
3178 e = loop_latch_edge (loop);
3179 EDGE_INFO (e)->back_edge = 1;
3181 bbs = get_loop_body (loop);
3182 for (i = 0; i < loop->num_nodes; i++)
3183 bitmap_set_bit (tovisit, bbs[i]->index);
3184 free (bbs);
3185 propagate_freq (loop->header, tovisit);
3189 /* Propagates frequencies through structure of loops. */
3191 static void
3192 estimate_loops (void)
3194 auto_bitmap tovisit;
3195 basic_block bb;
3197 /* Start by estimating the frequencies in the loops. */
3198 if (number_of_loops (cfun) > 1)
3199 estimate_loops_at_level (current_loops->tree_root->inner);
3201 /* Now propagate the frequencies through all the blocks. */
3202 FOR_ALL_BB_FN (bb, cfun)
3204 bitmap_set_bit (tovisit, bb->index);
3206 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun), tovisit);
3209 /* Drop the profile for NODE to guessed, and update its frequency based on
3210 whether it is expected to be hot given the CALL_COUNT. */
3212 static void
3213 drop_profile (struct cgraph_node *node, profile_count call_count)
3215 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3216 /* In the case where this was called by another function with a
3217 dropped profile, call_count will be 0. Since there are no
3218 non-zero call counts to this function, we don't know for sure
3219 whether it is hot, and therefore it will be marked normal below. */
3220 bool hot = maybe_hot_count_p (NULL, call_count);
3222 if (dump_file)
3223 fprintf (dump_file,
3224 "Dropping 0 profile for %s. %s based on calls.\n",
3225 node->dump_name (),
3226 hot ? "Function is hot" : "Function is normal");
3227 /* We only expect to miss profiles for functions that are reached
3228 via non-zero call edges in cases where the function may have
3229 been linked from another module or library (COMDATs and extern
3230 templates). See the comments below for handle_missing_profiles.
3231 Also, only warn in cases where the missing counts exceed the
3232 number of training runs. In certain cases with an execv followed
3233 by a no-return call the profile for the no-return call is not
3234 dumped and there can be a mismatch. */
3235 if (!DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl)
3236 && call_count > profile_info->runs)
3238 if (flag_profile_correction)
3240 if (dump_file)
3241 fprintf (dump_file,
3242 "Missing counts for called function %s\n",
3243 node->dump_name ());
3245 else
3246 warning (0, "Missing counts for called function %s",
3247 node->dump_name ());
3250 profile_status_for_fn (fn)
3251 = (flag_guess_branch_prob ? PROFILE_GUESSED : PROFILE_ABSENT);
3252 node->frequency
3253 = hot ? NODE_FREQUENCY_HOT : NODE_FREQUENCY_NORMAL;
3256 /* In the case of COMDAT routines, multiple object files will contain the same
3257 function and the linker will select one for the binary. In that case
3258 all the other copies from the profile instrument binary will be missing
3259 profile counts. Look for cases where this happened, due to non-zero
3260 call counts going to 0-count functions, and drop the profile to guessed
3261 so that we can use the estimated probabilities and avoid optimizing only
3262 for size.
3264 The other case where the profile may be missing is when the routine
3265 is not going to be emitted to the object file, e.g. for "extern template"
3266 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
3267 all other cases of non-zero calls to 0-count functions. */
3269 void
3270 handle_missing_profiles (void)
3272 struct cgraph_node *node;
3273 int unlikely_count_fraction = PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION);
3274 auto_vec<struct cgraph_node *, 64> worklist;
3276 /* See if 0 count function has non-0 count callers. In this case we
3277 lost some profile. Drop its function profile to PROFILE_GUESSED. */
3278 FOR_EACH_DEFINED_FUNCTION (node)
3280 struct cgraph_edge *e;
3281 profile_count call_count = profile_count::zero ();
3282 gcov_type max_tp_first_run = 0;
3283 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3285 if (!(node->count == profile_count::zero ()))
3286 continue;
3287 for (e = node->callers; e; e = e->next_caller)
3289 if (e->count.initialized_p () > 0)
3291 call_count = call_count + e->count;
3293 if (e->caller->tp_first_run > max_tp_first_run)
3294 max_tp_first_run = e->caller->tp_first_run;
3298 /* If time profile is missing, let assign the maximum that comes from
3299 caller functions. */
3300 if (!node->tp_first_run && max_tp_first_run)
3301 node->tp_first_run = max_tp_first_run + 1;
3303 if (call_count > 0
3304 && fn && fn->cfg
3305 && (call_count.apply_scale (unlikely_count_fraction, 1) >= profile_info->runs))
3307 drop_profile (node, call_count);
3308 worklist.safe_push (node);
3312 /* Propagate the profile dropping to other 0-count COMDATs that are
3313 potentially called by COMDATs we already dropped the profile on. */
3314 while (worklist.length () > 0)
3316 struct cgraph_edge *e;
3318 node = worklist.pop ();
3319 for (e = node->callees; e; e = e->next_caller)
3321 struct cgraph_node *callee = e->callee;
3322 struct function *fn = DECL_STRUCT_FUNCTION (callee->decl);
3324 if (callee->count > 0)
3325 continue;
3326 if (DECL_COMDAT (callee->decl) && fn && fn->cfg
3327 && profile_status_for_fn (fn) == PROFILE_READ)
3329 drop_profile (node, profile_count::zero ());
3330 worklist.safe_push (callee);
3336 /* Convert counts measured by profile driven feedback to frequencies.
3337 Return nonzero iff there was any nonzero execution count. */
3339 bool
3340 counts_to_freqs (void)
3342 gcov_type count_max;
3343 profile_count true_count_max = profile_count::zero ();
3344 basic_block bb;
3346 /* Don't overwrite the estimated frequencies when the profile for
3347 the function is missing. We may drop this function PROFILE_GUESSED
3348 later in drop_profile (). */
3349 if (!ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.initialized_p ()
3350 || ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ())
3351 return 0;
3353 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3354 if (bb->count > true_count_max)
3355 true_count_max = bb->count;
3357 count_max = MAX (true_count_max.to_gcov_type (), 1);
3359 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3360 if (bb->count.initialized_p ())
3361 bb->frequency = RDIV (bb->count.to_gcov_type () * BB_FREQ_MAX, count_max);
3363 return !(true_count_max == profile_count::zero ());
3366 /* Return true if function is likely to be expensive, so there is no point to
3367 optimize performance of prologue, epilogue or do inlining at the expense
3368 of code size growth. THRESHOLD is the limit of number of instructions
3369 function can execute at average to be still considered not expensive. */
3371 bool
3372 expensive_function_p (int threshold)
3374 unsigned int sum = 0;
3375 basic_block bb;
3376 unsigned int limit;
3378 /* We can not compute accurately for large thresholds due to scaled
3379 frequencies. */
3380 gcc_assert (threshold <= BB_FREQ_MAX);
3382 /* Frequencies are out of range. This either means that function contains
3383 internal loop executing more than BB_FREQ_MAX times or profile feedback
3384 is available and function has not been executed at all. */
3385 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency == 0)
3386 return true;
3388 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
3389 limit = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency * threshold;
3390 FOR_EACH_BB_FN (bb, cfun)
3392 rtx_insn *insn;
3394 FOR_BB_INSNS (bb, insn)
3395 if (active_insn_p (insn))
3397 sum += bb->frequency;
3398 if (sum > limit)
3399 return true;
3403 return false;
3406 /* Determine basic blocks/edges that are known to be unlikely executed and set
3407 their counters to zero.
3408 This is done with first identifying obviously unlikely BBs/edges and then
3409 propagating in both directions. */
3411 static void
3412 determine_unlikely_bbs ()
3414 basic_block bb;
3415 auto_vec<basic_block, 64> worklist;
3416 edge_iterator ei;
3417 edge e;
3419 FOR_EACH_BB_FN (bb, cfun)
3421 if (!(bb->count == profile_count::zero ())
3422 && unlikely_executed_bb_p (bb))
3424 if (dump_file && (dump_flags & TDF_DETAILS))
3425 fprintf (dump_file, "Basic block %i is locally unlikely\n",
3426 bb->index);
3427 bb->count = profile_count::zero ();
3430 if (bb->count == profile_count::zero ())
3432 bb->frequency = 0;
3433 FOR_EACH_EDGE (e, ei, bb->preds)
3434 e->count = profile_count::zero ();
3437 FOR_EACH_EDGE (e, ei, bb->succs)
3438 if (!(e->count == profile_count::zero ())
3439 && unlikely_executed_edge_p (e))
3441 if (dump_file && (dump_flags & TDF_DETAILS))
3442 fprintf (dump_file, "Edge %i->%i is locally unlikely\n",
3443 bb->index, e->dest->index);
3444 e->count = profile_count::zero ();
3447 gcc_checking_assert (!bb->aux);
3450 if (!(ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ()))
3452 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(size_t) 1;
3453 worklist.safe_push (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3455 while (worklist.length () > 0)
3457 bb = worklist.pop ();
3458 FOR_EACH_EDGE (e, ei, bb->succs)
3459 if (!(e->count == profile_count::zero ())
3460 && !(e->dest->count == profile_count::zero ())
3461 && !e->dest->aux)
3463 e->dest->aux = (void *)(size_t) 1;
3464 worklist.safe_push (e->dest);
3469 FOR_ALL_BB_FN (bb, cfun)
3471 if (!bb->aux)
3473 if (!(bb->count == profile_count::zero ())
3474 && (dump_file && (dump_flags & TDF_DETAILS)))
3475 fprintf (dump_file,
3476 "Basic block %i is marked unlikely by forward prop\n",
3477 bb->index);
3478 bb->count = profile_count::zero ();
3479 bb->frequency = 0;
3480 FOR_EACH_EDGE (e, ei, bb->succs)
3481 e->count = profile_count::zero ();
3483 else
3484 bb->aux = NULL;
3487 auto_vec<int, 64> nsuccs;
3488 nsuccs.safe_grow_cleared (last_basic_block_for_fn (cfun));
3489 FOR_ALL_BB_FN (bb, cfun)
3490 if (!(bb->count == profile_count::zero ())
3491 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3493 nsuccs[bb->index] = 0;
3494 FOR_EACH_EDGE (e, ei, bb->succs)
3495 if (!(e->count == profile_count::zero ()))
3496 nsuccs[bb->index]++;
3497 if (!nsuccs[bb->index])
3498 worklist.safe_push (bb);
3500 while (worklist.length () > 0)
3502 bb = worklist.pop ();
3503 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3505 bool found = false;
3506 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
3507 !gsi_end_p (gsi); gsi_next (&gsi))
3508 if (stmt_can_terminate_bb_p (gsi_stmt (gsi))
3509 /* stmt_can_terminate_bb_p special cases noreturns because it
3510 assumes that fake edges are created. We want to know that
3511 noreturn alone does not imply BB to be unlikely. */
3512 || (is_gimple_call (gsi_stmt (gsi))
3513 && (gimple_call_flags (gsi_stmt (gsi)) & ECF_NORETURN)))
3515 found = true;
3516 break;
3518 if (found)
3519 continue;
3521 if (!(bb->count == profile_count::zero ())
3522 && (dump_file && (dump_flags & TDF_DETAILS)))
3523 fprintf (dump_file,
3524 "Basic block %i is marked unlikely by backward prop\n",
3525 bb->index);
3526 bb->count = profile_count::zero ();
3527 bb->frequency = 0;
3528 FOR_EACH_EDGE (e, ei, bb->preds)
3529 if (!(e->count == profile_count::zero ()))
3531 e->count = profile_count::zero ();
3532 if (!(e->src->count == profile_count::zero ()))
3534 nsuccs[e->src->index]--;
3535 if (!nsuccs[e->src->index])
3536 worklist.safe_push (e->src);
3542 /* Estimate and propagate basic block frequencies using the given branch
3543 probabilities. If FORCE is true, the frequencies are used to estimate
3544 the counts even when there are already non-zero profile counts. */
3546 void
3547 estimate_bb_frequencies (bool force)
3549 basic_block bb;
3550 sreal freq_max;
3552 determine_unlikely_bbs ();
3554 if (force || profile_status_for_fn (cfun) != PROFILE_READ
3555 || !counts_to_freqs ())
3557 static int real_values_initialized = 0;
3559 if (!real_values_initialized)
3561 real_values_initialized = 1;
3562 real_br_prob_base = REG_BR_PROB_BASE;
3563 real_bb_freq_max = BB_FREQ_MAX;
3564 real_one_half = sreal (1, -1);
3565 real_inv_br_prob_base = sreal (1) / real_br_prob_base;
3566 real_almost_one = sreal (1) - real_inv_br_prob_base;
3569 mark_dfs_back_edges ();
3571 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->probability =
3572 REG_BR_PROB_BASE;
3574 /* Set up block info for each basic block. */
3575 alloc_aux_for_blocks (sizeof (block_info));
3576 alloc_aux_for_edges (sizeof (edge_prob_info));
3577 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3579 edge e;
3580 edge_iterator ei;
3582 FOR_EACH_EDGE (e, ei, bb->succs)
3584 EDGE_INFO (e)->back_edge_prob = e->probability;
3585 EDGE_INFO (e)->back_edge_prob *= real_inv_br_prob_base;
3589 /* First compute frequencies locally for each loop from innermost
3590 to outermost to examine frequencies for back edges. */
3591 estimate_loops ();
3593 freq_max = 0;
3594 FOR_EACH_BB_FN (bb, cfun)
3595 if (freq_max < BLOCK_INFO (bb)->frequency)
3596 freq_max = BLOCK_INFO (bb)->frequency;
3598 freq_max = real_bb_freq_max / freq_max;
3599 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3601 sreal tmp = BLOCK_INFO (bb)->frequency * freq_max + real_one_half;
3602 bb->frequency = tmp.to_int ();
3605 free_aux_for_blocks ();
3606 free_aux_for_edges ();
3608 compute_function_frequency ();
3611 /* Decide whether function is hot, cold or unlikely executed. */
3612 void
3613 compute_function_frequency (void)
3615 basic_block bb;
3616 struct cgraph_node *node = cgraph_node::get (current_function_decl);
3618 if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3619 || MAIN_NAME_P (DECL_NAME (current_function_decl)))
3620 node->only_called_at_startup = true;
3621 if (DECL_STATIC_DESTRUCTOR (current_function_decl))
3622 node->only_called_at_exit = true;
3624 if (profile_status_for_fn (cfun) != PROFILE_READ)
3626 int flags = flags_from_decl_or_type (current_function_decl);
3627 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ()
3628 || lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
3629 != NULL)
3630 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3631 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl))
3632 != NULL)
3633 node->frequency = NODE_FREQUENCY_HOT;
3634 else if (flags & ECF_NORETURN)
3635 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3636 else if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
3637 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3638 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3639 || DECL_STATIC_DESTRUCTOR (current_function_decl))
3640 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3641 return;
3644 /* Only first time try to drop function into unlikely executed.
3645 After inlining the roundoff errors may confuse us.
3646 Ipa-profile pass will drop functions only called from unlikely
3647 functions to unlikely and that is most of what we care about. */
3648 if (!cfun->after_inlining)
3649 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3650 FOR_EACH_BB_FN (bb, cfun)
3652 if (maybe_hot_bb_p (cfun, bb))
3654 node->frequency = NODE_FREQUENCY_HOT;
3655 return;
3657 if (!probably_never_executed_bb_p (cfun, bb))
3658 node->frequency = NODE_FREQUENCY_NORMAL;
3662 /* Build PREDICT_EXPR. */
3663 tree
3664 build_predict_expr (enum br_predictor predictor, enum prediction taken)
3666 tree t = build1 (PREDICT_EXPR, void_type_node,
3667 build_int_cst (integer_type_node, predictor));
3668 SET_PREDICT_EXPR_OUTCOME (t, taken);
3669 return t;
3672 const char *
3673 predictor_name (enum br_predictor predictor)
3675 return predictor_info[predictor].name;
3678 /* Predict branch probabilities and estimate profile of the tree CFG. */
3680 namespace {
3682 const pass_data pass_data_profile =
3684 GIMPLE_PASS, /* type */
3685 "profile_estimate", /* name */
3686 OPTGROUP_NONE, /* optinfo_flags */
3687 TV_BRANCH_PROB, /* tv_id */
3688 PROP_cfg, /* properties_required */
3689 0, /* properties_provided */
3690 0, /* properties_destroyed */
3691 0, /* todo_flags_start */
3692 0, /* todo_flags_finish */
3695 class pass_profile : public gimple_opt_pass
3697 public:
3698 pass_profile (gcc::context *ctxt)
3699 : gimple_opt_pass (pass_data_profile, ctxt)
3702 /* opt_pass methods: */
3703 virtual bool gate (function *) { return flag_guess_branch_prob; }
3704 virtual unsigned int execute (function *);
3706 }; // class pass_profile
3708 unsigned int
3709 pass_profile::execute (function *fun)
3711 unsigned nb_loops;
3713 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
3714 return 0;
3716 loop_optimizer_init (LOOPS_NORMAL);
3717 if (dump_file && (dump_flags & TDF_DETAILS))
3718 flow_loops_dump (dump_file, NULL, 0);
3720 mark_irreducible_loops ();
3722 nb_loops = number_of_loops (fun);
3723 if (nb_loops > 1)
3724 scev_initialize ();
3726 tree_estimate_probability (false);
3728 if (nb_loops > 1)
3729 scev_finalize ();
3731 loop_optimizer_finalize ();
3732 if (dump_file && (dump_flags & TDF_DETAILS))
3733 gimple_dump_cfg (dump_file, dump_flags);
3734 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
3735 profile_status_for_fn (fun) = PROFILE_GUESSED;
3736 if (dump_file && (dump_flags & TDF_DETAILS))
3738 struct loop *loop;
3739 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
3740 if (loop->header->frequency)
3741 fprintf (dump_file, "Loop got predicted %d to iterate %i times.\n",
3742 loop->num,
3743 (int)expected_loop_iterations_unbounded (loop));
3745 return 0;
3748 } // anon namespace
3750 gimple_opt_pass *
3751 make_pass_profile (gcc::context *ctxt)
3753 return new pass_profile (ctxt);
3756 namespace {
3758 const pass_data pass_data_strip_predict_hints =
3760 GIMPLE_PASS, /* type */
3761 "*strip_predict_hints", /* name */
3762 OPTGROUP_NONE, /* optinfo_flags */
3763 TV_BRANCH_PROB, /* tv_id */
3764 PROP_cfg, /* properties_required */
3765 0, /* properties_provided */
3766 0, /* properties_destroyed */
3767 0, /* todo_flags_start */
3768 0, /* todo_flags_finish */
3771 class pass_strip_predict_hints : public gimple_opt_pass
3773 public:
3774 pass_strip_predict_hints (gcc::context *ctxt)
3775 : gimple_opt_pass (pass_data_strip_predict_hints, ctxt)
3778 /* opt_pass methods: */
3779 opt_pass * clone () { return new pass_strip_predict_hints (m_ctxt); }
3780 virtual unsigned int execute (function *);
3782 }; // class pass_strip_predict_hints
3784 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
3785 we no longer need. */
3786 unsigned int
3787 pass_strip_predict_hints::execute (function *fun)
3789 basic_block bb;
3790 gimple *ass_stmt;
3791 tree var;
3792 bool changed = false;
3794 FOR_EACH_BB_FN (bb, fun)
3796 gimple_stmt_iterator bi;
3797 for (bi = gsi_start_bb (bb); !gsi_end_p (bi);)
3799 gimple *stmt = gsi_stmt (bi);
3801 if (gimple_code (stmt) == GIMPLE_PREDICT)
3803 gsi_remove (&bi, true);
3804 changed = true;
3805 continue;
3807 else if (is_gimple_call (stmt))
3809 tree fndecl = gimple_call_fndecl (stmt);
3811 if ((fndecl
3812 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
3813 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
3814 && gimple_call_num_args (stmt) == 2)
3815 || (gimple_call_internal_p (stmt)
3816 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
3818 var = gimple_call_lhs (stmt);
3819 changed = true;
3820 if (var)
3822 ass_stmt
3823 = gimple_build_assign (var, gimple_call_arg (stmt, 0));
3824 gsi_replace (&bi, ass_stmt, true);
3826 else
3828 gsi_remove (&bi, true);
3829 continue;
3833 gsi_next (&bi);
3836 return changed ? TODO_cleanup_cfg : 0;
3839 } // anon namespace
3841 gimple_opt_pass *
3842 make_pass_strip_predict_hints (gcc::context *ctxt)
3844 return new pass_strip_predict_hints (ctxt);
3847 /* Rebuild function frequencies. Passes are in general expected to
3848 maintain profile by hand, however in some cases this is not possible:
3849 for example when inlining several functions with loops freuqencies might run
3850 out of scale and thus needs to be recomputed. */
3852 void
3853 rebuild_frequencies (void)
3855 timevar_push (TV_REBUILD_FREQUENCIES);
3857 /* When the max bb count in the function is small, there is a higher
3858 chance that there were truncation errors in the integer scaling
3859 of counts by inlining and other optimizations. This could lead
3860 to incorrect classification of code as being cold when it isn't.
3861 In that case, force the estimation of bb counts/frequencies from the
3862 branch probabilities, rather than computing frequencies from counts,
3863 which may also lead to frequencies incorrectly reduced to 0. There
3864 is less precision in the probabilities, so we only do this for small
3865 max counts. */
3866 profile_count count_max = profile_count::zero ();
3867 basic_block bb;
3868 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3869 if (bb->count > count_max)
3870 count_max = bb->count;
3872 if (profile_status_for_fn (cfun) == PROFILE_GUESSED
3873 || (!flag_auto_profile && profile_status_for_fn (cfun) == PROFILE_READ
3874 && count_max < REG_BR_PROB_BASE / 10))
3876 loop_optimizer_init (0);
3877 add_noreturn_fake_exit_edges ();
3878 mark_irreducible_loops ();
3879 connect_infinite_loops_to_exit ();
3880 estimate_bb_frequencies (true);
3881 remove_fake_exit_edges ();
3882 loop_optimizer_finalize ();
3884 else if (profile_status_for_fn (cfun) == PROFILE_READ)
3885 counts_to_freqs ();
3886 else
3887 gcc_unreachable ();
3888 timevar_pop (TV_REBUILD_FREQUENCIES);
3891 /* Perform a dry run of the branch prediction pass and report comparsion of
3892 the predicted and real profile into the dump file. */
3894 void
3895 report_predictor_hitrates (void)
3897 unsigned nb_loops;
3899 loop_optimizer_init (LOOPS_NORMAL);
3900 if (dump_file && (dump_flags & TDF_DETAILS))
3901 flow_loops_dump (dump_file, NULL, 0);
3903 mark_irreducible_loops ();
3905 nb_loops = number_of_loops (cfun);
3906 if (nb_loops > 1)
3907 scev_initialize ();
3909 tree_estimate_probability (true);
3911 if (nb_loops > 1)
3912 scev_finalize ();
3914 loop_optimizer_finalize ();
3917 /* Force edge E to be cold.
3918 If IMPOSSIBLE is true, for edge to have count and probability 0 otherwise
3919 keep low probability to represent possible error in a guess. This is used
3920 i.e. in case we predict loop to likely iterate given number of times but
3921 we are not 100% sure.
3923 This function locally updates profile without attempt to keep global
3924 consistency which can not be reached in full generality without full profile
3925 rebuild from probabilities alone. Doing so is not necessarily a good idea
3926 because frequencies and counts may be more realistic then probabilities.
3928 In some cases (such as for elimination of early exits during full loop
3929 unrolling) the caller can ensure that profile will get consistent
3930 afterwards. */
3932 void
3933 force_edge_cold (edge e, bool impossible)
3935 profile_count count_sum = profile_count::zero ();
3936 int prob_sum = 0;
3937 edge_iterator ei;
3938 edge e2;
3939 profile_count old_count = e->count;
3940 int old_probability = e->probability;
3941 int prob_scale = REG_BR_PROB_BASE;
3943 /* If edge is already improbably or cold, just return. */
3944 if (e->probability <= (impossible ? PROB_VERY_UNLIKELY : 0)
3945 && (!impossible || e->count == profile_count::zero ()))
3946 return;
3947 FOR_EACH_EDGE (e2, ei, e->src->succs)
3948 if (e2 != e)
3950 if (e2->count.initialized_p ())
3951 count_sum += e2->count;
3952 prob_sum += e2->probability;
3955 /* If there are other edges out of e->src, redistribute probabilitity
3956 there. */
3957 if (prob_sum)
3959 e->probability
3960 = MIN (e->probability, impossible ? 0 : PROB_VERY_UNLIKELY);
3961 if (impossible)
3962 e->count = profile_count::zero ();
3963 if (old_probability)
3964 e->count = e->count.apply_scale (e->probability, old_probability);
3965 else
3966 e->count = e->count.apply_scale (1, REG_BR_PROB_BASE);
3968 prob_scale = RDIV ((REG_BR_PROB_BASE - e->probability) * REG_BR_PROB_BASE,
3969 prob_sum);
3970 if (dump_file && (dump_flags & TDF_DETAILS))
3971 fprintf (dump_file, "Making edge %i->%i %s by redistributing "
3972 "probability to other edges.\n",
3973 e->src->index, e->dest->index,
3974 impossible ? "impossible" : "cold");
3975 profile_count count_sum2 = count_sum + old_count - e->count;
3976 FOR_EACH_EDGE (e2, ei, e->src->succs)
3977 if (e2 != e)
3979 if (count_sum > 0)
3980 e2->count.apply_scale (count_sum2, count_sum);
3981 e2->probability = RDIV (e2->probability * prob_scale,
3982 REG_BR_PROB_BASE);
3985 /* If all edges out of e->src are unlikely, the basic block itself
3986 is unlikely. */
3987 else
3989 e->probability = REG_BR_PROB_BASE;
3991 /* If we did not adjusting, the source basic block has no likely edeges
3992 leaving other direction. In that case force that bb cold, too.
3993 This in general is difficult task to do, but handle special case when
3994 BB has only one predecestor. This is common case when we are updating
3995 after loop transforms. */
3996 if (!prob_sum && count_sum == profile_count::zero ()
3997 && single_pred_p (e->src) && e->src->frequency > (impossible ? 0 : 1))
3999 int old_frequency = e->src->frequency;
4000 if (dump_file && (dump_flags & TDF_DETAILS))
4001 fprintf (dump_file, "Making bb %i %s.\n", e->src->index,
4002 impossible ? "impossible" : "cold");
4003 e->src->frequency = MIN (e->src->frequency, impossible ? 0 : 1);
4004 if (impossible)
4005 e->src->count = e->count = profile_count::zero ();
4006 else
4007 e->src->count = e->count = e->count.apply_scale (e->src->frequency,
4008 old_frequency);
4009 force_edge_cold (single_pred_edge (e->src), impossible);
4011 else if (dump_file && (dump_flags & TDF_DETAILS)
4012 && maybe_hot_bb_p (cfun, e->src))
4013 fprintf (dump_file, "Giving up on making bb %i %s.\n", e->src->index,
4014 impossible ? "impossible" : "cold");