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