1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 [1] "Branch Prediction for Free"
24 Ball and Larus; PLDI '93.
25 [2] "Static Branch Frequency and Program Profile Analysis"
26 Wu and Larus; MICRO-27.
27 [3] "Corpus-based Static Branch Prediction"
28 Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95.
38 #include "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "insn-config.h"
51 /* Random guesstimation given names. */
52 #define PROB_NEVER (0)
53 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1)
54 #define PROB_UNLIKELY (REG_BR_PROB_BASE * 4 / 10 - 1)
55 #define PROB_EVEN (REG_BR_PROB_BASE / 2)
56 #define PROB_LIKELY (REG_BR_PROB_BASE - PROB_UNLIKELY)
57 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
58 #define PROB_ALWAYS (REG_BR_PROB_BASE)
60 static void combine_predictions_for_insn PARAMS ((rtx, basic_block));
61 static void dump_prediction PARAMS ((enum br_predictor, int,
64 /* Information we hold about each branch predictor.
65 Filled using information from predict.def. */
68 const char *name; /* Name used in the debugging dumps. */
69 int hitrate; /* Expected hitrate used by
70 predict_insn_def call. */
73 #define DEF_PREDICTOR(ENUM, NAME, HITRATE) {NAME, HITRATE},
74 struct predictor_info predictor_info[] = {
75 #include "predict.def"
77 /* Upper bound on non-language-specific builtins. */
83 predict_insn (insn, predictor, probability)
86 enum br_predictor predictor;
88 if (!any_condjump_p (insn))
91 = gen_rtx_EXPR_LIST (REG_BR_PRED,
92 gen_rtx_CONCAT (VOIDmode,
93 GEN_INT ((int) predictor),
94 GEN_INT ((int) probability)),
98 /* Predict insn by given predictor. */
100 predict_insn_def (insn, predictor, taken)
102 enum br_predictor predictor;
103 enum prediction taken;
105 int probability = predictor_info[(int) predictor].hitrate;
107 probability = REG_BR_PROB_BASE - probability;
108 predict_insn (insn, predictor, probability);
111 /* Predict edge E with given probability if possible. */
113 predict_edge (e, predictor, probability)
116 enum br_predictor predictor;
119 last_insn = e->src->end;
121 /* We can store the branch prediction information only about
122 conditional jumps. */
123 if (!any_condjump_p (last_insn))
126 /* We always store probability of branching. */
127 if (e->flags & EDGE_FALLTHRU)
128 probability = REG_BR_PROB_BASE - probability;
130 predict_insn (last_insn, predictor, probability);
133 /* Predict edge E by given predictor if possible. */
135 predict_edge_def (e, predictor, taken)
137 enum br_predictor predictor;
138 enum prediction taken;
140 int probability = predictor_info[(int) predictor].hitrate;
143 probability = REG_BR_PROB_BASE - probability;
144 predict_edge (e, predictor, probability);
147 /* Invert all branch predictions or probability notes in the INSN. This needs
148 to be done each time we invert the condition used by the jump. */
150 invert_br_probabilities (insn)
153 rtx note = REG_NOTES (insn);
157 if (REG_NOTE_KIND (note) == REG_BR_PROB)
158 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
159 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
160 XEXP (XEXP (note, 0), 1)
161 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
162 note = XEXP (note, 1);
166 /* Dump information about the branch prediction to the output file. */
168 dump_prediction (predictor, probability, bb)
169 enum br_predictor predictor;
178 while (e->flags & EDGE_FALLTHRU)
181 fprintf (rtl_dump_file, " %s heuristics: %.1f%%",
182 predictor_info[predictor].name,
183 probability * 100.0 / REG_BR_PROB_BASE);
186 fprintf (rtl_dump_file, " exec %i hit %i (%.1f%%)",
187 bb->count, e->count, e->count * 100.0 / bb->count);
188 fprintf (rtl_dump_file, "\n");
191 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
192 note if not already present. Remove now useless REG_BR_PRED notes. */
194 combine_predictions_for_insn (insn, bb)
198 rtx prob_note = find_reg_note (insn, REG_BR_PROB, 0);
199 rtx *pnote = ®_NOTES (insn);
200 int best_probability = PROB_EVEN;
201 int best_predictor = END_PREDICTORS;
204 fprintf (rtl_dump_file, "Predictions for insn %i\n", INSN_UID (insn));
206 /* We implement "first match" heuristics and use probability guessed
207 by predictor with smallest index. In future we will use better
208 probability combination techniques. */
211 rtx *next_pnote = &XEXP (*pnote, 1);
212 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
214 int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
215 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
217 dump_prediction (predictor, probability, bb);
218 if (best_predictor > predictor)
219 best_probability = probability, best_predictor = predictor;
220 *pnote = XEXP (*pnote, 1);
224 dump_prediction (PRED_FIRST_MATCH, best_probability, bb);
228 = gen_rtx_EXPR_LIST (REG_BR_PROB,
229 GEN_INT (best_probability), REG_NOTES (insn));
233 /* Statically estimate the probability that a branch will be taken.
234 ??? In the next revision there will be a number of other predictors added
235 from the above references. Further, each heuristic will be factored out
236 into its own function for clarity (and to facilitate the combination of
240 estimate_probability (loops_info)
241 struct loops *loops_info;
243 sbitmap *dominators, *post_dominators;
246 dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
247 post_dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
248 calculate_dominance_info (NULL, dominators, 0);
249 calculate_dominance_info (NULL, post_dominators, 1);
251 /* Try to predict out blocks in a loop that are not part of a
253 for (i = 0; i < loops_info->num; i++)
257 for (j = loops_info->array[i].first->index;
258 j <= loops_info->array[i].last->index;
261 if (TEST_BIT (loops_info->array[i].nodes, j))
263 int header_found = 0;
266 /* Loop branch heruistics - predict as taken an edge back to
268 for (e = BASIC_BLOCK(j)->succ; e; e = e->succ_next)
269 if (e->dest == loops_info->array[i].header)
272 predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
274 /* Loop exit heruistics - predict as not taken an edge exiting
275 the loop if the conditinal has no loop header successors */
277 for (e = BASIC_BLOCK(j)->succ; e; e = e->succ_next)
278 if (e->dest->index <= 0
279 || !TEST_BIT (loops_info->array[i].nodes, e->dest->index))
280 predict_edge_def (e, PRED_LOOP_EXIT, NOT_TAKEN);
285 /* Attempt to predict conditional jumps using a number of heuristics.
286 For each conditional jump, we try each heuristic in a fixed order.
287 If more than one heuristic applies to a particular branch, the first
288 is used as the prediction for the branch. */
289 for (i = 0; i < n_basic_blocks - 1; i++)
291 basic_block bb = BASIC_BLOCK (i);
292 rtx last_insn = bb->end;
296 /* If block has no sucessor, predict all possible paths to
297 it as improbable, as the block contains a call to a noreturn
298 function and thus can be executed only once. */
299 if (bb->succ == NULL)
302 for (y = 0; y < n_basic_blocks; y++)
303 if (!TEST_BIT (post_dominators[y], i))
305 for (e = BASIC_BLOCK (y)->succ; e; e = e->succ_next)
306 if (e->dest->index >= 0
307 && TEST_BIT (post_dominators[e->dest->index], i))
308 predict_edge_def (e, PRED_NORETURN, NOT_TAKEN);
312 if (GET_CODE (last_insn) != JUMP_INSN
313 || ! any_condjump_p (last_insn))
316 if (find_reg_note (last_insn, REG_BR_PROB, 0))
319 for (e = bb->succ; e; e = e->succ_next)
321 /* Predict edges to blocks that return immediately to be
322 improbable. These are usually used to signal error states. */
323 if (e->dest == EXIT_BLOCK_PTR
324 || (e->dest->succ && !e->dest->succ->succ_next
325 && e->dest->succ->dest == EXIT_BLOCK_PTR))
326 predict_edge_def (e, PRED_ERROR_RETURN, NOT_TAKEN);
328 /* Look for block we are guarding (ie we dominate it,
329 but it doesn't postdominate us). */
330 if (e->dest != EXIT_BLOCK_PTR
332 && TEST_BIT (dominators[e->dest->index], e->src->index)
333 && !TEST_BIT (post_dominators[e->src->index], e->dest->index))
336 /* The call heuristic claims that a guarded function call
337 is improbable. This is because such calls are often used
338 to signal exceptional situations such as printing error
340 for (insn = e->dest->head; insn != NEXT_INSN (e->dest->end);
341 insn = NEXT_INSN (insn))
342 if (GET_CODE (insn) == CALL_INSN
343 /* Constant and pure calls are hardly used to signalize
344 something exceptional. */
345 && ! CONST_CALL_P (insn))
347 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
353 cond = get_condition (last_insn, &earliest);
357 /* Try "pointer heuristic."
358 A comparison ptr == 0 is predicted as false.
359 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
360 switch (GET_CODE (cond))
363 if (GET_CODE (XEXP (cond, 0)) == REG
364 && REG_POINTER (XEXP (cond, 0))
365 && (XEXP (cond, 1) == const0_rtx
366 || (GET_CODE (XEXP (cond, 1)) == REG
367 && REG_POINTER (XEXP (cond, 1)))))
369 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
372 if (GET_CODE (XEXP (cond, 0)) == REG
373 && REG_POINTER (XEXP (cond, 0))
374 && (XEXP (cond, 1) == const0_rtx
375 || (GET_CODE (XEXP (cond, 1)) == REG
376 && REG_POINTER (XEXP (cond, 1)))))
377 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
384 /* Try "opcode heuristic."
385 EQ tests are usually false and NE tests are usually true. Also,
386 most quantities are positive, so we can make the appropriate guesses
387 about signed comparisons against zero. */
388 switch (GET_CODE (cond))
391 /* Unconditional branch. */
392 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
393 cond == const0_rtx ? NOT_TAKEN : TAKEN);
398 predict_insn_def (last_insn, PRED_OPCODE, NOT_TAKEN);
402 predict_insn_def (last_insn, PRED_OPCODE, TAKEN);
405 predict_insn_def (last_insn, PRED_OPCODE, TAKEN);
408 predict_insn_def (last_insn, PRED_OPCODE, NOT_TAKEN);
412 if (XEXP (cond, 1) == const0_rtx
413 || (GET_CODE (XEXP (cond, 1)) == CONST_INT
414 && INTVAL (XEXP (cond, 1)) == -1))
415 predict_insn_def (last_insn, PRED_OPCODE, NOT_TAKEN);
419 if (XEXP (cond, 1) == const0_rtx
420 || (GET_CODE (XEXP (cond, 1)) == CONST_INT
421 && INTVAL (XEXP (cond, 1)) == -1))
422 predict_insn_def (last_insn, PRED_OPCODE, TAKEN);
430 /* Attach the combined probability to each conditional jump. */
431 for (i = 0; i < n_basic_blocks - 1; i++)
433 rtx last_insn = BLOCK_END (i);
435 if (GET_CODE (last_insn) != JUMP_INSN
436 || ! any_condjump_p (last_insn))
438 combine_predictions_for_insn (last_insn, BASIC_BLOCK (i));
440 sbitmap_vector_free (post_dominators);
441 sbitmap_vector_free (dominators);
444 /* __builtin_expect dropped tokens into the insn stream describing
445 expected values of registers. Generate branch probabilities
446 based off these values. */
449 expected_value_to_br_prob ()
451 rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
453 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
455 switch (GET_CODE (insn))
458 /* Look for expected value notes. */
459 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
461 ev = NOTE_EXPECTED_VALUE (insn);
462 ev_reg = XEXP (ev, 0);
467 /* Never propagate across labels. */
472 /* Look for insns that clobber the EV register. */
473 if (ev && reg_set_p (ev_reg, insn))
478 /* Look for simple conditional branches. If we havn't got an
479 expected value yet, no point going further. */
480 if (GET_CODE (insn) != JUMP_INSN || ev == NULL_RTX)
482 if (! any_condjump_p (insn))
487 /* Collect the branch condition, hopefully relative to EV_REG. */
488 /* ??? At present we'll miss things like
489 (expected_value (eq r70 0))
491 (set r80 (lt r70 r71))
492 (set pc (if_then_else (ne r80 0) ...))
493 as canonicalize_condition will render this to us as
495 Could use cselib to try and reduce this further. */
496 cond = XEXP (SET_SRC (PATTERN (insn)), 0);
497 cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg);
499 || XEXP (cond, 0) != ev_reg
500 || GET_CODE (XEXP (cond, 1)) != CONST_INT)
503 /* Substitute and simplify. Given that the expression we're
504 building involves two constants, we should wind up with either
506 cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
507 XEXP (ev, 1), XEXP (cond, 1));
508 cond = simplify_rtx (cond);
510 /* Turn the condition into a scaled branch probability. */
511 if (cond != const1_rtx && cond != const0_rtx)
513 predict_insn_def (insn, PRED_BUILTIN_EXPECT,
514 cond == const1_rtx ? TAKEN : NOT_TAKEN);