LLVM 24.0.0git
MCRegisterInfo.h
Go to the documentation of this file.
1//===- MC/MCRegisterInfo.h - Target Register Description --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file describes an abstract interface used to get information about a
10// target machines register file. This information is used for a variety of
11// purposed, especially register allocation.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_MC_MCREGISTERINFO_H
16#define LLVM_MC_MCREGISTERINFO_H
17
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/Sequence.h"
20#include "llvm/ADT/iterator.h"
22#include "llvm/MC/LaneBitmask.h"
23#include "llvm/MC/MCRegister.h"
25#include <cassert>
26#include <cstdint>
27#include <iterator>
28#include <utility>
29
30namespace llvm {
31
35
36/// MCRegisterClass - Base class of TargetRegisterClass.
38public:
39 using iterator = const MCPhysReg*;
40 using const_iterator = const MCPhysReg*;
41
42 // TODO: reorder fields to reduce memory usage.
43 const uint32_t RegsOff; ///< Relative offset to MCPhysReg array.
44 const uint32_t RegSetOff; ///< Relative offset to uint8_t array.
48 /// Register denoted by first bit in RegSet.
51 const uint16_t ID;
53 const bool Allocatable;
54 const bool BaseClass;
55
56 const uint32_t SubClassMaskOff; ///< Relative offset to uint32_t array.
57 const uint32_t SuperRegIndicesOff; ///< Relative offset to MCPhysReg array.
59 /// Classes with a higher priority value are assigned first by register
60 /// allocators using a greedy heuristic. The value is in the range [0,31].
62
63 // Change allocation priority heuristic used by greedy.
64 const bool GlobalPriority;
65
66 /// Configurable target specific flags.
69 /// Whether the class supports two (or more) disjunct subregister indices.
71 /// Whether a combination of subregisters can cover every register in the
72 /// class. See also the CoveredBySubRegs description in Target.td.
73 const bool CoveredBySubRegs;
74 const uint32_t SuperClassesOff; ///< Relative offset to unsigned array.
76
77 /// getID() - Return the register class ID number.
78 ///
79 unsigned getID() const { return ID; }
80
81 /// begin/end - Return all of the registers in this class.
82 ///
83 iterator begin() const {
84 return reinterpret_cast<iterator>(reinterpret_cast<const char *>(this) +
85 RegsOff);
86 }
87 iterator end() const { return begin() + RegsSize; }
88
89 /// getNumRegs - Return the number of registers in this class.
90 ///
91 unsigned getNumRegs() const { return RegsSize; }
92
93 /// getRegister - Return the specified register in the class.
94 ///
95 MCRegister getRegister(unsigned i) const {
96 assert(i < getNumRegs() && "Register number out of range!");
97 return begin()[i];
98 }
99
101 return ArrayRef(begin(), RegsSize);
102 }
103
104 /// contains - Return true if the specified register is included in this
105 /// register class. This does not include virtual registers.
106 bool contains(MCRegister Reg) const {
107 unsigned RegSetIdx = Reg.id() - RegSetBegin;
108 if (RegSetIdx >= RegSetSize)
109 return false;
110 unsigned InByte = RegSetIdx % 8;
111 unsigned Byte = RegSetIdx / 8;
112 const uint8_t *RegSet = reinterpret_cast<const uint8_t *>(this) + RegSetOff;
113 return (RegSet[Byte] & (1 << InByte)) != 0;
114 }
115
116 /// contains - Return true if both registers are in this class.
117 bool contains(MCRegister Reg1, MCRegister Reg2) const {
118 return contains(Reg1) && contains(Reg2);
119 }
120
121 /// Return the size of the physical register in bits if we are able to
122 /// determine it. This always returns zero for registers of targets that use
123 /// HW modes, as we need more information to determine the size of registers
124 /// in such cases. Use TargetRegisterInfo to cover them.
125 unsigned getSizeInBits() const { return RegSizeInBits; }
126
127 /// getCopyCost - Return the cost of copying a value between two registers in
128 /// this class. A negative number means the register class is very expensive
129 /// to copy e.g. status flag register classes.
130 uint8_t getCopyCost() const { return CopyCost; }
131
132 /// \return true if register class is very expensive to copy e.g. status flag
133 /// register classes.
135 return CopyCost == std::numeric_limits<uint8_t>::max();
136 }
137
138 /// isAllocatable - Return true if this register class may be used to create
139 /// virtual registers.
140 bool isAllocatable() const { return Allocatable; }
141
142 /// Return true if this register class has a defined BaseClassOrder.
143 bool isBaseClass() const { return BaseClass; }
144
145 /// Return true if the specified TargetRegisterClass
146 /// is a proper sub-class of this TargetRegisterClass.
147 bool hasSubClass(const MCRegisterClass *RC) const {
148 return RC != this && hasSubClassEq(RC);
149 }
150
151 /// Returns true if RC is a sub-class of or equal to this class.
152 bool hasSubClassEq(const MCRegisterClass *RC) const {
153 unsigned ID = RC->getID();
154 return (getSubClassMask()[ID / 32] >> (ID % 32)) & 1;
155 }
156
157 /// Return true if the specified MCRegisterClass is a
158 /// proper super-class of this MCRegisterClass.
159 bool hasSuperClass(const MCRegisterClass *RC) const {
160 return RC->hasSubClass(this);
161 }
162
163 /// Returns true if RC is a super-class of or equal to this class.
164 bool hasSuperClassEq(const MCRegisterClass *RC) const {
165 return RC->hasSubClassEq(this);
166 }
167
168 /// Returns a bit vector of subclasses, including this one.
169 /// The vector is indexed by class IDs.
170 ///
171 /// To use it, consider the returned array as a chunk of memory that
172 /// contains an array of bits of size NumRegClasses. Each 32-bit chunk
173 /// contains a bitset of the ID of the subclasses in big-endian style.
174
175 /// I.e., the representation of the memory from left to right at the
176 /// bit level looks like:
177 /// [31 30 ... 1 0] [ 63 62 ... 33 32] ...
178 /// [ XXX NumRegClasses NumRegClasses - 1 ... ]
179 /// Where the number represents the class ID and XXX bits that
180 /// should be ignored.
181 ///
182 /// See the implementation of hasSubClassEq for an example of how it
183 /// can be used.
184 const uint32_t *getSubClassMask() const {
185 return reinterpret_cast<const uint32_t *>(
186 reinterpret_cast<const char *>(this) + SubClassMaskOff);
187 }
188
189 /// Returns a 0-terminated list of sub-register indices that project some
190 /// super-register class into this register class. The list has an entry for
191 /// each Idx such that:
192 ///
193 /// There exists SuperRC where:
194 /// For all Reg in SuperRC:
195 /// this->contains(Reg:Idx)
197 return reinterpret_cast<const uint16_t *>(
198 reinterpret_cast<const char *>(this) + SuperRegIndicesOff);
199 }
200
201 /// Returns a list of super-classes. The
202 /// classes are ordered by ID which is also a topological ordering from large
203 /// to small classes. The list does NOT include the current class.
205 const unsigned *SuperClasses = reinterpret_cast<const unsigned *>(
206 reinterpret_cast<const char *>(this) + SuperClassesOff);
207 return ArrayRef(SuperClasses, SuperClassesSize);
208 }
209
210 /// Returns the combination of all lane masks of register in this class.
211 /// The lane masks of the registers are the combination of all lane masks
212 /// of their subregisters. Returns 1 if there are no subregisters.
213 LaneBitmask getLaneMask() const { return LaneMask; }
214};
215
216template <unsigned RegClassCount, unsigned RegCount, unsigned BitSetSize,
217 unsigned SubClassMaskSize, unsigned SuperRegIdxSeqSize,
218 unsigned SuperClassSize>
220 MCRegisterClass Classes[RegClassCount];
221 MCPhysReg Regs[RegCount];
222 uint8_t BitSets[BitSetSize];
223 uint32_t SubClassMasks[SubClassMaskSize];
224 uint16_t SuperRegIdxSeqs[SuperRegIdxSeqSize];
225 // Avoid zero-sized arrays.
226 unsigned SuperClasses[SuperClassSize > 0 ? SuperClassSize : 1];
227};
228
229/// MCRegisterDesc - This record contains information about a particular
230/// register. The SubRegs field is a zero terminated array of registers that
231/// are sub-registers of the specific register, e.g. AL, AH are sub-registers
232/// of AX. The SuperRegs field is a zero terminated array of registers that are
233/// super-registers of the specific register, e.g. RAX, EAX, are
234/// super-registers of AX.
235///
237 uint32_t Name; // Printable name for the reg (for debugging)
238 uint32_t SubRegs; // Sub-register set, described above
239 uint32_t SuperRegs; // Super-register set, described above
240
241 // Offset into MCRI::SubRegIndices of a list of sub-register indices for each
242 // sub-register in SubRegs.
244
245 // Points to the list of register units. The low bits hold the first regunit
246 // number, the high bits hold an offset into DiffLists. See MCRegUnitIterator.
248
249 /// Index into list with lane mask sequences. The sequence contains a lanemask
250 /// for every register unit.
252
253 // Is true for constant registers.
255
256 // Is true for artificial registers.
258};
259
260/// MCRegisterInfo base class - We assume that the target defines a static
261/// array of MCRegisterDesc objects that represent all of the machine
262/// registers that the target has. As such, we simply have to track a pointer
263/// to this array so that we can turn register number into a register
264/// descriptor.
265///
266/// Note this class is designed to be a base class of TargetRegisterInfo, which
267/// is the interface used by codegen. However, specific targets *should never*
268/// specialize this class. MCRegisterInfo should only contain getters to access
269/// TableGen generated physical register data. It must not be extended with
270/// virtual methods.
271///
273public:
275
276 /// DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be
277 /// performed with a binary search.
279 unsigned FromReg;
280 unsigned ToReg;
281
282 bool operator<(DwarfLLVMRegPair RHS) const { return FromReg < RHS.FromReg; }
283 };
284
285private:
286 const MCRegisterDesc *Desc; // Pointer to the descriptor array
287 unsigned NumRegs; // Number of entries in the array
288 MCRegister RAReg; // Return address register
289 MCRegister PCReg; // Program counter register
290 const MCRegisterClass *Classes; // Pointer to the regclass array
291 unsigned NumClasses; // Number of entries in the array
292 unsigned NumRegUnits; // Number of regunits.
293 const MCPhysReg (*RegUnitRoots)[2]; // Pointer to regunit root table.
294 const int16_t *DiffLists; // Pointer to the difflists array
295 const LaneBitmask *RegUnitMaskSequences; // Pointer to lane mask sequences
296 // for register units.
297 const char *RegStrings; // Pointer to the string table.
298 const char *RegClassStrings; // Pointer to the class strings.
299 const uint16_t *SubRegIndices; // Pointer to the subreg lookup
300 // array.
301 unsigned NumSubRegIndices; // Number of subreg indices.
302 const uint16_t *RegEncodingTable; // Pointer to array of register
303 // encodings.
304 const unsigned (*RegUnitIntervals)[2]; // Pointer to regunit interval table.
305
306 unsigned L2DwarfRegsSize;
307 unsigned EHL2DwarfRegsSize;
308 unsigned Dwarf2LRegsSize;
309 unsigned EHDwarf2LRegsSize;
310 const DwarfLLVMRegPair *L2DwarfRegs; // LLVM to Dwarf regs mapping
311 const DwarfLLVMRegPair *EHL2DwarfRegs; // LLVM to Dwarf regs mapping EH
312 const DwarfLLVMRegPair *Dwarf2LRegs; // Dwarf to LLVM regs mapping
313 const DwarfLLVMRegPair *EHDwarf2LRegs; // Dwarf to LLVM regs mapping EH
314 DenseMap<MCRegister, int> L2SEHRegs; // LLVM to SEH regs mapping
315 DenseMap<MCRegister, int> L2CVRegs; // LLVM to CV regs mapping
316
317 mutable std::vector<std::vector<MCPhysReg>> RegAliasesCache;
318 ArrayRef<MCPhysReg> getCachedAliasesOf(MCRegister R) const;
319
320 /// Iterator class that can traverse the differentially encoded values in
321 /// DiffLists. Don't use this class directly, use one of the adaptors below.
322 class DiffListIterator
323 : public iterator_facade_base<DiffListIterator, std::forward_iterator_tag,
324 unsigned> {
325 unsigned Val = 0;
326 const int16_t *List = nullptr;
327
328 public:
329 /// Constructs an invalid iterator, which is also the end iterator.
330 /// Call init() to point to something useful.
331 DiffListIterator() = default;
332
333 /// Point the iterator to InitVal, decoding subsequent values from DiffList.
334 void init(unsigned InitVal, const int16_t *DiffList) {
335 Val = InitVal;
336 List = DiffList;
337 }
338
339 /// Returns true if this iterator is not yet at the end.
340 bool isValid() const { return List; }
341
342 /// Dereference the iterator to get the value at the current position.
343 const unsigned &operator*() const { return Val; }
344
345 using DiffListIterator::iterator_facade_base::operator++;
346 /// Pre-increment to move to the next position.
347 DiffListIterator &operator++() {
348 assert(isValid() && "Cannot move off the end of the list.");
349 int16_t D = *List++;
350 Val += D;
351 // The end of the list is encoded as a 0 differential.
352 if (!D)
353 List = nullptr;
354 return *this;
355 }
356
357 bool operator==(const DiffListIterator &Other) const {
358 return List == Other.List;
359 }
360 };
361
362public:
363 /// Return an iterator range over all sub-registers of \p Reg, excluding \p
364 /// Reg.
365 iterator_range<MCSubRegIterator> subregs(MCRegister Reg) const;
366
367 /// Return an iterator range over all sub-registers of \p Reg, including \p
368 /// Reg.
369 iterator_range<MCSubRegIterator> subregs_inclusive(MCRegister Reg) const;
370
371 /// Return an iterator range over all super-registers of \p Reg, excluding \p
372 /// Reg.
373 iterator_range<MCSuperRegIterator> superregs(MCRegister Reg) const;
374
375 /// Return an iterator range over all super-registers of \p Reg, including \p
376 /// Reg.
377 iterator_range<MCSuperRegIterator> superregs_inclusive(MCRegister Reg) const;
378
379 /// Return an iterator range over all sub- and super-registers of \p Reg,
380 /// including \p Reg.
381 detail::concat_range<const MCPhysReg, iterator_range<MCSubRegIterator>,
382 iterator_range<MCSuperRegIterator>>
383 sub_and_superregs_inclusive(MCRegister Reg) const;
384
385 /// Returns an iterator range over all regunits.
386 iota_range<MCRegUnit> regunits() const;
387
388 /// Returns an iterator range over all regunits for \p Reg.
389 iterator_range<MCRegUnitIterator> regunits(MCRegister Reg) const;
390
391 // These iterators are allowed to sub-class DiffListIterator and access
392 // internal list pointers.
393 friend class MCSubRegIterator;
395 friend class MCSuperRegIterator;
396 friend class MCRegUnitIterator;
399 friend class MCRegAliasIterator;
400
401 virtual ~MCRegisterInfo() = default;
402
403 /// Initialize MCRegisterInfo, called by TableGen
404 /// auto-generated routines. *DO NOT USE*.
405 void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA,
406 unsigned PC, const MCRegisterClass *C, unsigned NC,
407 const MCPhysReg (*RURoots)[2], unsigned NRU,
408 const int16_t *DL, const LaneBitmask *RUMS,
409 const char *Strings, const char *ClassStrings,
410 const uint16_t *SubIndices, unsigned NumIndices,
411 const uint16_t *RET,
412 const unsigned (*RUI)[2] = nullptr) {
413 Desc = D;
414 NumRegs = NR;
415 RAReg = RA;
416 PCReg = PC;
417 Classes = C;
418 DiffLists = DL;
419 RegUnitMaskSequences = RUMS;
420 RegStrings = Strings;
421 RegClassStrings = ClassStrings;
422 NumClasses = NC;
423 RegUnitRoots = RURoots;
424 NumRegUnits = NRU;
425 SubRegIndices = SubIndices;
426 NumSubRegIndices = NumIndices;
427 RegEncodingTable = RET;
428 RegUnitIntervals = RUI;
429
430 // Initialize DWARF register mapping variables
431 EHL2DwarfRegs = nullptr;
432 EHL2DwarfRegsSize = 0;
433 L2DwarfRegs = nullptr;
434 L2DwarfRegsSize = 0;
435 EHDwarf2LRegs = nullptr;
436 EHDwarf2LRegsSize = 0;
437 Dwarf2LRegs = nullptr;
438 Dwarf2LRegsSize = 0;
439
440 RegAliasesCache.resize(NumRegs);
441 }
442
443 /// Used to initialize LLVM register to Dwarf
444 /// register number mapping. Called by TableGen auto-generated routines.
445 /// *DO NOT USE*.
447 bool isEH) {
448 if (isEH) {
449 EHL2DwarfRegs = Map;
450 EHL2DwarfRegsSize = Size;
451 } else {
452 L2DwarfRegs = Map;
453 L2DwarfRegsSize = Size;
454 }
455 }
456
457 /// Used to initialize Dwarf register to LLVM
458 /// register number mapping. Called by TableGen auto-generated routines.
459 /// *DO NOT USE*.
461 bool isEH) {
462 if (isEH) {
463 EHDwarf2LRegs = Map;
464 EHDwarf2LRegsSize = Size;
465 } else {
466 Dwarf2LRegs = Map;
467 Dwarf2LRegsSize = Size;
468 }
469 }
470
471 /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register
472 /// number mapping. By default the SEH register number is just the same
473 /// as the LLVM register number.
474 /// FIXME: TableGen these numbers. Currently this requires target specific
475 /// initialization code.
476 void mapLLVMRegToSEHReg(MCRegister LLVMReg, int SEHReg) {
477 L2SEHRegs[LLVMReg] = SEHReg;
478 }
479
480 void mapLLVMRegToCVReg(MCRegister LLVMReg, int CVReg) {
481 L2CVRegs[LLVMReg] = CVReg;
482 }
483
484 /// This method should return the register where the return
485 /// address can be found.
487 return RAReg;
488 }
489
490 /// Return the register which is the program counter.
492 return PCReg;
493 }
494
496 assert(Reg.id() < NumRegs &&
497 "Attempting to access record for invalid register number!");
498 return Desc[Reg.id()];
499 }
500
501 /// Provide a get method, equivalent to [], but more useful with a
502 /// pointer to this object.
504 return operator[](Reg);
505 }
506
507 /// Returns the physical register number of sub-register "Index"
508 /// for physical register RegNo. Return zero if the sub-register does not
509 /// exist.
510 MCRegister getSubReg(MCRegister Reg, unsigned Idx) const;
511
512 /// Return a super-register of the specified register
513 /// Reg so its sub-register of index SubIdx is Reg.
514 MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx,
515 const MCRegisterClass *RC) const;
516
517 /// For a given register pair, return the sub-register index
518 /// if the second register is a sub-register of the first. Return zero
519 /// otherwise.
520 unsigned getSubRegIndex(MCRegister RegNo, MCRegister SubRegNo) const;
521
522 /// Return the human-readable symbolic target-specific name for the
523 /// specified physical register.
524 const char *getName(MCRegister RegNo) const {
525 return RegStrings + get(RegNo).Name;
526 }
527
528 /// Returns true if the given register is constant.
529 bool isConstant(MCRegister RegNo) const { return get(RegNo).IsConstant; }
530
531 /// Returns true if the given register is artificial, which means it
532 /// represents a regunit that is not separately addressable but still needs to
533 /// be modelled, such as the top 16-bits of a 32-bit GPR.
534 bool isArtificial(MCRegister RegNo) const { return get(RegNo).IsArtificial; }
535
536 /// Returns true when the given register unit is considered artificial.
537 /// Register units are considered artificial when at least one of the
538 /// root registers is artificial.
539 bool isArtificialRegUnit(MCRegUnit Unit) const;
540
541 /// Return the number of registers this target has (useful for
542 /// sizing arrays holding per register information)
543 unsigned getNumRegs() const {
544 return NumRegs;
545 }
546
547 /// Return the number of sub-register indices
548 /// understood by the target. Index 0 is reserved for the no-op sub-register,
549 /// while 1 to getNumSubRegIndices() - 1 represent real sub-registers.
550 unsigned getNumSubRegIndices() const {
551 return NumSubRegIndices;
552 }
553
554 /// Return the number of (native) register units in the
555 /// target. Register units are numbered from 0 to getNumRegUnits() - 1. They
556 /// can be accessed through MCRegUnitIterator defined below.
557 unsigned getNumRegUnits() const {
558 return NumRegUnits;
559 }
560
561 /// Map a target register to an equivalent dwarf register
562 /// number. Returns -1 if there is no equivalent value. The second
563 /// parameter allows targets to use different numberings for EH info and
564 /// debugging info.
565 virtual int64_t getDwarfRegNum(MCRegister Reg, bool isEH) const;
566
567 /// Map a dwarf register back to a target register. Returns std::nullopt if
568 /// there is no mapping.
569 std::optional<MCRegister> getLLVMRegNum(uint64_t RegNum, bool isEH) const;
570
571 /// Map a target EH register number to an equivalent DWARF register
572 /// number.
573 int64_t getDwarfRegNumFromDwarfEHRegNum(uint64_t RegNum) const;
574
575 /// Map a target register to an equivalent SEH register
576 /// number. Returns LLVM register number if there is no equivalent value.
577 int getSEHRegNum(MCRegister Reg) const;
578
579 /// Map a target register to an equivalent CodeView register
580 /// number.
581 int getCodeViewRegNum(MCRegister Reg) const;
582
583 regclass_iterator regclass_begin() const { return Classes; }
584 regclass_iterator regclass_end() const { return Classes+NumClasses; }
588
589 unsigned getNumRegClasses() const {
590 return (unsigned)(regclass_end()-regclass_begin());
591 }
592
593 /// Returns the register class associated with the enumeration
594 /// value. See class MCOperandInfo.
595 const MCRegisterClass& getRegClass(unsigned i) const {
596 assert(i < getNumRegClasses() && "Register Class ID out of range");
597 return Classes[i];
598 }
599
600 const char *getRegClassName(const MCRegisterClass *Class) const {
601 return RegClassStrings + Class->NameIdx;
602 }
603
604 /// Returns the encoding for Reg
606 assert(Reg.id() < NumRegs &&
607 "Attempting to get encoding for invalid register number!");
608 return RegEncodingTable[Reg.id()];
609 }
610
611 /// Returns true if RegB is a sub-register of RegA.
612 bool isSubRegister(MCRegister RegA, MCRegister RegB) const {
613 return isSuperRegister(RegB, RegA);
614 }
615
616 /// Returns true if RegB is a super-register of RegA.
617 bool isSuperRegister(MCRegister RegA, MCRegister RegB) const;
618
619 /// Returns true if RegB is a sub-register of RegA or if RegB == RegA.
620 bool isSubRegisterEq(MCRegister RegA, MCRegister RegB) const {
621 return isSuperRegisterEq(RegB, RegA);
622 }
623
624 /// Returns true if RegB is a super-register of RegA or if
625 /// RegB == RegA.
626 bool isSuperRegisterEq(MCRegister RegA, MCRegister RegB) const {
627 return RegA == RegB || isSuperRegister(RegA, RegB);
628 }
629
630 /// Returns true if RegB is a super-register or sub-register of RegA
631 /// or if RegB == RegA.
633 return isSubRegisterEq(RegA, RegB) || isSuperRegister(RegA, RegB);
634 }
635
636 /// Returns true if the two registers are equal or alias each other.
637 bool regsOverlap(MCRegister RegA, MCRegister RegB) const;
638
639 /// Returns true if this target uses regunit intervals.
640 bool hasRegUnitIntervals() const { return RegUnitIntervals != nullptr; }
641
642 /// Returns an iterator range over all native regunits in the RegUnitInterval
643 /// table for \p Reg.
646 "Target does not support regunit intervals");
647 assert(Reg.id() < NumRegs && "Invalid register number");
648 return seq<unsigned>(RegUnitIntervals[Reg.id()][0],
649 RegUnitIntervals[Reg.id()][1]);
650 }
651};
652
653//===----------------------------------------------------------------------===//
654// Register List Iterators
655//===----------------------------------------------------------------------===//
656
657// MCRegisterInfo provides lists of super-registers, sub-registers, and
658// aliasing registers. Use these iterator classes to traverse the lists.
659
660/// MCSubRegIterator enumerates all sub-registers of Reg.
661/// If IncludeSelf is set, Reg itself is included in the list.
663 : public iterator_adaptor_base<MCSubRegIterator,
664 MCRegisterInfo::DiffListIterator,
665 std::forward_iterator_tag, const MCPhysReg> {
666 // Cache the current value, so that we can return a reference to it.
667 MCPhysReg Val;
668
669public:
670 /// Constructs an end iterator.
671 MCSubRegIterator() = default;
672
674 bool IncludeSelf = false) {
675 assert(Reg.isPhysical());
676 I.init(Reg.id(), MCRI->DiffLists + MCRI->get(Reg).SubRegs);
677 // Initially, the iterator points to Reg itself.
678 Val = MCPhysReg(*I);
679 if (!IncludeSelf)
680 ++*this;
681 }
682
683 const MCPhysReg &operator*() const { return Val; }
684
685 using iterator_adaptor_base::operator++;
687 Val = MCPhysReg(*++I);
688 return *this;
689 }
690
691 /// Returns true if this iterator is not yet at the end.
692 bool isValid() const { return I.isValid(); }
693};
694
695/// Iterator that enumerates the sub-registers of a Reg and the associated
696/// sub-register indices.
698 MCSubRegIterator SRIter;
699 const uint16_t *SRIndex;
700
701public:
702 /// Constructs an iterator that traverses subregisters and their
703 /// associated subregister indices.
705 : SRIter(Reg, MCRI) {
706 SRIndex = MCRI->SubRegIndices + MCRI->get(Reg).SubRegIndices;
707 }
708
709 /// Returns current sub-register.
711 return *SRIter;
712 }
713
714 /// Returns sub-register index of the current sub-register.
715 unsigned getSubRegIndex() const {
716 return *SRIndex;
717 }
718
719 /// Returns true if this iterator is not yet at the end.
720 bool isValid() const { return SRIter.isValid(); }
721
722 /// Moves to the next position.
724 ++SRIter;
725 ++SRIndex;
726 return *this;
727 }
728};
729
730/// MCSuperRegIterator enumerates all super-registers of Reg.
731/// If IncludeSelf is set, Reg itself is included in the list.
733 : public iterator_adaptor_base<MCSuperRegIterator,
734 MCRegisterInfo::DiffListIterator,
735 std::forward_iterator_tag, const MCPhysReg> {
736 // Cache the current value, so that we can return a reference to it.
737 MCPhysReg Val;
738
739public:
740 /// Constructs an end iterator.
742
744 bool IncludeSelf = false) {
745 assert(Reg.isPhysical());
746 I.init(Reg.id(), MCRI->DiffLists + MCRI->get(Reg).SuperRegs);
747 // Initially, the iterator points to Reg itself.
748 Val = MCPhysReg(*I);
749 if (!IncludeSelf)
750 ++*this;
751 }
752
753 const MCPhysReg &operator*() const { return Val; }
754
755 using iterator_adaptor_base::operator++;
757 Val = MCPhysReg(*++I);
758 return *this;
759 }
760
761 /// Returns true if this iterator is not yet at the end.
762 bool isValid() const { return I.isValid(); }
763};
764
765// Definition for isSuperRegister. Put it down here since it needs the
766// iterator defined above in addition to the MCRegisterInfo class itself.
768 return is_contained(superregs(RegA), RegB);
769}
770
771//===----------------------------------------------------------------------===//
772// Register Units
773//===----------------------------------------------------------------------===//
774
775// MCRegUnitIterator enumerates a list of register units for Reg. The list is
776// in ascending numerical order.
778 : public iterator_adaptor_base<MCRegUnitIterator,
779 MCRegisterInfo::DiffListIterator,
780 std::forward_iterator_tag, const MCRegUnit> {
781 // The value must be kept in sync with RegisterInfoEmitter.cpp.
782 static constexpr unsigned RegUnitBits = 12;
783 // Cache the current value, so that we can return a reference to it.
784 MCRegUnit Val;
785
786public:
787 /// Constructs an end iterator.
788 MCRegUnitIterator() = default;
789
791 assert(Reg.isPhysical());
792 // Decode the RegUnits MCRegisterDesc field.
793 unsigned RU = MCRI->get(Reg).RegUnits;
794 unsigned FirstRU = RU & ((1u << RegUnitBits) - 1);
795 unsigned Offset = RU >> RegUnitBits;
796 I.init(FirstRU, MCRI->DiffLists + Offset);
797 Val = MCRegUnit(*I);
798 }
799
800 const MCRegUnit &operator*() const { return Val; }
801
802 using iterator_adaptor_base::operator++;
804 Val = MCRegUnit(*++I);
805 return *this;
806 }
807
808 /// Returns true if this iterator is not yet at the end.
809 bool isValid() const { return I.isValid(); }
810};
811
812/// MCRegUnitMaskIterator enumerates a list of register units and their
813/// associated lane masks for Reg. The register units are in ascending
814/// numerical order.
816 MCRegUnitIterator RUIter;
817 const LaneBitmask *MaskListIter;
818
819public:
821
822 /// Constructs an iterator that traverses the register units and their
823 /// associated LaneMasks in Reg.
825 : RUIter(Reg, MCRI) {
826 uint16_t Idx = MCRI->get(Reg).RegUnitLaneMasks;
827 MaskListIter = &MCRI->RegUnitMaskSequences[Idx];
828 }
829
830 /// Returns a (RegUnit, LaneMask) pair.
831 std::pair<MCRegUnit, LaneBitmask> operator*() const {
832 return std::make_pair(*RUIter, *MaskListIter);
833 }
834
835 /// Returns true if this iterator is not yet at the end.
836 bool isValid() const { return RUIter.isValid(); }
837
838 /// Moves to the next position.
840 ++MaskListIter;
841 ++RUIter;
842 return *this;
843 }
844};
845
846// Each register unit has one or two root registers. The complete set of
847// registers containing a register unit is the union of the roots and their
848// super-registers. All registers aliasing Unit can be visited like this:
849//
850// for (MCRegUnitRootIterator RI(Unit, MCRI); RI.isValid(); ++RI) {
851// for (MCSuperRegIterator SI(*RI, MCRI, true); SI.isValid(); ++SI)
852// visit(*SI);
853// }
854
855/// MCRegUnitRootIterator enumerates the root registers of a register unit.
857 uint16_t Reg0 = 0;
858 uint16_t Reg1 = 0;
859
860public:
862
863 MCRegUnitRootIterator(MCRegUnit RegUnit, const MCRegisterInfo *MCRI) {
864 assert(static_cast<unsigned>(RegUnit) < MCRI->getNumRegUnits() &&
865 "Invalid register unit");
866 Reg0 = MCRI->RegUnitRoots[static_cast<unsigned>(RegUnit)][0];
867 Reg1 = MCRI->RegUnitRoots[static_cast<unsigned>(RegUnit)][1];
868 }
869
870 /// Dereference to get the current root register.
871 unsigned operator*() const {
872 return Reg0;
873 }
874
875 /// Check if the iterator is at the end of the list.
876 bool isValid() const {
877 return Reg0;
878 }
879
880 /// Preincrement to move to the next root register.
882 assert(isValid() && "Cannot move off the end of the list.");
883 Reg0 = Reg1;
884 Reg1 = 0;
885 return *this;
886 }
887};
888
889/// MCRegAliasIterator enumerates all registers aliasing Reg.
891private:
892 const MCPhysReg *It = nullptr;
893 const MCPhysReg *End = nullptr;
894
895public:
897 bool IncludeSelf) {
898 ArrayRef<MCPhysReg> Cache = MCRI->getCachedAliasesOf(Reg);
899 assert(Cache.back() == Reg);
900 It = Cache.begin();
901 End = Cache.end();
902 if (!IncludeSelf)
903 --End;
904 }
905
906 bool isValid() const { return It != End; }
907
908 MCRegister operator*() const { return *It; }
909
911 assert(isValid() && "Cannot move off the end of the list.");
912 ++It;
913 return *this;
914 }
915};
916
919 return make_range({Reg, this, /*IncludeSelf=*/false}, MCSubRegIterator());
920}
921
924 return make_range({Reg, this, /*IncludeSelf=*/true}, MCSubRegIterator());
925}
926
929 return make_range({Reg, this, /*IncludeSelf=*/false}, MCSuperRegIterator());
930}
931
934 return make_range({Reg, this, /*IncludeSelf=*/true}, MCSuperRegIterator());
935}
936
942
944 return enum_seq(static_cast<MCRegUnit>(0),
945 static_cast<MCRegUnit>(getNumRegUnits()),
947}
948
953
954} // end namespace llvm
955
956#endif // LLVM_MC_MCREGISTERINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
A common definition of LaneBitmask for use in TableGen and CodeGen.
Register Reg
bool operator==(const MergedFunctionsInfo &LHS, const MergedFunctionsInfo &RHS)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
static constexpr MCPhysReg RAReg
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
SI optimize exec mask operations pre RA
Provides some synthesis utilities to produce sequences of values.
static unsigned getDwarfRegNum(MCRegister Reg, const TargetRegisterInfo *TRI)
Go up the super-register chain until we hit a valid dwarf register number.
Value * RHS
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
MCRegAliasIterator(MCRegister Reg, const MCRegisterInfo *MCRI, bool IncludeSelf)
MCRegister operator*() const
MCRegAliasIterator & operator++()
const MCRegUnit & operator*() const
MCRegUnitIterator()=default
Constructs an end iterator.
bool isValid() const
Returns true if this iterator is not yet at the end.
MCRegUnitIterator & operator++()
MCRegUnitIterator(MCRegister Reg, const MCRegisterInfo *MCRI)
MCRegUnitMaskIterator(MCRegister Reg, const MCRegisterInfo *MCRI)
Constructs an iterator that traverses the register units and their associated LaneMasks in Reg.
std::pair< MCRegUnit, LaneBitmask > operator*() const
Returns a (RegUnit, LaneMask) pair.
MCRegUnitMaskIterator & operator++()
Moves to the next position.
bool isValid() const
Returns true if this iterator is not yet at the end.
MCRegUnitRootIterator & operator++()
Preincrement to move to the next root register.
unsigned operator*() const
Dereference to get the current root register.
MCRegUnitRootIterator(MCRegUnit RegUnit, const MCRegisterInfo *MCRI)
bool isValid() const
Check if the iterator is at the end of the list.
MCRegisterClass - Base class of TargetRegisterClass.
const uint8_t SpillStackID
ArrayRef< unsigned > superclasses() const
Returns a list of super-classes.
const MCPhysReg RegSetBegin
Register denoted by first bit in RegSet.
bool hasSuperClass(const MCRegisterClass *RC) const
Return true if the specified MCRegisterClass is a proper super-class of this MCRegisterClass.
const bool CoveredBySubRegs
Whether a combination of subregisters can cover every register in the class.
const uint32_t * getSubClassMask() const
Returns a bit vector of subclasses, including this one.
const uint32_t NameIdx
bool hasSuperClassEq(const MCRegisterClass *RC) const
Returns true if RC is a super-class of or equal to this class.
unsigned getID() const
getID() - Return the register class ID number.
ArrayRef< MCPhysReg > getRegisters() const
const uint32_t RegSizeInBits
bool isAllocatable() const
isAllocatable - Return true if this register class may be used to create virtual registers.
const uint8_t AllocationPriority
Classes with a higher priority value are assigned first by register allocators using a greedy heurist...
const MCPhysReg * iterator
MCRegister getRegister(unsigned i) const
getRegister - Return the specified register in the class.
const uint8_t TSFlags
Configurable target specific flags.
const uint32_t SuperClassesOff
Relative offset to unsigned array.
const LaneBitmask LaneMask
unsigned getSizeInBits() const
Return the size of the physical register in bits if we are able to determine it.
const uint32_t RegSetOff
Relative offset to uint8_t array.
const uint16_t RegSetSize
bool contains(MCRegister Reg1, MCRegister Reg2) const
contains - Return true if both registers are in this class.
LaneBitmask getLaneMask() const
Returns the combination of all lane masks of register in this class.
const uint32_t SuperRegIndicesOff
Relative offset to MCPhysReg array.
const uint16_t * getSuperRegIndices() const
Returns a 0-terminated list of sub-register indices that project some super-register class into this ...
unsigned getNumRegs() const
getNumRegs - Return the number of registers in this class.
const uint32_t SubClassMaskOff
Relative offset to uint32_t array.
iterator begin() const
begin/end - Return all of the registers in this class.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
const uint32_t RegsOff
Relative offset to MCPhysReg array.
bool isBaseClass() const
Return true if this register class has a defined BaseClassOrder.
bool expensiveOrImpossibleToCopy() const
uint8_t getCopyCost() const
getCopyCost - Return the cost of copying a value between two registers in this class.
iterator end() const
const bool HasDisjunctSubRegs
Whether the class supports two (or more) disjunct subregister indices.
bool hasSubClass(const MCRegisterClass *RC) const
Return true if the specified TargetRegisterClass is a proper sub-class of this TargetRegisterClass.
const uint16_t SuperClassesSize
const MCPhysReg * const_iterator
const uint16_t RegsSize
bool hasSubClassEq(const MCRegisterClass *RC) const
Returns true if RC is a sub-class of or equal to this class.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
iota_range< unsigned > regunits_interval(MCRegister Reg) const
Returns an iterator range over all native regunits in the RegUnitInterval table for Reg.
void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA, unsigned PC, const MCRegisterClass *C, unsigned NC, const MCPhysReg(*RURoots)[2], unsigned NRU, const int16_t *DL, const LaneBitmask *RUMS, const char *Strings, const char *ClassStrings, const uint16_t *SubIndices, unsigned NumIndices, const uint16_t *RET, const unsigned(*RUI)[2]=nullptr)
Initialize MCRegisterInfo, called by TableGen auto-generated routines.
unsigned getNumSubRegIndices() const
Return the number of sub-register indices understood by the target.
const MCRegisterDesc & operator[](MCRegister Reg) const
bool isSuperRegisterEq(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a super-register of RegA or if RegB == RegA.
unsigned getNumRegClasses() const
MCRegister getRARegister() const
This method should return the register where the return address can be found.
virtual ~MCRegisterInfo()=default
MCRegister getProgramCounter() const
Return the register which is the program counter.
regclass_iterator regclass_end() const
void mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair *Map, unsigned Size, bool isEH)
Used to initialize Dwarf register to LLVM register number mapping.
unsigned getNumRegUnits() const
Return the number of (native) register units in the target.
friend class MCRegAliasIterator
const MCRegisterDesc & get(MCRegister Reg) const
Provide a get method, equivalent to [], but more useful with a pointer to this object.
const MCRegisterClass * regclass_iterator
iterator_range< regclass_iterator > regclasses() const
regclass_iterator regclass_begin() const
iota_range< MCRegUnit > regunits() const
Returns an iterator range over all regunits.
void mapLLVMRegToCVReg(MCRegister LLVMReg, int CVReg)
iterator_range< MCSuperRegIterator > superregs(MCRegister Reg) const
Return an iterator range over all super-registers of Reg, excluding Reg.
const char * getName(MCRegister RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register.
bool hasRegUnitIntervals() const
Returns true if this target uses regunit intervals.
const char * getRegClassName(const MCRegisterClass *Class) const
friend class MCSubRegIterator
friend class MCRegUnitRootIterator
uint16_t getEncodingValue(MCRegister Reg) const
Returns the encoding for Reg.
iterator_range< MCSubRegIterator > subregs_inclusive(MCRegister Reg) const
Return an iterator range over all sub-registers of Reg, including Reg.
bool isSuperOrSubRegisterEq(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a super-register or sub-register of RegA or if RegB == RegA.
bool isSubRegister(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a sub-register of RegA.
friend class MCSuperRegIterator
iterator_range< MCSubRegIterator > subregs(MCRegister Reg) const
Return an iterator range over all sub-registers of Reg, excluding Reg.
bool isConstant(MCRegister RegNo) const
Returns true if the given register is constant.
friend class MCRegUnitMaskIterator
void mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair *Map, unsigned Size, bool isEH)
Used to initialize LLVM register to Dwarf register number mapping.
bool isSuperRegister(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a super-register of RegA.
bool isArtificial(MCRegister RegNo) const
Returns true if the given register is artificial, which means it represents a regunit that is not sep...
bool isSubRegisterEq(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a sub-register of RegA or if RegB == RegA.
friend class MCRegUnitIterator
void mapLLVMRegToSEHReg(MCRegister LLVMReg, int SEHReg)
mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register number mapping.
iterator_range< MCSuperRegIterator > superregs_inclusive(MCRegister Reg) const
Return an iterator range over all super-registers of Reg, including Reg.
detail::concat_range< const MCPhysReg, iterator_range< MCSubRegIterator >, iterator_range< MCSuperRegIterator > > sub_and_superregs_inclusive(MCRegister Reg) const
Return an iterator range over all sub- and super-registers of Reg, including Reg.
const MCRegisterClass & getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
friend class MCSubRegIndexIterator
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
MCSubRegIndexIterator(MCRegister Reg, const MCRegisterInfo *MCRI)
Constructs an iterator that traverses subregisters and their associated subregister indices.
MCSubRegIndexIterator & operator++()
Moves to the next position.
bool isValid() const
Returns true if this iterator is not yet at the end.
unsigned getSubRegIndex() const
Returns sub-register index of the current sub-register.
MCRegister getSubReg() const
Returns current sub-register.
MCSubRegIterator enumerates all sub-registers of Reg.
const MCPhysReg & operator*() const
MCSubRegIterator & operator++()
bool isValid() const
Returns true if this iterator is not yet at the end.
MCSubRegIterator()=default
Constructs an end iterator.
MCSubRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI, bool IncludeSelf=false)
MCSuperRegIterator enumerates all super-registers of Reg.
MCSuperRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI, bool IncludeSelf=false)
MCSuperRegIterator & operator++()
const MCPhysReg & operator*() const
MCSuperRegIterator()=default
Constructs an end iterator.
bool isValid() const
Returns true if this iterator is not yet at the end.
Helper to store a sequence of ranges being concatenated and access them.
Definition STLExtras.h:1100
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterator.h:80
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
APInt operator*(APInt a, uint64_t RHS)
Definition APInt.h:2262
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
constexpr force_iteration_on_noniterable_enum_t force_iteration_on_noniterable_enum
Definition Sequence.h:110
Op::Description Desc
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
Definition STLExtras.h:1151
constexpr auto enum_seq(EnumT Begin, EnumT End)
Iterate over an enum type from Begin up to - but not including - End.
Definition Sequence.h:373
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
#define NC
Definition regutils.h:42
unsigned SuperClasses[SuperClassSize > 0 ? SuperClassSize :1]
uint32_t SubClassMasks[SubClassMaskSize]
MCRegisterClass Classes[RegClassCount]
uint16_t SuperRegIdxSeqs[SuperRegIdxSeqSize]
uint8_t BitSets[BitSetSize]
MCRegisterDesc - This record contains information about a particular register.
uint16_t RegUnitLaneMasks
Index into list with lane mask sequences.
DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be performed with a binary se...
bool operator<(DwarfLLVMRegPair RHS) const