gem5 v24.1.0.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
loop_predictor.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022-2023 The University of Edinburgh
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) 2006 INRIA (Institut National de Recherche en
15 * Informatique et en Automatique / French National Research Institute
16 * for Computer Science and Applied Mathematics)
17 *
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions are
22 * met: redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer;
24 * redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution;
27 * neither the name of the copyright holders nor the names of its
28 * contributors may be used to endorse or promote products derived from
29 * this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44#ifndef __CPU_PRED_LOOP_PREDICTOR_HH__
45#define __CPU_PRED_LOOP_PREDICTOR_HH__
46
47#include "base/random.hh"
48#include "base/statistics.hh"
49#include "base/types.hh"
50#include "sim/sim_object.hh"
51
52namespace gem5
53{
54
55struct LoopPredictorParams;
56
57namespace branch_prediction
58{
59
61{
62 protected:
63 const unsigned logSizeLoopPred;
64 const unsigned loopTableAgeBits;
66 const unsigned loopTableTagBits;
67 const unsigned loopTableIterBits;
68 const unsigned logLoopTableAssoc;
69 const uint8_t confidenceThreshold;
70 const uint16_t loopTagMask;
71 const uint16_t loopNumIterMask;
72 const int loopSetMask;
73
75
76 // Prediction Structures
77 // Loop Predictor Entry
78 struct LoopEntry
79 {
80 uint16_t numIter;
81 uint16_t currentIter;
82 uint16_t currentIterSpec; // only for useSpeculation
83 uint8_t confidence;
84 uint16_t tag;
85 uint8_t age;
86 bool dir; // only for useDirectionBit
87
89 confidence(0), tag(0), age(0), dir(0) { }
90 };
91
93
95 unsigned withLoopBits;
96
97 const bool useDirectionBit;
98 const bool useSpeculation;
99 const bool useHashing;
101 const unsigned initialLoopIter;
102 const unsigned initialLoopAge;
104
112
120 static inline void unsignedCtrUpdate(uint8_t &ctr, bool up, unsigned nbits)
121 {
122 assert(nbits <= sizeof(uint8_t) << 3);
123 if (up) {
124 if (ctr < ((1 << nbits) - 1))
125 ctr++;
126 } else {
127 if (ctr)
128 ctr--;
129 }
130 }
131 static inline void signedCtrUpdate(int8_t &ctr, bool up, unsigned nbits)
132 {
133 if (up) {
134 if (ctr < ((1 << (nbits - 1)) - 1))
135 ctr++;
136 } else {
137 if (ctr > -(1 << (nbits - 1)))
138 ctr--;
139 }
140 }
141 public:
142 // Primary branch history entry
144 {
145 uint16_t loopTag;
146 uint16_t currentIter;
147
152 int loopIndexB; // only for useHashing
155
157 : loopTag(0), currentIter(0),
158 loopPred(false),
159 loopPredValid(false), loopIndex(0), loopIndexB(0), loopHit(0),
160 predTaken(false)
161 {}
162 };
163
170 int lindex(Addr pc_in, unsigned instShiftAmt) const;
171
180 int finallindex(int lindex, int lowPcBits, int way) const;
181
193 bool getLoop(Addr pc, BranchInfo* bi, bool speculative,
194 unsigned instShiftAmt) const;
195
204 void loopUpdate(Addr pc, bool Taken, BranchInfo* bi, bool tage_pred);
205
213 void specLoopUpdate(bool taken, BranchInfo* bi);
214
224 void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken,
225 bool tage_pred, BranchInfo* bi, unsigned instShiftAmt);
226
240 bool loopPredict(
241 ThreadID tid, Addr branch_pc, bool cond_branch,
242 BranchInfo* bi, bool prev_pred_taken, unsigned instShiftAmt);
243
250 void updateStats(bool taken, BranchInfo* bi);
251
252 void squashLoop(BranchInfo * bi);
253
254 void squash(ThreadID tid, BranchInfo *bi);
255
256 virtual bool calcConf(int index) const;
257
258 virtual bool optionalAgeInc() const;
259
260 virtual BranchInfo *makeBranchInfo();
261
266 int8_t getLoopUseCounter() const
267 {
268 return loopUseCounter;
269 }
270
274 void init() override;
275
276 LoopPredictor(const LoopPredictorParams &p);
277
278 size_t getSizeInBits() const;
279};
280
281} // namespace branch_prediction
282} // namespace gem5
283
284#endif//__CPU_PRED_LOOP_PREDICTOR_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
std::shared_ptr< Random > RandomPtr
Definition random.hh:65
static RandomPtr genRandom()
Definition random.hh:68
Abstract superclass for simulation objects.
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:93
This is a simple scalar statistic, like a counter.
Bitfield< 23 > up
Definition types.hh:124
Bitfield< 4 > pc
Bitfield< 30, 0 > index
Bitfield< 0 > p
Bitfield< 20, 16 > bi
Definition types.hh:80
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
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 Mon Jan 13 2025 04:28:32 for gem5 by doxygen 1.9.8