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
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"
44 #include "diagnostic-core.h"
45 #include "gimple-predict.h"
46 #include "fold-const.h"
53 #include "gimple-iterator.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. */
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
,
86 struct loop
*in_loop
= NULL
);
87 static void predict_paths_leading_to_edge (edge
, enum br_predictor
,
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. */
97 const char *const name
; /* Name used in the debugging dumps. */
98 const int hitrate
; /* Expected hitrate used by
99 predict_insn_def call. */
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. */
120 /* Return TRUE if frequency FREQ is considered to be hot. */
123 maybe_hot_frequency_p (struct function
*fun
, int freq
)
125 struct cgraph_node
*node
= cgraph_node::get (fun
->decl
);
127 || !opt_for_fn (fun
->decl
, flag_branch_probabilities
))
129 if (node
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
)
131 if (node
->frequency
== NODE_FREQUENCY_HOT
)
134 if (profile_status_for_fn (fun
) == PROFILE_ABSENT
)
136 if (node
->frequency
== NODE_FREQUENCY_EXECUTED_ONCE
137 && freq
< (ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
* 2 / 3))
139 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
) == 0)
141 if (freq
* PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
)
142 < ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
)
147 static gcov_type min_count
= -1;
149 /* Determine the threshold for hot BB counts. */
152 get_hot_bb_threshold ()
154 gcov_working_set_t
*ws
;
157 ws
= find_working_set (PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE
));
159 min_count
= ws
->min_counter
;
164 /* Set the threshold for hot BB counts. */
167 set_hot_bb_threshold (gcov_type min
)
172 /* Return TRUE if frequency FREQ is considered to be hot. */
175 maybe_hot_count_p (struct function
*fun
, gcov_type count
)
177 if (fun
&& profile_status_for_fn (fun
) != PROFILE_READ
)
179 /* Code executed at most once is not hot. */
180 if (profile_info
->runs
>= count
)
182 return (count
>= get_hot_bb_threshold ());
185 /* Return true in case BB can be CPU intensive and should be optimized
186 for maximal performance. */
189 maybe_hot_bb_p (struct function
*fun
, const_basic_block bb
)
191 gcc_checking_assert (fun
);
192 if (profile_status_for_fn (fun
) == PROFILE_READ
)
193 return maybe_hot_count_p (fun
, bb
->count
);
194 return maybe_hot_frequency_p (fun
, bb
->frequency
);
197 /* Return true in case BB can be CPU intensive and should be optimized
198 for maximal performance. */
201 maybe_hot_edge_p (edge e
)
203 if (profile_status_for_fn (cfun
) == PROFILE_READ
)
204 return maybe_hot_count_p (cfun
, e
->count
);
205 return maybe_hot_frequency_p (cfun
, EDGE_FREQUENCY (e
));
208 /* Return true if profile COUNT and FREQUENCY, or function FUN static
209 node frequency reflects never being executed. */
212 probably_never_executed (struct function
*fun
,
213 gcov_type count
, int frequency
)
215 gcc_checking_assert (fun
);
216 if (profile_status_for_fn (fun
) == PROFILE_READ
)
218 int unlikely_count_fraction
= PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION
);
219 if (count
* unlikely_count_fraction
>= profile_info
->runs
)
223 if (!ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
)
225 if (ENTRY_BLOCK_PTR_FOR_FN (fun
)->count
)
227 gcov_type computed_count
;
228 /* Check for possibility of overflow, in which case entry bb count
229 is large enough to do the division first without losing much
231 if (ENTRY_BLOCK_PTR_FOR_FN (fun
)->count
< REG_BR_PROB_BASE
*
234 gcov_type scaled_count
235 = frequency
* ENTRY_BLOCK_PTR_FOR_FN (fun
)->count
*
236 unlikely_count_fraction
;
237 computed_count
= RDIV (scaled_count
,
238 ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
);
242 computed_count
= RDIV (ENTRY_BLOCK_PTR_FOR_FN (fun
)->count
,
243 ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
);
244 computed_count
*= frequency
* unlikely_count_fraction
;
246 if (computed_count
>= profile_info
->runs
)
251 if ((!profile_info
|| !(opt_for_fn (fun
->decl
, flag_branch_probabilities
)))
252 && (cgraph_node::get (fun
->decl
)->frequency
253 == NODE_FREQUENCY_UNLIKELY_EXECUTED
))
259 /* Return true in case BB is probably never executed. */
262 probably_never_executed_bb_p (struct function
*fun
, const_basic_block bb
)
264 return probably_never_executed (fun
, bb
->count
, bb
->frequency
);
268 /* Return true in case edge E is probably never executed. */
271 probably_never_executed_edge_p (struct function
*fun
, edge e
)
273 return probably_never_executed (fun
, e
->count
, EDGE_FREQUENCY (e
));
276 /* Return true when current function should always be optimized for size. */
279 optimize_function_for_size_p (struct function
*fun
)
281 if (!fun
|| !fun
->decl
)
282 return optimize_size
;
283 cgraph_node
*n
= cgraph_node::get (fun
->decl
);
284 return n
&& n
->optimize_for_size_p ();
287 /* Return true when current function should always be optimized for speed. */
290 optimize_function_for_speed_p (struct function
*fun
)
292 return !optimize_function_for_size_p (fun
);
295 /* Return the optimization type that should be used for the function FUN. */
298 function_optimization_type (struct function
*fun
)
300 return (optimize_function_for_speed_p (fun
)
302 : OPTIMIZE_FOR_SIZE
);
305 /* Return TRUE when BB should be optimized for size. */
308 optimize_bb_for_size_p (const_basic_block bb
)
310 return (optimize_function_for_size_p (cfun
)
311 || (bb
&& !maybe_hot_bb_p (cfun
, bb
)));
314 /* Return TRUE when BB should be optimized for speed. */
317 optimize_bb_for_speed_p (const_basic_block bb
)
319 return !optimize_bb_for_size_p (bb
);
322 /* Return the optimization type that should be used for block BB. */
325 bb_optimization_type (const_basic_block bb
)
327 return (optimize_bb_for_speed_p (bb
)
329 : OPTIMIZE_FOR_SIZE
);
332 /* Return TRUE when BB should be optimized for size. */
335 optimize_edge_for_size_p (edge e
)
337 return optimize_function_for_size_p (cfun
) || !maybe_hot_edge_p (e
);
340 /* Return TRUE when BB should be optimized for speed. */
343 optimize_edge_for_speed_p (edge e
)
345 return !optimize_edge_for_size_p (e
);
348 /* Return TRUE when BB should be optimized for size. */
351 optimize_insn_for_size_p (void)
353 return optimize_function_for_size_p (cfun
) || !crtl
->maybe_hot_insn_p
;
356 /* Return TRUE when BB should be optimized for speed. */
359 optimize_insn_for_speed_p (void)
361 return !optimize_insn_for_size_p ();
364 /* Return TRUE when LOOP should be optimized for size. */
367 optimize_loop_for_size_p (struct loop
*loop
)
369 return optimize_bb_for_size_p (loop
->header
);
372 /* Return TRUE when LOOP should be optimized for speed. */
375 optimize_loop_for_speed_p (struct loop
*loop
)
377 return optimize_bb_for_speed_p (loop
->header
);
380 /* Return TRUE when LOOP nest should be optimized for speed. */
383 optimize_loop_nest_for_speed_p (struct loop
*loop
)
385 struct loop
*l
= loop
;
386 if (optimize_loop_for_speed_p (loop
))
389 while (l
&& l
!= loop
)
391 if (optimize_loop_for_speed_p (l
))
399 while (l
!= loop
&& !l
->next
)
408 /* Return TRUE when LOOP nest should be optimized for size. */
411 optimize_loop_nest_for_size_p (struct loop
*loop
)
413 return !optimize_loop_nest_for_speed_p (loop
);
416 /* Return true when edge E is likely to be well predictable by branch
420 predictable_edge_p (edge e
)
422 if (profile_status_for_fn (cfun
) == PROFILE_ABSENT
)
425 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME
) * REG_BR_PROB_BASE
/ 100)
426 || (REG_BR_PROB_BASE
- e
->probability
427 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME
) * REG_BR_PROB_BASE
/ 100))
433 /* Set RTL expansion for BB profile. */
436 rtl_profile_for_bb (basic_block bb
)
438 crtl
->maybe_hot_insn_p
= maybe_hot_bb_p (cfun
, bb
);
441 /* Set RTL expansion for edge profile. */
444 rtl_profile_for_edge (edge e
)
446 crtl
->maybe_hot_insn_p
= maybe_hot_edge_p (e
);
449 /* Set RTL expansion to default mode (i.e. when profile info is not known). */
451 default_rtl_profile (void)
453 crtl
->maybe_hot_insn_p
= true;
456 /* Return true if the one of outgoing edges is already predicted by
460 rtl_predicted_by_p (const_basic_block bb
, enum br_predictor predictor
)
463 if (!INSN_P (BB_END (bb
)))
465 for (note
= REG_NOTES (BB_END (bb
)); note
; note
= XEXP (note
, 1))
466 if (REG_NOTE_KIND (note
) == REG_BR_PRED
467 && INTVAL (XEXP (XEXP (note
, 0), 0)) == (int)predictor
)
472 /* Structure representing predictions in tree level. */
474 struct edge_prediction
{
475 struct edge_prediction
*ep_next
;
477 enum br_predictor ep_predictor
;
481 /* This map contains for a basic block the list of predictions for the
484 static hash_map
<const_basic_block
, edge_prediction
*> *bb_predictions
;
486 /* Return true if the one of outgoing edges is already predicted by
490 gimple_predicted_by_p (const_basic_block bb
, enum br_predictor predictor
)
492 struct edge_prediction
*i
;
493 edge_prediction
**preds
= bb_predictions
->get (bb
);
498 for (i
= *preds
; i
; i
= i
->ep_next
)
499 if (i
->ep_predictor
== predictor
)
504 /* Return true if the one of outgoing edges is already predicted by
505 PREDICTOR for edge E predicted as TAKEN. */
508 edge_predicted_by_p (edge e
, enum br_predictor predictor
, bool taken
)
510 struct edge_prediction
*i
;
511 basic_block bb
= e
->src
;
512 edge_prediction
**preds
= bb_predictions
->get (bb
);
516 int probability
= predictor_info
[(int) predictor
].hitrate
;
519 probability
= REG_BR_PROB_BASE
- probability
;
521 for (i
= *preds
; i
; i
= i
->ep_next
)
522 if (i
->ep_predictor
== predictor
524 && i
->ep_probability
== probability
)
529 /* Return true when the probability of edge is reliable.
531 The profile guessing code is good at predicting branch outcome (ie.
532 taken/not taken), that is predicted right slightly over 75% of time.
533 It is however notoriously poor on predicting the probability itself.
534 In general the profile appear a lot flatter (with probabilities closer
535 to 50%) than the reality so it is bad idea to use it to drive optimization
536 such as those disabling dynamic branch prediction for well predictable
539 There are two exceptions - edges leading to noreturn edges and edges
540 predicted by number of iterations heuristics are predicted well. This macro
541 should be able to distinguish those, but at the moment it simply check for
542 noreturn heuristic that is only one giving probability over 99% or bellow
543 1%. In future we might want to propagate reliability information across the
544 CFG if we find this information useful on multiple places. */
546 probability_reliable_p (int prob
)
548 return (profile_status_for_fn (cfun
) == PROFILE_READ
549 || (profile_status_for_fn (cfun
) == PROFILE_GUESSED
550 && (prob
<= HITRATE (1) || prob
>= HITRATE (99))));
553 /* Same predicate as above, working on edges. */
555 edge_probability_reliable_p (const_edge e
)
557 return probability_reliable_p (e
->probability
);
560 /* Same predicate as edge_probability_reliable_p, working on notes. */
562 br_prob_note_reliable_p (const_rtx note
)
564 gcc_assert (REG_NOTE_KIND (note
) == REG_BR_PROB
);
565 return probability_reliable_p (XINT (note
, 0));
569 predict_insn (rtx_insn
*insn
, enum br_predictor predictor
, int probability
)
571 gcc_assert (any_condjump_p (insn
));
572 if (!flag_guess_branch_prob
)
575 add_reg_note (insn
, REG_BR_PRED
,
576 gen_rtx_CONCAT (VOIDmode
,
577 GEN_INT ((int) predictor
),
578 GEN_INT ((int) probability
)));
581 /* Predict insn by given predictor. */
584 predict_insn_def (rtx_insn
*insn
, enum br_predictor predictor
,
585 enum prediction taken
)
587 int probability
= predictor_info
[(int) predictor
].hitrate
;
590 probability
= REG_BR_PROB_BASE
- probability
;
592 predict_insn (insn
, predictor
, probability
);
595 /* Predict edge E with given probability if possible. */
598 rtl_predict_edge (edge e
, enum br_predictor predictor
, int probability
)
601 last_insn
= BB_END (e
->src
);
603 /* We can store the branch prediction information only about
604 conditional jumps. */
605 if (!any_condjump_p (last_insn
))
608 /* We always store probability of branching. */
609 if (e
->flags
& EDGE_FALLTHRU
)
610 probability
= REG_BR_PROB_BASE
- probability
;
612 predict_insn (last_insn
, predictor
, probability
);
615 /* Predict edge E with the given PROBABILITY. */
617 gimple_predict_edge (edge e
, enum br_predictor predictor
, int probability
)
619 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
620 && EDGE_COUNT (e
->src
->succs
) > 1
621 && flag_guess_branch_prob
624 struct edge_prediction
*i
= XNEW (struct edge_prediction
);
625 edge_prediction
*&preds
= bb_predictions
->get_or_insert (e
->src
);
629 i
->ep_probability
= probability
;
630 i
->ep_predictor
= predictor
;
635 /* Filter edge predictions PREDS by a function FILTER. DATA are passed
636 to the filter function. */
639 filter_predictions (edge_prediction
**preds
,
640 bool (*filter
) (edge_prediction
*, void *), void *data
)
647 struct edge_prediction
**prediction
= preds
;
648 struct edge_prediction
*next
;
652 if ((*filter
) (*prediction
, data
))
653 prediction
= &((*prediction
)->ep_next
);
656 next
= (*prediction
)->ep_next
;
664 /* Filter function predicate that returns true for a edge predicate P
665 if its edge is equal to DATA. */
668 equal_edge_p (edge_prediction
*p
, void *data
)
670 return p
->ep_edge
== (edge
)data
;
673 /* Remove all predictions on given basic block that are attached
676 remove_predictions_associated_with_edge (edge e
)
681 edge_prediction
**preds
= bb_predictions
->get (e
->src
);
682 filter_predictions (preds
, equal_edge_p
, e
);
685 /* Clears the list of predictions stored for BB. */
688 clear_bb_predictions (basic_block bb
)
690 edge_prediction
**preds
= bb_predictions
->get (bb
);
691 struct edge_prediction
*pred
, *next
;
696 for (pred
= *preds
; pred
; pred
= next
)
698 next
= pred
->ep_next
;
704 /* Return true when we can store prediction on insn INSN.
705 At the moment we represent predictions only on conditional
706 jumps, not at computed jump or other complicated cases. */
708 can_predict_insn_p (const rtx_insn
*insn
)
710 return (JUMP_P (insn
)
711 && any_condjump_p (insn
)
712 && EDGE_COUNT (BLOCK_FOR_INSN (insn
)->succs
) >= 2);
715 /* Predict edge E by given predictor if possible. */
718 predict_edge_def (edge e
, enum br_predictor predictor
,
719 enum prediction taken
)
721 int probability
= predictor_info
[(int) predictor
].hitrate
;
724 probability
= REG_BR_PROB_BASE
- probability
;
726 predict_edge (e
, predictor
, probability
);
729 /* Invert all branch predictions or probability notes in the INSN. This needs
730 to be done each time we invert the condition used by the jump. */
733 invert_br_probabilities (rtx insn
)
737 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
738 if (REG_NOTE_KIND (note
) == REG_BR_PROB
)
739 XINT (note
, 0) = REG_BR_PROB_BASE
- XINT (note
, 0);
740 else if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
741 XEXP (XEXP (note
, 0), 1)
742 = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (XEXP (note
, 0), 1)));
745 /* Dump information about the branch prediction to the output file. */
748 dump_prediction (FILE *file
, enum br_predictor predictor
, int probability
,
749 basic_block bb
, enum predictor_reason reason
= REASON_NONE
,
759 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
760 if (! (e
->flags
& EDGE_FALLTHRU
))
763 char edge_info_str
[128];
765 sprintf (edge_info_str
, " of edge %d->%d", ep_edge
->src
->index
,
766 ep_edge
->dest
->index
);
768 edge_info_str
[0] = '\0';
770 fprintf (file
, " %s heuristics%s%s: %.1f%%",
771 predictor_info
[predictor
].name
,
772 edge_info_str
, reason_messages
[reason
],
773 probability
* 100.0 / REG_BR_PROB_BASE
);
777 fprintf (file
, " exec %" PRId64
, bb
->count
);
780 fprintf (file
, " hit %" PRId64
, e
->count
);
781 fprintf (file
, " (%.1f%%)", e
->count
* 100.0 / bb
->count
);
785 fprintf (file
, "\n");
788 /* We can not predict the probabilities of outgoing edges of bb. Set them
789 evenly and hope for the best. If UNLIKELY_EDGES is not null, distribute
790 even probability for all edges not mentioned in the set. These edges
791 are given PROB_VERY_UNLIKELY probability. */
794 set_even_probabilities (basic_block bb
,
795 hash_set
<edge
> *unlikely_edges
= NULL
)
801 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
802 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
805 /* Make the distribution even if all edges are unlikely. */
806 unsigned unlikely_count
= unlikely_edges
? unlikely_edges
->elements () : 0;
807 if (unlikely_count
== nedges
)
809 unlikely_edges
= NULL
;
813 unsigned c
= nedges
- unlikely_count
;
815 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
816 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
818 if (unlikely_edges
!= NULL
&& unlikely_edges
->contains (e
))
819 e
->probability
= PROB_VERY_UNLIKELY
;
821 e
->probability
= (REG_BR_PROB_BASE
+ c
/ 2) / c
;
827 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
828 note if not already present. Remove now useless REG_BR_PRED notes. */
831 combine_predictions_for_insn (rtx_insn
*insn
, basic_block bb
)
836 int best_probability
= PROB_EVEN
;
837 enum br_predictor best_predictor
= END_PREDICTORS
;
838 int combined_probability
= REG_BR_PROB_BASE
/ 2;
840 bool first_match
= false;
843 if (!can_predict_insn_p (insn
))
845 set_even_probabilities (bb
);
849 prob_note
= find_reg_note (insn
, REG_BR_PROB
, 0);
850 pnote
= ®_NOTES (insn
);
852 fprintf (dump_file
, "Predictions for insn %i bb %i\n", INSN_UID (insn
),
855 /* We implement "first match" heuristics and use probability guessed
856 by predictor with smallest index. */
857 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
858 if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
860 enum br_predictor predictor
= ((enum br_predictor
)
861 INTVAL (XEXP (XEXP (note
, 0), 0)));
862 int probability
= INTVAL (XEXP (XEXP (note
, 0), 1));
865 if (best_predictor
> predictor
866 && predictor_info
[predictor
].flags
& PRED_FLAG_FIRST_MATCH
)
867 best_probability
= probability
, best_predictor
= predictor
;
869 d
= (combined_probability
* probability
870 + (REG_BR_PROB_BASE
- combined_probability
)
871 * (REG_BR_PROB_BASE
- probability
));
873 /* Use FP math to avoid overflows of 32bit integers. */
875 /* If one probability is 0% and one 100%, avoid division by zero. */
876 combined_probability
= REG_BR_PROB_BASE
/ 2;
878 combined_probability
= (((double) combined_probability
) * probability
879 * REG_BR_PROB_BASE
/ d
+ 0.5);
882 /* Decide which heuristic to use. In case we didn't match anything,
883 use no_prediction heuristic, in case we did match, use either
884 first match or Dempster-Shaffer theory depending on the flags. */
886 if (best_predictor
!= END_PREDICTORS
)
890 dump_prediction (dump_file
, PRED_NO_PREDICTION
,
891 combined_probability
, bb
);
895 dump_prediction (dump_file
, PRED_DS_THEORY
, combined_probability
,
896 bb
, !first_match
? REASON_NONE
: REASON_IGNORED
);
898 dump_prediction (dump_file
, PRED_FIRST_MATCH
, best_probability
,
899 bb
, first_match
? REASON_NONE
: REASON_IGNORED
);
903 combined_probability
= best_probability
;
904 dump_prediction (dump_file
, PRED_COMBINED
, combined_probability
, bb
);
908 if (REG_NOTE_KIND (*pnote
) == REG_BR_PRED
)
910 enum br_predictor predictor
= ((enum br_predictor
)
911 INTVAL (XEXP (XEXP (*pnote
, 0), 0)));
912 int probability
= INTVAL (XEXP (XEXP (*pnote
, 0), 1));
914 dump_prediction (dump_file
, predictor
, probability
, bb
,
915 (!first_match
|| best_predictor
== predictor
)
916 ? REASON_NONE
: REASON_IGNORED
);
917 *pnote
= XEXP (*pnote
, 1);
920 pnote
= &XEXP (*pnote
, 1);
925 add_int_reg_note (insn
, REG_BR_PROB
, combined_probability
);
927 /* Save the prediction into CFG in case we are seeing non-degenerated
929 if (!single_succ_p (bb
))
931 BRANCH_EDGE (bb
)->probability
= combined_probability
;
932 FALLTHRU_EDGE (bb
)->probability
933 = REG_BR_PROB_BASE
- combined_probability
;
936 else if (!single_succ_p (bb
))
938 int prob
= XINT (prob_note
, 0);
940 BRANCH_EDGE (bb
)->probability
= prob
;
941 FALLTHRU_EDGE (bb
)->probability
= REG_BR_PROB_BASE
- prob
;
944 single_succ_edge (bb
)->probability
= REG_BR_PROB_BASE
;
947 /* Edge prediction hash traits. */
949 struct predictor_hash
: pointer_hash
<edge_prediction
>
952 static inline hashval_t
hash (const edge_prediction
*);
953 static inline bool equal (const edge_prediction
*, const edge_prediction
*);
956 /* Calculate hash value of an edge prediction P based on predictor and
957 normalized probability. */
960 predictor_hash::hash (const edge_prediction
*p
)
962 inchash::hash hstate
;
963 hstate
.add_int (p
->ep_predictor
);
965 int prob
= p
->ep_probability
;
966 if (prob
> REG_BR_PROB_BASE
/ 2)
967 prob
= REG_BR_PROB_BASE
- prob
;
969 hstate
.add_int (prob
);
971 return hstate
.end ();
974 /* Return true whether edge predictions P1 and P2 use the same predictor and
975 have equal (or opposed probability). */
978 predictor_hash::equal (const edge_prediction
*p1
, const edge_prediction
*p2
)
980 return (p1
->ep_predictor
== p2
->ep_predictor
981 && (p1
->ep_probability
== p2
->ep_probability
982 || p1
->ep_probability
== REG_BR_PROB_BASE
- p2
->ep_probability
));
985 struct predictor_hash_traits
: predictor_hash
,
986 typed_noop_remove
<edge_prediction
*> {};
988 /* Return true if edge prediction P is not in DATA hash set. */
991 not_removed_prediction_p (edge_prediction
*p
, void *data
)
993 hash_set
<edge_prediction
*> *remove
= (hash_set
<edge_prediction
*> *) data
;
994 return !remove
->contains (p
);
997 /* Prune predictions for a basic block BB. Currently we do following
1000 1) remove duplicate prediction that is guessed with the same probability
1001 (different than 1/2) to both edge
1002 2) remove duplicates for a prediction that belongs with the same probability
1008 prune_predictions_for_bb (basic_block bb
)
1010 edge_prediction
**preds
= bb_predictions
->get (bb
);
1014 hash_table
<predictor_hash_traits
> s (13);
1015 hash_set
<edge_prediction
*> remove
;
1017 /* Step 1: identify predictors that should be removed. */
1018 for (edge_prediction
*pred
= *preds
; pred
; pred
= pred
->ep_next
)
1020 edge_prediction
*existing
= s
.find (pred
);
1023 if (pred
->ep_edge
== existing
->ep_edge
1024 && pred
->ep_probability
== existing
->ep_probability
)
1026 /* Remove a duplicate predictor. */
1027 dump_prediction (dump_file
, pred
->ep_predictor
,
1028 pred
->ep_probability
, bb
,
1029 REASON_SINGLE_EDGE_DUPLICATE
, pred
->ep_edge
);
1033 else if (pred
->ep_edge
!= existing
->ep_edge
1034 && pred
->ep_probability
== existing
->ep_probability
1035 && pred
->ep_probability
!= REG_BR_PROB_BASE
/ 2)
1037 /* Remove both predictors as they predict the same
1039 dump_prediction (dump_file
, existing
->ep_predictor
,
1040 pred
->ep_probability
, bb
,
1041 REASON_EDGE_PAIR_DUPLICATE
,
1043 dump_prediction (dump_file
, pred
->ep_predictor
,
1044 pred
->ep_probability
, bb
,
1045 REASON_EDGE_PAIR_DUPLICATE
,
1048 remove
.add (existing
);
1053 edge_prediction
**slot2
= s
.find_slot (pred
, INSERT
);
1057 /* Step 2: Remove predictors. */
1058 filter_predictions (preds
, not_removed_prediction_p
, &remove
);
1062 /* Combine predictions into single probability and store them into CFG.
1063 Remove now useless prediction entries.
1064 If DRY_RUN is set, only produce dumps and do not modify profile. */
1067 combine_predictions_for_bb (basic_block bb
, bool dry_run
)
1069 int best_probability
= PROB_EVEN
;
1070 enum br_predictor best_predictor
= END_PREDICTORS
;
1071 int combined_probability
= REG_BR_PROB_BASE
/ 2;
1073 bool first_match
= false;
1075 struct edge_prediction
*pred
;
1077 edge e
, first
= NULL
, second
= NULL
;
1080 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1081 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
1084 if (first
&& !second
)
1090 /* When there is no successor or only one choice, prediction is easy.
1092 When we have a basic block with more than 2 successors, the situation
1093 is more complicated as DS theory cannot be used literally.
1094 More precisely, let's assume we predicted edge e1 with probability p1,
1095 thus: m1({b1}) = p1. As we're going to combine more than 2 edges, we
1096 need to find probability of e.g. m1({b2}), which we don't know.
1097 The only approximation is to equally distribute 1-p1 to all edges
1100 According to numbers we've got from SPEC2006 benchark, there's only
1101 one interesting reliable predictor (noreturn call), which can be
1102 handled with a bit easier approach. */
1105 hash_set
<edge
> unlikely_edges (4);
1107 /* Identify all edges that have a probability close to very unlikely.
1108 Doing the approach for very unlikely doesn't worth for doing as
1109 there's no such probability in SPEC2006 benchmark. */
1110 edge_prediction
**preds
= bb_predictions
->get (bb
);
1112 for (pred
= *preds
; pred
; pred
= pred
->ep_next
)
1113 if (pred
->ep_probability
<= PROB_VERY_UNLIKELY
)
1114 unlikely_edges
.add (pred
->ep_edge
);
1116 if (!bb
->count
&& !dry_run
)
1117 set_even_probabilities (bb
, &unlikely_edges
);
1118 clear_bb_predictions (bb
);
1121 fprintf (dump_file
, "Predictions for bb %i\n", bb
->index
);
1122 if (unlikely_edges
.elements () == 0)
1124 "%i edges in bb %i predicted to even probabilities\n",
1129 "%i edges in bb %i predicted with some unlikely edges\n",
1131 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1132 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
1133 dump_prediction (dump_file
, PRED_COMBINED
, e
->probability
,
1134 bb
, REASON_NONE
, e
);
1141 fprintf (dump_file
, "Predictions for bb %i\n", bb
->index
);
1143 prune_predictions_for_bb (bb
);
1145 edge_prediction
**preds
= bb_predictions
->get (bb
);
1149 /* We implement "first match" heuristics and use probability guessed
1150 by predictor with smallest index. */
1151 for (pred
= *preds
; pred
; pred
= pred
->ep_next
)
1153 enum br_predictor predictor
= pred
->ep_predictor
;
1154 int probability
= pred
->ep_probability
;
1156 if (pred
->ep_edge
!= first
)
1157 probability
= REG_BR_PROB_BASE
- probability
;
1160 /* First match heuristics would be widly confused if we predicted
1162 if (best_predictor
> predictor
1163 && predictor_info
[predictor
].flags
& PRED_FLAG_FIRST_MATCH
)
1165 struct edge_prediction
*pred2
;
1166 int prob
= probability
;
1168 for (pred2
= (struct edge_prediction
*) *preds
;
1169 pred2
; pred2
= pred2
->ep_next
)
1170 if (pred2
!= pred
&& pred2
->ep_predictor
== pred
->ep_predictor
)
1172 int probability2
= pred2
->ep_probability
;
1174 if (pred2
->ep_edge
!= first
)
1175 probability2
= REG_BR_PROB_BASE
- probability2
;
1177 if ((probability
< REG_BR_PROB_BASE
/ 2) !=
1178 (probability2
< REG_BR_PROB_BASE
/ 2))
1181 /* If the same predictor later gave better result, go for it! */
1182 if ((probability
>= REG_BR_PROB_BASE
/ 2 && (probability2
> probability
))
1183 || (probability
<= REG_BR_PROB_BASE
/ 2 && (probability2
< probability
)))
1184 prob
= probability2
;
1187 best_probability
= prob
, best_predictor
= predictor
;
1190 d
= (combined_probability
* probability
1191 + (REG_BR_PROB_BASE
- combined_probability
)
1192 * (REG_BR_PROB_BASE
- probability
));
1194 /* Use FP math to avoid overflows of 32bit integers. */
1196 /* If one probability is 0% and one 100%, avoid division by zero. */
1197 combined_probability
= REG_BR_PROB_BASE
/ 2;
1199 combined_probability
= (((double) combined_probability
)
1201 * REG_BR_PROB_BASE
/ d
+ 0.5);
1205 /* Decide which heuristic to use. In case we didn't match anything,
1206 use no_prediction heuristic, in case we did match, use either
1207 first match or Dempster-Shaffer theory depending on the flags. */
1209 if (best_predictor
!= END_PREDICTORS
)
1213 dump_prediction (dump_file
, PRED_NO_PREDICTION
, combined_probability
, bb
);
1217 dump_prediction (dump_file
, PRED_DS_THEORY
, combined_probability
, bb
,
1218 !first_match
? REASON_NONE
: REASON_IGNORED
);
1220 dump_prediction (dump_file
, PRED_FIRST_MATCH
, best_probability
, bb
,
1221 first_match
? REASON_NONE
: REASON_IGNORED
);
1225 combined_probability
= best_probability
;
1226 dump_prediction (dump_file
, PRED_COMBINED
, combined_probability
, bb
);
1230 for (pred
= (struct edge_prediction
*) *preds
; pred
; pred
= pred
->ep_next
)
1232 enum br_predictor predictor
= pred
->ep_predictor
;
1233 int probability
= pred
->ep_probability
;
1235 dump_prediction (dump_file
, predictor
, probability
, bb
,
1236 (!first_match
|| best_predictor
== predictor
)
1237 ? REASON_NONE
: REASON_IGNORED
, pred
->ep_edge
);
1240 clear_bb_predictions (bb
);
1242 if (!bb
->count
&& !dry_run
)
1244 first
->probability
= combined_probability
;
1245 second
->probability
= REG_BR_PROB_BASE
- combined_probability
;
1249 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1250 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1252 T1 and T2 should be one of the following cases:
1253 1. T1 is SSA_NAME, T2 is NULL
1254 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1255 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1258 strips_small_constant (tree t1
, tree t2
)
1265 else if (TREE_CODE (t1
) == SSA_NAME
)
1267 else if (tree_fits_shwi_p (t1
))
1268 value
= tree_to_shwi (t1
);
1274 else if (tree_fits_shwi_p (t2
))
1275 value
= tree_to_shwi (t2
);
1276 else if (TREE_CODE (t2
) == SSA_NAME
)
1284 if (value
<= 4 && value
>= -4)
1290 /* Return the SSA_NAME in T or T's operands.
1291 Return NULL if SSA_NAME cannot be found. */
1294 get_base_value (tree t
)
1296 if (TREE_CODE (t
) == SSA_NAME
)
1299 if (!BINARY_CLASS_P (t
))
1302 switch (TREE_OPERAND_LENGTH (t
))
1305 return strips_small_constant (TREE_OPERAND (t
, 0), NULL
);
1307 return strips_small_constant (TREE_OPERAND (t
, 0),
1308 TREE_OPERAND (t
, 1));
1314 /* Check the compare STMT in LOOP. If it compares an induction
1315 variable to a loop invariant, return true, and save
1316 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1317 Otherwise return false and set LOOP_INVAIANT to NULL. */
1320 is_comparison_with_loop_invariant_p (gcond
*stmt
, struct loop
*loop
,
1321 tree
*loop_invariant
,
1322 enum tree_code
*compare_code
,
1326 tree op0
, op1
, bound
, base
;
1328 enum tree_code code
;
1331 code
= gimple_cond_code (stmt
);
1332 *loop_invariant
= NULL
;
1348 op0
= gimple_cond_lhs (stmt
);
1349 op1
= gimple_cond_rhs (stmt
);
1351 if ((TREE_CODE (op0
) != SSA_NAME
&& TREE_CODE (op0
) != INTEGER_CST
)
1352 || (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op1
) != INTEGER_CST
))
1354 if (!simple_iv (loop
, loop_containing_stmt (stmt
), op0
, &iv0
, true))
1356 if (!simple_iv (loop
, loop_containing_stmt (stmt
), op1
, &iv1
, true))
1358 if (TREE_CODE (iv0
.step
) != INTEGER_CST
1359 || TREE_CODE (iv1
.step
) != INTEGER_CST
)
1361 if ((integer_zerop (iv0
.step
) && integer_zerop (iv1
.step
))
1362 || (!integer_zerop (iv0
.step
) && !integer_zerop (iv1
.step
)))
1365 if (integer_zerop (iv0
.step
))
1367 if (code
!= NE_EXPR
&& code
!= EQ_EXPR
)
1368 code
= invert_tree_comparison (code
, false);
1371 if (tree_fits_shwi_p (iv1
.step
))
1380 if (tree_fits_shwi_p (iv0
.step
))
1386 if (TREE_CODE (bound
) != INTEGER_CST
)
1387 bound
= get_base_value (bound
);
1390 if (TREE_CODE (base
) != INTEGER_CST
)
1391 base
= get_base_value (base
);
1395 *loop_invariant
= bound
;
1396 *compare_code
= code
;
1398 *loop_iv_base
= base
;
1402 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1405 expr_coherent_p (tree t1
, tree t2
)
1408 tree ssa_name_1
= NULL
;
1409 tree ssa_name_2
= NULL
;
1411 gcc_assert (TREE_CODE (t1
) == SSA_NAME
|| TREE_CODE (t1
) == INTEGER_CST
);
1412 gcc_assert (TREE_CODE (t2
) == SSA_NAME
|| TREE_CODE (t2
) == INTEGER_CST
);
1417 if (TREE_CODE (t1
) == INTEGER_CST
&& TREE_CODE (t2
) == INTEGER_CST
)
1419 if (TREE_CODE (t1
) == INTEGER_CST
|| TREE_CODE (t2
) == INTEGER_CST
)
1422 /* Check to see if t1 is expressed/defined with t2. */
1423 stmt
= SSA_NAME_DEF_STMT (t1
);
1424 gcc_assert (stmt
!= NULL
);
1425 if (is_gimple_assign (stmt
))
1427 ssa_name_1
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_USE
);
1428 if (ssa_name_1
&& ssa_name_1
== t2
)
1432 /* Check to see if t2 is expressed/defined with t1. */
1433 stmt
= SSA_NAME_DEF_STMT (t2
);
1434 gcc_assert (stmt
!= NULL
);
1435 if (is_gimple_assign (stmt
))
1437 ssa_name_2
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_USE
);
1438 if (ssa_name_2
&& ssa_name_2
== t1
)
1442 /* Compare if t1 and t2's def_stmts are identical. */
1443 if (ssa_name_2
!= NULL
&& ssa_name_1
== ssa_name_2
)
1449 /* Return true if E is predicted by one of loop heuristics. */
1452 predicted_by_loop_heuristics_p (basic_block bb
)
1454 struct edge_prediction
*i
;
1455 edge_prediction
**preds
= bb_predictions
->get (bb
);
1460 for (i
= *preds
; i
; i
= i
->ep_next
)
1461 if (i
->ep_predictor
== PRED_LOOP_ITERATIONS_GUESSED
1462 || i
->ep_predictor
== PRED_LOOP_ITERATIONS_MAX
1463 || i
->ep_predictor
== PRED_LOOP_ITERATIONS
1464 || i
->ep_predictor
== PRED_LOOP_EXIT
1465 || i
->ep_predictor
== PRED_LOOP_EXIT_WITH_RECURSION
1466 || i
->ep_predictor
== PRED_LOOP_EXTRA_EXIT
)
1471 /* Predict branch probability of BB when BB contains a branch that compares
1472 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1473 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1476 for (int i = 0; i < bound; i++) {
1483 In this loop, we will predict the branch inside the loop to be taken. */
1486 predict_iv_comparison (struct loop
*loop
, basic_block bb
,
1487 tree loop_bound_var
,
1488 tree loop_iv_base_var
,
1489 enum tree_code loop_bound_code
,
1490 int loop_bound_step
)
1493 tree compare_var
, compare_base
;
1494 enum tree_code compare_code
;
1495 tree compare_step_var
;
1499 if (predicted_by_loop_heuristics_p (bb
))
1502 stmt
= last_stmt (bb
);
1503 if (!stmt
|| gimple_code (stmt
) != GIMPLE_COND
)
1505 if (!is_comparison_with_loop_invariant_p (as_a
<gcond
*> (stmt
),
1512 /* Find the taken edge. */
1513 FOR_EACH_EDGE (then_edge
, ei
, bb
->succs
)
1514 if (then_edge
->flags
& EDGE_TRUE_VALUE
)
1517 /* When comparing an IV to a loop invariant, NE is more likely to be
1518 taken while EQ is more likely to be not-taken. */
1519 if (compare_code
== NE_EXPR
)
1521 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1524 else if (compare_code
== EQ_EXPR
)
1526 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1530 if (!expr_coherent_p (loop_iv_base_var
, compare_base
))
1533 /* If loop bound, base and compare bound are all constants, we can
1534 calculate the probability directly. */
1535 if (tree_fits_shwi_p (loop_bound_var
)
1536 && tree_fits_shwi_p (compare_var
)
1537 && tree_fits_shwi_p (compare_base
))
1540 bool overflow
, overall_overflow
= false;
1541 widest_int compare_count
, tem
;
1543 /* (loop_bound - base) / compare_step */
1544 tem
= wi::sub (wi::to_widest (loop_bound_var
),
1545 wi::to_widest (compare_base
), SIGNED
, &overflow
);
1546 overall_overflow
|= overflow
;
1547 widest_int loop_count
= wi::div_trunc (tem
,
1548 wi::to_widest (compare_step_var
),
1550 overall_overflow
|= overflow
;
1552 if (!wi::neg_p (wi::to_widest (compare_step_var
))
1553 ^ (compare_code
== LT_EXPR
|| compare_code
== LE_EXPR
))
1555 /* (loop_bound - compare_bound) / compare_step */
1556 tem
= wi::sub (wi::to_widest (loop_bound_var
),
1557 wi::to_widest (compare_var
), SIGNED
, &overflow
);
1558 overall_overflow
|= overflow
;
1559 compare_count
= wi::div_trunc (tem
, wi::to_widest (compare_step_var
),
1561 overall_overflow
|= overflow
;
1565 /* (compare_bound - base) / compare_step */
1566 tem
= wi::sub (wi::to_widest (compare_var
),
1567 wi::to_widest (compare_base
), SIGNED
, &overflow
);
1568 overall_overflow
|= overflow
;
1569 compare_count
= wi::div_trunc (tem
, wi::to_widest (compare_step_var
),
1571 overall_overflow
|= overflow
;
1573 if (compare_code
== LE_EXPR
|| compare_code
== GE_EXPR
)
1575 if (loop_bound_code
== LE_EXPR
|| loop_bound_code
== GE_EXPR
)
1577 if (wi::neg_p (compare_count
))
1579 if (wi::neg_p (loop_count
))
1581 if (loop_count
== 0)
1583 else if (wi::cmps (compare_count
, loop_count
) == 1)
1584 probability
= REG_BR_PROB_BASE
;
1587 tem
= compare_count
* REG_BR_PROB_BASE
;
1588 tem
= wi::udiv_trunc (tem
, loop_count
);
1589 probability
= tem
.to_uhwi ();
1592 /* FIXME: The branch prediction seems broken. It has only 20% hitrate. */
1593 if (!overall_overflow
)
1594 predict_edge (then_edge
, PRED_LOOP_IV_COMPARE
, probability
);
1599 if (expr_coherent_p (loop_bound_var
, compare_var
))
1601 if ((loop_bound_code
== LT_EXPR
|| loop_bound_code
== LE_EXPR
)
1602 && (compare_code
== LT_EXPR
|| compare_code
== LE_EXPR
))
1603 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1604 else if ((loop_bound_code
== GT_EXPR
|| loop_bound_code
== GE_EXPR
)
1605 && (compare_code
== GT_EXPR
|| compare_code
== GE_EXPR
))
1606 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1607 else if (loop_bound_code
== NE_EXPR
)
1609 /* If the loop backedge condition is "(i != bound)", we do
1610 the comparison based on the step of IV:
1611 * step < 0 : backedge condition is like (i > bound)
1612 * step > 0 : backedge condition is like (i < bound) */
1613 gcc_assert (loop_bound_step
!= 0);
1614 if (loop_bound_step
> 0
1615 && (compare_code
== LT_EXPR
1616 || compare_code
== LE_EXPR
))
1617 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1618 else if (loop_bound_step
< 0
1619 && (compare_code
== GT_EXPR
1620 || compare_code
== GE_EXPR
))
1621 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1623 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1626 /* The branch is predicted not-taken if loop_bound_code is
1627 opposite with compare_code. */
1628 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1630 else if (expr_coherent_p (loop_iv_base_var
, compare_var
))
1633 for (i = s; i < h; i++)
1635 The branch should be predicted taken. */
1636 if (loop_bound_step
> 0
1637 && (compare_code
== GT_EXPR
|| compare_code
== GE_EXPR
))
1638 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1639 else if (loop_bound_step
< 0
1640 && (compare_code
== LT_EXPR
|| compare_code
== LE_EXPR
))
1641 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1643 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1647 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1648 exits are resulted from short-circuit conditions that will generate an
1651 if (foo() || global > 10)
1654 This will be translated into:
1659 if foo() goto BB6 else goto BB5
1661 if global > 10 goto BB6 else goto BB7
1665 iftmp = (PHI 0(BB5), 1(BB6))
1666 if iftmp == 1 goto BB8 else goto BB3
1668 outside of the loop...
1670 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1671 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1672 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1673 exits to predict them using PRED_LOOP_EXTRA_EXIT. */
1676 predict_extra_loop_exits (edge exit_edge
)
1679 bool check_value_one
;
1680 gimple
*lhs_def_stmt
;
1682 tree cmp_rhs
, cmp_lhs
;
1686 last
= last_stmt (exit_edge
->src
);
1689 cmp_stmt
= dyn_cast
<gcond
*> (last
);
1693 cmp_rhs
= gimple_cond_rhs (cmp_stmt
);
1694 cmp_lhs
= gimple_cond_lhs (cmp_stmt
);
1695 if (!TREE_CONSTANT (cmp_rhs
)
1696 || !(integer_zerop (cmp_rhs
) || integer_onep (cmp_rhs
)))
1698 if (TREE_CODE (cmp_lhs
) != SSA_NAME
)
1701 /* If check_value_one is true, only the phi_args with value '1' will lead
1702 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1704 check_value_one
= (((integer_onep (cmp_rhs
))
1705 ^ (gimple_cond_code (cmp_stmt
) == EQ_EXPR
))
1706 ^ ((exit_edge
->flags
& EDGE_TRUE_VALUE
) != 0));
1708 lhs_def_stmt
= SSA_NAME_DEF_STMT (cmp_lhs
);
1712 phi_stmt
= dyn_cast
<gphi
*> (lhs_def_stmt
);
1716 for (i
= 0; i
< gimple_phi_num_args (phi_stmt
); i
++)
1720 tree val
= gimple_phi_arg_def (phi_stmt
, i
);
1721 edge e
= gimple_phi_arg_edge (phi_stmt
, i
);
1723 if (!TREE_CONSTANT (val
) || !(integer_zerop (val
) || integer_onep (val
)))
1725 if ((check_value_one
^ integer_onep (val
)) == 1)
1727 if (EDGE_COUNT (e
->src
->succs
) != 1)
1729 predict_paths_leading_to_edge (e
, PRED_LOOP_EXTRA_EXIT
, NOT_TAKEN
);
1733 FOR_EACH_EDGE (e1
, ei
, e
->src
->preds
)
1734 predict_paths_leading_to_edge (e1
, PRED_LOOP_EXTRA_EXIT
, NOT_TAKEN
);
1739 /* Predict edge probabilities by exploiting loop structure. */
1742 predict_loops (void)
1746 hash_set
<struct loop
*> with_recursion(10);
1748 FOR_EACH_BB_FN (bb
, cfun
)
1750 gimple_stmt_iterator gsi
;
1753 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1754 if (is_gimple_call (gsi_stmt (gsi
))
1755 && (decl
= gimple_call_fndecl (gsi_stmt (gsi
))) != NULL
1756 && recursive_call_p (current_function_decl
, decl
))
1758 loop
= bb
->loop_father
;
1759 while (loop
&& !with_recursion
.add (loop
))
1760 loop
= loop_outer (loop
);
1764 /* Try to predict out blocks in a loop that are not part of a
1766 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
1768 basic_block bb
, *bbs
;
1769 unsigned j
, n_exits
= 0;
1771 struct tree_niter_desc niter_desc
;
1773 struct nb_iter_bound
*nb_iter
;
1774 enum tree_code loop_bound_code
= ERROR_MARK
;
1775 tree loop_bound_step
= NULL
;
1776 tree loop_bound_var
= NULL
;
1777 tree loop_iv_base
= NULL
;
1779 bool recursion
= with_recursion
.contains (loop
);
1781 exits
= get_loop_exit_edges (loop
);
1782 FOR_EACH_VEC_ELT (exits
, j
, ex
)
1783 if (!(ex
->flags
& (EDGE_EH
| EDGE_ABNORMAL_CALL
| EDGE_FAKE
)))
1791 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1792 fprintf (dump_file
, "Predicting loop %i%s with %i exits.\n",
1793 loop
->num
, recursion
? " (with recursion)":"", n_exits
);
1794 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
1795 && max_loop_iterations_int (loop
) >= 0)
1798 "Loop %d iterates at most %i times.\n", loop
->num
,
1799 (int)max_loop_iterations_int (loop
));
1801 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
1802 && likely_max_loop_iterations_int (loop
) >= 0)
1804 fprintf (dump_file
, "Loop %d likely iterates at most %i times.\n",
1805 loop
->num
, (int)likely_max_loop_iterations_int (loop
));
1808 FOR_EACH_VEC_ELT (exits
, j
, ex
)
1811 HOST_WIDE_INT nitercst
;
1812 int max
= PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS
);
1814 enum br_predictor predictor
;
1817 if (ex
->flags
& (EDGE_EH
| EDGE_ABNORMAL_CALL
| EDGE_FAKE
))
1819 /* Loop heuristics do not expect exit conditional to be inside
1820 inner loop. We predict from innermost to outermost loop. */
1821 if (predicted_by_loop_heuristics_p (ex
->src
))
1823 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1824 fprintf (dump_file
, "Skipping exit %i->%i because "
1825 "it is already predicted.\n",
1826 ex
->src
->index
, ex
->dest
->index
);
1829 predict_extra_loop_exits (ex
);
1831 if (number_of_iterations_exit (loop
, ex
, &niter_desc
, false, false))
1832 niter
= niter_desc
.niter
;
1833 if (!niter
|| TREE_CODE (niter_desc
.niter
) != INTEGER_CST
)
1834 niter
= loop_niter_by_eval (loop
, ex
);
1835 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
1836 && TREE_CODE (niter
) == INTEGER_CST
)
1838 fprintf (dump_file
, "Exit %i->%i %d iterates ",
1839 ex
->src
->index
, ex
->dest
->index
,
1841 print_generic_expr (dump_file
, niter
, TDF_SLIM
);
1842 fprintf (dump_file
, " times.\n");
1845 if (TREE_CODE (niter
) == INTEGER_CST
)
1847 if (tree_fits_uhwi_p (niter
)
1849 && compare_tree_int (niter
, max
- 1) == -1)
1850 nitercst
= tree_to_uhwi (niter
) + 1;
1853 predictor
= PRED_LOOP_ITERATIONS
;
1855 /* If we have just one exit and we can derive some information about
1856 the number of iterations of the loop from the statements inside
1857 the loop, use it to predict this exit. */
1858 else if (n_exits
== 1
1859 && estimated_stmt_executions (loop
, &nit
))
1861 if (wi::gtu_p (nit
, max
))
1864 nitercst
= nit
.to_shwi ();
1865 predictor
= PRED_LOOP_ITERATIONS_GUESSED
;
1867 /* If we have likely upper bound, trust it for very small iteration
1868 counts. Such loops would otherwise get mispredicted by standard
1869 LOOP_EXIT heuristics. */
1870 else if (n_exits
== 1
1871 && likely_max_stmt_executions (loop
, &nit
)
1873 RDIV (REG_BR_PROB_BASE
,
1877 ? PRED_LOOP_EXIT_WITH_RECURSION
1878 : PRED_LOOP_EXIT
].hitrate
)))
1880 nitercst
= nit
.to_shwi ();
1881 predictor
= PRED_LOOP_ITERATIONS_MAX
;
1885 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1886 fprintf (dump_file
, "Nothing known about exit %i->%i.\n",
1887 ex
->src
->index
, ex
->dest
->index
);
1891 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1892 fprintf (dump_file
, "Recording prediction to %i iterations by %s.\n",
1893 (int)nitercst
, predictor_info
[predictor
].name
);
1894 /* If the prediction for number of iterations is zero, do not
1895 predict the exit edges. */
1899 probability
= RDIV (REG_BR_PROB_BASE
, nitercst
);
1900 predict_edge (ex
, predictor
, probability
);
1904 /* Find information about loop bound variables. */
1905 for (nb_iter
= loop
->bounds
; nb_iter
;
1906 nb_iter
= nb_iter
->next
)
1908 && gimple_code (nb_iter
->stmt
) == GIMPLE_COND
)
1910 stmt
= as_a
<gcond
*> (nb_iter
->stmt
);
1913 if (!stmt
&& last_stmt (loop
->header
)
1914 && gimple_code (last_stmt (loop
->header
)) == GIMPLE_COND
)
1915 stmt
= as_a
<gcond
*> (last_stmt (loop
->header
));
1917 is_comparison_with_loop_invariant_p (stmt
, loop
,
1923 bbs
= get_loop_body (loop
);
1925 for (j
= 0; j
< loop
->num_nodes
; j
++)
1932 /* Bypass loop heuristics on continue statement. These
1933 statements construct loops via "non-loop" constructs
1934 in the source language and are better to be handled
1936 if (predicted_by_p (bb
, PRED_CONTINUE
))
1938 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1939 fprintf (dump_file
, "BB %i predicted by continue.\n",
1944 /* If we already used more reliable loop exit predictors, do not
1945 bother with PRED_LOOP_EXIT. */
1946 if (!predicted_by_loop_heuristics_p (bb
))
1948 /* For loop with many exits we don't want to predict all exits
1949 with the pretty large probability, because if all exits are
1950 considered in row, the loop would be predicted to iterate
1951 almost never. The code to divide probability by number of
1952 exits is very rough. It should compute the number of exits
1953 taken in each patch through function (not the overall number
1954 of exits that might be a lot higher for loops with wide switch
1955 statements in them) and compute n-th square root.
1957 We limit the minimal probability by 2% to avoid
1958 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
1959 as this was causing regression in perl benchmark containing such
1962 int probability
= ((REG_BR_PROB_BASE
1965 ? PRED_LOOP_EXIT_WITH_RECURSION
1966 : PRED_LOOP_EXIT
].hitrate
)
1968 if (probability
< HITRATE (2))
1969 probability
= HITRATE (2);
1970 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1971 if (e
->dest
->index
< NUM_FIXED_BLOCKS
1972 || !flow_bb_inside_loop_p (loop
, e
->dest
))
1974 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1976 "Predicting exit %i->%i with prob %i.\n",
1977 e
->src
->index
, e
->dest
->index
, probability
);
1979 recursion
? PRED_LOOP_EXIT_WITH_RECURSION
1980 : PRED_LOOP_EXIT
, probability
);
1984 predict_iv_comparison (loop
, bb
, loop_bound_var
, loop_iv_base
,
1986 tree_to_shwi (loop_bound_step
));
1989 /* In the following code
1994 guess that cond is unlikely. */
1995 if (loop_outer (loop
)->num
)
1997 basic_block bb
= NULL
;
1998 edge preheader_edge
= loop_preheader_edge (loop
);
2000 if (single_pred_p (preheader_edge
->src
)
2001 && single_succ_p (preheader_edge
->src
))
2002 preheader_edge
= single_pred_edge (preheader_edge
->src
);
2004 gimple
*stmt
= last_stmt (preheader_edge
->src
);
2005 /* Pattern match fortran loop preheader:
2006 _16 = BUILTIN_EXPECT (_15, 1, PRED_FORTRAN_LOOP_PREHEADER);
2007 _17 = (logical(kind=4)) _16;
2013 Loop guard branch prediction says nothing about duplicated loop
2014 headers produced by fortran frontend and in this case we want
2015 to predict paths leading to this preheader. */
2018 && gimple_code (stmt
) == GIMPLE_COND
2019 && gimple_cond_code (stmt
) == NE_EXPR
2020 && TREE_CODE (gimple_cond_lhs (stmt
)) == SSA_NAME
2021 && integer_zerop (gimple_cond_rhs (stmt
)))
2023 gimple
*call_stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
2024 if (gimple_code (call_stmt
) == GIMPLE_ASSIGN
2025 && gimple_expr_code (call_stmt
) == NOP_EXPR
2026 && TREE_CODE (gimple_assign_rhs1 (call_stmt
)) == SSA_NAME
)
2027 call_stmt
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (call_stmt
));
2028 if (gimple_call_internal_p (call_stmt
, IFN_BUILTIN_EXPECT
)
2029 && TREE_CODE (gimple_call_arg (call_stmt
, 2)) == INTEGER_CST
2030 && tree_fits_uhwi_p (gimple_call_arg (call_stmt
, 2))
2031 && tree_to_uhwi (gimple_call_arg (call_stmt
, 2))
2032 == PRED_FORTRAN_LOOP_PREHEADER
)
2033 bb
= preheader_edge
->src
;
2037 if (!dominated_by_p (CDI_DOMINATORS
,
2038 loop_outer (loop
)->latch
, loop
->header
))
2039 predict_paths_leading_to_edge (loop_preheader_edge (loop
),
2041 ? PRED_LOOP_GUARD_WITH_RECURSION
2048 if (!dominated_by_p (CDI_DOMINATORS
,
2049 loop_outer (loop
)->latch
, bb
))
2050 predict_paths_leading_to (bb
,
2052 ? PRED_LOOP_GUARD_WITH_RECURSION
2059 /* Free basic blocks from get_loop_body. */
2064 /* Attempt to predict probabilities of BB outgoing edges using local
2067 bb_estimate_probability_locally (basic_block bb
)
2069 rtx_insn
*last_insn
= BB_END (bb
);
2072 if (! can_predict_insn_p (last_insn
))
2074 cond
= get_condition (last_insn
, NULL
, false, false);
2078 /* Try "pointer heuristic."
2079 A comparison ptr == 0 is predicted as false.
2080 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2081 if (COMPARISON_P (cond
)
2082 && ((REG_P (XEXP (cond
, 0)) && REG_POINTER (XEXP (cond
, 0)))
2083 || (REG_P (XEXP (cond
, 1)) && REG_POINTER (XEXP (cond
, 1)))))
2085 if (GET_CODE (cond
) == EQ
)
2086 predict_insn_def (last_insn
, PRED_POINTER
, NOT_TAKEN
);
2087 else if (GET_CODE (cond
) == NE
)
2088 predict_insn_def (last_insn
, PRED_POINTER
, TAKEN
);
2092 /* Try "opcode heuristic."
2093 EQ tests are usually false and NE tests are usually true. Also,
2094 most quantities are positive, so we can make the appropriate guesses
2095 about signed comparisons against zero. */
2096 switch (GET_CODE (cond
))
2099 /* Unconditional branch. */
2100 predict_insn_def (last_insn
, PRED_UNCONDITIONAL
,
2101 cond
== const0_rtx
? NOT_TAKEN
: TAKEN
);
2106 /* Floating point comparisons appears to behave in a very
2107 unpredictable way because of special role of = tests in
2109 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
2111 /* Comparisons with 0 are often used for booleans and there is
2112 nothing useful to predict about them. */
2113 else if (XEXP (cond
, 1) == const0_rtx
2114 || XEXP (cond
, 0) == const0_rtx
)
2117 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, NOT_TAKEN
);
2122 /* Floating point comparisons appears to behave in a very
2123 unpredictable way because of special role of = tests in
2125 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
2127 /* Comparisons with 0 are often used for booleans and there is
2128 nothing useful to predict about them. */
2129 else if (XEXP (cond
, 1) == const0_rtx
2130 || XEXP (cond
, 0) == const0_rtx
)
2133 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, TAKEN
);
2137 predict_insn_def (last_insn
, PRED_FPOPCODE
, TAKEN
);
2141 predict_insn_def (last_insn
, PRED_FPOPCODE
, NOT_TAKEN
);
2146 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
2147 || XEXP (cond
, 1) == constm1_rtx
)
2148 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, NOT_TAKEN
);
2153 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
2154 || XEXP (cond
, 1) == constm1_rtx
)
2155 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, TAKEN
);
2163 /* Set edge->probability for each successor edge of BB. */
2165 guess_outgoing_edge_probabilities (basic_block bb
)
2167 bb_estimate_probability_locally (bb
);
2168 combine_predictions_for_insn (BB_END (bb
), bb
);
2171 static tree
expr_expected_value (tree
, bitmap
, enum br_predictor
*predictor
);
2173 /* Helper function for expr_expected_value. */
2176 expr_expected_value_1 (tree type
, tree op0
, enum tree_code code
,
2177 tree op1
, bitmap visited
, enum br_predictor
*predictor
)
2182 *predictor
= PRED_UNCONDITIONAL
;
2184 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
2186 if (TREE_CONSTANT (op0
))
2189 if (code
== IMAGPART_EXPR
)
2191 if (TREE_CODE (TREE_OPERAND (op0
, 0)) == SSA_NAME
)
2193 def
= SSA_NAME_DEF_STMT (TREE_OPERAND (op0
, 0));
2194 if (is_gimple_call (def
)
2195 && gimple_call_internal_p (def
)
2196 && (gimple_call_internal_fn (def
)
2197 == IFN_ATOMIC_COMPARE_EXCHANGE
))
2199 /* Assume that any given atomic operation has low contention,
2200 and thus the compare-and-swap operation succeeds. */
2202 *predictor
= PRED_COMPARE_AND_SWAP
;
2203 return build_one_cst (TREE_TYPE (op0
));
2208 if (code
!= SSA_NAME
)
2211 def
= SSA_NAME_DEF_STMT (op0
);
2213 /* If we were already here, break the infinite cycle. */
2214 if (!bitmap_set_bit (visited
, SSA_NAME_VERSION (op0
)))
2217 if (gimple_code (def
) == GIMPLE_PHI
)
2219 /* All the arguments of the PHI node must have the same constant
2221 int i
, n
= gimple_phi_num_args (def
);
2222 tree val
= NULL
, new_val
;
2224 for (i
= 0; i
< n
; i
++)
2226 tree arg
= PHI_ARG_DEF (def
, i
);
2227 enum br_predictor predictor2
;
2229 /* If this PHI has itself as an argument, we cannot
2230 determine the string length of this argument. However,
2231 if we can find an expected constant value for the other
2232 PHI args then we can still be sure that this is
2233 likely a constant. So be optimistic and just
2234 continue with the next argument. */
2235 if (arg
== PHI_RESULT (def
))
2238 new_val
= expr_expected_value (arg
, visited
, &predictor2
);
2240 /* It is difficult to combine value predictors. Simply assume
2241 that later predictor is weaker and take its prediction. */
2242 if (predictor
&& *predictor
< predictor2
)
2243 *predictor
= predictor2
;
2248 else if (!operand_equal_p (val
, new_val
, false))
2253 if (is_gimple_assign (def
))
2255 if (gimple_assign_lhs (def
) != op0
)
2258 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def
)),
2259 gimple_assign_rhs1 (def
),
2260 gimple_assign_rhs_code (def
),
2261 gimple_assign_rhs2 (def
),
2262 visited
, predictor
);
2265 if (is_gimple_call (def
))
2267 tree decl
= gimple_call_fndecl (def
);
2270 if (gimple_call_internal_p (def
)
2271 && gimple_call_internal_fn (def
) == IFN_BUILTIN_EXPECT
)
2273 gcc_assert (gimple_call_num_args (def
) == 3);
2274 tree val
= gimple_call_arg (def
, 0);
2275 if (TREE_CONSTANT (val
))
2279 tree val2
= gimple_call_arg (def
, 2);
2280 gcc_assert (TREE_CODE (val2
) == INTEGER_CST
2281 && tree_fits_uhwi_p (val2
)
2282 && tree_to_uhwi (val2
) < END_PREDICTORS
);
2283 *predictor
= (enum br_predictor
) tree_to_uhwi (val2
);
2285 return gimple_call_arg (def
, 1);
2289 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
2290 switch (DECL_FUNCTION_CODE (decl
))
2292 case BUILT_IN_EXPECT
:
2295 if (gimple_call_num_args (def
) != 2)
2297 val
= gimple_call_arg (def
, 0);
2298 if (TREE_CONSTANT (val
))
2301 *predictor
= PRED_BUILTIN_EXPECT
;
2302 return gimple_call_arg (def
, 1);
2305 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
:
2306 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
2307 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
2308 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
2309 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
2310 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
2311 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
2312 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
:
2313 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
2314 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
2315 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
2316 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
2317 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
2318 /* Assume that any given atomic operation has low contention,
2319 and thus the compare-and-swap operation succeeds. */
2321 *predictor
= PRED_COMPARE_AND_SWAP
;
2322 return boolean_true_node
;
2331 if (get_gimple_rhs_class (code
) == GIMPLE_BINARY_RHS
)
2334 enum br_predictor predictor2
;
2335 op0
= expr_expected_value (op0
, visited
, predictor
);
2338 op1
= expr_expected_value (op1
, visited
, &predictor2
);
2339 if (predictor
&& *predictor
< predictor2
)
2340 *predictor
= predictor2
;
2343 res
= fold_build2 (code
, type
, op0
, op1
);
2344 if (TREE_CONSTANT (res
))
2348 if (get_gimple_rhs_class (code
) == GIMPLE_UNARY_RHS
)
2351 op0
= expr_expected_value (op0
, visited
, predictor
);
2354 res
= fold_build1 (code
, type
, op0
);
2355 if (TREE_CONSTANT (res
))
2362 /* Return constant EXPR will likely have at execution time, NULL if unknown.
2363 The function is used by builtin_expect branch predictor so the evidence
2364 must come from this construct and additional possible constant folding.
2366 We may want to implement more involved value guess (such as value range
2367 propagation based prediction), but such tricks shall go to new
2371 expr_expected_value (tree expr
, bitmap visited
,
2372 enum br_predictor
*predictor
)
2374 enum tree_code code
;
2377 if (TREE_CONSTANT (expr
))
2380 *predictor
= PRED_UNCONDITIONAL
;
2384 extract_ops_from_tree (expr
, &code
, &op0
, &op1
);
2385 return expr_expected_value_1 (TREE_TYPE (expr
),
2386 op0
, code
, op1
, visited
, predictor
);
2389 /* Predict using opcode of the last statement in basic block. */
2391 tree_predict_by_opcode (basic_block bb
)
2393 gimple
*stmt
= last_stmt (bb
);
2400 enum br_predictor predictor
;
2402 if (!stmt
|| gimple_code (stmt
) != GIMPLE_COND
)
2404 FOR_EACH_EDGE (then_edge
, ei
, bb
->succs
)
2405 if (then_edge
->flags
& EDGE_TRUE_VALUE
)
2407 op0
= gimple_cond_lhs (stmt
);
2408 op1
= gimple_cond_rhs (stmt
);
2409 cmp
= gimple_cond_code (stmt
);
2410 type
= TREE_TYPE (op0
);
2411 val
= expr_expected_value_1 (boolean_type_node
, op0
, cmp
, op1
, auto_bitmap (),
2413 if (val
&& TREE_CODE (val
) == INTEGER_CST
)
2415 if (predictor
== PRED_BUILTIN_EXPECT
)
2417 int percent
= PARAM_VALUE (BUILTIN_EXPECT_PROBABILITY
);
2419 gcc_assert (percent
>= 0 && percent
<= 100);
2420 if (integer_zerop (val
))
2421 percent
= 100 - percent
;
2422 predict_edge (then_edge
, PRED_BUILTIN_EXPECT
, HITRATE (percent
));
2425 predict_edge_def (then_edge
, predictor
,
2426 integer_zerop (val
) ? NOT_TAKEN
: TAKEN
);
2428 /* Try "pointer heuristic."
2429 A comparison ptr == 0 is predicted as false.
2430 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2431 if (POINTER_TYPE_P (type
))
2434 predict_edge_def (then_edge
, PRED_TREE_POINTER
, NOT_TAKEN
);
2435 else if (cmp
== NE_EXPR
)
2436 predict_edge_def (then_edge
, PRED_TREE_POINTER
, TAKEN
);
2440 /* Try "opcode heuristic."
2441 EQ tests are usually false and NE tests are usually true. Also,
2442 most quantities are positive, so we can make the appropriate guesses
2443 about signed comparisons against zero. */
2448 /* Floating point comparisons appears to behave in a very
2449 unpredictable way because of special role of = tests in
2451 if (FLOAT_TYPE_P (type
))
2453 /* Comparisons with 0 are often used for booleans and there is
2454 nothing useful to predict about them. */
2455 else if (integer_zerop (op0
) || integer_zerop (op1
))
2458 predict_edge_def (then_edge
, PRED_TREE_OPCODE_NONEQUAL
, NOT_TAKEN
);
2463 /* Floating point comparisons appears to behave in a very
2464 unpredictable way because of special role of = tests in
2466 if (FLOAT_TYPE_P (type
))
2468 /* Comparisons with 0 are often used for booleans and there is
2469 nothing useful to predict about them. */
2470 else if (integer_zerop (op0
)
2471 || integer_zerop (op1
))
2474 predict_edge_def (then_edge
, PRED_TREE_OPCODE_NONEQUAL
, TAKEN
);
2478 predict_edge_def (then_edge
, PRED_TREE_FPOPCODE
, TAKEN
);
2481 case UNORDERED_EXPR
:
2482 predict_edge_def (then_edge
, PRED_TREE_FPOPCODE
, NOT_TAKEN
);
2487 if (integer_zerop (op1
)
2488 || integer_onep (op1
)
2489 || integer_all_onesp (op1
)
2492 || real_minus_onep (op1
))
2493 predict_edge_def (then_edge
, PRED_TREE_OPCODE_POSITIVE
, NOT_TAKEN
);
2498 if (integer_zerop (op1
)
2499 || integer_onep (op1
)
2500 || integer_all_onesp (op1
)
2503 || real_minus_onep (op1
))
2504 predict_edge_def (then_edge
, PRED_TREE_OPCODE_POSITIVE
, TAKEN
);
2512 /* Returns TRUE if the STMT is exit(0) like statement. */
2515 is_exit_with_zero_arg (const gimple
*stmt
)
2517 /* This is not exit, _exit or _Exit. */
2518 if (!gimple_call_builtin_p (stmt
, BUILT_IN_EXIT
)
2519 && !gimple_call_builtin_p (stmt
, BUILT_IN__EXIT
)
2520 && !gimple_call_builtin_p (stmt
, BUILT_IN__EXIT2
))
2523 /* Argument is an interger zero. */
2524 return integer_zerop (gimple_call_arg (stmt
, 0));
2527 /* Try to guess whether the value of return means error code. */
2529 static enum br_predictor
2530 return_prediction (tree val
, enum prediction
*prediction
)
2534 return PRED_NO_PREDICTION
;
2535 /* Different heuristics for pointers and scalars. */
2536 if (POINTER_TYPE_P (TREE_TYPE (val
)))
2538 /* NULL is usually not returned. */
2539 if (integer_zerop (val
))
2541 *prediction
= NOT_TAKEN
;
2542 return PRED_NULL_RETURN
;
2545 else if (INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2547 /* Negative return values are often used to indicate
2549 if (TREE_CODE (val
) == INTEGER_CST
2550 && tree_int_cst_sgn (val
) < 0)
2552 *prediction
= NOT_TAKEN
;
2553 return PRED_NEGATIVE_RETURN
;
2555 /* Constant return values seems to be commonly taken.
2556 Zero/one often represent booleans so exclude them from the
2558 if (TREE_CONSTANT (val
)
2559 && (!integer_zerop (val
) && !integer_onep (val
)))
2561 *prediction
= NOT_TAKEN
;
2562 return PRED_CONST_RETURN
;
2565 return PRED_NO_PREDICTION
;
2568 /* Find the basic block with return expression and look up for possible
2569 return value trying to apply RETURN_PREDICTION heuristics. */
2571 apply_return_prediction (void)
2573 greturn
*return_stmt
= NULL
;
2577 int phi_num_args
, i
;
2578 enum br_predictor pred
;
2579 enum prediction direction
;
2582 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
2584 gimple
*last
= last_stmt (e
->src
);
2586 && gimple_code (last
) == GIMPLE_RETURN
)
2588 return_stmt
= as_a
<greturn
*> (last
);
2594 return_val
= gimple_return_retval (return_stmt
);
2597 if (TREE_CODE (return_val
) != SSA_NAME
2598 || !SSA_NAME_DEF_STMT (return_val
)
2599 || gimple_code (SSA_NAME_DEF_STMT (return_val
)) != GIMPLE_PHI
)
2601 phi
= as_a
<gphi
*> (SSA_NAME_DEF_STMT (return_val
));
2602 phi_num_args
= gimple_phi_num_args (phi
);
2603 pred
= return_prediction (PHI_ARG_DEF (phi
, 0), &direction
);
2605 /* Avoid the degenerate case where all return values form the function
2606 belongs to same category (ie they are all positive constants)
2607 so we can hardly say something about them. */
2608 for (i
= 1; i
< phi_num_args
; i
++)
2609 if (pred
!= return_prediction (PHI_ARG_DEF (phi
, i
), &direction
))
2611 if (i
!= phi_num_args
)
2612 for (i
= 0; i
< phi_num_args
; i
++)
2614 pred
= return_prediction (PHI_ARG_DEF (phi
, i
), &direction
);
2615 if (pred
!= PRED_NO_PREDICTION
)
2616 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi
, i
), pred
,
2621 /* Look for basic block that contains unlikely to happen events
2622 (such as noreturn calls) and mark all paths leading to execution
2623 of this basic blocks as unlikely. */
2626 tree_bb_level_predictions (void)
2629 bool has_return_edges
= false;
2633 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
2634 if (!(e
->flags
& (EDGE_ABNORMAL
| EDGE_FAKE
| EDGE_EH
)))
2636 has_return_edges
= true;
2640 apply_return_prediction ();
2642 FOR_EACH_BB_FN (bb
, cfun
)
2644 gimple_stmt_iterator gsi
;
2646 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2648 gimple
*stmt
= gsi_stmt (gsi
);
2651 if (is_gimple_call (stmt
))
2653 if (gimple_call_noreturn_p (stmt
)
2655 && !is_exit_with_zero_arg (stmt
))
2656 predict_paths_leading_to (bb
, PRED_NORETURN
,
2658 decl
= gimple_call_fndecl (stmt
);
2660 && lookup_attribute ("cold",
2661 DECL_ATTRIBUTES (decl
)))
2662 predict_paths_leading_to (bb
, PRED_COLD_FUNCTION
,
2664 if (decl
&& recursive_call_p (current_function_decl
, decl
))
2665 predict_paths_leading_to (bb
, PRED_RECURSIVE_CALL
,
2668 else if (gimple_code (stmt
) == GIMPLE_PREDICT
)
2670 predict_paths_leading_to (bb
, gimple_predict_predictor (stmt
),
2671 gimple_predict_outcome (stmt
));
2672 /* Keep GIMPLE_PREDICT around so early inlining will propagate
2673 hints to callers. */
2679 /* Callback for hash_map::traverse, asserts that the pointer map is
2683 assert_is_empty (const_basic_block
const &, edge_prediction
*const &value
,
2686 gcc_assert (!value
);
2690 /* Predict branch probabilities and estimate profile for basic block BB. */
2693 tree_estimate_probability_bb (basic_block bb
)
2699 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2701 /* Predict edges to user labels with attributes. */
2702 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
2704 gimple_stmt_iterator gi
;
2705 for (gi
= gsi_start_bb (e
->dest
); !gsi_end_p (gi
); gsi_next (&gi
))
2707 glabel
*label_stmt
= dyn_cast
<glabel
*> (gsi_stmt (gi
));
2712 decl
= gimple_label_label (label_stmt
);
2713 if (DECL_ARTIFICIAL (decl
))
2716 /* Finally, we have a user-defined label. */
2717 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl
)))
2718 predict_edge_def (e
, PRED_COLD_LABEL
, NOT_TAKEN
);
2719 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (decl
)))
2720 predict_edge_def (e
, PRED_HOT_LABEL
, TAKEN
);
2724 /* Predict early returns to be probable, as we've already taken
2725 care for error returns and other cases are often used for
2726 fast paths through function.
2728 Since we've already removed the return statements, we are
2729 looking for CFG like:
2739 if (e
->dest
!= bb
->next_bb
2740 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
2741 && single_succ_p (e
->dest
)
2742 && single_succ_edge (e
->dest
)->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
2743 && (last
= last_stmt (e
->dest
)) != NULL
2744 && gimple_code (last
) == GIMPLE_RETURN
)
2749 if (single_succ_p (bb
))
2751 FOR_EACH_EDGE (e1
, ei1
, bb
->preds
)
2752 if (!predicted_by_p (e1
->src
, PRED_NULL_RETURN
)
2753 && !predicted_by_p (e1
->src
, PRED_CONST_RETURN
)
2754 && !predicted_by_p (e1
->src
, PRED_NEGATIVE_RETURN
))
2755 predict_edge_def (e1
, PRED_TREE_EARLY_RETURN
, NOT_TAKEN
);
2758 if (!predicted_by_p (e
->src
, PRED_NULL_RETURN
)
2759 && !predicted_by_p (e
->src
, PRED_CONST_RETURN
)
2760 && !predicted_by_p (e
->src
, PRED_NEGATIVE_RETURN
))
2761 predict_edge_def (e
, PRED_TREE_EARLY_RETURN
, NOT_TAKEN
);
2764 /* Look for block we are guarding (ie we dominate it,
2765 but it doesn't postdominate us). */
2766 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
) && e
->dest
!= bb
2767 && dominated_by_p (CDI_DOMINATORS
, e
->dest
, e
->src
)
2768 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, e
->dest
))
2770 gimple_stmt_iterator bi
;
2772 /* The call heuristic claims that a guarded function call
2773 is improbable. This is because such calls are often used
2774 to signal exceptional situations such as printing error
2776 for (bi
= gsi_start_bb (e
->dest
); !gsi_end_p (bi
);
2779 gimple
*stmt
= gsi_stmt (bi
);
2780 if (is_gimple_call (stmt
)
2781 && !gimple_inexpensive_call_p (as_a
<gcall
*> (stmt
))
2782 /* Constant and pure calls are hardly used to signalize
2783 something exceptional. */
2784 && gimple_has_side_effects (stmt
))
2786 if (gimple_call_fndecl (stmt
))
2787 predict_edge_def (e
, PRED_CALL
, NOT_TAKEN
);
2788 else if (virtual_method_call_p (gimple_call_fn (stmt
)))
2789 predict_edge_def (e
, PRED_POLYMORPHIC_CALL
, NOT_TAKEN
);
2791 predict_edge_def (e
, PRED_INDIR_CALL
, TAKEN
);
2797 tree_predict_by_opcode (bb
);
2800 /* Predict branch probabilities and estimate profile of the tree CFG.
2801 This function can be called from the loop optimizers to recompute
2802 the profile information.
2803 If DRY_RUN is set, do not modify CFG and only produce dump files. */
2806 tree_estimate_probability (bool dry_run
)
2810 add_noreturn_fake_exit_edges ();
2811 connect_infinite_loops_to_exit ();
2812 /* We use loop_niter_by_eval, which requires that the loops have
2814 create_preheaders (CP_SIMPLE_PREHEADERS
);
2815 calculate_dominance_info (CDI_POST_DOMINATORS
);
2817 bb_predictions
= new hash_map
<const_basic_block
, edge_prediction
*>;
2818 tree_bb_level_predictions ();
2819 record_loop_exits ();
2821 if (number_of_loops (cfun
) > 1)
2824 FOR_EACH_BB_FN (bb
, cfun
)
2825 tree_estimate_probability_bb (bb
);
2827 FOR_EACH_BB_FN (bb
, cfun
)
2828 combine_predictions_for_bb (bb
, dry_run
);
2831 bb_predictions
->traverse
<void *, assert_is_empty
> (NULL
);
2833 delete bb_predictions
;
2834 bb_predictions
= NULL
;
2837 estimate_bb_frequencies (false);
2838 free_dominance_info (CDI_POST_DOMINATORS
);
2839 remove_fake_exit_edges ();
2842 /* Predict edges to successors of CUR whose sources are not postdominated by
2843 BB by PRED and recurse to all postdominators. */
2846 predict_paths_for_bb (basic_block cur
, basic_block bb
,
2847 enum br_predictor pred
,
2848 enum prediction taken
,
2849 bitmap visited
, struct loop
*in_loop
= NULL
)
2855 /* If we exited the loop or CUR is unconditional in the loop, there is
2858 && (!flow_bb_inside_loop_p (in_loop
, cur
)
2859 || dominated_by_p (CDI_DOMINATORS
, in_loop
->latch
, cur
)))
2862 /* We are looking for all edges forming edge cut induced by
2863 set of all blocks postdominated by BB. */
2864 FOR_EACH_EDGE (e
, ei
, cur
->preds
)
2865 if (e
->src
->index
>= NUM_FIXED_BLOCKS
2866 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, bb
))
2872 /* Ignore fake edges and eh, we predict them as not taken anyway. */
2873 if (e
->flags
& (EDGE_EH
| EDGE_FAKE
))
2875 gcc_assert (bb
== cur
|| dominated_by_p (CDI_POST_DOMINATORS
, cur
, bb
));
2877 /* See if there is an edge from e->src that is not abnormal
2878 and does not lead to BB and does not exit the loop. */
2879 FOR_EACH_EDGE (e2
, ei2
, e
->src
->succs
)
2881 && !(e2
->flags
& (EDGE_EH
| EDGE_FAKE
))
2882 && !dominated_by_p (CDI_POST_DOMINATORS
, e2
->dest
, bb
)
2883 && (!in_loop
|| !loop_exit_edge_p (in_loop
, e2
)))
2889 /* If there is non-abnormal path leaving e->src, predict edge
2890 using predictor. Otherwise we need to look for paths
2893 The second may lead to infinite loop in the case we are predicitng
2894 regions that are only reachable by abnormal edges. We simply
2895 prevent visiting given BB twice. */
2898 if (!edge_predicted_by_p (e
, pred
, taken
))
2899 predict_edge_def (e
, pred
, taken
);
2901 else if (bitmap_set_bit (visited
, e
->src
->index
))
2902 predict_paths_for_bb (e
->src
, e
->src
, pred
, taken
, visited
, in_loop
);
2904 for (son
= first_dom_son (CDI_POST_DOMINATORS
, cur
);
2906 son
= next_dom_son (CDI_POST_DOMINATORS
, son
))
2907 predict_paths_for_bb (son
, bb
, pred
, taken
, visited
, in_loop
);
2910 /* Sets branch probabilities according to PREDiction and
2914 predict_paths_leading_to (basic_block bb
, enum br_predictor pred
,
2915 enum prediction taken
, struct loop
*in_loop
)
2917 predict_paths_for_bb (bb
, bb
, pred
, taken
, auto_bitmap (), in_loop
);
2920 /* Like predict_paths_leading_to but take edge instead of basic block. */
2923 predict_paths_leading_to_edge (edge e
, enum br_predictor pred
,
2924 enum prediction taken
, struct loop
*in_loop
)
2926 bool has_nonloop_edge
= false;
2930 basic_block bb
= e
->src
;
2931 FOR_EACH_EDGE (e2
, ei
, bb
->succs
)
2932 if (e2
->dest
!= e
->src
&& e2
->dest
!= e
->dest
2933 && !(e
->flags
& (EDGE_EH
| EDGE_FAKE
))
2934 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, e2
->dest
))
2936 has_nonloop_edge
= true;
2939 if (!has_nonloop_edge
)
2941 predict_paths_for_bb (bb
, bb
, pred
, taken
, auto_bitmap (), in_loop
);
2944 predict_edge_def (e
, pred
, taken
);
2947 /* This is used to carry information about basic blocks. It is
2948 attached to the AUX field of the standard CFG block. */
2952 /* Estimated frequency of execution of basic_block. */
2955 /* To keep queue of basic blocks to process. */
2958 /* Number of predecessors we need to visit first. */
2962 /* Similar information for edges. */
2963 struct edge_prob_info
2965 /* In case edge is a loopback edge, the probability edge will be reached
2966 in case header is. Estimated number of iterations of the loop can be
2967 then computed as 1 / (1 - back_edge_prob). */
2968 sreal back_edge_prob
;
2969 /* True if the edge is a loopback edge in the natural loop. */
2970 unsigned int back_edge
:1;
2973 #define BLOCK_INFO(B) ((block_info *) (B)->aux)
2975 #define EDGE_INFO(E) ((edge_prob_info *) (E)->aux)
2977 /* Helper function for estimate_bb_frequencies.
2978 Propagate the frequencies in blocks marked in
2979 TOVISIT, starting in HEAD. */
2982 propagate_freq (basic_block head
, bitmap tovisit
)
2991 /* For each basic block we need to visit count number of his predecessors
2992 we need to visit first. */
2993 EXECUTE_IF_SET_IN_BITMAP (tovisit
, 0, i
, bi
)
2998 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
3000 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3002 bool visit
= bitmap_bit_p (tovisit
, e
->src
->index
);
3004 if (visit
&& !(e
->flags
& EDGE_DFS_BACK
))
3006 else if (visit
&& dump_file
&& !EDGE_INFO (e
)->back_edge
)
3008 "Irreducible region hit, ignoring edge to %i->%i\n",
3009 e
->src
->index
, bb
->index
);
3011 BLOCK_INFO (bb
)->npredecessors
= count
;
3012 /* When function never returns, we will never process exit block. */
3013 if (!count
&& bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
3014 bb
->count
= bb
->frequency
= 0;
3017 BLOCK_INFO (head
)->frequency
= 1;
3019 for (bb
= head
; bb
; bb
= nextbb
)
3022 sreal cyclic_probability
= 0;
3023 sreal frequency
= 0;
3025 nextbb
= BLOCK_INFO (bb
)->next
;
3026 BLOCK_INFO (bb
)->next
= NULL
;
3028 /* Compute frequency of basic block. */
3032 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3033 gcc_assert (!bitmap_bit_p (tovisit
, e
->src
->index
)
3034 || (e
->flags
& EDGE_DFS_BACK
));
3036 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3037 if (EDGE_INFO (e
)->back_edge
)
3039 cyclic_probability
+= EDGE_INFO (e
)->back_edge_prob
;
3041 else if (!(e
->flags
& EDGE_DFS_BACK
))
3043 /* frequency += (e->probability
3044 * BLOCK_INFO (e->src)->frequency /
3045 REG_BR_PROB_BASE); */
3047 sreal tmp
= e
->probability
;
3048 tmp
*= BLOCK_INFO (e
->src
)->frequency
;
3049 tmp
*= real_inv_br_prob_base
;
3053 if (cyclic_probability
== 0)
3055 BLOCK_INFO (bb
)->frequency
= frequency
;
3059 if (cyclic_probability
> real_almost_one
)
3060 cyclic_probability
= real_almost_one
;
3062 /* BLOCK_INFO (bb)->frequency = frequency
3063 / (1 - cyclic_probability) */
3065 cyclic_probability
= sreal (1) - cyclic_probability
;
3066 BLOCK_INFO (bb
)->frequency
= frequency
/ cyclic_probability
;
3070 bitmap_clear_bit (tovisit
, bb
->index
);
3072 e
= find_edge (bb
, head
);
3075 /* EDGE_INFO (e)->back_edge_prob
3076 = ((e->probability * BLOCK_INFO (bb)->frequency)
3077 / REG_BR_PROB_BASE); */
3079 sreal tmp
= e
->probability
;
3080 tmp
*= BLOCK_INFO (bb
)->frequency
;
3081 EDGE_INFO (e
)->back_edge_prob
= tmp
* real_inv_br_prob_base
;
3084 /* Propagate to successor blocks. */
3085 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3086 if (!(e
->flags
& EDGE_DFS_BACK
)
3087 && BLOCK_INFO (e
->dest
)->npredecessors
)
3089 BLOCK_INFO (e
->dest
)->npredecessors
--;
3090 if (!BLOCK_INFO (e
->dest
)->npredecessors
)
3095 BLOCK_INFO (last
)->next
= e
->dest
;
3103 /* Estimate frequencies in loops at same nest level. */
3106 estimate_loops_at_level (struct loop
*first_loop
)
3110 for (loop
= first_loop
; loop
; loop
= loop
->next
)
3115 auto_bitmap tovisit
;
3117 estimate_loops_at_level (loop
->inner
);
3119 /* Find current loop back edge and mark it. */
3120 e
= loop_latch_edge (loop
);
3121 EDGE_INFO (e
)->back_edge
= 1;
3123 bbs
= get_loop_body (loop
);
3124 for (i
= 0; i
< loop
->num_nodes
; i
++)
3125 bitmap_set_bit (tovisit
, bbs
[i
]->index
);
3127 propagate_freq (loop
->header
, tovisit
);
3131 /* Propagates frequencies through structure of loops. */
3134 estimate_loops (void)
3136 auto_bitmap tovisit
;
3139 /* Start by estimating the frequencies in the loops. */
3140 if (number_of_loops (cfun
) > 1)
3141 estimate_loops_at_level (current_loops
->tree_root
->inner
);
3143 /* Now propagate the frequencies through all the blocks. */
3144 FOR_ALL_BB_FN (bb
, cfun
)
3146 bitmap_set_bit (tovisit
, bb
->index
);
3148 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun
), tovisit
);
3151 /* Drop the profile for NODE to guessed, and update its frequency based on
3152 whether it is expected to be hot given the CALL_COUNT. */
3155 drop_profile (struct cgraph_node
*node
, gcov_type call_count
)
3157 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->decl
);
3158 /* In the case where this was called by another function with a
3159 dropped profile, call_count will be 0. Since there are no
3160 non-zero call counts to this function, we don't know for sure
3161 whether it is hot, and therefore it will be marked normal below. */
3162 bool hot
= maybe_hot_count_p (NULL
, call_count
);
3166 "Dropping 0 profile for %s. %s based on calls.\n",
3168 hot
? "Function is hot" : "Function is normal");
3169 /* We only expect to miss profiles for functions that are reached
3170 via non-zero call edges in cases where the function may have
3171 been linked from another module or library (COMDATs and extern
3172 templates). See the comments below for handle_missing_profiles.
3173 Also, only warn in cases where the missing counts exceed the
3174 number of training runs. In certain cases with an execv followed
3175 by a no-return call the profile for the no-return call is not
3176 dumped and there can be a mismatch. */
3177 if (!DECL_COMDAT (node
->decl
) && !DECL_EXTERNAL (node
->decl
)
3178 && call_count
> profile_info
->runs
)
3180 if (flag_profile_correction
)
3184 "Missing counts for called function %s\n",
3185 node
->dump_name ());
3188 warning (0, "Missing counts for called function %s",
3189 node
->dump_name ());
3192 profile_status_for_fn (fn
)
3193 = (flag_guess_branch_prob
? PROFILE_GUESSED
: PROFILE_ABSENT
);
3195 = hot
? NODE_FREQUENCY_HOT
: NODE_FREQUENCY_NORMAL
;
3198 /* In the case of COMDAT routines, multiple object files will contain the same
3199 function and the linker will select one for the binary. In that case
3200 all the other copies from the profile instrument binary will be missing
3201 profile counts. Look for cases where this happened, due to non-zero
3202 call counts going to 0-count functions, and drop the profile to guessed
3203 so that we can use the estimated probabilities and avoid optimizing only
3206 The other case where the profile may be missing is when the routine
3207 is not going to be emitted to the object file, e.g. for "extern template"
3208 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
3209 all other cases of non-zero calls to 0-count functions. */
3212 handle_missing_profiles (void)
3214 struct cgraph_node
*node
;
3215 int unlikely_count_fraction
= PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION
);
3216 auto_vec
<struct cgraph_node
*, 64> worklist
;
3218 /* See if 0 count function has non-0 count callers. In this case we
3219 lost some profile. Drop its function profile to PROFILE_GUESSED. */
3220 FOR_EACH_DEFINED_FUNCTION (node
)
3222 struct cgraph_edge
*e
;
3223 gcov_type call_count
= 0;
3224 gcov_type max_tp_first_run
= 0;
3225 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->decl
);
3229 for (e
= node
->callers
; e
; e
= e
->next_caller
)
3231 call_count
+= e
->count
;
3233 if (e
->caller
->tp_first_run
> max_tp_first_run
)
3234 max_tp_first_run
= e
->caller
->tp_first_run
;
3237 /* If time profile is missing, let assign the maximum that comes from
3238 caller functions. */
3239 if (!node
->tp_first_run
&& max_tp_first_run
)
3240 node
->tp_first_run
= max_tp_first_run
+ 1;
3244 && (call_count
* unlikely_count_fraction
>= profile_info
->runs
))
3246 drop_profile (node
, call_count
);
3247 worklist
.safe_push (node
);
3251 /* Propagate the profile dropping to other 0-count COMDATs that are
3252 potentially called by COMDATs we already dropped the profile on. */
3253 while (worklist
.length () > 0)
3255 struct cgraph_edge
*e
;
3257 node
= worklist
.pop ();
3258 for (e
= node
->callees
; e
; e
= e
->next_caller
)
3260 struct cgraph_node
*callee
= e
->callee
;
3261 struct function
*fn
= DECL_STRUCT_FUNCTION (callee
->decl
);
3263 if (callee
->count
> 0)
3265 if (DECL_COMDAT (callee
->decl
) && fn
&& fn
->cfg
3266 && profile_status_for_fn (fn
) == PROFILE_READ
)
3268 drop_profile (node
, 0);
3269 worklist
.safe_push (callee
);
3275 /* Convert counts measured by profile driven feedback to frequencies.
3276 Return nonzero iff there was any nonzero execution count. */
3279 counts_to_freqs (void)
3281 gcov_type count_max
, true_count_max
= 0;
3284 /* Don't overwrite the estimated frequencies when the profile for
3285 the function is missing. We may drop this function PROFILE_GUESSED
3286 later in drop_profile (). */
3287 if (!flag_auto_profile
&& !ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
)
3290 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
3291 true_count_max
= MAX (bb
->count
, true_count_max
);
3293 count_max
= MAX (true_count_max
, 1);
3294 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
3295 bb
->frequency
= (bb
->count
* BB_FREQ_MAX
+ count_max
/ 2) / count_max
;
3297 return true_count_max
;
3300 /* Return true if function is likely to be expensive, so there is no point to
3301 optimize performance of prologue, epilogue or do inlining at the expense
3302 of code size growth. THRESHOLD is the limit of number of instructions
3303 function can execute at average to be still considered not expensive. */
3306 expensive_function_p (int threshold
)
3308 unsigned int sum
= 0;
3312 /* We can not compute accurately for large thresholds due to scaled
3314 gcc_assert (threshold
<= BB_FREQ_MAX
);
3316 /* Frequencies are out of range. This either means that function contains
3317 internal loop executing more than BB_FREQ_MAX times or profile feedback
3318 is available and function has not been executed at all. */
3319 if (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
== 0)
3322 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
3323 limit
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
* threshold
;
3324 FOR_EACH_BB_FN (bb
, cfun
)
3328 FOR_BB_INSNS (bb
, insn
)
3329 if (active_insn_p (insn
))
3331 sum
+= bb
->frequency
;
3340 /* Estimate and propagate basic block frequencies using the given branch
3341 probabilities. If FORCE is true, the frequencies are used to estimate
3342 the counts even when there are already non-zero profile counts. */
3345 estimate_bb_frequencies (bool force
)
3350 if (force
|| profile_status_for_fn (cfun
) != PROFILE_READ
|| !counts_to_freqs ())
3352 static int real_values_initialized
= 0;
3354 if (!real_values_initialized
)
3356 real_values_initialized
= 1;
3357 real_br_prob_base
= REG_BR_PROB_BASE
;
3358 real_bb_freq_max
= BB_FREQ_MAX
;
3359 real_one_half
= sreal (1, -1);
3360 real_inv_br_prob_base
= sreal (1) / real_br_prob_base
;
3361 real_almost_one
= sreal (1) - real_inv_br_prob_base
;
3364 mark_dfs_back_edges ();
3366 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
))->probability
=
3369 /* Set up block info for each basic block. */
3370 alloc_aux_for_blocks (sizeof (block_info
));
3371 alloc_aux_for_edges (sizeof (edge_prob_info
));
3372 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
3377 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3379 EDGE_INFO (e
)->back_edge_prob
= e
->probability
;
3380 EDGE_INFO (e
)->back_edge_prob
*= real_inv_br_prob_base
;
3384 /* First compute frequencies locally for each loop from innermost
3385 to outermost to examine frequencies for back edges. */
3389 FOR_EACH_BB_FN (bb
, cfun
)
3390 if (freq_max
< BLOCK_INFO (bb
)->frequency
)
3391 freq_max
= BLOCK_INFO (bb
)->frequency
;
3393 freq_max
= real_bb_freq_max
/ freq_max
;
3394 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
3396 sreal tmp
= BLOCK_INFO (bb
)->frequency
* freq_max
+ real_one_half
;
3397 bb
->frequency
= tmp
.to_int ();
3400 free_aux_for_blocks ();
3401 free_aux_for_edges ();
3403 compute_function_frequency ();
3406 /* Decide whether function is hot, cold or unlikely executed. */
3408 compute_function_frequency (void)
3411 struct cgraph_node
*node
= cgraph_node::get (current_function_decl
);
3413 if (DECL_STATIC_CONSTRUCTOR (current_function_decl
)
3414 || MAIN_NAME_P (DECL_NAME (current_function_decl
)))
3415 node
->only_called_at_startup
= true;
3416 if (DECL_STATIC_DESTRUCTOR (current_function_decl
))
3417 node
->only_called_at_exit
= true;
3419 if (profile_status_for_fn (cfun
) != PROFILE_READ
)
3421 int flags
= flags_from_decl_or_type (current_function_decl
);
3422 if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl
))
3424 node
->frequency
= NODE_FREQUENCY_UNLIKELY_EXECUTED
;
3425 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl
))
3427 node
->frequency
= NODE_FREQUENCY_HOT
;
3428 else if (flags
& ECF_NORETURN
)
3429 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
3430 else if (MAIN_NAME_P (DECL_NAME (current_function_decl
)))
3431 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
3432 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl
)
3433 || DECL_STATIC_DESTRUCTOR (current_function_decl
))
3434 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
3438 /* Only first time try to drop function into unlikely executed.
3439 After inlining the roundoff errors may confuse us.
3440 Ipa-profile pass will drop functions only called from unlikely
3441 functions to unlikely and that is most of what we care about. */
3442 if (!cfun
->after_inlining
)
3443 node
->frequency
= NODE_FREQUENCY_UNLIKELY_EXECUTED
;
3444 FOR_EACH_BB_FN (bb
, cfun
)
3446 if (maybe_hot_bb_p (cfun
, bb
))
3448 node
->frequency
= NODE_FREQUENCY_HOT
;
3451 if (!probably_never_executed_bb_p (cfun
, bb
))
3452 node
->frequency
= NODE_FREQUENCY_NORMAL
;
3456 /* Build PREDICT_EXPR. */
3458 build_predict_expr (enum br_predictor predictor
, enum prediction taken
)
3460 tree t
= build1 (PREDICT_EXPR
, void_type_node
,
3461 build_int_cst (integer_type_node
, predictor
));
3462 SET_PREDICT_EXPR_OUTCOME (t
, taken
);
3467 predictor_name (enum br_predictor predictor
)
3469 return predictor_info
[predictor
].name
;
3472 /* Predict branch probabilities and estimate profile of the tree CFG. */
3476 const pass_data pass_data_profile
=
3478 GIMPLE_PASS
, /* type */
3479 "profile_estimate", /* name */
3480 OPTGROUP_NONE
, /* optinfo_flags */
3481 TV_BRANCH_PROB
, /* tv_id */
3482 PROP_cfg
, /* properties_required */
3483 0, /* properties_provided */
3484 0, /* properties_destroyed */
3485 0, /* todo_flags_start */
3486 0, /* todo_flags_finish */
3489 class pass_profile
: public gimple_opt_pass
3492 pass_profile (gcc::context
*ctxt
)
3493 : gimple_opt_pass (pass_data_profile
, ctxt
)
3496 /* opt_pass methods: */
3497 virtual bool gate (function
*) { return flag_guess_branch_prob
; }
3498 virtual unsigned int execute (function
*);
3500 }; // class pass_profile
3503 pass_profile::execute (function
*fun
)
3507 if (profile_status_for_fn (cfun
) == PROFILE_GUESSED
)
3510 loop_optimizer_init (LOOPS_NORMAL
);
3511 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3512 flow_loops_dump (dump_file
, NULL
, 0);
3514 mark_irreducible_loops ();
3516 nb_loops
= number_of_loops (fun
);
3520 tree_estimate_probability (false);
3525 loop_optimizer_finalize ();
3526 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3527 gimple_dump_cfg (dump_file
, dump_flags
);
3528 if (profile_status_for_fn (fun
) == PROFILE_ABSENT
)
3529 profile_status_for_fn (fun
) = PROFILE_GUESSED
;
3530 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3533 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
3534 if (loop
->header
->frequency
)
3535 fprintf (dump_file
, "Loop got predicted %d to iterate %i times.\n",
3537 (int)expected_loop_iterations_unbounded (loop
));
3545 make_pass_profile (gcc::context
*ctxt
)
3547 return new pass_profile (ctxt
);
3552 const pass_data pass_data_strip_predict_hints
=
3554 GIMPLE_PASS
, /* type */
3555 "*strip_predict_hints", /* name */
3556 OPTGROUP_NONE
, /* optinfo_flags */
3557 TV_BRANCH_PROB
, /* tv_id */
3558 PROP_cfg
, /* properties_required */
3559 0, /* properties_provided */
3560 0, /* properties_destroyed */
3561 0, /* todo_flags_start */
3562 0, /* todo_flags_finish */
3565 class pass_strip_predict_hints
: public gimple_opt_pass
3568 pass_strip_predict_hints (gcc::context
*ctxt
)
3569 : gimple_opt_pass (pass_data_strip_predict_hints
, ctxt
)
3572 /* opt_pass methods: */
3573 opt_pass
* clone () { return new pass_strip_predict_hints (m_ctxt
); }
3574 virtual unsigned int execute (function
*);
3576 }; // class pass_strip_predict_hints
3578 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
3579 we no longer need. */
3581 pass_strip_predict_hints::execute (function
*fun
)
3586 bool changed
= false;
3588 FOR_EACH_BB_FN (bb
, fun
)
3590 gimple_stmt_iterator bi
;
3591 for (bi
= gsi_start_bb (bb
); !gsi_end_p (bi
);)
3593 gimple
*stmt
= gsi_stmt (bi
);
3595 if (gimple_code (stmt
) == GIMPLE_PREDICT
)
3597 gsi_remove (&bi
, true);
3601 else if (is_gimple_call (stmt
))
3603 tree fndecl
= gimple_call_fndecl (stmt
);
3606 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
3607 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
3608 && gimple_call_num_args (stmt
) == 2)
3609 || (gimple_call_internal_p (stmt
)
3610 && gimple_call_internal_fn (stmt
) == IFN_BUILTIN_EXPECT
))
3612 var
= gimple_call_lhs (stmt
);
3617 = gimple_build_assign (var
, gimple_call_arg (stmt
, 0));
3618 gsi_replace (&bi
, ass_stmt
, true);
3622 gsi_remove (&bi
, true);
3630 return changed
? TODO_cleanup_cfg
: 0;
3636 make_pass_strip_predict_hints (gcc::context
*ctxt
)
3638 return new pass_strip_predict_hints (ctxt
);
3641 /* Rebuild function frequencies. Passes are in general expected to
3642 maintain profile by hand, however in some cases this is not possible:
3643 for example when inlining several functions with loops freuqencies might run
3644 out of scale and thus needs to be recomputed. */
3647 rebuild_frequencies (void)
3649 timevar_push (TV_REBUILD_FREQUENCIES
);
3651 /* When the max bb count in the function is small, there is a higher
3652 chance that there were truncation errors in the integer scaling
3653 of counts by inlining and other optimizations. This could lead
3654 to incorrect classification of code as being cold when it isn't.
3655 In that case, force the estimation of bb counts/frequencies from the
3656 branch probabilities, rather than computing frequencies from counts,
3657 which may also lead to frequencies incorrectly reduced to 0. There
3658 is less precision in the probabilities, so we only do this for small
3660 gcov_type count_max
= 0;
3662 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
3663 count_max
= MAX (bb
->count
, count_max
);
3665 if (profile_status_for_fn (cfun
) == PROFILE_GUESSED
3666 || (!flag_auto_profile
&& profile_status_for_fn (cfun
) == PROFILE_READ
3667 && count_max
< REG_BR_PROB_BASE
/10))
3669 loop_optimizer_init (0);
3670 add_noreturn_fake_exit_edges ();
3671 mark_irreducible_loops ();
3672 connect_infinite_loops_to_exit ();
3673 estimate_bb_frequencies (true);
3674 remove_fake_exit_edges ();
3675 loop_optimizer_finalize ();
3677 else if (profile_status_for_fn (cfun
) == PROFILE_READ
)
3681 timevar_pop (TV_REBUILD_FREQUENCIES
);
3684 /* Perform a dry run of the branch prediction pass and report comparsion of
3685 the predicted and real profile into the dump file. */
3688 report_predictor_hitrates (void)
3692 loop_optimizer_init (LOOPS_NORMAL
);
3693 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3694 flow_loops_dump (dump_file
, NULL
, 0);
3696 mark_irreducible_loops ();
3698 nb_loops
= number_of_loops (cfun
);
3702 tree_estimate_probability (true);
3707 loop_optimizer_finalize ();
3710 /* Force edge E to be cold.
3711 If IMPOSSIBLE is true, for edge to have count and probability 0 otherwise
3712 keep low probability to represent possible error in a guess. This is used
3713 i.e. in case we predict loop to likely iterate given number of times but
3714 we are not 100% sure.
3716 This function locally updates profile without attempt to keep global
3717 consistency which can not be reached in full generality without full profile
3718 rebuild from probabilities alone. Doing so is not necessarily a good idea
3719 because frequencies and counts may be more realistic then probabilities.
3721 In some cases (such as for elimination of early exits during full loop
3722 unrolling) the caller can ensure that profile will get consistent
3726 force_edge_cold (edge e
, bool impossible
)
3728 gcov_type count_sum
= 0;
3732 gcov_type old_count
= e
->count
;
3733 int old_probability
= e
->probability
;
3734 gcov_type gcov_scale
= REG_BR_PROB_BASE
;
3735 int prob_scale
= REG_BR_PROB_BASE
;
3737 /* If edge is already improbably or cold, just return. */
3738 if (e
->probability
<= (impossible
? PROB_VERY_UNLIKELY
: 0)
3739 && (!impossible
|| !e
->count
))
3741 FOR_EACH_EDGE (e2
, ei
, e
->src
->succs
)
3744 count_sum
+= e2
->count
;
3745 prob_sum
+= e2
->probability
;
3748 /* If there are other edges out of e->src, redistribute probabilitity
3753 = MIN (e
->probability
, impossible
? 0 : PROB_VERY_UNLIKELY
);
3754 if (old_probability
)
3755 e
->count
= RDIV (e
->count
* e
->probability
, old_probability
);
3757 e
->count
= MIN (e
->count
, impossible
? 0 : 1);
3760 gcov_scale
= RDIV ((count_sum
+ old_count
- e
->count
) * REG_BR_PROB_BASE
,
3762 prob_scale
= RDIV ((REG_BR_PROB_BASE
- e
->probability
) * REG_BR_PROB_BASE
,
3764 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3765 fprintf (dump_file
, "Making edge %i->%i %s by redistributing "
3766 "probability to other edges.\n",
3767 e
->src
->index
, e
->dest
->index
,
3768 impossible
? "impossible" : "cold");
3769 FOR_EACH_EDGE (e2
, ei
, e
->src
->succs
)
3772 e2
->count
= RDIV (e2
->count
* gcov_scale
, REG_BR_PROB_BASE
);
3773 e2
->probability
= RDIV (e2
->probability
* prob_scale
,
3777 /* If all edges out of e->src are unlikely, the basic block itself
3781 e
->probability
= REG_BR_PROB_BASE
;
3783 /* If we did not adjusting, the source basic block has no likely edeges
3784 leaving other direction. In that case force that bb cold, too.
3785 This in general is difficult task to do, but handle special case when
3786 BB has only one predecestor. This is common case when we are updating
3787 after loop transforms. */
3788 if (!prob_sum
&& !count_sum
&& single_pred_p (e
->src
)
3789 && e
->src
->frequency
> (impossible
? 0 : 1))
3791 int old_frequency
= e
->src
->frequency
;
3792 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3793 fprintf (dump_file
, "Making bb %i %s.\n", e
->src
->index
,
3794 impossible
? "impossible" : "cold");
3795 e
->src
->frequency
= MIN (e
->src
->frequency
, impossible
? 0 : 1);
3796 e
->src
->count
= e
->count
= RDIV (e
->src
->count
* e
->src
->frequency
,
3798 force_edge_cold (single_pred_edge (e
->src
), impossible
);
3800 else if (dump_file
&& (dump_flags
& TDF_DETAILS
)
3801 && maybe_hot_bb_p (cfun
, e
->src
))
3802 fprintf (dump_file
, "Giving up on making bb %i %s.\n", e
->src
->index
,
3803 impossible
? "impossible" : "cold");