gem5  v20.0.0.2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
loop_predictor.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 The University of Wisconsin
3  *
4  * Copyright (c) 2006 INRIA (Institut National de Recherche en
5  * Informatique et en Automatique / French National Research Institute
6  * for Computer Science and Applied Mathematics)
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are
12  * met: redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer;
14  * redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution;
17  * neither the name of the copyright holders nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
35 
36 #include "base/random.hh"
37 #include "base/trace.hh"
38 #include "debug/LTage.hh"
39 #include "params/LoopPredictor.hh"
40 
41 LoopPredictor::LoopPredictor(LoopPredictorParams *p)
42  : SimObject(p), logSizeLoopPred(p->logSizeLoopPred),
43  loopTableAgeBits(p->loopTableAgeBits),
44  loopTableConfidenceBits(p->loopTableConfidenceBits),
45  loopTableTagBits(p->loopTableTagBits),
46  loopTableIterBits(p->loopTableIterBits),
47  logLoopTableAssoc(p->logLoopTableAssoc),
48  confidenceThreshold((1 << loopTableConfidenceBits) - 1),
49  loopTagMask((1 << loopTableTagBits) - 1),
50  loopNumIterMask((1 << loopTableIterBits) - 1),
51  loopSetMask((1 << (logSizeLoopPred - logLoopTableAssoc)) - 1),
52  loopUseCounter(-1),
53  withLoopBits(p->withLoopBits),
54  useDirectionBit(p->useDirectionBit),
55  useSpeculation(p->useSpeculation),
56  useHashing(p->useHashing),
57  restrictAllocation(p->restrictAllocation),
58  initialLoopIter(p->initialLoopIter),
59  initialLoopAge(p->initialLoopAge),
60  optionalAgeReset(p->optionalAgeReset)
61 {
62  assert(initialLoopAge <= ((1 << loopTableAgeBits) - 1));
63 }
64 
65 void
67 {
68  // we use uint16_t type for these vales, so they cannot be more than
69  // 16 bits
70  assert(loopTableTagBits <= 16);
71  assert(loopTableIterBits <= 16);
72 
74 
75  ltable = new LoopEntry[ULL(1) << logSizeLoopPred];
76 }
77 
80 {
81  return new BranchInfo();
82 }
83 
84 int
85 LoopPredictor::lindex(Addr pc_in, unsigned instShiftAmt) const
86 {
87  // The loop table is implemented as a linear table
88  // If associativity is N (N being 1 << logLoopTableAssoc),
89  // the first N entries are for set 0, the next N entries are for set 1,
90  // and so on.
91  // Thus, this function calculates the set and then it gets left shifted
92  // by logLoopTableAssoc in order to return the index of the first of the
93  // N entries of the set
94  Addr pc = pc_in >> instShiftAmt;
95  if (useHashing) {
96  pc ^= pc_in;
97  }
98  return ((pc & loopSetMask) << logLoopTableAssoc);
99 }
100 
101 int
102 LoopPredictor::finallindex(int index, int lowPcBits, int way) const
103 {
104  return (useHashing ? (index ^ ((lowPcBits >> way) << logLoopTableAssoc)) :
105  (index))
106  + way;
107 }
108 
109 //loop prediction: only used if high confidence
110 bool
112  unsigned instShiftAmt) const
113 {
114  bi->loopHit = -1;
115  bi->loopPredValid = false;
116  bi->loopIndex = lindex(pc, instShiftAmt);
117 
118  if (useHashing) {
119  unsigned pcShift = logSizeLoopPred - logLoopTableAssoc;
120  bi->loopIndexB = (pc >> pcShift) & loopSetMask;
121  bi->loopTag = (pc >> pcShift) ^ (pc >> (pcShift + loopTableTagBits));
122  bi->loopTag &= loopTagMask;
123  } else {
124  unsigned pcShift = instShiftAmt + logSizeLoopPred - logLoopTableAssoc;
125  bi->loopTag = (pc >> pcShift) & loopTagMask;
126  // bi->loopIndexB is not used without hash
127  }
128 
129  for (int i = 0; i < (1 << logLoopTableAssoc); i++) {
130  int idx = finallindex(bi->loopIndex, bi->loopIndexB, i);
131  if (ltable[idx].tag == bi->loopTag) {
132  bi->loopHit = i;
133  bi->loopPredValid = calcConf(idx);
134 
135  uint16_t iter = speculative ? ltable[idx].currentIterSpec
136  : ltable[idx].currentIter;
137 
138  if ((iter + 1) == ltable[idx].numIter) {
139  return useDirectionBit ? !(ltable[idx].dir) : false;
140  } else {
141  return useDirectionBit ? (ltable[idx].dir) : true;
142  }
143  }
144  }
145  return false;
146 }
147 
148 bool
150 {
152 }
153 
154 void
156 {
157  if (bi->loopHit>=0) {
158  int index = finallindex(bi->loopIndex, bi->loopIndexB, bi->loopHit);
159  if (taken != ltable[index].dir) {
161  } else {
164  }
165  }
166 }
167 
168 bool
170 {
171  return false;
172 }
173 
174 void
175 LoopPredictor::loopUpdate(Addr pc, bool taken, BranchInfo* bi, bool tage_pred)
176 {
177  int idx = finallindex(bi->loopIndex, bi->loopIndexB, bi->loopHit);
178  if (bi->loopHit >= 0) {
179  //already a hit
180  if (bi->loopPredValid) {
181  if (taken != bi->loopPred) {
182  // free the entry
183  ltable[idx].numIter = 0;
184  ltable[idx].age = 0;
185  ltable[idx].confidence = 0;
186  ltable[idx].currentIter = 0;
187  return;
188  } else if (bi->loopPred != tage_pred || optionalAgeInc()) {
189  DPRINTF(LTage, "Loop Prediction success:%lx\n",pc);
190  unsignedCtrUpdate(ltable[idx].age, true, loopTableAgeBits);
191  }
192  }
193 
194  ltable[idx].currentIter =
195  (ltable[idx].currentIter + 1) & loopNumIterMask;
196  if (ltable[idx].currentIter > ltable[idx].numIter) {
197  ltable[idx].confidence = 0;
198  if (ltable[idx].numIter != 0) {
199  // free the entry
200  ltable[idx].numIter = 0;
201  if (optionalAgeReset) {
202  ltable[idx].age = 0;
203  }
204  }
205  }
206 
207  if (taken != (useDirectionBit ? ltable[idx].dir : true)) {
208  if (ltable[idx].currentIter == ltable[idx].numIter) {
209  DPRINTF(LTage, "Loop End predicted successfully:%lx\n", pc);
210  unsignedCtrUpdate(ltable[idx].confidence, true,
212  //just do not predict when the loop count is 1 or 2
213  if (ltable[idx].numIter < 3) {
214  // free the entry
215  ltable[idx].dir = taken; // ignored if no useDirectionBit
216  ltable[idx].numIter = 0;
217  ltable[idx].age = 0;
218  ltable[idx].confidence = 0;
219  }
220  } else {
221  DPRINTF(LTage, "Loop End predicted incorrectly:%lx\n", pc);
222  if (ltable[idx].numIter == 0) {
223  // first complete nest;
224  ltable[idx].confidence = 0;
225  ltable[idx].numIter = ltable[idx].currentIter;
226  } else {
227  //not the same number of iterations as last time: free the
228  //entry
229  ltable[idx].numIter = 0;
230  if (optionalAgeReset) {
231  ltable[idx].age = 0;
232  }
233  ltable[idx].confidence = 0;
234  }
235  }
236  ltable[idx].currentIter = 0;
237  }
238 
239  } else if (useDirectionBit ? (bi->predTaken != taken) : taken) {
240  if ((random_mt.random<int>() & 3) == 0 || !restrictAllocation) {
241  //try to allocate an entry on taken branch
242  int nrand = random_mt.random<int>();
243  for (int i = 0; i < (1 << logLoopTableAssoc); i++) {
244  int loop_hit = (nrand + i) & ((1 << logLoopTableAssoc) - 1);
245  idx = finallindex(bi->loopIndex, bi->loopIndexB, loop_hit);
246  if (ltable[idx].age == 0) {
247  DPRINTF(LTage,
248  "Allocating loop pred entry for branch %lx\n",
249  pc);
250  ltable[idx].dir = !taken; // ignored if no useDirectionBit
251  ltable[idx].tag = bi->loopTag;
252  ltable[idx].numIter = 0;
253  ltable[idx].age = initialLoopAge;
254  ltable[idx].confidence = 0;
256  break;
257 
258  } else {
259  ltable[idx].age--;
260  }
261  if (restrictAllocation) {
262  break;
263  }
264  }
265  }
266  }
267 }
268 
269 bool
270 LoopPredictor::loopPredict(ThreadID tid, Addr branch_pc, bool cond_branch,
271  BranchInfo* bi, bool prev_pred_taken, unsigned instShiftAmt)
272 {
273  bool pred_taken = prev_pred_taken;
274  if (cond_branch) {
275  // loop prediction
276  bi->loopPred = getLoop(branch_pc, bi, useSpeculation, instShiftAmt);
277 
278  if ((loopUseCounter >= 0) && bi->loopPredValid) {
279  pred_taken = bi->loopPred;
280  bi->loopPredUsed = true;
281  }
282 
283  if (useSpeculation) {
284  specLoopUpdate(pred_taken, bi);
285  }
286  }
287 
288  return pred_taken;
289 }
290 
291 void
293 {
294  if (bi->loopHit >= 0) {
295  int idx = finallindex(bi->loopIndex,
296  bi->loopIndexB,
297  bi->loopHit);
299  }
300 }
301 
302 void
304 {
305  if (bi->loopHit >= 0) {
306  int idx = finallindex(bi->loopIndex,
307  bi->loopIndexB,
308  bi->loopHit);
310  }
311 }
312 
313 void
315 {
316  if (taken == bi->loopPred) {
318  } else {
320  }
321 }
322 
323 void
324 LoopPredictor::condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken,
325  bool tage_pred, BranchInfo* bi,
326  unsigned instShiftAmt)
327 {
328  if (useSpeculation) {
329  // recalculate loop prediction without speculation
330  // It is ok to overwrite the loop prediction fields in bi
331  // as the stats have already been updated with the previous
332  // values
333  bi->loopPred = getLoop(branch_pc, bi, false, instShiftAmt);
334  }
335 
336  if (bi->loopPredValid) {
337  if (bi->predTaken != bi->loopPred) {
339  (bi->loopPred == taken),
340  withLoopBits);
341  }
342  }
343 
344  loopUpdate(branch_pc, taken, bi, tage_pred);
345 }
346 
347 void
349 {
351  .name(name() + ".loopPredictorCorrect")
352  .desc("Number of times the loop predictor is the provider and "
353  "the prediction is correct");
354 
356  .name(name() + ".loopPredictorWrong")
357  .desc("Number of times the loop predictor is the provider and "
358  "the prediction is wrong");
359 }
360 
361 size_t
363 {
364  return (1ULL << logSizeLoopPred) *
365  ((useSpeculation ? 3 : 2) * loopTableIterBits +
368 }
369 
371 LoopPredictorParams::create()
372 {
373  return new LoopPredictor(this);
374 }
#define DPRINTF(x,...)
Definition: trace.hh:222
bool getLoop(Addr pc, BranchInfo *bi, bool speculative, unsigned instShiftAmt) const
Get a branch prediction from the loop predictor.
LoopEntry * ltable
void squashLoop(BranchInfo *bi)
const bool optionalAgeReset
Bitfield< 30, 0 > index
void squash(ThreadID tid, BranchInfo *bi)
Bitfield< 7 > i
Stats::Scalar loopPredictorCorrect
void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken, bool tage_pred, BranchInfo *bi, unsigned instShiftAmt)
Update LTAGE for conditional branches.
virtual BranchInfo * makeBranchInfo()
const unsigned loopTableAgeBits
const int loopSetMask
const bool useDirectionBit
Bitfield< 20, 16 > bi
Definition: types.hh:63
std::enable_if< std::is_integral< T >::value, T >::type random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
Definition: random.hh:79
void init() override
Initialize the loop predictor.
Stats::Scalar loopPredictorWrong
const uint16_t loopNumIterMask
const unsigned loopTableConfidenceBits
const unsigned logLoopTableAssoc
Bitfield< 4 > pc
static void unsignedCtrUpdate(uint8_t &ctr, bool up, unsigned nbits)
Updates an unsigned counter based on up/down parameter.
virtual bool calcConf(int index) const
int lindex(Addr pc_in, unsigned instShiftAmt) const
Computes the index used to access the loop predictor.
int finallindex(int lindex, int lowPcBits, int way) const
Computes the index used to access the ltable structures.
size_t getSizeInBits() const
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:140
#define ULL(N)
uint64_t constant
Definition: types.hh:48
static void signedCtrUpdate(int8_t &ctr, bool up, unsigned nbits)
const unsigned loopTableIterBits
Derived & name(const std::string &name)
Set the name and marks this stat to print at the end of simulation.
Definition: statistics.hh:276
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:225
virtual const std::string name() const
Definition: sim_object.hh:128
const uint8_t confidenceThreshold
const bool useHashing
void specLoopUpdate(bool taken, BranchInfo *bi)
Speculatively updates the loop predictor iteration count (only for useSpeculation).
void regStats() override
Register stats for this object.
LoopPredictor(LoopPredictorParams *p)
const unsigned loopTableTagBits
Random random_mt
Definition: random.cc:96
const unsigned initialLoopAge
const uint16_t loopTagMask
const unsigned logSizeLoopPred
const unsigned initialLoopIter
const bool useSpeculation
Derived & desc(const std::string &_desc)
Set the description and marks this stat to print at the end of simulation.
Definition: statistics.hh:309
unsigned withLoopBits
Bitfield< 0 > p
bool loopPredict(ThreadID tid, Addr branch_pc, bool cond_branch, BranchInfo *bi, bool prev_pred_taken, unsigned instShiftAmt)
Get the loop prediction.
int8_t loopUseCounter
Abstract superclass for simulation objects.
Definition: sim_object.hh:92
void loopUpdate(Addr pc, bool Taken, BranchInfo *bi, bool tage_pred)
Updates the loop predictor.
const bool restrictAllocation
void updateStats(bool taken, BranchInfo *bi)
Update the stats.
virtual bool optionalAgeInc() const

Generated on Mon Jun 8 2020 15:45:09 for gem5 by doxygen 1.8.13