tournament.cc (11427:fb512311295e) tournament.cc (11429:cf5af0cc3be4)
1/*
2 * Copyright (c) 2011, 2014 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software

--- 38 unchanged lines hidden (view full) ---

47TournamentBP::TournamentBP(const TournamentBPParams *params)
48 : BPredUnit(params),
49 localPredictorSize(params->localPredictorSize),
50 localCtrBits(params->localCtrBits),
51 localHistoryTableSize(params->localHistoryTableSize),
52 localHistoryBits(ceilLog2(params->localPredictorSize)),
53 globalPredictorSize(params->globalPredictorSize),
54 globalCtrBits(params->globalCtrBits),
1/*
2 * Copyright (c) 2011, 2014 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software

--- 38 unchanged lines hidden (view full) ---

47TournamentBP::TournamentBP(const TournamentBPParams *params)
48 : BPredUnit(params),
49 localPredictorSize(params->localPredictorSize),
50 localCtrBits(params->localCtrBits),
51 localHistoryTableSize(params->localHistoryTableSize),
52 localHistoryBits(ceilLog2(params->localPredictorSize)),
53 globalPredictorSize(params->globalPredictorSize),
54 globalCtrBits(params->globalCtrBits),
55 globalHistory(params->numThreads, 0),
56 globalHistoryBits(
57 ceilLog2(params->globalPredictorSize) >
58 ceilLog2(params->choicePredictorSize) ?
59 ceilLog2(params->globalPredictorSize) :
60 ceilLog2(params->choicePredictorSize)),
61 choicePredictorSize(params->choicePredictorSize),
62 choiceCtrBits(params->choiceCtrBits)
63{

--- 24 unchanged lines hidden (view full) ---

88 localHistoryTable[i] = 0;
89
90 //Setup the array of counters for the global predictor
91 globalCtrs.resize(globalPredictorSize);
92
93 for (int i = 0; i < globalPredictorSize; ++i)
94 globalCtrs[i].setBits(globalCtrBits);
95
55 globalHistoryBits(
56 ceilLog2(params->globalPredictorSize) >
57 ceilLog2(params->choicePredictorSize) ?
58 ceilLog2(params->globalPredictorSize) :
59 ceilLog2(params->choicePredictorSize)),
60 choicePredictorSize(params->choicePredictorSize),
61 choiceCtrBits(params->choiceCtrBits)
62{

--- 24 unchanged lines hidden (view full) ---

87 localHistoryTable[i] = 0;
88
89 //Setup the array of counters for the global predictor
90 globalCtrs.resize(globalPredictorSize);
91
92 for (int i = 0; i < globalPredictorSize; ++i)
93 globalCtrs[i].setBits(globalCtrBits);
94
95 //Clear the global history
96 globalHistory = 0;
96 // Set up the global history mask
97 // this is equivalent to mask(log2(globalPredictorSize)
98 globalHistoryMask = globalPredictorSize - 1;
99
100 if (!isPowerOf2(choicePredictorSize)) {
101 fatal("Invalid choice predictor size!\n");
102 }
103

--- 35 unchanged lines hidden (view full) ---

139TournamentBP::calcLocHistIdx(Addr &branch_addr)
140{
141 // Get low order bits after removing instruction offset.
142 return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1);
143}
144
145inline
146void
97 // Set up the global history mask
98 // this is equivalent to mask(log2(globalPredictorSize)
99 globalHistoryMask = globalPredictorSize - 1;
100
101 if (!isPowerOf2(choicePredictorSize)) {
102 fatal("Invalid choice predictor size!\n");
103 }
104

--- 35 unchanged lines hidden (view full) ---

140TournamentBP::calcLocHistIdx(Addr &branch_addr)
141{
142 // Get low order bits after removing instruction offset.
143 return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1);
144}
145
146inline
147void
147TournamentBP::updateGlobalHistTaken(ThreadID tid)
148TournamentBP::updateGlobalHistTaken()
148{
149{
149 globalHistory[tid] = (globalHistory[tid] << 1) | 1;
150 globalHistory[tid] = globalHistory[tid] & historyRegisterMask;
150 globalHistory = (globalHistory << 1) | 1;
151 globalHistory = globalHistory & historyRegisterMask;
151}
152
153inline
154void
152}
153
154inline
155void
155TournamentBP::updateGlobalHistNotTaken(ThreadID tid)
156TournamentBP::updateGlobalHistNotTaken()
156{
157{
157 globalHistory[tid] = (globalHistory[tid] << 1);
158 globalHistory[tid] = globalHistory[tid] & historyRegisterMask;
158 globalHistory = (globalHistory << 1);
159 globalHistory = globalHistory & historyRegisterMask;
159}
160
161inline
162void
163TournamentBP::updateLocalHistTaken(unsigned local_history_idx)
164{
165 localHistoryTable[local_history_idx] =
166 (localHistoryTable[local_history_idx] << 1) | 1;

--- 4 unchanged lines hidden (view full) ---

171TournamentBP::updateLocalHistNotTaken(unsigned local_history_idx)
172{
173 localHistoryTable[local_history_idx] =
174 (localHistoryTable[local_history_idx] << 1);
175}
176
177
178void
160}
161
162inline
163void
164TournamentBP::updateLocalHistTaken(unsigned local_history_idx)
165{
166 localHistoryTable[local_history_idx] =
167 (localHistoryTable[local_history_idx] << 1) | 1;

--- 4 unchanged lines hidden (view full) ---

172TournamentBP::updateLocalHistNotTaken(unsigned local_history_idx)
173{
174 localHistoryTable[local_history_idx] =
175 (localHistoryTable[local_history_idx] << 1);
176}
177
178
179void
179TournamentBP::btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history)
180TournamentBP::btbUpdate(Addr branch_addr, void * &bp_history)
180{
181 unsigned local_history_idx = calcLocHistIdx(branch_addr);
182 //Update Global History to Not Taken (clear LSB)
181{
182 unsigned local_history_idx = calcLocHistIdx(branch_addr);
183 //Update Global History to Not Taken (clear LSB)
183 globalHistory[tid] &= (historyRegisterMask & ~ULL(1));
184 globalHistory &= (historyRegisterMask & ~ULL(1));
184 //Update Local History to Not Taken
185 localHistoryTable[local_history_idx] =
186 localHistoryTable[local_history_idx] & (localPredictorMask & ~ULL(1));
187}
188
189bool
185 //Update Local History to Not Taken
186 localHistoryTable[local_history_idx] =
187 localHistoryTable[local_history_idx] & (localPredictorMask & ~ULL(1));
188}
189
190bool
190TournamentBP::lookup(ThreadID tid, Addr branch_addr, void * &bp_history)
191TournamentBP::lookup(Addr branch_addr, void * &bp_history)
191{
192 bool local_prediction;
193 unsigned local_history_idx;
194 unsigned local_predictor_idx;
195
196 bool global_prediction;
197 bool choice_prediction;
198
199 //Lookup in the local predictor to get its branch prediction
200 local_history_idx = calcLocHistIdx(branch_addr);
201 local_predictor_idx = localHistoryTable[local_history_idx]
202 & localPredictorMask;
203 local_prediction = localCtrs[local_predictor_idx].read() > localThreshold;
204
205 //Lookup in the global predictor to get its branch prediction
192{
193 bool local_prediction;
194 unsigned local_history_idx;
195 unsigned local_predictor_idx;
196
197 bool global_prediction;
198 bool choice_prediction;
199
200 //Lookup in the local predictor to get its branch prediction
201 local_history_idx = calcLocHistIdx(branch_addr);
202 local_predictor_idx = localHistoryTable[local_history_idx]
203 & localPredictorMask;
204 local_prediction = localCtrs[local_predictor_idx].read() > localThreshold;
205
206 //Lookup in the global predictor to get its branch prediction
206 global_prediction = globalThreshold <
207 globalCtrs[globalHistory[tid] & globalHistoryMask].read();
207 global_prediction =
208 globalCtrs[globalHistory & globalHistoryMask].read() > globalThreshold;
208
209 //Lookup in the choice predictor to see which one to use
209
210 //Lookup in the choice predictor to see which one to use
210 choice_prediction = choiceThreshold <
211 choiceCtrs[globalHistory[tid] & choiceHistoryMask].read();
211 choice_prediction =
212 choiceCtrs[globalHistory & choiceHistoryMask].read() > choiceThreshold;
212
213 // Create BPHistory and pass it back to be recorded.
214 BPHistory *history = new BPHistory;
213
214 // Create BPHistory and pass it back to be recorded.
215 BPHistory *history = new BPHistory;
215 history->globalHistory = globalHistory[tid];
216 history->globalHistory = globalHistory;
216 history->localPredTaken = local_prediction;
217 history->globalPredTaken = global_prediction;
218 history->globalUsed = choice_prediction;
219 history->localHistoryIdx = local_history_idx;
220 history->localHistory = local_predictor_idx;
221 bp_history = (void *)history;
222
223 assert(local_history_idx < localHistoryTableSize);
224
225 // Commented code is for doing speculative update of counters and
226 // all histories.
227 if (choice_prediction) {
228 if (global_prediction) {
217 history->localPredTaken = local_prediction;
218 history->globalPredTaken = global_prediction;
219 history->globalUsed = choice_prediction;
220 history->localHistoryIdx = local_history_idx;
221 history->localHistory = local_predictor_idx;
222 bp_history = (void *)history;
223
224 assert(local_history_idx < localHistoryTableSize);
225
226 // Commented code is for doing speculative update of counters and
227 // all histories.
228 if (choice_prediction) {
229 if (global_prediction) {
229 updateGlobalHistTaken(tid);
230 updateGlobalHistTaken();
230 updateLocalHistTaken(local_history_idx);
231 return true;
232 } else {
231 updateLocalHistTaken(local_history_idx);
232 return true;
233 } else {
233 updateGlobalHistNotTaken(tid);
234 updateGlobalHistNotTaken();
234 updateLocalHistNotTaken(local_history_idx);
235 return false;
236 }
237 } else {
238 if (local_prediction) {
235 updateLocalHistNotTaken(local_history_idx);
236 return false;
237 }
238 } else {
239 if (local_prediction) {
239 updateGlobalHistTaken(tid);
240 updateGlobalHistTaken();
240 updateLocalHistTaken(local_history_idx);
241 return true;
242 } else {
241 updateLocalHistTaken(local_history_idx);
242 return true;
243 } else {
243 updateGlobalHistNotTaken(tid);
244 updateGlobalHistNotTaken();
244 updateLocalHistNotTaken(local_history_idx);
245 return false;
246 }
247 }
248}
249
250void
245 updateLocalHistNotTaken(local_history_idx);
246 return false;
247 }
248 }
249}
250
251void
251TournamentBP::uncondBranch(ThreadID tid, Addr pc, void * &bp_history)
252TournamentBP::uncondBranch(Addr pc, void * &bp_history)
252{
253 // Create BPHistory and pass it back to be recorded.
254 BPHistory *history = new BPHistory;
253{
254 // Create BPHistory and pass it back to be recorded.
255 BPHistory *history = new BPHistory;
255 history->globalHistory = globalHistory[tid];
256 history->globalHistory = globalHistory;
256 history->localPredTaken = true;
257 history->globalPredTaken = true;
258 history->globalUsed = true;
259 history->localHistoryIdx = invalidPredictorIndex;
260 history->localHistory = invalidPredictorIndex;
261 bp_history = static_cast<void *>(history);
262
257 history->localPredTaken = true;
258 history->globalPredTaken = true;
259 history->globalUsed = true;
260 history->localHistoryIdx = invalidPredictorIndex;
261 history->localHistory = invalidPredictorIndex;
262 bp_history = static_cast<void *>(history);
263
263 updateGlobalHistTaken(tid);
264 updateGlobalHistTaken();
264}
265
266void
265}
266
267void
267TournamentBP::update(ThreadID tid, Addr branch_addr, bool taken,
268 void *bp_history, bool squashed)
268TournamentBP::update(Addr branch_addr, bool taken, void *bp_history,
269 bool squashed)
269{
270 unsigned local_history_idx;
271 unsigned local_predictor_idx M5_VAR_USED;
272 unsigned local_predictor_hist;
273
274 // Get the local predictor's current prediction
275 local_history_idx = calcLocHistIdx(branch_addr);
276 local_predictor_hist = localHistoryTable[local_history_idx];

--- 49 unchanged lines hidden (view full) ---

326 globalCtrs[global_predictor_idx].decrement();
327 if (old_local_pred_valid) {
328 localCtrs[old_local_pred_index].decrement();
329 }
330 }
331 }
332 if (squashed) {
333 if (taken) {
270{
271 unsigned local_history_idx;
272 unsigned local_predictor_idx M5_VAR_USED;
273 unsigned local_predictor_hist;
274
275 // Get the local predictor's current prediction
276 local_history_idx = calcLocHistIdx(branch_addr);
277 local_predictor_hist = localHistoryTable[local_history_idx];

--- 49 unchanged lines hidden (view full) ---

327 globalCtrs[global_predictor_idx].decrement();
328 if (old_local_pred_valid) {
329 localCtrs[old_local_pred_index].decrement();
330 }
331 }
332 }
333 if (squashed) {
334 if (taken) {
334 globalHistory[tid] = (history->globalHistory << 1) | 1;
335 globalHistory[tid] = globalHistory[tid] & historyRegisterMask;
335 globalHistory = (history->globalHistory << 1) | 1;
336 globalHistory = globalHistory & historyRegisterMask;
336 if (old_local_pred_valid) {
337 localHistoryTable[local_history_idx] =
338 (history->localHistory << 1) | 1;
339 }
340 } else {
337 if (old_local_pred_valid) {
338 localHistoryTable[local_history_idx] =
339 (history->localHistory << 1) | 1;
340 }
341 } else {
341 globalHistory[tid] = (history->globalHistory << 1);
342 globalHistory[tid] = globalHistory[tid] & historyRegisterMask;
342 globalHistory = (history->globalHistory << 1);
343 globalHistory = globalHistory & historyRegisterMask;
343 if (old_local_pred_valid) {
344 localHistoryTable[local_history_idx] =
345 history->localHistory << 1;
346 }
347 }
348
349 } else {
350 // We're done with this history, now delete it.
351 delete history;
352 }
353 }
354
355 assert(local_history_idx < localHistoryTableSize);
356
357
358}
359
360void
344 if (old_local_pred_valid) {
345 localHistoryTable[local_history_idx] =
346 history->localHistory << 1;
347 }
348 }
349
350 } else {
351 // We're done with this history, now delete it.
352 delete history;
353 }
354 }
355
356 assert(local_history_idx < localHistoryTableSize);
357
358
359}
360
361void
361TournamentBP::retireSquashed(ThreadID tid, void *bp_history)
362TournamentBP::retireSquashed(void *bp_history)
362{
363 BPHistory *history = static_cast<BPHistory *>(bp_history);
364 delete history;
365}
366
367void
363{
364 BPHistory *history = static_cast<BPHistory *>(bp_history);
365 delete history;
366}
367
368void
368TournamentBP::squash(ThreadID tid, void *bp_history)
369TournamentBP::squash(void *bp_history)
369{
370 BPHistory *history = static_cast<BPHistory *>(bp_history);
371
372 // Restore global history to state prior to this branch.
370{
371 BPHistory *history = static_cast<BPHistory *>(bp_history);
372
373 // Restore global history to state prior to this branch.
373 globalHistory[tid] = history->globalHistory;
374 globalHistory = history->globalHistory;
374
375 // Restore local history
376 if (history->localHistoryIdx != invalidPredictorIndex) {
377 localHistoryTable[history->localHistoryIdx] = history->localHistory;
378 }
379
380 // Delete this BPHistory now that we're done with it.
381 delete history;
382}
383
384TournamentBP*
385TournamentBPParams::create()
386{
387 return new TournamentBP(this);
388}
389
375
376 // Restore local history
377 if (history->localHistoryIdx != invalidPredictorIndex) {
378 localHistoryTable[history->localHistoryIdx] = history->localHistory;
379 }
380
381 // Delete this BPHistory now that we're done with it.
382 delete history;
383}
384
385TournamentBP*
386TournamentBPParams::create()
387{
388 return new TournamentBP(this);
389}
390
390unsigned
391TournamentBP::getGHR(ThreadID tid, void *bp_history) const
392{
393 return static_cast<BPHistory *>(bp_history)->globalHistory;
394}
395
396#ifdef DEBUG
397int
398TournamentBP::BPHistory::newCount = 0;
399#endif
391#ifdef DEBUG
392int
393TournamentBP::BPHistory::newCount = 0;
394#endif