xref: /llvm-project/clang/test/CodeGenCXX/builtins.cpp (revision 332ac18e318ce0b6bf316d7f35d33d8af4c56fc5)
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