1 /* { dg-do compile { target { powerpc64le-*-* } } } */
2 /* { dg-require-effective-target powerpc_p8vector_ok } */
3 /* { dg-skip-if "do not override -mcpu" { powerpc*-*-* } { "-mcpu=*" } { "-mcpu=power8" } } */
4 /* { dg-options "-mcpu=power8 -O3 " } */
5 
6 /* Previous versions of this test required that the assembler does not
7    contain xxpermdi or xxswapd.  However, with the more sophisticated
8    code generation used today, it is now possible that xxpermdi (aka
9    xxswapd) show up without being part of a lxvd2x or stxvd2x
10    sequence.  */
11 
12 #include <altivec.h>
13 
14 extern void abort (void);
15 
16 vector int x;
17 const vector int y = { 0, 1, 2, 3 };
18 vector int z;
19 
20 vector int
foo(void)21 foo (void)
22 {
23   return y;			/* Remove 1 swap and use lvx.  */
24 }
25 
26 vector int
foo1(void)27 foo1 (void)
28 {
29   x = y;			/* Remove 2 redundant swaps here.  */
30   return x;			/* Remove 1 swap and use lvx.  */
31 }
32 
33 void __attribute__ ((noinline))
fill_local(vector int * vp)34 fill_local (vector int *vp)
35 {
36   *vp = x;			/* Remove 2 redundant swaps here.  */
37 }
38 
39 /* Test aligned load from local.  */
40 vector int
foo2(void)41 foo2 (void)
42 {
43   vector int v;
44 
45   /* Need to be clever here because v will normally reside in a
46      register rather than memory.  */
47   fill_local (&v);
48   return v;			/* Remove 1 swap and use lvx.  */
49 }
50 
51 
52 /* Test aligned load from pointer.  */
53 vector int
foo3(vector int * arg)54 foo3 (vector int *arg)
55 {
56   return *arg;			/* Remove 1 swap and use lvx.  */
57 }
58 
59 /* In this structure, the compiler should insert padding to assure
60    that a_vector is properly aligned.  */
61 struct bar {
62   short a_field;
63   vector int a_vector;
64 };
65 
66 vector int
foo4(struct bar * bp)67 foo4 (struct bar *bp)
68 {
69   return bp->a_vector;		/* Remove 1 swap and use lvx.  */
70 }
71 
72 /* Test aligned store to global.  */
73 void
baz(vector int arg)74 baz (vector int arg)
75 {
76   x = arg;			/* Remove 1 swap and use stvx.  */
77 }
78 
79 void __attribute__ ((noinline))
copy_local(vector int * arg)80 copy_local (vector int *arg)
81 {
82   x = *arg;			/* Remove 2 redundant swaps.  */
83 }
84 
85 
86 /* Test aligned store to local.  */
87 void
baz1(vector int arg)88 baz1 (vector int arg)
89 {
90   vector int v;
91 
92   /* Need cleverness, because v will normally reside in a register
93      rather than memory.  */
94   v = arg;			/* Aligned store to local: remove 1
95 				   swap and use stvx.  */
96   copy_local (&v);
97 }
98 
99 /* Test aligned store to pointer.  */
100 void
baz2(vector int * arg1,vector int arg2)101 baz2 (vector int *arg1, vector int arg2)
102 {
103   /* Assume arg2 resides in register.  */
104   *arg1 = arg2;			/* Remove 1 swap and use stvx.  */
105 }
106 
107 void
baz3(struct bar * bp,vector int v)108 baz3 (struct bar *bp, vector int v)
109 {
110   /* Assume v resides in register.  */
111   bp->a_vector = v;		/* Remove 1 swap and use stvx.  */
112 }
113 
114 int
main(int argc,int * argv[])115 main (int argc, int *argv[])
116 {
117   vector int fetched_value = foo ();
118   if (fetched_value[0] != 0 || fetched_value[3] != 3)
119     abort ();
120 
121   fetched_value = foo1 ();
122   if (fetched_value[1] != 1 || fetched_value[2] != 2)
123     abort ();
124 
125   fetched_value = foo2 ();
126   if (fetched_value[2] != 2 || fetched_value[1] != 1)
127     abort ();
128 
129   fetched_value = foo3 (&x);
130   if (fetched_value[3] != 3 || fetched_value[0] != 0)
131     abort ();
132 
133   struct bar a_struct;
134   a_struct.a_vector = x;	/* Remove 2 redundant swaps.  */
135   fetched_value = foo4 (&a_struct);
136   if (fetched_value[2] != 2 || fetched_value[3] != 3)
137     abort ();
138 
139   z[0] = 7;
140   z[1] = 6;
141   z[2] = 5;
142   z[3] = 4;
143 
144   baz (z);
145   if (x[0] != 7 || x[3] != 4)
146     abort ();
147 
148   vector int source = { 8, 7, 6, 5 };
149 
150   baz1 (source);
151   if (x[3] != 6 || x[2] != 7)
152     abort ();
153 
154   vector int dest;
155   baz2 (&dest, source);
156   if (dest[0] != 8 || dest[1] != 7)
157     abort ();
158 
159   baz3 (&a_struct, source);
160   if (a_struct.a_vector[3] != 5 || a_struct.a_vector[0] != 8)
161     abort ();
162 
163   return 0;
164 }
165