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/iterator.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/MC/LaneBitmask.h"
22 #include "llvm/MC/MCRegister.h"
23 #include <cassert>
24 #include <cstdint>
25 #include <iterator>
26 #include <utility>
27 
28 namespace llvm {
29 
30 class MCRegUnitIterator;
31 class MCSubRegIterator;
32 class MCSuperRegIterator;
33 
34 /// MCRegisterClass - Base class of TargetRegisterClass.
35 class MCRegisterClass {
36 public:
37   using iterator = const MCPhysReg*;
38   using const_iterator = const MCPhysReg*;
39 
40   const iterator RegsBegin;
41   const uint8_t *const RegSet;
42   const uint32_t NameIdx;
43   const uint16_t RegsSize;
44   const uint16_t RegSetSize;
45   const uint16_t ID;
46   const uint16_t RegSizeInBits;
47   const int8_t CopyCost;
48   const bool Allocatable;
49   const bool BaseClass;
50 
51   /// getID() - Return the register class ID number.
52   ///
getID()53   unsigned getID() const { return ID; }
54 
55   /// begin/end - Return all of the registers in this class.
56   ///
begin()57   iterator       begin() const { return RegsBegin; }
end()58   iterator         end() const { return RegsBegin + RegsSize; }
59 
60   /// getNumRegs - Return the number of registers in this class.
61   ///
getNumRegs()62   unsigned getNumRegs() const { return RegsSize; }
63 
64   /// getRegister - Return the specified register in the class.
65   ///
getRegister(unsigned i)66   unsigned getRegister(unsigned i) const {
67     assert(i < getNumRegs() && "Register number out of range!");
68     return RegsBegin[i];
69   }
70 
71   /// contains - Return true if the specified register is included in this
72   /// register class.  This does not include virtual registers.
contains(MCRegister Reg)73   bool contains(MCRegister Reg) const {
74     unsigned RegNo = unsigned(Reg);
75     unsigned InByte = RegNo % 8;
76     unsigned Byte = RegNo / 8;
77     if (Byte >= RegSetSize)
78       return false;
79     return (RegSet[Byte] & (1 << InByte)) != 0;
80   }
81 
82   /// contains - Return true if both registers are in this class.
contains(MCRegister Reg1,MCRegister Reg2)83   bool contains(MCRegister Reg1, MCRegister Reg2) const {
84     return contains(Reg1) && contains(Reg2);
85   }
86 
87   /// Return the size of the physical register in bits if we are able to
88   /// determine it. This always returns zero for registers of targets that use
89   /// HW modes, as we need more information to determine the size of registers
90   /// in such cases. Use TargetRegisterInfo to cover them.
getSizeInBits()91   unsigned getSizeInBits() const { return RegSizeInBits; }
92 
93   /// getCopyCost - Return the cost of copying a value between two registers in
94   /// this class. A negative number means the register class is very expensive
95   /// to copy e.g. status flag register classes.
getCopyCost()96   int getCopyCost() const { return CopyCost; }
97 
98   /// isAllocatable - Return true if this register class may be used to create
99   /// virtual registers.
isAllocatable()100   bool isAllocatable() const { return Allocatable; }
101 
102   /// Return true if this register class has a defined BaseClassOrder.
isBaseClass()103   bool isBaseClass() const { return BaseClass; }
104 };
105 
106 /// MCRegisterDesc - This record contains information about a particular
107 /// register.  The SubRegs field is a zero terminated array of registers that
108 /// are sub-registers of the specific register, e.g. AL, AH are sub-registers
109 /// of AX. The SuperRegs field is a zero terminated array of registers that are
110 /// super-registers of the specific register, e.g. RAX, EAX, are
111 /// super-registers of AX.
112 ///
113 struct MCRegisterDesc {
114   uint32_t Name;      // Printable name for the reg (for debugging)
115   uint32_t SubRegs;   // Sub-register set, described above
116   uint32_t SuperRegs; // Super-register set, described above
117 
118   // Offset into MCRI::SubRegIndices of a list of sub-register indices for each
119   // sub-register in SubRegs.
120   uint32_t SubRegIndices;
121 
122   // Points to the list of register units. The low bits hold the first regunit
123   // number, the high bits hold an offset into DiffLists. See MCRegUnitIterator.
124   uint32_t RegUnits;
125 
126   /// Index into list with lane mask sequences. The sequence contains a lanemask
127   /// for every register unit.
128   uint16_t RegUnitLaneMasks;
129 
130   // Is true for constant registers.
131   bool IsConstant;
132 };
133 
134 /// MCRegisterInfo base class - We assume that the target defines a static
135 /// array of MCRegisterDesc objects that represent all of the machine
136 /// registers that the target has.  As such, we simply have to track a pointer
137 /// to this array so that we can turn register number into a register
138 /// descriptor.
139 ///
140 /// Note this class is designed to be a base class of TargetRegisterInfo, which
141 /// is the interface used by codegen. However, specific targets *should never*
142 /// specialize this class. MCRegisterInfo should only contain getters to access
143 /// TableGen generated physical register data. It must not be extended with
144 /// virtual methods.
145 ///
146 class MCRegisterInfo {
147 public:
148   using regclass_iterator = const MCRegisterClass *;
149 
150   /// DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be
151   /// performed with a binary search.
152   struct DwarfLLVMRegPair {
153     unsigned FromReg;
154     unsigned ToReg;
155 
156     bool operator<(DwarfLLVMRegPair RHS) const { return FromReg < RHS.FromReg; }
157   };
158 
159 private:
160   const MCRegisterDesc *Desc;                 // Pointer to the descriptor array
161   unsigned NumRegs;                           // Number of entries in the array
162   MCRegister RAReg;                           // Return address register
163   MCRegister PCReg;                           // Program counter register
164   const MCRegisterClass *Classes;             // Pointer to the regclass array
165   unsigned NumClasses;                        // Number of entries in the array
166   unsigned NumRegUnits;                       // Number of regunits.
167   const MCPhysReg (*RegUnitRoots)[2];         // Pointer to regunit root table.
168   const int16_t *DiffLists;                   // Pointer to the difflists array
169   const LaneBitmask *RegUnitMaskSequences;    // Pointer to lane mask sequences
170                                               // for register units.
171   const char *RegStrings;                     // Pointer to the string table.
172   const char *RegClassStrings;                // Pointer to the class strings.
173   const uint16_t *SubRegIndices;              // Pointer to the subreg lookup
174                                               // array.
175   unsigned NumSubRegIndices;                  // Number of subreg indices.
176   const uint16_t *RegEncodingTable;           // Pointer to array of register
177                                               // encodings.
178 
179   unsigned L2DwarfRegsSize;
180   unsigned EHL2DwarfRegsSize;
181   unsigned Dwarf2LRegsSize;
182   unsigned EHDwarf2LRegsSize;
183   const DwarfLLVMRegPair *L2DwarfRegs;        // LLVM to Dwarf regs mapping
184   const DwarfLLVMRegPair *EHL2DwarfRegs;      // LLVM to Dwarf regs mapping EH
185   const DwarfLLVMRegPair *Dwarf2LRegs;        // Dwarf to LLVM regs mapping
186   const DwarfLLVMRegPair *EHDwarf2LRegs;      // Dwarf to LLVM regs mapping EH
187   DenseMap<MCRegister, int> L2SEHRegs;        // LLVM to SEH regs mapping
188   DenseMap<MCRegister, int> L2CVRegs;         // LLVM to CV regs mapping
189 
190   /// Iterator class that can traverse the differentially encoded values in
191   /// DiffLists. Don't use this class directly, use one of the adaptors below.
192   class DiffListIterator
193       : public iterator_facade_base<DiffListIterator, std::forward_iterator_tag,
194                                     unsigned> {
195     unsigned Val = 0;
196     const int16_t *List = nullptr;
197 
198   public:
199     /// Constructs an invalid iterator, which is also the end iterator.
200     /// Call init() to point to something useful.
201     DiffListIterator() = default;
202 
203     /// Point the iterator to InitVal, decoding subsequent values from DiffList.
init(unsigned InitVal,const int16_t * DiffList)204     void init(unsigned InitVal, const int16_t *DiffList) {
205       Val = InitVal;
206       List = DiffList;
207     }
208 
209     /// Returns true if this iterator is not yet at the end.
isValid()210     bool isValid() const { return List; }
211 
212     /// Dereference the iterator to get the value at the current position.
213     const unsigned &operator*() const { return Val; }
214 
215     using DiffListIterator::iterator_facade_base::operator++;
216     /// Pre-increment to move to the next position.
217     DiffListIterator &operator++() {
218       assert(isValid() && "Cannot move off the end of the list.");
219       int16_t D = *List++;
220       Val += D;
221       // The end of the list is encoded as a 0 differential.
222       if (!D)
223         List = nullptr;
224       return *this;
225     }
226 
227     bool operator==(const DiffListIterator &Other) const {
228       return List == Other.List;
229     }
230   };
231 
232 public:
233   /// Return an iterator range over all sub-registers of \p Reg, excluding \p
234   /// Reg.
235   iterator_range<MCSubRegIterator> subregs(MCRegister Reg) const;
236 
237   /// Return an iterator range over all sub-registers of \p Reg, including \p
238   /// Reg.
239   iterator_range<MCSubRegIterator> subregs_inclusive(MCRegister Reg) const;
240 
241   /// Return an iterator range over all super-registers of \p Reg, excluding \p
242   /// Reg.
243   iterator_range<MCSuperRegIterator> superregs(MCRegister Reg) const;
244 
245   /// Return an iterator range over all super-registers of \p Reg, including \p
246   /// Reg.
247   iterator_range<MCSuperRegIterator> superregs_inclusive(MCRegister Reg) const;
248 
249   /// Return an iterator range over all sub- and super-registers of \p Reg,
250   /// including \p Reg.
251   detail::concat_range<const MCPhysReg, iterator_range<MCSubRegIterator>,
252                        iterator_range<MCSuperRegIterator>>
253   sub_and_superregs_inclusive(MCRegister Reg) const;
254 
255   /// Returns an iterator range over all regunits for \p Reg.
256   iterator_range<MCRegUnitIterator> regunits(MCRegister Reg) const;
257 
258   // These iterators are allowed to sub-class DiffListIterator and access
259   // internal list pointers.
260   friend class MCSubRegIterator;
261   friend class MCSubRegIndexIterator;
262   friend class MCSuperRegIterator;
263   friend class MCRegUnitIterator;
264   friend class MCRegUnitMaskIterator;
265   friend class MCRegUnitRootIterator;
266 
267   /// Initialize MCRegisterInfo, called by TableGen
268   /// auto-generated routines. *DO NOT USE*.
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)269   void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA,
270                           unsigned PC, const MCRegisterClass *C, unsigned NC,
271                           const MCPhysReg (*RURoots)[2], unsigned NRU,
272                           const int16_t *DL, const LaneBitmask *RUMS,
273                           const char *Strings, const char *ClassStrings,
274                           const uint16_t *SubIndices, unsigned NumIndices,
275                           const uint16_t *RET) {
276     Desc = D;
277     NumRegs = NR;
278     RAReg = RA;
279     PCReg = PC;
280     Classes = C;
281     DiffLists = DL;
282     RegUnitMaskSequences = RUMS;
283     RegStrings = Strings;
284     RegClassStrings = ClassStrings;
285     NumClasses = NC;
286     RegUnitRoots = RURoots;
287     NumRegUnits = NRU;
288     SubRegIndices = SubIndices;
289     NumSubRegIndices = NumIndices;
290     RegEncodingTable = RET;
291 
292     // Initialize DWARF register mapping variables
293     EHL2DwarfRegs = nullptr;
294     EHL2DwarfRegsSize = 0;
295     L2DwarfRegs = nullptr;
296     L2DwarfRegsSize = 0;
297     EHDwarf2LRegs = nullptr;
298     EHDwarf2LRegsSize = 0;
299     Dwarf2LRegs = nullptr;
300     Dwarf2LRegsSize = 0;
301   }
302 
303   /// Used to initialize LLVM register to Dwarf
304   /// register number mapping. Called by TableGen auto-generated routines.
305   /// *DO NOT USE*.
mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair * Map,unsigned Size,bool isEH)306   void mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair *Map, unsigned Size,
307                               bool isEH) {
308     if (isEH) {
309       EHL2DwarfRegs = Map;
310       EHL2DwarfRegsSize = Size;
311     } else {
312       L2DwarfRegs = Map;
313       L2DwarfRegsSize = Size;
314     }
315   }
316 
317   /// Used to initialize Dwarf register to LLVM
318   /// register number mapping. Called by TableGen auto-generated routines.
319   /// *DO NOT USE*.
mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair * Map,unsigned Size,bool isEH)320   void mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair *Map, unsigned Size,
321                               bool isEH) {
322     if (isEH) {
323       EHDwarf2LRegs = Map;
324       EHDwarf2LRegsSize = Size;
325     } else {
326       Dwarf2LRegs = Map;
327       Dwarf2LRegsSize = Size;
328     }
329   }
330 
331   /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register
332   /// number mapping. By default the SEH register number is just the same
333   /// as the LLVM register number.
334   /// FIXME: TableGen these numbers. Currently this requires target specific
335   /// initialization code.
mapLLVMRegToSEHReg(MCRegister LLVMReg,int SEHReg)336   void mapLLVMRegToSEHReg(MCRegister LLVMReg, int SEHReg) {
337     L2SEHRegs[LLVMReg] = SEHReg;
338   }
339 
mapLLVMRegToCVReg(MCRegister LLVMReg,int CVReg)340   void mapLLVMRegToCVReg(MCRegister LLVMReg, int CVReg) {
341     L2CVRegs[LLVMReg] = CVReg;
342   }
343 
344   /// This method should return the register where the return
345   /// address can be found.
getRARegister()346   MCRegister getRARegister() const {
347     return RAReg;
348   }
349 
350   /// Return the register which is the program counter.
getProgramCounter()351   MCRegister getProgramCounter() const {
352     return PCReg;
353   }
354 
355   const MCRegisterDesc &operator[](MCRegister RegNo) const {
356     assert(RegNo < NumRegs &&
357            "Attempting to access record for invalid register number!");
358     return Desc[RegNo];
359   }
360 
361   /// Provide a get method, equivalent to [], but more useful with a
362   /// pointer to this object.
get(MCRegister RegNo)363   const MCRegisterDesc &get(MCRegister RegNo) const {
364     return operator[](RegNo);
365   }
366 
367   /// Returns the physical register number of sub-register "Index"
368   /// for physical register RegNo. Return zero if the sub-register does not
369   /// exist.
370   MCRegister getSubReg(MCRegister Reg, unsigned Idx) const;
371 
372   /// Return a super-register of the specified register
373   /// Reg so its sub-register of index SubIdx is Reg.
374   MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx,
375                                  const MCRegisterClass *RC) const;
376 
377   /// For a given register pair, return the sub-register index
378   /// if the second register is a sub-register of the first. Return zero
379   /// otherwise.
380   unsigned getSubRegIndex(MCRegister RegNo, MCRegister SubRegNo) const;
381 
382   /// Return the human-readable symbolic target-specific name for the
383   /// specified physical register.
getName(MCRegister RegNo)384   const char *getName(MCRegister RegNo) const {
385     return RegStrings + get(RegNo).Name;
386   }
387 
388   /// Returns true if the given register is constant.
isConstant(MCRegister RegNo)389   bool isConstant(MCRegister RegNo) const { return get(RegNo).IsConstant; }
390 
391   /// Return the number of registers this target has (useful for
392   /// sizing arrays holding per register information)
getNumRegs()393   unsigned getNumRegs() const {
394     return NumRegs;
395   }
396 
397   /// Return the number of sub-register indices
398   /// understood by the target. Index 0 is reserved for the no-op sub-register,
399   /// while 1 to getNumSubRegIndices() - 1 represent real sub-registers.
getNumSubRegIndices()400   unsigned getNumSubRegIndices() const {
401     return NumSubRegIndices;
402   }
403 
404   /// Return the number of (native) register units in the
405   /// target. Register units are numbered from 0 to getNumRegUnits() - 1. They
406   /// can be accessed through MCRegUnitIterator defined below.
getNumRegUnits()407   unsigned getNumRegUnits() const {
408     return NumRegUnits;
409   }
410 
411   /// Map a target register to an equivalent dwarf register
412   /// number.  Returns -1 if there is no equivalent value.  The second
413   /// parameter allows targets to use different numberings for EH info and
414   /// debugging info.
415   int getDwarfRegNum(MCRegister RegNum, bool isEH) const;
416 
417   /// Map a dwarf register back to a target register. Returns std::nullopt is
418   /// there is no mapping.
419   std::optional<unsigned> getLLVMRegNum(unsigned RegNum, bool isEH) const;
420 
421   /// Map a target EH register number to an equivalent DWARF register
422   /// number.
423   int getDwarfRegNumFromDwarfEHRegNum(unsigned RegNum) const;
424 
425   /// Map a target register to an equivalent SEH register
426   /// number.  Returns LLVM register number if there is no equivalent value.
427   int getSEHRegNum(MCRegister RegNum) const;
428 
429   /// Map a target register to an equivalent CodeView register
430   /// number.
431   int getCodeViewRegNum(MCRegister RegNum) const;
432 
regclass_begin()433   regclass_iterator regclass_begin() const { return Classes; }
regclass_end()434   regclass_iterator regclass_end() const { return Classes+NumClasses; }
regclasses()435   iterator_range<regclass_iterator> regclasses() const {
436     return make_range(regclass_begin(), regclass_end());
437   }
438 
getNumRegClasses()439   unsigned getNumRegClasses() const {
440     return (unsigned)(regclass_end()-regclass_begin());
441   }
442 
443   /// Returns the register class associated with the enumeration
444   /// value.  See class MCOperandInfo.
getRegClass(unsigned i)445   const MCRegisterClass& getRegClass(unsigned i) const {
446     assert(i < getNumRegClasses() && "Register Class ID out of range");
447     return Classes[i];
448   }
449 
getRegClassName(const MCRegisterClass * Class)450   const char *getRegClassName(const MCRegisterClass *Class) const {
451     return RegClassStrings + Class->NameIdx;
452   }
453 
454    /// Returns the encoding for RegNo
getEncodingValue(MCRegister RegNo)455   uint16_t getEncodingValue(MCRegister RegNo) const {
456     assert(RegNo < NumRegs &&
457            "Attempting to get encoding for invalid register number!");
458     return RegEncodingTable[RegNo];
459   }
460 
461   /// Returns true if RegB is a sub-register of RegA.
isSubRegister(MCRegister RegA,MCRegister RegB)462   bool isSubRegister(MCRegister RegA, MCRegister RegB) const {
463     return isSuperRegister(RegB, RegA);
464   }
465 
466   /// Returns true if RegB is a super-register of RegA.
467   bool isSuperRegister(MCRegister RegA, MCRegister RegB) const;
468 
469   /// Returns true if RegB is a sub-register of RegA or if RegB == RegA.
isSubRegisterEq(MCRegister RegA,MCRegister RegB)470   bool isSubRegisterEq(MCRegister RegA, MCRegister RegB) const {
471     return isSuperRegisterEq(RegB, RegA);
472   }
473 
474   /// Returns true if RegB is a super-register of RegA or if
475   /// RegB == RegA.
isSuperRegisterEq(MCRegister RegA,MCRegister RegB)476   bool isSuperRegisterEq(MCRegister RegA, MCRegister RegB) const {
477     return RegA == RegB || isSuperRegister(RegA, RegB);
478   }
479 
480   /// Returns true if RegB is a super-register or sub-register of RegA
481   /// or if RegB == RegA.
isSuperOrSubRegisterEq(MCRegister RegA,MCRegister RegB)482   bool isSuperOrSubRegisterEq(MCRegister RegA, MCRegister RegB) const {
483     return isSubRegisterEq(RegA, RegB) || isSuperRegister(RegA, RegB);
484   }
485 
486   /// Returns true if the two registers are equal or alias each other.
487   bool regsOverlap(MCRegister RegA, MCRegister RegB) const;
488 };
489 
490 //===----------------------------------------------------------------------===//
491 //                          Register List Iterators
492 //===----------------------------------------------------------------------===//
493 
494 // MCRegisterInfo provides lists of super-registers, sub-registers, and
495 // aliasing registers. Use these iterator classes to traverse the lists.
496 
497 /// MCSubRegIterator enumerates all sub-registers of Reg.
498 /// If IncludeSelf is set, Reg itself is included in the list.
499 class MCSubRegIterator
500     : public iterator_adaptor_base<MCSubRegIterator,
501                                    MCRegisterInfo::DiffListIterator,
502                                    std::forward_iterator_tag, const MCPhysReg> {
503   // Cache the current value, so that we can return a reference to it.
504   MCPhysReg Val;
505 
506 public:
507   /// Constructs an end iterator.
508   MCSubRegIterator() = default;
509 
510   MCSubRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI,
511                    bool IncludeSelf = false) {
512     assert(MCRegister::isPhysicalRegister(Reg.id()));
513     I.init(Reg.id(), MCRI->DiffLists + MCRI->get(Reg).SubRegs);
514     // Initially, the iterator points to Reg itself.
515     Val = MCPhysReg(*I);
516     if (!IncludeSelf)
517       ++*this;
518   }
519 
520   const MCPhysReg &operator*() const { return Val; }
521 
522   using iterator_adaptor_base::operator++;
523   MCSubRegIterator &operator++() {
524     Val = MCPhysReg(*++I);
525     return *this;
526   }
527 
528   /// Returns true if this iterator is not yet at the end.
isValid()529   bool isValid() const { return I.isValid(); }
530 };
531 
532 /// Iterator that enumerates the sub-registers of a Reg and the associated
533 /// sub-register indices.
534 class MCSubRegIndexIterator {
535   MCSubRegIterator SRIter;
536   const uint16_t *SRIndex;
537 
538 public:
539   /// Constructs an iterator that traverses subregisters and their
540   /// associated subregister indices.
MCSubRegIndexIterator(MCRegister Reg,const MCRegisterInfo * MCRI)541   MCSubRegIndexIterator(MCRegister Reg, const MCRegisterInfo *MCRI)
542     : SRIter(Reg, MCRI) {
543     SRIndex = MCRI->SubRegIndices + MCRI->get(Reg).SubRegIndices;
544   }
545 
546   /// Returns current sub-register.
getSubReg()547   MCRegister getSubReg() const {
548     return *SRIter;
549   }
550 
551   /// Returns sub-register index of the current sub-register.
getSubRegIndex()552   unsigned getSubRegIndex() const {
553     return *SRIndex;
554   }
555 
556   /// Returns true if this iterator is not yet at the end.
isValid()557   bool isValid() const { return SRIter.isValid(); }
558 
559   /// Moves to the next position.
560   MCSubRegIndexIterator &operator++() {
561     ++SRIter;
562     ++SRIndex;
563     return *this;
564   }
565 };
566 
567 /// MCSuperRegIterator enumerates all super-registers of Reg.
568 /// If IncludeSelf is set, Reg itself is included in the list.
569 class MCSuperRegIterator
570     : public iterator_adaptor_base<MCSuperRegIterator,
571                                    MCRegisterInfo::DiffListIterator,
572                                    std::forward_iterator_tag, const MCPhysReg> {
573   // Cache the current value, so that we can return a reference to it.
574   MCPhysReg Val;
575 
576 public:
577   /// Constructs an end iterator.
578   MCSuperRegIterator() = default;
579 
580   MCSuperRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI,
581                      bool IncludeSelf = false) {
582     assert(MCRegister::isPhysicalRegister(Reg.id()));
583     I.init(Reg.id(), MCRI->DiffLists + MCRI->get(Reg).SuperRegs);
584     // Initially, the iterator points to Reg itself.
585     Val = MCPhysReg(*I);
586     if (!IncludeSelf)
587       ++*this;
588   }
589 
590   const MCPhysReg &operator*() const { return Val; }
591 
592   using iterator_adaptor_base::operator++;
593   MCSuperRegIterator &operator++() {
594     Val = MCPhysReg(*++I);
595     return *this;
596   }
597 
598   /// Returns true if this iterator is not yet at the end.
isValid()599   bool isValid() const { return I.isValid(); }
600 };
601 
602 // Definition for isSuperRegister. Put it down here since it needs the
603 // iterator defined above in addition to the MCRegisterInfo class itself.
isSuperRegister(MCRegister RegA,MCRegister RegB)604 inline bool MCRegisterInfo::isSuperRegister(MCRegister RegA, MCRegister RegB) const{
605   return is_contained(superregs(RegA), RegB);
606 }
607 
608 //===----------------------------------------------------------------------===//
609 //                               Register Units
610 //===----------------------------------------------------------------------===//
611 
612 // MCRegUnitIterator enumerates a list of register units for Reg. The list is
613 // in ascending numerical order.
614 class MCRegUnitIterator
615     : public iterator_adaptor_base<MCRegUnitIterator,
616                                    MCRegisterInfo::DiffListIterator,
617                                    std::forward_iterator_tag, const MCRegUnit> {
618   // The value must be kept in sync with RegisterInfoEmitter.cpp.
619   static constexpr unsigned RegUnitBits = 12;
620   // Cache the current value, so that we can return a reference to it.
621   MCRegUnit Val;
622 
623 public:
624   /// Constructs an end iterator.
625   MCRegUnitIterator() = default;
626 
MCRegUnitIterator(MCRegister Reg,const MCRegisterInfo * MCRI)627   MCRegUnitIterator(MCRegister Reg, const MCRegisterInfo *MCRI) {
628     assert(Reg && "Null register has no regunits");
629     assert(MCRegister::isPhysicalRegister(Reg.id()));
630     // Decode the RegUnits MCRegisterDesc field.
631     unsigned RU = MCRI->get(Reg).RegUnits;
632     unsigned FirstRU = RU & ((1u << RegUnitBits) - 1);
633     unsigned Offset = RU >> RegUnitBits;
634     I.init(FirstRU, MCRI->DiffLists + Offset);
635     Val = MCRegUnit(*I);
636   }
637 
638   const MCRegUnit &operator*() const { return Val; }
639 
640   using iterator_adaptor_base::operator++;
641   MCRegUnitIterator &operator++() {
642     Val = MCRegUnit(*++I);
643     return *this;
644   }
645 
646   /// Returns true if this iterator is not yet at the end.
isValid()647   bool isValid() const { return I.isValid(); }
648 };
649 
650 /// MCRegUnitMaskIterator enumerates a list of register units and their
651 /// associated lane masks for Reg. The register units are in ascending
652 /// numerical order.
653 class MCRegUnitMaskIterator {
654   MCRegUnitIterator RUIter;
655   const LaneBitmask *MaskListIter;
656 
657 public:
658   MCRegUnitMaskIterator() = default;
659 
660   /// Constructs an iterator that traverses the register units and their
661   /// associated LaneMasks in Reg.
MCRegUnitMaskIterator(MCRegister Reg,const MCRegisterInfo * MCRI)662   MCRegUnitMaskIterator(MCRegister Reg, const MCRegisterInfo *MCRI)
663     : RUIter(Reg, MCRI) {
664       uint16_t Idx = MCRI->get(Reg).RegUnitLaneMasks;
665       MaskListIter = &MCRI->RegUnitMaskSequences[Idx];
666   }
667 
668   /// Returns a (RegUnit, LaneMask) pair.
669   std::pair<unsigned,LaneBitmask> operator*() const {
670     return std::make_pair(*RUIter, *MaskListIter);
671   }
672 
673   /// Returns true if this iterator is not yet at the end.
isValid()674   bool isValid() const { return RUIter.isValid(); }
675 
676   /// Moves to the next position.
677   MCRegUnitMaskIterator &operator++() {
678     ++MaskListIter;
679     ++RUIter;
680     return *this;
681   }
682 };
683 
684 // Each register unit has one or two root registers. The complete set of
685 // registers containing a register unit is the union of the roots and their
686 // super-registers. All registers aliasing Unit can be visited like this:
687 //
688 //   for (MCRegUnitRootIterator RI(Unit, MCRI); RI.isValid(); ++RI) {
689 //     for (MCSuperRegIterator SI(*RI, MCRI, true); SI.isValid(); ++SI)
690 //       visit(*SI);
691 //    }
692 
693 /// MCRegUnitRootIterator enumerates the root registers of a register unit.
694 class MCRegUnitRootIterator {
695   uint16_t Reg0 = 0;
696   uint16_t Reg1 = 0;
697 
698 public:
699   MCRegUnitRootIterator() = default;
700 
MCRegUnitRootIterator(unsigned RegUnit,const MCRegisterInfo * MCRI)701   MCRegUnitRootIterator(unsigned RegUnit, const MCRegisterInfo *MCRI) {
702     assert(RegUnit < MCRI->getNumRegUnits() && "Invalid register unit");
703     Reg0 = MCRI->RegUnitRoots[RegUnit][0];
704     Reg1 = MCRI->RegUnitRoots[RegUnit][1];
705   }
706 
707   /// Dereference to get the current root register.
708   unsigned operator*() const {
709     return Reg0;
710   }
711 
712   /// Check if the iterator is at the end of the list.
isValid()713   bool isValid() const {
714     return Reg0;
715   }
716 
717   /// Preincrement to move to the next root register.
718   MCRegUnitRootIterator &operator++() {
719     assert(isValid() && "Cannot move off the end of the list.");
720     Reg0 = Reg1;
721     Reg1 = 0;
722     return *this;
723   }
724 };
725 
726 /// MCRegAliasIterator enumerates all registers aliasing Reg.  If IncludeSelf is
727 /// set, Reg itself is included in the list.  This iterator does not guarantee
728 /// any ordering or that entries are unique.
729 class MCRegAliasIterator {
730 private:
731   MCRegister Reg;
732   const MCRegisterInfo *MCRI;
733   bool IncludeSelf;
734 
735   MCRegUnitIterator RI;
736   MCRegUnitRootIterator RRI;
737   MCSuperRegIterator SI;
738 
739 public:
MCRegAliasIterator(MCRegister Reg,const MCRegisterInfo * MCRI,bool IncludeSelf)740   MCRegAliasIterator(MCRegister Reg, const MCRegisterInfo *MCRI,
741                      bool IncludeSelf)
742     : Reg(Reg), MCRI(MCRI), IncludeSelf(IncludeSelf) {
743     // Initialize the iterators.
744     for (RI = MCRegUnitIterator(Reg, MCRI); RI.isValid(); ++RI) {
745       for (RRI = MCRegUnitRootIterator(*RI, MCRI); RRI.isValid(); ++RRI) {
746         for (SI = MCSuperRegIterator(*RRI, MCRI, true); SI.isValid(); ++SI) {
747           if (!(!IncludeSelf && Reg == *SI))
748             return;
749         }
750       }
751     }
752   }
753 
isValid()754   bool isValid() const { return RI.isValid(); }
755 
756   MCRegister operator*() const {
757     assert(SI.isValid() && "Cannot dereference an invalid iterator.");
758     return *SI;
759   }
760 
advance()761   void advance() {
762     // Assuming SI is valid.
763     ++SI;
764     if (SI.isValid()) return;
765 
766     ++RRI;
767     if (RRI.isValid()) {
768       SI = MCSuperRegIterator(*RRI, MCRI, true);
769       return;
770     }
771 
772     ++RI;
773     if (RI.isValid()) {
774       RRI = MCRegUnitRootIterator(*RI, MCRI);
775       SI = MCSuperRegIterator(*RRI, MCRI, true);
776     }
777   }
778 
779   MCRegAliasIterator &operator++() {
780     assert(isValid() && "Cannot move off the end of the list.");
781     do advance();
782     while (!IncludeSelf && isValid() && *SI == Reg);
783     return *this;
784   }
785 };
786 
787 inline iterator_range<MCSubRegIterator>
subregs(MCRegister Reg)788 MCRegisterInfo::subregs(MCRegister Reg) const {
789   return make_range({Reg, this, /*IncludeSelf=*/false}, MCSubRegIterator());
790 }
791 
792 inline iterator_range<MCSubRegIterator>
subregs_inclusive(MCRegister Reg)793 MCRegisterInfo::subregs_inclusive(MCRegister Reg) const {
794   return make_range({Reg, this, /*IncludeSelf=*/true}, MCSubRegIterator());
795 }
796 
797 inline iterator_range<MCSuperRegIterator>
superregs(MCRegister Reg)798 MCRegisterInfo::superregs(MCRegister Reg) const {
799   return make_range({Reg, this, /*IncludeSelf=*/false}, MCSuperRegIterator());
800 }
801 
802 inline iterator_range<MCSuperRegIterator>
superregs_inclusive(MCRegister Reg)803 MCRegisterInfo::superregs_inclusive(MCRegister Reg) const {
804   return make_range({Reg, this, /*IncludeSelf=*/true}, MCSuperRegIterator());
805 }
806 
807 inline detail::concat_range<const MCPhysReg, iterator_range<MCSubRegIterator>,
808                             iterator_range<MCSuperRegIterator>>
sub_and_superregs_inclusive(MCRegister Reg)809 MCRegisterInfo::sub_and_superregs_inclusive(MCRegister Reg) const {
810   return concat<const MCPhysReg>(subregs_inclusive(Reg), superregs(Reg));
811 }
812 
813 inline iterator_range<MCRegUnitIterator>
regunits(MCRegister Reg)814 MCRegisterInfo::regunits(MCRegister Reg) const {
815   return make_range({Reg, this}, MCRegUnitIterator());
816 }
817 
818 } // end namespace llvm
819 
820 #endif // LLVM_MC_MCREGISTERINFO_H
821