gem5 v25.0.0.1
Loading...
Searching...
No Matches
tage_base.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 The University of Wisconsin
3 * Copyright (c) 2024 Technical University of Munich
4 *
5 * Copyright (c) 2006 INRIA (Institut National de Recherche en
6 * Informatique et en Automatique / French National Research Institute
7 * for Computer Science and Applied Mathematics)
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are
13 * met: redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer;
15 * redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution;
18 * neither the name of the copyright holders nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/* @file
36 * Implementation of a TAGE branch predictor. TAGE is a global-history based
37 * branch predictor. It features a PC-indexed bimodal predictor and N
38 * partially tagged tables, indexed with a hash of the PC and the global
39 * branch history. The different lengths of global branch history used to
40 * index the partially tagged tables grow geometrically. A small path history
41 * is also used in the hash.
42 *
43 * All TAGE tables are accessed in parallel, and the one using the longest
44 * history that matches provides the prediction (some exceptions apply).
45 * Entries are allocated in components using a longer history than the
46 * one that predicted when the prediction is incorrect.
47 */
48
49#ifndef __CPU_PRED_TAGE_BASE_HH__
50#define __CPU_PRED_TAGE_BASE_HH__
51
52#include <vector>
53
54#include "base/statistics.hh"
56#include "cpu/static_inst.hh"
57#include "params/TAGEBase.hh"
58#include "sim/sim_object.hh"
59
60namespace gem5
61{
62
63namespace branch_prediction
64{
65
66class TAGEBase : public SimObject
67{
68 public:
69 TAGEBase(const TAGEBaseParams &p);
70 void init() override;
71
72 protected:
73 // Prediction Structures
74
75 // Tage Entry
76 struct TageEntry
77 {
78 int8_t ctr;
79 uint16_t tag;
80 uint8_t u;
81 TageEntry() : ctr(0), tag(0), u(0) { }
82 };
83
84 // Folded History Table - compressed history
85 // to mix with instruction PC to index partially
86 // tagged tables.
88 {
89 unsigned comp;
94
96 {
97 comp = 0;
98 }
99
100 void init(int original_length, int compressed_length)
101 {
102 origLength = original_length;
103 compLength = compressed_length;
104 outpoint = original_length % compressed_length;
105 }
106
107 void update(uint8_t * h)
108 {
109 comp = (comp << 1) | h[0];
110 comp ^= h[origLength] << outpoint;
111 comp ^= (comp >> compLength);
112 comp &= (1ULL << compLength) - 1;
113 }
114
115 void restore(uint8_t * h)
116 {
117 comp ^= h[origLength] << outpoint;
118 auto tmp = (comp & 1) ^ h[0];
119 comp = (tmp << (compLength-1)) | (comp >> 1);
120 }
121 };
122
123 public:
124
125 // provider type
126 enum
127 {
133 };
134
135 // Primary branch history entry
137 {
139 const bool condBranch;
140
147
152
153 // Pointer to dynamically allocated storage
154 // to save table indices and folded histories.
155 // To do one call to new instead of five.
157
158 // Pointers to actual saved array within the dynamically
159 // allocated storage.
162 int *ci;
163 int *ct0;
164 int *ct1;
165
166 // for stats purposes
167 unsigned provider;
168
169 // The bit vector and the number of bits of global
170 // history used for this branch.
171 uint64_t ghist;
172 uint8_t nGhist;
173
174 // A flag to indicate if this history entry has
175 // modified the histories
177 // A flag to indicate if the indies and tags are valid.
178 bool valid;
179
180 BranchInfo(const TAGEBase &tage, Addr pc, bool conditional)
181 : branchPC(pc), condBranch(conditional),
182 hitBank(0), hitBankIndex(0),
183 altBank(0), altBankIndex(0),
184 bimodalIndex(0),
185 tagePred(false), altTaken(false),
186 longestMatchPred(false),
187 pseudoNewAlloc(false),
188 provider(-1),
189 ghist(0), nGhist(0),
190 modified(false),
191 valid(false)
192 {
193 int sz = tage.nHistoryTables + 1;
194 storage = new int [sz * 5];
196 tableTags = storage + sz;
197 ci = tableTags + sz;
198 ct0 = ci + sz;
199 ct1 = ct0 + sz;
200 }
201
202 virtual ~BranchInfo()
203 {
204 delete[] storage;
205 }
206 };
207
208 virtual BranchInfo *makeBranchInfo(Addr pc, bool conditional);
209
210
216 virtual int bindex(Addr pc_in) const;
217
226 virtual int gindex(ThreadID tid, Addr pc, int bank) const;
227
235 virtual int F(int phist, int size, int bank) const;
236
244 virtual uint16_t gtag(ThreadID tid, Addr pc, int bank) const;
245
253 template<typename T>
254 static void ctrUpdate(T & ctr, bool taken, int nbits);
255
263 static void unsignedCtrUpdate(uint8_t & ctr, bool up, unsigned nbits);
264
272 virtual bool getBimodePred(Addr pc, BranchInfo* bi) const;
273
281 void baseUpdate(Addr pc, bool taken, BranchInfo* bi);
282
293 void updateGHist(ThreadID tid, uint64_t bv, uint8_t n);
294
305 void update(ThreadID tid, Addr branch_pc, bool taken, BranchInfo* bi);
306
322 virtual void updateHistories(ThreadID tid, Addr branch_pc,
323 bool speculative, bool taken, Addr target,
324 const StaticInstPtr &inst, BranchInfo* bi);
332
340
350 virtual void updatePathAndGlobalHistory(ThreadID tid, int brtype,
351 bool taken, Addr branch_pc, Addr target, BranchInfo* bi);
352
356 virtual int branchTypeExtra(const StaticInstPtr & inst) { return 0; }
357
372 virtual void squash(ThreadID tid, bool taken, Addr target,
373 const StaticInstPtr &inst, BranchInfo *bi);
374
387 virtual void condBranchUpdate(
388 ThreadID tid, Addr branch_pc, bool taken, BranchInfo* bi,
389 int nrand, Addr corrTarget, bool pred, bool preAdjustAlloc = false);
390
399 bool tagePredict(
400 ThreadID tid, Addr branch_pc, bool cond_branch, BranchInfo* bi);
401
408 virtual void updateStats(bool taken, BranchInfo* bi);
409
413 virtual void buildTageTables();
414
419 virtual void calculateParameters();
420
425 virtual void calculateIndicesAndTags(
426 ThreadID tid, Addr branch_pc, BranchInfo* bi);
427
432 virtual unsigned getUseAltIdx(BranchInfo* bi, Addr branch_pc);
433
439 virtual void adjustAlloc(bool & alloc, bool taken, bool pred_taken);
440
444 virtual void handleAllocAndUReset(
445 bool alloc, bool taken, BranchInfo* bi, int nrand);
446
450 virtual void handleUReset();
451
455 virtual void handleTAGEUpdate(
456 Addr branch_pc, bool taken, BranchInfo* bi);
457
461 virtual void resetUctr(uint8_t & u);
462
467 virtual void extraAltCalc(BranchInfo* bi);
468
469 virtual bool isHighConfidence(BranchInfo* bi) const
470 {
471 return false;
472 }
473
474 unsigned getGHR(ThreadID tid) const;
475 int8_t getCtr(int hitBank, int hitBankIndex) const;
476 unsigned getTageCtrBits() const;
477 int getPathHist(ThreadID tid, bool speculative=true) const;
478 virtual int calcNewPathHist(ThreadID tid, Addr pc, int cur_phist,
479 bool taken, int brtype, Addr target) const;
480 bool isSpeculativeUpdateEnabled() const;
481 size_t getSizeInBits() const;
482
483 protected:
485 const unsigned nHistoryTables;
486 const unsigned tagTableCounterBits;
487 const unsigned tagTableUBits;
488 const unsigned histBufferSize;
489 const unsigned minHist;
490 const unsigned maxHist;
491 const unsigned pathHistBits;
492
495
499
500 // Keep per-thread histories to
501 // support SMT.
503 {
504 // Speculative path history
505 // (LSB of branch address)
507 // Non-speculative path history
509
510 // Speculative branch direction
511 // history (circular buffer)
513
514 // Index to most recent branch outcome
516
517 // Speculative folded histories.
520 };
521
523
527 virtual void initFoldedHistories(ThreadHistory & history);
528
532
534 int64_t tCounter;
536 const int64_t initialTCounterValue;
539 unsigned maxNumAlloc;
542
543 // Tells which tables are active
544 // (for the base TAGE implementation all are active)
545 // Some other classes use this for handling associativity
547
549
550 const unsigned instShiftAmt;
551
553
572};
573
574} // namespace branch_prediction
575} // namespace gem5
576
577#endif // __CPU_PRED_TAGE_BASE_HH__
int getPathHist(ThreadID tid, bool speculative=true) const
Definition tage_base.cc:870
TAGEBase(const TAGEBaseParams &p)
Definition tage_base.cc:51
virtual int branchTypeExtra(const StaticInstPtr &inst)
This function acts as a hook for other TAGE implementations to adjust the branch type.
Definition tage_base.hh:356
virtual BranchInfo * makeBranchInfo(Addr pc, bool conditional)
Definition tage_base.cc:83
virtual void handleAllocAndUReset(bool alloc, bool taken, BranchInfo *bi, int nrand)
Handles Allocation and U bits reset on an update.
Definition tage_base.cc:444
void update(ThreadID tid, Addr branch_pc, bool taken, BranchInfo *bi)
Update TAGE.
virtual void initFoldedHistories(ThreadHistory &history)
Initialization of the folded histories.
Definition tage_base.cc:151
static void ctrUpdate(T &ctr, bool taken, int nbits)
Updates a direction counter based on the actual branch outcome.
Definition tage_base.cc:242
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition tage_base.cc:88
virtual unsigned getUseAltIdx(BranchInfo *bi, Addr branch_pc)
Calculation of the index for useAltPredForNewlyAllocated On this base TAGE implementation it is alway...
Definition tage_base.cc:356
virtual void resetUctr(uint8_t &u)
Algorithm for resetting a single U counter.
Definition tage_base.cc:508
virtual void extraAltCalc(BranchInfo *bi)
Extra steps for calculating altTaken For this base TAGE class it does nothing.
Definition tage_base.cc:747
static void unsignedCtrUpdate(uint8_t &ctr, bool up, unsigned nbits)
Updates an unsigned counter based on up/down parameter.
Definition tage_base.cc:260
virtual void handleUReset()
Handles the U bits reset.
Definition tage_base.cc:493
virtual void updateStats(bool taken, BranchInfo *bi)
Update the stats.
Definition tage_base.cc:754
virtual void calculateParameters()
Calculates the history lengths and some other paramters in derived classes.
Definition tage_base.cc:174
const unsigned logRatioBiModalHystEntries
Definition tage_base.hh:484
std::vector< bool > btableHysteresis
Definition tage_base.hh:497
std::vector< bool > btablePrediction
Definition tage_base.hh:496
virtual void adjustAlloc(bool &alloc, bool taken, bool pred_taken)
Extra calculation to tell whether TAGE allocaitons may happen or not on an update For this base TAGE ...
Definition tage_base.cc:438
void recordHistState(ThreadID tid, BranchInfo *bi)
Records the current state of the histories to be able to restore it in case of a mispredicted specula...
Definition tage_base.cc:679
std::vector< ThreadHistory > threadHistory
Definition tage_base.hh:522
virtual void calculateIndicesAndTags(ThreadID tid, Addr branch_pc, BranchInfo *bi)
On a prediction, calculates the TAGE indices and tags for all the different history lengths.
Definition tage_base.cc:342
virtual bool getBimodePred(Addr pc, BranchInfo *bi) const
Get a branch prediction from the bimodal predictor.
Definition tage_base.cc:274
virtual int calcNewPathHist(ThreadID tid, Addr pc, int cur_phist, bool taken, int brtype, Addr target) const
Definition tage_base.cc:726
int8_t getCtr(int hitBank, int hitBankIndex) const
Definition tage_base.cc:858
virtual void squash(ThreadID tid, bool taken, Addr target, const StaticInstPtr &inst, BranchInfo *bi)
Restores speculatively updated path and direction histories.
Definition tage_base.cc:740
virtual uint16_t gtag(ThreadID tid, Addr pc, int bank) const
Computes the partial tag of a tagged table.
Definition tage_base.cc:229
std::vector< int > logTagTableSizes
Definition tage_base.hh:494
void baseUpdate(Addr pc, bool taken, BranchInfo *bi)
Updates the bimodal predictor.
Definition tage_base.cc:283
virtual bool isHighConfidence(BranchInfo *bi) const
Definition tage_base.hh:469
virtual int F(int phist, int size, int bank) const
Utility function to shuffle the path history depending on which tagged table we are accessing.
Definition tage_base.cc:194
bool tagePredict(ThreadID tid, Addr branch_pc, bool cond_branch, BranchInfo *bi)
TAGE prediction called from TAGE::predict.
Definition tage_base.cc:363
std::vector< int8_t > useAltPredForNewlyAllocated
Definition tage_base.hh:533
unsigned getGHR(ThreadID tid) const
Definition tage_base.cc:803
virtual void updatePathAndGlobalHistory(ThreadID tid, int brtype, bool taken, Addr branch_pc, Addr target, BranchInfo *bi)
Does the actual update of path and global history.
Definition tage_base.cc:588
const bool takenOnlyHistory
Use taken only history.
Definition tage_base.hh:541
virtual void updateHistories(ThreadID tid, Addr branch_pc, bool speculative, bool taken, Addr target, const StaticInstPtr &inst, BranchInfo *bi)
(Speculatively) updates global histories (path and direction).
Definition tage_base.cc:623
std::vector< unsigned > tagTableTagWidths
Definition tage_base.hh:493
gem5::branch_prediction::TAGEBase::TAGEBaseStats stats
virtual void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken, BranchInfo *bi, int nrand, Addr corrTarget, bool pred, bool preAdjustAlloc=false)
Update TAGE for conditional branches.
Definition tage_base.cc:514
virtual void buildTageTables()
Instantiates the TAGE table entries.
Definition tage_base.cc:166
void updateGHist(ThreadID tid, uint64_t bv, uint8_t n)
Internal history update function.
Definition tage_base.cc:303
virtual int gindex(ThreadID tid, Addr pc, int bank) const
Computes the index used to access a partially tagged table.
Definition tage_base.cc:211
virtual void handleTAGEUpdate(Addr branch_pc, bool taken, BranchInfo *bi)
Handles the update of the TAGE entries.
Definition tage_base.cc:555
virtual int bindex(Addr pc_in) const
Computes the index used to access the bimodal table.
Definition tage_base.cc:188
void restoreHistState(ThreadID tid, BranchInfo *bi)
Restore the state of the histories in case of detecting a mispredicted speculative update.
Definition tage_base.cc:692
Statistics container.
Definition group.hh:93
This is a simple scalar statistic, like a counter.
A vector of scalar stats.
STL vector class.
Definition stl.hh:37
SimObject(const Params &p)
Definition sim_object.cc:58
Bitfield< 31 > n
Bitfield< 23 > up
Definition types.hh:124
Bitfield< 22 > u
Bitfield< 4 > pc
Bitfield< 0 > p
Bitfield< 20, 16 > bi
Definition types.hh:80
Bitfield< 27, 24 > pred
Definition types.hh:91
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
RefCountingPtr< StaticInst > StaticInstPtr
Declaration of Statistics objects.
BranchInfo(const TAGEBase &tage, Addr pc, bool conditional)
Definition tage_base.hh:180
void init(int original_length, int compressed_length)
Definition tage_base.hh:100
TAGEBaseStats(statistics::Group *parent, unsigned nHistoryTables)
Definition tage_base.cc:815

Generated on Sat Oct 18 2025 08:06:42 for gem5 by doxygen 1.14.0