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