1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000 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.
37 #include "basic-block.h"
38 #include "insn-config.h"
40 #include "hard-reg-set.h"
47 #include "insn-flags.h"
52 /* Statically estimate the probability that a branch will be taken.
53 ??? In the next revision there will be a number of other predictors added
54 from the above references. Further, each heuristic will be factored out
55 into its own function for clarity (and to facilitate the combination of
59 estimate_probability (loops_info
)
60 struct loops
*loops_info
;
64 /* Try to predict out blocks in a loop that are not part of a
66 for (i
= 0; i
< loops_info
->num
; i
++)
70 for (j
= loops_info
->array
[i
].first
->index
;
71 j
<= loops_info
->array
[i
].last
->index
;
76 if (! TEST_BIT (loops_info
->array
[i
].nodes
, j
))
77 for (e
= BASIC_BLOCK(j
)->pred
; e
; e
= e
->pred_next
)
78 if (TEST_BIT (loops_info
->array
[i
].nodes
, e
->src
->index
))
80 rtx last_insn
= BLOCK_END (e
->src
->index
);
83 if (GET_CODE (last_insn
) != JUMP_INSN
84 || ! condjump_p (last_insn
) || simplejump_p (last_insn
))
86 cond
= get_condition (last_insn
, &earliest
);
89 if (! find_reg_note (last_insn
, REG_BR_PROB
, 0))
91 = gen_rtx_EXPR_LIST (REG_BR_PROB
,
92 GEN_INT (REG_BR_PROB_BASE
),
93 REG_NOTES (last_insn
));
98 /* Try to predict condjumps using same algorithm as mostly_true_jump. */
99 for (i
= 0; i
< n_basic_blocks
- 1; i
++)
101 rtx last_insn
= BLOCK_END (i
);
105 if (GET_CODE (last_insn
) != JUMP_INSN
106 || ! condjump_p (last_insn
) || simplejump_p (last_insn
))
108 cond
= get_condition (last_insn
, &earliest
);
111 /* EQ tests are usually false and NE tests are usually true. Also,
112 most quantities are positive, so we can make the appropriate guesses
113 about signed comparisons against zero. */
114 switch (GET_CODE (cond
))
117 /* Unconditional branch. */
118 prob
= REG_BR_PROB_BASE
/ 2;
121 prob
= REG_BR_PROB_BASE
/ 10;
124 prob
= REG_BR_PROB_BASE
/ 2;
128 if (XEXP (cond
, 1) == const0_rtx
)
129 prob
= REG_BR_PROB_BASE
/ 10;
133 if (XEXP (cond
, 1) == const0_rtx
134 || (GET_CODE (XEXP (cond
, 1)) == CONST_INT
135 && INTVAL (XEXP (cond
, 1)) == -1))
136 prob
= REG_BR_PROB_BASE
/ 2;
142 if (! find_reg_note (last_insn
, REG_BR_PROB
, 0))
143 REG_NOTES (last_insn
)
144 = gen_rtx_EXPR_LIST (REG_BR_PROB
, GEN_INT (prob
),
145 REG_NOTES (last_insn
));