LLVM 22.0.0git
Register.h
Go to the documentation of this file.
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
15namespace llvm {
16
17/// Wrapper class representing virtual and physical registers. Should be passed
18/// by value.
19class Register {
20 unsigned Reg;
21
22public:
23 constexpr Register(unsigned Val = 0) : Reg(Val) {}
24 constexpr Register(MCRegister Val) : Reg(Val.id()) {}
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 static constexpr unsigned FirstStackSlot = 1u << 30;
40 static constexpr unsigned VirtualRegFlag = 1u << 31;
41
42 /// Return true if this is a stack slot.
43 constexpr bool isStack() const {
45 }
46
47 /// Convert a non-negative frame index to a stack slot register value.
48 static Register index2StackSlot(int FI) {
49 assert(FI >= 0 && "Cannot hold a negative frame index.");
51 }
52
53 /// Return true if the specified register number is in
54 /// the physical register namespace.
55 static constexpr bool isPhysicalRegister(unsigned Reg) {
57 }
58
59 /// Return true if the specified register number is in
60 /// the virtual register namespace.
61 static constexpr bool isVirtualRegister(unsigned Reg) {
62 return Reg & Register::VirtualRegFlag;
63 }
64
65 /// Convert a 0-based index to a virtual register number.
66 /// This is the inverse operation of VirtReg2IndexFunctor below.
67 static Register index2VirtReg(unsigned Index) {
68 assert(Index < (1u << 31) && "Index too large for virtual register range.");
69 return Index | Register::VirtualRegFlag;
70 }
71
72 /// Return true if the specified register number is in the virtual register
73 /// namespace.
74 constexpr bool isVirtual() const { return isVirtualRegister(Reg); }
75
76 /// Return true if the specified register number is in the physical register
77 /// namespace.
78 constexpr bool isPhysical() const { return isPhysicalRegister(Reg); }
79
80 /// Convert a virtual register number to a 0-based index. The first virtual
81 /// register in a function will get the index 0.
82 unsigned virtRegIndex() const {
83 assert(isVirtual() && "Not a virtual register");
84 return Reg & ~Register::VirtualRegFlag;
85 }
86
87 /// Compute the frame index from a register value representing a stack slot.
88 int stackSlotIndex() const {
89 assert(isStack() && "Not a stack slot");
90 return static_cast<int>(Reg - Register::FirstStackSlot);
91 }
92
93 constexpr operator unsigned() const { return Reg; }
94
95 constexpr unsigned id() const { return Reg; }
96
97 constexpr operator MCRegister() const { return MCRegister(Reg); }
98
99 /// Utility to check-convert this value to a MCRegister. The caller is
100 /// expected to have already validated that this Register is, indeed,
101 /// physical.
103 assert(!isValid() || isPhysical());
104 return MCRegister(Reg);
105 }
106
107 constexpr bool isValid() const { return Reg != MCRegister::NoRegister; }
108
109 /// Comparisons between register objects
110 constexpr bool operator==(const Register &Other) const {
111 return Reg == Other.Reg;
112 }
113 constexpr bool operator!=(const Register &Other) const {
114 return Reg != Other.Reg;
115 }
116 constexpr bool operator==(const MCRegister &Other) const {
117 return Reg == Other.id();
118 }
119 constexpr bool operator!=(const MCRegister &Other) const {
120 return Reg != Other.id();
121 }
122
123 /// Comparisons against register constants. E.g.
124 /// * R == AArch64::WZR
125 /// * R == 0
126 constexpr bool operator==(unsigned Other) const { return Reg == Other; }
127 constexpr bool operator!=(unsigned Other) const { return Reg != Other; }
128 constexpr bool operator==(int Other) const { return Reg == unsigned(Other); }
129 constexpr bool operator!=(int Other) const { return Reg != unsigned(Other); }
130 // MSVC requires that we explicitly declare these two as well.
131 constexpr bool operator==(MCPhysReg Other) const {
132 return Reg == unsigned(Other);
133 }
134 constexpr bool operator!=(MCPhysReg Other) const {
135 return Reg != unsigned(Other);
136 }
137
138 /// Operators to move from one register to another nearby register by adding
139 /// an offset.
141 assert(isValid());
142 ++Reg;
143 return *this;
144 }
145
147 Register R(*this);
148 ++(*this);
149 return R;
150 }
151
153 assert(isValid());
154 Reg += RHS;
155 return *this;
156 }
157};
158
159// Provide DenseMapInfo for Register
160template <> struct DenseMapInfo<Register> {
161 static inline Register getEmptyKey() {
162 return DenseMapInfo<unsigned>::getEmptyKey();
163 }
164 static inline Register getTombstoneKey() {
165 return DenseMapInfo<unsigned>::getTombstoneKey();
166 }
167 static unsigned getHashValue(const Register &Val) {
168 return DenseMapInfo<unsigned>::getHashValue(Val.id());
169 }
170 static bool isEqual(const Register &LHS, const Register &RHS) {
171 return LHS == RHS;
172 }
173};
174
175/// Wrapper class representing a virtual register or register unit.
177 unsigned VRegOrUnit;
178
179public:
180 constexpr explicit VirtRegOrUnit(MCRegUnit Unit) : VRegOrUnit(Unit) {
182 }
183 constexpr explicit VirtRegOrUnit(Register Reg) : VRegOrUnit(Reg.id()) {
184 assert(Reg.isVirtual());
185 }
186
187 constexpr bool isVirtualReg() const {
188 return Register::isVirtualRegister(VRegOrUnit);
189 }
190
191 constexpr MCRegUnit asMCRegUnit() const {
192 assert(!isVirtualReg() && "Not a register unit");
193 return VRegOrUnit;
194 }
195
196 constexpr Register asVirtualReg() const {
197 assert(isVirtualReg() && "Not a virtual register");
198 return Register(VRegOrUnit);
199 }
200
201 constexpr bool operator==(const VirtRegOrUnit &Other) const {
202 return VRegOrUnit == Other.VRegOrUnit;
203 }
204};
205
206} // namespace llvm
207
208#endif // LLVM_CODEGEN_REGISTER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
Value * RHS
Value * LHS
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
static constexpr unsigned LastPhysicalReg
Definition MCRegister.h:54
static constexpr unsigned NoRegister
Definition MCRegister.h:52
static constexpr bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition MCRegister.h:58
Wrapper class representing virtual and physical registers.
Definition Register.h:19
constexpr bool isStack() const
Return true if this is a stack slot.
Definition Register.h:43
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:67
constexpr Register(unsigned Val=0)
Definition Register.h:23
constexpr bool operator==(const Register &Other) const
Comparisons between register objects.
Definition Register.h:110
constexpr bool operator!=(const Register &Other) const
Definition Register.h:113
Register & operator++()
Operators to move from one register to another nearby register by adding an offset.
Definition Register.h:140
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:102
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
Definition Register.h:82
int stackSlotIndex() const
Compute the frame index from a register value representing a stack slot.
Definition Register.h:88
constexpr bool operator==(const MCRegister &Other) const
Definition Register.h:116
constexpr bool operator==(unsigned Other) const
Comparisons against register constants.
Definition Register.h:126
constexpr bool isValid() const
Definition Register.h:107
constexpr bool operator!=(int Other) const
Definition Register.h:129
constexpr Register(MCRegister Val)
Definition Register.h:24
static Register index2StackSlot(int FI)
Convert a non-negative frame index to a stack slot register value.
Definition Register.h:48
constexpr bool operator!=(const MCRegister &Other) const
Definition Register.h:119
static constexpr unsigned FirstStackSlot
Definition Register.h:38
static constexpr unsigned VirtualRegFlag
Definition Register.h:40
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
constexpr bool operator!=(MCPhysReg Other) const
Definition Register.h:134
constexpr bool operator!=(unsigned Other) const
Definition Register.h:127
static constexpr bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:61
constexpr bool operator==(int Other) const
Definition Register.h:128
Register & operator+=(unsigned RHS)
Definition Register.h:152
Register operator++(int)
Definition Register.h:146
constexpr bool operator==(MCPhysReg Other) const
Definition Register.h:131
static constexpr bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition Register.h:55
constexpr unsigned id() const
Definition Register.h:95
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:78
constexpr bool operator==(const VirtRegOrUnit &Other) const
Definition Register.h:201
constexpr bool isVirtualReg() const
Definition Register.h:187
constexpr VirtRegOrUnit(MCRegUnit Unit)
Definition Register.h:180
constexpr VirtRegOrUnit(Register Reg)
Definition Register.h:183
constexpr MCRegUnit asMCRegUnit() const
Definition Register.h:191
constexpr Register asVirtualReg() const
Definition Register.h:196
This is an optimization pass for GlobalISel generic memory operations.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
bool isEqual(const GCNRPTracker::LiveRegSet &S1, const GCNRPTracker::LiveRegSet &S2)
unsigned MCRegUnit
Register units are used to compute register aliasing.
Definition MCRegister.h:30
@ Other
Any other memory.
Definition ModRef.h:68
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21