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   Register(unsigned Val = 0): Reg(Val) {}
24   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 
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   /// Note that isVirtualRegister() and isPhysicalRegister() cannot handle stack
42   /// slots, so if a variable may contains a stack slot, always check
43   /// isStackSlot() first.
44   ///
45   static bool isStackSlot(unsigned Reg) {
46     return MCRegister::isStackSlot(Reg);
47   }
48 
49   /// Compute the frame index from a register value representing a stack slot.
50   static int stackSlot2Index(unsigned Reg) {
51     assert(isStackSlot(Reg) && "Not a stack slot");
52     return int(Reg - (1u << 30));
53   }
54 
55   /// Convert a non-negative frame index to a stack slot register value.
56   static unsigned index2StackSlot(int FI) {
57     assert(FI >= 0 && "Cannot hold a negative frame index.");
58     return FI + (1u << 30);
59   }
60 
61   /// Return true if the specified register number is in
62   /// the physical register namespace.
63   static bool isPhysicalRegister(unsigned Reg) {
64     return MCRegister::isPhysicalRegister(Reg);
65   }
66 
67   /// Return true if the specified register number is in
68   /// the virtual register namespace.
69   static bool isVirtualRegister(unsigned Reg) {
70     assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
71     return int(Reg) < 0;
72   }
73 
74   /// Convert a virtual register number to a 0-based index.
75   /// The first virtual register in a function will get the index 0.
76   static unsigned virtReg2Index(unsigned Reg) {
77     assert(isVirtualRegister(Reg) && "Not a virtual register");
78     return Reg & ~(1u << 31);
79   }
80 
81   /// Convert a 0-based index to a virtual register number.
82   /// This is the inverse operation of VirtReg2IndexFunctor below.
83   static unsigned index2VirtReg(unsigned Index) {
84     return Index | (1u << 31);
85   }
86 
87   /// Return true if the specified register number is in the virtual register
88   /// namespace.
89   bool isVirtual() const {
90     return isVirtualRegister(Reg);
91   }
92 
93   /// Return true if the specified register number is in the physical register
94   /// namespace.
95   bool isPhysical() const {
96     return isPhysicalRegister(Reg);
97   }
98 
99   /// Convert a virtual register number to a 0-based index. The first virtual
100   /// register in a function will get the index 0.
101   unsigned virtRegIndex() const {
102     return virtReg2Index(Reg);
103   }
104 
105   operator unsigned() const {
106     return Reg;
107   }
108 
109   unsigned id() const { return Reg; }
110 
111   operator MCRegister() const {
112     return MCRegister(Reg);
113   }
114 
115   bool isValid() const {
116     return Reg != 0;
117   }
118 
119   /// Comparisons between register objects
120   bool operator==(const Register &Other) const { return Reg == Other.Reg; }
121   bool operator!=(const Register &Other) const { return Reg != Other.Reg; }
122   bool operator==(const MCRegister &Other) const { return Reg == Other.id(); }
123   bool operator!=(const MCRegister &Other) const { return Reg != Other.id(); }
124 
125   /// Comparisons against register constants. E.g.
126   /// * R == AArch64::WZR
127   /// * R == 0
128   /// * R == VirtRegMap::NO_PHYS_REG
129   bool operator==(unsigned Other) const { return Reg == Other; }
130   bool operator!=(unsigned Other) const { return Reg != Other; }
131   bool operator==(int Other) const { return Reg == unsigned(Other); }
132   bool operator!=(int Other) const { return Reg != unsigned(Other); }
133   // MSVC requires that we explicitly declare these two as well.
134   bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
135   bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
136 };
137 
138 // Provide DenseMapInfo for Register
139 template<> struct DenseMapInfo<Register> {
140   static inline unsigned getEmptyKey() {
141     return DenseMapInfo<unsigned>::getEmptyKey();
142   }
143   static inline unsigned getTombstoneKey() {
144     return DenseMapInfo<unsigned>::getTombstoneKey();
145   }
146   static unsigned getHashValue(const Register &Val) {
147     return DenseMapInfo<unsigned>::getHashValue(Val.id());
148   }
149   static bool isEqual(const Register &LHS, const Register &RHS) {
150     return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
151   }
152 };
153 
154 }
155 
156 #endif // ifndef LLVM_CODEGEN_REGISTER_H
157