[llvm] 16dae81 - [NFCI] Cleanup range checks in Register/MCRegister

David Tenty via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 11:56:02 PDT 2020


Author: David Tenty
Date: 2020-06-26T14:55:55-04:00
New Revision: 16dae81edc240b7c9f58e4b5ec0cf8d5ba0d847d

URL: https://github.com/llvm/llvm-project/commit/16dae81edc240b7c9f58e4b5ec0cf8d5ba0d847d
DIFF: https://github.com/llvm/llvm-project/commit/16dae81edc240b7c9f58e4b5ec0cf8d5ba0d847d.diff

LOG: [NFCI] Cleanup range checks in Register/MCRegister

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/Register.h b/llvm/include/llvm/CodeGen/Register.h
index a933a39185f7..054040cd29a1 100644
--- a/llvm/include/llvm/CodeGen/Register.h
+++ b/llvm/include/llvm/CodeGen/Register.h
@@ -33,6 +33,8 @@ class Register {
   //
   // 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 @@ class Register {
   /// 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 @@ class Register {
   /// 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 @@ class Register {
     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; }

diff  --git a/llvm/include/llvm/MC/MCRegister.h b/llvm/include/llvm/MC/MCRegister.h
index 79a8dcdadb41..1f3c4b8494cc 100644
--- a/llvm/include/llvm/MC/MCRegister.h
+++ b/llvm/include/llvm/MC/MCRegister.h
@@ -35,6 +35,12 @@ class MCRegister {
   //
   // 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 @@ class MCRegister {
   /// 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 @@ class MCRegister {
     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; }


        


More information about the llvm-commits mailing list