Skip to content

Conversation

kazutakahirata
Copy link
Contributor

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Oct 1, 2025

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

Changes

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.


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

1 Files Affected:

  • (modified) llvm/include/llvm/Support/InstructionCost.h (+10-8)
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;

Copy link
Member

@tgymnich tgymnich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the inline seems unnecessary since static and constexpr should already imply inline on data members.

@kazutakahirata
Copy link
Contributor Author

LGTM, the inline seems unnecessary since static and constexpr should already imply inline on data members.

Good point! Fixed. Thanks for the review as always!

@kazutakahirata kazutakahirata merged commit 515660d into llvm:main Oct 1, 2025
9 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250930_Support_InlineCost_constexpr branch October 1, 2025 16:02
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
…lvm#161480)

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.
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.

4 participants