[llvm] c6b2d25 - Constexprify all eligible functions in MCRegister and Register

Krzysztof Parzyszek via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 2 12:00:31 PDT 2023


Author: Krzysztof Parzyszek
Date: 2023-06-02T12:00:23-07:00
New Revision: c6b2d25927817bdeca99653ee3e66720f33ce3ae

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

LOG: Constexprify all eligible functions in MCRegister and Register

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 2f2d58f5185b0..e1456f81d4670 100644
--- a/llvm/include/llvm/CodeGen/Register.h
+++ b/llvm/include/llvm/CodeGen/Register.h
@@ -20,8 +20,8 @@ class Register {
   unsigned Reg;
 
 public:
-  constexpr Register(unsigned Val = 0): Reg(Val) {}
-  constexpr Register(MCRegister Val): Reg(Val) {}
+  constexpr Register(unsigned Val = 0) : Reg(Val) {}
+  constexpr Register(MCRegister Val) : Reg(Val) {}
 
   // Register numbers can represent physical registers, virtual registers, and
   // sometimes stack slots. The unsigned values are divided into these ranges:
@@ -41,12 +41,12 @@ class Register {
   /// returns true if Reg is in the range used for stack slots.
   ///
   /// FIXME: remove in favor of member.
-  static bool isStackSlot(unsigned Reg) {
+  static constexpr bool isStackSlot(unsigned Reg) {
     return MCRegister::isStackSlot(Reg);
   }
 
   /// Return true if this is a stack slot.
-  bool isStack() const { return MCRegister::isStackSlot(Reg); }
+  constexpr bool isStack() const { return MCRegister::isStackSlot(Reg); }
 
   /// Compute the frame index from a register value representing a stack slot.
   static int stackSlot2Index(Register Reg) {
@@ -62,13 +62,13 @@ class Register {
 
   /// Return true if the specified register number is in
   /// the physical register namespace.
-  static bool isPhysicalRegister(unsigned Reg) {
+  static constexpr bool isPhysicalRegister(unsigned Reg) {
     return MCRegister::isPhysicalRegister(Reg);
   }
 
   /// Return true if the specified register number is in
   /// the virtual register namespace.
-  static bool isVirtualRegister(unsigned Reg) {
+  static constexpr bool isVirtualRegister(unsigned Reg) {
     return Reg & MCRegister::VirtualRegFlag;
   }
 
@@ -88,31 +88,21 @@ class Register {
 
   /// Return true if the specified register number is in the virtual register
   /// namespace.
-  bool isVirtual() const {
-    return isVirtualRegister(Reg);
-  }
+  constexpr bool isVirtual() const { return isVirtualRegister(Reg); }
 
   /// Return true if the specified register number is in the physical register
   /// namespace.
-  bool isPhysical() const {
-    return isPhysicalRegister(Reg);
-  }
+  constexpr bool isPhysical() const { return isPhysicalRegister(Reg); }
 
   /// Convert a virtual register number to a 0-based index. The first virtual
   /// register in a function will get the index 0.
-  unsigned virtRegIndex() const {
-    return virtReg2Index(Reg);
-  }
+  unsigned virtRegIndex() const { return virtReg2Index(Reg); }
 
-  constexpr operator unsigned() const {
-    return Reg;
-  }
+  constexpr operator unsigned() const { return Reg; }
 
-  unsigned id() const { return Reg; }
+  constexpr unsigned id() const { return Reg; }
 
-  operator MCRegister() const {
-    return MCRegister(Reg);
-  }
+  constexpr operator MCRegister() const { return MCRegister(Reg); }
 
   /// Utility to check-convert this value to a MCRegister. The caller is
   /// expected to have already validated that this Register is, indeed,
@@ -123,29 +113,41 @@ class Register {
     return MCRegister(Reg);
   }
 
-  bool isValid() const { return Reg != MCRegister::NoRegister; }
+  constexpr bool isValid() const { return Reg != MCRegister::NoRegister; }
 
   /// Comparisons between register objects
-  bool operator==(const Register &Other) const { return Reg == Other.Reg; }
-  bool operator!=(const Register &Other) const { return Reg != Other.Reg; }
-  bool operator==(const MCRegister &Other) const { return Reg == Other.id(); }
-  bool operator!=(const MCRegister &Other) const { return Reg != Other.id(); }
+  constexpr bool operator==(const Register &Other) const {
+    return Reg == Other.Reg;
+  }
+  constexpr bool operator!=(const Register &Other) const {
+    return Reg != Other.Reg;
+  }
+  constexpr bool operator==(const MCRegister &Other) const {
+    return Reg == Other.id();
+  }
+  constexpr bool operator!=(const MCRegister &Other) const {
+    return Reg != Other.id();
+  }
 
   /// Comparisons against register constants. E.g.
   /// * R == AArch64::WZR
   /// * R == 0
   /// * R == VirtRegMap::NO_PHYS_REG
-  bool operator==(unsigned Other) const { return Reg == Other; }
-  bool operator!=(unsigned Other) const { return Reg != Other; }
-  bool operator==(int Other) const { return Reg == unsigned(Other); }
-  bool operator!=(int Other) const { return Reg != unsigned(Other); }
+  constexpr bool operator==(unsigned Other) const { return Reg == Other; }
+  constexpr bool operator!=(unsigned Other) const { return Reg != Other; }
+  constexpr bool operator==(int Other) const { return Reg == unsigned(Other); }
+  constexpr bool operator!=(int Other) const { return Reg != unsigned(Other); }
   // MSVC requires that we explicitly declare these two as well.
-  bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
-  bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
+  constexpr bool operator==(MCPhysReg Other) const {
+    return Reg == unsigned(Other);
+  }
+  constexpr bool operator!=(MCPhysReg Other) const {
+    return Reg != unsigned(Other);
+  }
 };
 
 // Provide DenseMapInfo for Register
-template<> struct DenseMapInfo<Register> {
+template <> struct DenseMapInfo<Register> {
   static inline unsigned getEmptyKey() {
     return DenseMapInfo<unsigned>::getEmptyKey();
   }
@@ -160,6 +162,6 @@ template<> struct DenseMapInfo<Register> {
   }
 };
 
-}
+} // namespace llvm
 
 #endif // LLVM_CODEGEN_REGISTER_H

diff  --git a/llvm/include/llvm/MC/MCRegister.h b/llvm/include/llvm/MC/MCRegister.h
index 1e8c747785eb8..1e2bdc32885fe 100644
--- a/llvm/include/llvm/MC/MCRegister.h
+++ b/llvm/include/llvm/MC/MCRegister.h
@@ -26,7 +26,7 @@ class MCRegister {
   unsigned Reg;
 
 public:
-  constexpr MCRegister(unsigned Val = 0): Reg(Val) {}
+  constexpr MCRegister(unsigned Val = 0) : Reg(Val) {}
 
   // Register numbers can represent physical registers, virtual registers, and
   // sometimes stack slots. The unsigned values are divided into these ranges:
@@ -49,19 +49,17 @@ class MCRegister {
   /// register. StackSlot values do not exist in the MC layer, see
   /// Register::isStackSlot() for the more information on them.
   ///
-  static bool isStackSlot(unsigned Reg) {
+  static constexpr bool isStackSlot(unsigned Reg) {
     return FirstStackSlot <= Reg && Reg < VirtualRegFlag;
   }
 
   /// Return true if the specified register number is in
   /// the physical register namespace.
-  static bool isPhysicalRegister(unsigned Reg) {
+  static constexpr bool isPhysicalRegister(unsigned Reg) {
     return FirstPhysicalReg <= Reg && Reg < FirstStackSlot;
   }
 
-  constexpr operator unsigned() const {
-    return Reg;
-  }
+  constexpr operator unsigned() const { return Reg; }
 
   /// Check the provided unsigned value is a valid MCRegister.
   static MCRegister from(unsigned Val) {
@@ -69,31 +67,37 @@ class MCRegister {
     return MCRegister(Val);
   }
 
-  unsigned id() const {
-    return Reg;
-  }
+  constexpr unsigned id() const { return Reg; }
 
-  bool isValid() const { return Reg != NoRegister; }
+  constexpr bool isValid() const { return Reg != NoRegister; }
 
   /// Comparisons between register objects
-  bool operator==(const MCRegister &Other) const { return Reg == Other.Reg; }
-  bool operator!=(const MCRegister &Other) const { return Reg != Other.Reg; }
+  constexpr bool operator==(const MCRegister &Other) const {
+    return Reg == Other.Reg;
+  }
+  constexpr bool operator!=(const MCRegister &Other) const {
+    return Reg != Other.Reg;
+  }
 
   /// Comparisons against register constants. E.g.
   /// * R == AArch64::WZR
   /// * R == 0
   /// * R == VirtRegMap::NO_PHYS_REG
-  bool operator==(unsigned Other) const { return Reg == Other; }
-  bool operator!=(unsigned Other) const { return Reg != Other; }
-  bool operator==(int Other) const { return Reg == unsigned(Other); }
-  bool operator!=(int Other) const { return Reg != unsigned(Other); }
+  constexpr bool operator==(unsigned Other) const { return Reg == Other; }
+  constexpr bool operator!=(unsigned Other) const { return Reg != Other; }
+  constexpr bool operator==(int Other) const { return Reg == unsigned(Other); }
+  constexpr bool operator!=(int Other) const { return Reg != unsigned(Other); }
   // MSVC requires that we explicitly declare these two as well.
-  bool operator==(MCPhysReg Other) const { return Reg == unsigned(Other); }
-  bool operator!=(MCPhysReg Other) const { return Reg != unsigned(Other); }
+  constexpr bool operator==(MCPhysReg Other) const {
+    return Reg == unsigned(Other);
+  }
+  constexpr bool operator!=(MCPhysReg Other) const {
+    return Reg != unsigned(Other);
+  }
 };
 
 // Provide DenseMapInfo for MCRegister
-template<> struct DenseMapInfo<MCRegister> {
+template <> struct DenseMapInfo<MCRegister> {
   static inline unsigned getEmptyKey() {
     return DenseMapInfo<unsigned>::getEmptyKey();
   }
@@ -111,6 +115,6 @@ template<> struct DenseMapInfo<MCRegister> {
 inline hash_code hash_value(const MCRegister &Reg) {
   return hash_value(Reg.id());
 }
-}
+} // namespace llvm
 
 #endif // LLVM_MC_MCREGISTER_H


        


More information about the llvm-commits mailing list