gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tournament.cc
Go to the documentation of this file.
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
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2004-2006 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * Authors: Kevin Lim
41  */
42 
43 #include "cpu/pred/tournament.hh"
44 
45 #include "base/bitfield.hh"
46 #include "base/intmath.hh"
47 
48 TournamentBP::TournamentBP(const TournamentBPParams *params)
49  : BPredUnit(params),
50  localPredictorSize(params->localPredictorSize),
51  localCtrBits(params->localCtrBits),
52  localHistoryTableSize(params->localHistoryTableSize),
53  localHistoryBits(ceilLog2(params->localPredictorSize)),
54  globalPredictorSize(params->globalPredictorSize),
55  globalCtrBits(params->globalCtrBits),
56  globalHistory(params->numThreads, 0),
57  globalHistoryBits(
58  ceilLog2(params->globalPredictorSize) >
59  ceilLog2(params->choicePredictorSize) ?
60  ceilLog2(params->globalPredictorSize) :
61  ceilLog2(params->choicePredictorSize)),
62  choicePredictorSize(params->choicePredictorSize),
63  choiceCtrBits(params->choiceCtrBits)
64 {
66  fatal("Invalid local predictor size!\n");
67  }
68 
70  fatal("Invalid global predictor size!\n");
71  }
72 
73  //Set up the array of counters for the local predictor
75 
76  for (int i = 0; i < localPredictorSize; ++i)
77  localCtrs[i].setBits(localCtrBits);
78 
80 
82  fatal("Invalid local history table size!\n");
83  }
84 
85  //Setup the history table for the local table
87 
88  for (int i = 0; i < localHistoryTableSize; ++i)
89  localHistoryTable[i] = 0;
90 
91  //Setup the array of counters for the global predictor
93 
94  for (int i = 0; i < globalPredictorSize; ++i)
95  globalCtrs[i].setBits(globalCtrBits);
96 
97  // Set up the global history mask
98  // this is equivalent to mask(log2(globalPredictorSize)
99  globalHistoryMask = globalPredictorSize - 1;
100 
102  fatal("Invalid choice predictor size!\n");
103  }
104 
105  // Set up choiceHistoryMask
106  // this is equivalent to mask(log2(choicePredictorSize)
108 
109  //Setup the array of counters for the choice predictor
111 
112  for (int i = 0; i < choicePredictorSize; ++i)
113  choiceCtrs[i].setBits(choiceCtrBits);
114 
115  //Set up historyRegisterMask
117 
118  //Check that predictors don't use more bits than they have available
120  fatal("Global predictor too large for global history bits!\n");
121  }
123  fatal("Choice predictor too large for global history bits!\n");
124  }
125 
128  inform("More global history bits than required by predictors\n");
129  }
130 
131  // Set thresholds for the three predictors' counters
132  // This is equivalent to (2^(Ctr))/2 - 1
133  localThreshold = (ULL(1) << (localCtrBits - 1)) - 1;
134  globalThreshold = (ULL(1) << (globalCtrBits - 1)) - 1;
135  choiceThreshold = (ULL(1) << (choiceCtrBits - 1)) - 1;
136 }
137 
138 inline
139 unsigned
141 {
142  // Get low order bits after removing instruction offset.
143  return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1);
144 }
145 
146 inline
147 void
149 {
150  globalHistory[tid] = (globalHistory[tid] << 1) | 1;
152 }
153 
154 inline
155 void
157 {
158  globalHistory[tid] = (globalHistory[tid] << 1);
160 }
161 
162 inline
163 void
164 TournamentBP::updateLocalHistTaken(unsigned local_history_idx)
165 {
166  localHistoryTable[local_history_idx] =
167  (localHistoryTable[local_history_idx] << 1) | 1;
168 }
169 
170 inline
171 void
172 TournamentBP::updateLocalHistNotTaken(unsigned local_history_idx)
173 {
174  localHistoryTable[local_history_idx] =
175  (localHistoryTable[local_history_idx] << 1);
176 }
177 
178 
179 void
180 TournamentBP::btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history)
181 {
182  unsigned local_history_idx = calcLocHistIdx(branch_addr);
183  //Update Global History to Not Taken (clear LSB)
184  globalHistory[tid] &= (historyRegisterMask & ~ULL(1));
185  //Update Local History to Not Taken
186  localHistoryTable[local_history_idx] =
187  localHistoryTable[local_history_idx] & (localPredictorMask & ~ULL(1));
188 }
189 
190 bool
191 TournamentBP::lookup(ThreadID tid, Addr branch_addr, void * &bp_history)
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]
204  local_prediction = localCtrs[local_predictor_idx].read() > localThreshold;
205 
206  //Lookup in the global predictor to get its branch prediction
207  global_prediction = globalThreshold <
209 
210  //Lookup in the choice predictor to see which one to use
211  choice_prediction = choiceThreshold <
213 
214  // Create BPHistory and pass it back to be recorded.
215  BPHistory *history = new BPHistory;
216  history->globalHistory = globalHistory[tid];
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  // Speculative update of the global history and the
227  // selected local history.
228  if (choice_prediction) {
229  if (global_prediction) {
231  updateLocalHistTaken(local_history_idx);
232  return true;
233  } else {
235  updateLocalHistNotTaken(local_history_idx);
236  return false;
237  }
238  } else {
239  if (local_prediction) {
241  updateLocalHistTaken(local_history_idx);
242  return true;
243  } else {
245  updateLocalHistNotTaken(local_history_idx);
246  return false;
247  }
248  }
249 }
250 
251 void
252 TournamentBP::uncondBranch(ThreadID tid, Addr pc, void * &bp_history)
253 {
254  // Create BPHistory and pass it back to be recorded.
255  BPHistory *history = new BPHistory;
256  history->globalHistory = globalHistory[tid];
257  history->localPredTaken = true;
258  history->globalPredTaken = true;
259  history->globalUsed = true;
262  bp_history = static_cast<void *>(history);
263 
265 }
266 
267 void
268 TournamentBP::update(ThreadID tid, Addr branch_addr, bool taken,
269  void *bp_history, bool squashed)
270 {
271  assert(bp_history);
272 
273  BPHistory *history = static_cast<BPHistory *>(bp_history);
274 
275  unsigned local_history_idx = calcLocHistIdx(branch_addr);
276 
277  assert(local_history_idx < localHistoryTableSize);
278 
279  // Unconditional branches do not use local history.
280  bool old_local_pred_valid = history->localHistory !=
282 
283  // If this is a misprediction, restore the speculatively
284  // updated state (global history register and local history)
285  // and update again.
286  if (squashed) {
287  // Global history restore and update
288  globalHistory[tid] = (history->globalHistory << 1) | taken;
290 
291  // Local history restore and update.
292  if (old_local_pred_valid) {
293  localHistoryTable[local_history_idx] =
294  (history->localHistory << 1) | taken;
295  }
296 
297  return;
298  }
299 
300  unsigned old_local_pred_index = history->localHistory &
302 
303  assert(old_local_pred_index < localPredictorSize);
304 
305  // Update the choice predictor to tell it which one was correct if
306  // there was a prediction.
307  if (history->localPredTaken != history->globalPredTaken &&
308  old_local_pred_valid)
309  {
310  // If the local prediction matches the actual outcome,
311  // decrement the counter. Otherwise increment the
312  // counter.
313  unsigned choice_predictor_idx =
314  history->globalHistory & choiceHistoryMask;
315  if (history->localPredTaken == taken) {
316  choiceCtrs[choice_predictor_idx].decrement();
317  } else if (history->globalPredTaken == taken) {
318  choiceCtrs[choice_predictor_idx].increment();
319  }
320  }
321 
322  // Update the counters with the proper
323  // resolution of the branch. Histories are updated
324  // speculatively, restored upon squash() calls, and
325  // recomputed upon update(squash = true) calls,
326  // so they do not need to be updated.
327  unsigned global_predictor_idx =
328  history->globalHistory & globalHistoryMask;
329  if (taken) {
330  globalCtrs[global_predictor_idx].increment();
331  if (old_local_pred_valid) {
332  localCtrs[old_local_pred_index].increment();
333  }
334  } else {
335  globalCtrs[global_predictor_idx].decrement();
336  if (old_local_pred_valid) {
337  localCtrs[old_local_pred_index].decrement();
338  }
339  }
340 
341  // We're done with this history, now delete it.
342  delete history;
343 }
344 
345 void
346 TournamentBP::squash(ThreadID tid, void *bp_history)
347 {
348  BPHistory *history = static_cast<BPHistory *>(bp_history);
349 
350  // Restore global history to state prior to this branch.
351  globalHistory[tid] = history->globalHistory;
352 
353  // Restore local history
354  if (history->localHistoryIdx != invalidPredictorIndex) {
355  localHistoryTable[history->localHistoryIdx] = history->localHistory;
356  }
357 
358  // Delete this BPHistory now that we're done with it.
359  delete history;
360 }
361 
363 TournamentBPParams::create()
364 {
365  return new TournamentBP(this);
366 }
367 
368 unsigned
369 TournamentBP::getGHR(ThreadID tid, void *bp_history) const
370 {
371  return static_cast<BPHistory *>(bp_history)->globalHistory;
372 }
373 
374 #ifdef DEBUG
375 int
376 TournamentBP::BPHistory::newCount = 0;
377 #endif
std::vector< SatCounter > localCtrs
Local counters.
Definition: tournament.hh:177
void uncondBranch(ThreadID tid, Addr pc, void *&bp_history)
Records that there was an unconditional branch, and modifies the bp history to point to an object tha...
Definition: tournament.cc:252
Implements a tournament branch predictor, hopefully identical to the one used in the 21264...
Definition: tournament.hh:63
Bitfield< 7 > i
Definition: miscregs.hh:1378
void squash(ThreadID tid, void *bp_history)
Restores the global branch history on a squash.
Definition: tournament.cc:346
unsigned globalPredictorSize
Number of entries in the global predictor.
Definition: tournament.hh:201
unsigned localCtrBits
Number of bits of the local predictor's counters.
Definition: tournament.hh:186
int ceilLog2(const T &n)
Definition: intmath.hh:174
void updateGlobalHistTaken(ThreadID tid)
Updates global history as taken.
Definition: tournament.cc:148
unsigned choicePredictorSize
Number of entries in the choice predictor.
Definition: tournament.hh:231
unsigned calcLocHistIdx(Addr &branch_addr)
Returns the local history index, given a branch address.
Definition: tournament.cc:140
unsigned localThreshold
Thresholds for the counter value; above the threshold is taken, equal to or below the threshold is no...
Definition: tournament.hh:239
void updateGlobalHistNotTaken(ThreadID tid)
Updates global history as not taken.
Definition: tournament.cc:156
unsigned globalHistoryMask
Mask to apply to globalHistory to access global history table.
Definition: tournament.hh:217
unsigned globalCtrBits
Number of bits of the global predictor's counters.
Definition: tournament.hh:204
unsigned globalHistoryBits
Number of bits for the global history.
Definition: tournament.hh:213
unsigned localHistoryTableSize
Number of entries in the local history table.
Definition: tournament.hh:192
unsigned choiceThreshold
Definition: tournament.hh:241
#define fatal(...)
Definition: misc.hh:163
unsigned localPredictorSize
Number of counters in the local predictor.
Definition: tournament.hh:180
bool isPowerOf2(const T &n)
Definition: intmath.hh:73
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
#define ULL(N)
uint64_t constant
Definition: types.hh:50
void btbUpdate(ThreadID tid, Addr branch_addr, void *&bp_history)
Updates the branch predictor to Not Taken if a BTB entry is invalid or not found. ...
Definition: tournament.cc:180
unsigned globalThreshold
Definition: tournament.hh:240
unsigned historyRegisterMask
Mask to control how much history is stored.
Definition: tournament.hh:225
Basically a wrapper class to hold both the branch predictor and the BTB.
Definition: bpred_unit.hh:67
void updateLocalHistNotTaken(unsigned local_history_idx)
Updates local histories as not taken.
Definition: tournament.cc:172
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:171
unsigned getGHR(ThreadID tid, void *bp_history) const
Definition: tournament.cc:369
std::vector< unsigned > localHistoryTable
Array of local history table entries.
Definition: tournament.hh:189
unsigned localPredictorMask
Mask to truncate values stored in the local history table.
Definition: tournament.hh:183
std::vector< SatCounter > choiceCtrs
Array of counters that make up the choice predictor.
Definition: tournament.hh:228
std::vector< unsigned > globalHistory
Global history register.
Definition: tournament.hh:209
The branch history information that is created upon predicting a branch.
Definition: tournament.hh:157
void update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history, bool squashed)
Updates the branch predictor with the actual result of a branch.
Definition: tournament.cc:268
bool lookup(ThreadID tid, Addr branch_addr, void *&bp_history)
Looks up the given address in the branch predictor and returns a true/false value as to whether it is...
Definition: tournament.cc:191
const unsigned instShiftAmt
Number of bits to shift instructions by for predictor addresses.
Definition: bpred_unit.hh:308
static const int invalidPredictorIndex
Flag for invalid predictor index.
Definition: tournament.hh:175
IntReg pc
Definition: remote_gdb.hh:91
Bitfield< 3, 0 > mask
Definition: types.hh:64
TournamentBP(const TournamentBPParams *params)
Default branch predictor constructor.
Definition: tournament.cc:48
std::vector< SatCounter > globalCtrs
Array of counters that make up the global predictor.
Definition: tournament.hh:198
#define inform(...)
Definition: misc.hh:221
unsigned choiceCtrBits
Number of bits in the choice predictor's counters.
Definition: tournament.hh:234
unsigned choiceHistoryMask
Mask to apply to globalHistory to access choice history table.
Definition: tournament.hh:221
unsigned localHistoryBits
Number of bits for each entry of the local history table.
Definition: tournament.hh:195
void updateLocalHistTaken(unsigned local_history_idx)
Updates local histories as taken.
Definition: tournament.cc:164

Generated on Fri Jun 9 2017 13:03:44 for gem5 by doxygen 1.8.6