[llvm] [ADT] Simplify DenseMapInfo<std::tuple<...>> with constexpr if (NFC) (PR #156810)
    via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Wed Sep  3 23:26:37 PDT 2025
    
    
  
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Kazu Hirata (kazutakahirata)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/156810.diff
1 Files Affected:
- (modified) llvm/include/llvm/ADT/DenseMapInfo.h (+20-26) 
``````````diff
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);
   }
 };
 
``````````
</details>
https://github.com/llvm/llvm-project/pull/156810
    
    
More information about the llvm-commits
mailing list