Skip to content

[MC][CodeGen] Move FirstStackSlot and VirtualRegFlag from MCRegister to Register. NFC #128444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2025

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Feb 24, 2025

These concepts don't exist for MCRegister.

I think there is a need for virtual registers in MCRegister for NVPTX, SPIR-V and WebAssembly, but those should not be confused with Register's virtual register. I will try to make a separate proposal for that with a real interface.

…to Register. NFC

These concepts don't exist for MCRegister.

I think there is a need for virtual registers in MCRegister for
NVPTX, SPIR-V and WebAssembly, but those should not be confused
with Register's virtual register. I will try to make a separate
proposal for that.
@llvmbot
Copy link
Member

llvmbot commented Feb 24, 2025

@llvm/pr-subscribers-llvm-regalloc

Author: Craig Topper (topperc)

Changes

These concepts don't exist for MCRegister.

I think there is a need for virtual registers in MCRegister for NVPTX, SPIR-V and WebAssembly, but those should not be confused with Register's virtual register. I will try to make a separate proposal for that with a real interface.


Full diff: https://github.com/llvm/llvm-project/pull/128444.diff

3 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/RDFRegisters.h (+2-2)
  • (modified) llvm/include/llvm/CodeGen/Register.h (+9-7)
  • (modified) llvm/include/llvm/MC/MCRegister.h (+2-3)
diff --git a/llvm/include/llvm/CodeGen/RDFRegisters.h b/llvm/include/llvm/CodeGen/RDFRegisters.h
index dcce190f0f308..4a9a4063c9e83 100644
--- a/llvm/include/llvm/CodeGen/RDFRegisters.h
+++ b/llvm/include/llvm/CodeGen/RDFRegisters.h
@@ -119,14 +119,14 @@ struct RegisterRef {
   static constexpr bool isMaskId(unsigned Id) { return Register(Id).isStack(); }
 
   static constexpr RegisterId toUnitId(unsigned Idx) {
-    return Idx | MCRegister::VirtualRegFlag;
+    return Idx | Register::VirtualRegFlag;
   }
 
   static constexpr unsigned toIdx(RegisterId Id) {
     // Not using virtReg2Index or stackSlot2Index, because they are
     // not constexpr.
     if (isUnitId(Id))
-      return Id & ~MCRegister::VirtualRegFlag;
+      return Id & ~Register::VirtualRegFlag;
     // RegId and MaskId are unchanged.
     return Id;
   }
diff --git a/llvm/include/llvm/CodeGen/Register.h b/llvm/include/llvm/CodeGen/Register.h
index 2fdc2148ef020..154ece09c145f 100644
--- a/llvm/include/llvm/CodeGen/Register.h
+++ b/llvm/include/llvm/CodeGen/Register.h
@@ -35,17 +35,19 @@ class Register {
   // DenseMapInfo<unsigned> uses -1u and -2u.
   static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
                 "Reg isn't large enough to hold full range.");
+  static constexpr unsigned FirstStackSlot = 1u << 30;
+  static_assert(FirstStackSlot >= MCRegister::LastPhysicalReg);
+  static constexpr unsigned VirtualRegFlag = 1u << 31;
 
   /// Return true if this is a stack slot.
   constexpr bool isStack() const {
-    return MCRegister::FirstStackSlot <= Reg &&
-           Reg < MCRegister::VirtualRegFlag;
+    return Register::FirstStackSlot <= Reg && Reg < Register::VirtualRegFlag;
   }
 
   /// Convert a non-negative frame index to a stack slot register value.
   static Register index2StackSlot(int FI) {
     assert(FI >= 0 && "Cannot hold a negative frame index.");
-    return Register(FI + MCRegister::FirstStackSlot);
+    return Register(FI + Register::FirstStackSlot);
   }
 
   /// Return true if the specified register number is in
@@ -57,14 +59,14 @@ class Register {
   /// Return true if the specified register number is in
   /// the virtual register namespace.
   static constexpr bool isVirtualRegister(unsigned Reg) {
-    return Reg & MCRegister::VirtualRegFlag;
+    return Reg & Register::VirtualRegFlag;
   }
 
   /// Convert a 0-based index to a virtual register number.
   /// This is the inverse operation of VirtReg2IndexFunctor below.
   static Register index2VirtReg(unsigned Index) {
     assert(Index < (1u << 31) && "Index too large for virtual register range.");
-    return Index | MCRegister::VirtualRegFlag;
+    return Index | Register::VirtualRegFlag;
   }
 
   /// Return true if the specified register number is in the virtual register
@@ -79,13 +81,13 @@ class Register {
   /// register in a function will get the index 0.
   unsigned virtRegIndex() const {
     assert(isVirtual() && "Not a virtual register");
-    return Reg & ~MCRegister::VirtualRegFlag;
+    return Reg & ~Register::VirtualRegFlag;
   }
 
   /// Compute the frame index from a register value representing a stack slot.
   int stackSlotIndex() const {
     assert(isStack() && "Not a stack slot");
-    return static_cast<int>(Reg - MCRegister::FirstStackSlot);
+    return static_cast<int>(Reg - Register::FirstStackSlot);
   }
 
   constexpr operator unsigned() const { return Reg; }
diff --git a/llvm/include/llvm/MC/MCRegister.h b/llvm/include/llvm/MC/MCRegister.h
index 16d0709753b35..388cb5958f32e 100644
--- a/llvm/include/llvm/MC/MCRegister.h
+++ b/llvm/include/llvm/MC/MCRegister.h
@@ -51,13 +51,12 @@ class MCRegister {
                 "Reg isn't large enough to hold full range.");
   static constexpr unsigned NoRegister = 0u;
   static constexpr unsigned FirstPhysicalReg = 1u;
-  static constexpr unsigned FirstStackSlot = 1u << 30;
-  static constexpr unsigned VirtualRegFlag = 1u << 31;
+  static constexpr unsigned LastPhysicalReg = (1u << 30) - 1;
 
   /// Return true if the specified register number is in
   /// the physical register namespace.
   static constexpr bool isPhysicalRegister(unsigned Reg) {
-    return FirstPhysicalReg <= Reg && Reg < FirstStackSlot;
+    return FirstPhysicalReg <= Reg && Reg <= LastPhysicalReg;
   }
 
   /// Return true if the specified register number is in the physical register

@topperc topperc merged commit 6053ca0 into llvm:main Feb 24, 2025
13 checks passed
@topperc topperc deleted the pr/mcregister-flags branch February 24, 2025 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants