gem5 [DEVELOP-FOR-25.1]
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:
171 virtual ~RegClassOps() = default;
173 virtual std::string regName(const RegId &id) const;
175 virtual std::string valString(const void *val, const size_t& size) const;
177 virtual RegId
178 flatten(const BaseISA &isa, const RegId &id) const
179 {
180 return id;
181 }
182};
183
184class RegClassIterator;
185
187{
188 private:
190 const char *_name;
191
192 size_t _numRegs;
193 size_t _regBytes = sizeof(RegVal);
194 // This is how much to shift an index by to get an offset of a register in
195 // a register file from the register index, which would otherwise need to
196 // be calculated with a multiply.
197 size_t _regShift = ceilLog2(sizeof(RegVal));
198
199 static inline RegClassOps defaultOps;
202
203 bool _flat = true;
204
205 public:
206 constexpr RegClass(RegClassType type, const char *new_name,
207 size_t num_regs, const debug::Flag &debug_flag) :
208 _type(type), _name(new_name), _numRegs(num_regs), debugFlag(debug_flag)
209 {}
210
211 constexpr RegClass
213 {
214 RegClass reg_class = *this;
215 reg_class._flat = false;
216 return reg_class;
217 }
218
219 constexpr RegClass
220 ops(const RegClassOps &new_ops) const
221 {
222 RegClass reg_class = *this;
223 reg_class._ops = &new_ops;
224 return reg_class;
225 }
226
227 template <class RegType>
228 constexpr RegClass
229 regType() const
230 {
231 RegClass reg_class = *this;
232 reg_class._regBytes = sizeof(RegType);
233 reg_class._regShift = ceilLog2(reg_class._regBytes);
234 return reg_class;
235 }
236
237 constexpr RegClassType type() const { return _type; }
238 constexpr const char *name() const { return _name; }
239 constexpr size_t numRegs() const { return _numRegs; }
240 constexpr size_t regBytes() const { return _regBytes; }
241 constexpr size_t regShift() const { return _regShift; }
242 constexpr const debug::Flag &debug() const { return debugFlag; }
243 constexpr bool isFlat() const { return _flat; }
244
245 std::string regName(const RegId &id) const { return _ops->regName(id); }
246 std::string
247 valString(const void *val) const
248 {
249 return _ops->valString(val, regBytes());
250 }
251 std::string
252 valString(const void *val, const size_t& num_bytes) const
253 {
254 return _ops->valString(val, std::min(regBytes(), num_bytes));
255 }
256 RegId
257 flatten(const BaseISA &isa, const RegId &id) const
258 {
259 return isFlat() ? id : _ops->flatten(isa, id);
260 }
261
263
264 inline iterator begin() const;
265 inline iterator end() const;
266
267 inline constexpr RegId operator[](RegIndex idx) const;
268};
269
270inline constexpr RegClass
271 invalidRegClass(InvalidRegClass, "invalid", 0, debug::InvalidReg);
272
274
275constexpr bool
276RegId::is(RegClassType reg_class) const
277{
278 return _regClass->type() == reg_class;
279}
280
281constexpr RegClassType RegId::classValue() const { return _regClass->type(); }
282constexpr const char* RegId::className() const { return _regClass->name(); }
283
284constexpr bool RegId::isFlat() const { return _regClass->isFlat(); }
285RegId
286RegId::flatten(const BaseISA &isa) const
287{
288 return _regClass->flatten(isa, *this);
289}
290
291std::ostream&
292operator<<(std::ostream& os, const RegId& rid)
293{
294 return os << rid.regClass().regName(rid);
295}
296
298{
299 private:
301
302 RegClassIterator(const RegClass &reg_class, RegIndex idx) :
303 id(reg_class, idx)
304 {}
305
306 friend class RegClass;
307
308 public:
309 using iterator_category = std::forward_iterator_tag;
310 using difference_type = std::size_t;
311 using value_type = const RegId;
314
315 reference operator*() const { return id; }
316 pointer operator->() { return &id; }
317
320 {
321 id.regIdx++;
322 return *this;
323 }
324
327 {
328 auto tmp = *this;
329 ++(*this);
330 return tmp;
331 }
332
333 bool
334 operator==(const RegClassIterator &other) const
335 {
336 return id == other.id;
337 }
338
339 bool
340 operator!=(const RegClassIterator &other) const
341 {
342 return id != other.id;
343 }
344};
345
346RegClassIterator
348{
349 return RegClassIterator(*this, 0);
350}
351
354{
355 return RegClassIterator(*this, numRegs());
356}
357
358constexpr RegId
360{
361 return RegId(*this, idx);
362}
363
364// Type matching for gem5::VecRegContainer class
365// This is used in TypedRegClassOps.
366template<typename>
367struct is_vec_reg_container : std::false_type {};
368template<std::size_t SIZE>
369struct is_vec_reg_container<gem5::VecRegContainer<SIZE>> : std::true_type {};
370
371template <typename ValueType>
373{
374 public:
375 std::string
376 valString(const void *val, const size_t& size) const override
377 {
379 if (size == sizeof(ValueType)) {
380 return csprintf("%s", *(const ValueType *)val);
381 } else {
382 return ((const ValueType *)val)->getString(size);
383 }
384 } else {
385 assert(size == sizeof(ValueType));
386 return csprintf("%s", *(const ValueType *)val);
387 }
388 }
389};
390
391template <typename ValueType>
392class VecElemRegClassOps : public TypedRegClassOps<ValueType>
393{
394 protected:
396
397 public:
398 explicit VecElemRegClassOps(size_t elems_per_vec) :
399 elemsPerVec(elems_per_vec)
400 {}
401
402 std::string
403 regName(const RegId &id) const override
404 {
405 RegIndex reg_idx = id.index() / elemsPerVec;
406 RegIndex elem_idx = id.index() % elemsPerVec;
407 return csprintf("v%d[%d]", reg_idx, elem_idx);
408 }
409};
410
415class PhysRegId : private RegId
416{
417 private:
420 bool pinned;
421
422 public:
423 explicit PhysRegId() : RegId(invalidRegClass, -1), flatIdx(-1),
425 {}
426
428 explicit PhysRegId(const RegClass &reg_class, RegIndex _regIdx,
429 RegIndex _flatIdx)
430 : RegId(reg_class, _regIdx), flatIdx(_flatIdx),
432 {}
433
436 using RegId::index;
437 using RegId::regClass;
438 using RegId::classValue;
439 using RegId::className;
440 using RegId::is;
447 bool
448 operator<(const PhysRegId& that) const
449 {
450 return RegId::operator<(that);
451 }
452
453 bool
454 operator==(const PhysRegId& that) const
455 {
456 return RegId::operator==(that);
457 }
458
459 bool
460 operator!=(const PhysRegId& that) const
461 {
462 return RegId::operator!=(that);
463 }
464
465
470 bool isFixedMapping() const { return !isRenameable(); }
471
473 const RegIndex& flatIndex() const { return flatIdx; }
474
475 int getNumPinnedWrites() const { return numPinnedWrites; }
476
477 void
478 setNumPinnedWrites(int numWrites)
479 {
480 // An instruction with a pinned destination reg can get
481 // squashed. The numPinnedWrites counter may be zero when
482 // the squash happens but we need to know if the dest reg
483 // was pinned originally in order to reset counters properly
484 // for a possible re-rename using the same physical reg (which
485 // may be required in case of a mem access order violation).
486 pinned = (numWrites != 0);
487 numPinnedWrites = numWrites;
488 }
489
492
493 bool isPinned() const { return pinned; }
494
495 int
500
501 void
503 {
504 numPinnedWritesToComplete = numWrites;
505 }
506
509};
510
512
513} // namespace gem5
514
515namespace std
516{
517template<>
518struct hash<gem5::RegId>
519{
520 size_t
521 operator()(const gem5::RegId& reg_id) const
522 {
523 // Extract unique integral values for the effective fields of a RegId.
524 const size_t index = static_cast<size_t>(reg_id.index());
525 const size_t class_num = static_cast<size_t>(reg_id.classValue());
526
527 const size_t shifted_class_num =
528 class_num << (sizeof(gem5::RegIndex) << 3);
529
530 // Concatenate the class_num to the end of the flat_index, in order to
531 // maximize information retained.
532 const size_t concatenated_hash = index | shifted_class_num;
533
534 // If RegIndex is larger than size_t, then class_num will not be
535 // considered by this hash function, so we may wish to perform a
536 // different operation to include that information in the hash.
537 static_assert(sizeof(gem5::RegIndex) < sizeof(size_t),
538 "sizeof(RegIndex) should be less than sizeof(size_t)");
539
540 return concatenated_hash;
541 }
542};
543} // namespace std
544
545#endif // __CPU__REG_CLASS_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Physical register ID.
Definition reg_class.hh:416
PhysRegId(const RegClass &reg_class, RegIndex _regIdx, RegIndex _flatIdx)
Scalar PhysRegId constructor.
Definition reg_class.hh:428
int getNumPinnedWrites() const
Definition reg_class.hh:475
int numPinnedWritesToComplete
Definition reg_class.hh:419
bool operator<(const PhysRegId &that) const
Explicit forward methods, to prevent comparisons of PhysRegId with RegIds.
Definition reg_class.hh:448
bool operator!=(const PhysRegId &that) const
Definition reg_class.hh:460
const RegIndex & flatIndex() const
Flat index accessor.
Definition reg_class.hh:473
int getNumPinnedWritesToComplete() const
Definition reg_class.hh:496
bool operator==(const PhysRegId &that) const
Definition reg_class.hh:454
void incrNumPinnedWritesToComplete()
Definition reg_class.hh:508
void decrNumPinnedWrites()
Definition reg_class.hh:490
void setNumPinnedWrites(int numWrites)
Definition reg_class.hh:478
void incrNumPinnedWrites()
Definition reg_class.hh:491
void decrNumPinnedWritesToComplete()
Definition reg_class.hh:507
void setNumPinnedWritesToComplete(int numWrites)
Definition reg_class.hh:502
bool isPinned() const
Definition reg_class.hh:493
RegIndex flatIdx
Definition reg_class.hh:418
bool isFixedMapping() const
Returns true if this register is always associated to the same architectural register.
Definition reg_class.hh:470
std::forward_iterator_tag iterator_category
Definition reg_class.hh:309
RegClassIterator & operator++()
Definition reg_class.hh:319
RegClassIterator operator++(int)
Definition reg_class.hh:326
value_type & reference
Definition reg_class.hh:313
friend class RegClass
Definition reg_class.hh:306
std::size_t difference_type
Definition reg_class.hh:310
bool operator!=(const RegClassIterator &other) const
Definition reg_class.hh:340
reference operator*() const
Definition reg_class.hh:315
bool operator==(const RegClassIterator &other) const
Definition reg_class.hh:334
RegClassIterator(const RegClass &reg_class, RegIndex idx)
Definition reg_class.hh:302
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:178
virtual ~RegClassOps()=default
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:257
RegClassIterator iterator
Definition reg_class.hh:262
constexpr RegId operator[](RegIndex idx) const
Definition reg_class.hh:359
constexpr RegClass(RegClassType type, const char *new_name, size_t num_regs, const debug::Flag &debug_flag)
Definition reg_class.hh:206
constexpr RegClass ops(const RegClassOps &new_ops) const
Definition reg_class.hh:220
iterator begin() const
Definition reg_class.hh:347
constexpr size_t regShift() const
Definition reg_class.hh:241
std::string regName(const RegId &id) const
Definition reg_class.hh:245
constexpr size_t numRegs() const
Definition reg_class.hh:239
constexpr RegClass regType() const
Definition reg_class.hh:229
constexpr RegClass needsFlattening() const
Definition reg_class.hh:212
constexpr bool isFlat() const
Definition reg_class.hh:243
static RegClassOps defaultOps
Definition reg_class.hh:199
iterator end() const
Definition reg_class.hh:353
constexpr const char * name() const
Definition reg_class.hh:238
constexpr RegClassType type() const
Definition reg_class.hh:237
constexpr const debug::Flag & debug() const
Definition reg_class.hh:242
std::string valString(const void *val) const
Definition reg_class.hh:247
std::string valString(const void *val, const size_t &num_bytes) const
Definition reg_class.hh:252
const debug::Flag & debugFlag
Definition reg_class.hh:201
RegClassType _type
Definition reg_class.hh:189
const char * _name
Definition reg_class.hh:190
const RegClassOps * _ops
Definition reg_class.hh:200
constexpr size_t regBytes() const
Definition reg_class.hh:240
Register ID: describe an architectural register with its class and index.
Definition reg_class.hh:94
constexpr RegId()
Definition reg_class.hh:273
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:281
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:276
constexpr const RegClass & regClass() const
Class accessor.
Definition reg_class.hh:154
friend class RegClassIterator
Definition reg_class.hh:101
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:284
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:286
constexpr const char * className() const
Return a const char* with the register class name.
Definition reg_class.hh:282
friend std::ostream & operator<<(std::ostream &os, const RegId &rid)
Definition reg_class.hh:292
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:376
std::string regName(const RegId &id) const override
Print the name of the register specified in id.
Definition reg_class.hh:403
VecElemRegClassOps(size_t elems_per_vec)
Definition reg_class.hh:398
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 Arm Limited 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
PhysRegId * PhysRegIdPtr
Definition reg_class.hh:511
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:521
Vector Registers layout specification.

Generated on Mon Oct 27 2025 04:13:01 for gem5 by doxygen 1.14.0