1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
13 // We optimize std::copy(_backward) and std::move(_backward) into memmove
14 // when the iterator is trivial and contiguous and the type in question
15 // is also trivially (copyable, movable). This test verifies that the
16 // optimization never eliminates an actually non-trivial copy or move.
22 #include "test_macros.h"
23 #include "test_iterators.h"
27 constexpr TMBNTC(int& copies
) : p(&copies
) {}
28 constexpr TMBNTC(const TMBNTC
&) = default;
29 TEST_CONSTEXPR_CXX14 TMBNTC
& operator=(TMBNTC
&&) = default;
30 TEST_CONSTEXPR_CXX14 TMBNTC
& operator=(const TMBNTC
&) { ++*p
; return *this; }
33 TEST_CONSTEXPR_CXX20
bool
34 test_trivial_moveassign_but_no_trivial_copyassign()
37 TMBNTC ia
[] = { copies
, copies
, copies
, copies
};
38 TMBNTC ib
[] = { copies
, copies
, copies
, copies
};
39 std::copy(ia
, ia
+4, ib
);
42 std::copy_backward(ia
, ia
+4, ib
+4);
46 std::copy(std::make_move_iterator(ia
), std::make_move_iterator(ia
+4), ib
);
48 std::copy_backward(std::make_move_iterator(ia
), std::make_move_iterator(ia
+4), ib
+4);
51 std::move(ia
, ia
+4, ib
);
53 std::move_backward(ia
, ia
+4, ib
+4);
61 constexpr TCBNTM(int& moves
) : p(&moves
) {}
62 constexpr TCBNTM(const TCBNTM
&) = default;
63 TEST_CONSTEXPR_CXX14 TCBNTM
& operator=(TCBNTM
&&) { ++*p
; return *this; }
64 TEST_CONSTEXPR_CXX14 TCBNTM
& operator=(const TCBNTM
&) = default;
67 TEST_CONSTEXPR_CXX20
bool
68 test_trivial_copyassign_but_no_trivial_moveassign()
71 TCBNTM ia
[] = { moves
, moves
, moves
, moves
};
72 TCBNTM ib
[] = { moves
, moves
, moves
, moves
};
73 std::move(ia
, ia
+4, ib
);
76 std::move_backward(ia
, ia
+4, ib
+4);
80 std::copy(std::make_move_iterator(ia
), std::make_move_iterator(ia
+4), ib
);
83 std::copy_backward(std::make_move_iterator(ia
), std::make_move_iterator(ia
+4), ib
+4);
87 std::copy(ia
, ia
+4, ib
);
89 std::copy_backward(ia
, ia
+4, ib
+4);
97 test_trivial_moveassign_but_no_trivial_copyassign();
98 test_trivial_copyassign_but_no_trivial_moveassign();
100 #if TEST_STD_VER > 17
101 static_assert(test_trivial_moveassign_but_no_trivial_copyassign());
102 static_assert(test_trivial_copyassign_but_no_trivial_moveassign());