Register.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/CodeGen/Register.h ---------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_REGISTER_H
  14. #define LLVM_CODEGEN_REGISTER_H
  15. #include "llvm/MC/MCRegister.h"
  16. #include <cassert>
  17. namespace llvm {
  18. /// Wrapper class representing virtual and physical registers. Should be passed
  19. /// by value.
  20. class Register {
  21. unsigned Reg;
  22. public:
  23. constexpr Register(unsigned Val = 0): Reg(Val) {}
  24. constexpr Register(MCRegister Val): Reg(Val) {}
  25. // Register numbers can represent physical registers, virtual registers, and
  26. // sometimes stack slots. The unsigned values are divided into these ranges:
  27. //
  28. // 0 Not a register, can be used as a sentinel.
  29. // [1;2^30) Physical registers assigned by TableGen.
  30. // [2^30;2^31) Stack slots. (Rarely used.)
  31. // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
  32. //
  33. // Further sentinels can be allocated from the small negative integers.
  34. // DenseMapInfo<unsigned> uses -1u and -2u.
  35. static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
  36. "Reg isn't large enough to hold full range.");
  37. /// isStackSlot - Sometimes it is useful the be able to store a non-negative
  38. /// frame index in a variable that normally holds a register. isStackSlot()
  39. /// returns true if Reg is in the range used for stack slots.
  40. ///
  41. /// FIXME: remove in favor of member.
  42. static bool isStackSlot(unsigned Reg) {
  43. return MCRegister::isStackSlot(Reg);
  44. }
  45. /// Return true if this is a stack slot.
  46. bool isStack() const { return MCRegister::isStackSlot(Reg); }
  47. /// Compute the frame index from a register value representing a stack slot.
  48. static int stackSlot2Index(Register Reg) {
  49. assert(Reg.isStack() && "Not a stack slot");
  50. return int(Reg - MCRegister::FirstStackSlot);
  51. }
  52. /// Convert a non-negative frame index to a stack slot register value.
  53. static Register index2StackSlot(int FI) {
  54. assert(FI >= 0 && "Cannot hold a negative frame index.");
  55. return Register(FI + MCRegister::FirstStackSlot);
  56. }
  57. /// Return true if the specified register number is in
  58. /// the physical register namespace.
  59. static bool isPhysicalRegister(unsigned Reg) {
  60. return MCRegister::isPhysicalRegister(Reg);
  61. }
  62. /// Return true if the specified register number is in
  63. /// the virtual register namespace.
  64. static bool isVirtualRegister(unsigned Reg) {
  65. return Reg & MCRegister::VirtualRegFlag;
  66. }
  67. /// Convert a virtual register number to a 0-based index.
  68. /// The first virtual register in a function will get the index 0.
  69. static unsigned virtReg2Index(Register Reg) {
  70. assert(Reg.isVirtual() && "Not a virtual register");
  71. return Reg & ~MCRegister::VirtualRegFlag;
  72. }
  73. /// Convert a 0-based index to a virtual register number.
  74. /// This is the inverse operation of VirtReg2IndexFunctor below.
  75. static Register index2VirtReg(unsigned Index) {
  76. assert(Index < (1u << 31) && "Index too large for virtual register range.");
  77. return Index | MCRegister::VirtualRegFlag;
  78. }
  79. /// Return true if the specified register number is in the virtual register
  80. /// namespace.
  81. bool isVirtual() const {
  82. return isVirtualRegister(Reg);
  83. }
  84. /// Return true if the specified register number is in the physical register
  85. /// namespace.
  86. bool isPhysical() const {
  87. return isPhysicalRegister(Reg);
  88. }
  89. /// Convert a virtual register number to a 0-based index. The first virtual
  90. /// register in a function will get the index 0.
  91. unsigned virtRegIndex() const {
  92. return virtReg2Index(Reg);
  93. }
  94. constexpr operator unsigned() const {
  95. return Reg;
  96. }
  97. unsigned id() const { return Reg; }
  98. operator MCRegister() const {
  99. return MCRegister(Reg);
  100. }
  101. /// Utility to check-convert this value to a MCRegister. The caller is
  102. /// expected to have already validated that this Register is, indeed,
  103. /// physical.
  104. MCRegister asMCReg() const {
  105. assert(Reg == MCRegister::NoRegister ||
  106. MCRegister::isPhysicalRegister(Reg));
  107. return MCRegister(Reg);
  108. }
  109. bool isValid() const { return Reg != MCRegister::NoRegister; }
  110. /// Comparisons between register objects
  111. bool operator==(const Register &Other) const { return Reg == Other.Reg; }
  112. bool operator!=(const Register &Other) const { return Reg != Other.Reg; }
  113. bool operator==(const MCRegister &Other) const { return Reg == Other.id(); }
  114. bool operator!=(const MCRegister &Other) const { return Reg != Other.id(); }
  115. /// Comparisons against register constants. E.g.
  116. /// * R == AArch64::WZR
  117. /// * R == 0
  118. /// * R == VirtRegMap::NO_PHYS_REG
  119. bool operator==(unsigned Other) const { return Reg == Other; }
  120. bool operator!=(unsigned Other) const { return Reg != Other; }
  121. bool operator==(int Other) const { return Reg == unsigned(Other); }
  122. bool operator!=(int Other) const { return Reg != unsigned(Other); }
  123. // MSVC requires that we explicitly declare these two as well.
  124. bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
  125. bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
  126. };
  127. // Provide DenseMapInfo for Register
  128. template<> struct DenseMapInfo<Register> {
  129. static inline unsigned getEmptyKey() {
  130. return DenseMapInfo<unsigned>::getEmptyKey();
  131. }
  132. static inline unsigned getTombstoneKey() {
  133. return DenseMapInfo<unsigned>::getTombstoneKey();
  134. }
  135. static unsigned getHashValue(const Register &Val) {
  136. return DenseMapInfo<unsigned>::getHashValue(Val.id());
  137. }
  138. static bool isEqual(const Register &LHS, const Register &RHS) {
  139. return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
  140. }
  141. };
  142. }
  143. #endif // LLVM_CODEGEN_REGISTER_H
  144. #ifdef __GNUC__
  145. #pragma GCC diagnostic pop
  146. #endif