gem5  v20.0.0.3
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 "debug/LTage.hh"
38 #include "params/LoopPredictor.hh"
39 
40 LoopPredictor::LoopPredictor(LoopPredictorParams *p)
41  : SimObject(p), logSizeLoopPred(p->logSizeLoopPred),
42  loopTableAgeBits(p->loopTableAgeBits),
43  loopTableConfidenceBits(p->loopTableConfidenceBits),
44  loopTableTagBits(p->loopTableTagBits),
45  loopTableIterBits(p->loopTableIterBits),
46  logLoopTableAssoc(p->logLoopTableAssoc),
47  confidenceThreshold((1 << loopTableConfidenceBits) - 1),
48  loopTagMask((1 << loopTableTagBits) - 1),
49  loopNumIterMask((1 << loopTableIterBits) - 1),
50  loopSetMask((1 << (logSizeLoopPred - logLoopTableAssoc)) - 1),
51  loopUseCounter(-1),
52  withLoopBits(p->withLoopBits),
53  useDirectionBit(p->useDirectionBit),
54  useSpeculation(p->useSpeculation),
55  useHashing(p->useHashing),
56  restrictAllocation(p->restrictAllocation),
57  initialLoopIter(p->initialLoopIter),
58  initialLoopAge(p->initialLoopAge),
59  optionalAgeReset(p->optionalAgeReset)
60 {
61  assert(initialLoopAge <= ((1 << loopTableAgeBits) - 1));
62 }
63 
64 void
66 {
67  // we use uint16_t type for these vales, so they cannot be more than
68  // 16 bits
69  assert(loopTableTagBits <= 16);
70  assert(loopTableIterBits <= 16);
71 
73 
74  ltable = new LoopEntry[ULL(1) << logSizeLoopPred];
75 }
76 
79 {
80  return new BranchInfo();
81 }
82 
83 int
84 LoopPredictor::lindex(Addr pc_in, unsigned instShiftAmt) const
85 {
86  // The loop table is implemented as a linear table
87  // If associativity is N (N being 1 << logLoopTableAssoc),
88  // the first N entries are for set 0, the next N entries are for set 1,
89  // and so on.
90  // Thus, this function calculates the set and then it gets left shifted
91  // by logLoopTableAssoc in order to return the index of the first of the
92  // N entries of the set
93  Addr pc = pc_in >> instShiftAmt;
94  if (useHashing) {
95  pc ^= pc_in;
96  }
97  return ((pc & loopSetMask) << logLoopTableAssoc);
98 }
99 
100 int
101 LoopPredictor::finallindex(int index, int lowPcBits, int way) const
102 {
103  return (useHashing ? (index ^ ((lowPcBits >> way) << logLoopTableAssoc)) :
104  (index))
105  + way;
106 }
107 
108 //loop prediction: only used if high confidence
109 bool
111  unsigned instShiftAmt) const
112 {
113  bi->loopHit = -1;
114  bi->loopPredValid = false;
115  bi->loopIndex = lindex(pc, instShiftAmt);
116 
117  if (useHashing) {
118  unsigned pcShift = logSizeLoopPred - logLoopTableAssoc;
119  bi->loopIndexB = (pc >> pcShift) & loopSetMask;
120  bi->loopTag = (pc >> pcShift) ^ (pc >> (pcShift + loopTableTagBits));
121  bi->loopTag &= loopTagMask;
122  } else {
123  unsigned pcShift = instShiftAmt + logSizeLoopPred - logLoopTableAssoc;
124  bi->loopTag = (pc >> pcShift) & loopTagMask;
125  // bi->loopIndexB is not used without hash
126  }
127 
128  for (int i = 0; i < (1 << logLoopTableAssoc); i++) {
129  int idx = finallindex(bi->loopIndex, bi->loopIndexB, i);
130  if (ltable[idx].tag == bi->loopTag) {
131  bi->loopHit = i;
132  bi->loopPredValid = calcConf(idx);
133 
134  uint16_t iter = speculative ? ltable[idx].currentIterSpec
135  : ltable[idx].currentIter;
136 
137  if ((iter + 1) == ltable[idx].numIter) {
138  return useDirectionBit ? !(ltable[idx].dir) : false;
139  } else {
140  return useDirectionBit ? (ltable[idx].dir) : true;
141  }
142  }
143  }
144  return false;
145 }
146 
147 bool
149 {
151 }
152 
153 void
155 {
156  if (bi->loopHit>=0) {
157  int index = finallindex(bi->loopIndex, bi->loopIndexB, bi->loopHit);
158  if (taken != ltable[index].dir) {
160  } else {
163  }
164  }
165 }
166 
167 bool
169 {
170  return false;
171 }
172 
173 void
174 LoopPredictor::loopUpdate(Addr pc, bool taken, BranchInfo* bi, bool tage_pred)
175 {
176  int idx = finallindex(bi->loopIndex, bi->loopIndexB, bi->loopHit);
177  if (bi->loopHit >= 0) {
178  //already a hit
179  if (bi->loopPredValid) {
180  if (taken != bi->loopPred) {
181  // free the entry
182  ltable[idx].numIter = 0;
183  ltable[idx].age = 0;
184  ltable[idx].confidence = 0;
185  ltable[idx].currentIter = 0;
186  return;
187  } else if (bi->loopPred != tage_pred || optionalAgeInc()) {
188  DPRINTF(LTage, "Loop Prediction success:%lx\n",pc);
189  unsignedCtrUpdate(ltable[idx].age, true, loopTableAgeBits);
190  }
191  }
192 
193  ltable[idx].currentIter =
194  (ltable[idx].currentIter + 1) & loopNumIterMask;
195  if (ltable[idx].currentIter > ltable[idx].numIter) {
196  ltable[idx].confidence = 0;
197  if (ltable[idx].numIter != 0) {
198  // free the entry
199  ltable[idx].numIter = 0;
200  if (optionalAgeReset) {
201  ltable[idx].age = 0;
202  }
203  }
204  }
205 
206  if (taken != (useDirectionBit ? ltable[idx].dir : true)) {
207  if (ltable[idx].currentIter == ltable[idx].numIter) {
208  DPRINTF(LTage, "Loop End predicted successfully:%lx\n", pc);
209  unsignedCtrUpdate(ltable[idx].confidence, true,
211  //just do not predict when the loop count is 1 or 2
212  if (ltable[idx].numIter < 3) {
213  // free the entry
214  ltable[idx].dir = taken; // ignored if no useDirectionBit
215  ltable[idx].numIter = 0;
216  ltable[idx].age = 0;
217  ltable[idx].confidence = 0;
218  }
219  } else {
220  DPRINTF(LTage, "Loop End predicted incorrectly:%lx\n", pc);
221  if (ltable[idx].numIter == 0) {
222  // first complete nest;
223  ltable[idx].confidence = 0;
224  ltable[idx].numIter = ltable[idx].currentIter;
225  } else {
226  //not the same number of iterations as last time: free the
227  //entry
228  ltable[idx].numIter = 0;
229  if (optionalAgeReset) {
230  ltable[idx].age = 0;
231  }
232  ltable[idx].confidence = 0;
233  }
234  }
235  ltable[idx].currentIter = 0;
236  }
237 
238  } else if (useDirectionBit ? (bi->predTaken != taken) : taken) {
239  if ((random_mt.random<int>() & 3) == 0 || !restrictAllocation) {
240  //try to allocate an entry on taken branch
241  int nrand = random_mt.random<int>();
242  for (int i = 0; i < (1 << logLoopTableAssoc); i++) {
243  int loop_hit = (nrand + i) & ((1 << logLoopTableAssoc) - 1);
244  idx = finallindex(bi->loopIndex, bi->loopIndexB, loop_hit);
245  if (ltable[idx].age == 0) {
246  DPRINTF(LTage,
247  "Allocating loop pred entry for branch %lx\n",
248  pc);
249  ltable[idx].dir = !taken; // ignored if no useDirectionBit
250  ltable[idx].tag = bi->loopTag;
251  ltable[idx].numIter = 0;
252  ltable[idx].age = initialLoopAge;
253  ltable[idx].confidence = 0;
255  break;
256 
257  } else {
258  ltable[idx].age--;
259  }
260  if (restrictAllocation) {
261  break;
262  }
263  }
264  }
265  }
266 }
267 
268 bool
269 LoopPredictor::loopPredict(ThreadID tid, Addr branch_pc, bool cond_branch,
270  BranchInfo* bi, bool prev_pred_taken, unsigned instShiftAmt)
271 {
272  bool pred_taken = prev_pred_taken;
273  if (cond_branch) {
274  // loop prediction
275  bi->loopPred = getLoop(branch_pc, bi, useSpeculation, instShiftAmt);
276 
277  if ((loopUseCounter >= 0) && bi->loopPredValid) {
278  pred_taken = bi->loopPred;
279  bi->loopPredUsed = true;
280  }
281 
282  if (useSpeculation) {
283  specLoopUpdate(pred_taken, bi);
284  }
285  }
286 
287  return pred_taken;
288 }
289 
290 void
292 {
293  if (bi->loopHit >= 0) {
294  int idx = finallindex(bi->loopIndex,
295  bi->loopIndexB,
296  bi->loopHit);
298  }
299 }
300 
301 void
303 {
304  if (bi->loopHit >= 0) {
305  int idx = finallindex(bi->loopIndex,
306  bi->loopIndexB,
307  bi->loopHit);
309  }
310 }
311 
312 void
314 {
315  if (taken == bi->loopPred) {
317  } else {
319  }
320 }
321 
322 void
323 LoopPredictor::condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken,
324  bool tage_pred, BranchInfo* bi,
325  unsigned instShiftAmt)
326 {
327  if (useSpeculation) {
328  // recalculate loop prediction without speculation
329  // It is ok to overwrite the loop prediction fields in bi
330  // as the stats have already been updated with the previous
331  // values
332  bi->loopPred = getLoop(branch_pc, bi, false, instShiftAmt);
333  }
334 
335  if (bi->loopPredValid) {
336  if (bi->predTaken != bi->loopPred) {
338  (bi->loopPred == taken),
339  withLoopBits);
340  }
341  }
342 
343  loopUpdate(branch_pc, taken, bi, tage_pred);
344 }
345 
346 void
348 {
350  .name(name() + ".loopPredictorCorrect")
351  .desc("Number of times the loop predictor is the provider and "
352  "the prediction is correct");
353 
355  .name(name() + ".loopPredictorWrong")
356  .desc("Number of times the loop predictor is the provider and "
357  "the prediction is wrong");
358 }
359 
360 size_t
362 {
363  return (1ULL << logSizeLoopPred) *
364  ((useSpeculation ? 3 : 2) * loopTableIterBits +
367 }
368 
370 LoopPredictorParams::create()
371 {
372  return new LoopPredictor(this);
373 }
#define DPRINTF(x,...)
Definition: trace.hh:225
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:129
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:93
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 Fri Jul 3 2020 15:53:00 for gem5 by doxygen 1.8.13