llvm-project/clang/test/CodeGenCXX/builtin-std-move.cpp
Richard Smith b27430f9f4 Treat std::move, forward, etc. as builtins.
This is extended to all `std::` functions that take a reference to a
value and return a reference (or pointer) to that same value: `move`,
`forward`, `move_if_noexcept`, `as_const`, `addressof`, and the
libstdc++-specific function `__addressof`.

We still require these functions to be declared before they can be used,
but don't instantiate their definitions unless their addresses are
taken. Instead, code generation, constant evaluation, and static
analysis are given direct knowledge of their effect.

This change aims to reduce various costs associated with these functions
-- per-instantiation memory costs, compile time and memory costs due to
creating out-of-line copies and inlining them, code size at -O0, and so
on -- so that they are not substantially more expensive than a cast.
Most of these improvements are very small, but I measured a 3% decrease
in -O0 object file size for a simple C++ source file using the standard
library after this change.

We now automatically infer the `const` and `nothrow` attributes on these
now-builtin functions, in particular meaning that we get a warning for
an unused call to one of these functions.

In C++20 onwards, we disallow taking the addresses of these functions,
per the C++20 "addressable function" rule. In earlier language modes, a
compatibility warning is produced but the address can still be taken.

The same infrastructure is extended to the existing MSVC builtin
`__GetExceptionInfo`, which is now only recognized in namespace `std`
like it always should have been.

This is a re-commit of
  fc3090109643af8d2da9822d0f99c84742b9c877,
  a571f82a50416b767fd3cce0fb5027bb5dfec58c, and
  64c045e25b8471bbb572bd29159c294a82a86a25
which were reverted in
  e75d8b70370435b0ad10388afba0df45fcf9bfcc
due to a crasher bug where CodeGen would emit a builtin glvalue as an
rvalue if it constant-folds.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D123345
2022-04-17 13:26:16 -07:00

67 lines
2.2 KiB
C++

// RUN: %clang_cc1 -triple=x86_64-linux-gnu -emit-llvm -o - -std=c++17 %s | FileCheck %s --implicit-check-not=@_ZSt4move
namespace std {
template<typename T> constexpr T &&move(T &val) { return static_cast<T&&>(val); }
template<typename T> constexpr T &&move_if_noexcept(T &val);
template<typename T> constexpr T &&forward(T &val);
template<typename T> constexpr const T &as_const(T &val);
// Not the builtin.
template<typename T, typename U> T move(U source, U source_end, T dest);
}
class T {};
extern "C" void take(T &&);
extern "C" void take_lval(const T &);
T a;
// Check emission of a constant-evaluated call.
// CHECK-DAG: @move_a = constant ptr @a
T &&move_a = std::move(a);
// CHECK-DAG: @move_if_noexcept_a = constant ptr @a
T &&move_if_noexcept_a = std::move_if_noexcept(a);
// CHECK-DAG: @forward_a = constant ptr @a
T &forward_a = std::forward<T&>(a);
// Check emission of a non-constant call.
// CHECK-LABEL: define {{.*}} void @test
extern "C" void test(T &t) {
// CHECK: store ptr %{{.*}}, ptr %[[T_REF:[^,]*]]
// CHECK: %0 = load ptr, ptr %[[T_REF]]
// CHECK: call void @take(ptr {{.*}} %0)
take(std::move(t));
// CHECK: %1 = load ptr, ptr %[[T_REF]]
// CHECK: call void @take(ptr {{.*}} %1)
take(std::move_if_noexcept(t));
// CHECK: %2 = load ptr, ptr %[[T_REF]]
// CHECK: call void @take(ptr {{.*}} %2)
take(std::forward<T&&>(t));
// CHECK: %3 = load ptr, ptr %[[T_REF]]
// CHECK: call void @take_lval(ptr {{.*}} %3)
take_lval(std::as_const<T&&>(t));
// CHECK: call {{.*}} @_ZSt4moveI1TS0_ET_T0_S2_S1_
std::move(t, t, t);
}
// CHECK: declare {{.*}} @_ZSt4moveI1TS0_ET_T0_S2_S1_
// Check that we instantiate and emit if the address is taken.
// CHECK-LABEL: define {{.*}} @use_address
extern "C" void *use_address() {
// CHECK: ret {{.*}} @_ZSt4moveIiEOT_RS0_
return (void*)&std::move<int>;
}
// CHECK: define {{.*}} ptr @_ZSt4moveIiEOT_RS0_(ptr
extern "C" void take_const_int_rref(const int &&);
// CHECK-LABEL: define {{.*}} @move_const_int(
extern "C" void move_const_int() {
// CHECK: store i32 5, ptr %[[N_ADDR:[^,]*]]
const int n = 5;
// CHECK: call {{.*}} @take_const_int_rref(ptr {{.*}} %[[N_ADDR]])
take_const_int_rref(std::move(n));
}