1 |
2 |
cvs |
/* branch_predict.c -- branch prediction simulation
|
2 |
|
|
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
|
3 |
|
|
|
4 |
|
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
5 |
|
|
|
6 |
|
|
This program 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 of the License, or
|
9 |
|
|
(at your option) any later version.
|
10 |
|
|
|
11 |
|
|
This program 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.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with this program; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
19 |
|
|
|
20 |
|
|
/* Branch prediction functions.
|
21 |
|
|
At the moment this functions only simulate functionality of branch
|
22 |
|
|
prediction and do not influence on fetche/decode/execute stages.
|
23 |
|
|
They are here only to verify performance of various branch
|
24 |
|
|
prediction configurations.
|
25 |
|
|
|
26 |
|
|
*/
|
27 |
|
|
|
28 |
|
|
#include <stdio.h>
|
29 |
|
|
#include <string.h>
|
30 |
|
|
#include <errno.h>
|
31 |
|
|
#include <stdarg.h>
|
32 |
|
|
|
33 |
|
|
#include "branch_predict.h"
|
34 |
|
|
#include "abstract.h"
|
35 |
|
|
#include "stats.h"
|
36 |
|
|
|
37 |
|
|
/* Branch prediction buffer */
|
38 |
|
|
|
39 |
|
|
/* Length of BPB */
|
40 |
|
|
#define BPB_LEN 32
|
41 |
|
|
|
42 |
|
|
/* Number of BPB ways (1, 2, 3 etc.). */
|
43 |
|
|
#define BPB_WAYS 2
|
44 |
|
|
|
45 |
|
|
/* Number of prediction states (2, 4, 8 etc.). */
|
46 |
|
|
#define BPB_PSTATES 2
|
47 |
|
|
|
48 |
|
|
/* Number of usage states (2, 3, 4 etc.). */
|
49 |
|
|
#define BPB_USTATES 2
|
50 |
|
|
|
51 |
|
|
/* branch prediction buffer entry */
|
52 |
|
|
struct bpb_entry {
|
53 |
|
|
struct {
|
54 |
|
|
unsigned long addr; /* address of a branch insn */
|
55 |
|
|
int taken; /* taken == 1, not taken == 0 OR */
|
56 |
|
|
/* strongly taken == 3, taken == 2,
|
57 |
|
|
not taken == 1, strongly not taken == 0 */
|
58 |
|
|
int lru; /* least recently == 0 */
|
59 |
|
|
} way[BPB_WAYS];
|
60 |
|
|
} bpb[BPB_LEN];
|
61 |
|
|
|
62 |
|
|
/* First check if branch is already in the cache and if it is:
|
63 |
|
|
- increment BPB hit stats,
|
64 |
|
|
- set 'lru' at this way to BPB_USTATES - 1 and
|
65 |
|
|
decrement 'lru' of other ways unless they have reached 0,
|
66 |
|
|
- increment correct/incorrect stats according to BPB 'taken' field
|
67 |
|
|
and 'taken' variable,
|
68 |
|
|
- increment or decrement BPB taken field according to 'taken' variable
|
69 |
|
|
and if not:
|
70 |
|
|
- increment BPB miss stats
|
71 |
|
|
- find lru way and entry and replace old address with 'addr' and
|
72 |
|
|
'taken' field with (BPB_PSTATES/2 - 1) + 'taken'
|
73 |
|
|
- set 'lru' with BPB_USTATES - 1 and decrement 'lru' of other
|
74 |
|
|
ways unless they have reached 0
|
75 |
|
|
*/
|
76 |
|
|
|
77 |
|
|
void bpb_update(unsigned long addr, int taken)
|
78 |
|
|
{
|
79 |
|
|
int entry, way = -1;
|
80 |
|
|
int i;
|
81 |
|
|
|
82 |
|
|
/* Calc entry. */
|
83 |
|
|
entry = addr % BPB_LEN;
|
84 |
|
|
|
85 |
|
|
/* Scan all ways and try to find our addr. */
|
86 |
|
|
for (i = 0; i < BPB_WAYS; i++)
|
87 |
|
|
if (bpb[entry].way[i].addr == addr)
|
88 |
|
|
way = i;
|
89 |
|
|
|
90 |
|
|
/* Did we find our cached branch? */
|
91 |
|
|
if (way >= 0) { /* Yes, we did. */
|
92 |
|
|
mstats.bpb.hit++;
|
93 |
|
|
|
94 |
|
|
for (i = 0; i < BPB_WAYS; i++)
|
95 |
|
|
if (bpb[entry].way[i].lru)
|
96 |
|
|
bpb[entry].way[i].lru--;
|
97 |
|
|
bpb[entry].way[way].lru = BPB_USTATES - 1;
|
98 |
|
|
|
99 |
|
|
if (bpb[entry].way[way].taken / (BPB_PSTATES / 2) == taken)
|
100 |
|
|
mstats.bpb.correct++;
|
101 |
|
|
else
|
102 |
|
|
mstats.bpb.incorrect++;
|
103 |
|
|
|
104 |
|
|
if (taken && (bpb[entry].way[way].taken < BPB_PSTATES - 1))
|
105 |
|
|
bpb[entry].way[way].taken++;
|
106 |
|
|
else
|
107 |
|
|
if (!taken && (bpb[entry].way[way].taken))
|
108 |
|
|
bpb[entry].way[way].taken--;
|
109 |
|
|
}
|
110 |
|
|
else { /* No, we didn't. */
|
111 |
|
|
int minlru = BPB_USTATES - 1;
|
112 |
|
|
int minway = 0;
|
113 |
|
|
|
114 |
|
|
mstats.bpb.miss++;
|
115 |
|
|
|
116 |
|
|
for (i = 0; i < BPB_WAYS; i++)
|
117 |
|
|
if (bpb[entry].way[i].lru < minlru)
|
118 |
|
|
minway = i;
|
119 |
|
|
|
120 |
|
|
bpb[entry].way[minway].addr = addr;
|
121 |
|
|
bpb[entry].way[minway].taken = (BPB_PSTATES / 2 - 1) + taken;
|
122 |
|
|
for (i = 0; i < BPB_WAYS; i++)
|
123 |
|
|
if (bpb[entry].way[i].lru)
|
124 |
|
|
bpb[entry].way[i].lru--;
|
125 |
|
|
bpb[entry].way[minway].lru = BPB_USTATES - 1;
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/* Branch target instruction cache */
|
130 |
|
|
|
131 |
|
|
/* Length of BTIC */
|
132 |
|
|
#define BTIC_LEN 32
|
133 |
|
|
|
134 |
|
|
/* Number of BTIC ways (1, 2, 3 etc.). */
|
135 |
|
|
#define BTIC_WAYS 2
|
136 |
|
|
|
137 |
|
|
/* Number of usage states (2, 3, 4 etc.). */
|
138 |
|
|
#define BTIC_USTATES 2
|
139 |
|
|
|
140 |
|
|
struct btic_entry {
|
141 |
|
|
struct {
|
142 |
|
|
unsigned long addr; /* cached target address of a branch */
|
143 |
|
|
int lru; /* least recently used */
|
144 |
|
|
char *insn; /* cached insn at target address (not used currently) */
|
145 |
|
|
} way[BTIC_WAYS];
|
146 |
|
|
} btic[BTIC_LEN];
|
147 |
|
|
|
148 |
|
|
/* First check if target addr is already in the cache and if it is:
|
149 |
|
|
- increment BTIC hit stats,
|
150 |
|
|
- set 'lru' at this way to BTIC_USTATES - 1 and
|
151 |
|
|
decrement 'lru' of other ways unless they have reached 0,
|
152 |
|
|
and if not:
|
153 |
|
|
- increment BTIC miss stats
|
154 |
|
|
- find lru way and entry and replace old address with 'addr' and
|
155 |
|
|
'insn' with NULL
|
156 |
|
|
- set 'lru' with BTIC_USTATES - 1 and decrement 'lru' of other
|
157 |
|
|
ways unless they have reached 0
|
158 |
|
|
*/
|
159 |
|
|
|
160 |
|
|
void btic_update(unsigned long targetaddr)
|
161 |
|
|
{
|
162 |
|
|
int entry, way = -1;
|
163 |
|
|
int i;
|
164 |
|
|
|
165 |
|
|
/* Calc entry. */
|
166 |
|
|
entry = targetaddr % BTIC_LEN;
|
167 |
|
|
|
168 |
|
|
/* Scan all ways and try to find our addr. */
|
169 |
|
|
for (i = 0; i < BTIC_WAYS; i++)
|
170 |
|
|
if (btic[entry].way[i].addr == targetaddr)
|
171 |
|
|
way = i;
|
172 |
|
|
|
173 |
|
|
/* Did we find our cached branch? */
|
174 |
|
|
if (way >= 0) { /* Yes, we did. */
|
175 |
|
|
mstats.btic.hit++;
|
176 |
|
|
|
177 |
|
|
for (i = 0; i < BTIC_WAYS; i++)
|
178 |
|
|
if (btic[entry].way[i].lru)
|
179 |
|
|
btic[entry].way[i].lru--;
|
180 |
|
|
btic[entry].way[way].lru = BTIC_USTATES - 1;
|
181 |
|
|
}
|
182 |
|
|
else { /* No, we didn't. */
|
183 |
|
|
int minlru = BTIC_USTATES - 1;
|
184 |
|
|
int minway = 0;
|
185 |
|
|
|
186 |
|
|
mstats.btic.miss++;
|
187 |
|
|
|
188 |
|
|
for (i = 0; i < BTIC_WAYS; i++)
|
189 |
|
|
if (btic[entry].way[i].lru < minlru)
|
190 |
|
|
minway = i;
|
191 |
|
|
|
192 |
|
|
btic[entry].way[minway].addr = targetaddr;
|
193 |
|
|
btic[entry].way[minway].insn = NULL;
|
194 |
|
|
for (i = 0; i < BTIC_WAYS; i++)
|
195 |
|
|
if (btic[entry].way[i].lru)
|
196 |
|
|
btic[entry].way[i].lru--;
|
197 |
|
|
btic[entry].way[minway].lru = BTIC_USTATES - 1;
|
198 |
|
|
}
|
199 |
|
|
}
|