xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGen/builtins.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -emit-llvm -o %t %s
2f4a2713aSLionel Sambuc // RUN: not grep __builtin %t
3f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple | FileCheck %s
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc int printf(const char *, ...);
6f4a2713aSLionel Sambuc 
p(char * str,int x)7f4a2713aSLionel Sambuc void p(char *str, int x) {
8f4a2713aSLionel Sambuc   printf("%s: %d\n", str, x);
9f4a2713aSLionel Sambuc }
q(char * str,double x)10f4a2713aSLionel Sambuc void q(char *str, double x) {
11f4a2713aSLionel Sambuc   printf("%s: %f\n", str, x);
12f4a2713aSLionel Sambuc }
r(char * str,void * ptr)13f4a2713aSLionel Sambuc void r(char *str, void *ptr) {
14f4a2713aSLionel Sambuc   printf("%s: %p\n", str, ptr);
15f4a2713aSLionel Sambuc }
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc int random(void);
18f4a2713aSLionel Sambuc 
main()19f4a2713aSLionel Sambuc int main() {
20f4a2713aSLionel Sambuc   int N = random();
21f4a2713aSLionel Sambuc #define P(n,args) p(#n #args, __builtin_##n args)
22f4a2713aSLionel Sambuc #define Q(n,args) q(#n #args, __builtin_##n args)
23f4a2713aSLionel Sambuc #define R(n,args) r(#n #args, __builtin_##n args)
24f4a2713aSLionel Sambuc #define V(n,args) p(#n #args, (__builtin_##n args, 0))
25f4a2713aSLionel Sambuc   P(types_compatible_p, (int, float));
26f4a2713aSLionel Sambuc   P(choose_expr, (0, 10, 20));
27f4a2713aSLionel Sambuc   P(constant_p, (sizeof(10)));
28f4a2713aSLionel Sambuc   P(expect, (N == 12, 0));
29f4a2713aSLionel Sambuc   V(prefetch, (&N));
30f4a2713aSLionel Sambuc   V(prefetch, (&N, 1));
31f4a2713aSLionel Sambuc   V(prefetch, (&N, 1, 0));
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc   // Numeric Constants
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc   Q(huge_val, ());
36f4a2713aSLionel Sambuc   Q(huge_valf, ());
37f4a2713aSLionel Sambuc   Q(huge_vall, ());
38f4a2713aSLionel Sambuc   Q(inf, ());
39f4a2713aSLionel Sambuc   Q(inff, ());
40f4a2713aSLionel Sambuc   Q(infl, ());
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc   P(fpclassify, (0, 1, 2, 3, 4, 1.0));
43f4a2713aSLionel Sambuc   P(fpclassify, (0, 1, 2, 3, 4, 1.0f));
44f4a2713aSLionel Sambuc   P(fpclassify, (0, 1, 2, 3, 4, 1.0l));
45f4a2713aSLionel Sambuc   // FIXME:
46f4a2713aSLionel Sambuc   //  P(isinf_sign, (1.0));
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc   Q(nan, (""));
49f4a2713aSLionel Sambuc   Q(nanf, (""));
50f4a2713aSLionel Sambuc   Q(nanl, (""));
51f4a2713aSLionel Sambuc   Q(nans, (""));
52f4a2713aSLionel Sambuc   Q(nan, ("10"));
53f4a2713aSLionel Sambuc   Q(nanf, ("10"));
54f4a2713aSLionel Sambuc   Q(nanl, ("10"));
55f4a2713aSLionel Sambuc   Q(nans, ("10"));
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc   P(isgreater, (1., 2.));
58f4a2713aSLionel Sambuc   P(isgreaterequal, (1., 2.));
59f4a2713aSLionel Sambuc   P(isless, (1., 2.));
60f4a2713aSLionel Sambuc   P(islessequal, (1., 2.));
61f4a2713aSLionel Sambuc   P(islessgreater, (1., 2.));
62f4a2713aSLionel Sambuc   P(isunordered, (1., 2.));
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc   P(isnan, (1.));
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc   // Bitwise & Numeric Functions
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc   P(abs, (N));
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc   P(clz, (N));
71f4a2713aSLionel Sambuc   P(clzl, (N));
72f4a2713aSLionel Sambuc   P(clzll, (N));
73f4a2713aSLionel Sambuc   P(ctz, (N));
74f4a2713aSLionel Sambuc   P(ctzl, (N));
75f4a2713aSLionel Sambuc   P(ctzll, (N));
76f4a2713aSLionel Sambuc   P(ffs, (N));
77f4a2713aSLionel Sambuc   P(ffsl, (N));
78f4a2713aSLionel Sambuc   P(ffsll, (N));
79f4a2713aSLionel Sambuc   P(parity, (N));
80f4a2713aSLionel Sambuc   P(parityl, (N));
81f4a2713aSLionel Sambuc   P(parityll, (N));
82f4a2713aSLionel Sambuc   P(popcount, (N));
83f4a2713aSLionel Sambuc   P(popcountl, (N));
84f4a2713aSLionel Sambuc   P(popcountll, (N));
85f4a2713aSLionel Sambuc   Q(powi, (1.2f, N));
86f4a2713aSLionel Sambuc   Q(powif, (1.2f, N));
87f4a2713aSLionel Sambuc   Q(powil, (1.2f, N));
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc   // Lib functions
90f4a2713aSLionel Sambuc   int a, b, n = random(); // Avoid optimizing out.
91f4a2713aSLionel Sambuc   char s0[10], s1[] = "Hello";
92f4a2713aSLionel Sambuc   V(strcat, (s0, s1));
93f4a2713aSLionel Sambuc   V(strcmp, (s0, s1));
94f4a2713aSLionel Sambuc   V(strncat, (s0, s1, n));
95f4a2713aSLionel Sambuc   V(strchr, (s0, s1[0]));
96f4a2713aSLionel Sambuc   V(strrchr, (s0, s1[0]));
97f4a2713aSLionel Sambuc   V(strcpy, (s0, s1));
98f4a2713aSLionel Sambuc   V(strncpy, (s0, s1, n));
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc   // Object size checking
101f4a2713aSLionel Sambuc   V(__memset_chk, (s0, 0, sizeof s0, n));
102f4a2713aSLionel Sambuc   V(__memcpy_chk, (s0, s1, sizeof s0, n));
103f4a2713aSLionel Sambuc   V(__memmove_chk, (s0, s1, sizeof s0, n));
104f4a2713aSLionel Sambuc   V(__mempcpy_chk, (s0, s1, sizeof s0, n));
105f4a2713aSLionel Sambuc   V(__strncpy_chk, (s0, s1, sizeof s0, n));
106f4a2713aSLionel Sambuc   V(__strcpy_chk, (s0, s1, n));
107f4a2713aSLionel Sambuc   s0[0] = 0;
108f4a2713aSLionel Sambuc   V(__strcat_chk, (s0, s1, n));
109f4a2713aSLionel Sambuc   P(object_size, (s0, 0));
110f4a2713aSLionel Sambuc   P(object_size, (s0, 1));
111f4a2713aSLionel Sambuc   P(object_size, (s0, 2));
112f4a2713aSLionel Sambuc   P(object_size, (s0, 3));
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   // Whatever
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc   P(bswap16, (N));
117f4a2713aSLionel Sambuc   P(bswap32, (N));
118f4a2713aSLionel Sambuc   P(bswap64, (N));
119f4a2713aSLionel Sambuc   // FIXME
120f4a2713aSLionel Sambuc   // V(clear_cache, (&N, &N+1));
121f4a2713aSLionel Sambuc   V(trap, ());
122f4a2713aSLionel Sambuc   R(extract_return_addr, (&N));
123f4a2713aSLionel Sambuc   P(signbit, (1.0));
124f4a2713aSLionel Sambuc 
125f4a2713aSLionel Sambuc   return 0;
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc 
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc 
foo()130f4a2713aSLionel Sambuc void foo() {
131f4a2713aSLionel Sambuc  __builtin_strcat(0, 0);
132f4a2713aSLionel Sambuc }
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc // CHECK-LABEL: define void @bar(
bar()135f4a2713aSLionel Sambuc void bar() {
136f4a2713aSLionel Sambuc   float f;
137f4a2713aSLionel Sambuc   double d;
138f4a2713aSLionel Sambuc   long double ld;
139f4a2713aSLionel Sambuc 
140f4a2713aSLionel Sambuc   // LLVM's hex representation of float constants is really unfortunate;
141f4a2713aSLionel Sambuc   // basically it does a float-to-double "conversion" and then prints the
142f4a2713aSLionel Sambuc   // hex form of that.  That gives us weird artifacts like exponents
143f4a2713aSLionel Sambuc   // that aren't numerically similar to the original exponent and
144f4a2713aSLionel Sambuc   // significand bit-patterns that are offset by three bits (because
145f4a2713aSLionel Sambuc   // the exponent was expanded from 8 bits to 11).
146f4a2713aSLionel Sambuc   //
147f4a2713aSLionel Sambuc   // 0xAE98 == 1010111010011000
148f4a2713aSLionel Sambuc   // 0x15D3 == 1010111010011
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc   f = __builtin_huge_valf();     // CHECK: float    0x7FF0000000000000
151f4a2713aSLionel Sambuc   d = __builtin_huge_val();      // CHECK: double   0x7FF0000000000000
152f4a2713aSLionel Sambuc   ld = __builtin_huge_vall();    // CHECK: x86_fp80 0xK7FFF8000000000000000
153f4a2713aSLionel Sambuc   f = __builtin_nanf("");        // CHECK: float    0x7FF8000000000000
154f4a2713aSLionel Sambuc   d = __builtin_nan("");         // CHECK: double   0x7FF8000000000000
155f4a2713aSLionel Sambuc   ld = __builtin_nanl("");       // CHECK: x86_fp80 0xK7FFFC000000000000000
156f4a2713aSLionel Sambuc   f = __builtin_nanf("0xAE98");  // CHECK: float    0x7FF815D300000000
157f4a2713aSLionel Sambuc   d = __builtin_nan("0xAE98");   // CHECK: double   0x7FF800000000AE98
158f4a2713aSLionel Sambuc   ld = __builtin_nanl("0xAE98"); // CHECK: x86_fp80 0xK7FFFC00000000000AE98
159f4a2713aSLionel Sambuc   f = __builtin_nansf("");       // CHECK: float    0x7FF4000000000000
160f4a2713aSLionel Sambuc   d = __builtin_nans("");        // CHECK: double   0x7FF4000000000000
161f4a2713aSLionel Sambuc   ld = __builtin_nansl("");      // CHECK: x86_fp80 0xK7FFFA000000000000000
162f4a2713aSLionel Sambuc   f = __builtin_nansf("0xAE98"); // CHECK: float    0x7FF015D300000000
163f4a2713aSLionel Sambuc   d = __builtin_nans("0xAE98");  // CHECK: double   0x7FF000000000AE98
164f4a2713aSLionel Sambuc   ld = __builtin_nansl("0xAE98");// CHECK: x86_fp80 0xK7FFF800000000000AE98
165f4a2713aSLionel Sambuc 
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc // CHECK: }
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc // CHECK-LABEL: define void @test_float_builtins
test_float_builtins(float F,double D,long double LD)171f4a2713aSLionel Sambuc void test_float_builtins(float F, double D, long double LD) {
172f4a2713aSLionel Sambuc   volatile int res;
173f4a2713aSLionel Sambuc   res = __builtin_isinf(F);
174*0a6a1f1dSLionel Sambuc   // CHECK:  call float @llvm.fabs.f32(float
175f4a2713aSLionel Sambuc   // CHECK:  fcmp oeq float {{.*}}, 0x7FF0000000000000
176f4a2713aSLionel Sambuc 
177f4a2713aSLionel Sambuc   res = __builtin_isinf(D);
178*0a6a1f1dSLionel Sambuc   // CHECK:  call double @llvm.fabs.f64(double
179f4a2713aSLionel Sambuc   // CHECK:  fcmp oeq double {{.*}}, 0x7FF0000000000000
180f4a2713aSLionel Sambuc 
181f4a2713aSLionel Sambuc   res = __builtin_isinf(LD);
182*0a6a1f1dSLionel Sambuc   // CHECK:  call x86_fp80 @llvm.fabs.f80(x86_fp80
183f4a2713aSLionel Sambuc   // CHECK:  fcmp oeq x86_fp80 {{.*}}, 0xK7FFF8000000000000000
184f4a2713aSLionel Sambuc 
185f4a2713aSLionel Sambuc   res = __builtin_isfinite(F);
186f4a2713aSLionel Sambuc   // CHECK: fcmp oeq float
187*0a6a1f1dSLionel Sambuc   // CHECK: call float @llvm.fabs.f32(float
188f4a2713aSLionel Sambuc   // CHECK: fcmp une float {{.*}}, 0x7FF0000000000000
189f4a2713aSLionel Sambuc   // CHECK: and i1
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc   res = __builtin_isnormal(F);
192f4a2713aSLionel Sambuc   // CHECK: fcmp oeq float
193*0a6a1f1dSLionel Sambuc   // CHECK: call float @llvm.fabs.f32(float
194f4a2713aSLionel Sambuc   // CHECK: fcmp ult float {{.*}}, 0x7FF0000000000000
195f4a2713aSLionel Sambuc   // CHECK: fcmp uge float {{.*}}, 0x3810000000000000
196f4a2713aSLionel Sambuc   // CHECK: and i1
197f4a2713aSLionel Sambuc   // CHECK: and i1
198f4a2713aSLionel Sambuc }
199f4a2713aSLionel Sambuc 
200*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define void @test_float_builtin_ops
test_float_builtin_ops(float F,double D,long double LD)201*0a6a1f1dSLionel Sambuc void test_float_builtin_ops(float F, double D, long double LD) {
202*0a6a1f1dSLionel Sambuc   volatile float resf;
203*0a6a1f1dSLionel Sambuc   volatile double resd;
204*0a6a1f1dSLionel Sambuc   volatile long double resld;
205*0a6a1f1dSLionel Sambuc 
206*0a6a1f1dSLionel Sambuc   resf = __builtin_fmodf(F,F);
207*0a6a1f1dSLionel Sambuc   // CHECK: frem float
208*0a6a1f1dSLionel Sambuc 
209*0a6a1f1dSLionel Sambuc   resd = __builtin_fmod(D,D);
210*0a6a1f1dSLionel Sambuc   // CHECK: frem double
211*0a6a1f1dSLionel Sambuc 
212*0a6a1f1dSLionel Sambuc   resld = __builtin_fmodl(LD,LD);
213*0a6a1f1dSLionel Sambuc   // CHECK: frem x86_fp80
214*0a6a1f1dSLionel Sambuc 
215*0a6a1f1dSLionel Sambuc   resf = __builtin_fabsf(F);
216*0a6a1f1dSLionel Sambuc   resd = __builtin_fabs(D);
217*0a6a1f1dSLionel Sambuc   resld = __builtin_fabsl(LD);
218*0a6a1f1dSLionel Sambuc   // CHECK: call float @llvm.fabs.f32(float
219*0a6a1f1dSLionel Sambuc   // CHECK: call double @llvm.fabs.f64(double
220*0a6a1f1dSLionel Sambuc   // CHECK: call x86_fp80 @llvm.fabs.f80(x86_fp80
221*0a6a1f1dSLionel Sambuc }
222*0a6a1f1dSLionel Sambuc 
223*0a6a1f1dSLionel Sambuc // __builtin_longjmp isn't supported on all platforms, so only test it on X86.
224*0a6a1f1dSLionel Sambuc #ifdef __x86_64__
225f4a2713aSLionel Sambuc // CHECK-LABEL: define void @test_builtin_longjmp
test_builtin_longjmp(void ** buffer)226f4a2713aSLionel Sambuc void test_builtin_longjmp(void **buffer) {
227f4a2713aSLionel Sambuc   // CHECK: [[BITCAST:%.*]] = bitcast
228f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @llvm.eh.sjlj.longjmp(i8* [[BITCAST]])
229f4a2713aSLionel Sambuc   __builtin_longjmp(buffer, 1);
230f4a2713aSLionel Sambuc   // CHECK-NEXT: unreachable
231f4a2713aSLionel Sambuc }
232*0a6a1f1dSLionel Sambuc #endif
233f4a2713aSLionel Sambuc 
234f4a2713aSLionel Sambuc // CHECK-LABEL: define i64 @test_builtin_readcyclecounter
test_builtin_readcyclecounter()235f4a2713aSLionel Sambuc long long test_builtin_readcyclecounter() {
236f4a2713aSLionel Sambuc   // CHECK: call i64 @llvm.readcyclecounter()
237f4a2713aSLionel Sambuc   return __builtin_readcyclecounter();
238f4a2713aSLionel Sambuc }
239