<div dir="ltr">straight up ^ isn't a very good hash algorithm, as you're seeing - my understanding was even a rudimentary hash algorithm multiplied the previous value by a prime before combining it<br><br>But I think we have a library in llvm for hashing that probably does something pretty good, whatever it is - llvm/ADT/Hashing.h - probably use that? (hash_combine(x, y, z), I guess?)</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Sep 9, 2021 at 11:54 PM Jeroen Dobbelaere via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all,<br>
<br>
is there a recommended way to implement a 'DenseMapInfo<..>::getHashValue()'<br>
for a class that contains multiple pointers of which some are potentially identical ?<br>
<br>
The standard used xor (^) is canceling out the hash contribution of those identical pointers.<br>
<br>
More concrete: I am adding a 'PtrProvenance' member to MemoryLocation. When I add<br>
the member to the hash computation in the way it seems intended,<br>
<br>
<a href="https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Analysis/MemoryLocation.h#L340-L344" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Analysis/MemoryLocation.h#L340-L344</a><br>
<br>
I would do:<br>
<br>
  static unsigned getHashValue(const MemoryLocation &Val) {<br>
      return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^<br>
           DenseMapInfo<const Value *>::getHashValue(Val.PtrProvenance) ^ // Handle new member<br>
           DenseMapInfo<LocationSize>::getHashValue(Val.Size) ^<br>
           DenseMapInfo<AAMDNodes>::getHashValue(Val.AATags);<br>
  }<br>
<br>
<br>
But, in the initial case, it is very likely that Val.Ptr == Val.PtrProvenance.<br>
<br>
Is there a recommended (standard) way to fix this ?<br>
<br>
Thanks,<br>
<br>
Jeroen Dobbelaere<br>
<br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>