gem5 v24.0.0.0
Loading...
Searching...
No Matches
simple_btb.cc
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) 2004-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
42
43#include "base/intmath.hh"
44#include "base/trace.hh"
45#include "debug/BTB.hh"
46
47namespace gem5
48{
49
50namespace branch_prediction
51{
52
53SimpleBTB::SimpleBTB(const SimpleBTBParams &p)
55 numEntries(p.numEntries),
56 tagBits(p.tagBits),
57 instShiftAmt(p.instShiftAmt),
58 log2NumThreads(floorLog2(p.numThreads))
59{
60 DPRINTF(BTB, "BTB: Creating BTB object.\n");
61
62 if (!isPowerOf2(numEntries)) {
63 fatal("BTB entries is not a power of 2!");
64 }
65
66 btb.resize(numEntries);
67
68 for (unsigned i = 0; i < numEntries; ++i) {
69 btb[i].valid = false;
70 }
71
72 idxMask = numEntries - 1;
73
74 tagMask = (1 << tagBits) - 1;
75
77}
78
79void
81{
82 for (unsigned i = 0; i < numEntries; ++i) {
83 btb[i].valid = false;
84 }
85}
86
87inline
88unsigned
90{
91 // Need to shift PC over by the word offset.
92 return ((instPC >> instShiftAmt)
94 & idxMask;
95}
96
97inline
98Addr
100{
101 return (instPC >> tagShiftAmt) & tagMask;
102}
103
106{
107 unsigned btb_idx = getIndex(instPC, tid);
108 Addr inst_tag = getTag(instPC);
109
110 assert(btb_idx < numEntries);
111
112 if (btb[btb_idx].valid
113 && inst_tag == btb[btb_idx].tag
114 && btb[btb_idx].tid == tid) {
115 return &btb[btb_idx];
116 }
117
118 return nullptr;
119}
120
121bool
123{
124 BTBEntry *entry = findEntry(instPC, tid);
125
126 return entry != nullptr;
127}
128
129// @todo Create some sort of return struct that has both whether or not the
130// address is valid, and also the address. For now will just use addr = 0 to
131// represent invalid entry.
132const PCStateBase *
134{
135 stats.lookups[type]++;
136
137 BTBEntry *entry = findEntry(instPC, tid);
138
139 if (entry) {
140 return entry->target.get();
141 }
142 stats.misses[type]++;
143 return nullptr;
144}
145
146const StaticInstPtr
148{
149 BTBEntry *entry = findEntry(instPC, tid);
150
151 if (entry) {
152 return entry->inst;
153 }
154 return nullptr;
155}
156
157void
159 const PCStateBase &target,
161{
162 unsigned btb_idx = getIndex(instPC, tid);
163
164 assert(btb_idx < numEntries);
165
166 stats.updates[type]++;
167
168 btb[btb_idx].tid = tid;
169 btb[btb_idx].valid = true;
170 set(btb[btb_idx].target, target);
171 btb[btb_idx].tag = getTag(instPC);
172 btb[btb_idx].inst = inst;
173}
174
175} // namespace branch_prediction
176} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
void update() const
Align cycle and tick to the next clock edge if not already done.
gem5::branch_prediction::BranchTargetBuffer::BranchTargetBufferStats stats
unsigned log2NumThreads
Log2 NumThreads used for hashing threadid.
bool valid(ThreadID tid, Addr instPC) override
Checks if a branch address is in the BTB.
const PCStateBase * lookup(ThreadID tid, Addr instPC, BranchType type=BranchType::NoBranch) override
Looks up an address in the BTB to get the target of the branch.
void memInvalidate() override
Invalidate the contents of memory buffers.
Definition simple_btb.cc:80
unsigned tagMask
The tag mask.
unsigned numEntries
The number of entries in the BTB.
Addr getTag(Addr instPC)
Returns the tag bits of a given address.
Definition simple_btb.cc:99
BTBEntry * findEntry(Addr instPC, ThreadID tid)
Internal call to find an address in the BTB.
unsigned instShiftAmt
Number of bits to shift PC when calculating index.
const StaticInstPtr getInst(ThreadID tid, Addr instPC) override
Looks up an address in the BTB and return the instruction information if existant.
unsigned tagShiftAmt
Number of bits to shift PC when calculating tag.
std::vector< BTBEntry > btb
The actual BTB.
SimpleBTB(const SimpleBTBParams &params)
Definition simple_btb.cc:53
unsigned getIndex(Addr instPC, ThreadID tid)
Returns the index into the BTB, based on the branch's PC.
Definition simple_btb.cc:89
unsigned tagBits
The number of tag bits per entry.
unsigned idxMask
The index mask.
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:200
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 12, 11 > set
Bitfield< 0 > p
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria 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
std::unique_ptr< PCStateBase > target
The entry's target.
Definition simple_btb.hh:77
StaticInstPtr inst
Pointer to the static branch instruction at this address.
Definition simple_btb.hh:86

Generated on Tue Jun 18 2024 16:24:02 for gem5 by doxygen 1.11.0