From: Janis Johnson Date: Tue, 4 Dec 2001 00:56:33 +0000 (+0000) Subject: New test for __builtin_prefetch. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=be18831b8d07371579b37a8468f58a38e3dee25f;p=gcc.git New test for __builtin_prefetch. From-SVN: r47585 --- diff --git a/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-2.c b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-2.c new file mode 100644 index 00000000000..530a1b0ef0d --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-2.c @@ -0,0 +1,152 @@ +/* Test that __builtin_prefetch does no harm. + + Prefetch data using a variety of storage classes and address + expressions. */ + +int glob_int_arr[100]; +int *glob_ptr_int = glob_int_arr; +int glob_int = 4; + +static stat_int_arr[100]; +static int *stat_ptr_int = stat_int_arr; +static int stat_int; + +struct S { + int a; + short b, c; + char d[8]; + struct S *next; +}; + +struct S str; +struct S *ptr_str = &str; + +/* Prefetch global variables using the address of the variable. */ + +void +simple_global () +{ + __builtin_prefetch (glob_int_arr, 0, 0); + __builtin_prefetch (glob_ptr_int, 0, 0); + __builtin_prefetch (&glob_int, 0, 0); +} + +/* Prefetch file-level static variables using the address of the variable. */ + +void +simple_file () +{ + __builtin_prefetch (stat_int_arr, 0, 0); + __builtin_prefetch (stat_ptr_int, 0, 0); + __builtin_prefetch (&stat_int, 0, 0); +} + +/* Prefetch local static variables using the address of the variable. */ + +void +simple_static_local () +{ + static int gx[100]; + static int *hx = gx; + static int ix; + __builtin_prefetch (gx, 0, 0); + __builtin_prefetch (hx, 0, 0); + __builtin_prefetch (&ix, 0, 0); +} + +/* Prefetch local stack variables using the address of the variable. */ + +void +simple_local () +{ + int gx[100]; + int *hx = gx; + int ix; + __builtin_prefetch (gx, 0, 0); + __builtin_prefetch (hx, 0, 0); + __builtin_prefetch (&ix, 0, 0); +} + +/* Prefetch arguments using the address of the variable. */ + +void +simple_arg (int g[100], int *h, int i) +{ + __builtin_prefetch (g, 0, 0); + __builtin_prefetch (h, 0, 0); + __builtin_prefetch (&i, 0, 0); +} + +/* Prefetch using address expressions involving global variables. */ + +void +expr_global (void) +{ + __builtin_prefetch (&str, 0, 0); + __builtin_prefetch (ptr_str, 0, 0); + __builtin_prefetch (&str.b, 0, 0); + __builtin_prefetch (&ptr_str->b, 0, 0); + __builtin_prefetch (&str.d, 0, 0); + __builtin_prefetch (&ptr_str->d, 0, 0); + __builtin_prefetch (str.next, 0, 0); + __builtin_prefetch (ptr_str->next, 0, 0); + __builtin_prefetch (str.next->d, 0, 0); + __builtin_prefetch (ptr_str->next->d, 0, 0); + + __builtin_prefetch (&glob_int_arr, 0, 0); + __builtin_prefetch (glob_ptr_int, 0, 0); + __builtin_prefetch (&glob_int_arr[2], 0, 0); + __builtin_prefetch (&glob_ptr_int[3], 0, 0); + __builtin_prefetch (glob_int_arr+3, 0, 0); + __builtin_prefetch (glob_int_arr+glob_int, 0, 0); + __builtin_prefetch (glob_ptr_int+5, 0, 0); + __builtin_prefetch (glob_ptr_int+glob_int, 0, 0); +} + +/* Prefetch using address expressions involving local variables. */ + +void +expr_local (void) +{ + int b[10]; + int *pb = b; + struct S t; + struct S *pt = &t; + int j = 4; + + __builtin_prefetch (&t, 0, 0); + __builtin_prefetch (pt, 0, 0); + __builtin_prefetch (&t.b, 0, 0); + __builtin_prefetch (&pt->b, 0, 0); + __builtin_prefetch (&t.d, 0, 0); + __builtin_prefetch (&pt->d, 0, 0); + __builtin_prefetch (t.next, 0, 0); + __builtin_prefetch (pt->next, 0, 0); + __builtin_prefetch (t.next->d, 0, 0); + __builtin_prefetch (pt->next->d, 0, 0); + + __builtin_prefetch (&b, 0, 0); + __builtin_prefetch (pb, 0, 0); + __builtin_prefetch (&b[2], 0, 0); + __builtin_prefetch (&pb[3], 0, 0); + __builtin_prefetch (b+3, 0, 0); + __builtin_prefetch (b+j, 0, 0); + __builtin_prefetch (pb+5, 0, 0); + __builtin_prefetch (pb+j, 0, 0); +} + +int +main () +{ + simple_global (); + simple_file (); + simple_static_local (); + simple_local (); + simple_arg (glob_int_arr, glob_ptr_int, glob_int); + + str.next = &str; + expr_global (); + expr_local (); + + exit (0); +} diff --git a/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-3.c b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-3.c new file mode 100644 index 00000000000..2e2e808c172 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-3.c @@ -0,0 +1,113 @@ +/* Test that __builtin_prefetch does no harm. + + Prefetch data using a variety of storage classes and address + expressions with volatile variables and pointers. */ + +int glob_int_arr[100]; +int glob_int = 4; +volatile int glob_vol_int_arr[100]; +int * volatile glob_vol_ptr_int = glob_int_arr; +volatile int *glob_ptr_vol_int = glob_vol_int_arr; +volatile int * volatile glob_vol_ptr_vol_int = glob_vol_int_arr; +volatile int glob_vol_int; + +static stat_int_arr[100]; +static volatile int stat_vol_int_arr[100]; +static int * volatile stat_vol_ptr_int = stat_int_arr; +static volatile int *stat_ptr_vol_int = stat_vol_int_arr; +static volatile int * volatile stat_vol_ptr_vol_int = stat_vol_int_arr; +static volatile int stat_vol_int; + +struct S { + int a; + short b, c; + char d[8]; + struct S *next; +}; + +struct S str; +volatile struct S vol_str; +struct S * volatile vol_ptr_str = &str; +volatile struct S *ptr_vol_str = &vol_str; +volatile struct S * volatile vol_ptr_vol_str = &vol_str; + +/* Prefetch volatile global variables using the address of the variable. */ + +void +simple_vol_global () +{ + __builtin_prefetch (glob_vol_int_arr, 0, 0); + __builtin_prefetch (glob_vol_ptr_int, 0, 0); + __builtin_prefetch (glob_ptr_vol_int, 0, 0); + __builtin_prefetch (glob_vol_ptr_vol_int, 0, 0); + __builtin_prefetch (&glob_vol_int, 0, 0); +} + +/* Prefetch volatile static variables using the address of the variable. */ + +void +simple_vol_file () +{ + __builtin_prefetch (stat_vol_int_arr, 0, 0); + __builtin_prefetch (stat_vol_ptr_int, 0, 0); + __builtin_prefetch (stat_ptr_vol_int, 0, 0); + __builtin_prefetch (stat_vol_ptr_vol_int, 0, 0); + __builtin_prefetch (&stat_vol_int, 0, 0); +} + +/* Prefetch using address expressions involving volatile global variables. */ + +void +expr_vol_global (void) +{ + __builtin_prefetch (&vol_str, 0, 0); + __builtin_prefetch (ptr_vol_str, 0, 0); + __builtin_prefetch (vol_ptr_str, 0, 0); + __builtin_prefetch (vol_ptr_vol_str, 0, 0); + __builtin_prefetch (&vol_str.b, 0, 0); + __builtin_prefetch (&ptr_vol_str->b, 0, 0); + __builtin_prefetch (&vol_ptr_str->b, 0, 0); + __builtin_prefetch (&vol_ptr_vol_str->b, 0, 0); + __builtin_prefetch (&vol_str.d, 0, 0); + __builtin_prefetch (&vol_ptr_str->d, 0, 0); + __builtin_prefetch (&ptr_vol_str->d, 0, 0); + __builtin_prefetch (&vol_ptr_vol_str->d, 0, 0); + __builtin_prefetch (vol_str.next, 0, 0); + __builtin_prefetch (vol_ptr_str->next, 0, 0); + __builtin_prefetch (ptr_vol_str->next, 0, 0); + __builtin_prefetch (vol_ptr_vol_str->next, 0, 0); + __builtin_prefetch (vol_str.next->d, 0, 0); + __builtin_prefetch (vol_ptr_str->next->d, 0, 0); + __builtin_prefetch (ptr_vol_str->next->d, 0, 0); + __builtin_prefetch (vol_ptr_vol_str->next->d, 0, 0); + + __builtin_prefetch (&glob_vol_int_arr, 0, 0); + __builtin_prefetch (glob_vol_ptr_int, 0, 0); + __builtin_prefetch (glob_ptr_vol_int, 0, 0); + __builtin_prefetch (glob_vol_ptr_vol_int, 0, 0); + __builtin_prefetch (&glob_vol_int_arr[2], 0, 0); + __builtin_prefetch (&glob_vol_ptr_int[3], 0, 0); + __builtin_prefetch (&glob_ptr_vol_int[3], 0, 0); + __builtin_prefetch (&glob_vol_ptr_vol_int[3], 0, 0); + __builtin_prefetch (glob_vol_int_arr+3, 0, 0); + __builtin_prefetch (glob_vol_int_arr+glob_vol_int, 0, 0); + __builtin_prefetch (glob_vol_ptr_int+5, 0, 0); + __builtin_prefetch (glob_ptr_vol_int+5, 0, 0); + __builtin_prefetch (glob_vol_ptr_vol_int+5, 0, 0); + __builtin_prefetch (glob_vol_ptr_int+glob_vol_int, 0, 0); + __builtin_prefetch (glob_ptr_vol_int+glob_vol_int, 0, 0); + __builtin_prefetch (glob_vol_ptr_vol_int+glob_vol_int, 0, 0); +} + +int +main () +{ + simple_vol_global (); + simple_vol_file (); + + str.next = &str; + vol_str.next = &str; + expr_vol_global (); + + exit (0); +} diff --git a/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-4.c b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-4.c new file mode 100644 index 00000000000..ade892b21a7 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-4.c @@ -0,0 +1,271 @@ +/* Test that __builtin_prefetch does no harm. + + Check that the expression containing the address to prefetch is + evaluated if it has side effects, even if the target does not support + data prefetch. Check changes to pointers and to array indices that are + either global variables or arguments. */ + +#define ARRSIZE 100 + +int arr[ARRSIZE]; +int *ptr = &arr[20]; +int arrindex = 4; + +/* Check that assignment within a prefetch argument is evaluated. */ + +int +assign_arg_ptr (int *p) +{ + int *q; + __builtin_prefetch ((q = p), 0, 0); + return q == p; +} + +int +assign_glob_ptr (void) +{ + int *q; + __builtin_prefetch ((q = ptr), 0, 0); + return q == ptr; +} + +int +assign_arg_idx (int *p, int i) +{ + int j; + __builtin_prefetch (&p[j = i], 0, 0); + return j == i; +} + +int +assign_glob_idx (void) +{ + int j; + __builtin_prefetch (&ptr[j = arrindex], 0, 0); + return j == arrindex; +} + +/* Check that pre/post increment/decrement within a prefetch argument are + evaluated. */ + +int +preinc_arg_ptr (int *p) +{ + int *q; + q = p + 1; + __builtin_prefetch (++p, 0, 0); + return p == q; +} + +int +preinc_glob_ptr (void) +{ + int *q; + q = ptr + 1; + __builtin_prefetch (++ptr, 0, 0); + return ptr == q; +} + +int +postinc_arg_ptr (int *p) +{ + int *q; + q = p + 1; + __builtin_prefetch (p++, 0, 0); + return p == q; +} + +int +postinc_glob_ptr (void) +{ + int *q; + q = ptr + 1; + __builtin_prefetch (ptr++, 0, 0); + return ptr == q; +} + +int +predec_arg_ptr (int *p) +{ + int *q; + q = p - 1; + __builtin_prefetch (--p, 0, 0); + return p == q; +} + +int +predec_glob_ptr (void) +{ + int *q; + q = ptr - 1; + __builtin_prefetch (--ptr, 0, 0); + return ptr == q; +} + +int +postdec_arg_ptr (int *p) +{ + int *q; + q = p - 1; + __builtin_prefetch (p--, 0, 0); + return p == q; +} + +int +postdec_glob_ptr (void) +{ + int *q; + q = ptr - 1; + __builtin_prefetch (ptr--, 0, 0); + return ptr == q; +} + +int +preinc_arg_idx (int *p, int i) +{ + int j = i + 1; + __builtin_prefetch (&p[++i], 0, 0); + return i == j; +} + + +int +preinc_glob_idx (void) +{ + int j = arrindex + 1; + __builtin_prefetch (&ptr[++arrindex], 0, 0); + return arrindex == j; +} + +int +postinc_arg_idx (int *p, int i) +{ + int j = i + 1; + __builtin_prefetch (&p[i++], 0, 0); + return i == j; +} + +int +postinc_glob_idx (void) +{ + int j = arrindex + 1; + __builtin_prefetch (&ptr[arrindex++], 0, 0); + return arrindex == j; +} + +int +predec_arg_idx (int *p, int i) +{ + int j = i - 1; + __builtin_prefetch (&p[--i], 0, 0); + return i == j; +} + +int +predec_glob_idx (void) +{ + int j = arrindex - 1; + __builtin_prefetch (&ptr[--arrindex], 0, 0); + return arrindex == j; +} + +int +postdec_arg_idx (int *p, int i) +{ + int j = i - 1; + __builtin_prefetch (&p[i--], 0, 0); + return i == j; +} + +int +postdec_glob_idx (void) +{ + int j = arrindex - 1; + __builtin_prefetch (&ptr[arrindex--], 0, 0); + return arrindex == j; +} + +/* Check that function calls within the first prefetch argument are + evaluated. */ + +int getptrcnt = 0; + +int * +getptr (int *p) +{ + getptrcnt++; + return p + 1; +} + +int +funccall_arg_ptr (int *p) +{ + __builtin_prefetch (getptr (p), 0, 0); + return getptrcnt == 1; +} + +int getintcnt = 0; + +int +getint (int i) +{ + getintcnt++; + return i + 1; +} + +int +funccall_arg_idx (int *p, int i) +{ + __builtin_prefetch (&p[getint (i)], 0, 0); + return getintcnt == 1; +} + +int +main () +{ + if (!assign_arg_ptr (ptr)) + abort (); + if (!assign_glob_ptr ()) + abort (); + if (!assign_arg_idx (ptr, 4)) + abort (); + if (!assign_glob_idx ()) + abort (); + if (!preinc_arg_ptr (ptr)) + abort (); + if (!preinc_glob_ptr ()) + abort (); + if (!postinc_arg_ptr (ptr)) + abort (); + if (!postinc_glob_ptr ()) + abort (); + if (!predec_arg_ptr (ptr)) + abort (); + if (!predec_glob_ptr ()) + abort (); + if (!postdec_arg_ptr (ptr)) + abort (); + if (!postdec_glob_ptr ()) + abort (); + if (!preinc_arg_idx (ptr, 3)) + abort (); + if (!preinc_glob_idx ()) + abort (); + if (!postinc_arg_idx (ptr, 3)) + abort (); + if (!postinc_glob_idx ()) + abort (); + if (!predec_arg_idx (ptr, 3)) + abort (); + if (!predec_glob_idx ()) + abort (); + if (!postdec_arg_idx (ptr, 3)) + abort (); + if (!postdec_glob_idx ()) + abort (); + if (!funccall_arg_ptr (ptr)) + abort (); + if (!funccall_arg_idx (ptr, 3)) + abort (); + exit (0); +} diff --git a/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-5.c b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-5.c new file mode 100644 index 00000000000..f42a2c0ca87 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-5.c @@ -0,0 +1,60 @@ +/* Test that __builtin_prefetch does no harm. + + Use addresses that are unlikely to be word-aligned. Some targets + have alignment requirements for prefetch addresses, so make sure the + compiler takes care of that. This fails if it aborts, anything else + is OK. */ + +struct S { + short a; + short b; + char c[8]; +} s; + +char arr[100]; +char *ptr = arr; +int idx = 3; + +void +arg_ptr (char *p) +{ + __builtin_prefetch (p, 0, 0); +} + +void +arg_idx (char *p, int i) +{ + __builtin_prefetch (&p[i], 0, 0); +} + +void +glob_ptr (void) +{ + __builtin_prefetch (ptr, 0, 0); +} + +void +glob_idx (void) +{ + __builtin_prefetch (&ptr[idx], 0, 0); +} + +int +main () +{ + __builtin_prefetch (&s.b, 0, 0); + __builtin_prefetch (&s.c[1], 0, 0); + + arg_ptr (&s.c[1]); + arg_ptr (ptr+3); + arg_idx (ptr, 3); + arg_idx (ptr+1, 2); + idx = 3; + glob_ptr (); + glob_idx (); + ptr++; + idx = 2; + glob_ptr (); + glob_idx (); + exit (0); +} diff --git a/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-6.c b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-6.c new file mode 100644 index 00000000000..f643c5c7286 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/builtin-prefetch-6.c @@ -0,0 +1,46 @@ +/* Test that __builtin_prefetch does no harm. + + Data prefetch should not fault if used with an invalid address. */ + +#include + +#define ARRSIZE 65 +int *bad_addr[ARRSIZE]; +int arr_used; + +/* Fill bad_addr with a range of values in the hopes that on any target + some will be invalid addresses. */ +void +init_addrs (void) +{ + int i; + int bits_per_ptr = sizeof (void *) * 8; + for (i = 0; i < bits_per_ptr; i++) + bad_addr[i] = (void *)(1UL << i); + arr_used = bits_per_ptr + 1; /* The last element used is zero. */ +} + +void +prefetch_for_read (void) +{ + int i; + for (i = 0; i < ARRSIZE; i++) + __builtin_prefetch (bad_addr[i], 0, 0); +} + +void +prefetch_for_write (void) +{ + int i; + for (i = 0; i < ARRSIZE; i++) + __builtin_prefetch (bad_addr[i], 1, 0); +} + +int +main () +{ + init_addrs (); + prefetch_for_read (); + prefetch_for_write (); + exit (0); +} diff --git a/gcc/testsuite/gcc.dg/builtin-prefetch-1.c b/gcc/testsuite/gcc.dg/builtin-prefetch-1.c new file mode 100644 index 00000000000..d547cfcd9e2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-prefetch-1.c @@ -0,0 +1,43 @@ +/* Test that __builtin_prefetch does no harm. + + Prefetch using some invalid rw and locality values. These must be + compile-time constants. */ + +/* { dg-do run } */ + +enum locality { none, low, moderate, high, bogus }; +enum rw { read, write }; + +int arr[10]; + +void +good (int *p) +{ + __builtin_prefetch (p, 0, 0); + __builtin_prefetch (p, 0, 1); + __builtin_prefetch (p, 0, 2); + __builtin_prefetch (p, 0, 3); + __builtin_prefetch (p, 1, 0); + __builtin_prefetch (p, 1, 1); + __builtin_prefetch (p, 1, 2); + __builtin_prefetch (p, 1, 3); +} + +void +bad (int *p) +{ + __builtin_prefetch (p, -1, 0); /* { dg-warning "invalid second arg to __builtin_prefetch; using zero" } */ + __builtin_prefetch (p, 2, 0); /* { dg-warning "invalid second arg to __builtin_prefetch; using zero" } */ + __builtin_prefetch (p, bogus, 0); /* { dg-warning "invalid second arg to __builtin_prefetch; using zero" } */ + __builtin_prefetch (p, 0, -1); /* { dg-warning "invalid third arg to __builtin_prefetch; using zero" } */ + __builtin_prefetch (p, 0, 4); /* { dg-warning "invalid third arg to __builtin_prefetch; using zero" } */ + __builtin_prefetch (p, 0, bogus); /* { dg-warning "invalid third arg to __builtin_prefetch; using zero" } */ +} + +int +main () +{ + good (arr); + bad (arr); + exit (0); +}