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 /// 53 unsigned getID() const { return ID; } 54 55 /// begin/end - Return all of the registers in this class. 56 /// 57 iterator begin() const { return RegsBegin; } 58 iterator end() const { return RegsBegin + RegsSize; } 59 60 /// getNumRegs - Return the number of registers in this class. 61 /// 62 unsigned getNumRegs() const { return RegsSize; } 63 64 /// getRegister - Return the specified register in the class. 65 /// 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. 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. 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. 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. 96 int getCopyCost() const { return CopyCost; } 97 98 /// isAllocatable - Return true if this register class may be used to create 99 /// virtual registers. 100 bool isAllocatable() const { return Allocatable; } 101 102 /// Return true if this register class has a defined BaseClassOrder. 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 mutable std::vector<std::vector<MCPhysReg>> RegAliasesCache; 191 ArrayRef<MCPhysReg> getCachedAliasesOf(MCPhysReg R) const; 192 193 /// Iterator class that can traverse the differentially encoded values in 194 /// DiffLists. Don't use this class directly, use one of the adaptors below. 195 class DiffListIterator 196 : public iterator_facade_base<DiffListIterator, std::forward_iterator_tag, 197 unsigned> { 198 unsigned Val = 0; 199 const int16_t *List = nullptr; 200 201 public: 202 /// Constructs an invalid iterator, which is also the end iterator. 203 /// Call init() to point to something useful. 204 DiffListIterator() = default; 205 206 /// Point the iterator to InitVal, decoding subsequent values from DiffList. 207 void init(unsigned InitVal, const int16_t *DiffList) { 208 Val = InitVal; 209 List = DiffList; 210 } 211 212 /// Returns true if this iterator is not yet at the end. 213 bool isValid() const { return List; } 214 215 /// Dereference the iterator to get the value at the current position. 216 const unsigned &operator*() const { return Val; } 217 218 using DiffListIterator::iterator_facade_base::operator++; 219 /// Pre-increment to move to the next position. 220 DiffListIterator &operator++() { 221 assert(isValid() && "Cannot move off the end of the list."); 222 int16_t D = *List++; 223 Val += D; 224 // The end of the list is encoded as a 0 differential. 225 if (!D) 226 List = nullptr; 227 return *this; 228 } 229 230 bool operator==(const DiffListIterator &Other) const { 231 return List == Other.List; 232 } 233 }; 234 235 public: 236 /// Return an iterator range over all sub-registers of \p Reg, excluding \p 237 /// Reg. 238 iterator_range<MCSubRegIterator> subregs(MCRegister Reg) const; 239 240 /// Return an iterator range over all sub-registers of \p Reg, including \p 241 /// Reg. 242 iterator_range<MCSubRegIterator> subregs_inclusive(MCRegister Reg) const; 243 244 /// Return an iterator range over all super-registers of \p Reg, excluding \p 245 /// Reg. 246 iterator_range<MCSuperRegIterator> superregs(MCRegister Reg) const; 247 248 /// Return an iterator range over all super-registers of \p Reg, including \p 249 /// Reg. 250 iterator_range<MCSuperRegIterator> superregs_inclusive(MCRegister Reg) const; 251 252 /// Return an iterator range over all sub- and super-registers of \p Reg, 253 /// including \p Reg. 254 detail::concat_range<const MCPhysReg, iterator_range<MCSubRegIterator>, 255 iterator_range<MCSuperRegIterator>> 256 sub_and_superregs_inclusive(MCRegister Reg) const; 257 258 /// Returns an iterator range over all regunits for \p Reg. 259 iterator_range<MCRegUnitIterator> regunits(MCRegister Reg) const; 260 261 // These iterators are allowed to sub-class DiffListIterator and access 262 // internal list pointers. 263 friend class MCSubRegIterator; 264 friend class MCSubRegIndexIterator; 265 friend class MCSuperRegIterator; 266 friend class MCRegUnitIterator; 267 friend class MCRegUnitMaskIterator; 268 friend class MCRegUnitRootIterator; 269 friend class MCRegAliasIterator; 270 271 /// Initialize MCRegisterInfo, called by TableGen 272 /// auto-generated routines. *DO NOT USE*. 273 void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA, 274 unsigned PC, const MCRegisterClass *C, unsigned NC, 275 const MCPhysReg (*RURoots)[2], unsigned NRU, 276 const int16_t *DL, const LaneBitmask *RUMS, 277 const char *Strings, const char *ClassStrings, 278 const uint16_t *SubIndices, unsigned NumIndices, 279 const uint16_t *RET) { 280 Desc = D; 281 NumRegs = NR; 282 RAReg = RA; 283 PCReg = PC; 284 Classes = C; 285 DiffLists = DL; 286 RegUnitMaskSequences = RUMS; 287 RegStrings = Strings; 288 RegClassStrings = ClassStrings; 289 NumClasses = NC; 290 RegUnitRoots = RURoots; 291 NumRegUnits = NRU; 292 SubRegIndices = SubIndices; 293 NumSubRegIndices = NumIndices; 294 RegEncodingTable = RET; 295 296 // Initialize DWARF register mapping variables 297 EHL2DwarfRegs = nullptr; 298 EHL2DwarfRegsSize = 0; 299 L2DwarfRegs = nullptr; 300 L2DwarfRegsSize = 0; 301 EHDwarf2LRegs = nullptr; 302 EHDwarf2LRegsSize = 0; 303 Dwarf2LRegs = nullptr; 304 Dwarf2LRegsSize = 0; 305 306 RegAliasesCache.resize(NumRegs); 307 } 308 309 /// Used to initialize LLVM register to Dwarf 310 /// register number mapping. Called by TableGen auto-generated routines. 311 /// *DO NOT USE*. 312 void mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair *Map, unsigned Size, 313 bool isEH) { 314 if (isEH) { 315 EHL2DwarfRegs = Map; 316 EHL2DwarfRegsSize = Size; 317 } else { 318 L2DwarfRegs = Map; 319 L2DwarfRegsSize = Size; 320 } 321 } 322 323 /// Used to initialize Dwarf register to LLVM 324 /// register number mapping. Called by TableGen auto-generated routines. 325 /// *DO NOT USE*. 326 void mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair *Map, unsigned Size, 327 bool isEH) { 328 if (isEH) { 329 EHDwarf2LRegs = Map; 330 EHDwarf2LRegsSize = Size; 331 } else { 332 Dwarf2LRegs = Map; 333 Dwarf2LRegsSize = Size; 334 } 335 } 336 337 /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register 338 /// number mapping. By default the SEH register number is just the same 339 /// as the LLVM register number. 340 /// FIXME: TableGen these numbers. Currently this requires target specific 341 /// initialization code. 342 void mapLLVMRegToSEHReg(MCRegister LLVMReg, int SEHReg) { 343 L2SEHRegs[LLVMReg] = SEHReg; 344 } 345 346 void mapLLVMRegToCVReg(MCRegister LLVMReg, int CVReg) { 347 L2CVRegs[LLVMReg] = CVReg; 348 } 349 350 /// This method should return the register where the return 351 /// address can be found. 352 MCRegister getRARegister() const { 353 return RAReg; 354 } 355 356 /// Return the register which is the program counter. 357 MCRegister getProgramCounter() const { 358 return PCReg; 359 } 360 361 const MCRegisterDesc &operator[](MCRegister RegNo) const { 362 assert(RegNo < NumRegs && 363 "Attempting to access record for invalid register number!"); 364 return Desc[RegNo]; 365 } 366 367 /// Provide a get method, equivalent to [], but more useful with a 368 /// pointer to this object. 369 const MCRegisterDesc &get(MCRegister RegNo) const { 370 return operator[](RegNo); 371 } 372 373 /// Returns the physical register number of sub-register "Index" 374 /// for physical register RegNo. Return zero if the sub-register does not 375 /// exist. 376 MCRegister getSubReg(MCRegister Reg, unsigned Idx) const; 377 378 /// Return a super-register of the specified register 379 /// Reg so its sub-register of index SubIdx is Reg. 380 MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx, 381 const MCRegisterClass *RC) const; 382 383 /// For a given register pair, return the sub-register index 384 /// if the second register is a sub-register of the first. Return zero 385 /// otherwise. 386 unsigned getSubRegIndex(MCRegister RegNo, MCRegister SubRegNo) const; 387 388 /// Return the human-readable symbolic target-specific name for the 389 /// specified physical register. 390 const char *getName(MCRegister RegNo) const { 391 return RegStrings + get(RegNo).Name; 392 } 393 394 /// Returns true if the given register is constant. 395 bool isConstant(MCRegister RegNo) const { return get(RegNo).IsConstant; } 396 397 /// Return the number of registers this target has (useful for 398 /// sizing arrays holding per register information) 399 unsigned getNumRegs() const { 400 return NumRegs; 401 } 402 403 /// Return the number of sub-register indices 404 /// understood by the target. Index 0 is reserved for the no-op sub-register, 405 /// while 1 to getNumSubRegIndices() - 1 represent real sub-registers. 406 unsigned getNumSubRegIndices() const { 407 return NumSubRegIndices; 408 } 409 410 /// Return the number of (native) register units in the 411 /// target. Register units are numbered from 0 to getNumRegUnits() - 1. They 412 /// can be accessed through MCRegUnitIterator defined below. 413 unsigned getNumRegUnits() const { 414 return NumRegUnits; 415 } 416 417 /// Map a target register to an equivalent dwarf register 418 /// number. Returns -1 if there is no equivalent value. The second 419 /// parameter allows targets to use different numberings for EH info and 420 /// debugging info. 421 int getDwarfRegNum(MCRegister RegNum, bool isEH) const; 422 423 /// Map a dwarf register back to a target register. Returns std::nullopt is 424 /// there is no mapping. 425 std::optional<unsigned> getLLVMRegNum(unsigned RegNum, bool isEH) const; 426 427 /// Map a target EH register number to an equivalent DWARF register 428 /// number. 429 int getDwarfRegNumFromDwarfEHRegNum(unsigned RegNum) const; 430 431 /// Map a target register to an equivalent SEH register 432 /// number. Returns LLVM register number if there is no equivalent value. 433 int getSEHRegNum(MCRegister RegNum) const; 434 435 /// Map a target register to an equivalent CodeView register 436 /// number. 437 int getCodeViewRegNum(MCRegister RegNum) const; 438 439 regclass_iterator regclass_begin() const { return Classes; } 440 regclass_iterator regclass_end() const { return Classes+NumClasses; } 441 iterator_range<regclass_iterator> regclasses() const { 442 return make_range(regclass_begin(), regclass_end()); 443 } 444 445 unsigned getNumRegClasses() const { 446 return (unsigned)(regclass_end()-regclass_begin()); 447 } 448 449 /// Returns the register class associated with the enumeration 450 /// value. See class MCOperandInfo. 451 const MCRegisterClass& getRegClass(unsigned i) const { 452 assert(i < getNumRegClasses() && "Register Class ID out of range"); 453 return Classes[i]; 454 } 455 456 const char *getRegClassName(const MCRegisterClass *Class) const { 457 return RegClassStrings + Class->NameIdx; 458 } 459 460 /// Returns the encoding for RegNo 461 uint16_t getEncodingValue(MCRegister RegNo) const { 462 assert(RegNo < NumRegs && 463 "Attempting to get encoding for invalid register number!"); 464 return RegEncodingTable[RegNo]; 465 } 466 467 /// Returns true if RegB is a sub-register of RegA. 468 bool isSubRegister(MCRegister RegA, MCRegister RegB) const { 469 return isSuperRegister(RegB, RegA); 470 } 471 472 /// Returns true if RegB is a super-register of RegA. 473 bool isSuperRegister(MCRegister RegA, MCRegister RegB) const; 474 475 /// Returns true if RegB is a sub-register of RegA or if RegB == RegA. 476 bool isSubRegisterEq(MCRegister RegA, MCRegister RegB) const { 477 return isSuperRegisterEq(RegB, RegA); 478 } 479 480 /// Returns true if RegB is a super-register of RegA or if 481 /// RegB == RegA. 482 bool isSuperRegisterEq(MCRegister RegA, MCRegister RegB) const { 483 return RegA == RegB || isSuperRegister(RegA, RegB); 484 } 485 486 /// Returns true if RegB is a super-register or sub-register of RegA 487 /// or if RegB == RegA. 488 bool isSuperOrSubRegisterEq(MCRegister RegA, MCRegister RegB) const { 489 return isSubRegisterEq(RegA, RegB) || isSuperRegister(RegA, RegB); 490 } 491 492 /// Returns true if the two registers are equal or alias each other. 493 bool regsOverlap(MCRegister RegA, MCRegister RegB) const; 494 }; 495 496 //===----------------------------------------------------------------------===// 497 // Register List Iterators 498 //===----------------------------------------------------------------------===// 499 500 // MCRegisterInfo provides lists of super-registers, sub-registers, and 501 // aliasing registers. Use these iterator classes to traverse the lists. 502 503 /// MCSubRegIterator enumerates all sub-registers of Reg. 504 /// If IncludeSelf is set, Reg itself is included in the list. 505 class MCSubRegIterator 506 : public iterator_adaptor_base<MCSubRegIterator, 507 MCRegisterInfo::DiffListIterator, 508 std::forward_iterator_tag, const MCPhysReg> { 509 // Cache the current value, so that we can return a reference to it. 510 MCPhysReg Val; 511 512 public: 513 /// Constructs an end iterator. 514 MCSubRegIterator() = default; 515 516 MCSubRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI, 517 bool IncludeSelf = false) { 518 assert(MCRegister::isPhysicalRegister(Reg.id())); 519 I.init(Reg.id(), MCRI->DiffLists + MCRI->get(Reg).SubRegs); 520 // Initially, the iterator points to Reg itself. 521 Val = MCPhysReg(*I); 522 if (!IncludeSelf) 523 ++*this; 524 } 525 526 const MCPhysReg &operator*() const { return Val; } 527 528 using iterator_adaptor_base::operator++; 529 MCSubRegIterator &operator++() { 530 Val = MCPhysReg(*++I); 531 return *this; 532 } 533 534 /// Returns true if this iterator is not yet at the end. 535 bool isValid() const { return I.isValid(); } 536 }; 537 538 /// Iterator that enumerates the sub-registers of a Reg and the associated 539 /// sub-register indices. 540 class MCSubRegIndexIterator { 541 MCSubRegIterator SRIter; 542 const uint16_t *SRIndex; 543 544 public: 545 /// Constructs an iterator that traverses subregisters and their 546 /// associated subregister indices. 547 MCSubRegIndexIterator(MCRegister Reg, const MCRegisterInfo *MCRI) 548 : SRIter(Reg, MCRI) { 549 SRIndex = MCRI->SubRegIndices + MCRI->get(Reg).SubRegIndices; 550 } 551 552 /// Returns current sub-register. 553 MCRegister getSubReg() const { 554 return *SRIter; 555 } 556 557 /// Returns sub-register index of the current sub-register. 558 unsigned getSubRegIndex() const { 559 return *SRIndex; 560 } 561 562 /// Returns true if this iterator is not yet at the end. 563 bool isValid() const { return SRIter.isValid(); } 564 565 /// Moves to the next position. 566 MCSubRegIndexIterator &operator++() { 567 ++SRIter; 568 ++SRIndex; 569 return *this; 570 } 571 }; 572 573 /// MCSuperRegIterator enumerates all super-registers of Reg. 574 /// If IncludeSelf is set, Reg itself is included in the list. 575 class MCSuperRegIterator 576 : public iterator_adaptor_base<MCSuperRegIterator, 577 MCRegisterInfo::DiffListIterator, 578 std::forward_iterator_tag, const MCPhysReg> { 579 // Cache the current value, so that we can return a reference to it. 580 MCPhysReg Val; 581 582 public: 583 /// Constructs an end iterator. 584 MCSuperRegIterator() = default; 585 586 MCSuperRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI, 587 bool IncludeSelf = false) { 588 assert(MCRegister::isPhysicalRegister(Reg.id())); 589 I.init(Reg.id(), MCRI->DiffLists + MCRI->get(Reg).SuperRegs); 590 // Initially, the iterator points to Reg itself. 591 Val = MCPhysReg(*I); 592 if (!IncludeSelf) 593 ++*this; 594 } 595 596 const MCPhysReg &operator*() const { return Val; } 597 598 using iterator_adaptor_base::operator++; 599 MCSuperRegIterator &operator++() { 600 Val = MCPhysReg(*++I); 601 return *this; 602 } 603 604 /// Returns true if this iterator is not yet at the end. 605 bool isValid() const { return I.isValid(); } 606 }; 607 608 // Definition for isSuperRegister. Put it down here since it needs the 609 // iterator defined above in addition to the MCRegisterInfo class itself. 610 inline bool MCRegisterInfo::isSuperRegister(MCRegister RegA, MCRegister RegB) const{ 611 return is_contained(superregs(RegA), RegB); 612 } 613 614 //===----------------------------------------------------------------------===// 615 // Register Units 616 //===----------------------------------------------------------------------===// 617 618 // MCRegUnitIterator enumerates a list of register units for Reg. The list is 619 // in ascending numerical order. 620 class MCRegUnitIterator 621 : public iterator_adaptor_base<MCRegUnitIterator, 622 MCRegisterInfo::DiffListIterator, 623 std::forward_iterator_tag, const MCRegUnit> { 624 // The value must be kept in sync with RegisterInfoEmitter.cpp. 625 static constexpr unsigned RegUnitBits = 12; 626 // Cache the current value, so that we can return a reference to it. 627 MCRegUnit Val; 628 629 public: 630 /// Constructs an end iterator. 631 MCRegUnitIterator() = default; 632 633 MCRegUnitIterator(MCRegister Reg, const MCRegisterInfo *MCRI) { 634 assert(Reg && "Null register has no regunits"); 635 assert(MCRegister::isPhysicalRegister(Reg.id())); 636 // Decode the RegUnits MCRegisterDesc field. 637 unsigned RU = MCRI->get(Reg).RegUnits; 638 unsigned FirstRU = RU & ((1u << RegUnitBits) - 1); 639 unsigned Offset = RU >> RegUnitBits; 640 I.init(FirstRU, MCRI->DiffLists + Offset); 641 Val = MCRegUnit(*I); 642 } 643 644 const MCRegUnit &operator*() const { return Val; } 645 646 using iterator_adaptor_base::operator++; 647 MCRegUnitIterator &operator++() { 648 Val = MCRegUnit(*++I); 649 return *this; 650 } 651 652 /// Returns true if this iterator is not yet at the end. 653 bool isValid() const { return I.isValid(); } 654 }; 655 656 /// MCRegUnitMaskIterator enumerates a list of register units and their 657 /// associated lane masks for Reg. The register units are in ascending 658 /// numerical order. 659 class MCRegUnitMaskIterator { 660 MCRegUnitIterator RUIter; 661 const LaneBitmask *MaskListIter; 662 663 public: 664 MCRegUnitMaskIterator() = default; 665 666 /// Constructs an iterator that traverses the register units and their 667 /// associated LaneMasks in Reg. 668 MCRegUnitMaskIterator(MCRegister Reg, const MCRegisterInfo *MCRI) 669 : RUIter(Reg, MCRI) { 670 uint16_t Idx = MCRI->get(Reg).RegUnitLaneMasks; 671 MaskListIter = &MCRI->RegUnitMaskSequences[Idx]; 672 } 673 674 /// Returns a (RegUnit, LaneMask) pair. 675 std::pair<unsigned,LaneBitmask> operator*() const { 676 return std::make_pair(*RUIter, *MaskListIter); 677 } 678 679 /// Returns true if this iterator is not yet at the end. 680 bool isValid() const { return RUIter.isValid(); } 681 682 /// Moves to the next position. 683 MCRegUnitMaskIterator &operator++() { 684 ++MaskListIter; 685 ++RUIter; 686 return *this; 687 } 688 }; 689 690 // Each register unit has one or two root registers. The complete set of 691 // registers containing a register unit is the union of the roots and their 692 // super-registers. All registers aliasing Unit can be visited like this: 693 // 694 // for (MCRegUnitRootIterator RI(Unit, MCRI); RI.isValid(); ++RI) { 695 // for (MCSuperRegIterator SI(*RI, MCRI, true); SI.isValid(); ++SI) 696 // visit(*SI); 697 // } 698 699 /// MCRegUnitRootIterator enumerates the root registers of a register unit. 700 class MCRegUnitRootIterator { 701 uint16_t Reg0 = 0; 702 uint16_t Reg1 = 0; 703 704 public: 705 MCRegUnitRootIterator() = default; 706 707 MCRegUnitRootIterator(unsigned RegUnit, const MCRegisterInfo *MCRI) { 708 assert(RegUnit < MCRI->getNumRegUnits() && "Invalid register unit"); 709 Reg0 = MCRI->RegUnitRoots[RegUnit][0]; 710 Reg1 = MCRI->RegUnitRoots[RegUnit][1]; 711 } 712 713 /// Dereference to get the current root register. 714 unsigned operator*() const { 715 return Reg0; 716 } 717 718 /// Check if the iterator is at the end of the list. 719 bool isValid() const { 720 return Reg0; 721 } 722 723 /// Preincrement to move to the next root register. 724 MCRegUnitRootIterator &operator++() { 725 assert(isValid() && "Cannot move off the end of the list."); 726 Reg0 = Reg1; 727 Reg1 = 0; 728 return *this; 729 } 730 }; 731 732 /// MCRegAliasIterator enumerates all registers aliasing Reg. 733 class MCRegAliasIterator { 734 private: 735 const MCPhysReg *It = nullptr; 736 const MCPhysReg *End = nullptr; 737 738 public: 739 MCRegAliasIterator(MCRegister Reg, const MCRegisterInfo *MCRI, 740 bool IncludeSelf) { 741 ArrayRef<MCPhysReg> Cache = MCRI->getCachedAliasesOf(Reg); 742 assert(Cache.back() == Reg); 743 It = Cache.begin(); 744 End = Cache.end(); 745 if (!IncludeSelf) 746 --End; 747 } 748 749 bool isValid() const { return It != End; } 750 751 MCRegister operator*() const { return *It; } 752 753 MCRegAliasIterator &operator++() { 754 assert(isValid() && "Cannot move off the end of the list."); 755 ++It; 756 return *this; 757 } 758 }; 759 760 inline iterator_range<MCSubRegIterator> 761 MCRegisterInfo::subregs(MCRegister Reg) const { 762 return make_range({Reg, this, /*IncludeSelf=*/false}, MCSubRegIterator()); 763 } 764 765 inline iterator_range<MCSubRegIterator> 766 MCRegisterInfo::subregs_inclusive(MCRegister Reg) const { 767 return make_range({Reg, this, /*IncludeSelf=*/true}, MCSubRegIterator()); 768 } 769 770 inline iterator_range<MCSuperRegIterator> 771 MCRegisterInfo::superregs(MCRegister Reg) const { 772 return make_range({Reg, this, /*IncludeSelf=*/false}, MCSuperRegIterator()); 773 } 774 775 inline iterator_range<MCSuperRegIterator> 776 MCRegisterInfo::superregs_inclusive(MCRegister Reg) const { 777 return make_range({Reg, this, /*IncludeSelf=*/true}, MCSuperRegIterator()); 778 } 779 780 inline detail::concat_range<const MCPhysReg, iterator_range<MCSubRegIterator>, 781 iterator_range<MCSuperRegIterator>> 782 MCRegisterInfo::sub_and_superregs_inclusive(MCRegister Reg) const { 783 return concat<const MCPhysReg>(subregs_inclusive(Reg), superregs(Reg)); 784 } 785 786 inline iterator_range<MCRegUnitIterator> 787 MCRegisterInfo::regunits(MCRegister Reg) const { 788 return make_range({Reg, this}, MCRegUnitIterator()); 789 } 790 791 } // end namespace llvm 792 793 #endif // LLVM_MC_MCREGISTERINFO_H 794