[llvm] [Support] Make getMaxValue and getMinValue constexpr variables (NFC) (PR #161480)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 1 07:18:41 PDT 2025


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/161480

>From 2343cd556b7c5ae00ef33de4a936896b681c8387 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 30 Sep 2025 00:41:48 -0700
Subject: [PATCH 1/2] [Support] Make getMaxValue and getMinValue constexpr
 variables (NFC)

This patch makes getMaxValue and getMinValue constexpr variables and
"inlines" the calls to getMaxValue and getMinValue.

We could probably make InstructionCost constexpr also, but that's left
for another day.
---
 llvm/include/llvm/Support/InstructionCost.h | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/Support/InstructionCost.h b/llvm/include/llvm/Support/InstructionCost.h
index ab1c8ebc8c95e..616b772a6e898 100644
--- a/llvm/include/llvm/Support/InstructionCost.h
+++ b/llvm/include/llvm/Support/InstructionCost.h
@@ -59,8 +59,10 @@ class InstructionCost {
       State = Invalid;
   }
 
-  static CostType getMaxValue() { return std::numeric_limits<CostType>::max(); }
-  static CostType getMinValue() { return std::numeric_limits<CostType>::min(); }
+  static inline constexpr CostType MaxValue =
+      std::numeric_limits<CostType>::max();
+  static inline constexpr CostType MinValue =
+      std::numeric_limits<CostType>::min();
 
 public:
   // A default constructed InstructionCost is a valid zero cost
@@ -69,8 +71,8 @@ class InstructionCost {
   InstructionCost(CostState) = delete;
   InstructionCost(CostType Val) : Value(Val), State(Valid) {}
 
-  static InstructionCost getMax() { return getMaxValue(); }
-  static InstructionCost getMin() { return getMinValue(); }
+  static InstructionCost getMax() { return MaxValue; }
+  static InstructionCost getMin() { return MinValue; }
   static InstructionCost getInvalid(CostType Val = 0) {
     InstructionCost Tmp(Val);
     Tmp.setInvalid();
@@ -102,7 +104,7 @@ class InstructionCost {
     // Saturating addition.
     InstructionCost::CostType Result;
     if (AddOverflow(Value, RHS.Value, Result))
-      Result = RHS.Value > 0 ? getMaxValue() : getMinValue();
+      Result = RHS.Value > 0 ? MaxValue : MinValue;
 
     Value = Result;
     return *this;
@@ -120,7 +122,7 @@ class InstructionCost {
     // Saturating subtract.
     InstructionCost::CostType Result;
     if (SubOverflow(Value, RHS.Value, Result))
-      Result = RHS.Value > 0 ? getMinValue() : getMaxValue();
+      Result = RHS.Value > 0 ? MinValue : MaxValue;
     Value = Result;
     return *this;
   }
@@ -138,9 +140,9 @@ class InstructionCost {
     InstructionCost::CostType Result;
     if (MulOverflow(Value, RHS.Value, Result)) {
       if ((Value > 0 && RHS.Value > 0) || (Value < 0 && RHS.Value < 0))
-        Result = getMaxValue();
+        Result = MaxValue;
       else
-        Result = getMinValue();
+        Result = MinValue;
     }
 
     Value = Result;

>From 893cde3178eb0c0ef1d6100647c832a2a607bfbc Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 1 Oct 2025 07:18:14 -0700
Subject: [PATCH 2/2] Address a comment.

---
 llvm/include/llvm/Support/InstructionCost.h | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/Support/InstructionCost.h b/llvm/include/llvm/Support/InstructionCost.h
index 616b772a6e898..507c16666b958 100644
--- a/llvm/include/llvm/Support/InstructionCost.h
+++ b/llvm/include/llvm/Support/InstructionCost.h
@@ -59,10 +59,8 @@ class InstructionCost {
       State = Invalid;
   }
 
-  static inline constexpr CostType MaxValue =
-      std::numeric_limits<CostType>::max();
-  static inline constexpr CostType MinValue =
-      std::numeric_limits<CostType>::min();
+  static constexpr CostType MaxValue = std::numeric_limits<CostType>::max();
+  static constexpr CostType MinValue = std::numeric_limits<CostType>::min();
 
 public:
   // A default constructed InstructionCost is a valid zero cost



More information about the llvm-commits mailing list