1 //===-- llvm/MC/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_MC_MCREGISTER_H
10 #define LLVM_MC_MCREGISTER_H
11
12 #include "llvm/ADT/DenseMapInfo.h"
13 #include <cassert>
14 #include <limits>
15
16 namespace llvm {
17
18 /// An unsigned integer type large enough to represent all physical registers,
19 /// but not necessarily virtual registers.
20 using MCPhysReg = uint16_t;
21
22 /// Wrapper class representing physical registers. Should be passed by value.
23 class MCRegister {
24 friend hash_code hash_value(const MCRegister &);
25 unsigned Reg;
26
27 public:
28 constexpr MCRegister(unsigned Val = 0): Reg(Val) {}
29
30 // Register numbers can represent physical registers, virtual registers, and
31 // sometimes stack slots. The unsigned values are divided into these ranges:
32 //
33 // 0 Not a register, can be used as a sentinel.
34 // [1;2^30) Physical registers assigned by TableGen.
35 // [2^30;2^31) Stack slots. (Rarely used.)
36 // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
37 //
38 // Further sentinels can be allocated from the small negative integers.
39 // DenseMapInfo<unsigned> uses -1u and -2u.
40 static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
41 "Reg isn't large enough to hold full range.");
42 static constexpr unsigned NoRegister = 0u;
43 static constexpr unsigned FirstPhysicalReg = 1u;
44 static constexpr unsigned FirstStackSlot = 1u << 30;
45 static constexpr unsigned VirtualRegFlag = 1u << 31;
46
47 /// This is the portion of the positive number space that is not a physical
48 /// register. StackSlot values do not exist in the MC layer, see
49 /// Register::isStackSlot() for the more information on them.
50 ///
51 static bool isStackSlot(unsigned Reg) {
52 return FirstStackSlot <= Reg && Reg < VirtualRegFlag;
53 }
54
55 /// Return true if the specified register number is in
56 /// the physical register namespace.
57 static bool isPhysicalRegister(unsigned Reg) {
58 return FirstPhysicalReg <= Reg && Reg < FirstStackSlot;
59 }
60
61 constexpr operator unsigned() const {
62 return Reg;
63 }
64
65 /// Check the provided unsigned value is a valid MCRegister.
66 static MCRegister from(unsigned Val) {
67 assert(Val == NoRegister || isPhysicalRegister(Val));
68 return MCRegister(Val);
69 }
70
71 unsigned id() const {
72 return Reg;
73 }
74
75 bool isValid() const { return Reg != NoRegister; }
76
MCPseudoProbe(MCSymbol * Label,uint64_t Guid,uint64_t Index,uint64_t Type,uint64_t Attributes)77 /// Comparisons between register objects
78 bool operator==(const MCRegister &Other) const { return Reg == Other.Reg; }
79 bool operator!=(const MCRegister &Other) const { return Reg != Other.Reg; }
80
81 /// Comparisons against register constants. E.g.
82 /// * R == AArch64::WZR
83 /// * R == 0
84 /// * R == VirtRegMap::NO_PHYS_REG
85 bool operator==(unsigned Other) const { return Reg == Other; }
getLabel()86 bool operator!=(unsigned Other) const { return Reg != Other; }
87 bool operator==(int Other) const { return Reg == unsigned(Other); }
getGuid()88 bool operator!=(int Other) const { return Reg != unsigned(Other); }
89 // MSVC requires that we explicitly declare these two as well.
getIndex()90 bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
91 bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
getType()92 };
93
getAttributes()94 // Provide DenseMapInfo for MCRegister
95 template<> struct DenseMapInfo<MCRegister> {
96 static inline unsigned getEmptyKey() {
97 return DenseMapInfo<unsigned>::getEmptyKey();
98 }
99 static inline unsigned getTombstoneKey() {
100 return DenseMapInfo<unsigned>::getTombstoneKey();
101 }
102 static unsigned getHashValue(const MCRegister &Val) {
103 return DenseMapInfo<unsigned>::getHashValue(Val.id());
104 }
105 static bool isEqual(const MCRegister &LHS, const MCRegister &RHS) {
106 return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
107 }
108 };
109
110 inline hash_code hash_value(const MCRegister &Reg) {
111 return hash_value(Reg.id());
112 }
113 }
114
115 #endif // LLVM_MC_MCREGISTER_H
isRoot()116