Home | History | Annotate | Download | only in CodeGen
      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
      4 
      5 int printf(const char *, ...);
      6 
      7 void p(char *str, int x) {
      8   printf("%s: %d\n", str, x);
      9 }
     10 void q(char *str, double x) {
     11   printf("%s: %f\n", str, x);
     12 }
     13 void r(char *str, void *ptr) {
     14   printf("%s: %p\n", str, ptr);
     15 }
     16 
     17 int random(void);
     18 
     19 int 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)
     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));
     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 
     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));
     45   // FIXME:
     46   //  P(isinf_sign, (1.0));
     47 
     48   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.));
     63 
     64   P(isnan, (1.));
     65 
     66   // Bitwise & Numeric Functions
     67 
     68   P(abs, (N));
     69 
     70   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 
     89   // Lib functions
     90   int a, b, n = random(); // Avoid optimizing out.
     91   char s0[10], s1[] = "Hello";
     92   V(strcat, (s0, s1));
     93   V(strcmp, (s0, s1));
     94   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
    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));
    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 
    116   P(bswap32, (N));
    117   P(bswap64, (N));
    118   // FIXME
    119   // V(clear_cache, (&N, &N+1));
    120   V(trap, ());
    121   R(extract_return_addr, (&N));
    122   P(signbit, (1.0));
    123 
    124   return 0;
    125 }
    126 
    127 
    128 
    129 void foo() {
    130  __builtin_strcat(0, 0);
    131 }
    132 
    133 // CHECK: define void @bar(
    134 void bar() {
    135   float f;
    136   double d;
    137   long double ld;
    138 
    139   // LLVM's hex representation of float constants is really unfortunate;
    140   // basically it does a float-to-double "conversion" and then prints the
    141   // hex form of that.  That gives us weird artifacts like exponents
    142   // that aren't numerically similar to the original exponent and
    143   // significand bit-patterns that are offset by three bits (because
    144   // the exponent was expanded from 8 bits to 11).
    145   //
    146   // 0xAE98 == 1010111010011000
    147   // 0x15D3 == 1010111010011
    148 
    149   f = __builtin_huge_valf();     // CHECK: float    0x7FF0000000000000
    150   d = __builtin_huge_val();      // CHECK: double   0x7FF0000000000000
    151   ld = __builtin_huge_vall();    // CHECK: x86_fp80 0xK7FFF8000000000000000
    152   f = __builtin_nanf("");        // CHECK: float    0x7FF8000000000000
    153   d = __builtin_nan("");         // CHECK: double   0x7FF8000000000000
    154   ld = __builtin_nanl("");       // CHECK: x86_fp80 0xK7FFFC000000000000000
    155   f = __builtin_nanf("0xAE98");  // CHECK: float    0x7FF815D300000000
    156   d = __builtin_nan("0xAE98");   // CHECK: double   0x7FF800000000AE98
    157   ld = __builtin_nanl("0xAE98"); // CHECK: x86_fp80 0xK7FFFC00000000000AE98
    158   f = __builtin_nansf("");       // CHECK: float    0x7FF4000000000000
    159   d = __builtin_nans("");        // CHECK: double   0x7FF4000000000000
    160   ld = __builtin_nansl("");      // CHECK: x86_fp80 0xK7FFFA000000000000000
    161   f = __builtin_nansf("0xAE98"); // CHECK: float    0x7FF015D300000000
    162   d = __builtin_nans("0xAE98");  // CHECK: double   0x7FF000000000AE98
    163   ld = __builtin_nansl("0xAE98");// CHECK: x86_fp80 0xK7FFF800000000000AE98
    164 
    165 }
    166 // CHECK: }
    167 
    168 
    169 // CHECK: define void @test_float_builtins
    170 void test_float_builtins(float F, double D, long double LD) {
    171   volatile int res;
    172   res = __builtin_isinf(F);
    173   // CHECK:  call float @fabsf(float
    174   // CHECK:  fcmp oeq float {{.*}}, 0x7FF0000000000000
    175 
    176   res = __builtin_isinf(D);
    177   // CHECK:  call double @fabs(double
    178   // CHECK:  fcmp oeq double {{.*}}, 0x7FF0000000000000
    179 
    180   res = __builtin_isinf(LD);
    181   // CHECK:  call x86_fp80 @fabsl(x86_fp80
    182   // CHECK:  fcmp oeq x86_fp80 {{.*}}, 0xK7FFF8000000000000000
    183 
    184   res = __builtin_isfinite(F);
    185   // CHECK: fcmp oeq float
    186   // CHECK: call float @fabsf
    187   // CHECK: fcmp une float {{.*}}, 0x7FF0000000000000
    188   // CHECK: and i1
    189 
    190   res = __builtin_isnormal(F);
    191   // CHECK: fcmp oeq float
    192   // CHECK: call float @fabsf
    193   // CHECK: fcmp ult float {{.*}}, 0x7FF0000000000000
    194   // CHECK: fcmp uge float {{.*}}, 0x3810000000000000
    195   // CHECK: and i1
    196   // CHECK: and i1
    197 }
    198 
    199 // CHECK: define void @test_builtin_longjmp
    200 void test_builtin_longjmp(void **buffer) {
    201   // CHECK: [[BITCAST:%.*]] = bitcast
    202   // CHECK-NEXT: call void @llvm.eh.sjlj.longjmp(i8* [[BITCAST]])
    203   __builtin_longjmp(buffer, 1);
    204   // CHECK-NEXT: unreachable
    205 }
    206