blob: 39bd84c5a5a4e7442039ce1807f5ec3ce7e5dea0 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -emit-llvm -o %t %s
Daniel Dunbard417d962008-09-03 21:17:21 +00002// RUN: not grep __builtin %t
John McCalldb7b72a2010-02-28 13:00:19 +00003// RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple | FileCheck %s
Daniel Dunbard417d962008-09-03 21:17:21 +00004
Daniel Dunbar23afaad2009-11-17 08:57:36 +00005int printf(const char *, ...);
Daniel Dunbard417d962008-09-03 21:17:21 +00006
7void p(char *str, int x) {
8 printf("%s: %d\n", str, x);
9}
10void q(char *str, double x) {
11 printf("%s: %f\n", str, x);
12}
John McCalldb7b72a2010-02-28 13:00:19 +000013void r(char *str, void *ptr) {
14 printf("%s: %p\n", str, ptr);
15}
16
17int random(void);
Daniel Dunbard417d962008-09-03 21:17:21 +000018
19int main() {
20 int N = random();
21#define P(n,args) p(#n #args, __builtin_##n args)
22#define Q(n,args) q(#n #args, __builtin_##n args)
John McCalldb7b72a2010-02-28 13:00:19 +000023#define R(n,args) r(#n #args, __builtin_##n args)
Daniel Dunbard417d962008-09-03 21:17:21 +000024#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));
29 V(prefetch, (&N));
30 V(prefetch, (&N, 1));
31 V(prefetch, (&N, 1, 0));
32
33 // Numeric Constants
34
35 Q(huge_val, ());
36 Q(huge_valf, ());
37 Q(huge_vall, ());
38 Q(inf, ());
39 Q(inff, ());
40 Q(infl, ());
41
Benjamin Kramera6cebab2010-06-14 10:41:45 +000042 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));
Daniel Dunbard417d962008-09-03 21:17:21 +000045 // FIXME:
Daniel Dunbard417d962008-09-03 21:17:21 +000046 // P(isinf_sign, (1.0));
47
Daniel Dunbar1768a622008-10-05 06:34:45 +000048 Q(nan, (""));
49 Q(nanf, (""));
50 Q(nanl, (""));
51 Q(nans, (""));
52 Q(nan, ("10"));
53 Q(nanf, ("10"));
54 Q(nanl, ("10"));
55 Q(nans, ("10"));
56
57 P(isgreater, (1., 2.));
58 P(isgreaterequal, (1., 2.));
59 P(isless, (1., 2.));
60 P(islessequal, (1., 2.));
61 P(islessgreater, (1., 2.));
62 P(isunordered, (1., 2.));
Daniel Dunbard417d962008-09-03 21:17:21 +000063
Eli Friedmand6139892009-09-01 04:19:44 +000064 P(isnan, (1.));
65
Daniel Dunbard417d962008-09-03 21:17:21 +000066 // Bitwise & Numeric Functions
67
Daniel Dunbar1768a622008-10-05 06:34:45 +000068 P(abs, (N));
69
Daniel Dunbard417d962008-09-03 21:17:21 +000070 P(clz, (N));
71 P(clzl, (N));
72 P(clzll, (N));
73 P(ctz, (N));
74 P(ctzl, (N));
75 P(ctzll, (N));
76 P(ffs, (N));
77 P(ffsl, (N));
78 P(ffsll, (N));
79 P(parity, (N));
80 P(parityl, (N));
81 P(parityll, (N));
82 P(popcount, (N));
83 P(popcountl, (N));
84 P(popcountll, (N));
85 Q(powi, (1.2f, N));
86 Q(powif, (1.2f, N));
87 Q(powil, (1.2f, N));
88
Daniel Dunbarf8ebb412009-02-20 06:36:40 +000089 // Lib functions
Daniel Dunbard417d962008-09-03 21:17:21 +000090 int a, b, n = random(); // Avoid optimizing out.
91 char s0[10], s1[] = "Hello";
Daniel Dunbarf8ebb412009-02-20 06:36:40 +000092 V(strcat, (s0, s1));
Daniel Dunbar7a0048b2009-04-09 16:42:50 +000093 V(strcmp, (s0, s1));
Daniel Dunbarf8ebb412009-02-20 06:36:40 +000094 V(strncat, (s0, s1, n));
95 V(strchr, (s0, s1[0]));
96 V(strrchr, (s0, s1[0]));
97 V(strcpy, (s0, s1));
98 V(strncpy, (s0, s1, n));
99
100 // Object size checking
Daniel Dunbard417d962008-09-03 21:17:21 +0000101 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));
107 s0[0] = 0;
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));
113
114 // Whatever
115
Benjamin Kramerd1900572012-10-06 14:42:22 +0000116 P(bswap16, (N));
Daniel Dunbard417d962008-09-03 21:17:21 +0000117 P(bswap32, (N));
118 P(bswap64, (N));
119 // FIXME
120 // V(clear_cache, (&N, &N+1));
121 V(trap, ());
John McCalldb7b72a2010-02-28 13:00:19 +0000122 R(extract_return_addr, (&N));
Eli Friedmanba68b082010-03-06 02:17:52 +0000123 P(signbit, (1.0));
Daniel Dunbard417d962008-09-03 21:17:21 +0000124
125 return 0;
126}
127
Chris Lattnerb808c952009-03-22 21:56:56 +0000128
129
Chris Lattnerb808c952009-03-22 21:56:56 +0000130void foo() {
131 __builtin_strcat(0, 0);
132}
133
Stephen Lin93ab6bf2013-08-15 06:47:53 +0000134// CHECK-LABEL: define void @bar(
John McCalldb7b72a2010-02-28 13:00:19 +0000135void bar() {
136 float f;
137 double d;
138 long double ld;
139
140 // LLVM's hex representation of float constants is really unfortunate;
141 // basically it does a float-to-double "conversion" and then prints the
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000142 // hex form of that. That gives us weird artifacts like exponents
John McCalldb7b72a2010-02-28 13:00:19 +0000143 // 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).
146 //
147 // 0xAE98 == 1010111010011000
148 // 0x15D3 == 1010111010011
149
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
165
166}
167// CHECK: }
Chris Lattner81368fb2010-05-06 05:50:07 +0000168
169
Stephen Lin93ab6bf2013-08-15 06:47:53 +0000170// CHECK-LABEL: define void @test_float_builtins
Chris Lattnered074152010-05-06 06:04:13 +0000171void test_float_builtins(float F, double D, long double LD) {
Chris Lattner81368fb2010-05-06 05:50:07 +0000172 volatile int res;
173 res = __builtin_isinf(F);
174 // CHECK: call float @fabsf(float
175 // CHECK: fcmp oeq float {{.*}}, 0x7FF0000000000000
176
177 res = __builtin_isinf(D);
178 // CHECK: call double @fabs(double
179 // CHECK: fcmp oeq double {{.*}}, 0x7FF0000000000000
180
181 res = __builtin_isinf(LD);
182 // CHECK: call x86_fp80 @fabsl(x86_fp80
183 // CHECK: fcmp oeq x86_fp80 {{.*}}, 0xK7FFF8000000000000000
Chris Lattnered074152010-05-06 06:04:13 +0000184
185 res = __builtin_isfinite(F);
186 // CHECK: fcmp oeq float
187 // CHECK: call float @fabsf
188 // CHECK: fcmp une float {{.*}}, 0x7FF0000000000000
189 // CHECK: and i1
Benjamin Kramer6349ce92010-05-19 11:24:26 +0000190
191 res = __builtin_isnormal(F);
192 // CHECK: fcmp oeq float
193 // CHECK: call float @fabsf
194 // CHECK: fcmp ult float {{.*}}, 0x7FF0000000000000
195 // CHECK: fcmp uge float {{.*}}, 0x3810000000000000
196 // CHECK: and i1
197 // CHECK: and i1
Chris Lattner81368fb2010-05-06 05:50:07 +0000198}
199
Stephen Lin93ab6bf2013-08-15 06:47:53 +0000200// CHECK-LABEL: define void @test_builtin_longjmp
John McCall78673d92010-05-27 18:47:06 +0000201void test_builtin_longjmp(void **buffer) {
202 // CHECK: [[BITCAST:%.*]] = bitcast
203 // CHECK-NEXT: call void @llvm.eh.sjlj.longjmp(i8* [[BITCAST]])
204 __builtin_longjmp(buffer, 1);
205 // CHECK-NEXT: unreachable
206}
Hal Finkela841c192012-08-05 22:03:08 +0000207
Stephen Lin93ab6bf2013-08-15 06:47:53 +0000208// CHECK-LABEL: define i64 @test_builtin_readcyclecounter
Hal Finkela841c192012-08-05 22:03:08 +0000209long long test_builtin_readcyclecounter() {
210 // CHECK: call i64 @llvm.readcyclecounter()
211 return __builtin_readcyclecounter();
212}