1 // RUN: %clang_cc1 -emit-llvm -o %t %s
2 // RUN: not grep __builtin %t
3 // RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple | FileCheck %s
5 int printf(const char *, ...);
7 void p(char *str, int x) {
8 printf("%s: %d\n", str, x);
10 void q(char *str, double x) {
11 printf("%s: %f\n", str, x);
13 void r(char *str, void *ptr) {
14 printf("%s: %p\n", str, ptr);
21 #define P(n,args) p(#n #args, __builtin_##n args)
22 #define Q(n,args) q(#n #args, __builtin_##n args)
23 #define R(n,args) r(#n #args, __builtin_##n args)
24 #define V(n,args) p(#n #args, (__builtin_##n args, 0))
25 P(types_compatible_p, (int, float));
26 P(choose_expr, (0, 10, 20));
27 P(constant_p, (sizeof(10)));
28 P(expect, (N == 12, 0));
31 V(prefetch, (&N, 1, 0));
42 P(fpclassify, (0, 1, 2, 3, 4, 1.0));
43 P(fpclassify, (0, 1, 2, 3, 4, 1.0f));
44 P(fpclassify, (0, 1, 2, 3, 4, 1.0l));
55 P(isgreater, (1., 2.));
56 P(isgreaterequal, (1., 2.));
58 P(islessequal, (1., 2.));
59 P(islessgreater, (1., 2.));
60 P(isunordered, (1., 2.));
66 // Bitwise & Numeric Functions
90 int a, b, n = random(); // Avoid optimizing out.
91 char s0[10], s1[] = "Hello";
94 V(strncat, (s0, s1, n));
95 V(strchr, (s0, s1[0]));
96 V(strrchr, (s0, s1[0]));
98 V(strncpy, (s0, s1, n));
100 // Object size checking
101 V(__memset_chk, (s0, 0, sizeof s0, n));
102 V(__memcpy_chk, (s0, s1, sizeof s0, n));
103 V(__memmove_chk, (s0, s1, sizeof s0, n));
104 V(__mempcpy_chk, (s0, s1, sizeof s0, n));
105 V(__strncpy_chk, (s0, s1, sizeof s0, n));
106 V(__strcpy_chk, (s0, s1, n));
108 V(__strcat_chk, (s0, s1, n));
109 P(object_size, (s0, 0));
110 P(object_size, (s0, 1));
111 P(object_size, (s0, 2));
112 P(object_size, (s0, 3));
120 // V(clear_cache, (&N, &N+1));
122 R(extract_return_addr, (&N));
131 __builtin_strcat(0, 0);
134 // CHECK-LABEL: define void @bar(
140 // LLVM's hex representation of float constants is really unfortunate;
141 // basically it does a float-to-double "conversion" and then prints the
142 // hex form of that. That gives us weird artifacts like exponents
143 // that aren't numerically similar to the original exponent and
144 // significand bit-patterns that are offset by three bits (because
145 // the exponent was expanded from 8 bits to 11).
147 // 0xAE98 == 1010111010011000
148 // 0x15D3 == 1010111010011
150 f = __builtin_huge_valf(); // CHECK: float 0x7FF0000000000000
151 d = __builtin_huge_val(); // CHECK: double 0x7FF0000000000000
152 ld = __builtin_huge_vall(); // CHECK: x86_fp80 0xK7FFF8000000000000000
153 f = __builtin_nanf(""); // CHECK: float 0x7FF8000000000000
154 d = __builtin_nan(""); // CHECK: double 0x7FF8000000000000
155 ld = __builtin_nanl(""); // CHECK: x86_fp80 0xK7FFFC000000000000000
156 f = __builtin_nanf("0xAE98"); // CHECK: float 0x7FF815D300000000
157 d = __builtin_nan("0xAE98"); // CHECK: double 0x7FF800000000AE98
158 ld = __builtin_nanl("0xAE98"); // CHECK: x86_fp80 0xK7FFFC00000000000AE98
159 f = __builtin_nansf(""); // CHECK: float 0x7FF4000000000000
160 d = __builtin_nans(""); // CHECK: double 0x7FF4000000000000
161 ld = __builtin_nansl(""); // CHECK: x86_fp80 0xK7FFFA000000000000000
162 f = __builtin_nansf("0xAE98"); // CHECK: float 0x7FF015D300000000
163 d = __builtin_nans("0xAE98"); // CHECK: double 0x7FF000000000AE98
164 ld = __builtin_nansl("0xAE98");// CHECK: x86_fp80 0xK7FFF800000000000AE98
170 // CHECK-LABEL: define void @test_float_builtins
171 void test_float_builtins(float F, double D, long double LD) {
173 res = __builtin_isinf(F);
174 // CHECK: call float @llvm.fabs.f32(float
175 // CHECK: fcmp oeq float {{.*}}, 0x7FF0000000000000
177 res = __builtin_isinf(D);
178 // CHECK: call double @llvm.fabs.f64(double
179 // CHECK: fcmp oeq double {{.*}}, 0x7FF0000000000000
181 res = __builtin_isinf(LD);
182 // CHECK: call x86_fp80 @llvm.fabs.f80(x86_fp80
183 // CHECK: fcmp oeq x86_fp80 {{.*}}, 0xK7FFF8000000000000000
185 res = __builtin_isinf_sign(F);
186 // CHECK: %[[ABS:.*]] = call float @llvm.fabs.f32(float %[[ARG:.*]])
187 // CHECK: %[[ISINF:.*]] = fcmp oeq float %[[ABS]], 0x7FF0000000000000
188 // CHECK: %[[BITCAST:.*]] = bitcast float %[[ARG]] to i32
189 // CHECK: %[[ISNEG:.*]] = icmp slt i32 %[[BITCAST]], 0
190 // CHECK: %[[SIGN:.*]] = select i1 %[[ISNEG]], i32 -1, i32 1
191 // CHECK: select i1 %[[ISINF]], i32 %[[SIGN]], i32 0
193 res = __builtin_isinf_sign(D);
194 // CHECK: %[[ABS:.*]] = call double @llvm.fabs.f64(double %[[ARG:.*]])
195 // CHECK: %[[ISINF:.*]] = fcmp oeq double %[[ABS]], 0x7FF0000000000000
196 // CHECK: %[[BITCAST:.*]] = bitcast double %[[ARG]] to i64
197 // CHECK: %[[ISNEG:.*]] = icmp slt i64 %[[BITCAST]], 0
198 // CHECK: %[[SIGN:.*]] = select i1 %[[ISNEG]], i32 -1, i32 1
199 // CHECK: select i1 %[[ISINF]], i32 %[[SIGN]], i32 0
201 res = __builtin_isinf_sign(LD);
202 // CHECK: %[[ABS:.*]] = call x86_fp80 @llvm.fabs.f80(x86_fp80 %[[ARG:.*]])
203 // CHECK: %[[ISINF:.*]] = fcmp oeq x86_fp80 %[[ABS]], 0xK7FFF8000000000000000
204 // CHECK: %[[BITCAST:.*]] = bitcast x86_fp80 %[[ARG]] to i80
205 // CHECK: %[[ISNEG:.*]] = icmp slt i80 %[[BITCAST]], 0
206 // CHECK: %[[SIGN:.*]] = select i1 %[[ISNEG]], i32 -1, i32 1
207 // CHECK: select i1 %[[ISINF]], i32 %[[SIGN]], i32 0
209 res = __builtin_isfinite(F);
210 // CHECK: fcmp oeq float
211 // CHECK: call float @llvm.fabs.f32(float
212 // CHECK: fcmp une float {{.*}}, 0x7FF0000000000000
215 res = __builtin_isnormal(F);
216 // CHECK: fcmp oeq float
217 // CHECK: call float @llvm.fabs.f32(float
218 // CHECK: fcmp ult float {{.*}}, 0x7FF0000000000000
219 // CHECK: fcmp uge float {{.*}}, 0x3810000000000000
224 // CHECK-LABEL: define void @test_float_builtin_ops
225 void test_float_builtin_ops(float F, double D, long double LD) {
227 volatile double resd;
228 volatile long double resld;
230 resf = __builtin_fmodf(F,F);
233 resd = __builtin_fmod(D,D);
234 // CHECK: frem double
236 resld = __builtin_fmodl(LD,LD);
237 // CHECK: frem x86_fp80
239 resf = __builtin_fabsf(F);
240 resd = __builtin_fabs(D);
241 resld = __builtin_fabsl(LD);
242 // CHECK: call float @llvm.fabs.f32(float
243 // CHECK: call double @llvm.fabs.f64(double
244 // CHECK: call x86_fp80 @llvm.fabs.f80(x86_fp80
247 // __builtin_longjmp isn't supported on all platforms, so only test it on X86.
249 // CHECK-LABEL: define void @test_builtin_longjmp
250 void test_builtin_longjmp(void **buffer) {
251 // CHECK: [[BITCAST:%.*]] = bitcast
252 // CHECK-NEXT: call void @llvm.eh.sjlj.longjmp(i8* [[BITCAST]])
253 __builtin_longjmp(buffer, 1);
254 // CHECK-NEXT: unreachable
258 // CHECK-LABEL: define i64 @test_builtin_readcyclecounter
259 long long test_builtin_readcyclecounter() {
260 // CHECK: call i64 @llvm.readcyclecounter()
261 return __builtin_readcyclecounter();