gem5 v24.1.0.1
Loading...
Searching...
No Matches
btb_entry.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Pranith Kumar
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 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
43#ifndef __CPU_PRED_BTB_ENTRY_HH__
44#define __CPU_PRED_BTB_ENTRY_HH__
45
46#include <vector>
47
49#include "base/intmath.hh"
50#include "base/types.hh"
51#include "cpu/static_inst.hh"
54#include "params/BTBIndexingPolicy.hh"
55#include "params/BTBSetAssociative.hh"
56
57namespace gem5 {
58
60{
61 public:
62 struct KeyType
63 {
66 };
67 using Params = BTBIndexingPolicyParams;
68};
69
72
74{
75 public:
78
80 : BTBIndexingPolicy(p, p.num_entries, p.set_shift),
81 tagMask(mask(p.tag_bits))
82 {
83 setNumThreads(p.numThreads);
84 }
85
86 protected:
90 uint32_t
91 extractSet(const KeyType &key) const
92 {
93 return ((key.address >> setShift)
94 ^ (key.tid << (tagShift - setShift - log2NumThreads)))
95 & setMask;
96 }
97
98 public:
103 getPossibleEntries(const KeyType &key) const override
104 {
105 auto set_idx = extractSet(key);
106
107 assert(set_idx < sets.size());
108
109 return sets[set_idx];
110 }
111
115 void
116 setNumThreads(unsigned num_threads)
117 {
118 log2NumThreads = log2i(num_threads);
119 }
120
124 Addr
125 extractTag(const Addr addr) const override
126 {
127 return (addr >> tagShift) & tagMask;
128 }
129
131 const ReplaceableEntry* entry) const override
132 {
133 panic("Not implemented!");
134 return 0;
135 }
136
137 private:
138 const uint64_t tagMask;
140};
141
142namespace branch_prediction
143{
144
146{
147 public:
150 using TagExtractor = std::function<Addr(Addr)>;
151
154 : inst(nullptr), extractTag(ext), valid(false), tag({MaxAddr, -1})
155 {}
156
160 void
161 update(const PCStateBase &_target,
162 StaticInstPtr _inst)
163 {
164 set(target, _target);
165 inst = _inst;
166 }
167
171 bool
172 match(const KeyType &key) const
173 {
174 return isValid() && (tag.address == extractTag(key.address))
175 && (tag.tid == key.tid);
176 }
177
182 void
183 insert(const KeyType &key)
184 {
185 setValid();
186 setTag({extractTag(key.address), key.tid});
187 }
188
190 BTBEntry(const BTBEntry &other)
191 {
192 valid = other.valid;
193 tag = other.tag;
194 inst = other.inst;
195 extractTag = other.extractTag;
196 set(target, other.target);
197 }
198
201 {
202 valid = other.valid;
203 tag = other.tag;
204 inst = other.inst;
205 extractTag = other.extractTag;
206 set(target, other.target);
207
208 return *this;
209 }
210
214 bool isValid() const { return valid; }
215
219 KeyType getTag() const { return tag; }
220
222 void
224 {
225 valid = false;
226 setTag({MaxAddr, -1});
227 }
228
230 std::unique_ptr<PCStateBase> target;
231
234
235 std::string
236 print() const override
237 {
238 return csprintf("tag: %#x tid: %d valid: %d | %s", tag.address, tag.tid,
239 isValid(), ReplaceableEntry::print());
240 }
241
242 protected:
246 void setTag(KeyType _tag) { tag = _tag; }
247
249 void
251 {
252 assert(!isValid());
253 valid = true;
254 }
255
256 private:
259
265 bool valid;
266
269};
270
271} // namespace gem5::branch_prediction
280static constexpr auto
282{
283 return [ip] (Addr addr) { return ip->extractTag(addr); };
284}
285
286}
287
288#endif //__CPU_PRED_BTB_ENTRY_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
const uint64_t tagMask
Definition btb_entry.hh:138
std::vector< ReplaceableEntry * > getPossibleEntries(const KeyType &key) const override
Find all possible entries for insertion and replacement of an address.
Definition btb_entry.hh:103
uint32_t extractSet(const KeyType &key) const
Extract the set index for the instruction PC based on tid.
Definition btb_entry.hh:91
Addr extractTag(const Addr addr) const override
Generate the tag from the given address.
Definition btb_entry.hh:125
BTBSetAssociative(const Params &p)
Definition btb_entry.hh:79
Addr regenerateAddr(const KeyType &key, const ReplaceableEntry *entry) const override
Definition btb_entry.hh:130
PARAMS(BTBSetAssociative)
void setNumThreads(unsigned num_threads)
Set number of threads sharing the BTB.
Definition btb_entry.hh:116
BTBIndexingPolicyParams Params
Definition btb_entry.hh:67
A common base class for indexing table locations.
Definition base.hh:73
typename Types::Params Params
Definition base.hh:76
A replaceable entry is a basic entry in a 2d table-like structure that needs to have replacement func...
virtual std::string print() const
Prints relevant information about this entry.
KeyType getTag() const
Get tag associated to this block.
Definition btb_entry.hh:219
StaticInstPtr inst
Pointer to the static branch inst at this address.
Definition btb_entry.hh:233
std::function< Addr(Addr)> TagExtractor
Definition btb_entry.hh:150
BTBEntry & operator=(const BTBEntry &other)
Assignment operator.
Definition btb_entry.hh:200
TagExtractor extractTag
Callback used to extract the tag from the entry.
Definition btb_entry.hh:258
KeyType tag
The entry's tag.
Definition btb_entry.hh:268
void insert(const KeyType &key)
Insert the block by assigning it a tag and marking it valid.
Definition btb_entry.hh:183
BTBEntry(TagExtractor ext)
Default constructor.
Definition btb_entry.hh:153
std::string print() const override
Prints relevant information about this entry.
Definition btb_entry.hh:236
void setTag(KeyType _tag)
Set tag associated to this block.
Definition btb_entry.hh:246
bool isValid() const
Checks if the entry is valid.
Definition btb_entry.hh:214
std::unique_ptr< PCStateBase > target
The entry's target.
Definition btb_entry.hh:230
void setValid()
Set valid bit.
Definition btb_entry.hh:250
void update(const PCStateBase &_target, StaticInstPtr _inst)
Update the target and instruction in the BTB entry.
Definition btb_entry.hh:161
void invalidate()
Invalidate the block.
Definition btb_entry.hh:223
bool match(const KeyType &key) const
Checks if the given tag information corresponds to this entry's.
Definition btb_entry.hh:172
BTBEntry(const BTBEntry &other)
Copy constructor.
Definition btb_entry.hh:190
STL vector class.
Definition stl.hh:37
static constexpr int log2i(int value)
Calculate the log2 of a power of 2 integer.
Definition intmath.hh:295
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
Declaration of a common framework for indexing policies.
Bitfield< 3, 0 > mask
Definition pcstate.hh:63
Bitfield< 12, 11 > set
Bitfield< 12 > ext
Bitfield< 0 > p
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
int16_t ThreadID
Thread index/ID type.
Definition types.hh:235
IndexingPolicyTemplate< BTBTagType > BTBIndexingPolicy
Definition btb_entry.hh:70
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
static constexpr auto genTagExtractor(BTBIndexingPolicy *ip)
This helper generates a tag extractor function object which will be typically used by Replaceable ent...
Definition btb_entry.hh:281
std::string csprintf(const char *format, const Args &...args)
Definition cprintf.hh:161
const Addr MaxAddr
Definition types.hh:171

Generated on Mon Jan 13 2025 04:28:32 for gem5 by doxygen 1.9.8