[llvm] r230920 - ArrayRef: Put back std::equal for operator== with a check for the empty ArrayRefs

Philip Reames listmail at philipreames.com
Mon Mar 2 15:10:12 PST 2015


If both array refs are zero length, it looks like we return them as 
non-equal with this change.  That seems incorrect.

Philip

On 03/01/2015 03:35 PM, Benjamin Kramer wrote:
> Author: d0k
> Date: Sun Mar  1 17:35:20 2015
> New Revision: 230920
>
> URL: http://llvm.org/viewvc/llvm-project?rev=230920&view=rev
> Log:
> ArrayRef: Put back std::equal for operator== with a check for the empty ArrayRefs
>
> This has the nice property of compiling down to memcmp when feasible. An empty
> ArrayRef can have a nullptr in its Data field. I didn't find anything in the
> standard speaking against std::equal(nullptr, nullptr, nullptr) begin valid but
> MSVC asserts. The way libstdc++ lowers std::equal down to memcmp also makes
> invoking std::equal with a nullptr undefined behavior so checking is the only
> way to be safe.
>
> The extra check doesn't cost us perf either because we're essentially peeling
> the loop header away from the rotated loop.
>
> Modified:
>      llvm/trunk/include/llvm/ADT/ArrayRef.h
>
> Modified: llvm/trunk/include/llvm/ADT/ArrayRef.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ArrayRef.h?rev=230920&r1=230919&r2=230920&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/ArrayRef.h (original)
> +++ llvm/trunk/include/llvm/ADT/ArrayRef.h Sun Mar  1 17:35:20 2015
> @@ -135,15 +135,9 @@ namespace llvm {
>   
>       /// equals - Check for element-wise equality.
>       bool equals(ArrayRef RHS) const {
> -      if (Length != RHS.Length)
> +      if (Length != RHS.Length || Length == 0)
>           return false;
> -      // Don't use std::equal(), since it asserts in MSVC on nullptr iterators.
> -      for (auto L = begin(), LE = end(), R = RHS.begin(); L != LE; ++L, ++R)
> -        // Match std::equal() in using == (instead of !=) to minimize API
> -        // requirements of ArrayRef'ed types.
> -        if (!(*L == *R))
> -          return false;
> -      return true;
> +      return std::equal(begin(), end(), RHS.begin());
>       }
>   
>       /// slice(n) - Chop off the first N elements of the array.
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list