gem5  v22.1.0.0
btb.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004-2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "cpu/pred/btb.hh"
30 
31 #include "base/intmath.hh"
32 #include "base/trace.hh"
33 #include "debug/Fetch.hh"
34 
35 namespace gem5
36 {
37 
38 namespace branch_prediction
39 {
40 
41 DefaultBTB::DefaultBTB(unsigned _numEntries,
42  unsigned _tagBits,
43  unsigned _instShiftAmt,
44  unsigned _num_threads)
45  : numEntries(_numEntries),
46  tagBits(_tagBits),
47  instShiftAmt(_instShiftAmt),
48  log2NumThreads(floorLog2(_num_threads))
49 {
50  DPRINTF(Fetch, "BTB: Creating BTB object.\n");
51 
52  if (!isPowerOf2(numEntries)) {
53  fatal("BTB entries is not a power of 2!");
54  }
55 
56  btb.resize(numEntries);
57 
58  for (unsigned i = 0; i < numEntries; ++i) {
59  btb[i].valid = false;
60  }
61 
62  idxMask = numEntries - 1;
63 
64  tagMask = (1 << tagBits) - 1;
65 
67 }
68 
69 void
71 {
72  for (unsigned i = 0; i < numEntries; ++i) {
73  btb[i].valid = false;
74  }
75 }
76 
77 inline
78 unsigned
80 {
81  // Need to shift PC over by the word offset.
82  return ((instPC >> instShiftAmt)
83  ^ (tid << (tagShiftAmt - instShiftAmt - log2NumThreads)))
84  & idxMask;
85 }
86 
87 inline
88 Addr
90 {
91  return (instPC >> tagShiftAmt) & tagMask;
92 }
93 
94 bool
96 {
97  unsigned btb_idx = getIndex(instPC, tid);
98 
99  Addr inst_tag = getTag(instPC);
100 
101  assert(btb_idx < numEntries);
102 
103  if (btb[btb_idx].valid
104  && inst_tag == btb[btb_idx].tag
105  && btb[btb_idx].tid == tid) {
106  return true;
107  } else {
108  return false;
109  }
110 }
111 
112 // @todo Create some sort of return struct that has both whether or not the
113 // address is valid, and also the address. For now will just use addr = 0 to
114 // represent invalid entry.
115 const PCStateBase *
117 {
118  unsigned btb_idx = getIndex(inst_pc, tid);
119 
120  Addr inst_tag = getTag(inst_pc);
121 
122  assert(btb_idx < numEntries);
123 
124  if (btb[btb_idx].valid
125  && inst_tag == btb[btb_idx].tag
126  && btb[btb_idx].tid == tid) {
127  return btb[btb_idx].target.get();
128  } else {
129  return nullptr;
130  }
131 }
132 
133 void
134 DefaultBTB::update(Addr inst_pc, const PCStateBase &target, ThreadID tid)
135 {
136  unsigned btb_idx = getIndex(inst_pc, tid);
137 
138  assert(btb_idx < numEntries);
139 
140  btb[btb_idx].tid = tid;
141  btb[btb_idx].valid = true;
142  set(btb[btb_idx].target, target);
143  btb[btb_idx].tag = getTag(inst_pc);
144 }
145 
146 } // namespace branch_prediction
147 } // namespace gem5
#define DPRINTF(x,...)
Definition: trace.hh:186
bool valid(Addr instPC, ThreadID tid)
Checks if a branch is in the BTB.
Definition: btb.cc:95
Addr getTag(Addr instPC)
Returns the tag bits of a given address.
Definition: btb.cc:89
std::vector< BTBEntry > btb
The actual BTB.
Definition: btb.hh:107
unsigned tagBits
The number of tag bits per entry.
Definition: btb.hh:116
unsigned instShiftAmt
Number of bits to shift PC when calculating index.
Definition: btb.hh:122
void update(Addr inst_pc, const PCStateBase &target_pc, ThreadID tid)
Updates the BTB with the target of a branch.
Definition: btb.cc:134
unsigned log2NumThreads
Log2 NumThreads used for hashing threadid.
Definition: btb.hh:128
unsigned idxMask
The index mask.
Definition: btb.hh:113
unsigned tagShiftAmt
Number of bits to shift PC when calculating tag.
Definition: btb.hh:125
const PCStateBase * lookup(Addr instPC, ThreadID tid)
Looks up an address in the BTB.
Definition: btb.cc:116
unsigned numEntries
The number of entries in the BTB.
Definition: btb.hh:110
unsigned getIndex(Addr instPC, ThreadID tid)
Returns the index into the BTB, based on the branch's PC.
Definition: btb.cc:79
unsigned tagMask
The tag mask.
Definition: btb.hh:119
DefaultBTB(unsigned numEntries, unsigned tagBits, unsigned instShiftAmt, unsigned numThreads)
Creates a BTB with the given number of entries, number of bits per tag, and instruction offset amount...
Definition: btb.cc:41
static constexpr std::enable_if_t< std::is_integral_v< T >, int > floorLog2(T x)
Definition: intmath.hh:59
static constexpr bool isPowerOf2(const T &n)
Definition: intmath.hh:98
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:190
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 12, 11 > set
Definition: misc_types.hh:709
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

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