libstdc++: Workaround is_trivially_copyable<volatile T> (PR 94013)
authorJonathan Wakely <jwakely@redhat.com>
Tue, 3 Mar 2020 21:38:57 +0000 (21:38 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 3 Mar 2020 21:39:19 +0000 (21:39 +0000)
commit462f6c2041fad058abcdd5122e99a024f69a39d5
treef6c890cf99d1ae83cf91bfd4a5d751e89d71a5e9
parent0e0ffbfc23ba98ac40cbc6330e2750a6448b79d9
libstdc++: Workaround is_trivially_copyable<volatile T> (PR 94013)

Several algorithms check the is_trivially_copyable trait to decide
whether to dispatch to memmove or memcmp as an optimization. Since
r271435 (CWG DR 2094) the trait is true for volatile-qualified scalars,
but we can't use memmove or memcmp when the type is volatile. We need to
also check for volatile types.

This is complicated by the fact that in C++20 (but not earlier standards)
iterator_traits<volatile T*>::value_type is T, so we can't just check
whether the value_type is volatile.

The solution in this patch is to introduce new traits __memcpyable and
__memcmpable which combine into a single trait the checks for pointers,
the value types being the same, and the type being trivially copyable
but not volatile-qualified.

PR libstdc++/94013
* include/bits/cpp_type_traits.h (__memcpyable, __memcmpable): New
traits to control when to use memmove and memcmp optimizations.
(__is_nonvolatile_trivially_copyable): New helper trait.
* include/bits/ranges_algo.h (__lexicographical_compare_fn): Do not
use memcmp optimization with volatile data.
* include/bits/ranges_algobase.h (__equal_fn): Use __memcmpable.
(__copy_or_move, __copy_or_move_backward): Use __memcpyable.
* include/bits/stl_algobase.h (__copy_move_a2): Use __memcpyable.
(__copy_move_backward_a2): Likewise.
(__equal_aux1): Use __memcmpable.
(__lexicographical_compare_aux): Do not use memcmp optimization with
volatile data.
* testsuite/25_algorithms/copy/94013.cc: New test.
* testsuite/25_algorithms/copy_backward/94013.cc: New test.
* testsuite/25_algorithms/equal/94013.cc: New test.
* testsuite/25_algorithms/fill/94013.cc: New test.
* testsuite/25_algorithms/lexicographical_compare/94013.cc: New test.
* testsuite/25_algorithms/move/94013.cc: New test.
* testsuite/25_algorithms/move_backward/94013.cc: New test.
12 files changed:
libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/cpp_type_traits.h
libstdc++-v3/include/bits/ranges_algo.h
libstdc++-v3/include/bits/ranges_algobase.h
libstdc++-v3/include/bits/stl_algobase.h
libstdc++-v3/testsuite/25_algorithms/copy/94013.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/copy_backward/94013.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/equal/94013.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/fill/94013.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/94013.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/move/94013.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/move_backward/94013.cc [new file with mode: 0644]