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"
14#include <cassert>
15
16namespace llvm {
17
18/// Wrapper class representing virtual and physical registers. Should be passed
19/// by value.
20class Register {
21 unsigned Reg;
22
23public:
24 constexpr Register(unsigned Val = 0) : Reg(Val) {}
25 constexpr Register(MCRegister Val) : Reg(Val.id()) {}
26
27 // Register numbers can represent physical registers, virtual registers, and
28 // sometimes stack slots. The unsigned values are divided into these ranges:
29 //
30 // 0 Not a register, can be used as a sentinel.
31 // [1;2^30) Physical registers assigned by TableGen.
32 // [2^30;2^31) Stack slots. (Rarely used.)
33 // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
34 //
35 // Further sentinels can be allocated from the small negative integers.
36 // DenseMapInfo<unsigned> uses -1u and -2u.
37 static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
38 "Reg isn't large enough to hold full range.");
39 static constexpr unsigned MaxFrameIndexBitwidth = 30;
40 static constexpr unsigned StackSlotZero = 1u << MaxFrameIndexBitwidth;
41 static constexpr const unsigned StackSlotMask = StackSlotZero - 1;
43 static constexpr unsigned VirtualRegFlag = 1u << 31;
44
45 /// Return true if this is a stack slot.
46 constexpr bool isStack() const {
48 }
49
50 /// Convert a non-negative frame index to a stack slot register value.
51 static Register index2StackSlot(int FI) {
53 "Frame index must be at most 30 bits.");
54 unsigned FIMasked = FI & Register::StackSlotMask;
55 return Register(FIMasked | Register::StackSlotZero);
56 }
57
58 /// Return true if the specified register number is in
59 /// the physical register namespace.
60 static constexpr bool isPhysicalRegister(unsigned Reg) {
62 }
63
64 /// Return true if the specified register number is in
65 /// the virtual register namespace.
66 static constexpr bool isVirtualRegister(unsigned Reg) {
67 return Reg & Register::VirtualRegFlag;
68 }
69
70 /// Convert a 0-based index to a virtual register number.
71 /// This is the inverse operation of VirtReg2IndexFunctor below.
72 static Register index2VirtReg(unsigned Index) {
73 assert(Index < (1u << 31) && "Index too large for virtual register range.");
74 return Index | Register::VirtualRegFlag;
75 }
76
77 /// Return true if the specified register number is in the virtual register
78 /// namespace.
79 constexpr bool isVirtual() const { return isVirtualRegister(Reg); }
80
81 /// Return true if the specified register number is in the physical register
82 /// namespace.
83 constexpr bool isPhysical() const { return isPhysicalRegister(Reg); }
84
85 /// Convert a virtual register number to a 0-based index. The first virtual
86 /// register in a function will get the index 0.
87 unsigned virtRegIndex() const {
88 assert(isVirtual() && "Not a virtual register");
89 return Reg & ~Register::VirtualRegFlag;
90 }
91
92 /// Compute the frame index from a register value representing a stack slot.
93 int stackSlotIndex() const {
94 assert(isStack() && "Not a stack slot");
96 }
97
98 constexpr operator unsigned() const { return Reg; }
99
100 constexpr unsigned id() const { return Reg; }
101
102 constexpr operator MCRegister() const { return MCRegister(Reg); }
103
104 /// Utility to check-convert this value to a MCRegister. The caller is
105 /// expected to have already validated that this Register is, indeed,
106 /// physical.
108 assert(!isValid() || isPhysical());
109 return MCRegister(Reg);
110 }
111
112 constexpr bool isValid() const { return Reg != MCRegister::NoRegister; }
113
114 /// Comparisons between register objects
115 constexpr bool operator==(const Register &Other) const {
116 return Reg == Other.Reg;
117 }
118 constexpr bool operator!=(const Register &Other) const {
119 return Reg != Other.Reg;
120 }
121 constexpr bool operator==(const MCRegister &Other) const {
122 return Reg == Other.id();
123 }
124 constexpr bool operator!=(const MCRegister &Other) const {
125 return Reg != Other.id();
126 }
127
128 /// Comparisons against register constants. E.g.
129 /// * R == AArch64::WZR
130 /// * R == 0
131 constexpr bool operator==(unsigned Other) const { return Reg == Other; }
132 constexpr bool operator!=(unsigned Other) const { return Reg != Other; }
133 constexpr bool operator==(int Other) const { return Reg == unsigned(Other); }
134 constexpr bool operator!=(int Other) const { return Reg != unsigned(Other); }
135 // MSVC requires that we explicitly declare these two as well.
136 constexpr bool operator==(MCPhysReg Other) const {
137 return Reg == unsigned(Other);
138 }
139 constexpr bool operator!=(MCPhysReg Other) const {
140 return Reg != unsigned(Other);
141 }
142
143 /// Operators to move from one register to another nearby register by adding
144 /// an offset.
146 assert(isValid());
147 ++Reg;
148 return *this;
149 }
150
152 Register R(*this);
153 ++(*this);
154 return R;
155 }
156
158 assert(isValid());
159 Reg += RHS;
160 return *this;
161 }
162};
163
164// Provide DenseMapInfo for Register
165template <> struct DenseMapInfo<Register> {
166 static inline Register getEmptyKey() {
167 return DenseMapInfo<unsigned>::getEmptyKey();
168 }
169 static inline Register getTombstoneKey() {
170 return DenseMapInfo<unsigned>::getTombstoneKey();
171 }
172 static unsigned getHashValue(const Register &Val) {
173 return DenseMapInfo<unsigned>::getHashValue(Val.id());
174 }
175 static bool isEqual(const Register &LHS, const Register &RHS) {
176 return LHS == RHS;
177 }
178};
179
180/// Wrapper class representing a virtual register or register unit.
182 unsigned VRegOrUnit;
183
184public:
185 constexpr explicit VirtRegOrUnit(MCRegUnit Unit) : VRegOrUnit(Unit) {
187 }
188 constexpr explicit VirtRegOrUnit(Register Reg) : VRegOrUnit(Reg.id()) {
189 assert(Reg.isVirtual());
190 }
191
192 constexpr bool isVirtualReg() const {
193 return Register::isVirtualRegister(VRegOrUnit);
194 }
195
196 constexpr MCRegUnit asMCRegUnit() const {
197 assert(!isVirtualReg() && "Not a register unit");
198 return VRegOrUnit;
199 }
200
201 constexpr Register asVirtualReg() const {
202 assert(isVirtualReg() && "Not a virtual register");
203 return Register(VRegOrUnit);
204 }
205
206 constexpr bool operator==(const VirtRegOrUnit &Other) const {
207 return VRegOrUnit == Other.VRegOrUnit;
208 }
209};
210
211} // namespace llvm
212
213#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:20
constexpr bool isStack() const
Return true if this is a stack slot.
Definition Register.h:46
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
constexpr Register(unsigned Val=0)
Definition Register.h:24
constexpr bool operator==(const Register &Other) const
Comparisons between register objects.
Definition Register.h:115
constexpr bool operator!=(const Register &Other) const
Definition Register.h:118
Register & operator++()
Operators to move from one register to another nearby register by adding an offset.
Definition Register.h:145
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
static constexpr unsigned StackSlotZero
Definition Register.h:40
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
Definition Register.h:87
int stackSlotIndex() const
Compute the frame index from a register value representing a stack slot.
Definition Register.h:93
constexpr bool operator==(const MCRegister &Other) const
Definition Register.h:121
constexpr bool operator==(unsigned Other) const
Comparisons against register constants.
Definition Register.h:131
constexpr bool isValid() const
Definition Register.h:112
constexpr bool operator!=(int Other) const
Definition Register.h:134
static constexpr const unsigned StackSlotMask
Definition Register.h:41
constexpr Register(MCRegister Val)
Definition Register.h:25
static Register index2StackSlot(int FI)
Convert a non-negative frame index to a stack slot register value.
Definition Register.h:51
constexpr bool operator!=(const MCRegister &Other) const
Definition Register.h:124
static constexpr unsigned MaxFrameIndexBitwidth
Definition Register.h:39
static constexpr unsigned VirtualRegFlag
Definition Register.h:43
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool operator!=(MCPhysReg Other) const
Definition Register.h:139
constexpr bool operator!=(unsigned Other) const
Definition Register.h:132
static constexpr bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:66
constexpr bool operator==(int Other) const
Definition Register.h:133
Register & operator+=(unsigned RHS)
Definition Register.h:157
Register operator++(int)
Definition Register.h:151
constexpr bool operator==(MCPhysReg Other) const
Definition Register.h:136
static constexpr bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition Register.h:60
constexpr unsigned id() const
Definition Register.h:100
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
constexpr bool operator==(const VirtRegOrUnit &Other) const
Definition Register.h:206
constexpr bool isVirtualReg() const
Definition Register.h:192
constexpr VirtRegOrUnit(MCRegUnit Unit)
Definition Register.h:185
constexpr VirtRegOrUnit(Register Reg)
Definition Register.h:188
constexpr MCRegUnit asMCRegUnit() const
Definition Register.h:196
constexpr Register asVirtualReg() const
Definition Register.h:201
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)
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
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
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
Definition MathExtras.h:554