gem5  v22.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 namespace gem5
42 {
43 
44 struct LoopPredictorParams;
45 
46 namespace branch_prediction
47 {
48 
49 class LoopPredictor : public SimObject
50 {
51  protected:
52  const unsigned logSizeLoopPred;
53  const unsigned loopTableAgeBits;
54  const unsigned loopTableConfidenceBits;
55  const unsigned loopTableTagBits;
56  const unsigned loopTableIterBits;
57  const unsigned logLoopTableAssoc;
58  const uint8_t confidenceThreshold;
59  const uint16_t loopTagMask;
60  const uint16_t loopNumIterMask;
61  const int loopSetMask;
62 
63  // Prediction Structures
64  // Loop Predictor Entry
65  struct LoopEntry
66  {
67  uint16_t numIter;
68  uint16_t currentIter;
69  uint16_t currentIterSpec; // only for useSpeculation
70  uint8_t confidence;
71  uint16_t tag;
72  uint8_t age;
73  bool dir; // only for useDirectionBit
74 
76  confidence(0), tag(0), age(0), dir(0) { }
77  };
78 
80 
82  unsigned withLoopBits;
83 
84  const bool useDirectionBit;
85  const bool useSpeculation;
86  const bool useHashing;
87  const bool restrictAllocation;
88  const unsigned initialLoopIter;
89  const unsigned initialLoopAge;
90  const bool optionalAgeReset;
91 
93  {
97  } stats;
98 
106  static inline void unsignedCtrUpdate(uint8_t &ctr, bool up, unsigned nbits)
107  {
108  assert(nbits <= sizeof(uint8_t) << 3);
109  if (up) {
110  if (ctr < ((1 << nbits) - 1))
111  ctr++;
112  } else {
113  if (ctr)
114  ctr--;
115  }
116  }
117  static inline void signedCtrUpdate(int8_t &ctr, bool up, unsigned nbits)
118  {
119  if (up) {
120  if (ctr < ((1 << (nbits - 1)) - 1))
121  ctr++;
122  } else {
123  if (ctr > -(1 << (nbits - 1)))
124  ctr--;
125  }
126  }
127  public:
128  // Primary branch history entry
129  struct BranchInfo
130  {
131  uint16_t loopTag;
132  uint16_t currentIter;
133 
134  bool loopPred;
138  int loopIndexB; // only for useHashing
139  int loopHit;
140  bool predTaken;
141 
143  : loopTag(0), currentIter(0),
144  loopPred(false),
145  loopPredValid(false), loopIndex(0), loopIndexB(0), loopHit(0),
146  predTaken(false)
147  {}
148  };
149 
156  int lindex(Addr pc_in, unsigned instShiftAmt) const;
157 
166  int finallindex(int lindex, int lowPcBits, int way) const;
167 
179  bool getLoop(Addr pc, BranchInfo* bi, bool speculative,
180  unsigned instShiftAmt) const;
181 
190  void loopUpdate(Addr pc, bool Taken, BranchInfo* bi, bool tage_pred);
191 
199  void specLoopUpdate(bool taken, BranchInfo* bi);
200 
210  void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken,
211  bool tage_pred, BranchInfo* bi, unsigned instShiftAmt);
212 
226  bool loopPredict(
227  ThreadID tid, Addr branch_pc, bool cond_branch,
228  BranchInfo* bi, bool prev_pred_taken, unsigned instShiftAmt);
229 
236  void updateStats(bool taken, BranchInfo* bi);
237 
238  void squashLoop(BranchInfo * bi);
239 
240  void squash(ThreadID tid, BranchInfo *bi);
241 
242  virtual bool calcConf(int index) const;
243 
244  virtual bool optionalAgeInc() const;
245 
246  virtual BranchInfo *makeBranchInfo();
247 
252  int8_t getLoopUseCounter() const
253  {
254  return loopUseCounter;
255  }
256 
260  void init() override;
261 
262  LoopPredictor(const LoopPredictorParams &p);
263 
264  size_t getSizeInBits() const;
265 };
266 
267 } // namespace branch_prediction
268 } // namespace gem5
269 
270 #endif//__CPU_PRED_LOOP_PREDICTOR_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Abstract superclass for simulation objects.
Definition: sim_object.hh:148
LoopPredictor(const LoopPredictorParams &p)
void updateStats(bool taken, BranchInfo *bi)
Update the stats.
int finallindex(int lindex, int lowPcBits, int way) const
Computes the index used to access the ltable structures.
static void unsignedCtrUpdate(uint8_t &ctr, bool up, unsigned nbits)
Updates an unsigned counter based on up/down parameter.
static void signedCtrUpdate(int8_t &ctr, bool up, unsigned nbits)
void specLoopUpdate(bool taken, BranchInfo *bi)
Speculatively updates the loop predictor iteration count (only for useSpeculation).
gem5::branch_prediction::LoopPredictor::LoopPredictorStats stats
int lindex(Addr pc_in, unsigned instShiftAmt) const
Computes the index used to access the loop predictor.
void init() override
Initialize the loop predictor.
void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken, bool tage_pred, BranchInfo *bi, unsigned instShiftAmt)
Update LTAGE for conditional branches.
virtual bool calcConf(int index) const
int8_t getLoopUseCounter() const
Gets the value of the loop use counter.
bool getLoop(Addr pc, BranchInfo *bi, bool speculative, unsigned instShiftAmt) const
Get a branch prediction from the loop predictor.
bool loopPredict(ThreadID tid, Addr branch_pc, bool cond_branch, BranchInfo *bi, bool prev_pred_taken, unsigned instShiftAmt)
Get the loop prediction.
void loopUpdate(Addr pc, bool Taken, BranchInfo *bi, bool tage_pred)
Updates the loop predictor.
void squash(ThreadID tid, BranchInfo *bi)
Statistics container.
Definition: group.hh:94
This is a simple scalar statistic, like a counter.
Definition: statistics.hh:1931
Bitfield< 23 > up
Definition: types.hh:124
Bitfield< 4 > pc
Bitfield< 30, 0 > index
Bitfield< 20, 16 > bi
Definition: types.hh:80
Bitfield< 54 > p
Definition: pagetable.hh:70
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:235
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
Declaration of Statistics objects.

Generated on Wed Dec 21 2022 10:22:31 for gem5 by doxygen 1.9.1