-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_bp.cc
438 lines (390 loc) · 10.3 KB
/
sim_bp.cc
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <bitset>
#include <map>
#include <cstring>
#include "BTB.cc"
using namespace std;
typedef unsigned long ulong;
enum Flags {
INVALID = 0, VALID = 1
};
typedef map<ulong, int> Map;
class BTB {
private:
typedef struct btb_line {
ulong tag, state, seq, pc;
} BTBLine;
BTBLine*** btbline;
ulong BTB_i, assoc, mispredictions_btb, numsets;
bool predicted;
public:
ulong current_seq;
BTB(ulong assoc, ulong BTB_i) {
if (assoc < 1) {
cout << "Error in BTB configuration!" << endl;
exit(1);
}
this->predicted = false;
this->current_seq = this->mispredictions_btb = 0;
this->assoc = assoc;
this->BTB_i = BTB_i;
this->numsets = (1 << BTB_i);
this->btbline = new BTBLine**[numsets];
for (int i = 0; i < (int) numsets; ++i) {
btbline[i] = new BTBLine*[assoc];
for (int j = 0; j < (int) assoc; ++j) {
btbline[i][j] = new BTBLine;
btbline[i][j]->state = INVALID;
}
}
}
BTBLine* fillblock(ulong address) {
BTBLine* victim = getVictim(address);
victim->tag = getTag(address);
victim->state = VALID;
victim->pc = address;
incrementLRU(victim);
return victim;
}
void incrementLRU(BTBLine* btbline) {
btbline->seq = (++current_seq);
}
inline ulong getTag(ulong address) const {
return (address >> (BTB_i + 2));
}
BTBLine* getVictim(ulong addr) {
BTBLine* victim = NULL;
ulong index = getIndex(addr);
for (int i = 0; i < (int) assoc; ++i) {
if (!(btbline[index][i]->state == VALID))
return btbline[index][i];
}
if (victim == NULL) {
victim = btbline[index][0];
for (int i = 1; i < (int) assoc; ++i) {
if (btbline[index][i]->seq < victim->seq) {
victim = btbline[index][i];
}
}
}
return victim;
}
ulong getIndex(ulong address) const {
return (address >> 2) & ((1 << BTB_i) - 1);
}
BTBLine* findline(ulong address) {
ulong tag = getTag(address);
BTBLine** btbline = this->btbline[getIndex(address)];
for (int i = 0; i < (int) assoc; ++i) {
if ((tag == btbline[i]->tag) && (btbline[i]->state == VALID)) {
return btbline[i];
}
}
return NULL;
}
bool getBTBPredictions() {
return predicted;
}
virtual void accessblock(ulong address, char branch_outcome) {
predicted = false;
BTBLine* btbline = findline(address);
if (btbline == NULL) {
btbline = fillblock(address);
if (branch_outcome == 't') {
mispredictions_btb++;
}
predicted = true;
} else {
incrementLRU(btbline);
}
}
ulong getMispredictionsBtb() const {
return mispredictions_btb;
}
void print_stats() {
printf("Final BTB Tag Array Contents {valid, pc}: \n");
for (int i = 0; i < (int) numsets; i++) {
printf("Set \t %d:", i);
for (int j = 0; j < (int) assoc; j++) {
printf(" {%lu, 0x ", btbline[i][j]->state);
cout << hex << btbline[i][j]->pc << "} ";
}
cout << endl;
}
cout << dec << endl;
}
};
class BranchPredictor {
protected:
Map prediction_table;
Map::iterator it;
ulong branch_count, predictions, mispredictions_bp, B_i;
BTB* btb;
bool branch_taken;
public:
BranchPredictor() {
this->branch_taken = false;
this->B_i = 0;
this->btb = NULL;
this->branch_count = this->predictions = this->mispredictions_bp = 0;
}
virtual ~BranchPredictor() {
}
void setBTB(BTB* btb) {
this->btb = btb;
}
char evaluate_branch(int counter) {
if (counter >= 2)
return 't';
else
return 'n';
}
char find(ulong index) {
it = prediction_table.find(index);
int counter = it->second;
if (it != prediction_table.end())
return evaluate_branch(counter);
else
return '\0';
}
void fill_bp(ulong index, int counter_value) {
prediction_table[index] = counter_value;
}
void Branches() {
branch_count++;
}
virtual void check_bp(ulong address, char branch_outcome) {
predictions++;
if (find(getIndex(address)) != branch_outcome) {
mispredictions_bp++;
branch_taken = false;
} else
branch_taken = true;
}
virtual ulong getIndex(ulong address) {
return (address >> 2) & ((1 << B_i) - 1);
}
virtual void update_ghr(char branch_outcome) {
}
bool getBranchTakenFlag() {
return branch_taken;
}
virtual void update_bp(ulong address, char branch_outcome) {
ulong index = getIndex(address);
it = prediction_table.find(index);
int counter = it->second;
if (branch_outcome == 't')
counter++;
else
counter--;
if (counter > 3)
counter = 3;
if (counter < 0)
counter = 0;
fill_bp(index, counter);
}
virtual void print_table() = 0;
void print_stats() {
if (btb != NULL)
btb->print_stats();
print_table();
printf("Final Branch Predictor Statistics:\n");
printf("a. Number of branches: %lu\n", branch_count);
printf("b. Number of predictions from the branch predictor: %lu\n",
predictions);
printf("c. Number of mispredictions from the branch predictor: %lu\n",
mispredictions_bp);
if (btb == NULL)
printf("d. Number of mispredictions from the BTB: 0\n");
else
printf("d. Number of mispredictions from the BTB: %lu\n",
btb->getMispredictionsBtb());
printf("e. Misprediction Rate: %.2f percent", getMissRates());
}
float getMissRates() {
ulong total_misspredictions =
(btb == NULL) ?
mispredictions_bp :
(mispredictions_bp + btb->getMispredictionsBtb());
return (float) 100 * (total_misspredictions) / (branch_count);
}
}
;
class Bimodal_BranchPredictor: public BranchPredictor {
public:
Bimodal_BranchPredictor(ulong B_i) : BranchPredictor() {
this->B_i = B_i;
for (int i = 0; i < (1 << B_i); i++)
fill_bp(i, 2);
}
void print_table() {
printf("Final Bimodal Table Contents: \n");
for (Map::iterator it = prediction_table.begin();
it != prediction_table.end(); ++it)
std::cout << "table[" << it->first << "]: " << it->second << '\n';
cout << endl;
}
};
class GShare_BanchPredictor: public BranchPredictor {
private:
ulong GHR;
ulong h;
public:
GShare_BanchPredictor(ulong G_i, ulong h) : BranchPredictor(){
this->B_i = G_i;
this->GHR = 0;
this->h = h;
for (int i = 0; i < (1 << B_i); i++)
fill_bp(i, 2);
}
ulong getIndex(ulong address) {
return ((BranchPredictor::getIndex(address)) ^ (GHR << (B_i - h)));
}
void update_ghr(char branch_outcome) {
GHR >>= 1;
if (branch_outcome == 't')
GHR |= (1 << (h - 1));
}
void print_table() {
printf("Final GShare Table Contents: \n");
for (Map::iterator it = prediction_table.begin();
it != prediction_table.end(); ++it)
std::cout << "table[" << it->first << "]: " << it->second << '\n';
cout << endl << "Final GHR Contents: 0x " << hex << GHR << dec << endl << endl;
}
};
class Hybrid_BranchPredictor: public BranchPredictor {
private:
GShare_BanchPredictor* gshare_bp;
Bimodal_BranchPredictor* bimodal_bp;
public:
Hybrid_BranchPredictor(ulong C_i, ulong B_i, ulong G_i, ulong h) : BranchPredictor() {
this->B_i = C_i;
for (int i = 0; i < (1 << C_i); i++)
fill_bp(i, 1);
bimodal_bp = new Bimodal_BranchPredictor(B_i);
gshare_bp = new GShare_BanchPredictor(G_i, h);
}
void update_ghr(char branch_outcome) {
gshare_bp->update_ghr(branch_outcome);
}
void update_bp(ulong address, char branch_outcome) {
int index = getIndex(address);
it = prediction_table.find(index);
int counter = it->second;
if (counter >= 2) {
gshare_bp->update_bp(address, branch_outcome);
if (!gshare_bp->getBranchTakenFlag())
mispredictions_bp++;
} else {
bimodal_bp->update_bp(address, branch_outcome);
if (!bimodal_bp->getBranchTakenFlag())
mispredictions_bp++;
}
if (gshare_bp->getBranchTakenFlag()
&& !bimodal_bp->getBranchTakenFlag())
counter++;
else if (!gshare_bp->getBranchTakenFlag()
&& bimodal_bp->getBranchTakenFlag())
counter--;
if (counter > 3)
counter = 3;
if (counter < 0)
counter = 0;
fill_bp(index, counter);
}
void check_bp(ulong address, char branch_outcome) {
predictions++;
gshare_bp->check_bp(address, branch_outcome);
bimodal_bp->check_bp(address, branch_outcome);
if (btb != NULL)
btb->accessblock(address, branch_outcome);
}
void print_table() {
bimodal_bp->print_table();
gshare_bp->print_table();
printf("Final Chooser Table Contents: \n");
for (Map::iterator it = prediction_table.begin();
it != prediction_table.end(); ++it)
std::cout << "Choice table[" << it->first << "]: " << it->second
<< '\n';
cout << endl;
}
}
;
int main(int argc, char *argv[]) {
char* fname;
ulong address;
ulong B_i, BTB_i, BTB_assoc, G_i, h, C_i;
BTB* btb = NULL;
BranchPredictor* bp;
FILE* pFile;
char* predictor_type = argv[1];
if (!strcmp(predictor_type, "bimodal")) {
B_i = atoi(argv[2]);
BTB_i = atoi(argv[3]);
BTB_assoc = atoi(argv[4]);
fname = argv[5];
printf("Command Line:\n./sim_bp %s %lu %lu %lu %s\n\n", predictor_type,
B_i, BTB_i, BTB_assoc, fname);
bp = new Bimodal_BranchPredictor(B_i);
} else if (!strcmp(predictor_type, "gshare")) {
G_i = atoi(argv[2]);
h = atoi(argv[3]);
BTB_i = atoi(argv[4]);
BTB_assoc = atoi(argv[5]);
fname = argv[6];
printf("Command Line:\n./sim_bp %s %lu %lu %lu %lu %s\n\n",
predictor_type, G_i, h, BTB_i, BTB_assoc, fname);
bp = new GShare_BanchPredictor(G_i, h);
} else if (!strcmp(predictor_type, "hybrid")) {
C_i = atoi(argv[2]);
G_i = atoi(argv[3]);
h = atoi(argv[4]);
B_i = atoi(argv[5]);
BTB_i = atoi(argv[6]);
BTB_assoc = atoi(argv[7]);
fname = argv[8];
printf("Command Line:\n./sim_bp %s %lu %lu %lu %lu %lu %lu %s\n\n",
predictor_type, C_i, G_i, h, B_i, BTB_i, BTB_assoc, fname);
bp = new Hybrid_BranchPredictor(C_i, B_i, G_i, h);
} else {
printf("Error in configuration. please check the arguments passed");
exit(-1);
}
pFile = fopen(fname, "r");
if (pFile == 0) {
printf("Trace file read problem\n");
exit(1);
}
if (!((BTB_i & BTB_assoc) == 0))
btb = new BTB(BTB_assoc, BTB_i);
bp->setBTB(btb);
char line[60];
while (fgets(line, 60, pFile) != NULL) {
int count = 1;
char* token = strtok(line, " ");
char branch_outcome;
while (token != NULL) {
if (count++ == 1)
address = strtol(token, NULL, 16);
else
branch_outcome = token[0];
token = strtok(NULL, " ");
}
bp->Branches();
bool btb_prediction = false;
if (btb != NULL) {
btb->accessblock(address, branch_outcome);
btb_prediction = btb->getBTBPredictions();
}
if (!btb_prediction) {
bp->check_bp(address, branch_outcome);
bp->update_bp(address, branch_outcome);
bp->update_ghr(branch_outcome);
}
}
bp->print_stats();
}