1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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
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/>. */
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. */
32 #include "coretypes.h"
38 #include "tree-pass.h"
43 #include "diagnostic-core.h"
44 #include "gimple-predict.h"
45 #include "fold-const.h"
52 #include "gimple-iterator.h"
54 #include "tree-ssa-loop-niter.h"
55 #include "tree-ssa-loop.h"
56 #include "tree-scalar-evolution.h"
58 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
59 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */
60 static sreal real_almost_one
, real_br_prob_base
,
61 real_inv_br_prob_base
, real_one_half
, real_bb_freq_max
;
63 static void combine_predictions_for_insn (rtx_insn
*, basic_block
);
64 static void dump_prediction (FILE *, enum br_predictor
, int, basic_block
, int);
65 static void predict_paths_leading_to (basic_block
, enum br_predictor
, enum prediction
);
66 static void predict_paths_leading_to_edge (edge
, enum br_predictor
, enum prediction
);
67 static bool can_predict_insn_p (const rtx_insn
*);
69 /* Information we hold about each branch predictor.
70 Filled using information from predict.def. */
74 const char *const name
; /* Name used in the debugging dumps. */
75 const int hitrate
; /* Expected hitrate used by
76 predict_insn_def call. */
80 /* Use given predictor without Dempster-Shaffer theory if it matches
81 using first_match heuristics. */
82 #define PRED_FLAG_FIRST_MATCH 1
84 /* Recompute hitrate in percent to our representation. */
86 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
88 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
89 static const struct predictor_info predictor_info
[]= {
90 #include "predict.def"
92 /* Upper bound on predictors. */
97 /* Return TRUE if frequency FREQ is considered to be hot. */
100 maybe_hot_frequency_p (struct function
*fun
, int freq
)
102 struct cgraph_node
*node
= cgraph_node::get (fun
->decl
);
104 || !opt_for_fn (fun
->decl
, flag_branch_probabilities
))
106 if (node
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
)
108 if (node
->frequency
== NODE_FREQUENCY_HOT
)
111 if (profile_status_for_fn (fun
) == PROFILE_ABSENT
)
113 if (node
->frequency
== NODE_FREQUENCY_EXECUTED_ONCE
114 && freq
< (ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
* 2 / 3))
116 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
) == 0)
118 if (freq
* PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
)
119 < ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
)
124 static gcov_type min_count
= -1;
126 /* Determine the threshold for hot BB counts. */
129 get_hot_bb_threshold ()
131 gcov_working_set_t
*ws
;
134 ws
= find_working_set (PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE
));
136 min_count
= ws
->min_counter
;
141 /* Set the threshold for hot BB counts. */
144 set_hot_bb_threshold (gcov_type min
)
149 /* Return TRUE if frequency FREQ is considered to be hot. */
152 maybe_hot_count_p (struct function
*fun
, gcov_type count
)
154 if (fun
&& profile_status_for_fn (fun
) != PROFILE_READ
)
156 /* Code executed at most once is not hot. */
157 if (profile_info
->runs
>= count
)
159 return (count
>= get_hot_bb_threshold ());
162 /* Return true in case BB can be CPU intensive and should be optimized
163 for maximal performance. */
166 maybe_hot_bb_p (struct function
*fun
, const_basic_block bb
)
168 gcc_checking_assert (fun
);
169 if (profile_status_for_fn (fun
) == PROFILE_READ
)
170 return maybe_hot_count_p (fun
, bb
->count
);
171 return maybe_hot_frequency_p (fun
, bb
->frequency
);
174 /* Return true in case BB can be CPU intensive and should be optimized
175 for maximal performance. */
178 maybe_hot_edge_p (edge e
)
180 if (profile_status_for_fn (cfun
) == PROFILE_READ
)
181 return maybe_hot_count_p (cfun
, e
->count
);
182 return maybe_hot_frequency_p (cfun
, EDGE_FREQUENCY (e
));
185 /* Return true if profile COUNT and FREQUENCY, or function FUN static
186 node frequency reflects never being executed. */
189 probably_never_executed (struct function
*fun
,
190 gcov_type count
, int frequency
)
192 gcc_checking_assert (fun
);
193 if (profile_status_for_fn (fun
) == PROFILE_READ
)
195 int unlikely_count_fraction
= PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION
);
196 if (count
* unlikely_count_fraction
>= profile_info
->runs
)
200 if (!ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
)
202 if (ENTRY_BLOCK_PTR_FOR_FN (fun
)->count
)
204 gcov_type computed_count
;
205 /* Check for possibility of overflow, in which case entry bb count
206 is large enough to do the division first without losing much
208 if (ENTRY_BLOCK_PTR_FOR_FN (fun
)->count
< REG_BR_PROB_BASE
*
211 gcov_type scaled_count
212 = frequency
* ENTRY_BLOCK_PTR_FOR_FN (fun
)->count
*
213 unlikely_count_fraction
;
214 computed_count
= RDIV (scaled_count
,
215 ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
);
219 computed_count
= RDIV (ENTRY_BLOCK_PTR_FOR_FN (fun
)->count
,
220 ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
);
221 computed_count
*= frequency
* unlikely_count_fraction
;
223 if (computed_count
>= profile_info
->runs
)
228 if ((!profile_info
|| !(opt_for_fn (fun
->decl
, flag_branch_probabilities
)))
229 && (cgraph_node::get (fun
->decl
)->frequency
230 == NODE_FREQUENCY_UNLIKELY_EXECUTED
))
236 /* Return true in case BB is probably never executed. */
239 probably_never_executed_bb_p (struct function
*fun
, const_basic_block bb
)
241 return probably_never_executed (fun
, bb
->count
, bb
->frequency
);
245 /* Return true in case edge E is probably never executed. */
248 probably_never_executed_edge_p (struct function
*fun
, edge e
)
250 return probably_never_executed (fun
, e
->count
, EDGE_FREQUENCY (e
));
253 /* Return true when current function should always be optimized for size. */
256 optimize_function_for_size_p (struct function
*fun
)
258 if (!fun
|| !fun
->decl
)
259 return optimize_size
;
260 cgraph_node
*n
= cgraph_node::get (fun
->decl
);
261 return n
&& n
->optimize_for_size_p ();
264 /* Return true when current function should always be optimized for speed. */
267 optimize_function_for_speed_p (struct function
*fun
)
269 return !optimize_function_for_size_p (fun
);
272 /* Return the optimization type that should be used for the function FUN. */
275 function_optimization_type (struct function
*fun
)
277 return (optimize_function_for_speed_p (fun
)
279 : OPTIMIZE_FOR_SIZE
);
282 /* Return TRUE when BB should be optimized for size. */
285 optimize_bb_for_size_p (const_basic_block bb
)
287 return (optimize_function_for_size_p (cfun
)
288 || (bb
&& !maybe_hot_bb_p (cfun
, bb
)));
291 /* Return TRUE when BB should be optimized for speed. */
294 optimize_bb_for_speed_p (const_basic_block bb
)
296 return !optimize_bb_for_size_p (bb
);
299 /* Return the optimization type that should be used for block BB. */
302 bb_optimization_type (const_basic_block bb
)
304 return (optimize_bb_for_speed_p (bb
)
306 : OPTIMIZE_FOR_SIZE
);
309 /* Return TRUE when BB should be optimized for size. */
312 optimize_edge_for_size_p (edge e
)
314 return optimize_function_for_size_p (cfun
) || !maybe_hot_edge_p (e
);
317 /* Return TRUE when BB should be optimized for speed. */
320 optimize_edge_for_speed_p (edge e
)
322 return !optimize_edge_for_size_p (e
);
325 /* Return TRUE when BB should be optimized for size. */
328 optimize_insn_for_size_p (void)
330 return optimize_function_for_size_p (cfun
) || !crtl
->maybe_hot_insn_p
;
333 /* Return TRUE when BB should be optimized for speed. */
336 optimize_insn_for_speed_p (void)
338 return !optimize_insn_for_size_p ();
341 /* Return TRUE when LOOP should be optimized for size. */
344 optimize_loop_for_size_p (struct loop
*loop
)
346 return optimize_bb_for_size_p (loop
->header
);
349 /* Return TRUE when LOOP should be optimized for speed. */
352 optimize_loop_for_speed_p (struct loop
*loop
)
354 return optimize_bb_for_speed_p (loop
->header
);
357 /* Return TRUE when LOOP nest should be optimized for speed. */
360 optimize_loop_nest_for_speed_p (struct loop
*loop
)
362 struct loop
*l
= loop
;
363 if (optimize_loop_for_speed_p (loop
))
366 while (l
&& l
!= loop
)
368 if (optimize_loop_for_speed_p (l
))
376 while (l
!= loop
&& !l
->next
)
385 /* Return TRUE when LOOP nest should be optimized for size. */
388 optimize_loop_nest_for_size_p (struct loop
*loop
)
390 return !optimize_loop_nest_for_speed_p (loop
);
393 /* Return true when edge E is likely to be well predictable by branch
397 predictable_edge_p (edge e
)
399 if (profile_status_for_fn (cfun
) == PROFILE_ABSENT
)
402 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME
) * REG_BR_PROB_BASE
/ 100)
403 || (REG_BR_PROB_BASE
- e
->probability
404 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME
) * REG_BR_PROB_BASE
/ 100))
410 /* Set RTL expansion for BB profile. */
413 rtl_profile_for_bb (basic_block bb
)
415 crtl
->maybe_hot_insn_p
= maybe_hot_bb_p (cfun
, bb
);
418 /* Set RTL expansion for edge profile. */
421 rtl_profile_for_edge (edge e
)
423 crtl
->maybe_hot_insn_p
= maybe_hot_edge_p (e
);
426 /* Set RTL expansion to default mode (i.e. when profile info is not known). */
428 default_rtl_profile (void)
430 crtl
->maybe_hot_insn_p
= true;
433 /* Return true if the one of outgoing edges is already predicted by
437 rtl_predicted_by_p (const_basic_block bb
, enum br_predictor predictor
)
440 if (!INSN_P (BB_END (bb
)))
442 for (note
= REG_NOTES (BB_END (bb
)); note
; note
= XEXP (note
, 1))
443 if (REG_NOTE_KIND (note
) == REG_BR_PRED
444 && INTVAL (XEXP (XEXP (note
, 0), 0)) == (int)predictor
)
449 /* Structure representing predictions in tree level. */
451 struct edge_prediction
{
452 struct edge_prediction
*ep_next
;
454 enum br_predictor ep_predictor
;
458 /* This map contains for a basic block the list of predictions for the
461 static hash_map
<const_basic_block
, edge_prediction
*> *bb_predictions
;
463 /* Return true if the one of outgoing edges is already predicted by
467 gimple_predicted_by_p (const_basic_block bb
, enum br_predictor predictor
)
469 struct edge_prediction
*i
;
470 edge_prediction
**preds
= bb_predictions
->get (bb
);
475 for (i
= *preds
; i
; i
= i
->ep_next
)
476 if (i
->ep_predictor
== predictor
)
481 /* Return true if the one of outgoing edges is already predicted by
482 PREDICTOR for edge E predicted as TAKEN. */
485 edge_predicted_by_p (edge e
, enum br_predictor predictor
, bool taken
)
487 struct edge_prediction
*i
;
488 basic_block bb
= e
->src
;
489 edge_prediction
**preds
= bb_predictions
->get (bb
);
493 int probability
= predictor_info
[(int) predictor
].hitrate
;
496 probability
= REG_BR_PROB_BASE
- probability
;
498 for (i
= *preds
; i
; i
= i
->ep_next
)
499 if (i
->ep_predictor
== predictor
501 && i
->ep_probability
== probability
)
506 /* Return true when the probability of edge is reliable.
508 The profile guessing code is good at predicting branch outcome (ie.
509 taken/not taken), that is predicted right slightly over 75% of time.
510 It is however notoriously poor on predicting the probability itself.
511 In general the profile appear a lot flatter (with probabilities closer
512 to 50%) than the reality so it is bad idea to use it to drive optimization
513 such as those disabling dynamic branch prediction for well predictable
516 There are two exceptions - edges leading to noreturn edges and edges
517 predicted by number of iterations heuristics are predicted well. This macro
518 should be able to distinguish those, but at the moment it simply check for
519 noreturn heuristic that is only one giving probability over 99% or bellow
520 1%. In future we might want to propagate reliability information across the
521 CFG if we find this information useful on multiple places. */
523 probability_reliable_p (int prob
)
525 return (profile_status_for_fn (cfun
) == PROFILE_READ
526 || (profile_status_for_fn (cfun
) == PROFILE_GUESSED
527 && (prob
<= HITRATE (1) || prob
>= HITRATE (99))));
530 /* Same predicate as above, working on edges. */
532 edge_probability_reliable_p (const_edge e
)
534 return probability_reliable_p (e
->probability
);
537 /* Same predicate as edge_probability_reliable_p, working on notes. */
539 br_prob_note_reliable_p (const_rtx note
)
541 gcc_assert (REG_NOTE_KIND (note
) == REG_BR_PROB
);
542 return probability_reliable_p (XINT (note
, 0));
546 predict_insn (rtx_insn
*insn
, enum br_predictor predictor
, int probability
)
548 gcc_assert (any_condjump_p (insn
));
549 if (!flag_guess_branch_prob
)
552 add_reg_note (insn
, REG_BR_PRED
,
553 gen_rtx_CONCAT (VOIDmode
,
554 GEN_INT ((int) predictor
),
555 GEN_INT ((int) probability
)));
558 /* Predict insn by given predictor. */
561 predict_insn_def (rtx_insn
*insn
, enum br_predictor predictor
,
562 enum prediction taken
)
564 int probability
= predictor_info
[(int) predictor
].hitrate
;
567 probability
= REG_BR_PROB_BASE
- probability
;
569 predict_insn (insn
, predictor
, probability
);
572 /* Predict edge E with given probability if possible. */
575 rtl_predict_edge (edge e
, enum br_predictor predictor
, int probability
)
578 last_insn
= BB_END (e
->src
);
580 /* We can store the branch prediction information only about
581 conditional jumps. */
582 if (!any_condjump_p (last_insn
))
585 /* We always store probability of branching. */
586 if (e
->flags
& EDGE_FALLTHRU
)
587 probability
= REG_BR_PROB_BASE
- probability
;
589 predict_insn (last_insn
, predictor
, probability
);
592 /* Predict edge E with the given PROBABILITY. */
594 gimple_predict_edge (edge e
, enum br_predictor predictor
, int probability
)
596 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
597 && EDGE_COUNT (e
->src
->succs
) > 1
598 && flag_guess_branch_prob
601 struct edge_prediction
*i
= XNEW (struct edge_prediction
);
602 edge_prediction
*&preds
= bb_predictions
->get_or_insert (e
->src
);
606 i
->ep_probability
= probability
;
607 i
->ep_predictor
= predictor
;
612 /* Filter edge predictions PREDS by a function FILTER. DATA are passed
613 to the filter function. */
616 filter_predictions (edge_prediction
**preds
,
617 bool (*filter
) (edge_prediction
*, void *), void *data
)
624 struct edge_prediction
**prediction
= preds
;
625 struct edge_prediction
*next
;
629 if ((*filter
) (*prediction
, data
))
630 prediction
= &((*prediction
)->ep_next
);
633 next
= (*prediction
)->ep_next
;
641 /* Filter function predicate that returns true for a edge predicate P
642 if its edge is equal to DATA. */
645 equal_edge_p (edge_prediction
*p
, void *data
)
647 return p
->ep_edge
== (edge
)data
;
650 /* Remove all predictions on given basic block that are attached
653 remove_predictions_associated_with_edge (edge e
)
658 edge_prediction
**preds
= bb_predictions
->get (e
->src
);
659 filter_predictions (preds
, equal_edge_p
, e
);
662 /* Clears the list of predictions stored for BB. */
665 clear_bb_predictions (basic_block bb
)
667 edge_prediction
**preds
= bb_predictions
->get (bb
);
668 struct edge_prediction
*pred
, *next
;
673 for (pred
= *preds
; pred
; pred
= next
)
675 next
= pred
->ep_next
;
681 /* Return true when we can store prediction on insn INSN.
682 At the moment we represent predictions only on conditional
683 jumps, not at computed jump or other complicated cases. */
685 can_predict_insn_p (const rtx_insn
*insn
)
687 return (JUMP_P (insn
)
688 && any_condjump_p (insn
)
689 && EDGE_COUNT (BLOCK_FOR_INSN (insn
)->succs
) >= 2);
692 /* Predict edge E by given predictor if possible. */
695 predict_edge_def (edge e
, enum br_predictor predictor
,
696 enum prediction taken
)
698 int probability
= predictor_info
[(int) predictor
].hitrate
;
701 probability
= REG_BR_PROB_BASE
- probability
;
703 predict_edge (e
, predictor
, probability
);
706 /* Invert all branch predictions or probability notes in the INSN. This needs
707 to be done each time we invert the condition used by the jump. */
710 invert_br_probabilities (rtx insn
)
714 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
715 if (REG_NOTE_KIND (note
) == REG_BR_PROB
)
716 XINT (note
, 0) = REG_BR_PROB_BASE
- XINT (note
, 0);
717 else if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
718 XEXP (XEXP (note
, 0), 1)
719 = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (XEXP (note
, 0), 1)));
722 /* Dump information about the branch prediction to the output file. */
725 dump_prediction (FILE *file
, enum br_predictor predictor
, int probability
,
726 basic_block bb
, int used
)
734 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
735 if (! (e
->flags
& EDGE_FALLTHRU
))
738 fprintf (file
, " %s heuristics%s: %.1f%%",
739 predictor_info
[predictor
].name
,
740 used
? "" : " (ignored)", probability
* 100.0 / REG_BR_PROB_BASE
);
744 fprintf (file
, " exec %" PRId64
, bb
->count
);
747 fprintf (file
, " hit %" PRId64
, e
->count
);
748 fprintf (file
, " (%.1f%%)", e
->count
* 100.0 / bb
->count
);
752 fprintf (file
, "\n");
755 /* We can not predict the probabilities of outgoing edges of bb. Set them
756 evenly and hope for the best. */
758 set_even_probabilities (basic_block bb
)
764 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
765 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
767 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
768 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
769 e
->probability
= (REG_BR_PROB_BASE
+ nedges
/ 2) / nedges
;
774 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
775 note if not already present. Remove now useless REG_BR_PRED notes. */
778 combine_predictions_for_insn (rtx_insn
*insn
, basic_block bb
)
783 int best_probability
= PROB_EVEN
;
784 enum br_predictor best_predictor
= END_PREDICTORS
;
785 int combined_probability
= REG_BR_PROB_BASE
/ 2;
787 bool first_match
= false;
790 if (!can_predict_insn_p (insn
))
792 set_even_probabilities (bb
);
796 prob_note
= find_reg_note (insn
, REG_BR_PROB
, 0);
797 pnote
= ®_NOTES (insn
);
799 fprintf (dump_file
, "Predictions for insn %i bb %i\n", INSN_UID (insn
),
802 /* We implement "first match" heuristics and use probability guessed
803 by predictor with smallest index. */
804 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
805 if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
807 enum br_predictor predictor
= ((enum br_predictor
)
808 INTVAL (XEXP (XEXP (note
, 0), 0)));
809 int probability
= INTVAL (XEXP (XEXP (note
, 0), 1));
812 if (best_predictor
> predictor
)
813 best_probability
= probability
, best_predictor
= predictor
;
815 d
= (combined_probability
* probability
816 + (REG_BR_PROB_BASE
- combined_probability
)
817 * (REG_BR_PROB_BASE
- probability
));
819 /* Use FP math to avoid overflows of 32bit integers. */
821 /* If one probability is 0% and one 100%, avoid division by zero. */
822 combined_probability
= REG_BR_PROB_BASE
/ 2;
824 combined_probability
= (((double) combined_probability
) * probability
825 * REG_BR_PROB_BASE
/ d
+ 0.5);
828 /* Decide which heuristic to use. In case we didn't match anything,
829 use no_prediction heuristic, in case we did match, use either
830 first match or Dempster-Shaffer theory depending on the flags. */
832 if (predictor_info
[best_predictor
].flags
& PRED_FLAG_FIRST_MATCH
)
836 dump_prediction (dump_file
, PRED_NO_PREDICTION
,
837 combined_probability
, bb
, true);
840 dump_prediction (dump_file
, PRED_DS_THEORY
, combined_probability
,
842 dump_prediction (dump_file
, PRED_FIRST_MATCH
, best_probability
,
847 combined_probability
= best_probability
;
848 dump_prediction (dump_file
, PRED_COMBINED
, combined_probability
, bb
, true);
852 if (REG_NOTE_KIND (*pnote
) == REG_BR_PRED
)
854 enum br_predictor predictor
= ((enum br_predictor
)
855 INTVAL (XEXP (XEXP (*pnote
, 0), 0)));
856 int probability
= INTVAL (XEXP (XEXP (*pnote
, 0), 1));
858 dump_prediction (dump_file
, predictor
, probability
, bb
,
859 !first_match
|| best_predictor
== predictor
);
860 *pnote
= XEXP (*pnote
, 1);
863 pnote
= &XEXP (*pnote
, 1);
868 add_int_reg_note (insn
, REG_BR_PROB
, combined_probability
);
870 /* Save the prediction into CFG in case we are seeing non-degenerated
872 if (!single_succ_p (bb
))
874 BRANCH_EDGE (bb
)->probability
= combined_probability
;
875 FALLTHRU_EDGE (bb
)->probability
876 = REG_BR_PROB_BASE
- combined_probability
;
879 else if (!single_succ_p (bb
))
881 int prob
= XINT (prob_note
, 0);
883 BRANCH_EDGE (bb
)->probability
= prob
;
884 FALLTHRU_EDGE (bb
)->probability
= REG_BR_PROB_BASE
- prob
;
887 single_succ_edge (bb
)->probability
= REG_BR_PROB_BASE
;
890 /* Combine predictions into single probability and store them into CFG.
891 Remove now useless prediction entries.
892 If DRY_RUN is set, only produce dumps and do not modify profile. */
895 combine_predictions_for_bb (basic_block bb
, bool dry_run
)
897 int best_probability
= PROB_EVEN
;
898 enum br_predictor best_predictor
= END_PREDICTORS
;
899 int combined_probability
= REG_BR_PROB_BASE
/ 2;
901 bool first_match
= false;
903 struct edge_prediction
*pred
;
905 edge e
, first
= NULL
, second
= NULL
;
908 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
909 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
912 if (first
&& !second
)
918 /* When there is no successor or only one choice, prediction is easy.
920 We are lazy for now and predict only basic blocks with two outgoing
921 edges. It is possible to predict generic case too, but we have to
922 ignore first match heuristics and do more involved combining. Implement
926 if (!bb
->count
&& !dry_run
)
927 set_even_probabilities (bb
);
928 clear_bb_predictions (bb
);
930 fprintf (dump_file
, "%i edges in bb %i predicted to even probabilities\n",
936 fprintf (dump_file
, "Predictions for bb %i\n", bb
->index
);
938 edge_prediction
**preds
= bb_predictions
->get (bb
);
941 /* We implement "first match" heuristics and use probability guessed
942 by predictor with smallest index. */
943 for (pred
= *preds
; pred
; pred
= pred
->ep_next
)
945 enum br_predictor predictor
= pred
->ep_predictor
;
946 int probability
= pred
->ep_probability
;
948 if (pred
->ep_edge
!= first
)
949 probability
= REG_BR_PROB_BASE
- probability
;
952 /* First match heuristics would be widly confused if we predicted
954 if (best_predictor
> predictor
)
956 struct edge_prediction
*pred2
;
957 int prob
= probability
;
959 for (pred2
= (struct edge_prediction
*) *preds
;
960 pred2
; pred2
= pred2
->ep_next
)
961 if (pred2
!= pred
&& pred2
->ep_predictor
== pred
->ep_predictor
)
963 int probability2
= pred2
->ep_probability
;
965 if (pred2
->ep_edge
!= first
)
966 probability2
= REG_BR_PROB_BASE
- probability2
;
968 if ((probability
< REG_BR_PROB_BASE
/ 2) !=
969 (probability2
< REG_BR_PROB_BASE
/ 2))
972 /* If the same predictor later gave better result, go for it! */
973 if ((probability
>= REG_BR_PROB_BASE
/ 2 && (probability2
> probability
))
974 || (probability
<= REG_BR_PROB_BASE
/ 2 && (probability2
< probability
)))
978 best_probability
= prob
, best_predictor
= predictor
;
981 d
= (combined_probability
* probability
982 + (REG_BR_PROB_BASE
- combined_probability
)
983 * (REG_BR_PROB_BASE
- probability
));
985 /* Use FP math to avoid overflows of 32bit integers. */
987 /* If one probability is 0% and one 100%, avoid division by zero. */
988 combined_probability
= REG_BR_PROB_BASE
/ 2;
990 combined_probability
= (((double) combined_probability
)
992 * REG_BR_PROB_BASE
/ d
+ 0.5);
996 /* Decide which heuristic to use. In case we didn't match anything,
997 use no_prediction heuristic, in case we did match, use either
998 first match or Dempster-Shaffer theory depending on the flags. */
1000 if (predictor_info
[best_predictor
].flags
& PRED_FLAG_FIRST_MATCH
)
1004 dump_prediction (dump_file
, PRED_NO_PREDICTION
, combined_probability
, bb
, true);
1007 dump_prediction (dump_file
, PRED_DS_THEORY
, combined_probability
, bb
,
1009 dump_prediction (dump_file
, PRED_FIRST_MATCH
, best_probability
, bb
,
1014 combined_probability
= best_probability
;
1015 dump_prediction (dump_file
, PRED_COMBINED
, combined_probability
, bb
, true);
1019 for (pred
= (struct edge_prediction
*) *preds
; pred
; pred
= pred
->ep_next
)
1021 enum br_predictor predictor
= pred
->ep_predictor
;
1022 int probability
= pred
->ep_probability
;
1024 if (pred
->ep_edge
!= EDGE_SUCC (bb
, 0))
1025 probability
= REG_BR_PROB_BASE
- probability
;
1026 dump_prediction (dump_file
, predictor
, probability
, bb
,
1027 !first_match
|| best_predictor
== predictor
);
1030 clear_bb_predictions (bb
);
1032 if (!bb
->count
&& !dry_run
)
1034 first
->probability
= combined_probability
;
1035 second
->probability
= REG_BR_PROB_BASE
- combined_probability
;
1039 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1040 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1042 T1 and T2 should be one of the following cases:
1043 1. T1 is SSA_NAME, T2 is NULL
1044 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1045 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1048 strips_small_constant (tree t1
, tree t2
)
1055 else if (TREE_CODE (t1
) == SSA_NAME
)
1057 else if (tree_fits_shwi_p (t1
))
1058 value
= tree_to_shwi (t1
);
1064 else if (tree_fits_shwi_p (t2
))
1065 value
= tree_to_shwi (t2
);
1066 else if (TREE_CODE (t2
) == SSA_NAME
)
1074 if (value
<= 4 && value
>= -4)
1080 /* Return the SSA_NAME in T or T's operands.
1081 Return NULL if SSA_NAME cannot be found. */
1084 get_base_value (tree t
)
1086 if (TREE_CODE (t
) == SSA_NAME
)
1089 if (!BINARY_CLASS_P (t
))
1092 switch (TREE_OPERAND_LENGTH (t
))
1095 return strips_small_constant (TREE_OPERAND (t
, 0), NULL
);
1097 return strips_small_constant (TREE_OPERAND (t
, 0),
1098 TREE_OPERAND (t
, 1));
1104 /* Check the compare STMT in LOOP. If it compares an induction
1105 variable to a loop invariant, return true, and save
1106 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1107 Otherwise return false and set LOOP_INVAIANT to NULL. */
1110 is_comparison_with_loop_invariant_p (gcond
*stmt
, struct loop
*loop
,
1111 tree
*loop_invariant
,
1112 enum tree_code
*compare_code
,
1116 tree op0
, op1
, bound
, base
;
1118 enum tree_code code
;
1121 code
= gimple_cond_code (stmt
);
1122 *loop_invariant
= NULL
;
1138 op0
= gimple_cond_lhs (stmt
);
1139 op1
= gimple_cond_rhs (stmt
);
1141 if ((TREE_CODE (op0
) != SSA_NAME
&& TREE_CODE (op0
) != INTEGER_CST
)
1142 || (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op1
) != INTEGER_CST
))
1144 if (!simple_iv (loop
, loop_containing_stmt (stmt
), op0
, &iv0
, true))
1146 if (!simple_iv (loop
, loop_containing_stmt (stmt
), op1
, &iv1
, true))
1148 if (TREE_CODE (iv0
.step
) != INTEGER_CST
1149 || TREE_CODE (iv1
.step
) != INTEGER_CST
)
1151 if ((integer_zerop (iv0
.step
) && integer_zerop (iv1
.step
))
1152 || (!integer_zerop (iv0
.step
) && !integer_zerop (iv1
.step
)))
1155 if (integer_zerop (iv0
.step
))
1157 if (code
!= NE_EXPR
&& code
!= EQ_EXPR
)
1158 code
= invert_tree_comparison (code
, false);
1161 if (tree_fits_shwi_p (iv1
.step
))
1170 if (tree_fits_shwi_p (iv0
.step
))
1176 if (TREE_CODE (bound
) != INTEGER_CST
)
1177 bound
= get_base_value (bound
);
1180 if (TREE_CODE (base
) != INTEGER_CST
)
1181 base
= get_base_value (base
);
1185 *loop_invariant
= bound
;
1186 *compare_code
= code
;
1188 *loop_iv_base
= base
;
1192 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1195 expr_coherent_p (tree t1
, tree t2
)
1198 tree ssa_name_1
= NULL
;
1199 tree ssa_name_2
= NULL
;
1201 gcc_assert (TREE_CODE (t1
) == SSA_NAME
|| TREE_CODE (t1
) == INTEGER_CST
);
1202 gcc_assert (TREE_CODE (t2
) == SSA_NAME
|| TREE_CODE (t2
) == INTEGER_CST
);
1207 if (TREE_CODE (t1
) == INTEGER_CST
&& TREE_CODE (t2
) == INTEGER_CST
)
1209 if (TREE_CODE (t1
) == INTEGER_CST
|| TREE_CODE (t2
) == INTEGER_CST
)
1212 /* Check to see if t1 is expressed/defined with t2. */
1213 stmt
= SSA_NAME_DEF_STMT (t1
);
1214 gcc_assert (stmt
!= NULL
);
1215 if (is_gimple_assign (stmt
))
1217 ssa_name_1
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_USE
);
1218 if (ssa_name_1
&& ssa_name_1
== t2
)
1222 /* Check to see if t2 is expressed/defined with t1. */
1223 stmt
= SSA_NAME_DEF_STMT (t2
);
1224 gcc_assert (stmt
!= NULL
);
1225 if (is_gimple_assign (stmt
))
1227 ssa_name_2
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_USE
);
1228 if (ssa_name_2
&& ssa_name_2
== t1
)
1232 /* Compare if t1 and t2's def_stmts are identical. */
1233 if (ssa_name_2
!= NULL
&& ssa_name_1
== ssa_name_2
)
1239 /* Return true if E is predicted by one of loop heuristics. */
1242 predicted_by_loop_heuristics_p (basic_block bb
)
1244 struct edge_prediction
*i
;
1245 edge_prediction
**preds
= bb_predictions
->get (bb
);
1250 for (i
= *preds
; i
; i
= i
->ep_next
)
1251 if (i
->ep_predictor
== PRED_LOOP_ITERATIONS_GUESSED
1252 || i
->ep_predictor
== PRED_LOOP_ITERATIONS_MAX
1253 || i
->ep_predictor
== PRED_LOOP_ITERATIONS
1254 || i
->ep_predictor
== PRED_LOOP_EXIT
1255 || i
->ep_predictor
== PRED_LOOP_EXTRA_EXIT
)
1260 /* Predict branch probability of BB when BB contains a branch that compares
1261 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1262 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1265 for (int i = 0; i < bound; i++) {
1272 In this loop, we will predict the branch inside the loop to be taken. */
1275 predict_iv_comparison (struct loop
*loop
, basic_block bb
,
1276 tree loop_bound_var
,
1277 tree loop_iv_base_var
,
1278 enum tree_code loop_bound_code
,
1279 int loop_bound_step
)
1282 tree compare_var
, compare_base
;
1283 enum tree_code compare_code
;
1284 tree compare_step_var
;
1288 if (predicted_by_loop_heuristics_p (bb
))
1291 stmt
= last_stmt (bb
);
1292 if (!stmt
|| gimple_code (stmt
) != GIMPLE_COND
)
1294 if (!is_comparison_with_loop_invariant_p (as_a
<gcond
*> (stmt
),
1301 /* Find the taken edge. */
1302 FOR_EACH_EDGE (then_edge
, ei
, bb
->succs
)
1303 if (then_edge
->flags
& EDGE_TRUE_VALUE
)
1306 /* When comparing an IV to a loop invariant, NE is more likely to be
1307 taken while EQ is more likely to be not-taken. */
1308 if (compare_code
== NE_EXPR
)
1310 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1313 else if (compare_code
== EQ_EXPR
)
1315 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1319 if (!expr_coherent_p (loop_iv_base_var
, compare_base
))
1322 /* If loop bound, base and compare bound are all constants, we can
1323 calculate the probability directly. */
1324 if (tree_fits_shwi_p (loop_bound_var
)
1325 && tree_fits_shwi_p (compare_var
)
1326 && tree_fits_shwi_p (compare_base
))
1329 bool overflow
, overall_overflow
= false;
1330 widest_int compare_count
, tem
;
1332 /* (loop_bound - base) / compare_step */
1333 tem
= wi::sub (wi::to_widest (loop_bound_var
),
1334 wi::to_widest (compare_base
), SIGNED
, &overflow
);
1335 overall_overflow
|= overflow
;
1336 widest_int loop_count
= wi::div_trunc (tem
,
1337 wi::to_widest (compare_step_var
),
1339 overall_overflow
|= overflow
;
1341 if (!wi::neg_p (wi::to_widest (compare_step_var
))
1342 ^ (compare_code
== LT_EXPR
|| compare_code
== LE_EXPR
))
1344 /* (loop_bound - compare_bound) / compare_step */
1345 tem
= wi::sub (wi::to_widest (loop_bound_var
),
1346 wi::to_widest (compare_var
), SIGNED
, &overflow
);
1347 overall_overflow
|= overflow
;
1348 compare_count
= wi::div_trunc (tem
, wi::to_widest (compare_step_var
),
1350 overall_overflow
|= overflow
;
1354 /* (compare_bound - base) / compare_step */
1355 tem
= wi::sub (wi::to_widest (compare_var
),
1356 wi::to_widest (compare_base
), SIGNED
, &overflow
);
1357 overall_overflow
|= overflow
;
1358 compare_count
= wi::div_trunc (tem
, wi::to_widest (compare_step_var
),
1360 overall_overflow
|= overflow
;
1362 if (compare_code
== LE_EXPR
|| compare_code
== GE_EXPR
)
1364 if (loop_bound_code
== LE_EXPR
|| loop_bound_code
== GE_EXPR
)
1366 if (wi::neg_p (compare_count
))
1368 if (wi::neg_p (loop_count
))
1370 if (loop_count
== 0)
1372 else if (wi::cmps (compare_count
, loop_count
) == 1)
1373 probability
= REG_BR_PROB_BASE
;
1376 tem
= compare_count
* REG_BR_PROB_BASE
;
1377 tem
= wi::udiv_trunc (tem
, loop_count
);
1378 probability
= tem
.to_uhwi ();
1381 /* FIXME: The branch prediction seems broken. It has only 20% hitrate. */
1382 if (!overall_overflow
)
1383 predict_edge (then_edge
, PRED_LOOP_IV_COMPARE
, probability
);
1388 if (expr_coherent_p (loop_bound_var
, compare_var
))
1390 if ((loop_bound_code
== LT_EXPR
|| loop_bound_code
== LE_EXPR
)
1391 && (compare_code
== LT_EXPR
|| compare_code
== LE_EXPR
))
1392 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1393 else if ((loop_bound_code
== GT_EXPR
|| loop_bound_code
== GE_EXPR
)
1394 && (compare_code
== GT_EXPR
|| compare_code
== GE_EXPR
))
1395 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1396 else if (loop_bound_code
== NE_EXPR
)
1398 /* If the loop backedge condition is "(i != bound)", we do
1399 the comparison based on the step of IV:
1400 * step < 0 : backedge condition is like (i > bound)
1401 * step > 0 : backedge condition is like (i < bound) */
1402 gcc_assert (loop_bound_step
!= 0);
1403 if (loop_bound_step
> 0
1404 && (compare_code
== LT_EXPR
1405 || compare_code
== LE_EXPR
))
1406 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1407 else if (loop_bound_step
< 0
1408 && (compare_code
== GT_EXPR
1409 || compare_code
== GE_EXPR
))
1410 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1412 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1415 /* The branch is predicted not-taken if loop_bound_code is
1416 opposite with compare_code. */
1417 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1419 else if (expr_coherent_p (loop_iv_base_var
, compare_var
))
1422 for (i = s; i < h; i++)
1424 The branch should be predicted taken. */
1425 if (loop_bound_step
> 0
1426 && (compare_code
== GT_EXPR
|| compare_code
== GE_EXPR
))
1427 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1428 else if (loop_bound_step
< 0
1429 && (compare_code
== LT_EXPR
|| compare_code
== LE_EXPR
))
1430 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1432 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1436 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1437 exits are resulted from short-circuit conditions that will generate an
1440 if (foo() || global > 10)
1443 This will be translated into:
1448 if foo() goto BB6 else goto BB5
1450 if global > 10 goto BB6 else goto BB7
1454 iftmp = (PHI 0(BB5), 1(BB6))
1455 if iftmp == 1 goto BB8 else goto BB3
1457 outside of the loop...
1459 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1460 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1461 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1462 exits to predict them using PRED_LOOP_EXTRA_EXIT. */
1465 predict_extra_loop_exits (edge exit_edge
)
1468 bool check_value_one
;
1469 gimple
*lhs_def_stmt
;
1471 tree cmp_rhs
, cmp_lhs
;
1475 last
= last_stmt (exit_edge
->src
);
1478 cmp_stmt
= dyn_cast
<gcond
*> (last
);
1482 cmp_rhs
= gimple_cond_rhs (cmp_stmt
);
1483 cmp_lhs
= gimple_cond_lhs (cmp_stmt
);
1484 if (!TREE_CONSTANT (cmp_rhs
)
1485 || !(integer_zerop (cmp_rhs
) || integer_onep (cmp_rhs
)))
1487 if (TREE_CODE (cmp_lhs
) != SSA_NAME
)
1490 /* If check_value_one is true, only the phi_args with value '1' will lead
1491 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1493 check_value_one
= (((integer_onep (cmp_rhs
))
1494 ^ (gimple_cond_code (cmp_stmt
) == EQ_EXPR
))
1495 ^ ((exit_edge
->flags
& EDGE_TRUE_VALUE
) != 0));
1497 lhs_def_stmt
= SSA_NAME_DEF_STMT (cmp_lhs
);
1501 phi_stmt
= dyn_cast
<gphi
*> (lhs_def_stmt
);
1505 for (i
= 0; i
< gimple_phi_num_args (phi_stmt
); i
++)
1509 tree val
= gimple_phi_arg_def (phi_stmt
, i
);
1510 edge e
= gimple_phi_arg_edge (phi_stmt
, i
);
1512 if (!TREE_CONSTANT (val
) || !(integer_zerop (val
) || integer_onep (val
)))
1514 if ((check_value_one
^ integer_onep (val
)) == 1)
1516 if (EDGE_COUNT (e
->src
->succs
) != 1)
1518 predict_paths_leading_to_edge (e
, PRED_LOOP_EXTRA_EXIT
, NOT_TAKEN
);
1522 FOR_EACH_EDGE (e1
, ei
, e
->src
->preds
)
1523 predict_paths_leading_to_edge (e1
, PRED_LOOP_EXTRA_EXIT
, NOT_TAKEN
);
1528 /* Predict edge probabilities by exploiting loop structure. */
1531 predict_loops (void)
1535 /* Try to predict out blocks in a loop that are not part of a
1537 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
1539 basic_block bb
, *bbs
;
1540 unsigned j
, n_exits
= 0;
1542 struct tree_niter_desc niter_desc
;
1544 struct nb_iter_bound
*nb_iter
;
1545 enum tree_code loop_bound_code
= ERROR_MARK
;
1546 tree loop_bound_step
= NULL
;
1547 tree loop_bound_var
= NULL
;
1548 tree loop_iv_base
= NULL
;
1551 exits
= get_loop_exit_edges (loop
);
1552 FOR_EACH_VEC_ELT (exits
, j
, ex
)
1553 if (!(ex
->flags
& (EDGE_EH
| EDGE_ABNORMAL_CALL
| EDGE_FAKE
)))
1561 FOR_EACH_VEC_ELT (exits
, j
, ex
)
1564 HOST_WIDE_INT nitercst
;
1565 int max
= PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS
);
1567 enum br_predictor predictor
;
1570 if (ex
->flags
& (EDGE_EH
| EDGE_ABNORMAL_CALL
| EDGE_FAKE
))
1572 /* Loop heuristics do not expect exit conditional to be inside
1573 inner loop. We predict from innermost to outermost loop. */
1574 if (predicted_by_loop_heuristics_p (ex
->src
))
1576 predict_extra_loop_exits (ex
);
1578 if (number_of_iterations_exit (loop
, ex
, &niter_desc
, false, false))
1579 niter
= niter_desc
.niter
;
1580 if (!niter
|| TREE_CODE (niter_desc
.niter
) != INTEGER_CST
)
1581 niter
= loop_niter_by_eval (loop
, ex
);
1583 if (TREE_CODE (niter
) == INTEGER_CST
)
1585 if (tree_fits_uhwi_p (niter
)
1587 && compare_tree_int (niter
, max
- 1) == -1)
1588 nitercst
= tree_to_uhwi (niter
) + 1;
1591 predictor
= PRED_LOOP_ITERATIONS
;
1593 /* If we have just one exit and we can derive some information about
1594 the number of iterations of the loop from the statements inside
1595 the loop, use it to predict this exit. */
1596 else if (n_exits
== 1
1597 && estimated_stmt_executions (loop
, &nit
))
1599 if (wi::gtu_p (nit
, max
))
1602 nitercst
= nit
.to_shwi ();
1603 predictor
= PRED_LOOP_ITERATIONS_GUESSED
;
1605 /* If we have likely upper bound, trust it for very small iteration
1606 counts. Such loops would otherwise get mispredicted by standard
1607 LOOP_EXIT heuristics. */
1608 else if (n_exits
== 1
1609 && likely_max_stmt_executions (loop
, &nit
)
1611 RDIV (REG_BR_PROB_BASE
,
1614 [PRED_LOOP_EXIT
].hitrate
)))
1616 nitercst
= nit
.to_shwi ();
1617 predictor
= PRED_LOOP_ITERATIONS_MAX
;
1622 gcc_checking_assert (nitercst
);
1623 probability
= RDIV (REG_BR_PROB_BASE
, nitercst
);
1624 predict_edge (ex
, predictor
, probability
);
1628 /* Find information about loop bound variables. */
1629 for (nb_iter
= loop
->bounds
; nb_iter
;
1630 nb_iter
= nb_iter
->next
)
1632 && gimple_code (nb_iter
->stmt
) == GIMPLE_COND
)
1634 stmt
= as_a
<gcond
*> (nb_iter
->stmt
);
1637 if (!stmt
&& last_stmt (loop
->header
)
1638 && gimple_code (last_stmt (loop
->header
)) == GIMPLE_COND
)
1639 stmt
= as_a
<gcond
*> (last_stmt (loop
->header
));
1641 is_comparison_with_loop_invariant_p (stmt
, loop
,
1647 bbs
= get_loop_body (loop
);
1649 for (j
= 0; j
< loop
->num_nodes
; j
++)
1651 int header_found
= 0;
1657 /* Bypass loop heuristics on continue statement. These
1658 statements construct loops via "non-loop" constructs
1659 in the source language and are better to be handled
1661 if (predicted_by_p (bb
, PRED_CONTINUE
))
1664 /* Loop branch heuristics - predict an edge back to a
1665 loop's head as taken. */
1666 if (bb
== loop
->latch
)
1668 e
= find_edge (loop
->latch
, loop
->header
);
1672 predict_edge_def (e
, PRED_LOOP_BRANCH
, TAKEN
);
1676 /* Loop exit heuristics - predict an edge exiting the loop if the
1677 conditional has no loop header successors as not taken. */
1679 /* If we already used more reliable loop exit predictors, do not
1680 bother with PRED_LOOP_EXIT. */
1681 && !predicted_by_loop_heuristics_p (bb
))
1683 /* For loop with many exits we don't want to predict all exits
1684 with the pretty large probability, because if all exits are
1685 considered in row, the loop would be predicted to iterate
1686 almost never. The code to divide probability by number of
1687 exits is very rough. It should compute the number of exits
1688 taken in each patch through function (not the overall number
1689 of exits that might be a lot higher for loops with wide switch
1690 statements in them) and compute n-th square root.
1692 We limit the minimal probability by 2% to avoid
1693 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
1694 as this was causing regression in perl benchmark containing such
1697 int probability
= ((REG_BR_PROB_BASE
1698 - predictor_info
[(int) PRED_LOOP_EXIT
].hitrate
)
1700 if (probability
< HITRATE (2))
1701 probability
= HITRATE (2);
1702 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1703 if (e
->dest
->index
< NUM_FIXED_BLOCKS
1704 || !flow_bb_inside_loop_p (loop
, e
->dest
))
1705 predict_edge (e
, PRED_LOOP_EXIT
, probability
);
1708 predict_iv_comparison (loop
, bb
, loop_bound_var
, loop_iv_base
,
1710 tree_to_shwi (loop_bound_step
));
1713 /* Free basic blocks from get_loop_body. */
1718 /* Attempt to predict probabilities of BB outgoing edges using local
1721 bb_estimate_probability_locally (basic_block bb
)
1723 rtx_insn
*last_insn
= BB_END (bb
);
1726 if (! can_predict_insn_p (last_insn
))
1728 cond
= get_condition (last_insn
, NULL
, false, false);
1732 /* Try "pointer heuristic."
1733 A comparison ptr == 0 is predicted as false.
1734 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
1735 if (COMPARISON_P (cond
)
1736 && ((REG_P (XEXP (cond
, 0)) && REG_POINTER (XEXP (cond
, 0)))
1737 || (REG_P (XEXP (cond
, 1)) && REG_POINTER (XEXP (cond
, 1)))))
1739 if (GET_CODE (cond
) == EQ
)
1740 predict_insn_def (last_insn
, PRED_POINTER
, NOT_TAKEN
);
1741 else if (GET_CODE (cond
) == NE
)
1742 predict_insn_def (last_insn
, PRED_POINTER
, TAKEN
);
1746 /* Try "opcode heuristic."
1747 EQ tests are usually false and NE tests are usually true. Also,
1748 most quantities are positive, so we can make the appropriate guesses
1749 about signed comparisons against zero. */
1750 switch (GET_CODE (cond
))
1753 /* Unconditional branch. */
1754 predict_insn_def (last_insn
, PRED_UNCONDITIONAL
,
1755 cond
== const0_rtx
? NOT_TAKEN
: TAKEN
);
1760 /* Floating point comparisons appears to behave in a very
1761 unpredictable way because of special role of = tests in
1763 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
1765 /* Comparisons with 0 are often used for booleans and there is
1766 nothing useful to predict about them. */
1767 else if (XEXP (cond
, 1) == const0_rtx
1768 || XEXP (cond
, 0) == const0_rtx
)
1771 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, NOT_TAKEN
);
1776 /* Floating point comparisons appears to behave in a very
1777 unpredictable way because of special role of = tests in
1779 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
1781 /* Comparisons with 0 are often used for booleans and there is
1782 nothing useful to predict about them. */
1783 else if (XEXP (cond
, 1) == const0_rtx
1784 || XEXP (cond
, 0) == const0_rtx
)
1787 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, TAKEN
);
1791 predict_insn_def (last_insn
, PRED_FPOPCODE
, TAKEN
);
1795 predict_insn_def (last_insn
, PRED_FPOPCODE
, NOT_TAKEN
);
1800 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
1801 || XEXP (cond
, 1) == constm1_rtx
)
1802 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, NOT_TAKEN
);
1807 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
1808 || XEXP (cond
, 1) == constm1_rtx
)
1809 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, TAKEN
);
1817 /* Set edge->probability for each successor edge of BB. */
1819 guess_outgoing_edge_probabilities (basic_block bb
)
1821 bb_estimate_probability_locally (bb
);
1822 combine_predictions_for_insn (BB_END (bb
), bb
);
1825 static tree
expr_expected_value (tree
, bitmap
, enum br_predictor
*predictor
);
1827 /* Helper function for expr_expected_value. */
1830 expr_expected_value_1 (tree type
, tree op0
, enum tree_code code
,
1831 tree op1
, bitmap visited
, enum br_predictor
*predictor
)
1836 *predictor
= PRED_UNCONDITIONAL
;
1838 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1840 if (TREE_CONSTANT (op0
))
1843 if (code
!= SSA_NAME
)
1846 def
= SSA_NAME_DEF_STMT (op0
);
1848 /* If we were already here, break the infinite cycle. */
1849 if (!bitmap_set_bit (visited
, SSA_NAME_VERSION (op0
)))
1852 if (gimple_code (def
) == GIMPLE_PHI
)
1854 /* All the arguments of the PHI node must have the same constant
1856 int i
, n
= gimple_phi_num_args (def
);
1857 tree val
= NULL
, new_val
;
1859 for (i
= 0; i
< n
; i
++)
1861 tree arg
= PHI_ARG_DEF (def
, i
);
1862 enum br_predictor predictor2
;
1864 /* If this PHI has itself as an argument, we cannot
1865 determine the string length of this argument. However,
1866 if we can find an expected constant value for the other
1867 PHI args then we can still be sure that this is
1868 likely a constant. So be optimistic and just
1869 continue with the next argument. */
1870 if (arg
== PHI_RESULT (def
))
1873 new_val
= expr_expected_value (arg
, visited
, &predictor2
);
1875 /* It is difficult to combine value predictors. Simply assume
1876 that later predictor is weaker and take its prediction. */
1877 if (predictor
&& *predictor
< predictor2
)
1878 *predictor
= predictor2
;
1883 else if (!operand_equal_p (val
, new_val
, false))
1888 if (is_gimple_assign (def
))
1890 if (gimple_assign_lhs (def
) != op0
)
1893 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def
)),
1894 gimple_assign_rhs1 (def
),
1895 gimple_assign_rhs_code (def
),
1896 gimple_assign_rhs2 (def
),
1897 visited
, predictor
);
1900 if (is_gimple_call (def
))
1902 tree decl
= gimple_call_fndecl (def
);
1905 if (gimple_call_internal_p (def
)
1906 && gimple_call_internal_fn (def
) == IFN_BUILTIN_EXPECT
)
1908 gcc_assert (gimple_call_num_args (def
) == 3);
1909 tree val
= gimple_call_arg (def
, 0);
1910 if (TREE_CONSTANT (val
))
1914 tree val2
= gimple_call_arg (def
, 2);
1915 gcc_assert (TREE_CODE (val2
) == INTEGER_CST
1916 && tree_fits_uhwi_p (val2
)
1917 && tree_to_uhwi (val2
) < END_PREDICTORS
);
1918 *predictor
= (enum br_predictor
) tree_to_uhwi (val2
);
1920 return gimple_call_arg (def
, 1);
1924 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
1925 switch (DECL_FUNCTION_CODE (decl
))
1927 case BUILT_IN_EXPECT
:
1930 if (gimple_call_num_args (def
) != 2)
1932 val
= gimple_call_arg (def
, 0);
1933 if (TREE_CONSTANT (val
))
1936 *predictor
= PRED_BUILTIN_EXPECT
;
1937 return gimple_call_arg (def
, 1);
1940 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
:
1941 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
1942 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
1943 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
1944 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
1945 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
1946 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
1947 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
:
1948 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
1949 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
1950 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
1951 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
1952 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
1953 /* Assume that any given atomic operation has low contention,
1954 and thus the compare-and-swap operation succeeds. */
1956 *predictor
= PRED_COMPARE_AND_SWAP
;
1957 return boolean_true_node
;
1966 if (get_gimple_rhs_class (code
) == GIMPLE_BINARY_RHS
)
1969 enum br_predictor predictor2
;
1970 op0
= expr_expected_value (op0
, visited
, predictor
);
1973 op1
= expr_expected_value (op1
, visited
, &predictor2
);
1974 if (predictor
&& *predictor
< predictor2
)
1975 *predictor
= predictor2
;
1978 res
= fold_build2 (code
, type
, op0
, op1
);
1979 if (TREE_CONSTANT (res
))
1983 if (get_gimple_rhs_class (code
) == GIMPLE_UNARY_RHS
)
1986 op0
= expr_expected_value (op0
, visited
, predictor
);
1989 res
= fold_build1 (code
, type
, op0
);
1990 if (TREE_CONSTANT (res
))
1997 /* Return constant EXPR will likely have at execution time, NULL if unknown.
1998 The function is used by builtin_expect branch predictor so the evidence
1999 must come from this construct and additional possible constant folding.
2001 We may want to implement more involved value guess (such as value range
2002 propagation based prediction), but such tricks shall go to new
2006 expr_expected_value (tree expr
, bitmap visited
,
2007 enum br_predictor
*predictor
)
2009 enum tree_code code
;
2012 if (TREE_CONSTANT (expr
))
2015 *predictor
= PRED_UNCONDITIONAL
;
2019 extract_ops_from_tree (expr
, &code
, &op0
, &op1
);
2020 return expr_expected_value_1 (TREE_TYPE (expr
),
2021 op0
, code
, op1
, visited
, predictor
);
2024 /* Predict using opcode of the last statement in basic block. */
2026 tree_predict_by_opcode (basic_block bb
)
2028 gimple
*stmt
= last_stmt (bb
);
2036 enum br_predictor predictor
;
2038 if (!stmt
|| gimple_code (stmt
) != GIMPLE_COND
)
2040 FOR_EACH_EDGE (then_edge
, ei
, bb
->succs
)
2041 if (then_edge
->flags
& EDGE_TRUE_VALUE
)
2043 op0
= gimple_cond_lhs (stmt
);
2044 op1
= gimple_cond_rhs (stmt
);
2045 cmp
= gimple_cond_code (stmt
);
2046 type
= TREE_TYPE (op0
);
2047 visited
= BITMAP_ALLOC (NULL
);
2048 val
= expr_expected_value_1 (boolean_type_node
, op0
, cmp
, op1
, visited
,
2050 BITMAP_FREE (visited
);
2051 if (val
&& TREE_CODE (val
) == INTEGER_CST
)
2053 if (predictor
== PRED_BUILTIN_EXPECT
)
2055 int percent
= PARAM_VALUE (BUILTIN_EXPECT_PROBABILITY
);
2057 gcc_assert (percent
>= 0 && percent
<= 100);
2058 if (integer_zerop (val
))
2059 percent
= 100 - percent
;
2060 predict_edge (then_edge
, PRED_BUILTIN_EXPECT
, HITRATE (percent
));
2063 predict_edge (then_edge
, predictor
,
2064 integer_zerop (val
) ? NOT_TAKEN
: TAKEN
);
2066 /* Try "pointer heuristic."
2067 A comparison ptr == 0 is predicted as false.
2068 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2069 if (POINTER_TYPE_P (type
))
2072 predict_edge_def (then_edge
, PRED_TREE_POINTER
, NOT_TAKEN
);
2073 else if (cmp
== NE_EXPR
)
2074 predict_edge_def (then_edge
, PRED_TREE_POINTER
, TAKEN
);
2078 /* Try "opcode heuristic."
2079 EQ tests are usually false and NE tests are usually true. Also,
2080 most quantities are positive, so we can make the appropriate guesses
2081 about signed comparisons against zero. */
2086 /* Floating point comparisons appears to behave in a very
2087 unpredictable way because of special role of = tests in
2089 if (FLOAT_TYPE_P (type
))
2091 /* Comparisons with 0 are often used for booleans and there is
2092 nothing useful to predict about them. */
2093 else if (integer_zerop (op0
) || integer_zerop (op1
))
2096 predict_edge_def (then_edge
, PRED_TREE_OPCODE_NONEQUAL
, NOT_TAKEN
);
2101 /* Floating point comparisons appears to behave in a very
2102 unpredictable way because of special role of = tests in
2104 if (FLOAT_TYPE_P (type
))
2106 /* Comparisons with 0 are often used for booleans and there is
2107 nothing useful to predict about them. */
2108 else if (integer_zerop (op0
)
2109 || integer_zerop (op1
))
2112 predict_edge_def (then_edge
, PRED_TREE_OPCODE_NONEQUAL
, TAKEN
);
2116 predict_edge_def (then_edge
, PRED_TREE_FPOPCODE
, TAKEN
);
2119 case UNORDERED_EXPR
:
2120 predict_edge_def (then_edge
, PRED_TREE_FPOPCODE
, NOT_TAKEN
);
2125 if (integer_zerop (op1
)
2126 || integer_onep (op1
)
2127 || integer_all_onesp (op1
)
2130 || real_minus_onep (op1
))
2131 predict_edge_def (then_edge
, PRED_TREE_OPCODE_POSITIVE
, NOT_TAKEN
);
2136 if (integer_zerop (op1
)
2137 || integer_onep (op1
)
2138 || integer_all_onesp (op1
)
2141 || real_minus_onep (op1
))
2142 predict_edge_def (then_edge
, PRED_TREE_OPCODE_POSITIVE
, TAKEN
);
2150 /* Try to guess whether the value of return means error code. */
2152 static enum br_predictor
2153 return_prediction (tree val
, enum prediction
*prediction
)
2157 return PRED_NO_PREDICTION
;
2158 /* Different heuristics for pointers and scalars. */
2159 if (POINTER_TYPE_P (TREE_TYPE (val
)))
2161 /* NULL is usually not returned. */
2162 if (integer_zerop (val
))
2164 *prediction
= NOT_TAKEN
;
2165 return PRED_NULL_RETURN
;
2168 else if (INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2170 /* Negative return values are often used to indicate
2172 if (TREE_CODE (val
) == INTEGER_CST
2173 && tree_int_cst_sgn (val
) < 0)
2175 *prediction
= NOT_TAKEN
;
2176 return PRED_NEGATIVE_RETURN
;
2178 /* Constant return values seems to be commonly taken.
2179 Zero/one often represent booleans so exclude them from the
2181 if (TREE_CONSTANT (val
)
2182 && (!integer_zerop (val
) && !integer_onep (val
)))
2184 *prediction
= NOT_TAKEN
;
2185 return PRED_CONST_RETURN
;
2188 return PRED_NO_PREDICTION
;
2191 /* Find the basic block with return expression and look up for possible
2192 return value trying to apply RETURN_PREDICTION heuristics. */
2194 apply_return_prediction (void)
2196 greturn
*return_stmt
= NULL
;
2200 int phi_num_args
, i
;
2201 enum br_predictor pred
;
2202 enum prediction direction
;
2205 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
2207 gimple
*last
= last_stmt (e
->src
);
2209 && gimple_code (last
) == GIMPLE_RETURN
)
2211 return_stmt
= as_a
<greturn
*> (last
);
2217 return_val
= gimple_return_retval (return_stmt
);
2220 if (TREE_CODE (return_val
) != SSA_NAME
2221 || !SSA_NAME_DEF_STMT (return_val
)
2222 || gimple_code (SSA_NAME_DEF_STMT (return_val
)) != GIMPLE_PHI
)
2224 phi
= as_a
<gphi
*> (SSA_NAME_DEF_STMT (return_val
));
2225 phi_num_args
= gimple_phi_num_args (phi
);
2226 pred
= return_prediction (PHI_ARG_DEF (phi
, 0), &direction
);
2228 /* Avoid the degenerate case where all return values form the function
2229 belongs to same category (ie they are all positive constants)
2230 so we can hardly say something about them. */
2231 for (i
= 1; i
< phi_num_args
; i
++)
2232 if (pred
!= return_prediction (PHI_ARG_DEF (phi
, i
), &direction
))
2234 if (i
!= phi_num_args
)
2235 for (i
= 0; i
< phi_num_args
; i
++)
2237 pred
= return_prediction (PHI_ARG_DEF (phi
, i
), &direction
);
2238 if (pred
!= PRED_NO_PREDICTION
)
2239 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi
, i
), pred
,
2244 /* Look for basic block that contains unlikely to happen events
2245 (such as noreturn calls) and mark all paths leading to execution
2246 of this basic blocks as unlikely. */
2249 tree_bb_level_predictions (void)
2252 bool has_return_edges
= false;
2256 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
2257 if (!(e
->flags
& (EDGE_ABNORMAL
| EDGE_FAKE
| EDGE_EH
)))
2259 has_return_edges
= true;
2263 apply_return_prediction ();
2265 FOR_EACH_BB_FN (bb
, cfun
)
2267 gimple_stmt_iterator gsi
;
2269 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2271 gimple
*stmt
= gsi_stmt (gsi
);
2274 if (is_gimple_call (stmt
))
2276 if ((gimple_call_flags (stmt
) & ECF_NORETURN
)
2277 && has_return_edges
)
2278 predict_paths_leading_to (bb
, PRED_NORETURN
,
2280 decl
= gimple_call_fndecl (stmt
);
2282 && lookup_attribute ("cold",
2283 DECL_ATTRIBUTES (decl
)))
2284 predict_paths_leading_to (bb
, PRED_COLD_FUNCTION
,
2287 else if (gimple_code (stmt
) == GIMPLE_PREDICT
)
2289 predict_paths_leading_to (bb
, gimple_predict_predictor (stmt
),
2290 gimple_predict_outcome (stmt
));
2291 /* Keep GIMPLE_PREDICT around so early inlining will propagate
2292 hints to callers. */
2298 /* Callback for hash_map::traverse, asserts that the pointer map is
2302 assert_is_empty (const_basic_block
const &, edge_prediction
*const &value
,
2305 gcc_assert (!value
);
2309 /* Predict branch probabilities and estimate profile for basic block BB. */
2312 tree_estimate_probability_bb (basic_block bb
)
2318 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2320 /* Predict edges to user labels with attributes. */
2321 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
2323 gimple_stmt_iterator gi
;
2324 for (gi
= gsi_start_bb (e
->dest
); !gsi_end_p (gi
); gsi_next (&gi
))
2326 glabel
*label_stmt
= dyn_cast
<glabel
*> (gsi_stmt (gi
));
2331 decl
= gimple_label_label (label_stmt
);
2332 if (DECL_ARTIFICIAL (decl
))
2335 /* Finally, we have a user-defined label. */
2336 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl
)))
2337 predict_edge_def (e
, PRED_COLD_LABEL
, NOT_TAKEN
);
2338 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (decl
)))
2339 predict_edge_def (e
, PRED_HOT_LABEL
, TAKEN
);
2343 /* Predict early returns to be probable, as we've already taken
2344 care for error returns and other cases are often used for
2345 fast paths through function.
2347 Since we've already removed the return statements, we are
2348 looking for CFG like:
2358 if (e
->dest
!= bb
->next_bb
2359 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
2360 && single_succ_p (e
->dest
)
2361 && single_succ_edge (e
->dest
)->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
2362 && (last
= last_stmt (e
->dest
)) != NULL
2363 && gimple_code (last
) == GIMPLE_RETURN
)
2368 if (single_succ_p (bb
))
2370 FOR_EACH_EDGE (e1
, ei1
, bb
->preds
)
2371 if (!predicted_by_p (e1
->src
, PRED_NULL_RETURN
)
2372 && !predicted_by_p (e1
->src
, PRED_CONST_RETURN
)
2373 && !predicted_by_p (e1
->src
, PRED_NEGATIVE_RETURN
))
2374 predict_edge_def (e1
, PRED_TREE_EARLY_RETURN
, NOT_TAKEN
);
2377 if (!predicted_by_p (e
->src
, PRED_NULL_RETURN
)
2378 && !predicted_by_p (e
->src
, PRED_CONST_RETURN
)
2379 && !predicted_by_p (e
->src
, PRED_NEGATIVE_RETURN
))
2380 predict_edge_def (e
, PRED_TREE_EARLY_RETURN
, NOT_TAKEN
);
2383 /* Look for block we are guarding (ie we dominate it,
2384 but it doesn't postdominate us). */
2385 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
) && e
->dest
!= bb
2386 && dominated_by_p (CDI_DOMINATORS
, e
->dest
, e
->src
)
2387 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, e
->dest
))
2389 gimple_stmt_iterator bi
;
2391 /* The call heuristic claims that a guarded function call
2392 is improbable. This is because such calls are often used
2393 to signal exceptional situations such as printing error
2395 for (bi
= gsi_start_bb (e
->dest
); !gsi_end_p (bi
);
2398 gimple
*stmt
= gsi_stmt (bi
);
2399 if (is_gimple_call (stmt
)
2400 /* Constant and pure calls are hardly used to signalize
2401 something exceptional. */
2402 && gimple_has_side_effects (stmt
))
2404 predict_edge_def (e
, PRED_CALL
, NOT_TAKEN
);
2410 tree_predict_by_opcode (bb
);
2413 /* Predict branch probabilities and estimate profile of the tree CFG.
2414 This function can be called from the loop optimizers to recompute
2415 the profile information.
2416 If DRY_RUN is set, do not modify CFG and only produce dump files. */
2419 tree_estimate_probability (bool dry_run
)
2423 add_noreturn_fake_exit_edges ();
2424 connect_infinite_loops_to_exit ();
2425 /* We use loop_niter_by_eval, which requires that the loops have
2427 create_preheaders (CP_SIMPLE_PREHEADERS
);
2428 calculate_dominance_info (CDI_POST_DOMINATORS
);
2430 bb_predictions
= new hash_map
<const_basic_block
, edge_prediction
*>;
2431 tree_bb_level_predictions ();
2432 record_loop_exits ();
2434 if (number_of_loops (cfun
) > 1)
2437 FOR_EACH_BB_FN (bb
, cfun
)
2438 tree_estimate_probability_bb (bb
);
2440 FOR_EACH_BB_FN (bb
, cfun
)
2441 combine_predictions_for_bb (bb
, dry_run
);
2444 bb_predictions
->traverse
<void *, assert_is_empty
> (NULL
);
2446 delete bb_predictions
;
2447 bb_predictions
= NULL
;
2450 estimate_bb_frequencies (false);
2451 free_dominance_info (CDI_POST_DOMINATORS
);
2452 remove_fake_exit_edges ();
2455 /* Predict edges to successors of CUR whose sources are not postdominated by
2456 BB by PRED and recurse to all postdominators. */
2459 predict_paths_for_bb (basic_block cur
, basic_block bb
,
2460 enum br_predictor pred
,
2461 enum prediction taken
,
2468 /* We are looking for all edges forming edge cut induced by
2469 set of all blocks postdominated by BB. */
2470 FOR_EACH_EDGE (e
, ei
, cur
->preds
)
2471 if (e
->src
->index
>= NUM_FIXED_BLOCKS
2472 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, bb
))
2478 /* Ignore fake edges and eh, we predict them as not taken anyway. */
2479 if (e
->flags
& (EDGE_EH
| EDGE_FAKE
))
2481 gcc_assert (bb
== cur
|| dominated_by_p (CDI_POST_DOMINATORS
, cur
, bb
));
2483 /* See if there is an edge from e->src that is not abnormal
2484 and does not lead to BB. */
2485 FOR_EACH_EDGE (e2
, ei2
, e
->src
->succs
)
2487 && !(e2
->flags
& (EDGE_EH
| EDGE_FAKE
))
2488 && !dominated_by_p (CDI_POST_DOMINATORS
, e2
->dest
, bb
))
2494 /* If there is non-abnormal path leaving e->src, predict edge
2495 using predictor. Otherwise we need to look for paths
2498 The second may lead to infinite loop in the case we are predicitng
2499 regions that are only reachable by abnormal edges. We simply
2500 prevent visiting given BB twice. */
2503 if (!edge_predicted_by_p (e
, pred
, taken
))
2504 predict_edge_def (e
, pred
, taken
);
2506 else if (bitmap_set_bit (visited
, e
->src
->index
))
2507 predict_paths_for_bb (e
->src
, e
->src
, pred
, taken
, visited
);
2509 for (son
= first_dom_son (CDI_POST_DOMINATORS
, cur
);
2511 son
= next_dom_son (CDI_POST_DOMINATORS
, son
))
2512 predict_paths_for_bb (son
, bb
, pred
, taken
, visited
);
2515 /* Sets branch probabilities according to PREDiction and
2519 predict_paths_leading_to (basic_block bb
, enum br_predictor pred
,
2520 enum prediction taken
)
2522 bitmap visited
= BITMAP_ALLOC (NULL
);
2523 predict_paths_for_bb (bb
, bb
, pred
, taken
, visited
);
2524 BITMAP_FREE (visited
);
2527 /* Like predict_paths_leading_to but take edge instead of basic block. */
2530 predict_paths_leading_to_edge (edge e
, enum br_predictor pred
,
2531 enum prediction taken
)
2533 bool has_nonloop_edge
= false;
2537 basic_block bb
= e
->src
;
2538 FOR_EACH_EDGE (e2
, ei
, bb
->succs
)
2539 if (e2
->dest
!= e
->src
&& e2
->dest
!= e
->dest
2540 && !(e
->flags
& (EDGE_EH
| EDGE_FAKE
))
2541 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, e2
->dest
))
2543 has_nonloop_edge
= true;
2546 if (!has_nonloop_edge
)
2548 bitmap visited
= BITMAP_ALLOC (NULL
);
2549 predict_paths_for_bb (bb
, bb
, pred
, taken
, visited
);
2550 BITMAP_FREE (visited
);
2553 predict_edge_def (e
, pred
, taken
);
2556 /* This is used to carry information about basic blocks. It is
2557 attached to the AUX field of the standard CFG block. */
2561 /* Estimated frequency of execution of basic_block. */
2564 /* To keep queue of basic blocks to process. */
2567 /* Number of predecessors we need to visit first. */
2571 /* Similar information for edges. */
2572 struct edge_prob_info
2574 /* In case edge is a loopback edge, the probability edge will be reached
2575 in case header is. Estimated number of iterations of the loop can be
2576 then computed as 1 / (1 - back_edge_prob). */
2577 sreal back_edge_prob
;
2578 /* True if the edge is a loopback edge in the natural loop. */
2579 unsigned int back_edge
:1;
2582 #define BLOCK_INFO(B) ((block_info *) (B)->aux)
2584 #define EDGE_INFO(E) ((edge_prob_info *) (E)->aux)
2586 /* Helper function for estimate_bb_frequencies.
2587 Propagate the frequencies in blocks marked in
2588 TOVISIT, starting in HEAD. */
2591 propagate_freq (basic_block head
, bitmap tovisit
)
2600 /* For each basic block we need to visit count number of his predecessors
2601 we need to visit first. */
2602 EXECUTE_IF_SET_IN_BITMAP (tovisit
, 0, i
, bi
)
2607 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
2609 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2611 bool visit
= bitmap_bit_p (tovisit
, e
->src
->index
);
2613 if (visit
&& !(e
->flags
& EDGE_DFS_BACK
))
2615 else if (visit
&& dump_file
&& !EDGE_INFO (e
)->back_edge
)
2617 "Irreducible region hit, ignoring edge to %i->%i\n",
2618 e
->src
->index
, bb
->index
);
2620 BLOCK_INFO (bb
)->npredecessors
= count
;
2621 /* When function never returns, we will never process exit block. */
2622 if (!count
&& bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
2623 bb
->count
= bb
->frequency
= 0;
2626 BLOCK_INFO (head
)->frequency
= 1;
2628 for (bb
= head
; bb
; bb
= nextbb
)
2631 sreal cyclic_probability
= 0;
2632 sreal frequency
= 0;
2634 nextbb
= BLOCK_INFO (bb
)->next
;
2635 BLOCK_INFO (bb
)->next
= NULL
;
2637 /* Compute frequency of basic block. */
2641 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2642 gcc_assert (!bitmap_bit_p (tovisit
, e
->src
->index
)
2643 || (e
->flags
& EDGE_DFS_BACK
));
2645 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2646 if (EDGE_INFO (e
)->back_edge
)
2648 cyclic_probability
+= EDGE_INFO (e
)->back_edge_prob
;
2650 else if (!(e
->flags
& EDGE_DFS_BACK
))
2652 /* frequency += (e->probability
2653 * BLOCK_INFO (e->src)->frequency /
2654 REG_BR_PROB_BASE); */
2656 sreal tmp
= e
->probability
;
2657 tmp
*= BLOCK_INFO (e
->src
)->frequency
;
2658 tmp
*= real_inv_br_prob_base
;
2662 if (cyclic_probability
== 0)
2664 BLOCK_INFO (bb
)->frequency
= frequency
;
2668 if (cyclic_probability
> real_almost_one
)
2669 cyclic_probability
= real_almost_one
;
2671 /* BLOCK_INFO (bb)->frequency = frequency
2672 / (1 - cyclic_probability) */
2674 cyclic_probability
= sreal (1) - cyclic_probability
;
2675 BLOCK_INFO (bb
)->frequency
= frequency
/ cyclic_probability
;
2679 bitmap_clear_bit (tovisit
, bb
->index
);
2681 e
= find_edge (bb
, head
);
2684 /* EDGE_INFO (e)->back_edge_prob
2685 = ((e->probability * BLOCK_INFO (bb)->frequency)
2686 / REG_BR_PROB_BASE); */
2688 sreal tmp
= e
->probability
;
2689 tmp
*= BLOCK_INFO (bb
)->frequency
;
2690 EDGE_INFO (e
)->back_edge_prob
= tmp
* real_inv_br_prob_base
;
2693 /* Propagate to successor blocks. */
2694 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2695 if (!(e
->flags
& EDGE_DFS_BACK
)
2696 && BLOCK_INFO (e
->dest
)->npredecessors
)
2698 BLOCK_INFO (e
->dest
)->npredecessors
--;
2699 if (!BLOCK_INFO (e
->dest
)->npredecessors
)
2704 BLOCK_INFO (last
)->next
= e
->dest
;
2712 /* Estimate frequencies in loops at same nest level. */
2715 estimate_loops_at_level (struct loop
*first_loop
)
2719 for (loop
= first_loop
; loop
; loop
= loop
->next
)
2724 bitmap tovisit
= BITMAP_ALLOC (NULL
);
2726 estimate_loops_at_level (loop
->inner
);
2728 /* Find current loop back edge and mark it. */
2729 e
= loop_latch_edge (loop
);
2730 EDGE_INFO (e
)->back_edge
= 1;
2732 bbs
= get_loop_body (loop
);
2733 for (i
= 0; i
< loop
->num_nodes
; i
++)
2734 bitmap_set_bit (tovisit
, bbs
[i
]->index
);
2736 propagate_freq (loop
->header
, tovisit
);
2737 BITMAP_FREE (tovisit
);
2741 /* Propagates frequencies through structure of loops. */
2744 estimate_loops (void)
2746 bitmap tovisit
= BITMAP_ALLOC (NULL
);
2749 /* Start by estimating the frequencies in the loops. */
2750 if (number_of_loops (cfun
) > 1)
2751 estimate_loops_at_level (current_loops
->tree_root
->inner
);
2753 /* Now propagate the frequencies through all the blocks. */
2754 FOR_ALL_BB_FN (bb
, cfun
)
2756 bitmap_set_bit (tovisit
, bb
->index
);
2758 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun
), tovisit
);
2759 BITMAP_FREE (tovisit
);
2762 /* Drop the profile for NODE to guessed, and update its frequency based on
2763 whether it is expected to be hot given the CALL_COUNT. */
2766 drop_profile (struct cgraph_node
*node
, gcov_type call_count
)
2768 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->decl
);
2769 /* In the case where this was called by another function with a
2770 dropped profile, call_count will be 0. Since there are no
2771 non-zero call counts to this function, we don't know for sure
2772 whether it is hot, and therefore it will be marked normal below. */
2773 bool hot
= maybe_hot_count_p (NULL
, call_count
);
2777 "Dropping 0 profile for %s/%i. %s based on calls.\n",
2778 node
->name (), node
->order
,
2779 hot
? "Function is hot" : "Function is normal");
2780 /* We only expect to miss profiles for functions that are reached
2781 via non-zero call edges in cases where the function may have
2782 been linked from another module or library (COMDATs and extern
2783 templates). See the comments below for handle_missing_profiles.
2784 Also, only warn in cases where the missing counts exceed the
2785 number of training runs. In certain cases with an execv followed
2786 by a no-return call the profile for the no-return call is not
2787 dumped and there can be a mismatch. */
2788 if (!DECL_COMDAT (node
->decl
) && !DECL_EXTERNAL (node
->decl
)
2789 && call_count
> profile_info
->runs
)
2791 if (flag_profile_correction
)
2795 "Missing counts for called function %s/%i\n",
2796 node
->name (), node
->order
);
2799 warning (0, "Missing counts for called function %s/%i",
2800 node
->name (), node
->order
);
2803 profile_status_for_fn (fn
)
2804 = (flag_guess_branch_prob
? PROFILE_GUESSED
: PROFILE_ABSENT
);
2806 = hot
? NODE_FREQUENCY_HOT
: NODE_FREQUENCY_NORMAL
;
2809 /* In the case of COMDAT routines, multiple object files will contain the same
2810 function and the linker will select one for the binary. In that case
2811 all the other copies from the profile instrument binary will be missing
2812 profile counts. Look for cases where this happened, due to non-zero
2813 call counts going to 0-count functions, and drop the profile to guessed
2814 so that we can use the estimated probabilities and avoid optimizing only
2817 The other case where the profile may be missing is when the routine
2818 is not going to be emitted to the object file, e.g. for "extern template"
2819 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
2820 all other cases of non-zero calls to 0-count functions. */
2823 handle_missing_profiles (void)
2825 struct cgraph_node
*node
;
2826 int unlikely_count_fraction
= PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION
);
2827 vec
<struct cgraph_node
*> worklist
;
2828 worklist
.create (64);
2830 /* See if 0 count function has non-0 count callers. In this case we
2831 lost some profile. Drop its function profile to PROFILE_GUESSED. */
2832 FOR_EACH_DEFINED_FUNCTION (node
)
2834 struct cgraph_edge
*e
;
2835 gcov_type call_count
= 0;
2836 gcov_type max_tp_first_run
= 0;
2837 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->decl
);
2841 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2843 call_count
+= e
->count
;
2845 if (e
->caller
->tp_first_run
> max_tp_first_run
)
2846 max_tp_first_run
= e
->caller
->tp_first_run
;
2849 /* If time profile is missing, let assign the maximum that comes from
2850 caller functions. */
2851 if (!node
->tp_first_run
&& max_tp_first_run
)
2852 node
->tp_first_run
= max_tp_first_run
+ 1;
2856 && (call_count
* unlikely_count_fraction
>= profile_info
->runs
))
2858 drop_profile (node
, call_count
);
2859 worklist
.safe_push (node
);
2863 /* Propagate the profile dropping to other 0-count COMDATs that are
2864 potentially called by COMDATs we already dropped the profile on. */
2865 while (worklist
.length () > 0)
2867 struct cgraph_edge
*e
;
2869 node
= worklist
.pop ();
2870 for (e
= node
->callees
; e
; e
= e
->next_caller
)
2872 struct cgraph_node
*callee
= e
->callee
;
2873 struct function
*fn
= DECL_STRUCT_FUNCTION (callee
->decl
);
2875 if (callee
->count
> 0)
2877 if (DECL_COMDAT (callee
->decl
) && fn
&& fn
->cfg
2878 && profile_status_for_fn (fn
) == PROFILE_READ
)
2880 drop_profile (node
, 0);
2881 worklist
.safe_push (callee
);
2885 worklist
.release ();
2888 /* Convert counts measured by profile driven feedback to frequencies.
2889 Return nonzero iff there was any nonzero execution count. */
2892 counts_to_freqs (void)
2894 gcov_type count_max
, true_count_max
= 0;
2897 /* Don't overwrite the estimated frequencies when the profile for
2898 the function is missing. We may drop this function PROFILE_GUESSED
2899 later in drop_profile (). */
2900 if (!flag_auto_profile
&& !ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
)
2903 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
2904 true_count_max
= MAX (bb
->count
, true_count_max
);
2906 count_max
= MAX (true_count_max
, 1);
2907 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
2908 bb
->frequency
= (bb
->count
* BB_FREQ_MAX
+ count_max
/ 2) / count_max
;
2910 return true_count_max
;
2913 /* Return true if function is likely to be expensive, so there is no point to
2914 optimize performance of prologue, epilogue or do inlining at the expense
2915 of code size growth. THRESHOLD is the limit of number of instructions
2916 function can execute at average to be still considered not expensive. */
2919 expensive_function_p (int threshold
)
2921 unsigned int sum
= 0;
2925 /* We can not compute accurately for large thresholds due to scaled
2927 gcc_assert (threshold
<= BB_FREQ_MAX
);
2929 /* Frequencies are out of range. This either means that function contains
2930 internal loop executing more than BB_FREQ_MAX times or profile feedback
2931 is available and function has not been executed at all. */
2932 if (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
== 0)
2935 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
2936 limit
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
* threshold
;
2937 FOR_EACH_BB_FN (bb
, cfun
)
2941 FOR_BB_INSNS (bb
, insn
)
2942 if (active_insn_p (insn
))
2944 sum
+= bb
->frequency
;
2953 /* Estimate and propagate basic block frequencies using the given branch
2954 probabilities. If FORCE is true, the frequencies are used to estimate
2955 the counts even when there are already non-zero profile counts. */
2958 estimate_bb_frequencies (bool force
)
2963 if (force
|| profile_status_for_fn (cfun
) != PROFILE_READ
|| !counts_to_freqs ())
2965 static int real_values_initialized
= 0;
2967 if (!real_values_initialized
)
2969 real_values_initialized
= 1;
2970 real_br_prob_base
= REG_BR_PROB_BASE
;
2971 real_bb_freq_max
= BB_FREQ_MAX
;
2972 real_one_half
= sreal (1, -1);
2973 real_inv_br_prob_base
= sreal (1) / real_br_prob_base
;
2974 real_almost_one
= sreal (1) - real_inv_br_prob_base
;
2977 mark_dfs_back_edges ();
2979 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
))->probability
=
2982 /* Set up block info for each basic block. */
2983 alloc_aux_for_blocks (sizeof (block_info
));
2984 alloc_aux_for_edges (sizeof (edge_prob_info
));
2985 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
2990 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2992 EDGE_INFO (e
)->back_edge_prob
= e
->probability
;
2993 EDGE_INFO (e
)->back_edge_prob
*= real_inv_br_prob_base
;
2997 /* First compute frequencies locally for each loop from innermost
2998 to outermost to examine frequencies for back edges. */
3002 FOR_EACH_BB_FN (bb
, cfun
)
3003 if (freq_max
< BLOCK_INFO (bb
)->frequency
)
3004 freq_max
= BLOCK_INFO (bb
)->frequency
;
3006 freq_max
= real_bb_freq_max
/ freq_max
;
3007 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
3009 sreal tmp
= BLOCK_INFO (bb
)->frequency
* freq_max
+ real_one_half
;
3010 bb
->frequency
= tmp
.to_int ();
3013 free_aux_for_blocks ();
3014 free_aux_for_edges ();
3016 compute_function_frequency ();
3019 /* Decide whether function is hot, cold or unlikely executed. */
3021 compute_function_frequency (void)
3024 struct cgraph_node
*node
= cgraph_node::get (current_function_decl
);
3026 if (DECL_STATIC_CONSTRUCTOR (current_function_decl
)
3027 || MAIN_NAME_P (DECL_NAME (current_function_decl
)))
3028 node
->only_called_at_startup
= true;
3029 if (DECL_STATIC_DESTRUCTOR (current_function_decl
))
3030 node
->only_called_at_exit
= true;
3032 if (profile_status_for_fn (cfun
) != PROFILE_READ
)
3034 int flags
= flags_from_decl_or_type (current_function_decl
);
3035 if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl
))
3037 node
->frequency
= NODE_FREQUENCY_UNLIKELY_EXECUTED
;
3038 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl
))
3040 node
->frequency
= NODE_FREQUENCY_HOT
;
3041 else if (flags
& ECF_NORETURN
)
3042 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
3043 else if (MAIN_NAME_P (DECL_NAME (current_function_decl
)))
3044 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
3045 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl
)
3046 || DECL_STATIC_DESTRUCTOR (current_function_decl
))
3047 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
3051 /* Only first time try to drop function into unlikely executed.
3052 After inlining the roundoff errors may confuse us.
3053 Ipa-profile pass will drop functions only called from unlikely
3054 functions to unlikely and that is most of what we care about. */
3055 if (!cfun
->after_inlining
)
3056 node
->frequency
= NODE_FREQUENCY_UNLIKELY_EXECUTED
;
3057 FOR_EACH_BB_FN (bb
, cfun
)
3059 if (maybe_hot_bb_p (cfun
, bb
))
3061 node
->frequency
= NODE_FREQUENCY_HOT
;
3064 if (!probably_never_executed_bb_p (cfun
, bb
))
3065 node
->frequency
= NODE_FREQUENCY_NORMAL
;
3069 /* Build PREDICT_EXPR. */
3071 build_predict_expr (enum br_predictor predictor
, enum prediction taken
)
3073 tree t
= build1 (PREDICT_EXPR
, void_type_node
,
3074 build_int_cst (integer_type_node
, predictor
));
3075 SET_PREDICT_EXPR_OUTCOME (t
, taken
);
3080 predictor_name (enum br_predictor predictor
)
3082 return predictor_info
[predictor
].name
;
3085 /* Predict branch probabilities and estimate profile of the tree CFG. */
3089 const pass_data pass_data_profile
=
3091 GIMPLE_PASS
, /* type */
3092 "profile_estimate", /* name */
3093 OPTGROUP_NONE
, /* optinfo_flags */
3094 TV_BRANCH_PROB
, /* tv_id */
3095 PROP_cfg
, /* properties_required */
3096 0, /* properties_provided */
3097 0, /* properties_destroyed */
3098 0, /* todo_flags_start */
3099 0, /* todo_flags_finish */
3102 class pass_profile
: public gimple_opt_pass
3105 pass_profile (gcc::context
*ctxt
)
3106 : gimple_opt_pass (pass_data_profile
, ctxt
)
3109 /* opt_pass methods: */
3110 virtual bool gate (function
*) { return flag_guess_branch_prob
; }
3111 virtual unsigned int execute (function
*);
3113 }; // class pass_profile
3116 pass_profile::execute (function
*fun
)
3120 if (profile_status_for_fn (cfun
) == PROFILE_GUESSED
)
3123 loop_optimizer_init (LOOPS_NORMAL
);
3124 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3125 flow_loops_dump (dump_file
, NULL
, 0);
3127 mark_irreducible_loops ();
3129 nb_loops
= number_of_loops (fun
);
3133 tree_estimate_probability (false);
3138 loop_optimizer_finalize ();
3139 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3140 gimple_dump_cfg (dump_file
, dump_flags
);
3141 if (profile_status_for_fn (fun
) == PROFILE_ABSENT
)
3142 profile_status_for_fn (fun
) = PROFILE_GUESSED
;
3149 make_pass_profile (gcc::context
*ctxt
)
3151 return new pass_profile (ctxt
);
3156 const pass_data pass_data_strip_predict_hints
=
3158 GIMPLE_PASS
, /* type */
3159 "*strip_predict_hints", /* name */
3160 OPTGROUP_NONE
, /* optinfo_flags */
3161 TV_BRANCH_PROB
, /* tv_id */
3162 PROP_cfg
, /* properties_required */
3163 0, /* properties_provided */
3164 0, /* properties_destroyed */
3165 0, /* todo_flags_start */
3166 0, /* todo_flags_finish */
3169 class pass_strip_predict_hints
: public gimple_opt_pass
3172 pass_strip_predict_hints (gcc::context
*ctxt
)
3173 : gimple_opt_pass (pass_data_strip_predict_hints
, ctxt
)
3176 /* opt_pass methods: */
3177 opt_pass
* clone () { return new pass_strip_predict_hints (m_ctxt
); }
3178 virtual unsigned int execute (function
*);
3180 }; // class pass_strip_predict_hints
3182 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
3183 we no longer need. */
3185 pass_strip_predict_hints::execute (function
*fun
)
3190 bool changed
= false;
3192 FOR_EACH_BB_FN (bb
, fun
)
3194 gimple_stmt_iterator bi
;
3195 for (bi
= gsi_start_bb (bb
); !gsi_end_p (bi
);)
3197 gimple
*stmt
= gsi_stmt (bi
);
3199 if (gimple_code (stmt
) == GIMPLE_PREDICT
)
3201 gsi_remove (&bi
, true);
3205 else if (is_gimple_call (stmt
))
3207 tree fndecl
= gimple_call_fndecl (stmt
);
3210 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
3211 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
3212 && gimple_call_num_args (stmt
) == 2)
3213 || (gimple_call_internal_p (stmt
)
3214 && gimple_call_internal_fn (stmt
) == IFN_BUILTIN_EXPECT
))
3216 var
= gimple_call_lhs (stmt
);
3221 = gimple_build_assign (var
, gimple_call_arg (stmt
, 0));
3222 gsi_replace (&bi
, ass_stmt
, true);
3226 gsi_remove (&bi
, true);
3234 return changed
? TODO_cleanup_cfg
: 0;
3240 make_pass_strip_predict_hints (gcc::context
*ctxt
)
3242 return new pass_strip_predict_hints (ctxt
);
3245 /* Rebuild function frequencies. Passes are in general expected to
3246 maintain profile by hand, however in some cases this is not possible:
3247 for example when inlining several functions with loops freuqencies might run
3248 out of scale and thus needs to be recomputed. */
3251 rebuild_frequencies (void)
3253 timevar_push (TV_REBUILD_FREQUENCIES
);
3255 /* When the max bb count in the function is small, there is a higher
3256 chance that there were truncation errors in the integer scaling
3257 of counts by inlining and other optimizations. This could lead
3258 to incorrect classification of code as being cold when it isn't.
3259 In that case, force the estimation of bb counts/frequencies from the
3260 branch probabilities, rather than computing frequencies from counts,
3261 which may also lead to frequencies incorrectly reduced to 0. There
3262 is less precision in the probabilities, so we only do this for small
3264 gcov_type count_max
= 0;
3266 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
3267 count_max
= MAX (bb
->count
, count_max
);
3269 if (profile_status_for_fn (cfun
) == PROFILE_GUESSED
3270 || (!flag_auto_profile
&& profile_status_for_fn (cfun
) == PROFILE_READ
3271 && count_max
< REG_BR_PROB_BASE
/10))
3273 loop_optimizer_init (0);
3274 add_noreturn_fake_exit_edges ();
3275 mark_irreducible_loops ();
3276 connect_infinite_loops_to_exit ();
3277 estimate_bb_frequencies (true);
3278 remove_fake_exit_edges ();
3279 loop_optimizer_finalize ();
3281 else if (profile_status_for_fn (cfun
) == PROFILE_READ
)
3285 timevar_pop (TV_REBUILD_FREQUENCIES
);
3288 /* Perform a dry run of the branch prediction pass and report comparsion of
3289 the predicted and real profile into the dump file. */
3292 report_predictor_hitrates (void)
3296 loop_optimizer_init (LOOPS_NORMAL
);
3297 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3298 flow_loops_dump (dump_file
, NULL
, 0);
3300 mark_irreducible_loops ();
3302 nb_loops
= number_of_loops (cfun
);
3306 tree_estimate_probability (true);
3311 loop_optimizer_finalize ();
3314 /* Force edge E to be cold.
3315 If IMPOSSIBLE is true, for edge to have count and probability 0 otherwise
3316 keep low probability to represent possible error in a guess. This is used
3317 i.e. in case we predict loop to likely iterate given number of times but
3318 we are not 100% sure.
3320 This function locally updates profile without attempt to keep global
3321 consistency which can not be reached in full generality without full profile
3322 rebuild from probabilities alone. Doing so is not necessarily a good idea
3323 because frequencies and counts may be more realistic then probabilities.
3325 In some cases (such as for elimination of early exits during full loop
3326 unrolling) the caller can ensure that profile will get consistent
3330 force_edge_cold (edge e
, bool impossible
)
3332 gcov_type count_sum
= 0;
3336 gcov_type old_count
= e
->count
;
3337 int old_probability
= e
->probability
;
3338 gcov_type gcov_scale
= REG_BR_PROB_BASE
;
3339 int prob_scale
= REG_BR_PROB_BASE
;
3341 /* If edge is already improbably or cold, just return. */
3342 if (e
->probability
<= impossible
? PROB_VERY_UNLIKELY
: 0
3343 && (!impossible
|| !e
->count
))
3345 FOR_EACH_EDGE (e2
, ei
, e
->src
->succs
)
3348 count_sum
+= e2
->count
;
3349 prob_sum
+= e2
->probability
;
3352 /* If there are other edges out of e->src, redistribute probabilitity
3357 = MIN (e
->probability
, impossible
? 0 : PROB_VERY_UNLIKELY
);
3358 if (old_probability
)
3359 e
->count
= RDIV (e
->count
* e
->probability
, old_probability
);
3361 e
->count
= MIN (e
->count
, impossible
? 0 : 1);
3364 gcov_scale
= RDIV ((count_sum
+ old_count
- e
->count
) * REG_BR_PROB_BASE
,
3366 prob_scale
= RDIV ((REG_BR_PROB_BASE
- e
->probability
) * REG_BR_PROB_BASE
,
3368 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3369 fprintf (dump_file
, "Making edge %i->%i %s by redistributing "
3370 "probability to other edges.\n",
3371 e
->src
->index
, e
->dest
->index
,
3372 impossible
? "imposisble" : "cold");
3373 FOR_EACH_EDGE (e2
, ei
, e
->src
->succs
)
3376 e2
->count
= RDIV (e2
->count
* gcov_scale
, REG_BR_PROB_BASE
);
3377 e2
->probability
= RDIV (e2
->probability
* prob_scale
,
3381 /* If all edges out of e->src are unlikely, the basic block itself
3385 e
->probability
= REG_BR_PROB_BASE
;
3387 /* If we did not adjusting, the source basic block has no likely edeges
3388 leaving other direction. In that case force that bb cold, too.
3389 This in general is difficult task to do, but handle special case when
3390 BB has only one predecestor. This is common case when we are updating
3391 after loop transforms. */
3392 if (!prob_sum
&& !count_sum
&& single_pred_p (e
->src
)
3393 && e
->src
->frequency
> (impossible
? 0 : 1))
3395 int old_frequency
= e
->src
->frequency
;
3396 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3397 fprintf (dump_file
, "Making bb %i %s.\n", e
->src
->index
,
3398 impossible
? "imposisble" : "cold");
3399 e
->src
->frequency
= MIN (e
->src
->frequency
, impossible
? 0 : 1);
3400 e
->src
->count
= e
->count
= RDIV (e
->src
->count
* e
->src
->frequency
,
3402 force_edge_cold (single_pred_edge (e
->src
), impossible
);
3404 else if (dump_file
&& (dump_flags
& TDF_DETAILS
)
3405 && maybe_hot_bb_p (cfun
, e
->src
))
3406 fprintf (dump_file
, "Giving up on making bb %i %s.\n", e
->src
->index
,
3407 impossible
? "imposisble" : "cold");