[Alignment][NFC] Convert StoreInst to MaybeAlign
[llvm-complete.git] / include / llvm / CodeGen / Register.h
blobaa5173684e24e5e33291d49a342a90602a80eaed
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CODEGEN_REGISTER_H
10 #define LLVM_CODEGEN_REGISTER_H
12 #include "llvm/MC/MCRegister.h"
13 #include <cassert>
15 namespace llvm {
17 /// Wrapper class representing virtual and physical registers. Should be passed
18 /// by value.
19 class Register {
20 unsigned Reg;
22 public:
23 Register(unsigned Val = 0): Reg(Val) {}
24 Register(MCRegister Val): Reg(Val) {}
26 // Register numbers can represent physical registers, virtual registers, and
27 // sometimes stack slots. The unsigned values are divided into these ranges:
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.
34 // Further sentinels can be allocated from the small negative integers.
35 // DenseMapInfo<unsigned> uses -1u and -2u.
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);
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));
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);
61 /// Return true if the specified register number is in
62 /// the physical register namespace.
63 static bool isPhysicalRegister(unsigned Reg) {
64 return MCRegister::isPhysicalRegister(Reg);
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;
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);
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);
87 /// Return true if the specified register number is in the virtual register
88 /// namespace.
89 bool isVirtual() const {
90 return isVirtualRegister(Reg);
93 /// Return true if the specified register number is in the physical register
94 /// namespace.
95 bool isPhysical() const {
96 return isPhysicalRegister(Reg);
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);
105 operator unsigned() const {
106 return Reg;
109 unsigned id() const { return Reg; }
111 operator MCRegister() const {
112 return MCRegister(Reg);
115 bool isValid() const {
116 return Reg != 0;
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(); }
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); }
138 // Provide DenseMapInfo for Register
139 template<> struct DenseMapInfo<Register> {
140 static inline unsigned getEmptyKey() {
141 return DenseMapInfo<unsigned>::getEmptyKey();
143 static inline unsigned getTombstoneKey() {
144 return DenseMapInfo<unsigned>::getTombstoneKey();
146 static unsigned getHashValue(const Register &Val) {
147 return DenseMapInfo<unsigned>::getHashValue(Val.id());
149 static bool isEqual(const Register &LHS, const Register &RHS) {
150 return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
156 #endif // ifndef LLVM_CODEGEN_REGISTER_H