1 //===-- llvm/CodeGen/Register.h ---------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CODEGEN_REGISTER_H
10 #define LLVM_CODEGEN_REGISTER_H
12 #include "llvm/MC/MCRegister.h"
17 /// Wrapper class representing virtual and physical registers. Should be passed
23 constexpr Register(unsigned Val = 0): Reg(Val) {}
24 constexpr Register(MCRegister Val): Reg(Val) {}
26 // Register numbers can represent physical registers, virtual registers, and
27 // sometimes stack slots. The unsigned values are divided into these ranges:
29 // 0 Not a register, can be used as a sentinel.
30 // [1;2^30) Physical registers assigned by TableGen.
31 // [2^30;2^31) Stack slots. (Rarely used.)
32 // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
34 // Further sentinels can be allocated from the small negative integers.
35 // DenseMapInfo<unsigned> uses -1u and -2u.
36 static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
37 "Reg isn't large enough to hold full range.");
39 /// isStackSlot - Sometimes it is useful the be able to store a non-negative
40 /// frame index in a variable that normally holds a register. isStackSlot()
41 /// returns true if Reg is in the range used for stack slots.
43 /// Note that isVirtualRegister() and isPhysicalRegister() cannot handle stack
44 /// slots, so if a variable may contains a stack slot, always check
45 /// isStackSlot() first.
47 static bool isStackSlot(unsigned Reg) {
48 return MCRegister::isStackSlot(Reg);
51 /// Compute the frame index from a register value representing a stack slot.
52 static int stackSlot2Index(unsigned Reg) {
53 assert(isStackSlot(Reg) && "Not a stack slot");
54 return int(Reg - MCRegister::FirstStackSlot);
57 /// Convert a non-negative frame index to a stack slot register value.
58 static unsigned index2StackSlot(int FI) {
59 assert(FI >= 0 && "Cannot hold a negative frame index.");
60 return FI + MCRegister::FirstStackSlot;
63 /// Return true if the specified register number is in
64 /// the physical register namespace.
65 static bool isPhysicalRegister(unsigned Reg) {
66 return MCRegister::isPhysicalRegister(Reg);
69 /// Return true if the specified register number is in
70 /// the virtual register namespace.
71 static bool isVirtualRegister(unsigned Reg) {
72 assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
73 return Reg & MCRegister::VirtualRegFlag;
76 /// Convert a virtual register number to a 0-based index.
77 /// The first virtual register in a function will get the index 0.
78 static unsigned virtReg2Index(unsigned Reg) {
79 assert(isVirtualRegister(Reg) && "Not a virtual register");
80 return Reg & ~MCRegister::VirtualRegFlag;
83 /// Convert a 0-based index to a virtual register number.
84 /// This is the inverse operation of VirtReg2IndexFunctor below.
85 static unsigned index2VirtReg(unsigned Index) {
86 assert(Index < (1u << 31) && "Index too large for virtual register range.");
87 return Index | MCRegister::VirtualRegFlag;
90 /// Return true if the specified register number is in the virtual register
92 bool isVirtual() const {
93 return isVirtualRegister(Reg);
96 /// Return true if the specified register number is in the physical register
98 bool isPhysical() const {
99 return isPhysicalRegister(Reg);
102 /// Convert a virtual register number to a 0-based index. The first virtual
103 /// register in a function will get the index 0.
104 unsigned virtRegIndex() const {
105 return virtReg2Index(Reg);
108 constexpr operator unsigned() const {
112 unsigned id() const { return Reg; }
114 operator MCRegister() const {
115 return MCRegister(Reg);
118 bool isValid() const { return Reg != MCRegister::NoRegister; }
120 /// Comparisons between register objects
121 bool operator==(const Register &Other) const { return Reg == Other.Reg; }
122 bool operator!=(const Register &Other) const { return Reg != Other.Reg; }
123 bool operator==(const MCRegister &Other) const { return Reg == Other.id(); }
124 bool operator!=(const MCRegister &Other) const { return Reg != Other.id(); }
126 /// Comparisons against register constants. E.g.
127 /// * R == AArch64::WZR
129 /// * R == VirtRegMap::NO_PHYS_REG
130 bool operator==(unsigned Other) const { return Reg == Other; }
131 bool operator!=(unsigned Other) const { return Reg != Other; }
132 bool operator==(int Other) const { return Reg == unsigned(Other); }
133 bool operator!=(int Other) const { return Reg != unsigned(Other); }
134 // MSVC requires that we explicitly declare these two as well.
135 bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
136 bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
139 // Provide DenseMapInfo for Register
140 template<> struct DenseMapInfo<Register> {
141 static inline unsigned getEmptyKey() {
142 return DenseMapInfo<unsigned>::getEmptyKey();
144 static inline unsigned getTombstoneKey() {
145 return DenseMapInfo<unsigned>::getTombstoneKey();
147 static unsigned getHashValue(const Register &Val) {
148 return DenseMapInfo<unsigned>::getHashValue(Val.id());
150 static bool isEqual(const Register &LHS, const Register &RHS) {
151 return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
157 #endif // ifndef LLVM_CODEGEN_REGISTER_H