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