[NFCI] Cleanup range checks in Register/MCRegister
authorDavid Tenty <daltenty@ibm.com>
Fri, 26 Jun 2020 18:49:24 +0000 (14:49 -0400)
committerDavid Tenty <daltenty@ibm.com>
Fri, 26 Jun 2020 18:55:55 +0000 (14:55 -0400)
Summary:
by removing casts from unsigned to int that which may be implementation
defined according to C++14 (and thus trip up the XL compiler on AIX) by
just using unsigned comparisons/masks and refactor out the range
constants to cleanup things a bit while we are at it.

Reviewers: hubert.reinterpretcast, arsenm

Reviewed By: hubert.reinterpretcast

Subscribers: wdng, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D82197

llvm/include/llvm/CodeGen/Register.h
llvm/include/llvm/MC/MCRegister.h

index a933a39..054040c 100644 (file)
@@ -33,6 +33,8 @@ public:
   //
   // Further sentinels can be allocated from the small negative integers.
   // DenseMapInfo<unsigned> uses -1u and -2u.
+  static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
+                "Reg isn't large enough to hold full range.");
 
   /// isStackSlot - Sometimes it is useful the be able to store a non-negative
   /// frame index in a variable that normally holds a register. isStackSlot()
@@ -49,13 +51,13 @@ public:
   /// Compute the frame index from a register value representing a stack slot.
   static int stackSlot2Index(unsigned Reg) {
     assert(isStackSlot(Reg) && "Not a stack slot");
-    return int(Reg - (1u << 30));
+    return int(Reg - MCRegister::FirstStackSlot);
   }
 
   /// Convert a non-negative frame index to a stack slot register value.
   static unsigned index2StackSlot(int FI) {
     assert(FI >= 0 && "Cannot hold a negative frame index.");
-    return FI + (1u << 30);
+    return FI + MCRegister::FirstStackSlot;
   }
 
   /// Return true if the specified register number is in
@@ -68,20 +70,21 @@ public:
   /// the virtual register namespace.
   static bool isVirtualRegister(unsigned Reg) {
     assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
-    return int(Reg) < 0;
+    return Reg & MCRegister::VirtualRegFlag;
   }
 
   /// Convert a virtual register number to a 0-based index.
   /// The first virtual register in a function will get the index 0.
   static unsigned virtReg2Index(unsigned Reg) {
     assert(isVirtualRegister(Reg) && "Not a virtual register");
-    return Reg & ~(1u << 31);
+    return Reg & ~MCRegister::VirtualRegFlag;
   }
 
   /// Convert a 0-based index to a virtual register number.
   /// This is the inverse operation of VirtReg2IndexFunctor below.
   static unsigned index2VirtReg(unsigned Index) {
-    return Index | (1u << 31);
+    assert(Index < (1u << 31) && "Index too large for virtual register range.");
+    return Index | MCRegister::VirtualRegFlag;
   }
 
   /// Return true if the specified register number is in the virtual register
@@ -112,9 +115,7 @@ public:
     return MCRegister(Reg);
   }
 
-  bool isValid() const {
-    return Reg != 0;
-  }
+  bool isValid() const { return Reg != MCRegister::NoRegister; }
 
   /// Comparisons between register objects
   bool operator==(const Register &Other) const { return Reg == Other.Reg; }
index 79a8dcd..1f3c4b8 100644 (file)
@@ -35,6 +35,12 @@ public:
   //
   // Further sentinels can be allocated from the small negative integers.
   // 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 NoRegister = 0u;
+  static constexpr unsigned FirstPhysicalReg = 1u;
+  static constexpr unsigned FirstStackSlot = 1u << 30;
+  static constexpr unsigned VirtualRegFlag = 1u << 31;
 
   /// This is the portion of the positive number space that is not a physical
   /// register. StackSlot values do not exist in the MC layer, see
@@ -44,14 +50,15 @@ public:
   /// slots, so if a variable may contains a stack slot, always check
   /// isStackSlot() first.
   static bool isStackSlot(unsigned Reg) {
-    return int(Reg) >= (1 << 30);
+    return !(Reg & VirtualRegFlag) &&
+           uint32_t(Reg & ~VirtualRegFlag) >= FirstStackSlot;
   }
 
   /// Return true if the specified register number is in
   /// the physical register namespace.
   static bool isPhysicalRegister(unsigned Reg) {
     assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
-    return int(Reg) > 0;
+    return Reg >= FirstPhysicalReg && !(Reg & VirtualRegFlag);
   }
 
   /// Return true if the specified register number is in the physical register
@@ -68,9 +75,7 @@ public:
     return Reg;
   }
 
-  bool isValid() const {
-    return Reg != 0;
-  }
+  bool isValid() const { return Reg != NoRegister; }
 
   /// Comparisons between register objects
   bool operator==(const MCRegister &Other) const { return Reg == Other.Reg; }