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