1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
/* Branch prediction routines for the GNU compiler.
Copyright (C) 2000 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* References:
[1] "Branch Prediction for Free"
Ball and Larus; PLDI '93.
[2] "Static Branch Frequency and Program Profile Analysis"
Wu and Larus; MICRO-27.
[3] "Corpus-based Static Branch Prediction"
Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95.
*/
#include "config.h"
#include "system.h"
#include "tree.h"
#include "rtl.h"
#include "tm_p.h"
#include "basic-block.h"
#include "insn-config.h"
#include "regs.h"
#include "hard-reg-set.h"
#include "flags.h"
#include "output.h"
#include "function.h"
#include "except.h"
#include "toplev.h"
#include "recog.h"
#include "insn-flags.h"
#include "expr.h"
/* Statically estimate the probability that a branch will be taken.
??? In the next revision there will be a number of other predictors added
from the above references. Further, each heuristic will be factored out
into its own function for clarity (and to facilitate the combination of
predictions). */
void
estimate_probability (loops_info)
struct loops *loops_info;
{
int i;
/* Try to predict out blocks in a loop that are not part of a
natural loop. */
for (i = 0; i < loops_info->num; i++)
{
int j;
for (j = loops_info->array[i].first->index;
j <= loops_info->array[i].last->index;
++j)
{
edge e;
if (! TEST_BIT (loops_info->array[i].nodes, j))
for (e = BASIC_BLOCK(j)->pred; e; e = e->pred_next)
if (TEST_BIT (loops_info->array[i].nodes, e->src->index))
{
rtx last_insn = BLOCK_END (e->src->index);
rtx cond, earliest;
if (GET_CODE (last_insn) != JUMP_INSN
|| ! condjump_p (last_insn) || simplejump_p (last_insn))
continue;
cond = get_condition (last_insn, &earliest);
if (! cond)
continue;
if (! find_reg_note (last_insn, REG_BR_PROB, 0))
REG_NOTES (last_insn)
= gen_rtx_EXPR_LIST (REG_BR_PROB,
GEN_INT (REG_BR_PROB_BASE),
REG_NOTES (last_insn));
}
}
}
/* Attempt to predict conditional jumps using a number of heuristics.
For each conditional jump, we try each heuristic in a fixed order.
If more than one heuristic applies to a particular branch, the first
is used as the prediction for the branch. */
for (i = 0; i < n_basic_blocks - 1; i++)
{
rtx last_insn = BLOCK_END (i);
rtx cond, earliest;
int prob = 0;
if (GET_CODE (last_insn) != JUMP_INSN
|| ! condjump_p (last_insn) || simplejump_p (last_insn))
continue;
cond = get_condition (last_insn, &earliest);
if (! cond)
continue;
/* Try "pointer heuristic."
A comparison ptr == 0 is predicted as false.
Similarly, a comparison ptr1 == ptr2 is predicted as false. */
prob = 0;
switch (GET_CODE (cond))
{
case EQ:
if (GET_CODE (XEXP (cond, 0)) == REG
&& REGNO_POINTER_FLAG (REGNO (XEXP (cond, 0)))
&& (XEXP (cond, 1) == const0_rtx
|| (GET_CODE (XEXP (cond, 1)) == REG
&& REGNO_POINTER_FLAG (REGNO (XEXP (cond, 1))))))
prob = REG_BR_PROB_BASE / 10;
break;
case NE:
if (GET_CODE (XEXP (cond, 0)) == REG
&& REGNO_POINTER_FLAG (REGNO (XEXP (cond, 0)))
&& (XEXP (cond, 1) == const0_rtx
|| (GET_CODE (XEXP (cond, 1)) == REG
&& REGNO_POINTER_FLAG (REGNO (XEXP (cond, 1))))))
prob = REG_BR_PROB_BASE / 2;
break;
default:
prob = 0;
}
if (prob && ! find_reg_note (last_insn, REG_BR_PROB, 0))
REG_NOTES (last_insn)
= gen_rtx_EXPR_LIST (REG_BR_PROB, GEN_INT (prob),
REG_NOTES (last_insn));
/* Try "opcode heuristic."
EQ tests are usually false and NE tests are usually true. Also,
most quantities are positive, so we can make the appropriate guesses
about signed comparisons against zero. */
switch (GET_CODE (cond))
{
case CONST_INT:
/* Unconditional branch. */
prob = REG_BR_PROB_BASE / 2;
break;
case EQ:
prob = REG_BR_PROB_BASE / 10;
break;
case NE:
prob = REG_BR_PROB_BASE / 2;
break;
case LE:
case LT:
if (XEXP (cond, 1) == const0_rtx)
prob = REG_BR_PROB_BASE / 10;
break;
case GE:
case GT:
if (XEXP (cond, 1) == const0_rtx
|| (GET_CODE (XEXP (cond, 1)) == CONST_INT
&& INTVAL (XEXP (cond, 1)) == -1))
prob = REG_BR_PROB_BASE / 2;
break;
default:
prob = 0;
}
if (! find_reg_note (last_insn, REG_BR_PROB, 0))
REG_NOTES (last_insn)
= gen_rtx_EXPR_LIST (REG_BR_PROB, GEN_INT (prob),
REG_NOTES (last_insn));
}
}
|