Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:14

0001 //===-- llvm/MC/Register.h --------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_MC_MCREGISTER_H
0010 #define LLVM_MC_MCREGISTER_H
0011 
0012 #include "llvm/ADT/DenseMapInfo.h"
0013 #include "llvm/ADT/Hashing.h"
0014 #include <cassert>
0015 #include <limits>
0016 
0017 namespace llvm {
0018 
0019 /// An unsigned integer type large enough to represent all physical registers,
0020 /// but not necessarily virtual registers.
0021 using MCPhysReg = uint16_t;
0022 
0023 /// Register units are used to compute register aliasing. Every register has at
0024 /// least one register unit, but it can have more. Two registers overlap if and
0025 /// only if they have a common register unit.
0026 ///
0027 /// A target with a complicated sub-register structure will typically have many
0028 /// fewer register units than actual registers. MCRI::getNumRegUnits() returns
0029 /// the number of register units in the target.
0030 using MCRegUnit = unsigned;
0031 
0032 /// Wrapper class representing physical registers. Should be passed by value.
0033 class MCRegister {
0034   friend hash_code hash_value(const MCRegister &);
0035   unsigned Reg;
0036 
0037 public:
0038   constexpr MCRegister(unsigned Val = 0) : Reg(Val) {}
0039 
0040   // Register numbers can represent physical registers, virtual registers, and
0041   // sometimes stack slots. The unsigned values are divided into these ranges:
0042   //
0043   //   0           Not a register, can be used as a sentinel.
0044   //   [1;2^30)    Physical registers assigned by TableGen.
0045   //   [2^30;2^31) Stack slots. (Rarely used.)
0046   //   [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
0047   //
0048   // Further sentinels can be allocated from the small negative integers.
0049   // DenseMapInfo<unsigned> uses -1u and -2u.
0050   static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
0051                 "Reg isn't large enough to hold full range.");
0052   static constexpr unsigned NoRegister = 0u;
0053   static constexpr unsigned FirstPhysicalReg = 1u;
0054   static constexpr unsigned FirstStackSlot = 1u << 30;
0055   static constexpr unsigned VirtualRegFlag = 1u << 31;
0056 
0057   /// This is the portion of the positive number space that is not a physical
0058   /// register. StackSlot values do not exist in the MC layer, see
0059   /// Register::isStackSlot() for the more information on them.
0060   ///
0061   static constexpr bool isStackSlot(unsigned Reg) {
0062     return FirstStackSlot <= Reg && Reg < VirtualRegFlag;
0063   }
0064 
0065   /// Return true if the specified register number is in
0066   /// the physical register namespace.
0067   static constexpr bool isPhysicalRegister(unsigned Reg) {
0068     return FirstPhysicalReg <= Reg && Reg < FirstStackSlot;
0069   }
0070 
0071   /// Return true if the specified register number is in the physical register
0072   /// namespace.
0073   constexpr bool isPhysical() const { return isPhysicalRegister(Reg); }
0074 
0075   constexpr operator unsigned() const { return Reg; }
0076 
0077   /// Check the provided unsigned value is a valid MCRegister.
0078   static MCRegister from(unsigned Val) {
0079     assert(Val == NoRegister || isPhysicalRegister(Val));
0080     return MCRegister(Val);
0081   }
0082 
0083   constexpr unsigned id() const { return Reg; }
0084 
0085   constexpr bool isValid() const { return Reg != NoRegister; }
0086 
0087   /// Comparisons between register objects
0088   constexpr bool operator==(const MCRegister &Other) const {
0089     return Reg == Other.Reg;
0090   }
0091   constexpr bool operator!=(const MCRegister &Other) const {
0092     return Reg != Other.Reg;
0093   }
0094 
0095   /// Comparisons against register constants. E.g.
0096   /// * R == AArch64::WZR
0097   /// * R == 0
0098   constexpr bool operator==(unsigned Other) const { return Reg == Other; }
0099   constexpr bool operator!=(unsigned Other) const { return Reg != Other; }
0100   constexpr bool operator==(int Other) const { return Reg == unsigned(Other); }
0101   constexpr bool operator!=(int Other) const { return Reg != unsigned(Other); }
0102   // MSVC requires that we explicitly declare these two as well.
0103   constexpr bool operator==(MCPhysReg Other) const {
0104     return Reg == unsigned(Other);
0105   }
0106   constexpr bool operator!=(MCPhysReg Other) const {
0107     return Reg != unsigned(Other);
0108   }
0109 };
0110 
0111 // Provide DenseMapInfo for MCRegister
0112 template <> struct DenseMapInfo<MCRegister> {
0113   static inline MCRegister getEmptyKey() {
0114     return DenseMapInfo<unsigned>::getEmptyKey();
0115   }
0116   static inline MCRegister getTombstoneKey() {
0117     return DenseMapInfo<unsigned>::getTombstoneKey();
0118   }
0119   static unsigned getHashValue(const MCRegister &Val) {
0120     return DenseMapInfo<unsigned>::getHashValue(Val.id());
0121   }
0122   static bool isEqual(const MCRegister &LHS, const MCRegister &RHS) {
0123     return LHS == RHS;
0124   }
0125 };
0126 
0127 inline hash_code hash_value(const MCRegister &Reg) {
0128   return hash_value(Reg.id());
0129 }
0130 } // namespace llvm
0131 
0132 #endif // LLVM_MC_MCREGISTER_H