-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpred.h
288 lines (260 loc) · 11.6 KB
/
bpred.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* bpred.h - branch predictor interfaces */
/* SimpleScalar(TM) Tool Suite
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
* All Rights Reserved.
*
* THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR,
* YOU ARE AGREEING TO THESE TERMS AND CONDITIONS.
*
* No portion of this work may be used by any commercial entity, or for any
* commercial purpose, without the prior, written permission of SimpleScalar,
* LLC ([email protected]). Nonprofit and noncommercial use is permitted
* as described below.
*
* 1. SimpleScalar is provided AS IS, with no warranty of any kind, express
* or implied. The user of the program accepts full responsibility for the
* application of the program and the use of any results.
*
* 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be
* downloaded, compiled, executed, copied, and modified solely for nonprofit,
* educational, noncommercial research, and noncommercial scholarship
* purposes provided that this notice in its entirety accompanies all copies.
* Copies of the modified software can be delivered to persons who use it
* solely for nonprofit, educational, noncommercial research, and
* noncommercial scholarship purposes provided that this notice in its
* entirety accompanies all copies.
*
* 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY
* PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC ([email protected]).
*
* 4. No nonprofit user may place any restrictions on the use of this software,
* including as modified by the user, by any other authorized user.
*
* 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar
* in compiled or executable form as set forth in Section 2, provided that
* either: (A) it is accompanied by the corresponding machine-readable source
* code, or (B) it is accompanied by a written offer, with no time limit, to
* give anyone a machine-readable copy of the corresponding source code in
* return for reimbursement of the cost of distribution. This written offer
* must permit verbatim duplication by anyone, or (C) it is distributed by
* someone who received only the executable form, and is accompanied by a
* copy of the written offer of source code.
*
* 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is
* currently maintained by SimpleScalar LLC ([email protected]). US Mail:
* 2395 Timbercrest Court, Ann Arbor, MI 48105.
*
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
#ifndef BPRED_H
#define BPRED_H
#define dassert(a) assert(a)
#include <stdio.h>
#include "host.h"
#include "misc.h"
#include "machine.h"
#include "stats.h"
/*
* This module implements a number of branch predictor mechanisms. The
* following predictors are supported:
*
* BPred2Level: two level adaptive branch predictor
*
* It can simulate many prediction mechanisms that have up to
* two levels of tables. Parameters are:
* N # entries in first level (# of shift register(s))
* W width of shift register(s)
* M # entries in 2nd level (# of counters, or other FSM)
* One BTB entry per level-2 counter.
*
* Configurations: N, W, M
*
* counter based: 1, 0, M
*
* GAg : 1, W, 2^W
* GAp : 1, W, M (M > 2^W)
* PAg : N, W, 2^W
* PAp : N, W, M (M == 2^(N+W))
*
* BPred2bit: a simple direct mapped bimodal predictor
*
* This predictor has a table of two bit saturating counters.
* Where counter states 0 & 1 are predict not taken and
* counter states 2 & 3 are predict taken, the per-branch counters
* are incremented on taken branches and decremented on
* no taken branches. One BTB entry per counter.
*
* BPredTaken: static predict branch taken
*
* BPredNotTaken: static predict branch not taken
*
*/
/* branch predictor types */
enum bpred_class {
BPredComb, /* combined predictor (McFarling) */
BPred2Level, /* 2-level correlating pred w/2-bit counters */
BPred2bit, /* 2-bit saturating cntr pred (dir mapped) */
BPredTaken, /* static predict taken */
BPredNotTaken, /* static predict not taken */
BPred_NUM,
BPredMine
};
/* an entry in a BTB */
struct bpred_btb_ent_t {
md_addr_t addr; /* address of branch being tracked */
enum md_opcode op; /* opcode of branch corresp. to addr */
md_addr_t target; /* last destination of branch when taken */
struct bpred_btb_ent_t *prev, *next; /* lru chaining pointers */
};
/* direction predictor def */
struct bpred_dir_t {
enum bpred_class class; /* type of predictor */
union {
struct {
unsigned int size; /* number of entries in direct-mapped table */
unsigned char *table; /* prediction state table */
} bimod;
struct {
int l1size; /* level-1 size, number of history regs */
int l2size; /* level-2 size, number of pred states */
int shift_width; /* amount of history in level-1 shift regs */
int xor; /* history xor address flag */
int *shiftregs; /* level-1 history table */
unsigned char *l2table; /* level-2 prediction state table */
} two;
int mineState; /*current state of the mine predictor*/
} config;
};
/* branch predictor def */
struct bpred_t {
enum bpred_class class; /* type of predictor */
struct {
struct bpred_dir_t *bimod; /* first direction predictor */
struct bpred_dir_t *twolev; /* second direction predictor */
struct bpred_dir_t *meta; /* meta predictor */
struct bpred_dir_t *mine; /*My own predictor */
} dirpred;
struct {
int sets; /* num BTB sets */
int assoc; /* BTB associativity */
struct bpred_btb_ent_t *btb_data; /* BTB addr-prediction table */
} btb;
struct {
int size; /* return-address stack size */
int tos; /* top-of-stack */
struct bpred_btb_ent_t *stack; /* return-address stack */
} retstack;
/* stats */
counter_t addr_hits; /* num correct addr-predictions */
counter_t dir_hits; /* num correct dir-predictions (incl addr) */
counter_t used_ras; /* num RAS predictions used */
counter_t used_bimod; /* num bimodal predictions used (BPredComb) */
counter_t used_2lev; /* num 2-level predictions used (BPredComb) */
counter_t jr_hits; /* num correct addr-predictions for JR's */
counter_t jr_seen; /* num JR's seen */
counter_t jr_non_ras_hits; /* num correct addr-preds for non-RAS JR's */
counter_t jr_non_ras_seen; /* num non-RAS JR's seen */
counter_t misses; /* num incorrect predictions */
counter_t lookups; /* num lookups */
counter_t retstack_pops; /* number of times a value was popped */
counter_t retstack_pushes; /* number of times a value was pushed */
counter_t ras_hits; /* num correct return-address predictions */
};
//TODO: add mine here
/* branch predictor update information */
struct bpred_update_t {
char *pdir1; /* direction-1 predictor counter */
char *pdir2; /* direction-2 predictor counter */
char *pmeta; /* meta predictor counter */
struct { /* predicted directions */
unsigned int ras : 1; /* RAS used */
unsigned int bimod : 1; /* bimodal predictor */
unsigned int twolev : 1; /* 2-level predictor */
unsigned int meta : 1; /* meta predictor (0..bimod / 1..2lev) */
} dir;
};
/* create a branch predictor */
struct bpred_t * /* branch predictory instance */
bpred_create(enum bpred_class class, /* type of predictor to create */
unsigned int bimod_size, /* bimod table size */
unsigned int l1size, /* level-1 table size */
unsigned int l2size, /* level-2 table size */
unsigned int meta_size, /* meta predictor table size */
unsigned int shift_width, /* history register width */
unsigned int xor, /* history xor address flag */
unsigned int btb_sets, /* number of sets in BTB */
unsigned int btb_assoc, /* BTB associativity */
unsigned int retstack_size);/* num entries in ret-addr stack */
/* create a branch direction predictor */
struct bpred_dir_t * /* branch direction predictor instance */
bpred_dir_create (
enum bpred_class class, /* type of predictor to create */
unsigned int l1size, /* level-1 table size */
unsigned int l2size, /* level-2 table size (if relevant) */
unsigned int shift_width, /* history register width */
unsigned int xor); /* history xor address flag */
/* print branch predictor configuration */
void
bpred_config(struct bpred_t *pred, /* branch predictor instance */
FILE *stream); /* output stream */
/* print predictor stats */
void
bpred_stats(struct bpred_t *pred, /* branch predictor instance */
FILE *stream); /* output stream */
/* register branch predictor stats */
void
bpred_reg_stats(struct bpred_t *pred, /* branch predictor instance */
struct stat_sdb_t *sdb);/* stats database */
/* reset stats after priming, if appropriate */
void bpred_after_priming(struct bpred_t *bpred);
/* probe a predictor for a next fetch address, the predictor is probed
with branch address BADDR, the branch target is BTARGET (used for
static predictors), and OP is the instruction opcode (used to simulate
predecode bits; a pointer to the predictor state entry (or null for jumps)
is returned in *DIR_UPDATE_PTR (used for updating predictor state),
and the non-speculative top-of-stack is returned in stack_recover_idx
(used for recovering ret-addr stack after mis-predict). */
md_addr_t /* predicted branch target addr */
bpred_lookup(struct bpred_t *pred, /* branch predictor instance */
md_addr_t baddr, /* branch address */
md_addr_t btarget, /* branch target if taken */
enum md_opcode op, /* opcode of instruction */
int is_call, /* non-zero if inst is fn call */
int is_return, /* non-zero if inst is fn return */
struct bpred_update_t *dir_update_ptr, /* pred state pointer */
int *stack_recover_idx); /* Non-speculative top-of-stack;
* used on mispredict recovery */
/* Speculative execution can corrupt the ret-addr stack. So for each
* lookup we return the top-of-stack (TOS) at that point; a mispredicted
* branch, as part of its recovery, restores the TOS using this value --
* hopefully this uncorrupts the stack. */
void
bpred_recover(struct bpred_t *pred, /* branch predictor instance */
md_addr_t baddr, /* branch address */
int stack_recover_idx); /* Non-speculative top-of-stack;
* used on mispredict recovery */
/* update the branch predictor, only useful for stateful predictors; updates
entry for instruction type OP at address BADDR. BTB only gets updated
for branches which are taken. Inst was determined to jump to
address BTARGET and was taken if TAKEN is non-zero. Predictor
statistics are updated with result of prediction, indicated by CORRECT and
PRED_TAKEN, predictor state to be updated is indicated by *DIR_UPDATE_PTR
(may be NULL for jumps, which shouldn't modify state bits). Note if
bpred_update is done speculatively, branch-prediction may get polluted. */
void
bpred_update(struct bpred_t *pred, /* branch predictor instance */
md_addr_t baddr, /* branch address */
md_addr_t btarget, /* resolved branch target */
int taken, /* non-zero if branch was taken */
int pred_taken, /* non-zero if branch was pred taken */
int correct, /* was earlier prediction correct? */
enum md_opcode op, /* opcode of instruction */
struct bpred_update_t *dir_update_ptr); /* pred state pointer */
#ifdef foo0
/* OBSOLETE */
/* dump branch predictor state (for debug) */
void
bpred_dump(struct bpred_t *pred, /* branch predictor instance */
FILE *stream); /* output stream */
#endif
#endif /* BPRED_H */