1  /* Test that __builtin_prefetch does no harm.
       2  
       3     Prefetch data using a variety of storage classes and address
       4     expressions.  */
       5  
       6  int glob_int_arr[100];
       7  int *glob_ptr_int = glob_int_arr;
       8  int glob_int = 4;
       9  
      10  static stat_int_arr[100];
      11  static int *stat_ptr_int = stat_int_arr;
      12  static int stat_int;
      13  
      14  struct S {
      15    int a;
      16    short b, c;
      17    char d[8];
      18    struct S *next;
      19  };
      20  
      21  struct S str;
      22  struct S *ptr_str = &str;
      23  
      24  /* Prefetch global variables using the address of the variable.  */
      25  
      26  void
      27  simple_global ()
      28  {
      29    __builtin_prefetch (glob_int_arr, 0, 0);
      30    __builtin_prefetch (glob_ptr_int, 0, 0);
      31    __builtin_prefetch (&glob_int, 0, 0);
      32  }
      33  
      34  /* Prefetch file-level static variables using the address of the variable.  */
      35  
      36  void
      37  simple_file ()
      38  {
      39    __builtin_prefetch (stat_int_arr, 0, 0);
      40    __builtin_prefetch (stat_ptr_int, 0, 0);
      41    __builtin_prefetch (&stat_int, 0, 0);
      42  }
      43  
      44  /* Prefetch local static variables using the address of the variable.  */
      45  
      46  void
      47  simple_static_local ()
      48  {
      49    static int gx[100];
      50    static int *hx = gx;
      51    static int ix;
      52    __builtin_prefetch (gx, 0, 0);
      53    __builtin_prefetch (hx, 0, 0);
      54    __builtin_prefetch (&ix, 0, 0);
      55  }
      56  
      57  /* Prefetch local stack variables using the address of the variable.  */
      58  
      59  void
      60  simple_local ()
      61  {
      62    int gx[100];
      63    int *hx = gx;
      64    int ix;
      65    __builtin_prefetch (gx, 0, 0);
      66    __builtin_prefetch (hx, 0, 0);
      67    __builtin_prefetch (&ix, 0, 0);
      68  }
      69  
      70  /* Prefetch arguments using the address of the variable.  */
      71  
      72  void
      73  simple_arg (int g[100], int *h, int i)
      74  {
      75    __builtin_prefetch (g, 0, 0);
      76    __builtin_prefetch (h, 0, 0);
      77    __builtin_prefetch (&i, 0, 0);
      78  }
      79  
      80  /* Prefetch using address expressions involving global variables.  */
      81  
      82  void
      83  expr_global (void)
      84  {
      85    __builtin_prefetch (&str, 0, 0);
      86    __builtin_prefetch (ptr_str, 0, 0);
      87    __builtin_prefetch (&str.b, 0, 0);
      88    __builtin_prefetch (&ptr_str->b, 0, 0);
      89    __builtin_prefetch (&str.d, 0, 0);
      90    __builtin_prefetch (&ptr_str->d, 0, 0);
      91    __builtin_prefetch (str.next, 0, 0);
      92    __builtin_prefetch (ptr_str->next, 0, 0);
      93    __builtin_prefetch (str.next->d, 0, 0);
      94    __builtin_prefetch (ptr_str->next->d, 0, 0);
      95  
      96    __builtin_prefetch (&glob_int_arr, 0, 0);
      97    __builtin_prefetch (glob_ptr_int, 0, 0);
      98    __builtin_prefetch (&glob_int_arr[2], 0, 0);
      99    __builtin_prefetch (&glob_ptr_int[3], 0, 0);
     100    __builtin_prefetch (glob_int_arr+3, 0, 0);
     101    __builtin_prefetch (glob_int_arr+glob_int, 0, 0);
     102    __builtin_prefetch (glob_ptr_int+5, 0, 0);
     103    __builtin_prefetch (glob_ptr_int+glob_int, 0, 0);
     104  }
     105  
     106  /* Prefetch using address expressions involving local variables.  */
     107  
     108  void
     109  expr_local (void)
     110  {
     111    int b[10];
     112    int *pb = b;
     113    struct S t;
     114    struct S *pt = &t;
     115    int j = 4;
     116  
     117    __builtin_prefetch (&t, 0, 0);
     118    __builtin_prefetch (pt, 0, 0);
     119    __builtin_prefetch (&t.b, 0, 0);
     120    __builtin_prefetch (&pt->b, 0, 0);
     121    __builtin_prefetch (&t.d, 0, 0);
     122    __builtin_prefetch (&pt->d, 0, 0);
     123    __builtin_prefetch (t.next, 0, 0);
     124    __builtin_prefetch (pt->next, 0, 0);
     125    __builtin_prefetch (t.next->d, 0, 0);
     126    __builtin_prefetch (pt->next->d, 0, 0);
     127  
     128    __builtin_prefetch (&b, 0, 0);
     129    __builtin_prefetch (pb, 0, 0);
     130    __builtin_prefetch (&b[2], 0, 0);
     131    __builtin_prefetch (&pb[3], 0, 0);
     132    __builtin_prefetch (b+3, 0, 0);
     133    __builtin_prefetch (b+j, 0, 0);
     134    __builtin_prefetch (pb+5, 0, 0);
     135    __builtin_prefetch (pb+j, 0, 0);
     136  }
     137  
     138  int
     139  main ()
     140  {
     141    simple_global ();
     142    simple_file ();
     143    simple_static_local ();
     144    simple_local ();
     145    simple_arg (glob_int_arr, glob_ptr_int, glob_int);
     146  
     147    str.next = &str;
     148    expr_global ();
     149    expr_local ();
     150  
     151    exit (0);
     152  }