gem5  v20.1.0.0
loop_predictor.hh
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 
34 #ifndef __CPU_PRED_LOOP_PREDICTOR_HH__
35 #define __CPU_PRED_LOOP_PREDICTOR_HH__
36 
37 #include "base/statistics.hh"
38 #include "base/types.hh"
39 #include "sim/sim_object.hh"
40 
41 struct LoopPredictorParams;
42 
43 class LoopPredictor : public SimObject
44 {
45  protected:
46  const unsigned logSizeLoopPred;
47  const unsigned loopTableAgeBits;
48  const unsigned loopTableConfidenceBits;
49  const unsigned loopTableTagBits;
50  const unsigned loopTableIterBits;
51  const unsigned logLoopTableAssoc;
52  const uint8_t confidenceThreshold;
53  const uint16_t loopTagMask;
54  const uint16_t loopNumIterMask;
55  const int loopSetMask;
56 
57  // Prediction Structures
58  // Loop Predictor Entry
59  struct LoopEntry
60  {
61  uint16_t numIter;
62  uint16_t currentIter;
63  uint16_t currentIterSpec; // only for useSpeculation
64  uint8_t confidence;
65  uint16_t tag;
66  uint8_t age;
67  bool dir; // only for useDirectionBit
68 
70  confidence(0), tag(0), age(0), dir(0) { }
71  };
72 
74 
76  unsigned withLoopBits;
77 
78  const bool useDirectionBit;
79  const bool useSpeculation;
80  const bool useHashing;
81  const bool restrictAllocation;
82  const unsigned initialLoopIter;
83  const unsigned initialLoopAge;
84  const bool optionalAgeReset;
85 
86  struct LoopPredictorStats : public Stats::Group {
90  } stats;
91 
99  static inline void unsignedCtrUpdate(uint8_t &ctr, bool up, unsigned nbits)
100  {
101  assert(nbits <= sizeof(uint8_t) << 3);
102  if (up) {
103  if (ctr < ((1 << nbits) - 1))
104  ctr++;
105  } else {
106  if (ctr)
107  ctr--;
108  }
109  }
110  static inline void signedCtrUpdate(int8_t &ctr, bool up, unsigned nbits)
111  {
112  if (up) {
113  if (ctr < ((1 << (nbits - 1)) - 1))
114  ctr++;
115  } else {
116  if (ctr > -(1 << (nbits - 1)))
117  ctr--;
118  }
119  }
120  public:
121  // Primary branch history entry
122  struct BranchInfo
123  {
124  uint16_t loopTag;
125  uint16_t currentIter;
126 
127  bool loopPred;
131  int loopIndexB; // only for useHashing
132  int loopHit;
133  bool predTaken;
134 
136  : loopTag(0), currentIter(0),
137  loopPred(false),
138  loopPredValid(false), loopIndex(0), loopIndexB(0), loopHit(0),
139  predTaken(false)
140  {}
141  };
142 
149  int lindex(Addr pc_in, unsigned instShiftAmt) const;
150 
159  int finallindex(int lindex, int lowPcBits, int way) const;
160 
172  bool getLoop(Addr pc, BranchInfo* bi, bool speculative,
173  unsigned instShiftAmt) const;
174 
183  void loopUpdate(Addr pc, bool Taken, BranchInfo* bi, bool tage_pred);
184 
192  void specLoopUpdate(bool taken, BranchInfo* bi);
193 
203  void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken,
204  bool tage_pred, BranchInfo* bi, unsigned instShiftAmt);
205 
219  bool loopPredict(
220  ThreadID tid, Addr branch_pc, bool cond_branch,
221  BranchInfo* bi, bool prev_pred_taken, unsigned instShiftAmt);
222 
229  void updateStats(bool taken, BranchInfo* bi);
230 
231  void squashLoop(BranchInfo * bi);
232 
233  void squash(ThreadID tid, BranchInfo *bi);
234 
235  virtual bool calcConf(int index) const;
236 
237  virtual bool optionalAgeInc() const;
238 
239  virtual BranchInfo *makeBranchInfo();
240 
245  int8_t getLoopUseCounter() const
246  {
247  return loopUseCounter;
248  }
249 
253  void init() override;
254 
255  LoopPredictor(LoopPredictorParams *p);
256 
257  size_t getSizeInBits() const;
258 };
259 #endif//__CPU_PRED_LOOP_PREDICTOR_HH__
LoopPredictor::LoopEntry::tag
uint16_t tag
Definition: loop_predictor.hh:65
LoopPredictor::BranchInfo::loopIndex
int loopIndex
Definition: loop_predictor.hh:130
LoopPredictor::loopTableConfidenceBits
const unsigned loopTableConfidenceBits
Definition: loop_predictor.hh:48
LoopPredictor::BranchInfo::loopPred
bool loopPred
Definition: loop_predictor.hh:127
LoopPredictor::BranchInfo::loopHit
int loopHit
Definition: loop_predictor.hh:132
LoopPredictor::unsignedCtrUpdate
static void unsignedCtrUpdate(uint8_t &ctr, bool up, unsigned nbits)
Updates an unsigned counter based on up/down parameter.
Definition: loop_predictor.hh:99
MipsISA::index
Bitfield< 30, 0 > index
Definition: pra_constants.hh:44
LoopPredictor::updateStats
void updateStats(bool taken, BranchInfo *bi)
Update the stats.
Definition: loop_predictor.cc:315
LoopPredictor::BranchInfo
Definition: loop_predictor.hh:122
ThreadID
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:227
LoopPredictor::stats
LoopPredictor::LoopPredictorStats stats
LoopPredictor::withLoopBits
unsigned withLoopBits
Definition: loop_predictor.hh:76
LoopPredictor::BranchInfo::BranchInfo
BranchInfo()
Definition: loop_predictor.hh:135
LoopPredictor::BranchInfo::loopPredValid
bool loopPredValid
Definition: loop_predictor.hh:128
LoopPredictor::squash
void squash(ThreadID tid, BranchInfo *bi)
Definition: loop_predictor.cc:293
LoopPredictor::finallindex
int finallindex(int lindex, int lowPcBits, int way) const
Computes the index used to access the ltable structures.
Definition: loop_predictor.cc:103
LoopPredictor::confidenceThreshold
const uint8_t confidenceThreshold
Definition: loop_predictor.hh:52
LoopPredictor::specLoopUpdate
void specLoopUpdate(bool taken, BranchInfo *bi)
Speculatively updates the loop predictor iteration count (only for useSpeculation).
Definition: loop_predictor.cc:156
LoopPredictor
Definition: loop_predictor.hh:43
LoopPredictor::loopUpdate
void loopUpdate(Addr pc, bool Taken, BranchInfo *bi, bool tage_pred)
Updates the loop predictor.
Definition: loop_predictor.cc:176
LoopPredictor::loopSetMask
const int loopSetMask
Definition: loop_predictor.hh:55
LoopPredictor::loopUseCounter
int8_t loopUseCounter
Definition: loop_predictor.hh:75
LoopPredictor::initialLoopAge
const unsigned initialLoopAge
Definition: loop_predictor.hh:83
LoopPredictor::optionalAgeInc
virtual bool optionalAgeInc() const
Definition: loop_predictor.cc:170
LoopPredictor::LoopPredictorStats::correct
Stats::Scalar correct
Definition: loop_predictor.hh:88
LoopPredictor::optionalAgeReset
const bool optionalAgeReset
Definition: loop_predictor.hh:84
LoopPredictor::LoopPredictorStats
Definition: loop_predictor.hh:86
LoopPredictor::LoopPredictorStats::wrong
Stats::Scalar wrong
Definition: loop_predictor.hh:89
LoopPredictor::calcConf
virtual bool calcConf(int index) const
Definition: loop_predictor.cc:150
Stats::Scalar
This is a simple scalar statistic, like a counter.
Definition: statistics.hh:2533
PowerISA::bi
Bitfield< 20, 16 > bi
Definition: types.hh:63
LoopPredictor::makeBranchInfo
virtual BranchInfo * makeBranchInfo()
Definition: loop_predictor.cc:80
LoopPredictor::useHashing
const bool useHashing
Definition: loop_predictor.hh:80
LoopPredictor::useSpeculation
const bool useSpeculation
Definition: loop_predictor.hh:79
LoopPredictor::BranchInfo::loopIndexB
int loopIndexB
Definition: loop_predictor.hh:131
LoopPredictor::LoopPredictor
LoopPredictor(LoopPredictorParams *p)
Definition: loop_predictor.cc:41
LoopPredictor::init
void init() override
Initialize the loop predictor.
Definition: loop_predictor.cc:67
LoopPredictor::ltable
LoopEntry * ltable
Definition: loop_predictor.hh:73
LoopPredictor::BranchInfo::predTaken
bool predTaken
Definition: loop_predictor.hh:133
sim_object.hh
LoopPredictor::loopTagMask
const uint16_t loopTagMask
Definition: loop_predictor.hh:53
MipsISA::pc
Bitfield< 4 > pc
Definition: pra_constants.hh:240
statistics.hh
LoopPredictor::LoopPredictorStats::LoopPredictorStats
LoopPredictorStats(Stats::Group *parent)
Definition: loop_predictor.cc:348
LoopPredictor::logSizeLoopPred
const unsigned logSizeLoopPred
Definition: loop_predictor.hh:46
LoopPredictor::squashLoop
void squashLoop(BranchInfo *bi)
Definition: loop_predictor.cc:304
LoopPredictor::initialLoopIter
const unsigned initialLoopIter
Definition: loop_predictor.hh:82
LoopPredictor::loopTableIterBits
const unsigned loopTableIterBits
Definition: loop_predictor.hh:50
LoopPredictor::getLoop
bool getLoop(Addr pc, BranchInfo *bi, bool speculative, unsigned instShiftAmt) const
Get a branch prediction from the loop predictor.
Definition: loop_predictor.cc:112
LoopPredictor::BranchInfo::loopTag
uint16_t loopTag
Definition: loop_predictor.hh:124
LoopPredictor::signedCtrUpdate
static void signedCtrUpdate(int8_t &ctr, bool up, unsigned nbits)
Definition: loop_predictor.hh:110
LoopPredictor::LoopEntry::confidence
uint8_t confidence
Definition: loop_predictor.hh:64
LoopPredictor::LoopEntry
Definition: loop_predictor.hh:59
LoopPredictor::loopPredict
bool loopPredict(ThreadID tid, Addr branch_pc, bool cond_branch, BranchInfo *bi, bool prev_pred_taken, unsigned instShiftAmt)
Get the loop prediction.
Definition: loop_predictor.cc:271
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
LoopPredictor::loopTableTagBits
const unsigned loopTableTagBits
Definition: loop_predictor.hh:49
LoopPredictor::BranchInfo::loopPredUsed
bool loopPredUsed
Definition: loop_predictor.hh:129
LoopPredictor::LoopEntry::numIter
uint16_t numIter
Definition: loop_predictor.hh:61
LoopPredictor::restrictAllocation
const bool restrictAllocation
Definition: loop_predictor.hh:81
LoopPredictor::LoopEntry::LoopEntry
LoopEntry()
Definition: loop_predictor.hh:69
LoopPredictor::loopTableAgeBits
const unsigned loopTableAgeBits
Definition: loop_predictor.hh:47
LoopPredictor::LoopEntry::age
uint8_t age
Definition: loop_predictor.hh:66
LoopPredictor::getSizeInBits
size_t getSizeInBits() const
Definition: loop_predictor.cc:358
LoopPredictor::LoopEntry::dir
bool dir
Definition: loop_predictor.hh:67
LoopPredictor::LoopEntry::currentIterSpec
uint16_t currentIterSpec
Definition: loop_predictor.hh:63
types.hh
LoopPredictor::BranchInfo::currentIter
uint16_t currentIter
Definition: loop_predictor.hh:125
Stats::Group
Statistics container.
Definition: group.hh:83
LoopPredictor::condBranchUpdate
void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken, bool tage_pred, BranchInfo *bi, unsigned instShiftAmt)
Update LTAGE for conditional branches.
Definition: loop_predictor.cc:325
ArmISA::up
Bitfield< 23 > up
Definition: types.hh:133
LoopPredictor::getLoopUseCounter
int8_t getLoopUseCounter() const
Gets the value of the loop use counter.
Definition: loop_predictor.hh:245
LoopPredictor::lindex
int lindex(Addr pc_in, unsigned instShiftAmt) const
Computes the index used to access the loop predictor.
Definition: loop_predictor.cc:86
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
LoopPredictor::logLoopTableAssoc
const unsigned logLoopTableAssoc
Definition: loop_predictor.hh:51
LoopPredictor::loopNumIterMask
const uint16_t loopNumIterMask
Definition: loop_predictor.hh:54
LoopPredictor::LoopEntry::currentIter
uint16_t currentIter
Definition: loop_predictor.hh:62
LoopPredictor::useDirectionBit
const bool useDirectionBit
Definition: loop_predictor.hh:78
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:92

Generated on Wed Sep 30 2020 14:02:09 for gem5 by doxygen 1.8.17