1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000-2013 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 "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "insn-config.h"
45 #include "diagnostic-core.h"
55 #include "gimple-iterator.h"
56 #include "gimple-ssa.h"
59 #include "tree-phinodes.h"
60 #include "ssa-iterators.h"
61 #include "tree-ssa-loop-niter.h"
62 #include "tree-ssa-loop.h"
64 #include "tree-pass.h"
65 #include "tree-scalar-evolution.h"
67 #include "pointer-set.h"
69 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
70 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */
71 static sreal real_zero
, real_one
, real_almost_one
, real_br_prob_base
,
72 real_inv_br_prob_base
, real_one_half
, real_bb_freq_max
;
74 /* Random guesstimation given names.
75 PROV_VERY_UNLIKELY should be small enough so basic block predicted
76 by it gets below HOT_BB_FREQUENCY_FRACTION. */
77 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 2000 - 1)
78 #define PROB_EVEN (REG_BR_PROB_BASE / 2)
79 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
80 #define PROB_ALWAYS (REG_BR_PROB_BASE)
82 static void combine_predictions_for_insn (rtx
, basic_block
);
83 static void dump_prediction (FILE *, enum br_predictor
, int, basic_block
, int);
84 static void predict_paths_leading_to (basic_block
, enum br_predictor
, enum prediction
);
85 static void predict_paths_leading_to_edge (edge
, enum br_predictor
, enum prediction
);
86 static bool can_predict_insn_p (const_rtx
);
88 /* Information we hold about each branch predictor.
89 Filled using information from predict.def. */
93 const char *const name
; /* Name used in the debugging dumps. */
94 const int hitrate
; /* Expected hitrate used by
95 predict_insn_def call. */
99 /* Use given predictor without Dempster-Shaffer theory if it matches
100 using first_match heuristics. */
101 #define PRED_FLAG_FIRST_MATCH 1
103 /* Recompute hitrate in percent to our representation. */
105 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
107 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
108 static const struct predictor_info predictor_info
[]= {
109 #include "predict.def"
111 /* Upper bound on predictors. */
116 /* Return TRUE if frequency FREQ is considered to be hot. */
119 maybe_hot_frequency_p (struct function
*fun
, int freq
)
121 struct cgraph_node
*node
= cgraph_get_node (fun
->decl
);
122 if (!profile_info
|| !flag_branch_probabilities
)
124 if (node
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
)
126 if (node
->frequency
== NODE_FREQUENCY_HOT
)
129 if (profile_status_for_function (fun
) == PROFILE_ABSENT
)
131 if (node
->frequency
== NODE_FREQUENCY_EXECUTED_ONCE
132 && freq
< (ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
* 2 / 3))
134 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
) == 0)
136 if (freq
< (ENTRY_BLOCK_PTR_FOR_FN (fun
)->frequency
137 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
)))
142 static gcov_type min_count
= -1;
144 /* Determine the threshold for hot BB counts. */
147 get_hot_bb_threshold ()
149 gcov_working_set_t
*ws
;
152 ws
= find_working_set (PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE
));
154 min_count
= ws
->min_counter
;
159 /* Set the threshold for hot BB counts. */
162 set_hot_bb_threshold (gcov_type min
)
167 /* Return TRUE if frequency FREQ is considered to be hot. */
170 maybe_hot_count_p (struct function
*fun
, gcov_type count
)
172 if (fun
&& profile_status_for_function (fun
) != PROFILE_READ
)
174 /* Code executed at most once is not hot. */
175 if (profile_info
->runs
>= count
)
177 return (count
>= get_hot_bb_threshold ());
180 /* Return true in case BB can be CPU intensive and should be optimized
181 for maximal performance. */
184 maybe_hot_bb_p (struct function
*fun
, const_basic_block bb
)
186 gcc_checking_assert (fun
);
187 if (profile_status_for_function (fun
) == PROFILE_READ
)
188 return maybe_hot_count_p (fun
, bb
->count
);
189 return maybe_hot_frequency_p (fun
, bb
->frequency
);
192 /* Return true if the call can be hot. */
195 cgraph_maybe_hot_edge_p (struct cgraph_edge
*edge
)
197 if (profile_info
&& flag_branch_probabilities
198 && !maybe_hot_count_p (NULL
,
201 if (edge
->caller
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
203 && edge
->callee
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
))
205 if (edge
->caller
->frequency
> NODE_FREQUENCY_UNLIKELY_EXECUTED
207 && edge
->callee
->frequency
<= NODE_FREQUENCY_EXECUTED_ONCE
))
211 if (edge
->caller
->frequency
== NODE_FREQUENCY_HOT
)
213 if (edge
->caller
->frequency
== NODE_FREQUENCY_EXECUTED_ONCE
214 && edge
->frequency
< CGRAPH_FREQ_BASE
* 3 / 2)
216 if (flag_guess_branch_prob
)
218 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
) == 0
219 || edge
->frequency
<= (CGRAPH_FREQ_BASE
220 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
)))
226 /* Return true in case BB can be CPU intensive and should be optimized
227 for maximal performance. */
230 maybe_hot_edge_p (edge e
)
232 if (profile_status
== PROFILE_READ
)
233 return maybe_hot_count_p (cfun
, e
->count
);
234 return maybe_hot_frequency_p (cfun
, EDGE_FREQUENCY (e
));
239 /* Return true if profile COUNT and FREQUENCY, or function FUN static
240 node frequency reflects never being executed. */
243 probably_never_executed (struct function
*fun
,
244 gcov_type count
, int frequency
)
246 gcc_checking_assert (fun
);
247 if (profile_status_for_function (fun
) == PROFILE_READ
)
249 int unlikely_count_fraction
= PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION
);
250 if (count
* unlikely_count_fraction
>= profile_info
->runs
)
254 if (!ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
)
256 if (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
)
258 gcov_type computed_count
;
259 /* Check for possibility of overflow, in which case entry bb count
260 is large enough to do the division first without losing much
262 if (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
< REG_BR_PROB_BASE
*
265 gcov_type scaled_count
266 = frequency
* ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
*
267 unlikely_count_fraction
;
268 computed_count
= RDIV (scaled_count
,
269 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
);
273 computed_count
= RDIV (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
,
274 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
);
275 computed_count
*= frequency
* unlikely_count_fraction
;
277 if (computed_count
>= profile_info
->runs
)
282 if ((!profile_info
|| !flag_branch_probabilities
)
283 && (cgraph_get_node (fun
->decl
)->frequency
284 == NODE_FREQUENCY_UNLIKELY_EXECUTED
))
290 /* Return true in case BB is probably never executed. */
293 probably_never_executed_bb_p (struct function
*fun
, const_basic_block bb
)
295 return probably_never_executed (fun
, bb
->count
, bb
->frequency
);
299 /* Return true in case edge E is probably never executed. */
302 probably_never_executed_edge_p (struct function
*fun
, edge e
)
304 return probably_never_executed (fun
, e
->count
, EDGE_FREQUENCY (e
));
307 /* Return true if NODE should be optimized for size. */
310 cgraph_optimize_for_size_p (struct cgraph_node
*node
)
314 if (node
&& (node
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
))
320 /* Return true when current function should always be optimized for size. */
323 optimize_function_for_size_p (struct function
*fun
)
327 if (!fun
|| !fun
->decl
)
329 return cgraph_optimize_for_size_p (cgraph_get_node (fun
->decl
));
332 /* Return true when current function should always be optimized for speed. */
335 optimize_function_for_speed_p (struct function
*fun
)
337 return !optimize_function_for_size_p (fun
);
340 /* Return TRUE when BB should be optimized for size. */
343 optimize_bb_for_size_p (const_basic_block bb
)
345 return optimize_function_for_size_p (cfun
) || !maybe_hot_bb_p (cfun
, bb
);
348 /* Return TRUE when BB should be optimized for speed. */
351 optimize_bb_for_speed_p (const_basic_block bb
)
353 return !optimize_bb_for_size_p (bb
);
356 /* Return TRUE when BB should be optimized for size. */
359 optimize_edge_for_size_p (edge e
)
361 return optimize_function_for_size_p (cfun
) || !maybe_hot_edge_p (e
);
364 /* Return TRUE when BB should be optimized for speed. */
367 optimize_edge_for_speed_p (edge e
)
369 return !optimize_edge_for_size_p (e
);
372 /* Return TRUE when BB should be optimized for size. */
375 optimize_insn_for_size_p (void)
377 return optimize_function_for_size_p (cfun
) || !crtl
->maybe_hot_insn_p
;
380 /* Return TRUE when BB should be optimized for speed. */
383 optimize_insn_for_speed_p (void)
385 return !optimize_insn_for_size_p ();
388 /* Return TRUE when LOOP should be optimized for size. */
391 optimize_loop_for_size_p (struct loop
*loop
)
393 return optimize_bb_for_size_p (loop
->header
);
396 /* Return TRUE when LOOP should be optimized for speed. */
399 optimize_loop_for_speed_p (struct loop
*loop
)
401 return optimize_bb_for_speed_p (loop
->header
);
404 /* Return TRUE when LOOP nest should be optimized for speed. */
407 optimize_loop_nest_for_speed_p (struct loop
*loop
)
409 struct loop
*l
= loop
;
410 if (optimize_loop_for_speed_p (loop
))
413 while (l
&& l
!= loop
)
415 if (optimize_loop_for_speed_p (l
))
423 while (l
!= loop
&& !l
->next
)
432 /* Return TRUE when LOOP nest should be optimized for size. */
435 optimize_loop_nest_for_size_p (struct loop
*loop
)
437 return !optimize_loop_nest_for_speed_p (loop
);
440 /* Return true when edge E is likely to be well predictable by branch
444 predictable_edge_p (edge e
)
446 if (profile_status
== PROFILE_ABSENT
)
449 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME
) * REG_BR_PROB_BASE
/ 100)
450 || (REG_BR_PROB_BASE
- e
->probability
451 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME
) * REG_BR_PROB_BASE
/ 100))
457 /* Set RTL expansion for BB profile. */
460 rtl_profile_for_bb (basic_block bb
)
462 crtl
->maybe_hot_insn_p
= maybe_hot_bb_p (cfun
, bb
);
465 /* Set RTL expansion for edge profile. */
468 rtl_profile_for_edge (edge e
)
470 crtl
->maybe_hot_insn_p
= maybe_hot_edge_p (e
);
473 /* Set RTL expansion to default mode (i.e. when profile info is not known). */
475 default_rtl_profile (void)
477 crtl
->maybe_hot_insn_p
= true;
480 /* Return true if the one of outgoing edges is already predicted by
484 rtl_predicted_by_p (const_basic_block bb
, enum br_predictor predictor
)
487 if (!INSN_P (BB_END (bb
)))
489 for (note
= REG_NOTES (BB_END (bb
)); note
; note
= XEXP (note
, 1))
490 if (REG_NOTE_KIND (note
) == REG_BR_PRED
491 && INTVAL (XEXP (XEXP (note
, 0), 0)) == (int)predictor
)
496 /* This map contains for a basic block the list of predictions for the
499 static struct pointer_map_t
*bb_predictions
;
501 /* Structure representing predictions in tree level. */
503 struct edge_prediction
{
504 struct edge_prediction
*ep_next
;
506 enum br_predictor ep_predictor
;
510 /* Return true if the one of outgoing edges is already predicted by
514 gimple_predicted_by_p (const_basic_block bb
, enum br_predictor predictor
)
516 struct edge_prediction
*i
;
517 void **preds
= pointer_map_contains (bb_predictions
, bb
);
522 for (i
= (struct edge_prediction
*) *preds
; i
; i
= i
->ep_next
)
523 if (i
->ep_predictor
== predictor
)
528 /* Return true when the probability of edge is reliable.
530 The profile guessing code is good at predicting branch outcome (ie.
531 taken/not taken), that is predicted right slightly over 75% of time.
532 It is however notoriously poor on predicting the probability itself.
533 In general the profile appear a lot flatter (with probabilities closer
534 to 50%) than the reality so it is bad idea to use it to drive optimization
535 such as those disabling dynamic branch prediction for well predictable
538 There are two exceptions - edges leading to noreturn edges and edges
539 predicted by number of iterations heuristics are predicted well. This macro
540 should be able to distinguish those, but at the moment it simply check for
541 noreturn heuristic that is only one giving probability over 99% or bellow
542 1%. In future we might want to propagate reliability information across the
543 CFG if we find this information useful on multiple places. */
545 probability_reliable_p (int prob
)
547 return (profile_status
== PROFILE_READ
548 || (profile_status
== PROFILE_GUESSED
549 && (prob
<= HITRATE (1) || prob
>= HITRATE (99))));
552 /* Same predicate as above, working on edges. */
554 edge_probability_reliable_p (const_edge e
)
556 return probability_reliable_p (e
->probability
);
559 /* Same predicate as edge_probability_reliable_p, working on notes. */
561 br_prob_note_reliable_p (const_rtx note
)
563 gcc_assert (REG_NOTE_KIND (note
) == REG_BR_PROB
);
564 return probability_reliable_p (XINT (note
, 0));
568 predict_insn (rtx insn
, enum br_predictor predictor
, int probability
)
570 gcc_assert (any_condjump_p (insn
));
571 if (!flag_guess_branch_prob
)
574 add_reg_note (insn
, REG_BR_PRED
,
575 gen_rtx_CONCAT (VOIDmode
,
576 GEN_INT ((int) predictor
),
577 GEN_INT ((int) probability
)));
580 /* Predict insn by given predictor. */
583 predict_insn_def (rtx insn
, enum br_predictor predictor
,
584 enum prediction taken
)
586 int probability
= predictor_info
[(int) predictor
].hitrate
;
589 probability
= REG_BR_PROB_BASE
- probability
;
591 predict_insn (insn
, predictor
, probability
);
594 /* Predict edge E with given probability if possible. */
597 rtl_predict_edge (edge e
, enum br_predictor predictor
, int probability
)
600 last_insn
= BB_END (e
->src
);
602 /* We can store the branch prediction information only about
603 conditional jumps. */
604 if (!any_condjump_p (last_insn
))
607 /* We always store probability of branching. */
608 if (e
->flags
& EDGE_FALLTHRU
)
609 probability
= REG_BR_PROB_BASE
- probability
;
611 predict_insn (last_insn
, predictor
, probability
);
614 /* Predict edge E with the given PROBABILITY. */
616 gimple_predict_edge (edge e
, enum br_predictor predictor
, int probability
)
618 gcc_assert (profile_status
!= PROFILE_GUESSED
);
619 if ((e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
) && EDGE_COUNT (e
->src
->succs
) >
621 && flag_guess_branch_prob
&& optimize
)
623 struct edge_prediction
*i
= XNEW (struct edge_prediction
);
624 void **preds
= pointer_map_insert (bb_predictions
, e
->src
);
626 i
->ep_next
= (struct edge_prediction
*) *preds
;
628 i
->ep_probability
= probability
;
629 i
->ep_predictor
= predictor
;
634 /* Remove all predictions on given basic block that are attached
637 remove_predictions_associated_with_edge (edge e
)
644 preds
= pointer_map_contains (bb_predictions
, e
->src
);
648 struct edge_prediction
**prediction
= (struct edge_prediction
**) preds
;
649 struct edge_prediction
*next
;
653 if ((*prediction
)->ep_edge
== e
)
655 next
= (*prediction
)->ep_next
;
660 prediction
= &((*prediction
)->ep_next
);
665 /* Clears the list of predictions stored for BB. */
668 clear_bb_predictions (basic_block bb
)
670 void **preds
= pointer_map_contains (bb_predictions
, bb
);
671 struct edge_prediction
*pred
, *next
;
676 for (pred
= (struct edge_prediction
*) *preds
; pred
; pred
= next
)
678 next
= pred
->ep_next
;
684 /* Return true when we can store prediction on insn INSN.
685 At the moment we represent predictions only on conditional
686 jumps, not at computed jump or other complicated cases. */
688 can_predict_insn_p (const_rtx insn
)
690 return (JUMP_P (insn
)
691 && any_condjump_p (insn
)
692 && EDGE_COUNT (BLOCK_FOR_INSN (insn
)->succs
) >= 2);
695 /* Predict edge E by given predictor if possible. */
698 predict_edge_def (edge e
, enum br_predictor predictor
,
699 enum prediction taken
)
701 int probability
= predictor_info
[(int) predictor
].hitrate
;
704 probability
= REG_BR_PROB_BASE
- probability
;
706 predict_edge (e
, predictor
, probability
);
709 /* Invert all branch predictions or probability notes in the INSN. This needs
710 to be done each time we invert the condition used by the jump. */
713 invert_br_probabilities (rtx insn
)
717 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
718 if (REG_NOTE_KIND (note
) == REG_BR_PROB
)
719 XINT (note
, 0) = REG_BR_PROB_BASE
- XINT (note
, 0);
720 else if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
721 XEXP (XEXP (note
, 0), 1)
722 = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (XEXP (note
, 0), 1)));
725 /* Dump information about the branch prediction to the output file. */
728 dump_prediction (FILE *file
, enum br_predictor predictor
, int probability
,
729 basic_block bb
, int used
)
737 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
738 if (! (e
->flags
& EDGE_FALLTHRU
))
741 fprintf (file
, " %s heuristics%s: %.1f%%",
742 predictor_info
[predictor
].name
,
743 used
? "" : " (ignored)", probability
* 100.0 / REG_BR_PROB_BASE
);
747 fprintf (file
, " exec ");
748 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, bb
->count
);
751 fprintf (file
, " hit ");
752 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, e
->count
);
753 fprintf (file
, " (%.1f%%)", e
->count
* 100.0 / bb
->count
);
757 fprintf (file
, "\n");
760 /* We can not predict the probabilities of outgoing edges of bb. Set them
761 evenly and hope for the best. */
763 set_even_probabilities (basic_block bb
)
769 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
770 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
772 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
773 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
774 e
->probability
= (REG_BR_PROB_BASE
+ nedges
/ 2) / nedges
;
779 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
780 note if not already present. Remove now useless REG_BR_PRED notes. */
783 combine_predictions_for_insn (rtx insn
, basic_block bb
)
788 int best_probability
= PROB_EVEN
;
789 enum br_predictor best_predictor
= END_PREDICTORS
;
790 int combined_probability
= REG_BR_PROB_BASE
/ 2;
792 bool first_match
= false;
795 if (!can_predict_insn_p (insn
))
797 set_even_probabilities (bb
);
801 prob_note
= find_reg_note (insn
, REG_BR_PROB
, 0);
802 pnote
= ®_NOTES (insn
);
804 fprintf (dump_file
, "Predictions for insn %i bb %i\n", INSN_UID (insn
),
807 /* We implement "first match" heuristics and use probability guessed
808 by predictor with smallest index. */
809 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
810 if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
812 enum br_predictor predictor
= ((enum br_predictor
)
813 INTVAL (XEXP (XEXP (note
, 0), 0)));
814 int probability
= INTVAL (XEXP (XEXP (note
, 0), 1));
817 if (best_predictor
> predictor
)
818 best_probability
= probability
, best_predictor
= predictor
;
820 d
= (combined_probability
* probability
821 + (REG_BR_PROB_BASE
- combined_probability
)
822 * (REG_BR_PROB_BASE
- probability
));
824 /* Use FP math to avoid overflows of 32bit integers. */
826 /* If one probability is 0% and one 100%, avoid division by zero. */
827 combined_probability
= REG_BR_PROB_BASE
/ 2;
829 combined_probability
= (((double) combined_probability
) * probability
830 * REG_BR_PROB_BASE
/ d
+ 0.5);
833 /* Decide which heuristic to use. In case we didn't match anything,
834 use no_prediction heuristic, in case we did match, use either
835 first match or Dempster-Shaffer theory depending on the flags. */
837 if (predictor_info
[best_predictor
].flags
& PRED_FLAG_FIRST_MATCH
)
841 dump_prediction (dump_file
, PRED_NO_PREDICTION
,
842 combined_probability
, bb
, true);
845 dump_prediction (dump_file
, PRED_DS_THEORY
, combined_probability
,
847 dump_prediction (dump_file
, PRED_FIRST_MATCH
, best_probability
,
852 combined_probability
= best_probability
;
853 dump_prediction (dump_file
, PRED_COMBINED
, combined_probability
, bb
, true);
857 if (REG_NOTE_KIND (*pnote
) == REG_BR_PRED
)
859 enum br_predictor predictor
= ((enum br_predictor
)
860 INTVAL (XEXP (XEXP (*pnote
, 0), 0)));
861 int probability
= INTVAL (XEXP (XEXP (*pnote
, 0), 1));
863 dump_prediction (dump_file
, predictor
, probability
, bb
,
864 !first_match
|| best_predictor
== predictor
);
865 *pnote
= XEXP (*pnote
, 1);
868 pnote
= &XEXP (*pnote
, 1);
873 add_int_reg_note (insn
, REG_BR_PROB
, combined_probability
);
875 /* Save the prediction into CFG in case we are seeing non-degenerated
877 if (!single_succ_p (bb
))
879 BRANCH_EDGE (bb
)->probability
= combined_probability
;
880 FALLTHRU_EDGE (bb
)->probability
881 = REG_BR_PROB_BASE
- combined_probability
;
884 else if (!single_succ_p (bb
))
886 int prob
= XINT (prob_note
, 0);
888 BRANCH_EDGE (bb
)->probability
= prob
;
889 FALLTHRU_EDGE (bb
)->probability
= REG_BR_PROB_BASE
- prob
;
892 single_succ_edge (bb
)->probability
= REG_BR_PROB_BASE
;
895 /* Combine predictions into single probability and store them into CFG.
896 Remove now useless prediction entries. */
899 combine_predictions_for_bb (basic_block bb
)
901 int best_probability
= PROB_EVEN
;
902 enum br_predictor best_predictor
= END_PREDICTORS
;
903 int combined_probability
= REG_BR_PROB_BASE
/ 2;
905 bool first_match
= false;
907 struct edge_prediction
*pred
;
909 edge e
, first
= NULL
, second
= NULL
;
913 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
914 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
917 if (first
&& !second
)
923 /* When there is no successor or only one choice, prediction is easy.
925 We are lazy for now and predict only basic blocks with two outgoing
926 edges. It is possible to predict generic case too, but we have to
927 ignore first match heuristics and do more involved combining. Implement
932 set_even_probabilities (bb
);
933 clear_bb_predictions (bb
);
935 fprintf (dump_file
, "%i edges in bb %i predicted to even probabilities\n",
941 fprintf (dump_file
, "Predictions for bb %i\n", bb
->index
);
943 preds
= pointer_map_contains (bb_predictions
, bb
);
946 /* We implement "first match" heuristics and use probability guessed
947 by predictor with smallest index. */
948 for (pred
= (struct edge_prediction
*) *preds
; pred
; pred
= pred
->ep_next
)
950 enum br_predictor predictor
= pred
->ep_predictor
;
951 int probability
= pred
->ep_probability
;
953 if (pred
->ep_edge
!= first
)
954 probability
= REG_BR_PROB_BASE
- probability
;
957 /* First match heuristics would be widly confused if we predicted
959 if (best_predictor
> predictor
)
961 struct edge_prediction
*pred2
;
962 int prob
= probability
;
964 for (pred2
= (struct edge_prediction
*) *preds
; pred2
; pred2
= pred2
->ep_next
)
965 if (pred2
!= pred
&& pred2
->ep_predictor
== pred
->ep_predictor
)
967 int probability2
= pred
->ep_probability
;
969 if (pred2
->ep_edge
!= first
)
970 probability2
= REG_BR_PROB_BASE
- probability2
;
972 if ((probability
< REG_BR_PROB_BASE
/ 2) !=
973 (probability2
< REG_BR_PROB_BASE
/ 2))
976 /* If the same predictor later gave better result, go for it! */
977 if ((probability
>= REG_BR_PROB_BASE
/ 2 && (probability2
> probability
))
978 || (probability
<= REG_BR_PROB_BASE
/ 2 && (probability2
< probability
)))
982 best_probability
= prob
, best_predictor
= predictor
;
985 d
= (combined_probability
* probability
986 + (REG_BR_PROB_BASE
- combined_probability
)
987 * (REG_BR_PROB_BASE
- probability
));
989 /* Use FP math to avoid overflows of 32bit integers. */
991 /* If one probability is 0% and one 100%, avoid division by zero. */
992 combined_probability
= REG_BR_PROB_BASE
/ 2;
994 combined_probability
= (((double) combined_probability
)
996 * REG_BR_PROB_BASE
/ d
+ 0.5);
1000 /* Decide which heuristic to use. In case we didn't match anything,
1001 use no_prediction heuristic, in case we did match, use either
1002 first match or Dempster-Shaffer theory depending on the flags. */
1004 if (predictor_info
[best_predictor
].flags
& PRED_FLAG_FIRST_MATCH
)
1008 dump_prediction (dump_file
, PRED_NO_PREDICTION
, combined_probability
, bb
, true);
1011 dump_prediction (dump_file
, PRED_DS_THEORY
, combined_probability
, bb
,
1013 dump_prediction (dump_file
, PRED_FIRST_MATCH
, best_probability
, bb
,
1018 combined_probability
= best_probability
;
1019 dump_prediction (dump_file
, PRED_COMBINED
, combined_probability
, bb
, true);
1023 for (pred
= (struct edge_prediction
*) *preds
; pred
; pred
= pred
->ep_next
)
1025 enum br_predictor predictor
= pred
->ep_predictor
;
1026 int probability
= pred
->ep_probability
;
1028 if (pred
->ep_edge
!= EDGE_SUCC (bb
, 0))
1029 probability
= REG_BR_PROB_BASE
- probability
;
1030 dump_prediction (dump_file
, predictor
, probability
, bb
,
1031 !first_match
|| best_predictor
== predictor
);
1034 clear_bb_predictions (bb
);
1038 first
->probability
= combined_probability
;
1039 second
->probability
= REG_BR_PROB_BASE
- combined_probability
;
1043 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1044 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1046 T1 and T2 should be one of the following cases:
1047 1. T1 is SSA_NAME, T2 is NULL
1048 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1049 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1052 strips_small_constant (tree t1
, tree t2
)
1059 else if (TREE_CODE (t1
) == SSA_NAME
)
1061 else if (tree_fits_shwi_p (t1
))
1062 value
= tree_to_shwi (t1
);
1068 else if (tree_fits_shwi_p (t2
))
1069 value
= tree_to_shwi (t2
);
1070 else if (TREE_CODE (t2
) == SSA_NAME
)
1078 if (value
<= 4 && value
>= -4)
1084 /* Return the SSA_NAME in T or T's operands.
1085 Return NULL if SSA_NAME cannot be found. */
1088 get_base_value (tree t
)
1090 if (TREE_CODE (t
) == SSA_NAME
)
1093 if (!BINARY_CLASS_P (t
))
1096 switch (TREE_OPERAND_LENGTH (t
))
1099 return strips_small_constant (TREE_OPERAND (t
, 0), NULL
);
1101 return strips_small_constant (TREE_OPERAND (t
, 0),
1102 TREE_OPERAND (t
, 1));
1108 /* Check the compare STMT in LOOP. If it compares an induction
1109 variable to a loop invariant, return true, and save
1110 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1111 Otherwise return false and set LOOP_INVAIANT to NULL. */
1114 is_comparison_with_loop_invariant_p (gimple stmt
, struct loop
*loop
,
1115 tree
*loop_invariant
,
1116 enum tree_code
*compare_code
,
1120 tree op0
, op1
, bound
, base
;
1122 enum tree_code code
;
1125 code
= gimple_cond_code (stmt
);
1126 *loop_invariant
= NULL
;
1142 op0
= gimple_cond_lhs (stmt
);
1143 op1
= gimple_cond_rhs (stmt
);
1145 if ((TREE_CODE (op0
) != SSA_NAME
&& TREE_CODE (op0
) != INTEGER_CST
)
1146 || (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op1
) != INTEGER_CST
))
1148 if (!simple_iv (loop
, loop_containing_stmt (stmt
), op0
, &iv0
, true))
1150 if (!simple_iv (loop
, loop_containing_stmt (stmt
), op1
, &iv1
, true))
1152 if (TREE_CODE (iv0
.step
) != INTEGER_CST
1153 || TREE_CODE (iv1
.step
) != INTEGER_CST
)
1155 if ((integer_zerop (iv0
.step
) && integer_zerop (iv1
.step
))
1156 || (!integer_zerop (iv0
.step
) && !integer_zerop (iv1
.step
)))
1159 if (integer_zerop (iv0
.step
))
1161 if (code
!= NE_EXPR
&& code
!= EQ_EXPR
)
1162 code
= invert_tree_comparison (code
, false);
1165 if (tree_fits_shwi_p (iv1
.step
))
1174 if (tree_fits_shwi_p (iv0
.step
))
1180 if (TREE_CODE (bound
) != INTEGER_CST
)
1181 bound
= get_base_value (bound
);
1184 if (TREE_CODE (base
) != INTEGER_CST
)
1185 base
= get_base_value (base
);
1189 *loop_invariant
= bound
;
1190 *compare_code
= code
;
1192 *loop_iv_base
= base
;
1196 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1199 expr_coherent_p (tree t1
, tree t2
)
1202 tree ssa_name_1
= NULL
;
1203 tree ssa_name_2
= NULL
;
1205 gcc_assert (TREE_CODE (t1
) == SSA_NAME
|| TREE_CODE (t1
) == INTEGER_CST
);
1206 gcc_assert (TREE_CODE (t2
) == SSA_NAME
|| TREE_CODE (t2
) == INTEGER_CST
);
1211 if (TREE_CODE (t1
) == INTEGER_CST
&& TREE_CODE (t2
) == INTEGER_CST
)
1213 if (TREE_CODE (t1
) == INTEGER_CST
|| TREE_CODE (t2
) == INTEGER_CST
)
1216 /* Check to see if t1 is expressed/defined with t2. */
1217 stmt
= SSA_NAME_DEF_STMT (t1
);
1218 gcc_assert (stmt
!= NULL
);
1219 if (is_gimple_assign (stmt
))
1221 ssa_name_1
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_USE
);
1222 if (ssa_name_1
&& ssa_name_1
== t2
)
1226 /* Check to see if t2 is expressed/defined with t1. */
1227 stmt
= SSA_NAME_DEF_STMT (t2
);
1228 gcc_assert (stmt
!= NULL
);
1229 if (is_gimple_assign (stmt
))
1231 ssa_name_2
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_USE
);
1232 if (ssa_name_2
&& ssa_name_2
== t1
)
1236 /* Compare if t1 and t2's def_stmts are identical. */
1237 if (ssa_name_2
!= NULL
&& ssa_name_1
== ssa_name_2
)
1243 /* Predict branch probability of BB when BB contains a branch that compares
1244 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1245 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1248 for (int i = 0; i < bound; i++) {
1255 In this loop, we will predict the branch inside the loop to be taken. */
1258 predict_iv_comparison (struct loop
*loop
, basic_block bb
,
1259 tree loop_bound_var
,
1260 tree loop_iv_base_var
,
1261 enum tree_code loop_bound_code
,
1262 int loop_bound_step
)
1265 tree compare_var
, compare_base
;
1266 enum tree_code compare_code
;
1267 tree compare_step_var
;
1271 if (predicted_by_p (bb
, PRED_LOOP_ITERATIONS_GUESSED
)
1272 || predicted_by_p (bb
, PRED_LOOP_ITERATIONS
)
1273 || predicted_by_p (bb
, PRED_LOOP_EXIT
))
1276 stmt
= last_stmt (bb
);
1277 if (!stmt
|| gimple_code (stmt
) != GIMPLE_COND
)
1279 if (!is_comparison_with_loop_invariant_p (stmt
, loop
, &compare_var
,
1285 /* Find the taken edge. */
1286 FOR_EACH_EDGE (then_edge
, ei
, bb
->succs
)
1287 if (then_edge
->flags
& EDGE_TRUE_VALUE
)
1290 /* When comparing an IV to a loop invariant, NE is more likely to be
1291 taken while EQ is more likely to be not-taken. */
1292 if (compare_code
== NE_EXPR
)
1294 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1297 else if (compare_code
== EQ_EXPR
)
1299 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1303 if (!expr_coherent_p (loop_iv_base_var
, compare_base
))
1306 /* If loop bound, base and compare bound are all constants, we can
1307 calculate the probability directly. */
1308 if (tree_fits_shwi_p (loop_bound_var
)
1309 && tree_fits_shwi_p (compare_var
)
1310 && tree_fits_shwi_p (compare_base
))
1313 bool of
, overflow
= false;
1314 double_int mod
, compare_count
, tem
, loop_count
;
1316 double_int loop_bound
= tree_to_double_int (loop_bound_var
);
1317 double_int compare_bound
= tree_to_double_int (compare_var
);
1318 double_int base
= tree_to_double_int (compare_base
);
1319 double_int compare_step
= tree_to_double_int (compare_step_var
);
1321 /* (loop_bound - base) / compare_step */
1322 tem
= loop_bound
.sub_with_overflow (base
, &of
);
1324 loop_count
= tem
.divmod_with_overflow (compare_step
,
1329 if ((!compare_step
.is_negative ())
1330 ^ (compare_code
== LT_EXPR
|| compare_code
== LE_EXPR
))
1332 /* (loop_bound - compare_bound) / compare_step */
1333 tem
= loop_bound
.sub_with_overflow (compare_bound
, &of
);
1335 compare_count
= tem
.divmod_with_overflow (compare_step
,
1342 /* (compare_bound - base) / compare_step */
1343 tem
= compare_bound
.sub_with_overflow (base
, &of
);
1345 compare_count
= tem
.divmod_with_overflow (compare_step
,
1350 if (compare_code
== LE_EXPR
|| compare_code
== GE_EXPR
)
1352 if (loop_bound_code
== LE_EXPR
|| loop_bound_code
== GE_EXPR
)
1354 if (compare_count
.is_negative ())
1355 compare_count
= double_int_zero
;
1356 if (loop_count
.is_negative ())
1357 loop_count
= double_int_zero
;
1358 if (loop_count
.is_zero ())
1360 else if (compare_count
.scmp (loop_count
) == 1)
1361 probability
= REG_BR_PROB_BASE
;
1364 /* If loop_count is too big, such that REG_BR_PROB_BASE * loop_count
1365 could overflow, shift both loop_count and compare_count right
1366 a bit so that it doesn't overflow. Note both counts are known not
1367 to be negative at this point. */
1368 int clz_bits
= clz_hwi (loop_count
.high
);
1369 gcc_assert (REG_BR_PROB_BASE
< 32768);
1372 loop_count
.arshift (16 - clz_bits
, HOST_BITS_PER_DOUBLE_INT
);
1373 compare_count
.arshift (16 - clz_bits
, HOST_BITS_PER_DOUBLE_INT
);
1375 tem
= compare_count
.mul_with_sign (double_int::from_shwi
1376 (REG_BR_PROB_BASE
), true, &of
);
1378 tem
= tem
.divmod (loop_count
, true, TRUNC_DIV_EXPR
, &mod
);
1379 probability
= tem
.to_uhwi ();
1383 predict_edge (then_edge
, PRED_LOOP_IV_COMPARE
, probability
);
1388 if (expr_coherent_p (loop_bound_var
, compare_var
))
1390 if ((loop_bound_code
== LT_EXPR
|| loop_bound_code
== LE_EXPR
)
1391 && (compare_code
== LT_EXPR
|| compare_code
== LE_EXPR
))
1392 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1393 else if ((loop_bound_code
== GT_EXPR
|| loop_bound_code
== GE_EXPR
)
1394 && (compare_code
== GT_EXPR
|| compare_code
== GE_EXPR
))
1395 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1396 else if (loop_bound_code
== NE_EXPR
)
1398 /* If the loop backedge condition is "(i != bound)", we do
1399 the comparison based on the step of IV:
1400 * step < 0 : backedge condition is like (i > bound)
1401 * step > 0 : backedge condition is like (i < bound) */
1402 gcc_assert (loop_bound_step
!= 0);
1403 if (loop_bound_step
> 0
1404 && (compare_code
== LT_EXPR
1405 || compare_code
== LE_EXPR
))
1406 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1407 else if (loop_bound_step
< 0
1408 && (compare_code
== GT_EXPR
1409 || compare_code
== GE_EXPR
))
1410 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1412 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1415 /* The branch is predicted not-taken if loop_bound_code is
1416 opposite with compare_code. */
1417 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1419 else if (expr_coherent_p (loop_iv_base_var
, compare_var
))
1422 for (i = s; i < h; i++)
1424 The branch should be predicted taken. */
1425 if (loop_bound_step
> 0
1426 && (compare_code
== GT_EXPR
|| compare_code
== GE_EXPR
))
1427 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1428 else if (loop_bound_step
< 0
1429 && (compare_code
== LT_EXPR
|| compare_code
== LE_EXPR
))
1430 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, TAKEN
);
1432 predict_edge_def (then_edge
, PRED_LOOP_IV_COMPARE_GUESS
, NOT_TAKEN
);
1436 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1437 exits are resulted from short-circuit conditions that will generate an
1440 if (foo() || global > 10)
1443 This will be translated into:
1448 if foo() goto BB6 else goto BB5
1450 if global > 10 goto BB6 else goto BB7
1454 iftmp = (PHI 0(BB5), 1(BB6))
1455 if iftmp == 1 goto BB8 else goto BB3
1457 outside of the loop...
1459 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1460 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1461 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1462 exits to predict them using PRED_LOOP_EXIT. */
1465 predict_extra_loop_exits (edge exit_edge
)
1468 bool check_value_one
;
1470 tree cmp_rhs
, cmp_lhs
;
1471 gimple cmp_stmt
= last_stmt (exit_edge
->src
);
1473 if (!cmp_stmt
|| gimple_code (cmp_stmt
) != GIMPLE_COND
)
1475 cmp_rhs
= gimple_cond_rhs (cmp_stmt
);
1476 cmp_lhs
= gimple_cond_lhs (cmp_stmt
);
1477 if (!TREE_CONSTANT (cmp_rhs
)
1478 || !(integer_zerop (cmp_rhs
) || integer_onep (cmp_rhs
)))
1480 if (TREE_CODE (cmp_lhs
) != SSA_NAME
)
1483 /* If check_value_one is true, only the phi_args with value '1' will lead
1484 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1486 check_value_one
= (((integer_onep (cmp_rhs
))
1487 ^ (gimple_cond_code (cmp_stmt
) == EQ_EXPR
))
1488 ^ ((exit_edge
->flags
& EDGE_TRUE_VALUE
) != 0));
1490 phi_stmt
= SSA_NAME_DEF_STMT (cmp_lhs
);
1491 if (!phi_stmt
|| gimple_code (phi_stmt
) != GIMPLE_PHI
)
1494 for (i
= 0; i
< gimple_phi_num_args (phi_stmt
); i
++)
1498 tree val
= gimple_phi_arg_def (phi_stmt
, i
);
1499 edge e
= gimple_phi_arg_edge (phi_stmt
, i
);
1501 if (!TREE_CONSTANT (val
) || !(integer_zerop (val
) || integer_onep (val
)))
1503 if ((check_value_one
^ integer_onep (val
)) == 1)
1505 if (EDGE_COUNT (e
->src
->succs
) != 1)
1507 predict_paths_leading_to_edge (e
, PRED_LOOP_EXIT
, NOT_TAKEN
);
1511 FOR_EACH_EDGE (e1
, ei
, e
->src
->preds
)
1512 predict_paths_leading_to_edge (e1
, PRED_LOOP_EXIT
, NOT_TAKEN
);
1516 /* Predict edge probabilities by exploiting loop structure. */
1519 predict_loops (void)
1523 /* Try to predict out blocks in a loop that are not part of a
1525 FOR_EACH_LOOP (loop
, 0)
1527 basic_block bb
, *bbs
;
1528 unsigned j
, n_exits
;
1530 struct tree_niter_desc niter_desc
;
1532 struct nb_iter_bound
*nb_iter
;
1533 enum tree_code loop_bound_code
= ERROR_MARK
;
1534 tree loop_bound_step
= NULL
;
1535 tree loop_bound_var
= NULL
;
1536 tree loop_iv_base
= NULL
;
1539 exits
= get_loop_exit_edges (loop
);
1540 n_exits
= exits
.length ();
1547 FOR_EACH_VEC_ELT (exits
, j
, ex
)
1550 HOST_WIDE_INT nitercst
;
1551 int max
= PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS
);
1553 enum br_predictor predictor
;
1555 predict_extra_loop_exits (ex
);
1557 if (number_of_iterations_exit (loop
, ex
, &niter_desc
, false, false))
1558 niter
= niter_desc
.niter
;
1559 if (!niter
|| TREE_CODE (niter_desc
.niter
) != INTEGER_CST
)
1560 niter
= loop_niter_by_eval (loop
, ex
);
1562 if (TREE_CODE (niter
) == INTEGER_CST
)
1564 if (tree_fits_uhwi_p (niter
)
1566 && compare_tree_int (niter
, max
- 1) == -1)
1567 nitercst
= tree_to_uhwi (niter
) + 1;
1570 predictor
= PRED_LOOP_ITERATIONS
;
1572 /* If we have just one exit and we can derive some information about
1573 the number of iterations of the loop from the statements inside
1574 the loop, use it to predict this exit. */
1575 else if (n_exits
== 1)
1577 nitercst
= estimated_stmt_executions_int (loop
);
1583 predictor
= PRED_LOOP_ITERATIONS_GUESSED
;
1588 /* If the prediction for number of iterations is zero, do not
1589 predict the exit edges. */
1593 probability
= ((REG_BR_PROB_BASE
+ nitercst
/ 2) / nitercst
);
1594 predict_edge (ex
, predictor
, probability
);
1598 /* Find information about loop bound variables. */
1599 for (nb_iter
= loop
->bounds
; nb_iter
;
1600 nb_iter
= nb_iter
->next
)
1602 && gimple_code (nb_iter
->stmt
) == GIMPLE_COND
)
1604 stmt
= nb_iter
->stmt
;
1607 if (!stmt
&& last_stmt (loop
->header
)
1608 && gimple_code (last_stmt (loop
->header
)) == GIMPLE_COND
)
1609 stmt
= last_stmt (loop
->header
);
1611 is_comparison_with_loop_invariant_p (stmt
, loop
,
1617 bbs
= get_loop_body (loop
);
1619 for (j
= 0; j
< loop
->num_nodes
; j
++)
1621 int header_found
= 0;
1627 /* Bypass loop heuristics on continue statement. These
1628 statements construct loops via "non-loop" constructs
1629 in the source language and are better to be handled
1631 if (predicted_by_p (bb
, PRED_CONTINUE
))
1634 /* Loop branch heuristics - predict an edge back to a
1635 loop's head as taken. */
1636 if (bb
== loop
->latch
)
1638 e
= find_edge (loop
->latch
, loop
->header
);
1642 predict_edge_def (e
, PRED_LOOP_BRANCH
, TAKEN
);
1646 /* Loop exit heuristics - predict an edge exiting the loop if the
1647 conditional has no loop header successors as not taken. */
1649 /* If we already used more reliable loop exit predictors, do not
1650 bother with PRED_LOOP_EXIT. */
1651 && !predicted_by_p (bb
, PRED_LOOP_ITERATIONS_GUESSED
)
1652 && !predicted_by_p (bb
, PRED_LOOP_ITERATIONS
))
1654 /* For loop with many exits we don't want to predict all exits
1655 with the pretty large probability, because if all exits are
1656 considered in row, the loop would be predicted to iterate
1657 almost never. The code to divide probability by number of
1658 exits is very rough. It should compute the number of exits
1659 taken in each patch through function (not the overall number
1660 of exits that might be a lot higher for loops with wide switch
1661 statements in them) and compute n-th square root.
1663 We limit the minimal probability by 2% to avoid
1664 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
1665 as this was causing regression in perl benchmark containing such
1668 int probability
= ((REG_BR_PROB_BASE
1669 - predictor_info
[(int) PRED_LOOP_EXIT
].hitrate
)
1671 if (probability
< HITRATE (2))
1672 probability
= HITRATE (2);
1673 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1674 if (e
->dest
->index
< NUM_FIXED_BLOCKS
1675 || !flow_bb_inside_loop_p (loop
, e
->dest
))
1676 predict_edge (e
, PRED_LOOP_EXIT
, probability
);
1679 predict_iv_comparison (loop
, bb
, loop_bound_var
, loop_iv_base
,
1681 tree_to_shwi (loop_bound_step
));
1684 /* Free basic blocks from get_loop_body. */
1689 /* Attempt to predict probabilities of BB outgoing edges using local
1692 bb_estimate_probability_locally (basic_block bb
)
1694 rtx last_insn
= BB_END (bb
);
1697 if (! can_predict_insn_p (last_insn
))
1699 cond
= get_condition (last_insn
, NULL
, false, false);
1703 /* Try "pointer heuristic."
1704 A comparison ptr == 0 is predicted as false.
1705 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
1706 if (COMPARISON_P (cond
)
1707 && ((REG_P (XEXP (cond
, 0)) && REG_POINTER (XEXP (cond
, 0)))
1708 || (REG_P (XEXP (cond
, 1)) && REG_POINTER (XEXP (cond
, 1)))))
1710 if (GET_CODE (cond
) == EQ
)
1711 predict_insn_def (last_insn
, PRED_POINTER
, NOT_TAKEN
);
1712 else if (GET_CODE (cond
) == NE
)
1713 predict_insn_def (last_insn
, PRED_POINTER
, TAKEN
);
1717 /* Try "opcode heuristic."
1718 EQ tests are usually false and NE tests are usually true. Also,
1719 most quantities are positive, so we can make the appropriate guesses
1720 about signed comparisons against zero. */
1721 switch (GET_CODE (cond
))
1724 /* Unconditional branch. */
1725 predict_insn_def (last_insn
, PRED_UNCONDITIONAL
,
1726 cond
== const0_rtx
? NOT_TAKEN
: TAKEN
);
1731 /* Floating point comparisons appears to behave in a very
1732 unpredictable way because of special role of = tests in
1734 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
1736 /* Comparisons with 0 are often used for booleans and there is
1737 nothing useful to predict about them. */
1738 else if (XEXP (cond
, 1) == const0_rtx
1739 || XEXP (cond
, 0) == const0_rtx
)
1742 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, NOT_TAKEN
);
1747 /* Floating point comparisons appears to behave in a very
1748 unpredictable way because of special role of = tests in
1750 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
1752 /* Comparisons with 0 are often used for booleans and there is
1753 nothing useful to predict about them. */
1754 else if (XEXP (cond
, 1) == const0_rtx
1755 || XEXP (cond
, 0) == const0_rtx
)
1758 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, TAKEN
);
1762 predict_insn_def (last_insn
, PRED_FPOPCODE
, TAKEN
);
1766 predict_insn_def (last_insn
, PRED_FPOPCODE
, NOT_TAKEN
);
1771 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
1772 || XEXP (cond
, 1) == constm1_rtx
)
1773 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, NOT_TAKEN
);
1778 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
1779 || XEXP (cond
, 1) == constm1_rtx
)
1780 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, TAKEN
);
1788 /* Set edge->probability for each successor edge of BB. */
1790 guess_outgoing_edge_probabilities (basic_block bb
)
1792 bb_estimate_probability_locally (bb
);
1793 combine_predictions_for_insn (BB_END (bb
), bb
);
1796 static tree
expr_expected_value (tree
, bitmap
);
1798 /* Helper function for expr_expected_value. */
1801 expr_expected_value_1 (tree type
, tree op0
, enum tree_code code
,
1802 tree op1
, bitmap visited
)
1806 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1808 if (TREE_CONSTANT (op0
))
1811 if (code
!= SSA_NAME
)
1814 def
= SSA_NAME_DEF_STMT (op0
);
1816 /* If we were already here, break the infinite cycle. */
1817 if (!bitmap_set_bit (visited
, SSA_NAME_VERSION (op0
)))
1820 if (gimple_code (def
) == GIMPLE_PHI
)
1822 /* All the arguments of the PHI node must have the same constant
1824 int i
, n
= gimple_phi_num_args (def
);
1825 tree val
= NULL
, new_val
;
1827 for (i
= 0; i
< n
; i
++)
1829 tree arg
= PHI_ARG_DEF (def
, i
);
1831 /* If this PHI has itself as an argument, we cannot
1832 determine the string length of this argument. However,
1833 if we can find an expected constant value for the other
1834 PHI args then we can still be sure that this is
1835 likely a constant. So be optimistic and just
1836 continue with the next argument. */
1837 if (arg
== PHI_RESULT (def
))
1840 new_val
= expr_expected_value (arg
, visited
);
1845 else if (!operand_equal_p (val
, new_val
, false))
1850 if (is_gimple_assign (def
))
1852 if (gimple_assign_lhs (def
) != op0
)
1855 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def
)),
1856 gimple_assign_rhs1 (def
),
1857 gimple_assign_rhs_code (def
),
1858 gimple_assign_rhs2 (def
),
1862 if (is_gimple_call (def
))
1864 tree decl
= gimple_call_fndecl (def
);
1867 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
1868 switch (DECL_FUNCTION_CODE (decl
))
1870 case BUILT_IN_EXPECT
:
1873 if (gimple_call_num_args (def
) != 2)
1875 val
= gimple_call_arg (def
, 0);
1876 if (TREE_CONSTANT (val
))
1878 return gimple_call_arg (def
, 1);
1881 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
:
1882 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
1883 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
1884 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
1885 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
1886 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
1887 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
1888 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
:
1889 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
1890 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
1891 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
1892 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
1893 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
1894 /* Assume that any given atomic operation has low contention,
1895 and thus the compare-and-swap operation succeeds. */
1896 return boolean_true_node
;
1903 if (get_gimple_rhs_class (code
) == GIMPLE_BINARY_RHS
)
1906 op0
= expr_expected_value (op0
, visited
);
1909 op1
= expr_expected_value (op1
, visited
);
1912 res
= fold_build2 (code
, type
, op0
, op1
);
1913 if (TREE_CONSTANT (res
))
1917 if (get_gimple_rhs_class (code
) == GIMPLE_UNARY_RHS
)
1920 op0
= expr_expected_value (op0
, visited
);
1923 res
= fold_build1 (code
, type
, op0
);
1924 if (TREE_CONSTANT (res
))
1931 /* Return constant EXPR will likely have at execution time, NULL if unknown.
1932 The function is used by builtin_expect branch predictor so the evidence
1933 must come from this construct and additional possible constant folding.
1935 We may want to implement more involved value guess (such as value range
1936 propagation based prediction), but such tricks shall go to new
1940 expr_expected_value (tree expr
, bitmap visited
)
1942 enum tree_code code
;
1945 if (TREE_CONSTANT (expr
))
1948 extract_ops_from_tree (expr
, &code
, &op0
, &op1
);
1949 return expr_expected_value_1 (TREE_TYPE (expr
),
1950 op0
, code
, op1
, visited
);
1954 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
1955 we no longer need. */
1957 strip_predict_hints (void)
1965 gimple_stmt_iterator bi
;
1966 for (bi
= gsi_start_bb (bb
); !gsi_end_p (bi
);)
1968 gimple stmt
= gsi_stmt (bi
);
1970 if (gimple_code (stmt
) == GIMPLE_PREDICT
)
1972 gsi_remove (&bi
, true);
1975 else if (gimple_code (stmt
) == GIMPLE_CALL
)
1977 tree fndecl
= gimple_call_fndecl (stmt
);
1980 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
1981 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
1982 && gimple_call_num_args (stmt
) == 2)
1984 var
= gimple_call_lhs (stmt
);
1988 = gimple_build_assign (var
, gimple_call_arg (stmt
, 0));
1989 gsi_replace (&bi
, ass_stmt
, true);
1993 gsi_remove (&bi
, true);
2004 /* Predict using opcode of the last statement in basic block. */
2006 tree_predict_by_opcode (basic_block bb
)
2008 gimple stmt
= last_stmt (bb
);
2017 if (!stmt
|| gimple_code (stmt
) != GIMPLE_COND
)
2019 FOR_EACH_EDGE (then_edge
, ei
, bb
->succs
)
2020 if (then_edge
->flags
& EDGE_TRUE_VALUE
)
2022 op0
= gimple_cond_lhs (stmt
);
2023 op1
= gimple_cond_rhs (stmt
);
2024 cmp
= gimple_cond_code (stmt
);
2025 type
= TREE_TYPE (op0
);
2026 visited
= BITMAP_ALLOC (NULL
);
2027 val
= expr_expected_value_1 (boolean_type_node
, op0
, cmp
, op1
, visited
);
2028 BITMAP_FREE (visited
);
2031 int percent
= PARAM_VALUE (BUILTIN_EXPECT_PROBABILITY
);
2033 gcc_assert (percent
>= 0 && percent
<= 100);
2034 if (integer_zerop (val
))
2035 percent
= 100 - percent
;
2036 predict_edge (then_edge
, PRED_BUILTIN_EXPECT
, HITRATE (percent
));
2038 /* Try "pointer heuristic."
2039 A comparison ptr == 0 is predicted as false.
2040 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2041 if (POINTER_TYPE_P (type
))
2044 predict_edge_def (then_edge
, PRED_TREE_POINTER
, NOT_TAKEN
);
2045 else if (cmp
== NE_EXPR
)
2046 predict_edge_def (then_edge
, PRED_TREE_POINTER
, TAKEN
);
2050 /* Try "opcode heuristic."
2051 EQ tests are usually false and NE tests are usually true. Also,
2052 most quantities are positive, so we can make the appropriate guesses
2053 about signed comparisons against zero. */
2058 /* Floating point comparisons appears to behave in a very
2059 unpredictable way because of special role of = tests in
2061 if (FLOAT_TYPE_P (type
))
2063 /* Comparisons with 0 are often used for booleans and there is
2064 nothing useful to predict about them. */
2065 else if (integer_zerop (op0
) || integer_zerop (op1
))
2068 predict_edge_def (then_edge
, PRED_TREE_OPCODE_NONEQUAL
, NOT_TAKEN
);
2073 /* Floating point comparisons appears to behave in a very
2074 unpredictable way because of special role of = tests in
2076 if (FLOAT_TYPE_P (type
))
2078 /* Comparisons with 0 are often used for booleans and there is
2079 nothing useful to predict about them. */
2080 else if (integer_zerop (op0
)
2081 || integer_zerop (op1
))
2084 predict_edge_def (then_edge
, PRED_TREE_OPCODE_NONEQUAL
, TAKEN
);
2088 predict_edge_def (then_edge
, PRED_TREE_FPOPCODE
, TAKEN
);
2091 case UNORDERED_EXPR
:
2092 predict_edge_def (then_edge
, PRED_TREE_FPOPCODE
, NOT_TAKEN
);
2097 if (integer_zerop (op1
)
2098 || integer_onep (op1
)
2099 || integer_all_onesp (op1
)
2102 || real_minus_onep (op1
))
2103 predict_edge_def (then_edge
, PRED_TREE_OPCODE_POSITIVE
, NOT_TAKEN
);
2108 if (integer_zerop (op1
)
2109 || integer_onep (op1
)
2110 || integer_all_onesp (op1
)
2113 || real_minus_onep (op1
))
2114 predict_edge_def (then_edge
, PRED_TREE_OPCODE_POSITIVE
, TAKEN
);
2122 /* Try to guess whether the value of return means error code. */
2124 static enum br_predictor
2125 return_prediction (tree val
, enum prediction
*prediction
)
2129 return PRED_NO_PREDICTION
;
2130 /* Different heuristics for pointers and scalars. */
2131 if (POINTER_TYPE_P (TREE_TYPE (val
)))
2133 /* NULL is usually not returned. */
2134 if (integer_zerop (val
))
2136 *prediction
= NOT_TAKEN
;
2137 return PRED_NULL_RETURN
;
2140 else if (INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2142 /* Negative return values are often used to indicate
2144 if (TREE_CODE (val
) == INTEGER_CST
2145 && tree_int_cst_sgn (val
) < 0)
2147 *prediction
= NOT_TAKEN
;
2148 return PRED_NEGATIVE_RETURN
;
2150 /* Constant return values seems to be commonly taken.
2151 Zero/one often represent booleans so exclude them from the
2153 if (TREE_CONSTANT (val
)
2154 && (!integer_zerop (val
) && !integer_onep (val
)))
2156 *prediction
= TAKEN
;
2157 return PRED_CONST_RETURN
;
2160 return PRED_NO_PREDICTION
;
2163 /* Find the basic block with return expression and look up for possible
2164 return value trying to apply RETURN_PREDICTION heuristics. */
2166 apply_return_prediction (void)
2168 gimple return_stmt
= NULL
;
2172 int phi_num_args
, i
;
2173 enum br_predictor pred
;
2174 enum prediction direction
;
2177 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
2179 return_stmt
= last_stmt (e
->src
);
2181 && gimple_code (return_stmt
) == GIMPLE_RETURN
)
2186 return_val
= gimple_return_retval (return_stmt
);
2189 if (TREE_CODE (return_val
) != SSA_NAME
2190 || !SSA_NAME_DEF_STMT (return_val
)
2191 || gimple_code (SSA_NAME_DEF_STMT (return_val
)) != GIMPLE_PHI
)
2193 phi
= SSA_NAME_DEF_STMT (return_val
);
2194 phi_num_args
= gimple_phi_num_args (phi
);
2195 pred
= return_prediction (PHI_ARG_DEF (phi
, 0), &direction
);
2197 /* Avoid the degenerate case where all return values form the function
2198 belongs to same category (ie they are all positive constants)
2199 so we can hardly say something about them. */
2200 for (i
= 1; i
< phi_num_args
; i
++)
2201 if (pred
!= return_prediction (PHI_ARG_DEF (phi
, i
), &direction
))
2203 if (i
!= phi_num_args
)
2204 for (i
= 0; i
< phi_num_args
; i
++)
2206 pred
= return_prediction (PHI_ARG_DEF (phi
, i
), &direction
);
2207 if (pred
!= PRED_NO_PREDICTION
)
2208 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi
, i
), pred
,
2213 /* Look for basic block that contains unlikely to happen events
2214 (such as noreturn calls) and mark all paths leading to execution
2215 of this basic blocks as unlikely. */
2218 tree_bb_level_predictions (void)
2221 bool has_return_edges
= false;
2225 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
2226 if (!(e
->flags
& (EDGE_ABNORMAL
| EDGE_FAKE
| EDGE_EH
)))
2228 has_return_edges
= true;
2232 apply_return_prediction ();
2236 gimple_stmt_iterator gsi
;
2238 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2240 gimple stmt
= gsi_stmt (gsi
);
2243 if (is_gimple_call (stmt
))
2245 if ((gimple_call_flags (stmt
) & ECF_NORETURN
)
2246 && has_return_edges
)
2247 predict_paths_leading_to (bb
, PRED_NORETURN
,
2249 decl
= gimple_call_fndecl (stmt
);
2251 && lookup_attribute ("cold",
2252 DECL_ATTRIBUTES (decl
)))
2253 predict_paths_leading_to (bb
, PRED_COLD_FUNCTION
,
2256 else if (gimple_code (stmt
) == GIMPLE_PREDICT
)
2258 predict_paths_leading_to (bb
, gimple_predict_predictor (stmt
),
2259 gimple_predict_outcome (stmt
));
2260 /* Keep GIMPLE_PREDICT around so early inlining will propagate
2261 hints to callers. */
2267 #ifdef ENABLE_CHECKING
2269 /* Callback for pointer_map_traverse, asserts that the pointer map is
2273 assert_is_empty (const void *key ATTRIBUTE_UNUSED
, void **value
,
2274 void *data ATTRIBUTE_UNUSED
)
2276 gcc_assert (!*value
);
2281 /* Predict branch probabilities and estimate profile for basic block BB. */
2284 tree_estimate_probability_bb (basic_block bb
)
2290 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2292 /* Predict edges to user labels with attributes. */
2293 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
2295 gimple_stmt_iterator gi
;
2296 for (gi
= gsi_start_bb (e
->dest
); !gsi_end_p (gi
); gsi_next (&gi
))
2298 gimple stmt
= gsi_stmt (gi
);
2301 if (gimple_code (stmt
) != GIMPLE_LABEL
)
2303 decl
= gimple_label_label (stmt
);
2304 if (DECL_ARTIFICIAL (decl
))
2307 /* Finally, we have a user-defined label. */
2308 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl
)))
2309 predict_edge_def (e
, PRED_COLD_LABEL
, NOT_TAKEN
);
2310 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (decl
)))
2311 predict_edge_def (e
, PRED_HOT_LABEL
, TAKEN
);
2315 /* Predict early returns to be probable, as we've already taken
2316 care for error returns and other cases are often used for
2317 fast paths through function.
2319 Since we've already removed the return statements, we are
2320 looking for CFG like:
2330 if (e
->dest
!= bb
->next_bb
2331 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
2332 && single_succ_p (e
->dest
)
2333 && single_succ_edge (e
->dest
)->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
2334 && (last
= last_stmt (e
->dest
)) != NULL
2335 && gimple_code (last
) == GIMPLE_RETURN
)
2340 if (single_succ_p (bb
))
2342 FOR_EACH_EDGE (e1
, ei1
, bb
->preds
)
2343 if (!predicted_by_p (e1
->src
, PRED_NULL_RETURN
)
2344 && !predicted_by_p (e1
->src
, PRED_CONST_RETURN
)
2345 && !predicted_by_p (e1
->src
, PRED_NEGATIVE_RETURN
))
2346 predict_edge_def (e1
, PRED_TREE_EARLY_RETURN
, NOT_TAKEN
);
2349 if (!predicted_by_p (e
->src
, PRED_NULL_RETURN
)
2350 && !predicted_by_p (e
->src
, PRED_CONST_RETURN
)
2351 && !predicted_by_p (e
->src
, PRED_NEGATIVE_RETURN
))
2352 predict_edge_def (e
, PRED_TREE_EARLY_RETURN
, NOT_TAKEN
);
2355 /* Look for block we are guarding (ie we dominate it,
2356 but it doesn't postdominate us). */
2357 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
) && e
->dest
!= bb
2358 && dominated_by_p (CDI_DOMINATORS
, e
->dest
, e
->src
)
2359 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, e
->dest
))
2361 gimple_stmt_iterator bi
;
2363 /* The call heuristic claims that a guarded function call
2364 is improbable. This is because such calls are often used
2365 to signal exceptional situations such as printing error
2367 for (bi
= gsi_start_bb (e
->dest
); !gsi_end_p (bi
);
2370 gimple stmt
= gsi_stmt (bi
);
2371 if (is_gimple_call (stmt
)
2372 /* Constant and pure calls are hardly used to signalize
2373 something exceptional. */
2374 && gimple_has_side_effects (stmt
))
2376 predict_edge_def (e
, PRED_CALL
, NOT_TAKEN
);
2382 tree_predict_by_opcode (bb
);
2385 /* Predict branch probabilities and estimate profile of the tree CFG.
2386 This function can be called from the loop optimizers to recompute
2387 the profile information. */
2390 tree_estimate_probability (void)
2394 add_noreturn_fake_exit_edges ();
2395 connect_infinite_loops_to_exit ();
2396 /* We use loop_niter_by_eval, which requires that the loops have
2398 create_preheaders (CP_SIMPLE_PREHEADERS
);
2399 calculate_dominance_info (CDI_POST_DOMINATORS
);
2401 bb_predictions
= pointer_map_create ();
2402 tree_bb_level_predictions ();
2403 record_loop_exits ();
2405 if (number_of_loops (cfun
) > 1)
2409 tree_estimate_probability_bb (bb
);
2412 combine_predictions_for_bb (bb
);
2414 #ifdef ENABLE_CHECKING
2415 pointer_map_traverse (bb_predictions
, assert_is_empty
, NULL
);
2417 pointer_map_destroy (bb_predictions
);
2418 bb_predictions
= NULL
;
2420 estimate_bb_frequencies (false);
2421 free_dominance_info (CDI_POST_DOMINATORS
);
2422 remove_fake_exit_edges ();
2425 /* Predict branch probabilities and estimate profile of the tree CFG.
2426 This is the driver function for PASS_PROFILE. */
2429 tree_estimate_probability_driver (void)
2433 loop_optimizer_init (LOOPS_NORMAL
);
2434 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2435 flow_loops_dump (dump_file
, NULL
, 0);
2437 mark_irreducible_loops ();
2439 nb_loops
= number_of_loops (cfun
);
2443 tree_estimate_probability ();
2448 loop_optimizer_finalize ();
2449 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2450 gimple_dump_cfg (dump_file
, dump_flags
);
2451 if (profile_status
== PROFILE_ABSENT
)
2452 profile_status
= PROFILE_GUESSED
;
2456 /* Predict edges to successors of CUR whose sources are not postdominated by
2457 BB by PRED and recurse to all postdominators. */
2460 predict_paths_for_bb (basic_block cur
, basic_block bb
,
2461 enum br_predictor pred
,
2462 enum prediction taken
,
2469 /* We are looking for all edges forming edge cut induced by
2470 set of all blocks postdominated by BB. */
2471 FOR_EACH_EDGE (e
, ei
, cur
->preds
)
2472 if (e
->src
->index
>= NUM_FIXED_BLOCKS
2473 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, bb
))
2479 /* Ignore fake edges and eh, we predict them as not taken anyway. */
2480 if (e
->flags
& (EDGE_EH
| EDGE_FAKE
))
2482 gcc_assert (bb
== cur
|| dominated_by_p (CDI_POST_DOMINATORS
, cur
, bb
));
2484 /* See if there is an edge from e->src that is not abnormal
2485 and does not lead to BB. */
2486 FOR_EACH_EDGE (e2
, ei2
, e
->src
->succs
)
2488 && !(e2
->flags
& (EDGE_EH
| EDGE_FAKE
))
2489 && !dominated_by_p (CDI_POST_DOMINATORS
, e2
->dest
, bb
))
2495 /* If there is non-abnormal path leaving e->src, predict edge
2496 using predictor. Otherwise we need to look for paths
2499 The second may lead to infinite loop in the case we are predicitng
2500 regions that are only reachable by abnormal edges. We simply
2501 prevent visiting given BB twice. */
2503 predict_edge_def (e
, pred
, taken
);
2504 else if (bitmap_set_bit (visited
, e
->src
->index
))
2505 predict_paths_for_bb (e
->src
, e
->src
, pred
, taken
, visited
);
2507 for (son
= first_dom_son (CDI_POST_DOMINATORS
, cur
);
2509 son
= next_dom_son (CDI_POST_DOMINATORS
, son
))
2510 predict_paths_for_bb (son
, bb
, pred
, taken
, visited
);
2513 /* Sets branch probabilities according to PREDiction and
2517 predict_paths_leading_to (basic_block bb
, enum br_predictor pred
,
2518 enum prediction taken
)
2520 bitmap visited
= BITMAP_ALLOC (NULL
);
2521 predict_paths_for_bb (bb
, bb
, pred
, taken
, visited
);
2522 BITMAP_FREE (visited
);
2525 /* Like predict_paths_leading_to but take edge instead of basic block. */
2528 predict_paths_leading_to_edge (edge e
, enum br_predictor pred
,
2529 enum prediction taken
)
2531 bool has_nonloop_edge
= false;
2535 basic_block bb
= e
->src
;
2536 FOR_EACH_EDGE (e2
, ei
, bb
->succs
)
2537 if (e2
->dest
!= e
->src
&& e2
->dest
!= e
->dest
2538 && !(e
->flags
& (EDGE_EH
| EDGE_FAKE
))
2539 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, e2
->dest
))
2541 has_nonloop_edge
= true;
2544 if (!has_nonloop_edge
)
2546 bitmap visited
= BITMAP_ALLOC (NULL
);
2547 predict_paths_for_bb (bb
, bb
, pred
, taken
, visited
);
2548 BITMAP_FREE (visited
);
2551 predict_edge_def (e
, pred
, taken
);
2554 /* This is used to carry information about basic blocks. It is
2555 attached to the AUX field of the standard CFG block. */
2557 typedef struct block_info_def
2559 /* Estimated frequency of execution of basic_block. */
2562 /* To keep queue of basic blocks to process. */
2565 /* Number of predecessors we need to visit first. */
2569 /* Similar information for edges. */
2570 typedef struct edge_info_def
2572 /* In case edge is a loopback edge, the probability edge will be reached
2573 in case header is. Estimated number of iterations of the loop can be
2574 then computed as 1 / (1 - back_edge_prob). */
2575 sreal back_edge_prob
;
2576 /* True if the edge is a loopback edge in the natural loop. */
2577 unsigned int back_edge
:1;
2580 #define BLOCK_INFO(B) ((block_info) (B)->aux)
2581 #define EDGE_INFO(E) ((edge_info) (E)->aux)
2583 /* Helper function for estimate_bb_frequencies.
2584 Propagate the frequencies in blocks marked in
2585 TOVISIT, starting in HEAD. */
2588 propagate_freq (basic_block head
, bitmap tovisit
)
2597 /* For each basic block we need to visit count number of his predecessors
2598 we need to visit first. */
2599 EXECUTE_IF_SET_IN_BITMAP (tovisit
, 0, i
, bi
)
2604 bb
= BASIC_BLOCK (i
);
2606 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2608 bool visit
= bitmap_bit_p (tovisit
, e
->src
->index
);
2610 if (visit
&& !(e
->flags
& EDGE_DFS_BACK
))
2612 else if (visit
&& dump_file
&& !EDGE_INFO (e
)->back_edge
)
2614 "Irreducible region hit, ignoring edge to %i->%i\n",
2615 e
->src
->index
, bb
->index
);
2617 BLOCK_INFO (bb
)->npredecessors
= count
;
2618 /* When function never returns, we will never process exit block. */
2619 if (!count
&& bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
2620 bb
->count
= bb
->frequency
= 0;
2623 memcpy (&BLOCK_INFO (head
)->frequency
, &real_one
, sizeof (real_one
));
2625 for (bb
= head
; bb
; bb
= nextbb
)
2628 sreal cyclic_probability
, frequency
;
2630 memcpy (&cyclic_probability
, &real_zero
, sizeof (real_zero
));
2631 memcpy (&frequency
, &real_zero
, sizeof (real_zero
));
2633 nextbb
= BLOCK_INFO (bb
)->next
;
2634 BLOCK_INFO (bb
)->next
= NULL
;
2636 /* Compute frequency of basic block. */
2639 #ifdef ENABLE_CHECKING
2640 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2641 gcc_assert (!bitmap_bit_p (tovisit
, e
->src
->index
)
2642 || (e
->flags
& EDGE_DFS_BACK
));
2645 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2646 if (EDGE_INFO (e
)->back_edge
)
2648 sreal_add (&cyclic_probability
, &cyclic_probability
,
2649 &EDGE_INFO (e
)->back_edge_prob
);
2651 else if (!(e
->flags
& EDGE_DFS_BACK
))
2655 /* frequency += (e->probability
2656 * BLOCK_INFO (e->src)->frequency /
2657 REG_BR_PROB_BASE); */
2659 sreal_init (&tmp
, e
->probability
, 0);
2660 sreal_mul (&tmp
, &tmp
, &BLOCK_INFO (e
->src
)->frequency
);
2661 sreal_mul (&tmp
, &tmp
, &real_inv_br_prob_base
);
2662 sreal_add (&frequency
, &frequency
, &tmp
);
2665 if (sreal_compare (&cyclic_probability
, &real_zero
) == 0)
2667 memcpy (&BLOCK_INFO (bb
)->frequency
, &frequency
,
2668 sizeof (frequency
));
2672 if (sreal_compare (&cyclic_probability
, &real_almost_one
) > 0)
2674 memcpy (&cyclic_probability
, &real_almost_one
,
2675 sizeof (real_almost_one
));
2678 /* BLOCK_INFO (bb)->frequency = frequency
2679 / (1 - cyclic_probability) */
2681 sreal_sub (&cyclic_probability
, &real_one
, &cyclic_probability
);
2682 sreal_div (&BLOCK_INFO (bb
)->frequency
,
2683 &frequency
, &cyclic_probability
);
2687 bitmap_clear_bit (tovisit
, bb
->index
);
2689 e
= find_edge (bb
, head
);
2694 /* EDGE_INFO (e)->back_edge_prob
2695 = ((e->probability * BLOCK_INFO (bb)->frequency)
2696 / REG_BR_PROB_BASE); */
2698 sreal_init (&tmp
, e
->probability
, 0);
2699 sreal_mul (&tmp
, &tmp
, &BLOCK_INFO (bb
)->frequency
);
2700 sreal_mul (&EDGE_INFO (e
)->back_edge_prob
,
2701 &tmp
, &real_inv_br_prob_base
);
2704 /* Propagate to successor blocks. */
2705 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2706 if (!(e
->flags
& EDGE_DFS_BACK
)
2707 && BLOCK_INFO (e
->dest
)->npredecessors
)
2709 BLOCK_INFO (e
->dest
)->npredecessors
--;
2710 if (!BLOCK_INFO (e
->dest
)->npredecessors
)
2715 BLOCK_INFO (last
)->next
= e
->dest
;
2723 /* Estimate frequencies in loops at same nest level. */
2726 estimate_loops_at_level (struct loop
*first_loop
)
2730 for (loop
= first_loop
; loop
; loop
= loop
->next
)
2735 bitmap tovisit
= BITMAP_ALLOC (NULL
);
2737 estimate_loops_at_level (loop
->inner
);
2739 /* Find current loop back edge and mark it. */
2740 e
= loop_latch_edge (loop
);
2741 EDGE_INFO (e
)->back_edge
= 1;
2743 bbs
= get_loop_body (loop
);
2744 for (i
= 0; i
< loop
->num_nodes
; i
++)
2745 bitmap_set_bit (tovisit
, bbs
[i
]->index
);
2747 propagate_freq (loop
->header
, tovisit
);
2748 BITMAP_FREE (tovisit
);
2752 /* Propagates frequencies through structure of loops. */
2755 estimate_loops (void)
2757 bitmap tovisit
= BITMAP_ALLOC (NULL
);
2760 /* Start by estimating the frequencies in the loops. */
2761 if (number_of_loops (cfun
) > 1)
2762 estimate_loops_at_level (current_loops
->tree_root
->inner
);
2764 /* Now propagate the frequencies through all the blocks. */
2767 bitmap_set_bit (tovisit
, bb
->index
);
2769 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun
), tovisit
);
2770 BITMAP_FREE (tovisit
);
2773 /* Drop the profile for NODE to guessed, and update its frequency based on
2774 whether it is expected to be hot given the CALL_COUNT. */
2777 drop_profile (struct cgraph_node
*node
, gcov_type call_count
)
2779 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->decl
);
2780 /* In the case where this was called by another function with a
2781 dropped profile, call_count will be 0. Since there are no
2782 non-zero call counts to this function, we don't know for sure
2783 whether it is hot, and therefore it will be marked normal below. */
2784 bool hot
= maybe_hot_count_p (NULL
, call_count
);
2788 "Dropping 0 profile for %s/%i. %s based on calls.\n",
2789 node
->name (), node
->order
,
2790 hot
? "Function is hot" : "Function is normal");
2791 /* We only expect to miss profiles for functions that are reached
2792 via non-zero call edges in cases where the function may have
2793 been linked from another module or library (COMDATs and extern
2794 templates). See the comments below for handle_missing_profiles.
2795 Also, only warn in cases where the missing counts exceed the
2796 number of training runs. In certain cases with an execv followed
2797 by a no-return call the profile for the no-return call is not
2798 dumped and there can be a mismatch. */
2799 if (!DECL_COMDAT (node
->decl
) && !DECL_EXTERNAL (node
->decl
)
2800 && call_count
> profile_info
->runs
)
2802 if (flag_profile_correction
)
2806 "Missing counts for called function %s/%i\n",
2807 node
->name (), node
->order
);
2810 warning (0, "Missing counts for called function %s/%i",
2811 node
->name (), node
->order
);
2814 profile_status_for_function (fn
)
2815 = (flag_guess_branch_prob
? PROFILE_GUESSED
: PROFILE_ABSENT
);
2817 = hot
? NODE_FREQUENCY_HOT
: NODE_FREQUENCY_NORMAL
;
2820 /* In the case of COMDAT routines, multiple object files will contain the same
2821 function and the linker will select one for the binary. In that case
2822 all the other copies from the profile instrument binary will be missing
2823 profile counts. Look for cases where this happened, due to non-zero
2824 call counts going to 0-count functions, and drop the profile to guessed
2825 so that we can use the estimated probabilities and avoid optimizing only
2828 The other case where the profile may be missing is when the routine
2829 is not going to be emitted to the object file, e.g. for "extern template"
2830 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
2831 all other cases of non-zero calls to 0-count functions. */
2834 handle_missing_profiles (void)
2836 struct cgraph_node
*node
;
2837 int unlikely_count_fraction
= PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION
);
2838 vec
<struct cgraph_node
*> worklist
;
2839 worklist
.create (64);
2841 /* See if 0 count function has non-0 count callers. In this case we
2842 lost some profile. Drop its function profile to PROFILE_GUESSED. */
2843 FOR_EACH_DEFINED_FUNCTION (node
)
2845 struct cgraph_edge
*e
;
2846 gcov_type call_count
= 0;
2847 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->decl
);
2851 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2852 call_count
+= e
->count
;
2855 && (call_count
* unlikely_count_fraction
>= profile_info
->runs
))
2857 drop_profile (node
, call_count
);
2858 worklist
.safe_push (node
);
2862 /* Propagate the profile dropping to other 0-count COMDATs that are
2863 potentially called by COMDATs we already dropped the profile on. */
2864 while (worklist
.length () > 0)
2866 struct cgraph_edge
*e
;
2868 node
= worklist
.pop ();
2869 for (e
= node
->callees
; e
; e
= e
->next_caller
)
2871 struct cgraph_node
*callee
= e
->callee
;
2872 struct function
*fn
= DECL_STRUCT_FUNCTION (callee
->decl
);
2874 if (callee
->count
> 0)
2876 if (DECL_COMDAT (callee
->decl
) && fn
&& fn
->cfg
2877 && profile_status_for_function (fn
) == PROFILE_READ
)
2879 drop_profile (node
, 0);
2880 worklist
.safe_push (callee
);
2884 worklist
.release ();
2887 /* Convert counts measured by profile driven feedback to frequencies.
2888 Return nonzero iff there was any nonzero execution count. */
2891 counts_to_freqs (void)
2893 gcov_type count_max
, true_count_max
= 0;
2896 /* Don't overwrite the estimated frequencies when the profile for
2897 the function is missing. We may drop this function PROFILE_GUESSED
2898 later in drop_profile (). */
2899 if (!ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
)
2902 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
2903 true_count_max
= MAX (bb
->count
, true_count_max
);
2905 count_max
= MAX (true_count_max
, 1);
2906 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
2907 bb
->frequency
= (bb
->count
* BB_FREQ_MAX
+ count_max
/ 2) / count_max
;
2909 return true_count_max
;
2912 /* Return true if function is likely to be expensive, so there is no point to
2913 optimize performance of prologue, epilogue or do inlining at the expense
2914 of code size growth. THRESHOLD is the limit of number of instructions
2915 function can execute at average to be still considered not expensive. */
2918 expensive_function_p (int threshold
)
2920 unsigned int sum
= 0;
2924 /* We can not compute accurately for large thresholds due to scaled
2926 gcc_assert (threshold
<= BB_FREQ_MAX
);
2928 /* Frequencies are out of range. This either means that function contains
2929 internal loop executing more than BB_FREQ_MAX times or profile feedback
2930 is available and function has not been executed at all. */
2931 if (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
== 0)
2934 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
2935 limit
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
* threshold
;
2940 FOR_BB_INSNS (bb
, insn
)
2941 if (active_insn_p (insn
))
2943 sum
+= bb
->frequency
;
2952 /* Estimate and propagate basic block frequencies using the given branch
2953 probabilities. If FORCE is true, the frequencies are used to estimate
2954 the counts even when there are already non-zero profile counts. */
2957 estimate_bb_frequencies (bool force
)
2962 if (force
|| profile_status
!= PROFILE_READ
|| !counts_to_freqs ())
2964 static int real_values_initialized
= 0;
2966 if (!real_values_initialized
)
2968 real_values_initialized
= 1;
2969 sreal_init (&real_zero
, 0, 0);
2970 sreal_init (&real_one
, 1, 0);
2971 sreal_init (&real_br_prob_base
, REG_BR_PROB_BASE
, 0);
2972 sreal_init (&real_bb_freq_max
, BB_FREQ_MAX
, 0);
2973 sreal_init (&real_one_half
, 1, -1);
2974 sreal_div (&real_inv_br_prob_base
, &real_one
, &real_br_prob_base
);
2975 sreal_sub (&real_almost_one
, &real_one
, &real_inv_br_prob_base
);
2978 mark_dfs_back_edges ();
2980 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
))->probability
=
2983 /* Set up block info for each basic block. */
2984 alloc_aux_for_blocks (sizeof (struct block_info_def
));
2985 alloc_aux_for_edges (sizeof (struct edge_info_def
));
2986 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
2991 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2993 sreal_init (&EDGE_INFO (e
)->back_edge_prob
, e
->probability
, 0);
2994 sreal_mul (&EDGE_INFO (e
)->back_edge_prob
,
2995 &EDGE_INFO (e
)->back_edge_prob
,
2996 &real_inv_br_prob_base
);
3000 /* First compute frequencies locally for each loop from innermost
3001 to outermost to examine frequencies for back edges. */
3004 memcpy (&freq_max
, &real_zero
, sizeof (real_zero
));
3006 if (sreal_compare (&freq_max
, &BLOCK_INFO (bb
)->frequency
) < 0)
3007 memcpy (&freq_max
, &BLOCK_INFO (bb
)->frequency
, sizeof (freq_max
));
3009 sreal_div (&freq_max
, &real_bb_freq_max
, &freq_max
);
3010 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
3014 sreal_mul (&tmp
, &BLOCK_INFO (bb
)->frequency
, &freq_max
);
3015 sreal_add (&tmp
, &tmp
, &real_one_half
);
3016 bb
->frequency
= sreal_to_int (&tmp
);
3019 free_aux_for_blocks ();
3020 free_aux_for_edges ();
3022 compute_function_frequency ();
3025 /* Decide whether function is hot, cold or unlikely executed. */
3027 compute_function_frequency (void)
3030 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
3032 if (DECL_STATIC_CONSTRUCTOR (current_function_decl
)
3033 || MAIN_NAME_P (DECL_NAME (current_function_decl
)))
3034 node
->only_called_at_startup
= true;
3035 if (DECL_STATIC_DESTRUCTOR (current_function_decl
))
3036 node
->only_called_at_exit
= true;
3038 if (profile_status
!= PROFILE_READ
)
3040 int flags
= flags_from_decl_or_type (current_function_decl
);
3041 if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl
))
3043 node
->frequency
= NODE_FREQUENCY_UNLIKELY_EXECUTED
;
3044 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl
))
3046 node
->frequency
= NODE_FREQUENCY_HOT
;
3047 else if (flags
& ECF_NORETURN
)
3048 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
3049 else if (MAIN_NAME_P (DECL_NAME (current_function_decl
)))
3050 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
3051 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl
)
3052 || DECL_STATIC_DESTRUCTOR (current_function_decl
))
3053 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
3057 /* Only first time try to drop function into unlikely executed.
3058 After inlining the roundoff errors may confuse us.
3059 Ipa-profile pass will drop functions only called from unlikely
3060 functions to unlikely and that is most of what we care about. */
3061 if (!cfun
->after_inlining
)
3062 node
->frequency
= NODE_FREQUENCY_UNLIKELY_EXECUTED
;
3065 if (maybe_hot_bb_p (cfun
, bb
))
3067 node
->frequency
= NODE_FREQUENCY_HOT
;
3070 if (!probably_never_executed_bb_p (cfun
, bb
))
3071 node
->frequency
= NODE_FREQUENCY_NORMAL
;
3076 gate_estimate_probability (void)
3078 return flag_guess_branch_prob
;
3081 /* Build PREDICT_EXPR. */
3083 build_predict_expr (enum br_predictor predictor
, enum prediction taken
)
3085 tree t
= build1 (PREDICT_EXPR
, void_type_node
,
3086 build_int_cst (integer_type_node
, predictor
));
3087 SET_PREDICT_EXPR_OUTCOME (t
, taken
);
3092 predictor_name (enum br_predictor predictor
)
3094 return predictor_info
[predictor
].name
;
3099 const pass_data pass_data_profile
=
3101 GIMPLE_PASS
, /* type */
3102 "profile_estimate", /* name */
3103 OPTGROUP_NONE
, /* optinfo_flags */
3104 true, /* has_gate */
3105 true, /* has_execute */
3106 TV_BRANCH_PROB
, /* tv_id */
3107 PROP_cfg
, /* properties_required */
3108 0, /* properties_provided */
3109 0, /* properties_destroyed */
3110 0, /* todo_flags_start */
3111 TODO_verify_ssa
, /* todo_flags_finish */
3114 class pass_profile
: public gimple_opt_pass
3117 pass_profile (gcc::context
*ctxt
)
3118 : gimple_opt_pass (pass_data_profile
, ctxt
)
3121 /* opt_pass methods: */
3122 bool gate () { return gate_estimate_probability (); }
3123 unsigned int execute () { return tree_estimate_probability_driver (); }
3125 }; // class pass_profile
3130 make_pass_profile (gcc::context
*ctxt
)
3132 return new pass_profile (ctxt
);
3137 const pass_data pass_data_strip_predict_hints
=
3139 GIMPLE_PASS
, /* type */
3140 "*strip_predict_hints", /* name */
3141 OPTGROUP_NONE
, /* optinfo_flags */
3142 false, /* has_gate */
3143 true, /* has_execute */
3144 TV_BRANCH_PROB
, /* tv_id */
3145 PROP_cfg
, /* properties_required */
3146 0, /* properties_provided */
3147 0, /* properties_destroyed */
3148 0, /* todo_flags_start */
3149 TODO_verify_ssa
, /* todo_flags_finish */
3152 class pass_strip_predict_hints
: public gimple_opt_pass
3155 pass_strip_predict_hints (gcc::context
*ctxt
)
3156 : gimple_opt_pass (pass_data_strip_predict_hints
, ctxt
)
3159 /* opt_pass methods: */
3160 opt_pass
* clone () { return new pass_strip_predict_hints (m_ctxt
); }
3161 unsigned int execute () { return strip_predict_hints (); }
3163 }; // class pass_strip_predict_hints
3168 make_pass_strip_predict_hints (gcc::context
*ctxt
)
3170 return new pass_strip_predict_hints (ctxt
);
3173 /* Rebuild function frequencies. Passes are in general expected to
3174 maintain profile by hand, however in some cases this is not possible:
3175 for example when inlining several functions with loops freuqencies might run
3176 out of scale and thus needs to be recomputed. */
3179 rebuild_frequencies (void)
3181 timevar_push (TV_REBUILD_FREQUENCIES
);
3183 /* When the max bb count in the function is small, there is a higher
3184 chance that there were truncation errors in the integer scaling
3185 of counts by inlining and other optimizations. This could lead
3186 to incorrect classification of code as being cold when it isn't.
3187 In that case, force the estimation of bb counts/frequencies from the
3188 branch probabilities, rather than computing frequencies from counts,
3189 which may also lead to frequencies incorrectly reduced to 0. There
3190 is less precision in the probabilities, so we only do this for small
3192 gcov_type count_max
= 0;
3194 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
3195 count_max
= MAX (bb
->count
, count_max
);
3197 if (profile_status
== PROFILE_GUESSED
3198 || (profile_status
== PROFILE_READ
&& count_max
< REG_BR_PROB_BASE
/10))
3200 loop_optimizer_init (0);
3201 add_noreturn_fake_exit_edges ();
3202 mark_irreducible_loops ();
3203 connect_infinite_loops_to_exit ();
3204 estimate_bb_frequencies (true);
3205 remove_fake_exit_edges ();
3206 loop_optimizer_finalize ();
3208 else if (profile_status
== PROFILE_READ
)
3212 timevar_pop (TV_REBUILD_FREQUENCIES
);