gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  localCtrs(localPredictorSize, SatCounter(localCtrBits)),
53  localHistoryTableSize(params->localHistoryTableSize),
54  localHistoryBits(ceilLog2(params->localPredictorSize)),
55  globalPredictorSize(params->globalPredictorSize),
56  globalCtrBits(params->globalCtrBits),
57  globalCtrs(globalPredictorSize, SatCounter(globalCtrBits)),
58  globalHistory(params->numThreads, 0),
59  globalHistoryBits(
60  ceilLog2(params->globalPredictorSize) >
61  ceilLog2(params->choicePredictorSize) ?
62  ceilLog2(params->globalPredictorSize) :
63  ceilLog2(params->choicePredictorSize)),
64  choicePredictorSize(params->choicePredictorSize),
65  choiceCtrBits(params->choiceCtrBits),
66  choiceCtrs(choicePredictorSize, SatCounter(choiceCtrBits))
67 {
69  fatal("Invalid local predictor size!\n");
70  }
71 
73  fatal("Invalid global predictor size!\n");
74  }
75 
77 
79  fatal("Invalid local history table size!\n");
80  }
81 
82  //Setup the history table for the local table
84 
85  for (int i = 0; i < localHistoryTableSize; ++i)
86  localHistoryTable[i] = 0;
87 
88  // Set up the global history mask
89  // this is equivalent to mask(log2(globalPredictorSize)
91 
93  fatal("Invalid choice predictor size!\n");
94  }
95 
96  // Set up choiceHistoryMask
97  // this is equivalent to mask(log2(choicePredictorSize)
99 
100  //Set up historyRegisterMask
102 
103  //Check that predictors don't use more bits than they have available
105  fatal("Global predictor too large for global history bits!\n");
106  }
108  fatal("Choice predictor too large for global history bits!\n");
109  }
110 
113  inform("More global history bits than required by predictors\n");
114  }
115 
116  // Set thresholds for the three predictors' counters
117  // This is equivalent to (2^(Ctr))/2 - 1
118  localThreshold = (ULL(1) << (localCtrBits - 1)) - 1;
119  globalThreshold = (ULL(1) << (globalCtrBits - 1)) - 1;
120  choiceThreshold = (ULL(1) << (choiceCtrBits - 1)) - 1;
121 }
122 
123 inline
124 unsigned
126 {
127  // Get low order bits after removing instruction offset.
128  return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1);
129 }
130 
131 inline
132 void
134 {
135  globalHistory[tid] = (globalHistory[tid] << 1) | 1;
137 }
138 
139 inline
140 void
142 {
143  globalHistory[tid] = (globalHistory[tid] << 1);
145 }
146 
147 inline
148 void
149 TournamentBP::updateLocalHistTaken(unsigned local_history_idx)
150 {
151  localHistoryTable[local_history_idx] =
152  (localHistoryTable[local_history_idx] << 1) | 1;
153 }
154 
155 inline
156 void
157 TournamentBP::updateLocalHistNotTaken(unsigned local_history_idx)
158 {
159  localHistoryTable[local_history_idx] =
160  (localHistoryTable[local_history_idx] << 1);
161 }
162 
163 
164 void
165 TournamentBP::btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history)
166 {
167  unsigned local_history_idx = calcLocHistIdx(branch_addr);
168  //Update Global History to Not Taken (clear LSB)
169  globalHistory[tid] &= (historyRegisterMask & ~ULL(1));
170  //Update Local History to Not Taken
171  localHistoryTable[local_history_idx] =
172  localHistoryTable[local_history_idx] & (localPredictorMask & ~ULL(1));
173 }
174 
175 bool
176 TournamentBP::lookup(ThreadID tid, Addr branch_addr, void * &bp_history)
177 {
178  bool local_prediction;
179  unsigned local_history_idx;
180  unsigned local_predictor_idx;
181 
182  bool global_prediction;
183  bool choice_prediction;
184 
185  //Lookup in the local predictor to get its branch prediction
186  local_history_idx = calcLocHistIdx(branch_addr);
187  local_predictor_idx = localHistoryTable[local_history_idx]
189  local_prediction = localCtrs[local_predictor_idx] > localThreshold;
190 
191  //Lookup in the global predictor to get its branch prediction
192  global_prediction = globalThreshold <
194 
195  //Lookup in the choice predictor to see which one to use
196  choice_prediction = choiceThreshold <
197  choiceCtrs[globalHistory[tid] & choiceHistoryMask];
198 
199  // Create BPHistory and pass it back to be recorded.
200  BPHistory *history = new BPHistory;
201  history->globalHistory = globalHistory[tid];
202  history->localPredTaken = local_prediction;
203  history->globalPredTaken = global_prediction;
204  history->globalUsed = choice_prediction;
205  history->localHistoryIdx = local_history_idx;
206  history->localHistory = local_predictor_idx;
207  bp_history = (void *)history;
208 
209  assert(local_history_idx < localHistoryTableSize);
210 
211  // Speculative update of the global history and the
212  // selected local history.
213  if (choice_prediction) {
214  if (global_prediction) {
216  updateLocalHistTaken(local_history_idx);
217  return true;
218  } else {
220  updateLocalHistNotTaken(local_history_idx);
221  return false;
222  }
223  } else {
224  if (local_prediction) {
226  updateLocalHistTaken(local_history_idx);
227  return true;
228  } else {
230  updateLocalHistNotTaken(local_history_idx);
231  return false;
232  }
233  }
234 }
235 
236 void
237 TournamentBP::uncondBranch(ThreadID tid, Addr pc, void * &bp_history)
238 {
239  // Create BPHistory and pass it back to be recorded.
240  BPHistory *history = new BPHistory;
241  history->globalHistory = globalHistory[tid];
242  history->localPredTaken = true;
243  history->globalPredTaken = true;
244  history->globalUsed = true;
247  bp_history = static_cast<void *>(history);
248 
250 }
251 
252 void
253 TournamentBP::update(ThreadID tid, Addr branch_addr, bool taken,
254  void *bp_history, bool squashed,
255  const StaticInstPtr & inst, Addr corrTarget)
256 {
257  assert(bp_history);
258 
259  BPHistory *history = static_cast<BPHistory *>(bp_history);
260 
261  unsigned local_history_idx = calcLocHistIdx(branch_addr);
262 
263  assert(local_history_idx < localHistoryTableSize);
264 
265  // Unconditional branches do not use local history.
266  bool old_local_pred_valid = history->localHistory !=
268 
269  // If this is a misprediction, restore the speculatively
270  // updated state (global history register and local history)
271  // and update again.
272  if (squashed) {
273  // Global history restore and update
274  globalHistory[tid] = (history->globalHistory << 1) | taken;
276 
277  // Local history restore and update.
278  if (old_local_pred_valid) {
279  localHistoryTable[local_history_idx] =
280  (history->localHistory << 1) | taken;
281  }
282 
283  return;
284  }
285 
286  unsigned old_local_pred_index = history->localHistory &
288 
289  assert(old_local_pred_index < localPredictorSize);
290 
291  // Update the choice predictor to tell it which one was correct if
292  // there was a prediction.
293  if (history->localPredTaken != history->globalPredTaken &&
294  old_local_pred_valid)
295  {
296  // If the local prediction matches the actual outcome,
297  // decrement the counter. Otherwise increment the
298  // counter.
299  unsigned choice_predictor_idx =
300  history->globalHistory & choiceHistoryMask;
301  if (history->localPredTaken == taken) {
302  choiceCtrs[choice_predictor_idx]--;
303  } else if (history->globalPredTaken == taken) {
304  choiceCtrs[choice_predictor_idx]++;
305  }
306  }
307 
308  // Update the counters with the proper
309  // resolution of the branch. Histories are updated
310  // speculatively, restored upon squash() calls, and
311  // recomputed upon update(squash = true) calls,
312  // so they do not need to be updated.
313  unsigned global_predictor_idx =
314  history->globalHistory & globalHistoryMask;
315  if (taken) {
316  globalCtrs[global_predictor_idx]++;
317  if (old_local_pred_valid) {
318  localCtrs[old_local_pred_index]++;
319  }
320  } else {
321  globalCtrs[global_predictor_idx]--;
322  if (old_local_pred_valid) {
323  localCtrs[old_local_pred_index]--;
324  }
325  }
326 
327  // We're done with this history, now delete it.
328  delete history;
329 }
330 
331 void
332 TournamentBP::squash(ThreadID tid, void *bp_history)
333 {
334  BPHistory *history = static_cast<BPHistory *>(bp_history);
335 
336  // Restore global history to state prior to this branch.
337  globalHistory[tid] = history->globalHistory;
338 
339  // Restore local history
340  if (history->localHistoryIdx != invalidPredictorIndex) {
341  localHistoryTable[history->localHistoryIdx] = history->localHistory;
342  }
343 
344  // Delete this BPHistory now that we're done with it.
345  delete history;
346 }
347 
349 TournamentBPParams::create()
350 {
351  return new TournamentBP(this);
352 }
353 
354 #ifdef DEBUG
355 int
356 TournamentBP::BPHistory::newCount = 0;
357 #endif
std::vector< SatCounter > localCtrs
Local counters.
Definition: tournament.hh:187
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:237
Implements a tournament branch predictor, hopefully identical to the one used in the 21264...
Definition: tournament.hh:63
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:175
Bitfield< 7 > i
void squash(ThreadID tid, void *bp_history)
Restores the global branch history on a squash.
Definition: tournament.cc:332
unsigned globalPredictorSize
Number of entries in the global predictor.
Definition: tournament.hh:199
unsigned localCtrBits
Number of bits of the local predictor&#39;s counters.
Definition: tournament.hh:184
int ceilLog2(const T &n)
Definition: intmath.hh:135
void updateGlobalHistTaken(ThreadID tid)
Updates global history as taken.
Definition: tournament.cc:133
unsigned choicePredictorSize
Number of entries in the choice predictor.
Definition: tournament.hh:229
unsigned calcLocHistIdx(Addr &branch_addr)
Returns the local history index, given a branch address.
Definition: tournament.cc:125
unsigned localThreshold
Thresholds for the counter value; above the threshold is taken, equal to or below the threshold is no...
Definition: tournament.hh:240
void updateGlobalHistNotTaken(ThreadID tid)
Updates global history as not taken.
Definition: tournament.cc:141
unsigned globalHistoryMask
Mask to apply to globalHistory to access global history table.
Definition: tournament.hh:218
#define inform(...)
Definition: logging.hh:213
Bitfield< 4 > pc
unsigned globalCtrBits
Number of bits of the global predictor&#39;s counters.
Definition: tournament.hh:202
unsigned globalHistoryBits
Number of bits for the global history.
Definition: tournament.hh:214
void update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history, bool squashed, const StaticInstPtr &inst, Addr corrTarget)
Updates the branch predictor with the actual result of a branch.
Definition: tournament.cc:253
unsigned localHistoryTableSize
Number of entries in the local history table.
Definition: tournament.hh:193
Implements an n bit saturating counter and provides methods to increment, decrement, and read it.
Definition: sat_counter.hh:57
unsigned choiceThreshold
Definition: tournament.hh:242
unsigned localPredictorSize
Number of counters in the local predictor.
Definition: tournament.hh:178
bool isPowerOf2(const T &n)
Definition: intmath.hh:146
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:165
unsigned globalThreshold
Definition: tournament.hh:241
unsigned historyRegisterMask
Mask to control how much history is stored.
Definition: tournament.hh:226
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:157
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:227
std::vector< unsigned > localHistoryTable
Array of local history table entries.
Definition: tournament.hh:190
unsigned localPredictorMask
Mask to truncate values stored in the local history table.
Definition: tournament.hh:181
std::vector< SatCounter > choiceCtrs
Array of counters that make up the choice predictor.
Definition: tournament.hh:235
std::vector< unsigned > globalHistory
Global history register.
Definition: tournament.hh:210
The branch history information that is created upon predicting a branch.
Definition: tournament.hh:158
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:176
const unsigned instShiftAmt
Number of bits to shift instructions by for predictor addresses.
Definition: bpred_unit.hh:321
static const int invalidPredictorIndex
Flag for invalid predictor index.
Definition: tournament.hh:176
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:205
unsigned choiceCtrBits
Number of bits in the choice predictor&#39;s counters.
Definition: tournament.hh:232
unsigned choiceHistoryMask
Mask to apply to globalHistory to access choice history table.
Definition: tournament.hh:222
unsigned localHistoryBits
Number of bits for each entry of the local history table.
Definition: tournament.hh:196
void updateLocalHistTaken(unsigned local_history_idx)
Updates local histories as taken.
Definition: tournament.cc:149

Generated on Fri Feb 28 2020 16:27:00 for gem5 by doxygen 1.8.13