LLVM  10.0.0svn
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  Register(unsigned Val = 0): Reg(Val) {}
24  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 
37  /// isStackSlot - Sometimes it is useful the be able to store a non-negative
38  /// frame index in a variable that normally holds a register. isStackSlot()
39  /// returns true if Reg is in the range used for stack slots.
40  ///
41  /// Note that isVirtualRegister() and isPhysicalRegister() cannot handle stack
42  /// slots, so if a variable may contains a stack slot, always check
43  /// isStackSlot() first.
44  ///
45  static bool isStackSlot(unsigned Reg) {
46  return MCRegister::isStackSlot(Reg);
47  }
48 
49  /// Compute the frame index from a register value representing a stack slot.
50  static int stackSlot2Index(unsigned Reg) {
51  assert(isStackSlot(Reg) && "Not a stack slot");
52  return int(Reg - (1u << 30));
53  }
54 
55  /// Convert a non-negative frame index to a stack slot register value.
56  static unsigned index2StackSlot(int FI) {
57  assert(FI >= 0 && "Cannot hold a negative frame index.");
58  return FI + (1u << 30);
59  }
60 
61  /// Return true if the specified register number is in
62  /// the physical register namespace.
63  static bool isPhysicalRegister(unsigned Reg) {
65  }
66 
67  /// Return true if the specified register number is in
68  /// the virtual register namespace.
69  static bool isVirtualRegister(unsigned Reg) {
70  assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
71  return int(Reg) < 0;
72  }
73 
74  /// Convert a virtual register number to a 0-based index.
75  /// The first virtual register in a function will get the index 0.
76  static unsigned virtReg2Index(unsigned Reg) {
77  assert(isVirtualRegister(Reg) && "Not a virtual register");
78  return Reg & ~(1u << 31);
79  }
80 
81  /// Convert a 0-based index to a virtual register number.
82  /// This is the inverse operation of VirtReg2IndexFunctor below.
83  static unsigned index2VirtReg(unsigned Index) {
84  return Index | (1u << 31);
85  }
86 
87  /// Return true if the specified register number is in the virtual register
88  /// namespace.
89  bool isVirtual() const {
90  return isVirtualRegister(Reg);
91  }
92 
93  /// Return true if the specified register number is in the physical register
94  /// namespace.
95  bool isPhysical() const {
96  return isPhysicalRegister(Reg);
97  }
98 
99  /// Convert a virtual register number to a 0-based index. The first virtual
100  /// register in a function will get the index 0.
101  unsigned virtRegIndex() const {
102  return virtReg2Index(Reg);
103  }
104 
105  operator unsigned() const {
106  return Reg;
107  }
108 
109  unsigned id() const { return Reg; }
110 
111  operator MCRegister() const {
112  return MCRegister(Reg);
113  }
114 
115  bool isValid() const {
116  return Reg != 0;
117  }
118 
119  /// Comparisons between register objects
120  bool operator==(const Register &Other) const { return Reg == Other.Reg; }
121  bool operator!=(const Register &Other) const { return Reg != Other.Reg; }
122  bool operator==(const MCRegister &Other) const { return Reg == Other.id(); }
123  bool operator!=(const MCRegister &Other) const { return Reg != Other.id(); }
124 
125  /// Comparisons against register constants. E.g.
126  /// * R == AArch64::WZR
127  /// * R == 0
128  /// * R == VirtRegMap::NO_PHYS_REG
129  bool operator==(unsigned Other) const { return Reg == Other; }
130  bool operator!=(unsigned Other) const { return Reg != Other; }
131  bool operator==(int Other) const { return Reg == unsigned(Other); }
132  bool operator!=(int Other) const { return Reg != unsigned(Other); }
133  // MSVC requires that we explicitly declare these two as well.
134  bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
135  bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
136 };
137 
138 // Provide DenseMapInfo for Register
139 template<> struct DenseMapInfo<Register> {
140  static inline unsigned getEmptyKey() {
142  }
143  static inline unsigned getTombstoneKey() {
145  }
146  static unsigned getHashValue(const Register &Val) {
148  }
149  static bool isEqual(const Register &LHS, const Register &RHS) {
150  return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
151  }
152 };
153 
154 }
155 
156 #endif // ifndef LLVM_CODEGEN_REGISTER_H
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:22
bool operator!=(MCPhysReg Other) const
Definition: Register.h:135
static unsigned virtReg2Index(unsigned Reg)
Convert a virtual register number to a 0-based index.
Definition: Register.h:76
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
Definition: Register.h:101
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static bool isEqual(const Register &LHS, const Register &RHS)
Definition: Register.h:149
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:63
unsigned id() const
Definition: Register.h:109
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:45
bool operator!=(const Register &Other) const
Definition: Register.h:121
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition: Register.h:83
bool operator==(int Other) const
Definition: Register.h:131
bool operator==(MCPhysReg Other) const
Definition: Register.h:134
bool operator!=(unsigned Other) const
Definition: Register.h:130
static bool isEqual(const Function &Caller, const Function &Callee)
static unsigned getTombstoneKey()
Definition: Register.h:143
bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:95
bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:89
static unsigned index2StackSlot(int FI)
Convert a non-negative frame index to a stack slot register value.
Definition: Register.h:56
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition: MCRegister.h:19
Register(MCRegister Val)
Definition: Register.h:24
bool operator==(const MCRegister &Other) const
Definition: Register.h:122
static int stackSlot2Index(unsigned Reg)
Compute the frame index from a register value representing a stack slot.
Definition: Register.h:50
static bool isStackSlot(unsigned Reg)
This is the portion of the positive number space that is not a physical register. ...
Definition: MCRegister.h:46
bool operator!=(int Other) const
Definition: Register.h:132
bool operator!=(const MCRegister &Other) const
Definition: Register.h:123
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition: MCRegister.h:52
bool operator==(const Register &Other) const
Comparisons between register objects.
Definition: Register.h:120
Register(unsigned Val=0)
Definition: Register.h:23
bool operator==(unsigned Other) const
Comparisons against register constants.
Definition: Register.h:129
bool isValid() const
Definition: Register.h:115
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned id() const
Definition: MCRegister.h:67
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:69
static unsigned getEmptyKey()
Definition: Register.h:140
static unsigned getHashValue(const Register &Val)
Definition: Register.h:146
Wrapper class representing virtual and physical registers.
Definition: Register.h:19