Help the compiler to eliminate some dead code when hashing an array of T
authorJay Foad <jay.foad@gmail.com>
Mon, 27 Feb 2012 11:00:17 +0000 (11:00 +0000)
committerJay Foad <jay.foad@gmail.com>
Mon, 27 Feb 2012 11:00:17 +0000 (11:00 +0000)
where sizeof (T) is a multiple of 4.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@151523 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/Hashing.h

index 27c411e3223d65467e2978a721603332d96b2939..682dc223e229f9b323a0c3b79cc07863c2284673 100644 (file)
@@ -142,6 +142,7 @@ private:
   }
 
   // Add a range of bytes from I to E.
+  template<bool ElementsHaveEvenLength>
   void addBytes(const char *I, const char *E) {
     uint32_t Data;
     // Note that aliasing rules forbid us from dereferencing
@@ -154,7 +155,7 @@ private:
       std::memcpy(&Data, I, sizeof Data);
       mix(Data);
     }
-    if (I != E) {
+    if (!ElementsHaveEvenLength && I != E) {
       Data = 0;
       std::memcpy(&Data, I, E - I);
       mix(Data);
@@ -164,8 +165,9 @@ private:
   // Add a range of bits from I to E.
   template<typename T>
   void addBits(const T *I, const T *E) {
-    addBytes(reinterpret_cast<const char *>(I),
-             reinterpret_cast<const char *>(E));
+    addBytes<sizeof (T) % sizeof (uint32_t) == 0>(
+      reinterpret_cast<const char *>(I),
+      reinterpret_cast<const char *>(E));
   }
 };