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 "llvm/ADT/Hashing.h"
14#include <cassert>
15#include <limits>
16
17namespace llvm {
18
19/// An unsigned integer type large enough to represent all physical registers,
20/// but not necessarily virtual registers.
21using MCPhysReg = uint16_t;
22
23/// Register units are used to compute register aliasing. Every register has at
24/// least one register unit, but it can have more. Two registers overlap if and
25/// only if they have a common register unit.
26///
27/// A target with a complicated sub-register structure will typically have many
28/// fewer register units than actual registers. MCRI::getNumRegUnits() returns
29/// the number of register units in the target.
30enum class MCRegUnit : unsigned;
31
32struct MCRegUnitToIndex {
33 using argument_type = MCRegUnit;
34
35 unsigned operator()(MCRegUnit Unit) const {
36 return static_cast<unsigned>(Unit);
37 }
38};
39
40/// Wrapper class representing physical registers. Should be passed by value.
41class MCRegister {
42 friend hash_code hash_value(const MCRegister &);
43 unsigned Reg;
44
45public:
46 constexpr MCRegister(unsigned Val = 0) : Reg(Val) {}
47
48 // Register numbers can represent physical registers, virtual registers, and
49 // sometimes stack slots. The unsigned values are divided into these ranges:
50 //
51 // 0 Not a register, can be used as a sentinel.
52 // [1;2^30) Physical registers assigned by TableGen.
53 // [2^30;2^31) Stack slots. (Rarely used.)
54 // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
55 //
56 // Further sentinels can be allocated from the small negative integers.
57 // DenseMapInfo<unsigned> uses -1u and -2u.
58 static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
59 "Reg isn't large enough to hold full range.");
60 static constexpr unsigned NoRegister = 0u;
61 static constexpr unsigned FirstPhysicalReg = 1u;
62 static constexpr unsigned LastPhysicalReg = (1u << 30) - 1;
63
64 /// Return true if the specified register number is in
65 /// the physical register namespace.
66 static constexpr bool isPhysicalRegister(unsigned Reg) {
67 return FirstPhysicalReg <= Reg && Reg <= LastPhysicalReg;
68 }
69
70 /// Return true if the specified register number is in the physical register
71 /// namespace.
72 constexpr bool isPhysical() const { return isPhysicalRegister(Reg); }
73
74 constexpr operator unsigned() const { return Reg; }
75
76 /// Check the provided unsigned value is a valid MCRegister.
77 static MCRegister from(unsigned Val) {
78 assert(Val == NoRegister || isPhysicalRegister(Val));
79 return MCRegister(Val);
80 }
81
82 constexpr unsigned id() const { return Reg; }
83
84 constexpr bool isValid() const { return Reg != NoRegister; }
85
86 /// Comparisons between register objects
87 constexpr bool operator==(const MCRegister &Other) const {
88 return Reg == Other.Reg;
89 }
90 constexpr bool operator!=(const MCRegister &Other) const {
91 return Reg != Other.Reg;
92 }
93
94 /// Comparisons against register constants. E.g.
95 /// * R == AArch64::WZR
96 /// * R == 0
97 constexpr bool operator==(unsigned Other) const { return Reg == Other; }
98 constexpr bool operator!=(unsigned Other) const { return Reg != Other; }
99 constexpr bool operator==(int Other) const { return Reg == unsigned(Other); }
100 constexpr bool operator!=(int Other) const { return Reg != unsigned(Other); }
101 // MSVC requires that we explicitly declare these two as well.
102 constexpr bool operator==(MCPhysReg Other) const {
103 return Reg == unsigned(Other);
104 }
105 constexpr bool operator!=(MCPhysReg Other) const {
106 return Reg != unsigned(Other);
107 }
108};
109
110// Provide DenseMapInfo for MCRegister
111template <> struct DenseMapInfo<MCRegister> {
112 static inline MCRegister getEmptyKey() {
113 return DenseMapInfo<unsigned>::getEmptyKey();
114 }
115 static inline MCRegister getTombstoneKey() {
116 return DenseMapInfo<unsigned>::getTombstoneKey();
117 }
118 static unsigned getHashValue(const MCRegister &Val) {
119 return DenseMapInfo<unsigned>::getHashValue(Val: Val.id());
120 }
121 static bool isEqual(const MCRegister &LHS, const MCRegister &RHS) {
122 return LHS == RHS;
123 }
124};
125
126inline hash_code hash_value(const MCRegister &Reg) {
127 return hash_value(value: Reg.id());
128}
129} // namespace llvm
130
131#endif // LLVM_MC_MCREGISTER_H
132