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 offc30901096,a571f82a50,64c045e25b, andde6ddaeef3, and revertsaa643f455a. This change also includes a workaround for users using libc++ 3.1 and earlier (!!), as apparently happens on AIX, where std::move sometimes returns by value. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D123345 Revert "Fixup D123950 to address revert of D123345" This reverts commitaa643f455a.
81 lines
2.3 KiB
C++
81 lines
2.3 KiB
C++
// RUN: %clang_cc1 -no-opaque-pointers -triple=x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
|
|
|
|
// Builtins inside a namespace inside an extern "C" must be considered builtins.
|
|
extern "C" {
|
|
namespace X {
|
|
double __builtin_fabs(double);
|
|
float __builtin_fabsf(float) noexcept;
|
|
} // namespace X
|
|
}
|
|
|
|
int o = X::__builtin_fabs(-2.0);
|
|
// CHECK: @o ={{.*}} global i32 2, align 4
|
|
|
|
long p = X::__builtin_fabsf(-3.0f);
|
|
// CHECK: @p ={{.*}} global i64 3, align 8
|
|
|
|
// PR8839
|
|
extern "C" char memmove();
|
|
|
|
int main() {
|
|
// CHECK: call {{signext i8|i8}} @memmove()
|
|
return memmove();
|
|
}
|
|
|
|
struct S;
|
|
// CHECK: define {{.*}} @_Z9addressofbR1SS0_(
|
|
S *addressof(bool b, S &s, S &t) {
|
|
// CHECK: %[[LVALUE:.*]] = phi
|
|
// CHECK: ret {{.*}}* %[[LVALUE]]
|
|
return __builtin_addressof(b ? s : t);
|
|
}
|
|
|
|
namespace std { template<typename T> T *addressof(T &); }
|
|
|
|
// CHECK: define {{.*}} @_Z13std_addressofbR1SS0_(
|
|
S *std_addressof(bool b, S &s, S &t) {
|
|
// CHECK: %[[LVALUE:.*]] = phi
|
|
// CHECK: ret {{.*}}* %[[LVALUE]]
|
|
return std::addressof(b ? s : t);
|
|
}
|
|
|
|
namespace std { template<typename T> T *__addressof(T &); }
|
|
|
|
// CHECK: define {{.*}} @_Z15std___addressofbR1SS0_(
|
|
S *std___addressof(bool b, S &s, S &t) {
|
|
// CHECK: %[[LVALUE:.*]] = phi
|
|
// CHECK: ret {{.*}}* %[[LVALUE]]
|
|
return std::__addressof(b ? s : t);
|
|
}
|
|
|
|
extern "C" int __builtin_abs(int); // #1
|
|
long __builtin_abs(long); // #2
|
|
extern "C" int __builtin_abs(int); // #3
|
|
|
|
int x = __builtin_abs(-2);
|
|
// CHECK: store i32 2, i32* @x, align 4
|
|
|
|
long y = __builtin_abs(-2l);
|
|
// CHECK: [[Y:%.+]] = call noundef i64 @_Z13__builtin_absl(i64 noundef -2)
|
|
// CHECK: store i64 [[Y]], i64* @y, align 8
|
|
|
|
extern const char char_memchr_arg[32];
|
|
char *memchr_result = __builtin_char_memchr(char_memchr_arg, 123, 32);
|
|
// CHECK: call i8* @memchr(i8* noundef getelementptr inbounds ([32 x i8], [32 x i8]* @char_memchr_arg, i64 0, i64 0), i32 noundef 123, i64 noundef 32)
|
|
|
|
int constexpr_overflow_result() {
|
|
constexpr int x = 1;
|
|
// CHECK: alloca i32
|
|
constexpr int y = 2;
|
|
// CHECK: alloca i32
|
|
int z;
|
|
// CHECK: [[Z:%.+]] = alloca i32
|
|
|
|
__builtin_sadd_overflow(x, y, &z);
|
|
return z;
|
|
// CHECK: [[RET_PTR:%.+]] = extractvalue { i32, i1 } %0, 0
|
|
// CHECK: store i32 [[RET_PTR]], i32* [[Z]]
|
|
// CHECK: [[RET_VAL:%.+]] = load i32, i32* [[Z]]
|
|
// CHECK: ret i32 [[RET_VAL]]
|
|
}
|