forked from aengelke/raspsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
branchpred.cpp
434 lines (361 loc) · 12.2 KB
/
branchpred.cpp
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
//
// PTLsim: Cycle Accurate x86-64 Simulator
// Branch Prediction
//
// Copyright 2003-2008 Matt T. Yourst <[email protected]>
//
// This program is free software; it is licensed under the
// GNU General Public License, Version 2.
//
#include <branchpred.h>
#include <stats.h>
template <int SIZE>
struct BimodalPredictor {
array<byte, SIZE> table;
void reset() {
foreach (i, SIZE) table[i] = bit(i, 0) + 1;
}
inline int hash(W64 branchaddr) {
return lowbits((branchaddr >> 16) ^ branchaddr, log2(SIZE));
}
byte* predict(W64 branchaddr) {
return &table[hash(branchaddr)];
}
};
template <int L1SIZE, int L2SIZE, int SHIFTWIDTH, bool HISTORYXOR>
struct TwoLevelPredictor {
array<int, L1SIZE> shiftregs; // L1 history shift register(s)
array<byte, L2SIZE> L2table; // L2 prediction state table
void reset() {
// initialize counters to weakly this-or-that
foreach (i, L2SIZE) L2table[i] = bit(i, 0) + 1;
}
byte* predict(W64 branchaddr) {
int L1index = lowbits(branchaddr, log2(L1SIZE));
int L2index = shiftregs[L1index];
if (HISTORYXOR) {
L2index ^= branchaddr;
} else {
L2index |= branchaddr << SHIFTWIDTH;
}
L2index = lowbits(L2index, log2(L2SIZE));
return &L2table[L2index];
}
};
struct BTBEntry {
W64 target; // last destination of branch when taken
void reset() {
target = 0;
}
ostream& print(ostream& os, W64 tag) const {
os << (void*)(Waddr)target;
return os;
}
};
template <int SETCOUNT, int WAYCOUNT>
struct BranchTargetBuffer: public AssociativeArray<W64, BTBEntry, SETCOUNT, WAYCOUNT, 1> { };
template <int SIZE> struct ReturnAddressStack;
template <int SIZE>
ostream& operator <<(ostream& os, ReturnAddressStack<SIZE>& ras);
ostream& operator <<(ostream& os, const ReturnAddressStackEntry& e) {
os << " ", intstring(e.idx, 4), ": uuid ", intstring(e.uuid, 16), ", rip ", (void*)(Waddr)e.rip, endl;
return os;
}
// Enable to debug the return address stack (RAS) predictor mechanism
// #define DEBUG_RAS
template <int SIZE>
struct ReturnAddressStack: public Queue<ReturnAddressStackEntry, SIZE> {
typedef Queue<ReturnAddressStackEntry, SIZE> base_t;
void push(W64 uuid, W64 rip, ReturnAddressStackEntry& old) {
#ifdef DEBUG_RAS
if (logable(5)) logfile << "ReturnAddressStack::push(uuid ", uuid, ", rip ", (void*)(Waddr)rip, "):", endl;
#endif
if (base_t::full()) {
if (logable(5)) logfile << " Return address stack overflow: removing oldest entry to make space", endl;
stats.ooocore.branchpred.ras.overflows++;
base_t::pophead();
}
ReturnAddressStackEntry& e =* base_t::push();
assert(&e);
old = e;
#ifdef DEBUG_RAS
if (logable(5)) logfile << " Old entry: ", old, endl;
#endif
e.uuid = uuid;
e.rip = rip;
stats.ooocore.branchpred.ras.pushes++;
#ifdef DEBUG_RAS
if (logable(5)) { logfile << *this; }
#endif
}
ReturnAddressStackEntry& pop(ReturnAddressStackEntry& old) {
#ifdef DEBUG_RAS
if (logable(5)) logfile << "ReturnAddressStack::pop():", endl;
#endif
if (base_t::empty()) {
stats.ooocore.branchpred.ras.underflows++;
if (logable(5)) logfile << " Return address stack underflow: returning entry with zero fields", endl;
old.idx = -1;
old.uuid = 0;
old.rip = 0;
return old;
}
ReturnAddressStackEntry& e =* base_t::pop();
assert(&e);
old = e;
#ifdef DEBUG_RAS
if (logable(5)) { logfile << " Old entry: ", old, endl; logfile << *this; }
#endif
stats.ooocore.branchpred.ras.pops++;
return e;
}
W64 peek() {
#ifdef DEBUG_RAS
if (logable(5)) logfile << "ReturnAddressStack::peek():", endl;
#endif
if (base_t::empty()) {
if (logable(5)) logfile << " Return address stack is empty: returning bogus rip 0", endl;
return 0;
}
#ifdef DEBUG_RAS
if (logable(5)) { logfile << " Peeking entry ", (*base_t::peektail()); }
#endif
return base_t::peektail()->rip;
}
//
// Pop a speculative push from the stack
//
void annulpush(const ReturnAddressStackEntry& old) {
#ifdef DEBUG_RAS
if (logable(5)) logfile << "ReturnAddressStack::annulpush(old index ", old.idx, ", uuid ", old.uuid, ", rip ", (void*)(Waddr)old.rip, "):", endl;
#endif
if (base_t::empty()) {
#ifdef DEBUG_RAS
if (logable(5)) logfile << " Cannot annul: return address stack is empty", endl;
#endif
return;
}
ReturnAddressStackEntry& e =* base_t::peektail();
e.uuid = old.uuid;
e.rip = old.rip;
ReturnAddressStackEntry dummy;
pop(dummy);
#ifdef DEBUG_RAS
if (logable(5)) logfile << " Popped speculative push; e.index = ", e.index(), " vs tail ", base_t::tail, endl;
assert(e.index() == base_t::tail);
#endif
stats.ooocore.branchpred.ras.annuls++;
}
//
// Push the old data back on the stack
//
void annulpop(const ReturnAddressStackEntry& old) {
#ifdef DEBUG_RAS
if (logable(5)) logfile << "ReturnAddressStack::annulpop(old index ", old.idx, ", uuid ", old.uuid, ", rip ", (void*)(Waddr)old.rip, "):", endl;
#endif
if (base_t::full()) {
#ifdef DEBUG_RAS
if (logable(5)) logfile << " Cannot annul: stack is full", endl;
#endif
return;
}
ReturnAddressStackEntry dummy;
#ifdef DEBUG_RAS
if (logable(5)) logfile << " Pushed speculative pop; old.index = ", old.index(), " vs tail ", base_t::tail, endl;
assert(old.index() == base_t::tail);
#endif
push(old.uuid, old.rip, dummy);
stats.ooocore.branchpred.ras.annuls++;
}
};
template <int SIZE>
ostream& operator <<(ostream& os, ReturnAddressStack<SIZE>& ras) {
ras.print(os);
return os;
}
template <int METASIZE, int BIMODSIZE, int L1SIZE, int L2SIZE, int SHIFTWIDTH, bool HISTORYXOR, int BTBSETS, int BTBWAYS, int RASSIZE>
struct CombinedPredictor {
TwoLevelPredictor<L1SIZE, L2SIZE, SHIFTWIDTH, HISTORYXOR> twolevel;
BimodalPredictor<BIMODSIZE> bimodal;
BimodalPredictor<METASIZE> meta;
BranchTargetBuffer<BTBSETS, BTBWAYS> btb;
ReturnAddressStack<RASSIZE> ras;
void reset() {
twolevel.reset();
bimodal.reset();
meta.reset();
btb.reset();
ras.reset();
}
void updateras(PredictorUpdate& predinfo, W64 rip) {
if unlikely (predinfo.flags & BRANCH_HINT_RET) {
predinfo.ras_push = 0;
ras.pop(predinfo.ras_old);
} else if likely (predinfo.flags & BRANCH_HINT_CALL) {
predinfo.ras_push = 1;
ras.push(predinfo.uuid, rip, predinfo.ras_old);
}
}
//
// NOTE: branchaddr should point to first byte *after* branching insn,
// since x86 has variable length instructions.
//
W64 predict(PredictorUpdate& update, int type, W64 branchaddr, W64 target) {
update.cp1 = null;
update.cp2 = null;
update.cpmeta = null;
update.flags = type;
if unlikely ((type & (BRANCH_HINT_COND|BRANCH_HINT_INDIRECT)) == 0) {
// Unconditional: always return target
return target;
}
if likely (type & BRANCH_HINT_COND) {
byte& bimodalctr = *bimodal.predict(branchaddr);
byte& twolevelctr = *twolevel.predict(branchaddr);
byte& metactr = *meta.predict(branchaddr);
update.cpmeta = &metactr;
update.meta = (metactr >= 2);
update.bimodal = (bimodalctr >= 2);
update.twolevel = (twolevelctr >= 2);
if (metactr >= 2) {
update.cp1 = &twolevelctr;
update.cp2 = &bimodalctr;
} else {
update.cp1 = &bimodalctr;
update.cp2 = &twolevelctr;
}
}
//
// If this is a return, find next entry that would be popped
// Caller is responsible for using updateras() to update the
// RAS once annulable resources have been allocated for this
// return insn.
//
if unlikely (type & BRANCH_HINT_RET) {
#ifdef DEBUG_RAS
if (logable(5)) logfile << "Peeking RAS for uuid ", update.uuid, ":", endl;
#endif
return ras.peek();
}
BTBEntry* pbtb = btb.probe(branchaddr);
// if this is a jump, ignore predicted direction; we know it's taken.
if unlikely (!(type & BRANCH_HINT_COND)) {
return (pbtb ? pbtb->target : target);
}
// Predict conditional branch. If this is the first time, predict backward
// jumps as taken and forward jumps as not-taken.
if unlikely (!pbtb && config.static_branchpred) {
return target < branchaddr ? target : branchaddr;
}
return (*(update.cp1) >= 2) ? target : branchaddr;
}
void update(PredictorUpdate& update, W64 branchaddr, W64 target) {
int type = update.flags;
bool taken = (target != branchaddr);
//
// keep stats about JMPs; also, but don't change any pred state for JMPs
// which are returns.
//
if unlikely (type & BRANCH_HINT_INDIRECT) {
if unlikely (type & BRANCH_HINT_RET) return;
}
//
// L1 table is updated unconditionally for combining predictor too:
//
if likely (type & BRANCH_HINT_COND) {
int l1index = lowbits(branchaddr, log2(L1SIZE));
twolevel.shiftregs[l1index] = lowbits((twolevel.shiftregs[l1index] << 1) | taken, SHIFTWIDTH);
}
//
// Find BTB entry. Allocate always to detect first use of conditional branch
//
BTBEntry* pbtb = btb.select(branchaddr);
//
// Now p is a possibly null pointer into the direction prediction table,
// and pbtb is a possibly null pointer into the BTB (either to a
// matched-on entry or a victim which was LRU in its set)
//
//
// update state (but not for jumps)
//
if likely (update.cp1) {
byte& counter = *update.cp1;
counter = clipto(counter + (taken ? +1 : -1), 0, 3);
}
//
// combining predictor also updates second predictor and meta predictor
// second direction predictor
//
if likely (update.cp2) {
byte& counter = *update.cp2;
counter = clipto(counter + (taken ? +1 : -1), 0, 3);
}
//
// Update meta predictor
//
if likely (update.cpmeta) {
if (update.bimodal != update.twolevel) {
//
// We only update meta predictor if directions were different.
// We increment the counter if the twolevel predictor was correct;
// if the bimodal predictor was correct, we decrement it.
//
byte& counter = *update.cpmeta;
bool twolevel_or_bimodal = (update.twolevel == taken);
counter = clipto(counter + (twolevel_or_bimodal ? +1 : -1), 0, 3);
}
}
//
// update BTB (but only for taken branches)
//
if likely (pbtb) {
// Update either the entry selected above, or if not found, use the LRU entry:
pbtb->target = target;
}
}
//
// Speculative execution can corrupt the RAS, since entries will be pushed
// as call insns are fetched. If those call insns were along an incorrect
// branch path, they must be annulled.
//
void annulras(const PredictorUpdate& predinfo) {
#ifdef DEBUG_RAS
if (logable(5)) logfile << "Update RAS for uuid ", predinfo.uuid, ":", endl;
#endif
if (predinfo.ras_push)
ras.annulpush(predinfo.ras_old);
else ras.annulpop(predinfo.ras_old);
}
};
// template <int METASIZE, int BIMODSIZE, int L1SIZE, int L2SIZE, int SHIFTWIDTH, bool HISTORYXOR, int BTBSETS, int BTBWAYS, int RASSIZE>
// G-share constraints: METASIZE, BIMODSIZE, 1, L2SIZE, log2(L2SIZE), (HISTORYXOR = true), BTBSETS, BTBWAYS, RASSIZE
struct BranchPredictorImplementation: public CombinedPredictor<65536, 65536, 1, 65536, 16, 1, 1024, 4, 1024> { };
void BranchPredictorInterface::destroy() {
if (impl) delete impl;
impl = null;
}
void BranchPredictorInterface::reset() {
impl->reset();
}
void BranchPredictorInterface::init() {
destroy();
impl = new BranchPredictorImplementation();
reset();
}
W64 BranchPredictorInterface::predict(PredictorUpdate& update, int type, W64 branchaddr, W64 target) {
return impl->predict(update, type, branchaddr, target);
}
void BranchPredictorInterface::update(PredictorUpdate& update, W64 branchaddr, W64 target) {
impl->update(update, branchaddr, target);
}
void BranchPredictorInterface::updateras(PredictorUpdate& predinfo, W64 branchaddr) {
impl->updateras(predinfo, branchaddr);
};
void BranchPredictorInterface::annulras(const PredictorUpdate& predinfo) {
impl->annulras(predinfo);
};
void BranchPredictorInterface::flush() { }
ostream& operator <<(ostream& os, const BranchPredictorInterface& branchpred) {
os << branchpred.impl->ras;
return os;
}