LLVM  14.0.0git
Register.h
Go to the documentation of this file.
1 //===-- llvm/CodeGen/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_CODEGEN_REGISTER_H
10 #define LLVM_CODEGEN_REGISTER_H
11 
12 #include "llvm/MC/MCRegister.h"
13 #include <cassert>
14 
15 namespace llvm {
16 
17 /// Wrapper class representing virtual and physical registers. Should be passed
18 /// by value.
19 class Register {
20  unsigned Reg;
21 
22 public:
23  constexpr Register(unsigned Val = 0): Reg(Val) {}
24  constexpr Register(MCRegister Val): Reg(Val) {}
25 
26  // Register numbers can represent physical registers, virtual registers, and
27  // sometimes stack slots. The unsigned values are divided into these ranges:
28  //
29  // 0 Not a register, can be used as a sentinel.
30  // [1;2^30) Physical registers assigned by TableGen.
31  // [2^30;2^31) Stack slots. (Rarely used.)
32  // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
33  //
34  // Further sentinels can be allocated from the small negative integers.
35  // DenseMapInfo<unsigned> uses -1u and -2u.
36  static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
37  "Reg isn't large enough to hold full range.");
38 
39  /// isStackSlot - Sometimes it is useful the be able to store a non-negative
40  /// frame index in a variable that normally holds a register. isStackSlot()
41  /// returns true if Reg is in the range used for stack slots.
42  ///
43  /// FIXME: remove in favor of member.
44  static bool isStackSlot(unsigned Reg) {
45  return MCRegister::isStackSlot(Reg);
46  }
47 
48  /// Return true if this is a stack slot.
49  bool isStack() const { return MCRegister::isStackSlot(Reg); }
50 
51  /// Compute the frame index from a register value representing a stack slot.
52  static int stackSlot2Index(Register Reg) {
53  assert(Reg.isStack() && "Not a stack slot");
54  return int(Reg - MCRegister::FirstStackSlot);
55  }
56 
57  /// Convert a non-negative frame index to a stack slot register value.
58  static Register index2StackSlot(int FI) {
59  assert(FI >= 0 && "Cannot hold a negative frame index.");
61  }
62 
63  /// Return true if the specified register number is in
64  /// the physical register namespace.
65  static bool isPhysicalRegister(unsigned Reg) {
67  }
68 
69  /// Return true if the specified register number is in
70  /// the virtual register namespace.
71  static bool isVirtualRegister(unsigned Reg) {
72  return Reg & MCRegister::VirtualRegFlag && !isStackSlot(Reg);
73  }
74 
75  /// Convert a virtual register number to a 0-based index.
76  /// The first virtual register in a function will get the index 0.
77  static unsigned virtReg2Index(Register Reg) {
78  assert(isVirtualRegister(Reg) && "Not a virtual register");
79  return Reg & ~MCRegister::VirtualRegFlag;
80  }
81 
82  /// Convert a 0-based index to a virtual register number.
83  /// This is the inverse operation of VirtReg2IndexFunctor below.
84  static Register index2VirtReg(unsigned Index) {
85  assert(Index < (1u << 31) && "Index too large for virtual register range.");
87  }
88 
89  /// Return true if the specified register number is in the virtual register
90  /// namespace.
91  bool isVirtual() const {
92  return isVirtualRegister(Reg);
93  }
94 
95  /// Return true if the specified register number is in the physical register
96  /// namespace.
97  bool isPhysical() const {
98  return isPhysicalRegister(Reg);
99  }
100 
101  /// Convert a virtual register number to a 0-based index. The first virtual
102  /// register in a function will get the index 0.
103  unsigned virtRegIndex() const {
104  return virtReg2Index(Reg);
105  }
106 
107  constexpr operator unsigned() const {
108  return Reg;
109  }
110 
111  unsigned id() const { return Reg; }
112 
113  operator MCRegister() const {
114  return MCRegister(Reg);
115  }
116 
117  /// Utility to check-convert this value to a MCRegister. The caller is
118  /// expected to have already validated that this Register is, indeed,
119  /// physical.
120  MCRegister asMCReg() const {
123  return MCRegister(Reg);
124  }
125 
126  bool isValid() const { return Reg != MCRegister::NoRegister; }
127 
128  /// Comparisons between register objects
129  bool operator==(const Register &Other) const { return Reg == Other.Reg; }
130  bool operator!=(const Register &Other) const { return Reg != Other.Reg; }
131  bool operator==(const MCRegister &Other) const { return Reg == Other.id(); }
132  bool operator!=(const MCRegister &Other) const { return Reg != Other.id(); }
133 
134  /// Comparisons against register constants. E.g.
135  /// * R == AArch64::WZR
136  /// * R == 0
137  /// * R == VirtRegMap::NO_PHYS_REG
138  bool operator==(unsigned Other) const { return Reg == Other; }
139  bool operator!=(unsigned Other) const { return Reg != Other; }
140  bool operator==(int Other) const { return Reg == unsigned(Other); }
141  bool operator!=(int Other) const { return Reg != unsigned(Other); }
142  // MSVC requires that we explicitly declare these two as well.
143  bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
144  bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
145 };
146 
147 // Provide DenseMapInfo for Register
148 template<> struct DenseMapInfo<Register> {
149  static inline unsigned getEmptyKey() {
151  }
152  static inline unsigned getTombstoneKey() {
154  }
155  static unsigned getHashValue(const Register &Val) {
157  }
158  static bool isEqual(const Register &LHS, const Register &RHS) {
159  return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
160  }
161 };
162 
163 }
164 
165 #endif // LLVM_CODEGEN_REGISTER_H
llvm::MCRegister::VirtualRegFlag
static constexpr unsigned VirtualRegFlag
Definition: MCRegister.h:45
llvm::Register::operator==
bool operator==(int Other) const
Definition: Register.h:140
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::Register::virtRegIndex
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
Definition: Register.h:103
llvm::MCRegister::FirstStackSlot
static constexpr unsigned FirstStackSlot
Definition: MCRegister.h:44
llvm::Register::id
unsigned id() const
Definition: Register.h:111
llvm::Register::stackSlot2Index
static int stackSlot2Index(Register Reg)
Compute the frame index from a register value representing a stack slot.
Definition: Register.h:52
llvm::MCRegister::NoRegister
static constexpr unsigned NoRegister
Definition: MCRegister.h:42
llvm::Register::index2VirtReg
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition: Register.h:84
llvm::Register::isPhysical
bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:97
llvm::DenseMapInfo< Register >::getTombstoneKey
static unsigned getTombstoneKey()
Definition: Register.h:152
llvm::DenseMapInfo
Definition: APInt.h:34
llvm::Register::index2StackSlot
static Register index2StackSlot(int FI)
Convert a non-negative frame index to a stack slot register value.
Definition: Register.h:58
llvm::Register::operator!=
bool operator!=(unsigned Other) const
Definition: Register.h:139
llvm::Register::isStackSlot
static bool isStackSlot(unsigned Reg)
isStackSlot - Sometimes it is useful the be able to store a non-negative frame index in a variable th...
Definition: Register.h:44
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
llvm::Register::operator==
bool operator==(unsigned Other) const
Comparisons against register constants.
Definition: Register.h:138
llvm::Register::isPhysicalRegister
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:65
llvm::DenseMapInfo< Register >::getEmptyKey
static unsigned getEmptyKey()
Definition: Register.h:149
llvm::Register::operator!=
bool operator!=(MCPhysReg Other) const
Definition: Register.h:144
MCRegister.h
llvm::Register::isVirtual
bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:91
llvm::Register::operator==
bool operator==(MCPhysReg Other) const
Definition: Register.h:143
llvm::Register::operator!=
bool operator!=(const MCRegister &Other) const
Definition: Register.h:132
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::Register::operator==
bool operator==(const MCRegister &Other) const
Definition: Register.h:131
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::Register::isVirtualRegister
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:71
llvm::DenseMapInfo< Register >::isEqual
static bool isEqual(const Register &LHS, const Register &RHS)
Definition: Register.h:158
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Register::operator==
bool operator==(const Register &Other) const
Comparisons between register objects.
Definition: Register.h:129
llvm::Register::asMCReg
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition: Register.h:120
llvm::Register::isStack
bool isStack() const
Return true if this is a stack slot.
Definition: Register.h:49
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
llvm::Register::operator!=
bool operator!=(int Other) const
Definition: Register.h:141
uint16_t
llvm::Register::Register
constexpr Register(MCRegister Val)
Definition: Register.h:24
isEqual
static bool isEqual(const Function &Caller, const Function &Callee)
Definition: Attributes.cpp:1935
llvm::DenseMapInfo< Register >::getHashValue
static unsigned getHashValue(const Register &Val)
Definition: Register.h:155
llvm::Register::isValid
bool isValid() const
Definition: Register.h:126
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
llvm::Register::virtReg2Index
static unsigned virtReg2Index(Register Reg)
Convert a virtual register number to a 0-based index.
Definition: Register.h:77
llvm::Register::Register
constexpr Register(unsigned Val=0)
Definition: Register.h:23
llvm::Register::operator!=
bool operator!=(const Register &Other) const
Definition: Register.h:130
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