1 //==- CodeGen/TargetRegisterInfo.h - Target Register Information -*- 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_CODEGEN_TARGETREGISTERINFO_H 16 #define LLVM_CODEGEN_TARGETREGISTERINFO_H 17 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/iterator_range.h" 22 #include "llvm/CodeGen/MachineBasicBlock.h" 23 #include "llvm/IR/CallingConv.h" 24 #include "llvm/MC/LaneBitmask.h" 25 #include "llvm/MC/MCRegisterInfo.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Support/MachineValueType.h" 28 #include "llvm/Support/MathExtras.h" 29 #include "llvm/Support/Printable.h" 30 #include <cassert> 31 #include <cstdint> 32 #include <functional> 33 34 namespace llvm { 35 36 class BitVector; 37 class LiveRegMatrix; 38 class MachineFunction; 39 class MachineInstr; 40 class RegScavenger; 41 class VirtRegMap; 42 class LiveIntervals; 43 class LiveInterval; 44 45 class TargetRegisterClass { 46 public: 47 using iterator = const MCPhysReg *; 48 using const_iterator = const MCPhysReg *; 49 using sc_iterator = const TargetRegisterClass* const *; 50 51 // Instance variables filled by tablegen, do not use! 52 const MCRegisterClass *MC; 53 const uint32_t *SubClassMask; 54 const uint16_t *SuperRegIndices; 55 const LaneBitmask LaneMask; 56 /// Classes with a higher priority value are assigned first by register 57 /// allocators using a greedy heuristic. The value is in the range [0,63]. 58 const uint8_t AllocationPriority; 59 /// Whether the class supports two (or more) disjunct subregister indices. 60 const bool HasDisjunctSubRegs; 61 /// Whether a combination of subregisters can cover every register in the 62 /// class. See also the CoveredBySubRegs description in Target.td. 63 const bool CoveredBySubRegs; 64 const sc_iterator SuperClasses; 65 ArrayRef<MCPhysReg> (*OrderFunc)(const MachineFunction&); 66 67 /// Return the register class ID number. getID()68 unsigned getID() const { return MC->getID(); } 69 70 /// begin/end - Return all of the registers in this class. 71 /// begin()72 iterator begin() const { return MC->begin(); } end()73 iterator end() const { return MC->end(); } 74 75 /// Return the number of registers in this class. getNumRegs()76 unsigned getNumRegs() const { return MC->getNumRegs(); } 77 78 iterator_range<SmallVectorImpl<MCPhysReg>::const_iterator> getRegisters()79 getRegisters() const { 80 return make_range(MC->begin(), MC->end()); 81 } 82 83 /// Return the specified register in the class. getRegister(unsigned i)84 MCRegister getRegister(unsigned i) const { 85 return MC->getRegister(i); 86 } 87 88 /// Return true if the specified register is included in this register class. 89 /// This does not include virtual registers. contains(Register Reg)90 bool contains(Register Reg) const { 91 /// FIXME: Historically this function has returned false when given vregs 92 /// but it should probably only receive physical registers 93 if (!Reg.isPhysical()) 94 return false; 95 return MC->contains(Reg.asMCReg()); 96 } 97 98 /// Return true if both registers are in this class. contains(Register Reg1,Register Reg2)99 bool contains(Register Reg1, Register Reg2) const { 100 /// FIXME: Historically this function has returned false when given a vregs 101 /// but it should probably only receive physical registers 102 if (!Reg1.isPhysical() || !Reg2.isPhysical()) 103 return false; 104 return MC->contains(Reg1.asMCReg(), Reg2.asMCReg()); 105 } 106 107 /// Return the cost of copying a value between two registers in this class. 108 /// A negative number means the register class is very expensive 109 /// to copy e.g. status flag register classes. getCopyCost()110 int getCopyCost() const { return MC->getCopyCost(); } 111 112 /// Return true if this register class may be used to create virtual 113 /// registers. isAllocatable()114 bool isAllocatable() const { return MC->isAllocatable(); } 115 116 /// Return true if the specified TargetRegisterClass 117 /// is a proper sub-class of this TargetRegisterClass. hasSubClass(const TargetRegisterClass * RC)118 bool hasSubClass(const TargetRegisterClass *RC) const { 119 return RC != this && hasSubClassEq(RC); 120 } 121 122 /// Returns true if RC is a sub-class of or equal to this class. hasSubClassEq(const TargetRegisterClass * RC)123 bool hasSubClassEq(const TargetRegisterClass *RC) const { 124 unsigned ID = RC->getID(); 125 return (SubClassMask[ID / 32] >> (ID % 32)) & 1; 126 } 127 128 /// Return true if the specified TargetRegisterClass is a 129 /// proper super-class of this TargetRegisterClass. hasSuperClass(const TargetRegisterClass * RC)130 bool hasSuperClass(const TargetRegisterClass *RC) const { 131 return RC->hasSubClass(this); 132 } 133 134 /// Returns true if RC is a super-class of or equal to this class. hasSuperClassEq(const TargetRegisterClass * RC)135 bool hasSuperClassEq(const TargetRegisterClass *RC) const { 136 return RC->hasSubClassEq(this); 137 } 138 139 /// Returns a bit vector of subclasses, including this one. 140 /// The vector is indexed by class IDs. 141 /// 142 /// To use it, consider the returned array as a chunk of memory that 143 /// contains an array of bits of size NumRegClasses. Each 32-bit chunk 144 /// contains a bitset of the ID of the subclasses in big-endian style. 145 146 /// I.e., the representation of the memory from left to right at the 147 /// bit level looks like: 148 /// [31 30 ... 1 0] [ 63 62 ... 33 32] ... 149 /// [ XXX NumRegClasses NumRegClasses - 1 ... ] 150 /// Where the number represents the class ID and XXX bits that 151 /// should be ignored. 152 /// 153 /// See the implementation of hasSubClassEq for an example of how it 154 /// can be used. getSubClassMask()155 const uint32_t *getSubClassMask() const { 156 return SubClassMask; 157 } 158 159 /// Returns a 0-terminated list of sub-register indices that project some 160 /// super-register class into this register class. The list has an entry for 161 /// each Idx such that: 162 /// 163 /// There exists SuperRC where: 164 /// For all Reg in SuperRC: 165 /// this->contains(Reg:Idx) getSuperRegIndices()166 const uint16_t *getSuperRegIndices() const { 167 return SuperRegIndices; 168 } 169 170 /// Returns a NULL-terminated list of super-classes. The 171 /// classes are ordered by ID which is also a topological ordering from large 172 /// to small classes. The list does NOT include the current class. getSuperClasses()173 sc_iterator getSuperClasses() const { 174 return SuperClasses; 175 } 176 177 /// Return true if this TargetRegisterClass is a subset 178 /// class of at least one other TargetRegisterClass. isASubClass()179 bool isASubClass() const { 180 return SuperClasses[0] != nullptr; 181 } 182 183 /// Returns the preferred order for allocating registers from this register 184 /// class in MF. The raw order comes directly from the .td file and may 185 /// include reserved registers that are not allocatable. 186 /// Register allocators should also make sure to allocate 187 /// callee-saved registers only after all the volatiles are used. The 188 /// RegisterClassInfo class provides filtered allocation orders with 189 /// callee-saved registers moved to the end. 190 /// 191 /// The MachineFunction argument can be used to tune the allocatable 192 /// registers based on the characteristics of the function, subtarget, or 193 /// other criteria. 194 /// 195 /// By default, this method returns all registers in the class. getRawAllocationOrder(const MachineFunction & MF)196 ArrayRef<MCPhysReg> getRawAllocationOrder(const MachineFunction &MF) const { 197 return OrderFunc ? OrderFunc(MF) : makeArrayRef(begin(), getNumRegs()); 198 } 199 200 /// Returns the combination of all lane masks of register in this class. 201 /// The lane masks of the registers are the combination of all lane masks 202 /// of their subregisters. Returns 1 if there are no subregisters. getLaneMask()203 LaneBitmask getLaneMask() const { 204 return LaneMask; 205 } 206 }; 207 208 /// Extra information, not in MCRegisterDesc, about registers. 209 /// These are used by codegen, not by MC. 210 struct TargetRegisterInfoDesc { 211 unsigned CostPerUse; // Extra cost of instructions using register. 212 bool inAllocatableClass; // Register belongs to an allocatable regclass. 213 }; 214 215 /// Each TargetRegisterClass has a per register weight, and weight 216 /// limit which must be less than the limits of its pressure sets. 217 struct RegClassWeight { 218 unsigned RegWeight; 219 unsigned WeightLimit; 220 }; 221 222 /// TargetRegisterInfo base class - We assume that the target defines a static 223 /// array of TargetRegisterDesc objects that represent all of the machine 224 /// registers that the target has. As such, we simply have to track a pointer 225 /// to this array so that we can turn register number into a register 226 /// descriptor. 227 /// 228 class TargetRegisterInfo : public MCRegisterInfo { 229 public: 230 using regclass_iterator = const TargetRegisterClass * const *; 231 using vt_iterator = const MVT::SimpleValueType *; 232 struct RegClassInfo { 233 unsigned RegSize, SpillSize, SpillAlignment; 234 vt_iterator VTList; 235 }; 236 private: 237 const TargetRegisterInfoDesc *InfoDesc; // Extra desc array for codegen 238 const char *const *SubRegIndexNames; // Names of subreg indexes. 239 // Pointer to array of lane masks, one per sub-reg index. 240 const LaneBitmask *SubRegIndexLaneMasks; 241 242 regclass_iterator RegClassBegin, RegClassEnd; // List of regclasses 243 LaneBitmask CoveringLanes; 244 const RegClassInfo *const RCInfos; 245 unsigned HwMode; 246 247 protected: 248 TargetRegisterInfo(const TargetRegisterInfoDesc *ID, 249 regclass_iterator RCB, 250 regclass_iterator RCE, 251 const char *const *SRINames, 252 const LaneBitmask *SRILaneMasks, 253 LaneBitmask CoveringLanes, 254 const RegClassInfo *const RCIs, 255 unsigned Mode = 0); 256 virtual ~TargetRegisterInfo(); 257 258 public: 259 // Register numbers can represent physical registers, virtual registers, and 260 // sometimes stack slots. The unsigned values are divided into these ranges: 261 // 262 // 0 Not a register, can be used as a sentinel. 263 // [1;2^30) Physical registers assigned by TableGen. 264 // [2^30;2^31) Stack slots. (Rarely used.) 265 // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo. 266 // 267 // Further sentinels can be allocated from the small negative integers. 268 // DenseMapInfo<unsigned> uses -1u and -2u. 269 270 /// Return the size in bits of a register from class RC. getRegSizeInBits(const TargetRegisterClass & RC)271 unsigned getRegSizeInBits(const TargetRegisterClass &RC) const { 272 return getRegClassInfo(RC).RegSize; 273 } 274 275 /// Return the size in bytes of the stack slot allocated to hold a spilled 276 /// copy of a register from class RC. getSpillSize(const TargetRegisterClass & RC)277 unsigned getSpillSize(const TargetRegisterClass &RC) const { 278 return getRegClassInfo(RC).SpillSize / 8; 279 } 280 281 /// Return the minimum required alignment in bytes for a spill slot for 282 /// a register of this class. getSpillAlignment(const TargetRegisterClass & RC)283 unsigned getSpillAlignment(const TargetRegisterClass &RC) const { 284 return getRegClassInfo(RC).SpillAlignment / 8; 285 } 286 287 /// Return the minimum required alignment in bytes for a spill slot for 288 /// a register of this class. getSpillAlign(const TargetRegisterClass & RC)289 Align getSpillAlign(const TargetRegisterClass &RC) const { 290 return Align(getRegClassInfo(RC).SpillAlignment / 8); 291 } 292 293 /// Return true if the given TargetRegisterClass has the ValueType T. isTypeLegalForClass(const TargetRegisterClass & RC,MVT T)294 bool isTypeLegalForClass(const TargetRegisterClass &RC, MVT T) const { 295 for (auto I = legalclasstypes_begin(RC); *I != MVT::Other; ++I) 296 if (MVT(*I) == T) 297 return true; 298 return false; 299 } 300 301 /// Loop over all of the value types that can be represented by values 302 /// in the given register class. legalclasstypes_begin(const TargetRegisterClass & RC)303 vt_iterator legalclasstypes_begin(const TargetRegisterClass &RC) const { 304 return getRegClassInfo(RC).VTList; 305 } 306 legalclasstypes_end(const TargetRegisterClass & RC)307 vt_iterator legalclasstypes_end(const TargetRegisterClass &RC) const { 308 vt_iterator I = legalclasstypes_begin(RC); 309 while (*I != MVT::Other) 310 ++I; 311 return I; 312 } 313 314 /// Returns the Register Class of a physical register of the given type, 315 /// picking the most sub register class of the right type that contains this 316 /// physreg. 317 const TargetRegisterClass *getMinimalPhysRegClass(MCRegister Reg, 318 MVT VT = MVT::Other) const; 319 320 /// Return the maximal subclass of the given register class that is 321 /// allocatable or NULL. 322 const TargetRegisterClass * 323 getAllocatableClass(const TargetRegisterClass *RC) const; 324 325 /// Returns a bitset indexed by register number indicating if a register is 326 /// allocatable or not. If a register class is specified, returns the subset 327 /// for the class. 328 BitVector getAllocatableSet(const MachineFunction &MF, 329 const TargetRegisterClass *RC = nullptr) const; 330 331 /// Return the additional cost of using this register instead 332 /// of other registers in its class. getCostPerUse(MCRegister RegNo)333 unsigned getCostPerUse(MCRegister RegNo) const { 334 return InfoDesc[RegNo].CostPerUse; 335 } 336 337 /// Return true if the register is in the allocation of any register class. isInAllocatableClass(MCRegister RegNo)338 bool isInAllocatableClass(MCRegister RegNo) const { 339 return InfoDesc[RegNo].inAllocatableClass; 340 } 341 342 /// Return the human-readable symbolic target-specific 343 /// name for the specified SubRegIndex. getSubRegIndexName(unsigned SubIdx)344 const char *getSubRegIndexName(unsigned SubIdx) const { 345 assert(SubIdx && SubIdx < getNumSubRegIndices() && 346 "This is not a subregister index"); 347 return SubRegIndexNames[SubIdx-1]; 348 } 349 350 /// Return a bitmask representing the parts of a register that are covered by 351 /// SubIdx \see LaneBitmask. 352 /// 353 /// SubIdx == 0 is allowed, it has the lane mask ~0u. getSubRegIndexLaneMask(unsigned SubIdx)354 LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const { 355 assert(SubIdx < getNumSubRegIndices() && "This is not a subregister index"); 356 return SubRegIndexLaneMasks[SubIdx]; 357 } 358 359 /// The lane masks returned by getSubRegIndexLaneMask() above can only be 360 /// used to determine if sub-registers overlap - they can't be used to 361 /// determine if a set of sub-registers completely cover another 362 /// sub-register. 363 /// 364 /// The X86 general purpose registers have two lanes corresponding to the 365 /// sub_8bit and sub_8bit_hi sub-registers. Both sub_32bit and sub_16bit have 366 /// lane masks '3', but the sub_16bit sub-register doesn't fully cover the 367 /// sub_32bit sub-register. 368 /// 369 /// On the other hand, the ARM NEON lanes fully cover their registers: The 370 /// dsub_0 sub-register is completely covered by the ssub_0 and ssub_1 lanes. 371 /// This is related to the CoveredBySubRegs property on register definitions. 372 /// 373 /// This function returns a bit mask of lanes that completely cover their 374 /// sub-registers. More precisely, given: 375 /// 376 /// Covering = getCoveringLanes(); 377 /// MaskA = getSubRegIndexLaneMask(SubA); 378 /// MaskB = getSubRegIndexLaneMask(SubB); 379 /// 380 /// If (MaskA & ~(MaskB & Covering)) == 0, then SubA is completely covered by 381 /// SubB. getCoveringLanes()382 LaneBitmask getCoveringLanes() const { return CoveringLanes; } 383 384 /// Returns true if the two registers are equal or alias each other. 385 /// The registers may be virtual registers. regsOverlap(Register regA,Register regB)386 bool regsOverlap(Register regA, Register regB) const { 387 if (regA == regB) return true; 388 if (!regA.isPhysical() || !regB.isPhysical()) 389 return false; 390 391 // Regunits are numerically ordered. Find a common unit. 392 MCRegUnitIterator RUA(regA.asMCReg(), this); 393 MCRegUnitIterator RUB(regB.asMCReg(), this); 394 do { 395 if (*RUA == *RUB) return true; 396 if (*RUA < *RUB) ++RUA; 397 else ++RUB; 398 } while (RUA.isValid() && RUB.isValid()); 399 return false; 400 } 401 402 /// Returns true if Reg contains RegUnit. hasRegUnit(MCRegister Reg,Register RegUnit)403 bool hasRegUnit(MCRegister Reg, Register RegUnit) const { 404 for (MCRegUnitIterator Units(Reg, this); Units.isValid(); ++Units) 405 if (Register(*Units) == RegUnit) 406 return true; 407 return false; 408 } 409 410 /// Returns the original SrcReg unless it is the target of a copy-like 411 /// operation, in which case we chain backwards through all such operations 412 /// to the ultimate source register. If a physical register is encountered, 413 /// we stop the search. 414 virtual Register lookThruCopyLike(Register SrcReg, 415 const MachineRegisterInfo *MRI) const; 416 417 /// Return a null-terminated list of all of the callee-saved registers on 418 /// this target. The register should be in the order of desired callee-save 419 /// stack frame offset. The first register is closest to the incoming stack 420 /// pointer if stack grows down, and vice versa. 421 /// Notice: This function does not take into account disabled CSRs. 422 /// In most cases you will want to use instead the function 423 /// getCalleeSavedRegs that is implemented in MachineRegisterInfo. 424 virtual const MCPhysReg* 425 getCalleeSavedRegs(const MachineFunction *MF) const = 0; 426 427 /// Return a mask of call-preserved registers for the given calling convention 428 /// on the current function. The mask should include all call-preserved 429 /// aliases. This is used by the register allocator to determine which 430 /// registers can be live across a call. 431 /// 432 /// The mask is an array containing (TRI::getNumRegs()+31)/32 entries. 433 /// A set bit indicates that all bits of the corresponding register are 434 /// preserved across the function call. The bit mask is expected to be 435 /// sub-register complete, i.e. if A is preserved, so are all its 436 /// sub-registers. 437 /// 438 /// Bits are numbered from the LSB, so the bit for physical register Reg can 439 /// be found as (Mask[Reg / 32] >> Reg % 32) & 1. 440 /// 441 /// A NULL pointer means that no register mask will be used, and call 442 /// instructions should use implicit-def operands to indicate call clobbered 443 /// registers. 444 /// getCallPreservedMask(const MachineFunction & MF,CallingConv::ID)445 virtual const uint32_t *getCallPreservedMask(const MachineFunction &MF, 446 CallingConv::ID) const { 447 // The default mask clobbers everything. All targets should override. 448 return nullptr; 449 } 450 451 /// Return a register mask for the registers preserved by the unwinder, 452 /// or nullptr if no custom mask is needed. 453 virtual const uint32_t * getCustomEHPadPreservedMask(const MachineFunction & MF)454 getCustomEHPadPreservedMask(const MachineFunction &MF) const { 455 return nullptr; 456 } 457 458 /// Return a register mask that clobbers everything. getNoPreservedMask()459 virtual const uint32_t *getNoPreservedMask() const { 460 llvm_unreachable("target does not provide no preserved mask"); 461 } 462 463 /// Return a list of all of the registers which are clobbered "inside" a call 464 /// to the given function. For example, these might be needed for PLT 465 /// sequences of long-branch veneers. 466 virtual ArrayRef<MCPhysReg> getIntraCallClobberedRegs(const MachineFunction * MF)467 getIntraCallClobberedRegs(const MachineFunction *MF) const { 468 return {}; 469 } 470 471 /// Return true if all bits that are set in mask \p mask0 are also set in 472 /// \p mask1. 473 bool regmaskSubsetEqual(const uint32_t *mask0, const uint32_t *mask1) const; 474 475 /// Return all the call-preserved register masks defined for this target. 476 virtual ArrayRef<const uint32_t *> getRegMasks() const = 0; 477 virtual ArrayRef<const char *> getRegMaskNames() const = 0; 478 479 /// Returns a bitset indexed by physical register number indicating if a 480 /// register is a special register that has particular uses and should be 481 /// considered unavailable at all times, e.g. stack pointer, return address. 482 /// A reserved register: 483 /// - is not allocatable 484 /// - is considered always live 485 /// - is ignored by liveness tracking 486 /// It is often necessary to reserve the super registers of a reserved 487 /// register as well, to avoid them getting allocated indirectly. You may use 488 /// markSuperRegs() and checkAllSuperRegsMarked() in this case. 489 virtual BitVector getReservedRegs(const MachineFunction &MF) const = 0; 490 491 /// Returns false if we can't guarantee that Physreg, specified as an IR asm 492 /// clobber constraint, will be preserved across the statement. isAsmClobberable(const MachineFunction & MF,MCRegister PhysReg)493 virtual bool isAsmClobberable(const MachineFunction &MF, 494 MCRegister PhysReg) const { 495 return true; 496 } 497 498 /// Returns true if PhysReg cannot be written to in inline asm statements. isInlineAsmReadOnlyReg(const MachineFunction & MF,unsigned PhysReg)499 virtual bool isInlineAsmReadOnlyReg(const MachineFunction &MF, 500 unsigned PhysReg) const { 501 return false; 502 } 503 504 /// Returns true if PhysReg is unallocatable and constant throughout the 505 /// function. Used by MachineRegisterInfo::isConstantPhysReg(). isConstantPhysReg(MCRegister PhysReg)506 virtual bool isConstantPhysReg(MCRegister PhysReg) const { return false; } 507 508 /// Returns true if the register class is considered divergent. isDivergentRegClass(const TargetRegisterClass * RC)509 virtual bool isDivergentRegClass(const TargetRegisterClass *RC) const { 510 return false; 511 } 512 513 /// Physical registers that may be modified within a function but are 514 /// guaranteed to be restored before any uses. This is useful for targets that 515 /// have call sequences where a GOT register may be updated by the caller 516 /// prior to a call and is guaranteed to be restored (also by the caller) 517 /// after the call. isCallerPreservedPhysReg(MCRegister PhysReg,const MachineFunction & MF)518 virtual bool isCallerPreservedPhysReg(MCRegister PhysReg, 519 const MachineFunction &MF) const { 520 return false; 521 } 522 523 /// This is a wrapper around getCallPreservedMask(). 524 /// Return true if the register is preserved after the call. 525 virtual bool isCalleeSavedPhysReg(MCRegister PhysReg, 526 const MachineFunction &MF) const; 527 528 /// Prior to adding the live-out mask to a stackmap or patchpoint 529 /// instruction, provide the target the opportunity to adjust it (mainly to 530 /// remove pseudo-registers that should be ignored). adjustStackMapLiveOutMask(uint32_t * Mask)531 virtual void adjustStackMapLiveOutMask(uint32_t *Mask) const {} 532 533 /// Return a super-register of the specified register 534 /// Reg so its sub-register of index SubIdx is Reg. getMatchingSuperReg(MCRegister Reg,unsigned SubIdx,const TargetRegisterClass * RC)535 MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx, 536 const TargetRegisterClass *RC) const { 537 return MCRegisterInfo::getMatchingSuperReg(Reg, SubIdx, RC->MC); 538 } 539 540 /// Return a subclass of the specified register 541 /// class A so that each register in it has a sub-register of the 542 /// specified sub-register index which is in the specified register class B. 543 /// 544 /// TableGen will synthesize missing A sub-classes. 545 virtual const TargetRegisterClass * 546 getMatchingSuperRegClass(const TargetRegisterClass *A, 547 const TargetRegisterClass *B, unsigned Idx) const; 548 549 // For a copy-like instruction that defines a register of class DefRC with 550 // subreg index DefSubReg, reading from another source with class SrcRC and 551 // subregister SrcSubReg return true if this is a preferable copy 552 // instruction or an earlier use should be used. 553 virtual bool shouldRewriteCopySrc(const TargetRegisterClass *DefRC, 554 unsigned DefSubReg, 555 const TargetRegisterClass *SrcRC, 556 unsigned SrcSubReg) const; 557 558 /// Returns the largest legal sub-class of RC that 559 /// supports the sub-register index Idx. 560 /// If no such sub-class exists, return NULL. 561 /// If all registers in RC already have an Idx sub-register, return RC. 562 /// 563 /// TableGen generates a version of this function that is good enough in most 564 /// cases. Targets can override if they have constraints that TableGen 565 /// doesn't understand. For example, the x86 sub_8bit sub-register index is 566 /// supported by the full GR32 register class in 64-bit mode, but only by the 567 /// GR32_ABCD regiister class in 32-bit mode. 568 /// 569 /// TableGen will synthesize missing RC sub-classes. 570 virtual const TargetRegisterClass * getSubClassWithSubReg(const TargetRegisterClass * RC,unsigned Idx)571 getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const { 572 assert(Idx == 0 && "Target has no sub-registers"); 573 return RC; 574 } 575 576 /// Return the subregister index you get from composing 577 /// two subregister indices. 578 /// 579 /// The special null sub-register index composes as the identity. 580 /// 581 /// If R:a:b is the same register as R:c, then composeSubRegIndices(a, b) 582 /// returns c. Note that composeSubRegIndices does not tell you about illegal 583 /// compositions. If R does not have a subreg a, or R:a does not have a subreg 584 /// b, composeSubRegIndices doesn't tell you. 585 /// 586 /// The ARM register Q0 has two D subregs dsub_0:D0 and dsub_1:D1. It also has 587 /// ssub_0:S0 - ssub_3:S3 subregs. 588 /// If you compose subreg indices dsub_1, ssub_0 you get ssub_2. composeSubRegIndices(unsigned a,unsigned b)589 unsigned composeSubRegIndices(unsigned a, unsigned b) const { 590 if (!a) return b; 591 if (!b) return a; 592 return composeSubRegIndicesImpl(a, b); 593 } 594 595 /// Transforms a LaneMask computed for one subregister to the lanemask that 596 /// would have been computed when composing the subsubregisters with IdxA 597 /// first. @sa composeSubRegIndices() composeSubRegIndexLaneMask(unsigned IdxA,LaneBitmask Mask)598 LaneBitmask composeSubRegIndexLaneMask(unsigned IdxA, 599 LaneBitmask Mask) const { 600 if (!IdxA) 601 return Mask; 602 return composeSubRegIndexLaneMaskImpl(IdxA, Mask); 603 } 604 605 /// Transform a lanemask given for a virtual register to the corresponding 606 /// lanemask before using subregister with index \p IdxA. 607 /// This is the reverse of composeSubRegIndexLaneMask(), assuming Mask is a 608 /// valie lane mask (no invalid bits set) the following holds: 609 /// X0 = composeSubRegIndexLaneMask(Idx, Mask) 610 /// X1 = reverseComposeSubRegIndexLaneMask(Idx, X0) 611 /// => X1 == Mask reverseComposeSubRegIndexLaneMask(unsigned IdxA,LaneBitmask LaneMask)612 LaneBitmask reverseComposeSubRegIndexLaneMask(unsigned IdxA, 613 LaneBitmask LaneMask) const { 614 if (!IdxA) 615 return LaneMask; 616 return reverseComposeSubRegIndexLaneMaskImpl(IdxA, LaneMask); 617 } 618 619 /// Debugging helper: dump register in human readable form to dbgs() stream. 620 static void dumpReg(Register Reg, unsigned SubRegIndex = 0, 621 const TargetRegisterInfo *TRI = nullptr); 622 623 protected: 624 /// Overridden by TableGen in targets that have sub-registers. composeSubRegIndicesImpl(unsigned,unsigned)625 virtual unsigned composeSubRegIndicesImpl(unsigned, unsigned) const { 626 llvm_unreachable("Target has no sub-registers"); 627 } 628 629 /// Overridden by TableGen in targets that have sub-registers. 630 virtual LaneBitmask composeSubRegIndexLaneMaskImpl(unsigned,LaneBitmask)631 composeSubRegIndexLaneMaskImpl(unsigned, LaneBitmask) const { 632 llvm_unreachable("Target has no sub-registers"); 633 } 634 reverseComposeSubRegIndexLaneMaskImpl(unsigned,LaneBitmask)635 virtual LaneBitmask reverseComposeSubRegIndexLaneMaskImpl(unsigned, 636 LaneBitmask) const { 637 llvm_unreachable("Target has no sub-registers"); 638 } 639 640 public: 641 /// Find a common super-register class if it exists. 642 /// 643 /// Find a register class, SuperRC and two sub-register indices, PreA and 644 /// PreB, such that: 645 /// 646 /// 1. PreA + SubA == PreB + SubB (using composeSubRegIndices()), and 647 /// 648 /// 2. For all Reg in SuperRC: Reg:PreA in RCA and Reg:PreB in RCB, and 649 /// 650 /// 3. SuperRC->getSize() >= max(RCA->getSize(), RCB->getSize()). 651 /// 652 /// SuperRC will be chosen such that no super-class of SuperRC satisfies the 653 /// requirements, and there is no register class with a smaller spill size 654 /// that satisfies the requirements. 655 /// 656 /// SubA and SubB must not be 0. Use getMatchingSuperRegClass() instead. 657 /// 658 /// Either of the PreA and PreB sub-register indices may be returned as 0. In 659 /// that case, the returned register class will be a sub-class of the 660 /// corresponding argument register class. 661 /// 662 /// The function returns NULL if no register class can be found. 663 const TargetRegisterClass* 664 getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA, 665 const TargetRegisterClass *RCB, unsigned SubB, 666 unsigned &PreA, unsigned &PreB) const; 667 668 //===--------------------------------------------------------------------===// 669 // Register Class Information 670 // 671 protected: getRegClassInfo(const TargetRegisterClass & RC)672 const RegClassInfo &getRegClassInfo(const TargetRegisterClass &RC) const { 673 return RCInfos[getNumRegClasses() * HwMode + RC.getID()]; 674 } 675 676 public: 677 /// Register class iterators regclass_begin()678 regclass_iterator regclass_begin() const { return RegClassBegin; } regclass_end()679 regclass_iterator regclass_end() const { return RegClassEnd; } regclasses()680 iterator_range<regclass_iterator> regclasses() const { 681 return make_range(regclass_begin(), regclass_end()); 682 } 683 getNumRegClasses()684 unsigned getNumRegClasses() const { 685 return (unsigned)(regclass_end()-regclass_begin()); 686 } 687 688 /// Returns the register class associated with the enumeration value. 689 /// See class MCOperandInfo. getRegClass(unsigned i)690 const TargetRegisterClass *getRegClass(unsigned i) const { 691 assert(i < getNumRegClasses() && "Register Class ID out of range"); 692 return RegClassBegin[i]; 693 } 694 695 /// Returns the name of the register class. getRegClassName(const TargetRegisterClass * Class)696 const char *getRegClassName(const TargetRegisterClass *Class) const { 697 return MCRegisterInfo::getRegClassName(Class->MC); 698 } 699 700 /// Find the largest common subclass of A and B. 701 /// Return NULL if there is no common subclass. 702 const TargetRegisterClass * 703 getCommonSubClass(const TargetRegisterClass *A, 704 const TargetRegisterClass *B) const; 705 706 /// Returns a TargetRegisterClass used for pointer values. 707 /// If a target supports multiple different pointer register classes, 708 /// kind specifies which one is indicated. 709 virtual const TargetRegisterClass * 710 getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const { 711 llvm_unreachable("Target didn't implement getPointerRegClass!"); 712 } 713 714 /// Returns a legal register class to copy a register in the specified class 715 /// to or from. If it is possible to copy the register directly without using 716 /// a cross register class copy, return the specified RC. Returns NULL if it 717 /// is not possible to copy between two registers of the specified class. 718 virtual const TargetRegisterClass * getCrossCopyRegClass(const TargetRegisterClass * RC)719 getCrossCopyRegClass(const TargetRegisterClass *RC) const { 720 return RC; 721 } 722 723 /// Returns the largest super class of RC that is legal to use in the current 724 /// sub-target and has the same spill size. 725 /// The returned register class can be used to create virtual registers which 726 /// means that all its registers can be copied and spilled. 727 virtual const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass * RC,const MachineFunction &)728 getLargestLegalSuperClass(const TargetRegisterClass *RC, 729 const MachineFunction &) const { 730 /// The default implementation is very conservative and doesn't allow the 731 /// register allocator to inflate register classes. 732 return RC; 733 } 734 735 /// Return the register pressure "high water mark" for the specific register 736 /// class. The scheduler is in high register pressure mode (for the specific 737 /// register class) if it goes over the limit. 738 /// 739 /// Note: this is the old register pressure model that relies on a manually 740 /// specified representative register class per value type. getRegPressureLimit(const TargetRegisterClass * RC,MachineFunction & MF)741 virtual unsigned getRegPressureLimit(const TargetRegisterClass *RC, 742 MachineFunction &MF) const { 743 return 0; 744 } 745 746 /// Return a heuristic for the machine scheduler to compare the profitability 747 /// of increasing one register pressure set versus another. The scheduler 748 /// will prefer increasing the register pressure of the set which returns 749 /// the largest value for this function. getRegPressureSetScore(const MachineFunction & MF,unsigned PSetID)750 virtual unsigned getRegPressureSetScore(const MachineFunction &MF, 751 unsigned PSetID) const { 752 return PSetID; 753 } 754 755 /// Get the weight in units of pressure for this register class. 756 virtual const RegClassWeight &getRegClassWeight( 757 const TargetRegisterClass *RC) const = 0; 758 759 /// Returns size in bits of a phys/virtual/generic register. 760 unsigned getRegSizeInBits(Register Reg, const MachineRegisterInfo &MRI) const; 761 762 /// Get the weight in units of pressure for this register unit. 763 virtual unsigned getRegUnitWeight(unsigned RegUnit) const = 0; 764 765 /// Get the number of dimensions of register pressure. 766 virtual unsigned getNumRegPressureSets() const = 0; 767 768 /// Get the name of this register unit pressure set. 769 virtual const char *getRegPressureSetName(unsigned Idx) const = 0; 770 771 /// Get the register unit pressure limit for this dimension. 772 /// This limit must be adjusted dynamically for reserved registers. 773 virtual unsigned getRegPressureSetLimit(const MachineFunction &MF, 774 unsigned Idx) const = 0; 775 776 /// Get the dimensions of register pressure impacted by this register class. 777 /// Returns a -1 terminated array of pressure set IDs. 778 virtual const int *getRegClassPressureSets( 779 const TargetRegisterClass *RC) const = 0; 780 781 /// Get the dimensions of register pressure impacted by this register unit. 782 /// Returns a -1 terminated array of pressure set IDs. 783 virtual const int *getRegUnitPressureSets(unsigned RegUnit) const = 0; 784 785 /// Get a list of 'hint' registers that the register allocator should try 786 /// first when allocating a physical register for the virtual register 787 /// VirtReg. These registers are effectively moved to the front of the 788 /// allocation order. If true is returned, regalloc will try to only use 789 /// hints to the greatest extent possible even if it means spilling. 790 /// 791 /// The Order argument is the allocation order for VirtReg's register class 792 /// as returned from RegisterClassInfo::getOrder(). The hint registers must 793 /// come from Order, and they must not be reserved. 794 /// 795 /// The default implementation of this function will only add target 796 /// independent register allocation hints. Targets that override this 797 /// function should typically call this default implementation as well and 798 /// expect to see generic copy hints added. 799 virtual bool 800 getRegAllocationHints(Register VirtReg, ArrayRef<MCPhysReg> Order, 801 SmallVectorImpl<MCPhysReg> &Hints, 802 const MachineFunction &MF, 803 const VirtRegMap *VRM = nullptr, 804 const LiveRegMatrix *Matrix = nullptr) const; 805 806 /// A callback to allow target a chance to update register allocation hints 807 /// when a register is "changed" (e.g. coalesced) to another register. 808 /// e.g. On ARM, some virtual registers should target register pairs, 809 /// if one of pair is coalesced to another register, the allocation hint of 810 /// the other half of the pair should be changed to point to the new register. updateRegAllocHint(Register Reg,Register NewReg,MachineFunction & MF)811 virtual void updateRegAllocHint(Register Reg, Register NewReg, 812 MachineFunction &MF) const { 813 // Do nothing. 814 } 815 816 /// Allow the target to reverse allocation order of local live ranges. This 817 /// will generally allocate shorter local live ranges first. For targets with 818 /// many registers, this could reduce regalloc compile time by a large 819 /// factor. It is disabled by default for three reasons: 820 /// (1) Top-down allocation is simpler and easier to debug for targets that 821 /// don't benefit from reversing the order. 822 /// (2) Bottom-up allocation could result in poor evicition decisions on some 823 /// targets affecting the performance of compiled code. 824 /// (3) Bottom-up allocation is no longer guaranteed to optimally color. reverseLocalAssignment()825 virtual bool reverseLocalAssignment() const { return false; } 826 827 /// Allow the target to override the cost of using a callee-saved register for 828 /// the first time. Default value of 0 means we will use a callee-saved 829 /// register if it is available. getCSRFirstUseCost()830 virtual unsigned getCSRFirstUseCost() const { return 0; } 831 832 /// Returns true if the target requires (and can make use of) the register 833 /// scavenger. requiresRegisterScavenging(const MachineFunction & MF)834 virtual bool requiresRegisterScavenging(const MachineFunction &MF) const { 835 return false; 836 } 837 838 /// Returns true if the target wants to use frame pointer based accesses to 839 /// spill to the scavenger emergency spill slot. useFPForScavengingIndex(const MachineFunction & MF)840 virtual bool useFPForScavengingIndex(const MachineFunction &MF) const { 841 return true; 842 } 843 844 /// Returns true if the target requires post PEI scavenging of registers for 845 /// materializing frame index constants. requiresFrameIndexScavenging(const MachineFunction & MF)846 virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const { 847 return false; 848 } 849 850 /// Returns true if the target requires using the RegScavenger directly for 851 /// frame elimination despite using requiresFrameIndexScavenging. requiresFrameIndexReplacementScavenging(const MachineFunction & MF)852 virtual bool requiresFrameIndexReplacementScavenging( 853 const MachineFunction &MF) const { 854 return false; 855 } 856 857 /// Returns true if the target wants the LocalStackAllocation pass to be run 858 /// and virtual base registers used for more efficient stack access. requiresVirtualBaseRegisters(const MachineFunction & MF)859 virtual bool requiresVirtualBaseRegisters(const MachineFunction &MF) const { 860 return false; 861 } 862 863 /// Return true if target has reserved a spill slot in the stack frame of 864 /// the given function for the specified register. e.g. On x86, if the frame 865 /// register is required, the first fixed stack object is reserved as its 866 /// spill slot. This tells PEI not to create a new stack frame 867 /// object for the given register. It should be called only after 868 /// determineCalleeSaves(). hasReservedSpillSlot(const MachineFunction & MF,Register Reg,int & FrameIdx)869 virtual bool hasReservedSpillSlot(const MachineFunction &MF, Register Reg, 870 int &FrameIdx) const { 871 return false; 872 } 873 874 /// Returns true if the live-ins should be tracked after register allocation. trackLivenessAfterRegAlloc(const MachineFunction & MF)875 virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const { 876 return true; 877 } 878 879 /// True if the stack can be realigned for the target. 880 virtual bool canRealignStack(const MachineFunction &MF) const; 881 882 /// True if storage within the function requires the stack pointer to be 883 /// aligned more than the normal calling convention calls for. 884 /// This cannot be overriden by the target, but canRealignStack can be 885 /// overridden. 886 bool needsStackRealignment(const MachineFunction &MF) const; 887 888 /// Get the offset from the referenced frame index in the instruction, 889 /// if there is one. getFrameIndexInstrOffset(const MachineInstr * MI,int Idx)890 virtual int64_t getFrameIndexInstrOffset(const MachineInstr *MI, 891 int Idx) const { 892 return 0; 893 } 894 895 /// Returns true if the instruction's frame index reference would be better 896 /// served by a base register other than FP or SP. 897 /// Used by LocalStackFrameAllocation to determine which frame index 898 /// references it should create new base registers for. needsFrameBaseReg(MachineInstr * MI,int64_t Offset)899 virtual bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { 900 return false; 901 } 902 903 /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx 904 /// before insertion point I. materializeFrameBaseRegister(MachineBasicBlock * MBB,Register BaseReg,int FrameIdx,int64_t Offset)905 virtual void materializeFrameBaseRegister(MachineBasicBlock *MBB, 906 Register BaseReg, int FrameIdx, 907 int64_t Offset) const { 908 llvm_unreachable("materializeFrameBaseRegister does not exist on this " 909 "target"); 910 } 911 912 /// Resolve a frame index operand of an instruction 913 /// to reference the indicated base register plus offset instead. resolveFrameIndex(MachineInstr & MI,Register BaseReg,int64_t Offset)914 virtual void resolveFrameIndex(MachineInstr &MI, Register BaseReg, 915 int64_t Offset) const { 916 llvm_unreachable("resolveFrameIndex does not exist on this target"); 917 } 918 919 /// Determine whether a given base register plus offset immediate is 920 /// encodable to resolve a frame index. isFrameOffsetLegal(const MachineInstr * MI,Register BaseReg,int64_t Offset)921 virtual bool isFrameOffsetLegal(const MachineInstr *MI, Register BaseReg, 922 int64_t Offset) const { 923 llvm_unreachable("isFrameOffsetLegal does not exist on this target"); 924 } 925 926 /// Spill the register so it can be used by the register scavenger. 927 /// Return true if the register was spilled, false otherwise. 928 /// If this function does not spill the register, the scavenger 929 /// will instead spill it to the emergency spill slot. saveScavengerRegister(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,MachineBasicBlock::iterator & UseMI,const TargetRegisterClass * RC,Register Reg)930 virtual bool saveScavengerRegister(MachineBasicBlock &MBB, 931 MachineBasicBlock::iterator I, 932 MachineBasicBlock::iterator &UseMI, 933 const TargetRegisterClass *RC, 934 Register Reg) const { 935 return false; 936 } 937 938 /// This method must be overriden to eliminate abstract frame indices from 939 /// instructions which may use them. The instruction referenced by the 940 /// iterator contains an MO_FrameIndex operand which must be eliminated by 941 /// this method. This method may modify or replace the specified instruction, 942 /// as long as it keeps the iterator pointing at the finished product. 943 /// SPAdj is the SP adjustment due to call frame setup instruction. 944 /// FIOperandNum is the FI operand number. 945 virtual void eliminateFrameIndex(MachineBasicBlock::iterator MI, 946 int SPAdj, unsigned FIOperandNum, 947 RegScavenger *RS = nullptr) const = 0; 948 949 /// Return the assembly name for \p Reg. getRegAsmName(MCRegister Reg)950 virtual StringRef getRegAsmName(MCRegister Reg) const { 951 // FIXME: We are assuming that the assembly name is equal to the TableGen 952 // name converted to lower case 953 // 954 // The TableGen name is the name of the definition for this register in the 955 // target's tablegen files. For example, the TableGen name of 956 // def EAX : Register <...>; is "EAX" 957 return StringRef(getName(Reg)); 958 } 959 960 //===--------------------------------------------------------------------===// 961 /// Subtarget Hooks 962 963 /// SrcRC and DstRC will be morphed into NewRC if this returns true. shouldCoalesce(MachineInstr * MI,const TargetRegisterClass * SrcRC,unsigned SubReg,const TargetRegisterClass * DstRC,unsigned DstSubReg,const TargetRegisterClass * NewRC,LiveIntervals & LIS)964 virtual bool shouldCoalesce(MachineInstr *MI, 965 const TargetRegisterClass *SrcRC, 966 unsigned SubReg, 967 const TargetRegisterClass *DstRC, 968 unsigned DstSubReg, 969 const TargetRegisterClass *NewRC, 970 LiveIntervals &LIS) const 971 { return true; } 972 973 /// Region split has a high compile time cost especially for large live range. 974 /// This method is used to decide whether or not \p VirtReg should 975 /// go through this expensive splitting heuristic. 976 virtual bool shouldRegionSplitForVirtReg(const MachineFunction &MF, 977 const LiveInterval &VirtReg) const; 978 979 /// Last chance recoloring has a high compile time cost especially for 980 /// targets with a lot of registers. 981 /// This method is used to decide whether or not \p VirtReg should 982 /// go through this expensive heuristic. 983 /// When this target hook is hit, by returning false, there is a high 984 /// chance that the register allocation will fail altogether (usually with 985 /// "ran out of registers"). 986 /// That said, this error usually points to another problem in the 987 /// optimization pipeline. 988 virtual bool shouldUseLastChanceRecoloringForVirtReg(const MachineFunction & MF,const LiveInterval & VirtReg)989 shouldUseLastChanceRecoloringForVirtReg(const MachineFunction &MF, 990 const LiveInterval &VirtReg) const { 991 return true; 992 } 993 994 /// Deferred spilling delays the spill insertion of a virtual register 995 /// after every other allocation. By deferring the spilling, it is 996 /// sometimes possible to eliminate that spilling altogether because 997 /// something else could have been eliminated, thus leaving some space 998 /// for the virtual register. 999 /// However, this comes with a compile time impact because it adds one 1000 /// more stage to the greedy register allocator. 1001 /// This method is used to decide whether \p VirtReg should use the deferred 1002 /// spilling stage instead of being spilled right away. 1003 virtual bool shouldUseDeferredSpillingForVirtReg(const MachineFunction & MF,const LiveInterval & VirtReg)1004 shouldUseDeferredSpillingForVirtReg(const MachineFunction &MF, 1005 const LiveInterval &VirtReg) const { 1006 return false; 1007 } 1008 1009 //===--------------------------------------------------------------------===// 1010 /// Debug information queries. 1011 1012 /// getFrameRegister - This method should return the register used as a base 1013 /// for values allocated in the current stack frame. 1014 virtual Register getFrameRegister(const MachineFunction &MF) const = 0; 1015 1016 /// Mark a register and all its aliases as reserved in the given set. 1017 void markSuperRegs(BitVector &RegisterSet, MCRegister Reg) const; 1018 1019 /// Returns true if for every register in the set all super registers are part 1020 /// of the set as well. 1021 bool checkAllSuperRegsMarked(const BitVector &RegisterSet, 1022 ArrayRef<MCPhysReg> Exceptions = ArrayRef<MCPhysReg>()) const; 1023 1024 virtual const TargetRegisterClass * getConstrainedRegClassForOperand(const MachineOperand & MO,const MachineRegisterInfo & MRI)1025 getConstrainedRegClassForOperand(const MachineOperand &MO, 1026 const MachineRegisterInfo &MRI) const { 1027 return nullptr; 1028 } 1029 1030 /// Returns the physical register number of sub-register "Index" 1031 /// for physical register RegNo. Return zero if the sub-register does not 1032 /// exist. getSubReg(MCRegister Reg,unsigned Idx)1033 inline MCRegister getSubReg(MCRegister Reg, unsigned Idx) const { 1034 return static_cast<const MCRegisterInfo *>(this)->getSubReg(Reg, Idx); 1035 } 1036 }; 1037 1038 //===----------------------------------------------------------------------===// 1039 // SuperRegClassIterator 1040 //===----------------------------------------------------------------------===// 1041 // 1042 // Iterate over the possible super-registers for a given register class. The 1043 // iterator will visit a list of pairs (Idx, Mask) corresponding to the 1044 // possible classes of super-registers. 1045 // 1046 // Each bit mask will have at least one set bit, and each set bit in Mask 1047 // corresponds to a SuperRC such that: 1048 // 1049 // For all Reg in SuperRC: Reg:Idx is in RC. 1050 // 1051 // The iterator can include (O, RC->getSubClassMask()) as the first entry which 1052 // also satisfies the above requirement, assuming Reg:0 == Reg. 1053 // 1054 class SuperRegClassIterator { 1055 const unsigned RCMaskWords; 1056 unsigned SubReg = 0; 1057 const uint16_t *Idx; 1058 const uint32_t *Mask; 1059 1060 public: 1061 /// Create a SuperRegClassIterator that visits all the super-register classes 1062 /// of RC. When IncludeSelf is set, also include the (0, sub-classes) entry. 1063 SuperRegClassIterator(const TargetRegisterClass *RC, 1064 const TargetRegisterInfo *TRI, 1065 bool IncludeSelf = false) 1066 : RCMaskWords((TRI->getNumRegClasses() + 31) / 32), 1067 Idx(RC->getSuperRegIndices()), Mask(RC->getSubClassMask()) { 1068 if (!IncludeSelf) 1069 ++*this; 1070 } 1071 1072 /// Returns true if this iterator is still pointing at a valid entry. isValid()1073 bool isValid() const { return Idx; } 1074 1075 /// Returns the current sub-register index. getSubReg()1076 unsigned getSubReg() const { return SubReg; } 1077 1078 /// Returns the bit mask of register classes that getSubReg() projects into 1079 /// RC. 1080 /// See TargetRegisterClass::getSubClassMask() for how to use it. getMask()1081 const uint32_t *getMask() const { return Mask; } 1082 1083 /// Advance iterator to the next entry. 1084 void operator++() { 1085 assert(isValid() && "Cannot move iterator past end."); 1086 Mask += RCMaskWords; 1087 SubReg = *Idx++; 1088 if (!SubReg) 1089 Idx = nullptr; 1090 } 1091 }; 1092 1093 //===----------------------------------------------------------------------===// 1094 // BitMaskClassIterator 1095 //===----------------------------------------------------------------------===// 1096 /// This class encapuslates the logic to iterate over bitmask returned by 1097 /// the various RegClass related APIs. 1098 /// E.g., this class can be used to iterate over the subclasses provided by 1099 /// TargetRegisterClass::getSubClassMask or SuperRegClassIterator::getMask. 1100 class BitMaskClassIterator { 1101 /// Total number of register classes. 1102 const unsigned NumRegClasses; 1103 /// Base index of CurrentChunk. 1104 /// In other words, the number of bit we read to get at the 1105 /// beginning of that chunck. 1106 unsigned Base = 0; 1107 /// Adjust base index of CurrentChunk. 1108 /// Base index + how many bit we read within CurrentChunk. 1109 unsigned Idx = 0; 1110 /// Current register class ID. 1111 unsigned ID = 0; 1112 /// Mask we are iterating over. 1113 const uint32_t *Mask; 1114 /// Current chunk of the Mask we are traversing. 1115 uint32_t CurrentChunk; 1116 1117 /// Move ID to the next set bit. moveToNextID()1118 void moveToNextID() { 1119 // If the current chunk of memory is empty, move to the next one, 1120 // while making sure we do not go pass the number of register 1121 // classes. 1122 while (!CurrentChunk) { 1123 // Move to the next chunk. 1124 Base += 32; 1125 if (Base >= NumRegClasses) { 1126 ID = NumRegClasses; 1127 return; 1128 } 1129 CurrentChunk = *++Mask; 1130 Idx = Base; 1131 } 1132 // Otherwise look for the first bit set from the right 1133 // (representation of the class ID is big endian). 1134 // See getSubClassMask for more details on the representation. 1135 unsigned Offset = countTrailingZeros(CurrentChunk); 1136 // Add the Offset to the adjusted base number of this chunk: Idx. 1137 // This is the ID of the register class. 1138 ID = Idx + Offset; 1139 1140 // Consume the zeros, if any, and the bit we just read 1141 // so that we are at the right spot for the next call. 1142 // Do not do Offset + 1 because Offset may be 31 and 32 1143 // will be UB for the shift, though in that case we could 1144 // have make the chunk being equal to 0, but that would 1145 // have introduced a if statement. 1146 moveNBits(Offset); 1147 moveNBits(1); 1148 } 1149 1150 /// Move \p NumBits Bits forward in CurrentChunk. moveNBits(unsigned NumBits)1151 void moveNBits(unsigned NumBits) { 1152 assert(NumBits < 32 && "Undefined behavior spotted!"); 1153 // Consume the bit we read for the next call. 1154 CurrentChunk >>= NumBits; 1155 // Adjust the base for the chunk. 1156 Idx += NumBits; 1157 } 1158 1159 public: 1160 /// Create a BitMaskClassIterator that visits all the register classes 1161 /// represented by \p Mask. 1162 /// 1163 /// \pre \p Mask != nullptr BitMaskClassIterator(const uint32_t * Mask,const TargetRegisterInfo & TRI)1164 BitMaskClassIterator(const uint32_t *Mask, const TargetRegisterInfo &TRI) 1165 : NumRegClasses(TRI.getNumRegClasses()), Mask(Mask), CurrentChunk(*Mask) { 1166 // Move to the first ID. 1167 moveToNextID(); 1168 } 1169 1170 /// Returns true if this iterator is still pointing at a valid entry. isValid()1171 bool isValid() const { return getID() != NumRegClasses; } 1172 1173 /// Returns the current register class ID. getID()1174 unsigned getID() const { return ID; } 1175 1176 /// Advance iterator to the next entry. 1177 void operator++() { 1178 assert(isValid() && "Cannot move iterator past end."); 1179 moveToNextID(); 1180 } 1181 }; 1182 1183 // This is useful when building IndexedMaps keyed on virtual registers 1184 struct VirtReg2IndexFunctor { 1185 using argument_type = Register; operatorVirtReg2IndexFunctor1186 unsigned operator()(Register Reg) const { 1187 return Register::virtReg2Index(Reg); 1188 } 1189 }; 1190 1191 /// Prints virtual and physical registers with or without a TRI instance. 1192 /// 1193 /// The format is: 1194 /// %noreg - NoRegister 1195 /// %5 - a virtual register. 1196 /// %5:sub_8bit - a virtual register with sub-register index (with TRI). 1197 /// %eax - a physical register 1198 /// %physreg17 - a physical register when no TRI instance given. 1199 /// 1200 /// Usage: OS << printReg(Reg, TRI, SubRegIdx) << '\n'; 1201 Printable printReg(Register Reg, const TargetRegisterInfo *TRI = nullptr, 1202 unsigned SubIdx = 0, 1203 const MachineRegisterInfo *MRI = nullptr); 1204 1205 /// Create Printable object to print register units on a \ref raw_ostream. 1206 /// 1207 /// Register units are named after their root registers: 1208 /// 1209 /// al - Single root. 1210 /// fp0~st7 - Dual roots. 1211 /// 1212 /// Usage: OS << printRegUnit(Unit, TRI) << '\n'; 1213 Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI); 1214 1215 /// Create Printable object to print virtual registers and physical 1216 /// registers on a \ref raw_ostream. 1217 Printable printVRegOrUnit(unsigned VRegOrUnit, const TargetRegisterInfo *TRI); 1218 1219 /// Create Printable object to print register classes or register banks 1220 /// on a \ref raw_ostream. 1221 Printable printRegClassOrBank(Register Reg, const MachineRegisterInfo &RegInfo, 1222 const TargetRegisterInfo *TRI); 1223 1224 } // end namespace llvm 1225 1226 #endif // LLVM_CODEGEN_TARGETREGISTERINFO_H 1227