Yi Kong | e5b9f24 | 2023-04-26 13:35:57 +0900 | [diff] [blame^] | 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. |
| 21 | using MCPhysReg = uint16_t; |
| 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() { |
| 98 | return DenseMapInfo<unsigned>::getEmptyKey(); |
| 99 | } |
| 100 | static inline unsigned getTombstoneKey() { |
| 101 | return DenseMapInfo<unsigned>::getTombstoneKey(); |
| 102 | } |
| 103 | static unsigned getHashValue(const MCRegister &Val) { |
| 104 | return DenseMapInfo<unsigned>::getHashValue(Val.id()); |
| 105 | } |
| 106 | static bool isEqual(const MCRegister &LHS, const MCRegister &RHS) { |
| 107 | return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id()); |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | inline hash_code hash_value(const MCRegister &Reg) { |
| 112 | return hash_value(Reg.id()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #endif // LLVM_MC_MCREGISTER_H |