gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tage_base.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  * Authors: Vignyan Reddy, Dibakar Gope and Arthur Perais,
34  * from André Seznec's code.
35  */
36 
37 /* @file
38  * Implementation of a TAGE branch predictor. TAGE is a global-history based
39  * branch predictor. It features a PC-indexed bimodal predictor and N
40  * partially tagged tables, indexed with a hash of the PC and the global
41  * branch history. The different lengths of global branch history used to
42  * index the partially tagged tables grow geometrically. A small path history
43  * is also used in the hash.
44  *
45  * All TAGE tables are accessed in parallel, and the one using the longest
46  * history that matches provides the prediction (some exceptions apply).
47  * Entries are allocated in components using a longer history than the
48  * one that predicted when the prediction is incorrect.
49  */
50 
51 #ifndef __CPU_PRED_TAGE_BASE
52 #define __CPU_PRED_TAGE_BASE
53 
54 #include <vector>
55 
56 #include "base/statistics.hh"
57 #include "cpu/static_inst.hh"
58 #include "params/TAGEBase.hh"
59 #include "sim/sim_object.hh"
60 
61 class TAGEBase : public SimObject
62 {
63  public:
64  TAGEBase(const TAGEBaseParams *p);
65  void regStats() override;
66  void init() override;
67 
68  protected:
69  // Prediction Structures
70 
71  // Tage Entry
72  struct TageEntry
73  {
74  int8_t ctr;
75  uint16_t tag;
76  uint8_t u;
77  TageEntry() : ctr(0), tag(0), u(0) { }
78  };
79 
80  // Folded History Table - compressed history
81  // to mix with instruction PC to index partially
82  // tagged tables.
84  {
85  unsigned comp;
88  int outpoint;
90 
92  {
93  comp = 0;
94  }
95 
96  void init(int original_length, int compressed_length)
97  {
98  origLength = original_length;
99  compLength = compressed_length;
100  outpoint = original_length % compressed_length;
101  }
102 
103  void update(uint8_t * h)
104  {
105  comp = (comp << 1) | h[0];
106  comp ^= h[origLength] << outpoint;
107  comp ^= (comp >> compLength);
108  comp &= (ULL(1) << compLength) - 1;
109  }
110  };
111 
112  public:
113 
114  // provider type
115  enum {
121  };
122 
123  // Primary branch history entry
124  struct BranchInfo
125  {
126  int pathHist;
127  int ptGhist;
128  int hitBank;
130  int altBank;
133 
134  bool tagePred;
135  bool altTaken;
140 
141  // Pointer to dynamically allocated storage
142  // to save table indices and folded histories.
143  // To do one call to new instead of five.
144  int *storage;
145 
146  // Pointers to actual saved array within the dynamically
147  // allocated storage.
149  int *tableTags;
150  int *ci;
151  int *ct0;
152  int *ct1;
153 
154  // for stats purposes
155  unsigned provider;
156 
157  BranchInfo(const TAGEBase &tage)
158  : pathHist(0), ptGhist(0),
159  hitBank(0), hitBankIndex(0),
160  altBank(0), altBankIndex(0),
161  bimodalIndex(0),
162  tagePred(false), altTaken(false),
163  condBranch(false), longestMatchPred(false),
164  pseudoNewAlloc(false), branchPC(0),
165  provider(-1)
166  {
167  int sz = tage.nHistoryTables + 1;
168  storage = new int [sz * 5];
169  tableIndices = storage;
170  tableTags = storage + sz;
171  ci = tableTags + sz;
172  ct0 = ci + sz;
173  ct1 = ct0 + sz;
174  }
175 
176  virtual ~BranchInfo()
177  {
178  delete[] storage;
179  }
180  };
181 
182  virtual BranchInfo *makeBranchInfo();
183 
189  virtual int bindex(Addr pc_in) const;
190 
199  virtual int gindex(ThreadID tid, Addr pc, int bank) const;
200 
208  virtual int F(int phist, int size, int bank) const;
209 
217  virtual uint16_t gtag(ThreadID tid, Addr pc, int bank) const;
218 
226  template<typename T>
227  static void ctrUpdate(T & ctr, bool taken, int nbits);
228 
236  static void unsignedCtrUpdate(uint8_t & ctr, bool up, unsigned nbits);
237 
245  virtual bool getBimodePred(Addr pc, BranchInfo* bi) const;
246 
254  void baseUpdate(Addr pc, bool taken, BranchInfo* bi);
255 
264  void updateGHist(uint8_t * &h, bool dir, uint8_t * tab, int &PT);
265 
276  void update(ThreadID tid, Addr branch_pc, bool taken, BranchInfo* bi);
277 
290  virtual void updateHistories(
291  ThreadID tid, Addr branch_pc, bool taken, BranchInfo* b,
292  bool speculative,
294  Addr target = MaxAddr);
295 
309  virtual void squash(
310  ThreadID tid, bool taken, BranchInfo *bi, Addr target);
311 
324  virtual void condBranchUpdate(
325  ThreadID tid, Addr branch_pc, bool taken, BranchInfo* bi,
326  int nrand, Addr corrTarget, bool pred, bool preAdjustAlloc = false);
327 
336  bool tagePredict(
337  ThreadID tid, Addr branch_pc, bool cond_branch, BranchInfo* bi);
338 
345  virtual void updateStats(bool taken, BranchInfo* bi);
346 
350  virtual void buildTageTables();
351 
356  virtual void calculateParameters();
357 
362  virtual void calculateIndicesAndTags(
363  ThreadID tid, Addr branch_pc, BranchInfo* bi);
364 
369  virtual unsigned getUseAltIdx(BranchInfo* bi, Addr branch_pc);
370 
376  virtual void adjustAlloc(bool & alloc, bool taken, bool pred_taken);
377 
381  virtual void handleAllocAndUReset(
382  bool alloc, bool taken, BranchInfo* bi, int nrand);
383 
387  virtual void handleUReset();
388 
392  virtual void handleTAGEUpdate(
393  Addr branch_pc, bool taken, BranchInfo* bi);
394 
398  virtual void resetUctr(uint8_t & u);
399 
404  virtual void extraAltCalc(BranchInfo* bi);
405 
406  virtual bool isHighConfidence(BranchInfo* bi) const
407  {
408  return false;
409  }
410 
411  void btbUpdate(ThreadID tid, Addr branch_addr, BranchInfo* &bi);
412  unsigned getGHR(ThreadID tid, BranchInfo *bi) const;
413  int8_t getCtr(int hitBank, int hitBankIndex) const;
414  unsigned getTageCtrBits() const;
415  int getPathHist(ThreadID tid) const;
416  bool isSpeculativeUpdateEnabled() const;
417  size_t getSizeInBits() const;
418 
419  protected:
421  const unsigned nHistoryTables;
422  const unsigned tagTableCounterBits;
423  const unsigned tagTableUBits;
424  const unsigned histBufferSize;
425  const unsigned minHist;
426  const unsigned maxHist;
427  const unsigned pathHistBits;
428 
431 
435 
436  // Keep per-thread histories to
437  // support SMT.
438  struct ThreadHistory {
439  // Speculative path history
440  // (LSB of branch address)
441  int pathHist;
442 
443  // Speculative branch direction
444  // history (circular buffer)
445  // @TODO Convert to std::vector<bool>
446  uint8_t *globalHistory;
447 
448  // Pointer to most recent branch outcome
449  uint8_t* gHist;
450 
451  // Index to most recent branch outcome
452  int ptGhist;
453 
454  // Speculative folded histories.
456  FoldedHistory *computeTags[2];
457  };
458 
460 
464  virtual void initFoldedHistories(ThreadHistory & history);
465 
468  int *tableTags;
469 
471  int64_t tCounter;
472  uint64_t logUResetPeriod;
473  const int64_t initialTCounterValue;
474  unsigned numUseAltOnNa;
475  unsigned useAltOnNaBits;
476  unsigned maxNumAlloc;
477 
478  // Tells which tables are active
479  // (for the base TAGE implementation all are active)
480  // Some other classes use this for handling associativity
482 
484 
485  const unsigned instShiftAmt;
486 
488 
489  // stats
500 
503 };
504 
505 #endif // __CPU_PRED_TAGE_BASE
std::vector< bool > btablePrediction
Definition: tage_base.hh:432
virtual bool isHighConfidence(BranchInfo *bi) const
Definition: tage_base.hh:406
const unsigned tagTableUBits
Definition: tage_base.hh:423
virtual void handleAllocAndUReset(bool alloc, bool taken, BranchInfo *bi, int nrand)
Handles Allocation and U bits reset on an update.
Definition: tage_base.cc:439
virtual void calculateParameters()
Calculates the history lengths and some other paramters in derived classes.
Definition: tage_base.cc:169
std::vector< int > logTagTableSizes
Definition: tage_base.hh:430
const Addr MaxAddr
Definition: types.hh:166
unsigned useAltOnNaBits
Definition: tage_base.hh:475
bool isSpeculativeUpdateEnabled() const
Definition: tage_base.cc:801
virtual void extraAltCalc(BranchInfo *bi)
Extra steps for calculating altTaken For this base TAGE class it does nothing.
Definition: tage_base.cc:650
static void unsignedCtrUpdate(uint8_t &ctr, bool up, unsigned nbits)
Updates an unsigned counter based on up/down parameter.
Definition: tage_base.cc:274
virtual void updateStats(bool taken, BranchInfo *bi)
Update the stats.
Definition: tage_base.cc:657
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:509
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition: tage_base.cc:83
unsigned getGHR(ThreadID tid, BranchInfo *bi) const
Definition: tage_base.cc:704
const unsigned pathHistBits
Definition: tage_base.hh:427
A vector of scalar stats.
Definition: statistics.hh:2550
void baseUpdate(Addr pc, bool taken, BranchInfo *bi)
Updates the bimodal predictor.
Definition: tage_base.cc:297
Stats::Scalar tageLongestMatchProviderCorrect
Definition: tage_base.hh:490
static void ctrUpdate(T &ctr, bool taken, int nbits)
Updates a direction counter based on the actual branch outcome.
Definition: tage_base.cc:256
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:348
virtual void resetUctr(uint8_t &u)
Algorithm for resetting a single U counter.
Definition: tage_base.cc:503
uint64_t logUResetPeriod
Definition: tage_base.hh:472
TageEntry ** gtable
Definition: tage_base.hh:434
virtual uint16_t gtag(ThreadID tid, Addr pc, int bank) const
Computes the partial tag of a tagged table.
Definition: tage_base.cc:243
int * tableIndices
Definition: tage_base.hh:467
const unsigned maxHist
Definition: tage_base.hh:426
void regStats() override
Callback to set stat parameters.
Definition: tage_base.cc:719
Bitfield< 20, 16 > bi
Definition: types.hh:65
Stats::Scalar tageBimodalProviderCorrect
Definition: tage_base.hh:493
Stats::Scalar tageAltMatchProviderWrong
Definition: tage_base.hh:495
Declaration of Statistics objects.
This is a simple scalar statistic, like a counter.
Definition: statistics.hh:2508
bool initialized
Definition: tage_base.hh:487
Stats::Scalar tageAltMatchProviderWouldHaveHit
Definition: tage_base.hh:498
virtual BranchInfo * makeBranchInfo()
Definition: tage_base.cc:78
Stats::Vector tageLongestMatchProvider
Definition: tage_base.hh:501
std::vector< bool > btableHysteresis
Definition: tage_base.hh:433
const unsigned nHistoryTables
Definition: tage_base.hh:421
const unsigned instShiftAmt
Definition: tage_base.hh:485
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:208
Bitfield< 7 > b
const int64_t initialTCounterValue
Definition: tage_base.hh:473
Bitfield< 4 > pc
unsigned numUseAltOnNa
Definition: tage_base.hh:474
virtual void squash(ThreadID tid, bool taken, BranchInfo *bi, Addr target)
Restores speculatively updated path and direction histories.
Definition: tage_base.cc:624
void update(ThreadID tid, Addr branch_pc, bool taken, BranchInfo *bi)
Update TAGE.
virtual int gindex(ThreadID tid, Addr pc, int bank) const
Computes the index used to access a partially tagged table.
Definition: tage_base.cc:225
Stats::Scalar bimodalAltMatchProviderWrong
Definition: tage_base.hh:496
Stats::Scalar bimodalAltMatchProviderCorrect
Definition: tage_base.hh:492
const unsigned histBufferSize
Definition: tage_base.hh:424
const unsigned minHist
Definition: tage_base.hh:425
const bool speculativeHistUpdate
Definition: tage_base.hh:483
virtual bool getBimodePred(Addr pc, BranchInfo *bi) const
Get a branch prediction from the bimodal predictor.
Definition: tage_base.cc:288
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:433
Stats::Scalar tageLongestMatchProviderWouldHaveHit
Definition: tage_base.hh:499
int getPathHist(ThreadID tid) const
Definition: tage_base.cc:795
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
Stats::Scalar tageBimodalProviderWrong
Definition: tage_base.hh:497
void updateGHist(uint8_t *&h, bool dir, uint8_t *tab, int &PT)
(Speculatively) updates the global branch history.
Definition: tage_base.cc:317
#define ULL(N)
uint64_t constant
Definition: types.hh:50
void init(int original_length, int compressed_length)
Definition: tage_base.hh:96
bool tagePredict(ThreadID tid, Addr branch_pc, bool cond_branch, BranchInfo *bi)
TAGE prediction called from TAGE::predict.
Definition: tage_base.cc:355
BranchInfo(const TAGEBase &tage)
Definition: tage_base.hh:157
void btbUpdate(ThreadID tid, Addr branch_addr, BranchInfo *&bi)
Definition: tage_base.cc:183
unsigned maxNumAlloc
Definition: tage_base.hh:476
std::vector< bool > noSkip
Definition: tage_base.hh:481
void update(uint8_t *h)
Definition: tage_base.hh:103
const unsigned logRatioBiModalHystEntries
Definition: tage_base.hh:420
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:227
Bitfield< 23 > up
Definition: types.hh:134
virtual void buildTageTables()
Instantiates the TAGE table entries.
Definition: tage_base.cc:161
unsigned getTageCtrBits() const
Definition: tage_base.cc:789
std::vector< ThreadHistory > threadHistory
Definition: tage_base.hh:459
int * tableTags
Definition: tage_base.hh:468
Stats::Scalar tageAltMatchProviderCorrect
Definition: tage_base.hh:491
virtual int bindex(Addr pc_in) const
Computes the index used to access the bimodal table.
Definition: tage_base.cc:202
int64_t tCounter
Definition: tage_base.hh:471
Stats::Vector tageAltMatchProvider
Definition: tage_base.hh:502
TAGEBase(const TAGEBaseParams *p)
Definition: tage_base.cc:48
std::vector< int8_t > useAltPredForNewlyAllocated
Definition: tage_base.hh:470
static StaticInstPtr nullStaticInstPtr
Pointer to a statically allocated "null" instruction object.
Definition: static_inst.hh:223
virtual void handleTAGEUpdate(Addr branch_pc, bool taken, BranchInfo *bi)
Handles the update of the TAGE entries.
Definition: tage_base.cc:550
std::vector< unsigned > tagTableTagWidths
Definition: tage_base.hh:429
Stats::Scalar tageLongestMatchProviderWrong
Definition: tage_base.hh:494
FoldedHistory * computeIndices
Definition: tage_base.hh:455
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:335
int * histLengths
Definition: tage_base.hh:466
virtual void initFoldedHistories(ThreadHistory &history)
Initialization of the folded histories.
Definition: tage_base.cc:146
Bitfield< 0 > p
virtual void updateHistories(ThreadID tid, Addr branch_pc, bool taken, BranchInfo *b, bool speculative, const StaticInstPtr &inst=StaticInst::nullStaticInstPtr, Addr target=MaxAddr)
(Speculatively) updates global histories (path and direction).
Definition: tage_base.cc:583
virtual ~BranchInfo()
Definition: tage_base.hh:176
Abstract superclass for simulation objects.
Definition: sim_object.hh:96
int8_t getCtr(int hitBank, int hitBankIndex) const
Definition: tage_base.cc:783
const unsigned tagTableCounterBits
Definition: tage_base.hh:422
virtual void handleUReset()
Handles the U bits reset.
Definition: tage_base.cc:488
size_t getSizeInBits() const
Definition: tage_base.cc:807

Generated on Fri Feb 28 2020 16:26:59 for gem5 by doxygen 1.8.13