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