1 //===-- llvm/CodeGen/Register.h ---------------------------------*- 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 #ifndef LLVM_CODEGEN_REGISTER_H
10 #define LLVM_CODEGEN_REGISTER_H
11 
12 #include "llvm/MC/MCRegister.h"
13 #include <cassert>
14 
15 namespace llvm {
16 
17 /// Wrapper class representing virtual and physical registers. Should be passed
18 /// by value.
19 class Register {
20   unsigned Reg;
21 
22 public:
23   constexpr Register(unsigned Val = 0): Reg(Val) {}
24   constexpr Register(MCRegister Val): Reg(Val) {}
25 
26   // Register numbers can represent physical registers, virtual registers, and
27   // sometimes stack slots. The unsigned values are divided into these ranges:
28   //
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.
33   //
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.");
38 
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.
42   ///
43   static bool isStackSlot(unsigned Reg) {
44     return MCRegister::isStackSlot(Reg);
45   }
46 
47   /// Compute the frame index from a register value representing a stack slot.
48   static int stackSlot2Index(unsigned Reg) {
49     assert(isStackSlot(Reg) && "Not a stack slot");
50     return int(Reg - MCRegister::FirstStackSlot);
51   }
52 
53   /// Convert a non-negative frame index to a stack slot register value.
54   static unsigned index2StackSlot(int FI) {
55     assert(FI >= 0 && "Cannot hold a negative frame index.");
56     return FI + MCRegister::FirstStackSlot;
57   }
58 
59   /// Return true if the specified register number is in
60   /// the physical register namespace.
61   static bool isPhysicalRegister(unsigned Reg) {
62     return MCRegister::isPhysicalRegister(Reg);
63   }
64 
65   /// Return true if the specified register number is in
66   /// the virtual register namespace.
67   static bool isVirtualRegister(unsigned Reg) {
68     return Reg & MCRegister::VirtualRegFlag && !isStackSlot(Reg);
69   }
70 
71   /// Convert a virtual register number to a 0-based index.
72   /// The first virtual register in a function will get the index 0.
73   static unsigned virtReg2Index(unsigned Reg) {
74     assert(isVirtualRegister(Reg) && "Not a virtual register");
75     return Reg & ~MCRegister::VirtualRegFlag;
76   }
77 
78   /// Convert a 0-based index to a virtual register number.
79   /// This is the inverse operation of VirtReg2IndexFunctor below.
80   static unsigned index2VirtReg(unsigned Index) {
81     assert(Index < (1u << 31) && "Index too large for virtual register range.");
82     return Index | MCRegister::VirtualRegFlag;
83   }
84 
85   /// Return true if the specified register number is in the virtual register
86   /// namespace.
87   bool isVirtual() const {
88     return isVirtualRegister(Reg);
89   }
90 
91   /// Return true if the specified register number is in the physical register
92   /// namespace.
93   bool isPhysical() const {
94     return isPhysicalRegister(Reg);
95   }
96 
97   /// Convert a virtual register number to a 0-based index. The first virtual
98   /// register in a function will get the index 0.
99   unsigned virtRegIndex() const {
100     return virtReg2Index(Reg);
101   }
102 
103   constexpr operator unsigned() const {
104     return Reg;
105   }
106 
107   unsigned id() const { return Reg; }
108 
109   operator MCRegister() const {
110     return MCRegister(Reg);
111   }
112 
113   /// Utility to check-convert this value to a MCRegister. The caller is
114   /// expected to have already validated that this Register is, indeed,
115   /// physical.
116   MCRegister asMCReg() const {
117     assert(Reg == MCRegister::NoRegister ||
118            MCRegister::isPhysicalRegister(Reg));
119     return MCRegister(Reg);
120   }
121 
122   bool isValid() const { return Reg != MCRegister::NoRegister; }
123 
124   /// Comparisons between register objects
125   bool operator==(const Register &Other) const { return Reg == Other.Reg; }
126   bool operator!=(const Register &Other) const { return Reg != Other.Reg; }
127   bool operator==(const MCRegister &Other) const { return Reg == Other.id(); }
128   bool operator!=(const MCRegister &Other) const { return Reg != Other.id(); }
129 
130   /// Comparisons against register constants. E.g.
131   /// * R == AArch64::WZR
132   /// * R == 0
133   /// * R == VirtRegMap::NO_PHYS_REG
134   bool operator==(unsigned Other) const { return Reg == Other; }
135   bool operator!=(unsigned Other) const { return Reg != Other; }
136   bool operator==(int Other) const { return Reg == unsigned(Other); }
137   bool operator!=(int Other) const { return Reg != unsigned(Other); }
138   // MSVC requires that we explicitly declare these two as well.
139   bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
140   bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
141 };
142 
143 // Provide DenseMapInfo for Register
144 template<> struct DenseMapInfo<Register> {
145   static inline unsigned getEmptyKey() {
146     return DenseMapInfo<unsigned>::getEmptyKey();
147   }
148   static inline unsigned getTombstoneKey() {
149     return DenseMapInfo<unsigned>::getTombstoneKey();
150   }
151   static unsigned getHashValue(const Register &Val) {
152     return DenseMapInfo<unsigned>::getHashValue(Val.id());
153   }
154   static bool isEqual(const Register &LHS, const Register &RHS) {
155     return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
156   }
157 };
158 
159 }
160 
161 #endif // ifndef LLVM_CODEGEN_REGISTER_H
162