LLVM  13.0.0git
MCRegister.h
Go to the documentation of this file.
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.
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 
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; }
86  bool operator!=(unsigned Other) const { return Reg != Other; }
87  bool operator==(int Other) const { return Reg == unsigned(Other); }
88  bool operator!=(int Other) const { return Reg != unsigned(Other); }
89  // MSVC requires that we explicitly declare these two as well.
90  bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
91  bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
92 };
93 
94 // Provide DenseMapInfo for MCRegister
95 template<> struct DenseMapInfo<MCRegister> {
96  static inline unsigned getEmptyKey() {
98  }
99  static inline unsigned getTombstoneKey() {
101  }
102  static unsigned getHashValue(const MCRegister &Val) {
104  }
105  static bool isEqual(const MCRegister &LHS, const MCRegister &RHS) {
106  return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
107  }
108 };
109 
111  return hash_value(Reg.id());
112 }
113 }
114 
115 #endif // LLVM_MC_MCREGISTER_H
llvm::MCRegister::VirtualRegFlag
static constexpr unsigned VirtualRegFlag
Definition: MCRegister.h:45
llvm::DenseMapInfo< MCRegister >::getTombstoneKey
static unsigned getTombstoneKey()
Definition: MCRegister.h:99
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::MCRegister::FirstStackSlot
static constexpr unsigned FirstStackSlot
Definition: MCRegister.h:44
llvm::MCRegister::FirstPhysicalReg
static constexpr unsigned FirstPhysicalReg
Definition: MCRegister.h:43
llvm::MCRegister::operator!=
bool operator!=(int Other) const
Definition: MCRegister.h:88
llvm::MCRegister::operator==
bool operator==(MCPhysReg Other) const
Definition: MCRegister.h:90
llvm::MCRegister::from
static MCRegister from(unsigned Val)
Check the provided unsigned value is a valid MCRegister.
Definition: MCRegister.h:66
llvm::MCRegister::operator!=
bool operator!=(MCPhysReg Other) const
Definition: MCRegister.h:91
llvm::MCRegister::isValid
bool isValid() const
Definition: MCRegister.h:75
llvm::MCRegister::NoRegister
static constexpr unsigned NoRegister
Definition: MCRegister.h:42
llvm::hash_value
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4810
llvm::MCRegister::hash_value
friend hash_code hash_value(const MCRegister &)
Definition: MCRegister.h:110
llvm::DenseMapInfo
Definition: APInt.h:34
llvm::MCRegister::operator==
bool operator==(unsigned Other) const
Comparisons against register constants.
Definition: MCRegister.h:85
llvm::DenseMapInfo< MCRegister >::isEqual
static bool isEqual(const MCRegister &LHS, const MCRegister &RHS)
Definition: MCRegister.h:105
llvm::DenseMapInfo< MCRegister >::getHashValue
static unsigned getHashValue(const MCRegister &Val)
Definition: MCRegister.h:102
llvm::MCRegister::isPhysicalRegister
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition: MCRegister.h:57
llvm::MCRegister::operator==
bool operator==(const MCRegister &Other) const
Comparisons between register objects.
Definition: MCRegister.h:78
llvm::MCRegister::operator==
bool operator==(int Other) const
Definition: MCRegister.h:87
llvm::MCRegister::operator!=
bool operator!=(unsigned Other) const
Definition: MCRegister.h:86
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCRegister::id
unsigned id() const
Definition: MCRegister.h:71
uint16_t
isEqual
static bool isEqual(const Function &Caller, const Function &Callee)
Definition: Attributes.cpp:1928
llvm::MCRegister::MCRegister
constexpr MCRegister(unsigned Val=0)
Definition: MCRegister.h:28
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::MCRegister::isStackSlot
static bool isStackSlot(unsigned Reg)
This is the portion of the positive number space that is not a physical register.
Definition: MCRegister.h:51
DenseMapInfo.h
llvm::MCRegister::operator!=
bool operator!=(const MCRegister &Other) const
Definition: MCRegister.h:79
llvm::DenseMapInfo< MCRegister >::getEmptyKey
static unsigned getEmptyKey()
Definition: MCRegister.h:96
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1172
llvm::MCRegister
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:23
llvm::hash_code
An opaque object representing a hash code.
Definition: Hashing.h:72