[llvm] b22a97d - [Support] Merge two implementations of addRangeElementsImpl (NFC) (#158212)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 12 07:51:53 PDT 2025


Author: Kazu Hirata
Date: 2025-09-12T07:51:49-07:00
New Revision: b22a97d9d28ad25ecf7c28c0b11e9bfca52923a5

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

LOG: [Support] Merge two implementations of addRangeElementsImpl (NFC) (#158212)

This patch uses "constexpr if" to merge two implementations of
addRangeElementsImpl.  While the line count does not change much, the
"if" condition should be a lot more readable than in std::enable_if.

Added: 
    

Modified: 
    llvm/include/llvm/Support/HashBuilder.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/HashBuilder.h b/llvm/include/llvm/Support/HashBuilder.h
index 097110874400d..17fbc3f96ed04 100644
--- a/llvm/include/llvm/Support/HashBuilder.h
+++ b/llvm/include/llvm/Support/HashBuilder.h
@@ -366,18 +366,16 @@ class HashBuilder : public HashBuilderBase<HasherT> {
   HashBuilder &addRangeElementsImpl(ForwardIteratorT First,
                                     ForwardIteratorT Last,
                                     std::forward_iterator_tag) {
-    for (auto It = First; It != Last; ++It)
-      add(*It);
-    return *this;
-  }
-
-  template <typename T>
-  std::enable_if_t<hashbuilder_detail::IsHashableData<T>::value &&
-                       Endianness == llvm::endianness::native,
-                   HashBuilder &>
-  addRangeElementsImpl(T *First, T *Last, std::forward_iterator_tag) {
-    this->update(ArrayRef(reinterpret_cast<const uint8_t *>(First),
-                          (Last - First) * sizeof(T)));
+    using T = typename std::iterator_traits<ForwardIteratorT>::value_type;
+    if constexpr (std::is_pointer_v<ForwardIteratorT> &&
+                  hashbuilder_detail::IsHashableData<T>::value &&
+                  Endianness == llvm::endianness::native) {
+      this->update(ArrayRef(reinterpret_cast<const uint8_t *>(First),
+                            (Last - First) * sizeof(T)));
+    } else {
+      for (auto It = First; It != Last; ++It)
+        add(*It);
+    }
     return *this;
   }
 };


        


More information about the llvm-commits mailing list