Home | History | Annotate | Line # | Download | only in MC
      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 <cassert>
     14 
     15 namespace llvm {
     16 
     17 /// An unsigned integer type large enough to represent all physical registers,
     18 /// but not necessarily virtual registers.
     19 using MCPhysReg = uint16_t;
     20 
     21 /// Wrapper class representing physical registers. Should be passed by value.
     22 class MCRegister {
     23   friend hash_code hash_value(const MCRegister &);
     24   unsigned Reg;
     25 
     26 public:
     27   constexpr MCRegister(unsigned Val = 0): Reg(Val) {}
     28 
     29   // Register numbers can represent physical registers, virtual registers, and
     30   // sometimes stack slots. The unsigned values are divided into these ranges:
     31   //
     32   //   0           Not a register, can be used as a sentinel.
     33   //   [1;2^30)    Physical registers assigned by TableGen.
     34   //   [2^30;2^31) Stack slots. (Rarely used.)
     35   //   [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
     36   //
     37   // Further sentinels can be allocated from the small negative integers.
     38   // DenseMapInfo<unsigned> uses -1u and -2u.
     39   static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
     40                 "Reg isn't large enough to hold full range.");
     41   static constexpr unsigned NoRegister = 0u;
     42   static constexpr unsigned FirstPhysicalReg = 1u;
     43   static constexpr unsigned FirstStackSlot = 1u << 30;
     44   static constexpr unsigned VirtualRegFlag = 1u << 31;
     45 
     46   /// This is the portion of the positive number space that is not a physical
     47   /// register. StackSlot values do not exist in the MC layer, see
     48   /// Register::isStackSlot() for the more information on them.
     49   ///
     50   static bool isStackSlot(unsigned Reg) {
     51     return FirstStackSlot <= Reg && Reg < VirtualRegFlag;
     52   }
     53 
     54   /// Return true if the specified register number is in
     55   /// the physical register namespace.
     56   static bool isPhysicalRegister(unsigned Reg) {
     57     return FirstPhysicalReg <= Reg && Reg < FirstStackSlot;
     58   }
     59 
     60   constexpr operator unsigned() const {
     61     return Reg;
     62   }
     63 
     64   /// Check the provided unsigned value is a valid MCRegister.
     65   static MCRegister from(unsigned Val) {
     66     assert(Val == NoRegister || isPhysicalRegister(Val));
     67     return MCRegister(Val);
     68   }
     69 
     70   unsigned id() const {
     71     return Reg;
     72   }
     73 
     74   bool isValid() const { return Reg != NoRegister; }
     75 
     76   /// Comparisons between register objects
     77   bool operator==(const MCRegister &Other) const { return Reg == Other.Reg; }
     78   bool operator!=(const MCRegister &Other) const { return Reg != Other.Reg; }
     79 
     80   /// Comparisons against register constants. E.g.
     81   /// * R == AArch64::WZR
     82   /// * R == 0
     83   /// * R == VirtRegMap::NO_PHYS_REG
     84   bool operator==(unsigned Other) const { return Reg == Other; }
     85   bool operator!=(unsigned Other) const { return Reg != Other; }
     86   bool operator==(int Other) const { return Reg == unsigned(Other); }
     87   bool operator!=(int Other) const { return Reg != unsigned(Other); }
     88   // MSVC requires that we explicitly declare these two as well.
     89   bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
     90   bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
     91 };
     92 
     93 // Provide DenseMapInfo for MCRegister
     94 template<> struct DenseMapInfo<MCRegister> {
     95   static inline unsigned getEmptyKey() {
     96     return DenseMapInfo<unsigned>::getEmptyKey();
     97   }
     98   static inline unsigned getTombstoneKey() {
     99     return DenseMapInfo<unsigned>::getTombstoneKey();
    100   }
    101   static unsigned getHashValue(const MCRegister &Val) {
    102     return DenseMapInfo<unsigned>::getHashValue(Val.id());
    103   }
    104   static bool isEqual(const MCRegister &LHS, const MCRegister &RHS) {
    105     return DenseMapInfo<unsigned>::isEqual(LHS.id(), RHS.id());
    106   }
    107 };
    108 
    109 inline hash_code hash_value(const MCRegister &Reg) {
    110   return hash_value(Reg.id());
    111 }
    112 }
    113 
    114 #endif // LLVM_MC_MCREGISTER_H
    115