1 //===-- llvm/MC/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_MC_REGISTER_H
10 #define LLVM_MC_REGISTER_H
12 #include "llvm/ADT/DenseMapInfo.h"
17 /// An unsigned integer type large enough to represent all physical registers,
18 /// but not necessarily virtual registers.
19 using MCPhysReg
= uint16_t;
21 /// Wrapper class representing physical registers. Should be passed by value.
26 MCRegister(unsigned Val
= 0): Reg(Val
) {}
28 // Register numbers can represent physical registers, virtual registers, and
29 // sometimes stack slots. The unsigned values are divided into these ranges:
31 // 0 Not a register, can be used as a sentinel.
32 // [1;2^30) Physical registers assigned by TableGen.
33 // [2^30;2^31) Stack slots. (Rarely used.)
34 // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
36 // Further sentinels can be allocated from the small negative integers.
37 // DenseMapInfo<unsigned> uses -1u and -2u.
39 /// This is the portion of the positive number space that is not a physical
40 /// register. StackSlot values do not exist in the MC layer, see
41 /// Register::isStackSlot() for the more information on them.
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.
46 static bool isStackSlot(unsigned Reg
) {
47 return int(Reg
) >= (1 << 30);
50 /// Return true if the specified register number is in
51 /// the physical register namespace.
52 static bool isPhysicalRegister(unsigned Reg
) {
53 assert(!isStackSlot(Reg
) && "Not a register! Check isStackSlot() first.");
57 /// Return true if the specified register number is in the physical register
59 bool isPhysical() const {
60 return isPhysicalRegister(Reg
);
63 operator unsigned() const {
71 bool isValid() const {
75 /// Comparisons between register objects
76 bool operator==(const MCRegister
&Other
) const { return Reg
== Other
.Reg
; }
77 bool operator!=(const MCRegister
&Other
) const { return Reg
!= Other
.Reg
; }
79 /// Comparisons against register constants. E.g.
80 /// * R == AArch64::WZR
82 /// * R == VirtRegMap::NO_PHYS_REG
83 bool operator==(unsigned Other
) const { return Reg
== Other
; }
84 bool operator!=(unsigned Other
) const { return Reg
!= Other
; }
85 bool operator==(int Other
) const { return Reg
== unsigned(Other
); }
86 bool operator!=(int Other
) const { return Reg
!= unsigned(Other
); }
87 // MSVC requires that we explicitly declare these two as well.
88 bool operator==(MCPhysReg Other
) const { return Reg
== unsigned(Other
); }
89 bool operator!=(MCPhysReg Other
) const { return Reg
!= unsigned(Other
); }
92 // Provide DenseMapInfo for MCRegister
93 template<> struct DenseMapInfo
<MCRegister
> {
94 static inline unsigned getEmptyKey() {
95 return DenseMapInfo
<unsigned>::getEmptyKey();
97 static inline unsigned getTombstoneKey() {
98 return DenseMapInfo
<unsigned>::getTombstoneKey();
100 static unsigned getHashValue(const MCRegister
&Val
) {
101 return DenseMapInfo
<unsigned>::getHashValue(Val
.id());
103 static bool isEqual(const MCRegister
&LHS
, const MCRegister
&RHS
) {
104 return DenseMapInfo
<unsigned>::isEqual(LHS
.id(), RHS
.id());
110 #endif // ifndef LLVM_MC_REGISTER_H