| 1 |
// RUN: %clang_cc1 -triple=x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s |
| 2 |
|
| 3 |
// Builtins inside a namespace inside an extern "C" must be considered builtins. |
| 4 |
extern "C" { |
| 5 |
namespace X { |
| 6 |
double __builtin_fabs(double); |
| 7 |
float __builtin_fabsf(float) noexcept; |
| 8 |
} // namespace X |
| 9 |
} |
| 10 |
|
| 11 |
int o = X::__builtin_fabs(-2.0); |
| 12 |
// CHECK: @o ={{.*}} global i32 2, align 4 |
| 13 |
|
| 14 |
long p = X::__builtin_fabsf(-3.0f); |
| 15 |
// CHECK: @p ={{.*}} global i64 3, align 8 |
| 16 |
|
| 17 |
int x = __builtin_abs(-2); |
| 18 |
// CHECK: @x ={{.*}} global i32 2, align 4 |
| 19 |
|
| 20 |
long y = __builtin_abs(-2l); |
| 21 |
// CHECK: @y ={{.*}} global i64 2, align 8 |
| 22 |
|
| 23 |
// PR8839 |
| 24 |
extern "C" char memmove(); |
| 25 |
|
| 26 |
int main() { |
| 27 |
// CHECK: call {{signext i8|i8}} @memmove() |
| 28 |
return memmove(); |
| 29 |
} |
| 30 |
|
| 31 |
struct S; |
| 32 |
// CHECK: define {{.*}} @_Z9addressofbR1SS0_( |
| 33 |
S *addressof(bool b, S &s, S &t) { |
| 34 |
// CHECK: %[[LVALUE:.*]] = phi |
| 35 |
// CHECK: ret ptr %[[LVALUE]] |
| 36 |
return __builtin_addressof(b ? s : t); |
| 37 |
} |
| 38 |
|
| 39 |
namespace std { template<typename T> T *addressof(T &); } |
| 40 |
|
| 41 |
// CHECK: define {{.*}} @_Z13std_addressofbR1SS0_( |
| 42 |
S *std_addressof(bool b, S &s, S &t) { |
| 43 |
// CHECK: %[[LVALUE:.*]] = phi |
| 44 |
// CHECK: ret ptr %[[LVALUE]] |
| 45 |
return std::addressof(b ? s : t); |
| 46 |
} |
| 47 |
|
| 48 |
namespace std { template<typename T> T *__addressof(T &); } |
| 49 |
|
| 50 |
// CHECK: define {{.*}} @_Z15std___addressofbR1SS0_( |
| 51 |
S *std___addressof(bool b, S &s, S &t) { |
| 52 |
// CHECK: %[[LVALUE:.*]] = phi |
| 53 |
// CHECK: ret ptr %[[LVALUE]] |
| 54 |
return std::__addressof(b ? s : t); |
| 55 |
} |
| 56 |
|
| 57 |
extern "C" int __builtin_abs(int); // #1 |
| 58 |
long __builtin_abs(long); // #2 |
| 59 |
extern "C" int __builtin_abs(int); // #3 |
| 60 |
|
| 61 |
extern const char char_memchr_arg[32]; |
| 62 |
char *memchr_result = __builtin_char_memchr(char_memchr_arg, 123, 32); |
| 63 |
// CHECK: call ptr @memchr(ptr noundef @char_memchr_arg, i32 noundef 123, i64 noundef 32) |
| 64 |
|
| 65 |
int constexpr_overflow_result() { |
| 66 |
constexpr int x = 1; |
| 67 |
// CHECK: alloca i32 |
| 68 |
constexpr int y = 2; |
| 69 |
// CHECK: alloca i32 |
| 70 |
int z; |
| 71 |
// CHECK: [[Z:%.+]] = alloca i32 |
| 72 |
|
| 73 |
__builtin_sadd_overflow(x, y, &z); |
| 74 |
return z; |
| 75 |
// CHECK: [[RET_PTR:%.+]] = extractvalue { i32, i1 } %0, 0 |
| 76 |
// CHECK: store i32 [[RET_PTR]], ptr [[Z]] |
| 77 |
// CHECK: [[RET_VAL:%.+]] = load i32, ptr [[Z]] |
| 78 |
// CHECK: ret i32 [[RET_VAL]] |
| 79 |
} |
| 80 |
|
| 81 |
int structured_binding_size() { |
| 82 |
struct S2 {int a, b;}; |
| 83 |
return __builtin_structured_binding_size(S2); |
| 84 |
// CHECK: ret i32 2 |
| 85 |
} |
| 86 |
|
| 87 |
void test_int_reference(int& a) { |
| 88 |
__builtin_bswapg(a); |
| 89 |
} |
| 90 |
// CHECK-LABEL: @_Z18test_int_referenceRi |
| 91 |
// CHECK: store ptr %a, ptr |
| 92 |
// CHECK: load ptr, ptr |
| 93 |
// CHECK: load i32, ptr |
| 94 |
// CHECK: call i32 @llvm.bswap.i32 |
| 95 |
|
| 96 |
void test_long_reference(long& a) { |
| 97 |
__builtin_bswapg(a); |
| 98 |
} |
| 99 |
// CHECK-LABEL: @_Z19test_long_referenceRl |
| 100 |
// CHECK: store ptr %a, ptr |
| 101 |
// CHECK: load ptr, ptr |
| 102 |
// CHECK: load i64, ptr |
| 103 |
// CHECK: call i64 @llvm.bswap.i64 |
| 104 |
|
| 105 |
void test_short_reference(short& a) { |
| 106 |
__builtin_bswapg(a); |
| 107 |
} |
| 108 |
// CHECK-LABEL: @_Z20test_short_referenceRs |
| 109 |
// CHECK: store ptr %a, ptr |
| 110 |
// CHECK: load ptr, ptr |
| 111 |
// CHECK: load i16, ptr |
| 112 |
// CHECK: call i16 @llvm.bswap.i16 |
| 113 |
|
| 114 |
void test_char_reference(char& a) { |
| 115 |
__builtin_bswapg(a); |
| 116 |
} |
| 117 |
// CHECK-LABEL: @_Z19test_char_referenceRc |
| 118 |
// CHECK: store ptr %a, ptr |
| 119 |
// CHECK: load ptr, ptr |
| 120 |
// CHECK-NOT: call i8 @llvm.bswap.i8 |
| 121 |
// CHECK: ret void |
| 122 |
|
| 123 |
void test_bitint() { |
| 124 |
_BitInt(8) a = 0x12; |
| 125 |
__builtin_bswapg(a); |
| 126 |
_BitInt(16) b = 0x1234; |
| 127 |
__builtin_bswapg(b); |
| 128 |
_BitInt(32) c = 0x00001234; |
| 129 |
__builtin_bswapg(c); |
| 130 |
_BitInt(64) d = 0x0000000000001234; |
| 131 |
__builtin_bswapg(d); |
| 132 |
_BitInt(128) e = ~(_BitInt(128))0; |
| 133 |
__builtin_bswapg(e); |
| 134 |
} |
| 135 |
// CHECK-LABEL: @_Z11test_bitintv |
| 136 |
// CHECK-NOT: call i8 @llvm.bswap.i8 |
| 137 |
// CHECK: call i16 @llvm.bswap.i16 |
| 138 |
// CHECK: call i32 @llvm.bswap.i32 |
| 139 |
// CHECK: call i64 @llvm.bswap.i64 |
| 140 |
// CHECK: call i128 @llvm.bswap.i128 |
| 141 |
// CHECK: ret void |