[llvm] [ADT] Simplify DenseMapInfo<std::tuple<...>> with constexpr if (NFC) (PR #156810)
    Kazu Hirata via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Wed Sep  3 23:26:06 PDT 2025
    
    
  
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/156810
This patch consolidates two implementations of getHashValueImpl into
one with "constexpr if", which should be more readable than the
SFINAE-based approach.
The same applies to isEqualImpl.
>From 7c683bf80c50b95b39f490602939cf68f7573e5c Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 3 Sep 2025 00:34:13 -0700
Subject: [PATCH] [ADT] Simplify DenseMapInfo<std::tuple<...>> with constexpr
 if (NFC)
This patch consolidates two implementations of getHashValueImpl into
one with "constexpr if", which should be more readable than the
SFINAE-based approach.
The same applies to isEqualImpl.
---
 llvm/include/llvm/ADT/DenseMapInfo.h | 46 ++++++++++++----------------
 1 file changed, 20 insertions(+), 26 deletions(-)
diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h
index ec7a116856bb4..717156c4bd196 100644
--- a/llvm/include/llvm/ADT/DenseMapInfo.h
+++ b/llvm/include/llvm/ADT/DenseMapInfo.h
@@ -179,41 +179,35 @@ template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
     return Tuple(DenseMapInfo<Ts>::getTombstoneKey()...);
   }
 
-  template <unsigned I>
-  static unsigned getHashValueImpl(const Tuple &values, std::false_type) {
-    using EltType = std::tuple_element_t<I, Tuple>;
-    std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
-    return detail::combineHashValue(
-        DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
-        getHashValueImpl<I + 1>(values, atEnd));
-  }
-
-  template <unsigned I>
-  static unsigned getHashValueImpl(const Tuple &, std::true_type) {
-    return 0;
+  template <unsigned I> static unsigned getHashValueImpl(const Tuple &values) {
+    if constexpr (I == sizeof...(Ts))
+      return 0;
+    else {
+      using EltType = std::tuple_element_t<I, Tuple>;
+      return detail::combineHashValue(
+          DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
+          getHashValueImpl<I + 1>(values));
+    }
   }
 
   static unsigned getHashValue(const std::tuple<Ts...> &values) {
-    std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
-    return getHashValueImpl<0>(values, atEnd);
+    return getHashValueImpl<0>(values);
   }
 
   template <unsigned I>
-  static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::false_type) {
-    using EltType = std::tuple_element_t<I, Tuple>;
-    std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
-    return DenseMapInfo<EltType>::isEqual(std::get<I>(lhs), std::get<I>(rhs)) &&
-           isEqualImpl<I + 1>(lhs, rhs, atEnd);
-  }
-
-  template <unsigned I>
-  static bool isEqualImpl(const Tuple &, const Tuple &, std::true_type) {
-    return true;
+  static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs) {
+    if constexpr (I == sizeof...(Ts))
+      return true;
+    else {
+      using EltType = std::tuple_element_t<I, Tuple>;
+      return DenseMapInfo<EltType>::isEqual(std::get<I>(lhs),
+                                            std::get<I>(rhs)) &&
+             isEqualImpl<I + 1>(lhs, rhs);
+    }
   }
 
   static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
-    std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
-    return isEqualImpl<0>(lhs, rhs, atEnd);
+    return isEqualImpl<0>(lhs, rhs);
   }
 };
 
    
    
More information about the llvm-commits
mailing list