gem5 v24.0.0.0
Loading...
Searching...
No Matches
reg_class.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016-2019 ARM Limited
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) 2013 Advanced Micro Devices, Inc.
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
41#ifndef __CPU__REG_CLASS_HH__
42#define __CPU__REG_CLASS_HH__
43
44#include <cstddef>
45#include <iterator>
46#include <string>
47
49#include "base/cprintf.hh"
50#include "base/debug.hh"
51#include "base/intmath.hh"
52#include "base/types.hh"
53#include "debug/InvalidReg.hh"
54
55namespace gem5
56{
57
73
74// "Standard" register class names. Using these is encouraged but optional.
75inline constexpr char IntRegClassName[] = "integer";
76inline constexpr char FloatRegClassName[] = "floating_point";
77inline constexpr char VecRegClassName[] = "vector";
78inline constexpr char VecElemClassName[] = "vector_element";
79inline constexpr char VecPredRegClassName[] = "vector_predicate";
80inline constexpr char MatRegClassName[] = "matrix";
81inline constexpr char CCRegClassName[] = "condition_code";
82inline constexpr char MiscRegClassName[] = "miscellaneous";
83
84class RegClass;
86class BaseISA;
87
93class RegId
94{
95 protected:
96 const RegClass *_regClass = nullptr;
99
100 friend struct std::hash<RegId>;
101 friend class RegClassIterator;
102
103 public:
104 inline constexpr RegId();
105
106 constexpr RegId(const RegClass &reg_class, RegIndex reg_idx)
107 : _regClass(&reg_class), regIdx(reg_idx), numPinnedWrites(0)
108 {}
109
110 constexpr operator RegIndex() const
111 {
112 return index();
113 }
114
115 constexpr bool
116 operator==(const RegId& that) const
117 {
118 return classValue() == that.classValue() && regIdx == that.index();
119 }
120
121 constexpr bool
122 operator!=(const RegId& that) const
123 {
124 return !(*this==that);
125 }
126
130 constexpr bool
131 operator<(const RegId& that) const
132 {
133 return classValue() < that.classValue() ||
134 (classValue() == that.classValue() && (regIdx < that.index()));
135 }
136
140 constexpr bool
142 {
144 }
145
147 inline constexpr bool is(RegClassType reg_class) const;
148
151 constexpr RegIndex index() const { return regIdx; }
152
154 constexpr const RegClass &regClass() const { return *_regClass; }
155 inline constexpr RegClassType classValue() const;
157 inline constexpr const char* className() const;
158
159 inline constexpr bool isFlat() const;
160 inline RegId flatten(const BaseISA &isa) const;
161
162 int getNumPinnedWrites() const { return numPinnedWrites; }
163 void setNumPinnedWrites(int num_writes) { numPinnedWrites = num_writes; }
164
165 friend inline std::ostream& operator<<(std::ostream& os, const RegId& rid);
166};
167
169{
170 public:
172 virtual std::string regName(const RegId &id) const;
174 virtual std::string valString(const void *val, const size_t& size) const;
176 virtual RegId
177 flatten(const BaseISA &isa, const RegId &id) const
178 {
179 return id;
180 }
181};
182
183class RegClassIterator;
184
186{
187 private:
189 const char *_name;
190
191 size_t _numRegs;
192 size_t _regBytes = sizeof(RegVal);
193 // This is how much to shift an index by to get an offset of a register in
194 // a register file from the register index, which would otherwise need to
195 // be calculated with a multiply.
196 size_t _regShift = ceilLog2(sizeof(RegVal));
197
198 static inline RegClassOps defaultOps;
201
202 bool _flat = true;
203
204 public:
205 constexpr RegClass(RegClassType type, const char *new_name,
206 size_t num_regs, const debug::Flag &debug_flag) :
207 _type(type), _name(new_name), _numRegs(num_regs), debugFlag(debug_flag)
208 {}
209
210 constexpr RegClass
212 {
213 RegClass reg_class = *this;
214 reg_class._flat = false;
215 return reg_class;
216 }
217
218 constexpr RegClass
219 ops(const RegClassOps &new_ops) const
220 {
221 RegClass reg_class = *this;
222 reg_class._ops = &new_ops;
223 return reg_class;
224 }
225
226 template <class RegType>
227 constexpr RegClass
228 regType() const
229 {
230 RegClass reg_class = *this;
231 reg_class._regBytes = sizeof(RegType);
232 reg_class._regShift = ceilLog2(reg_class._regBytes);
233 return reg_class;
234 }
235
236 constexpr RegClassType type() const { return _type; }
237 constexpr const char *name() const { return _name; }
238 constexpr size_t numRegs() const { return _numRegs; }
239 constexpr size_t regBytes() const { return _regBytes; }
240 constexpr size_t regShift() const { return _regShift; }
241 constexpr const debug::Flag &debug() const { return debugFlag; }
242 constexpr bool isFlat() const { return _flat; }
243
244 std::string regName(const RegId &id) const { return _ops->regName(id); }
245 std::string
246 valString(const void *val) const
247 {
248 return _ops->valString(val, regBytes());
249 }
250 std::string
251 valString(const void *val, const uint64_t& num_bytes) const
252 {
253 return _ops->valString(val, std::min(regBytes(), num_bytes));
254 }
255 RegId
256 flatten(const BaseISA &isa, const RegId &id) const
257 {
258 return isFlat() ? id : _ops->flatten(isa, id);
259 }
260
262
263 inline iterator begin() const;
264 inline iterator end() const;
265
266 inline constexpr RegId operator[](RegIndex idx) const;
267};
268
269inline constexpr RegClass
270 invalidRegClass(InvalidRegClass, "invalid", 0, debug::InvalidReg);
271
273
274constexpr bool
275RegId::is(RegClassType reg_class) const
276{
277 return _regClass->type() == reg_class;
278}
279
280constexpr RegClassType RegId::classValue() const { return _regClass->type(); }
281constexpr const char* RegId::className() const { return _regClass->name(); }
282
283constexpr bool RegId::isFlat() const { return _regClass->isFlat(); }
284RegId
285RegId::flatten(const BaseISA &isa) const
286{
287 return _regClass->flatten(isa, *this);
288}
289
290std::ostream&
291operator<<(std::ostream& os, const RegId& rid)
292{
293 return os << rid.regClass().regName(rid);
294}
295
297{
298 private:
300
301 RegClassIterator(const RegClass &reg_class, RegIndex idx) :
302 id(reg_class, idx)
303 {}
304
305 friend class RegClass;
306
307 public:
308 using iterator_category = std::forward_iterator_tag;
309 using difference_type = std::size_t;
310 using value_type = const RegId;
313
314 reference operator*() const { return id; }
315 pointer operator->() { return &id; }
316
319 {
320 id.regIdx++;
321 return *this;
322 }
323
326 {
327 auto tmp = *this;
328 ++(*this);
329 return tmp;
330 }
331
332 bool
333 operator==(const RegClassIterator &other) const
334 {
335 return id == other.id;
336 }
337
338 bool
339 operator!=(const RegClassIterator &other) const
340 {
341 return id != other.id;
342 }
343};
344
345RegClassIterator
347{
348 return RegClassIterator(*this, 0);
349}
350
353{
354 return RegClassIterator(*this, numRegs());
355}
356
357constexpr RegId
359{
360 return RegId(*this, idx);
361}
362
363// Type matching for gem5::VecRegContainer class
364// This is used in TypedRegClassOps.
365template<typename>
366struct is_vec_reg_container : std::false_type {};
367template<std::size_t SIZE>
368struct is_vec_reg_container<gem5::VecRegContainer<SIZE>> : std::true_type {};
369
370template <typename ValueType>
372{
373 public:
374 std::string
375 valString(const void *val, const size_t& size) const override
376 {
378 if (size == sizeof(ValueType)) {
379 return csprintf("%s", *(const ValueType *)val);
380 } else {
381 return ((const ValueType *)val)->getString(size);
382 }
383 } else {
384 assert(size == sizeof(ValueType));
385 return csprintf("%s", *(const ValueType *)val);
386 }
387 }
388};
389
390template <typename ValueType>
391class VecElemRegClassOps : public TypedRegClassOps<ValueType>
392{
393 protected:
395
396 public:
397 explicit VecElemRegClassOps(size_t elems_per_vec) :
398 elemsPerVec(elems_per_vec)
399 {}
400
401 std::string
402 regName(const RegId &id) const override
403 {
404 RegIndex reg_idx = id.index() / elemsPerVec;
405 RegIndex elem_idx = id.index() % elemsPerVec;
406 return csprintf("v%d[%d]", reg_idx, elem_idx);
407 }
408};
409
414class PhysRegId : private RegId
415{
416 private:
419 bool pinned;
420
421 public:
422 explicit PhysRegId() : RegId(invalidRegClass, -1), flatIdx(-1),
424 {}
425
427 explicit PhysRegId(const RegClass &reg_class, RegIndex _regIdx,
428 RegIndex _flatIdx)
429 : RegId(reg_class, _regIdx), flatIdx(_flatIdx),
431 {}
432
435 using RegId::index;
436 using RegId::regClass;
437 using RegId::classValue;
438 using RegId::className;
439 using RegId::is;
446 bool
447 operator<(const PhysRegId& that) const
448 {
449 return RegId::operator<(that);
450 }
451
452 bool
453 operator==(const PhysRegId& that) const
454 {
455 return RegId::operator==(that);
456 }
457
458 bool
459 operator!=(const PhysRegId& that) const
460 {
461 return RegId::operator!=(that);
462 }
469 bool isFixedMapping() const { return !isRenameable(); }
470
472 const RegIndex& flatIndex() const { return flatIdx; }
473
474 int getNumPinnedWrites() const { return numPinnedWrites; }
475
476 void
477 setNumPinnedWrites(int numWrites)
478 {
479 // An instruction with a pinned destination reg can get
480 // squashed. The numPinnedWrites counter may be zero when
481 // the squash happens but we need to know if the dest reg
482 // was pinned originally in order to reset counters properly
483 // for a possible re-rename using the same physical reg (which
484 // may be required in case of a mem access order violation).
485 pinned = (numWrites != 0);
486 numPinnedWrites = numWrites;
487 }
488
491
492 bool isPinned() const { return pinned; }
493
494 int
499
500 void
502 {
503 numPinnedWritesToComplete = numWrites;
504 }
505
508};
509
511
512} // namespace gem5
513
514namespace std
515{
516template<>
517struct hash<gem5::RegId>
518{
519 size_t
520 operator()(const gem5::RegId& reg_id) const
521 {
522 // Extract unique integral values for the effective fields of a RegId.
523 const size_t index = static_cast<size_t>(reg_id.index());
524 const size_t class_num = static_cast<size_t>(reg_id.classValue());
525
526 const size_t shifted_class_num =
527 class_num << (sizeof(gem5::RegIndex) << 3);
528
529 // Concatenate the class_num to the end of the flat_index, in order to
530 // maximize information retained.
531 const size_t concatenated_hash = index | shifted_class_num;
532
533 // If RegIndex is larger than size_t, then class_num will not be
534 // considered by this hash function, so we may wish to perform a
535 // different operation to include that information in the hash.
536 static_assert(sizeof(gem5::RegIndex) < sizeof(size_t),
537 "sizeof(RegIndex) should be less than sizeof(size_t)");
538
539 return concatenated_hash;
540 }
541};
542} // namespace std
543
544#endif // __CPU__REG_CLASS_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Physical register ID.
Definition reg_class.hh:415
PhysRegId(const RegClass &reg_class, RegIndex _regIdx, RegIndex _flatIdx)
Scalar PhysRegId constructor.
Definition reg_class.hh:427
int getNumPinnedWrites() const
Definition reg_class.hh:474
int numPinnedWritesToComplete
Definition reg_class.hh:418
bool operator<(const PhysRegId &that) const
Explicit forward methods, to prevent comparisons of PhysRegId with RegIds.
Definition reg_class.hh:447
bool operator!=(const PhysRegId &that) const
Definition reg_class.hh:459
const RegIndex & flatIndex() const
Flat index accessor.
Definition reg_class.hh:472
int getNumPinnedWritesToComplete() const
Definition reg_class.hh:495
bool operator==(const PhysRegId &that) const
Definition reg_class.hh:453
void incrNumPinnedWritesToComplete()
Definition reg_class.hh:507
void decrNumPinnedWrites()
Definition reg_class.hh:489
void setNumPinnedWrites(int numWrites)
Definition reg_class.hh:477
void incrNumPinnedWrites()
Definition reg_class.hh:490
void decrNumPinnedWritesToComplete()
Definition reg_class.hh:506
void setNumPinnedWritesToComplete(int numWrites)
Definition reg_class.hh:501
bool isPinned() const
Definition reg_class.hh:492
RegIndex flatIdx
Definition reg_class.hh:417
bool isFixedMapping() const
Returns true if this register is always associated to the same architectural register.
Definition reg_class.hh:469
std::forward_iterator_tag iterator_category
Definition reg_class.hh:308
RegClassIterator & operator++()
Definition reg_class.hh:318
RegClassIterator operator++(int)
Definition reg_class.hh:325
std::size_t difference_type
Definition reg_class.hh:309
bool operator!=(const RegClassIterator &other) const
Definition reg_class.hh:339
reference operator*() const
Definition reg_class.hh:314
bool operator==(const RegClassIterator &other) const
Definition reg_class.hh:333
RegClassIterator(const RegClass &reg_class, RegIndex idx)
Definition reg_class.hh:301
virtual std::string regName(const RegId &id) const
Print the name of the register specified in id.
Definition reg_class.cc:53
virtual RegId flatten(const BaseISA &isa, const RegId &id) const
Flatten register id id using information in the ISA object isa.
Definition reg_class.hh:177
virtual std::string valString(const void *val, const size_t &size) const
Print the value of a register pointed to by val of size size.
Definition reg_class.cc:59
RegId flatten(const BaseISA &isa, const RegId &id) const
Definition reg_class.hh:256
constexpr RegId operator[](RegIndex idx) const
Definition reg_class.hh:358
constexpr RegClass(RegClassType type, const char *new_name, size_t num_regs, const debug::Flag &debug_flag)
Definition reg_class.hh:205
constexpr RegClass ops(const RegClassOps &new_ops) const
Definition reg_class.hh:219
iterator begin() const
Definition reg_class.hh:346
constexpr size_t regShift() const
Definition reg_class.hh:240
std::string regName(const RegId &id) const
Definition reg_class.hh:244
std::string valString(const void *val, const uint64_t &num_bytes) const
Definition reg_class.hh:251
constexpr size_t numRegs() const
Definition reg_class.hh:238
constexpr RegClass regType() const
Definition reg_class.hh:228
constexpr RegClass needsFlattening() const
Definition reg_class.hh:211
constexpr bool isFlat() const
Definition reg_class.hh:242
static RegClassOps defaultOps
Definition reg_class.hh:198
iterator end() const
Definition reg_class.hh:352
constexpr const char * name() const
Definition reg_class.hh:237
constexpr RegClassType type() const
Definition reg_class.hh:236
constexpr const debug::Flag & debug() const
Definition reg_class.hh:241
std::string valString(const void *val) const
Definition reg_class.hh:246
const debug::Flag & debugFlag
Definition reg_class.hh:200
RegClassType _type
Definition reg_class.hh:188
const char * _name
Definition reg_class.hh:189
const RegClassOps * _ops
Definition reg_class.hh:199
constexpr size_t regBytes() const
Definition reg_class.hh:239
Register ID: describe an architectural register with its class and index.
Definition reg_class.hh:94
constexpr RegId()
Definition reg_class.hh:272
constexpr bool isRenameable() const
Return true if this register can be renamed.
Definition reg_class.hh:141
constexpr bool operator<(const RegId &that) const
Order operator.
Definition reg_class.hh:131
constexpr RegClassType classValue() const
Definition reg_class.hh:280
constexpr bool operator==(const RegId &that) const
Definition reg_class.hh:116
constexpr bool operator!=(const RegId &that) const
Definition reg_class.hh:122
constexpr bool is(RegClassType reg_class) const
Definition reg_class.hh:275
constexpr const RegClass & regClass() const
Class accessor.
Definition reg_class.hh:154
constexpr RegId(const RegClass &reg_class, RegIndex reg_idx)
Definition reg_class.hh:106
void setNumPinnedWrites(int num_writes)
Definition reg_class.hh:163
constexpr RegIndex index() const
Index accessors.
Definition reg_class.hh:151
int getNumPinnedWrites() const
Definition reg_class.hh:162
constexpr bool isFlat() const
Definition reg_class.hh:283
const RegClass * _regClass
Definition reg_class.hh:96
int numPinnedWrites
Definition reg_class.hh:98
RegIndex regIdx
Definition reg_class.hh:97
RegId flatten(const BaseISA &isa) const
Definition reg_class.hh:285
constexpr const char * className() const
Return a const char* with the register class name.
Definition reg_class.hh:281
friend std::ostream & operator<<(std::ostream &os, const RegId &rid)
Definition reg_class.hh:291
std::string valString(const void *val, const size_t &size) const override
Print the value of a register pointed to by val of size size.
Definition reg_class.hh:375
std::string regName(const RegId &id) const override
Print the name of the register specified in id.
Definition reg_class.hh:402
VecElemRegClassOps(size_t elems_per_vec)
Definition reg_class.hh:397
Vector Register Abstraction This generic class is the model in a particularization of MVC,...
Definition vec_reg.hh:126
static constexpr int ceilLog2(const T &n)
Definition intmath.hh:84
Bitfield< 33 > id
Bitfield< 24, 22 > is
Bitfield< 17 > os
Definition misc.hh:838
Bitfield< 63 > val
Definition misc.hh:804
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
uint16_t RegIndex
Definition types.hh:176
constexpr char MiscRegClassName[]
Definition reg_class.hh:82
uint64_t RegVal
Definition types.hh:173
constexpr char CCRegClassName[]
Definition reg_class.hh:81
constexpr char VecPredRegClassName[]
Definition reg_class.hh:79
constexpr char IntRegClassName[]
Definition reg_class.hh:75
constexpr char VecRegClassName[]
Definition reg_class.hh:77
static std::ostream & operator<<(std::ostream &os, const DummyMatRegContainer &d)
Definition matrix.hh:564
constexpr RegClass invalidRegClass(InvalidRegClass, "invalid", 0, debug::InvalidReg)
std::string csprintf(const char *format, const Args &...args)
Definition cprintf.hh:161
constexpr char MatRegClassName[]
Definition reg_class.hh:80
RegClassType
Enumerate the classes of registers.
Definition reg_class.hh:60
@ VecPredRegClass
Definition reg_class.hh:67
@ MatRegClass
Matrix Register.
Definition reg_class.hh:68
@ FloatRegClass
Floating-point register.
Definition reg_class.hh:62
@ CCRegClass
Condition-code register.
Definition reg_class.hh:69
@ VecRegClass
Vector Register.
Definition reg_class.hh:64
@ IntRegClass
Integer register.
Definition reg_class.hh:61
@ InvalidRegClass
Definition reg_class.hh:71
@ MiscRegClass
Control (misc) register.
Definition reg_class.hh:70
@ VecElemClass
Vector Register Native Elem lane.
Definition reg_class.hh:66
constexpr char VecElemClassName[]
Definition reg_class.hh:78
constexpr char FloatRegClassName[]
Definition reg_class.hh:76
Overload hash function for BasicBlockRange type.
Definition binary32.hh:81
size_t operator()(const gem5::RegId &reg_id) const
Definition reg_class.hh:520
Vector Registers layout specification.

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