[PATCH] Help compiler detect invalid code

François Dumont frs.dumont@gmail.com
Tue Oct 1 20:05:00 GMT 2019


On 9/27/19 1:24 PM, Jonathan Wakely wrote:
> On 20/09/19 07:08 +0200, François Dumont wrote:
>> I already realized that previous patch will be too controversial to 
>> be accepted.
>>
>> In this new version I just implement a real memmove in __memmove so 
>
> A real memmove doesn't just work backwards, it needs to detect any
> overlaps and work forwards *or* backwards, as needed.
ok, good to know, I understand now why using __builtin_memcopy didn't 
show any performance enhancement when I tested it !
>
> I think your change will break this case:
>
> #include <algorithm>
>
> constexpr int f(int i, int j, int k)
> {
>  int arr[5] = { 0, 0, i, j, k };
>  std::move(arr+2, arr+5, arr);
>  return arr[0] + arr[1] + arr[2];
> }
>
> static_assert( f(1, 2, 3) == 6 );
>
> This is valid because std::move only requires that the result iterator
> is not in the input range, but it's OK for the two ranges to overlap.
>
> I haven't tested it, but I think with your change the array will end
> up containing {3, 2, 3, 2, 3} instead of {1, 2, 3, 2, 3}.
>
Indeed, I've added a std::move constexpr test in this new proposal which 
demonstrate that.

C++ Standard clearly states that [copy|move]_backward is done backward. 
So in this new proposal I propose to add a __memcopy used in copy/move 
and keep __memmove for *_backward algos. Both are using 
__builtin_memmove as before.


     * include/bits/stl_algobase.h (__memmove): Return void, loop as long as
     __n != 0.
     (__memcopy): New.
     (__copy_move<_IsMove, true, 
std::random_access_iterator_tag>::__copy_m):
     Adapt to use latter.
     (__copy_move_backward_a): Remove std::is_constant_evaluated block.
     * testsuite/25_algorithms/copy/constexpr.cc (test): Add check on copied
     values.
     * testsuite/25_algorithms/copy_backward/constexpr.cc (test): Likewise
     and rename in test1.
     (test2): New.
     * testsuite/25_algorithms/copy/constexpr_neg.cc: New.
     * testsuite/25_algorithms/copy_backward/constexpr.cc: New.
     * testsuite/25_algorithms/equal/constexpr_neg.cc: New.
     * testsuite/25_algorithms/move/constexpr.cc: New.
     * testsuite/25_algorithms/move/constexpr_neg.cc: New.

Tested under Linux x86_64.

Ok to commit ?

François

-------------- next part --------------
A non-text attachment was scrubbed...
Name: constexpr_algo.patch
Type: text/x-patch
Size: 13138 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20191001/4323c754/attachment.bin>


More information about the Libstdc++ mailing list