xref: /dflybsd-src/contrib/gcc-4.7/gcc/graphite-interchange.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Interchange heuristics and transform for loop interchange on
2*e4b17023SJohn Marino    polyhedral representation.
3*e4b17023SJohn Marino 
4*e4b17023SJohn Marino    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5*e4b17023SJohn Marino    Contributed by Sebastian Pop <sebastian.pop@amd.com> and
6*e4b17023SJohn Marino    Harsha Jagasia <harsha.jagasia@amd.com>.
7*e4b17023SJohn Marino 
8*e4b17023SJohn Marino This file is part of GCC.
9*e4b17023SJohn Marino 
10*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
11*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
12*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
13*e4b17023SJohn Marino any later version.
14*e4b17023SJohn Marino 
15*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
16*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
17*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*e4b17023SJohn Marino GNU General Public License for more details.
19*e4b17023SJohn Marino 
20*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
21*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
22*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
23*e4b17023SJohn Marino #include "config.h"
24*e4b17023SJohn Marino #include "system.h"
25*e4b17023SJohn Marino #include "coretypes.h"
26*e4b17023SJohn Marino #include "tree-flow.h"
27*e4b17023SJohn Marino #include "tree-dump.h"
28*e4b17023SJohn Marino #include "cfgloop.h"
29*e4b17023SJohn Marino #include "tree-chrec.h"
30*e4b17023SJohn Marino #include "tree-data-ref.h"
31*e4b17023SJohn Marino #include "tree-scalar-evolution.h"
32*e4b17023SJohn Marino #include "sese.h"
33*e4b17023SJohn Marino 
34*e4b17023SJohn Marino #ifdef HAVE_cloog
35*e4b17023SJohn Marino #include "ppl_c.h"
36*e4b17023SJohn Marino #include "graphite-ppl.h"
37*e4b17023SJohn Marino #include "graphite-poly.h"
38*e4b17023SJohn Marino 
39*e4b17023SJohn Marino /* Builds a linear expression, of dimension DIM, representing PDR's
40*e4b17023SJohn Marino    memory access:
41*e4b17023SJohn Marino 
42*e4b17023SJohn Marino    L = r_{n}*r_{n-1}*...*r_{1}*s_{0} + ... + r_{n}*s_{n-1} + s_{n}.
43*e4b17023SJohn Marino 
44*e4b17023SJohn Marino    For an array A[10][20] with two subscript locations s0 and s1, the
45*e4b17023SJohn Marino    linear memory access is 20 * s0 + s1: a stride of 1 in subscript s0
46*e4b17023SJohn Marino    corresponds to a memory stride of 20.
47*e4b17023SJohn Marino 
48*e4b17023SJohn Marino    OFFSET is a number of dimensions to prepend before the
49*e4b17023SJohn Marino    subscript dimensions: s_0, s_1, ..., s_n.
50*e4b17023SJohn Marino 
51*e4b17023SJohn Marino    Thus, the final linear expression has the following format:
52*e4b17023SJohn Marino    0 .. 0_{offset} | 0 .. 0_{nit} | 0 .. 0_{gd} | 0 | c_0 c_1 ... c_n
53*e4b17023SJohn Marino    where the expression itself is:
54*e4b17023SJohn Marino    c_0 * s_0 + c_1 * s_1 + ... c_n * s_n.  */
55*e4b17023SJohn Marino 
56*e4b17023SJohn Marino static ppl_Linear_Expression_t
build_linearized_memory_access(ppl_dimension_type offset,poly_dr_p pdr)57*e4b17023SJohn Marino build_linearized_memory_access (ppl_dimension_type offset, poly_dr_p pdr)
58*e4b17023SJohn Marino {
59*e4b17023SJohn Marino   ppl_Linear_Expression_t res;
60*e4b17023SJohn Marino   ppl_Linear_Expression_t le;
61*e4b17023SJohn Marino   ppl_dimension_type i;
62*e4b17023SJohn Marino   ppl_dimension_type first = pdr_subscript_dim (pdr, 0);
63*e4b17023SJohn Marino   ppl_dimension_type last = pdr_subscript_dim (pdr, PDR_NB_SUBSCRIPTS (pdr));
64*e4b17023SJohn Marino   mpz_t size, sub_size;
65*e4b17023SJohn Marino   graphite_dim_t dim = offset + pdr_dim (pdr);
66*e4b17023SJohn Marino 
67*e4b17023SJohn Marino   ppl_new_Linear_Expression_with_dimension (&res, dim);
68*e4b17023SJohn Marino 
69*e4b17023SJohn Marino   mpz_init (size);
70*e4b17023SJohn Marino   mpz_set_si (size, 1);
71*e4b17023SJohn Marino   mpz_init (sub_size);
72*e4b17023SJohn Marino   mpz_set_si (sub_size, 1);
73*e4b17023SJohn Marino 
74*e4b17023SJohn Marino   for (i = last - 1; i >= first; i--)
75*e4b17023SJohn Marino     {
76*e4b17023SJohn Marino       ppl_set_coef_gmp (res, i + offset, size);
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino       ppl_new_Linear_Expression_with_dimension (&le, dim - offset);
79*e4b17023SJohn Marino       ppl_set_coef (le, i, 1);
80*e4b17023SJohn Marino       ppl_max_for_le_pointset (PDR_ACCESSES (pdr), le, sub_size);
81*e4b17023SJohn Marino       mpz_mul (size, size, sub_size);
82*e4b17023SJohn Marino       ppl_delete_Linear_Expression (le);
83*e4b17023SJohn Marino     }
84*e4b17023SJohn Marino 
85*e4b17023SJohn Marino   mpz_clear (sub_size);
86*e4b17023SJohn Marino   mpz_clear (size);
87*e4b17023SJohn Marino   return res;
88*e4b17023SJohn Marino }
89*e4b17023SJohn Marino 
90*e4b17023SJohn Marino /* Builds a partial difference equations and inserts them
91*e4b17023SJohn Marino    into pointset powerset polyhedron P.  Polyhedron is assumed
92*e4b17023SJohn Marino    to have the format: T|I|T'|I'|G|S|S'|l1|l2.
93*e4b17023SJohn Marino 
94*e4b17023SJohn Marino    TIME_DEPTH is the time dimension w.r.t. which we are
95*e4b17023SJohn Marino    differentiating.
96*e4b17023SJohn Marino    OFFSET represents the number of dimensions between
97*e4b17023SJohn Marino    columns t_{time_depth} and t'_{time_depth}.
98*e4b17023SJohn Marino    DIM_SCTR is the number of scattering dimensions.  It is
99*e4b17023SJohn Marino    essentially the dimensionality of the T vector.
100*e4b17023SJohn Marino 
101*e4b17023SJohn Marino    The following equations are inserted into the polyhedron P:
102*e4b17023SJohn Marino     | t_1 = t_1'
103*e4b17023SJohn Marino     | ...
104*e4b17023SJohn Marino     | t_{time_depth-1} = t'_{time_depth-1}
105*e4b17023SJohn Marino     | t_{time_depth} = t'_{time_depth} + 1
106*e4b17023SJohn Marino     | t_{time_depth+1} = t'_{time_depth + 1}
107*e4b17023SJohn Marino     | ...
108*e4b17023SJohn Marino     | t_{dim_sctr} = t'_{dim_sctr}.  */
109*e4b17023SJohn Marino 
110*e4b17023SJohn Marino static void
build_partial_difference(ppl_Pointset_Powerset_C_Polyhedron_t * p,ppl_dimension_type time_depth,ppl_dimension_type offset,ppl_dimension_type dim_sctr)111*e4b17023SJohn Marino build_partial_difference (ppl_Pointset_Powerset_C_Polyhedron_t *p,
112*e4b17023SJohn Marino                           ppl_dimension_type time_depth,
113*e4b17023SJohn Marino                           ppl_dimension_type offset,
114*e4b17023SJohn Marino                           ppl_dimension_type dim_sctr)
115*e4b17023SJohn Marino {
116*e4b17023SJohn Marino   ppl_Constraint_t new_cstr;
117*e4b17023SJohn Marino   ppl_Linear_Expression_t le;
118*e4b17023SJohn Marino   ppl_dimension_type i;
119*e4b17023SJohn Marino   ppl_dimension_type dim;
120*e4b17023SJohn Marino   ppl_Pointset_Powerset_C_Polyhedron_t temp;
121*e4b17023SJohn Marino 
122*e4b17023SJohn Marino   /* Add the equality: t_{time_depth} = t'_{time_depth} + 1.
123*e4b17023SJohn Marino      This is the core part of this alogrithm, since this
124*e4b17023SJohn Marino      constraint asks for the memory access stride (difference)
125*e4b17023SJohn Marino      between two consecutive points in time dimensions.  */
126*e4b17023SJohn Marino 
127*e4b17023SJohn Marino   ppl_Pointset_Powerset_C_Polyhedron_space_dimension (*p, &dim);
128*e4b17023SJohn Marino   ppl_new_Linear_Expression_with_dimension (&le, dim);
129*e4b17023SJohn Marino   ppl_set_coef (le, time_depth, 1);
130*e4b17023SJohn Marino   ppl_set_coef (le, time_depth + offset, -1);
131*e4b17023SJohn Marino   ppl_set_inhomogeneous (le, 1);
132*e4b17023SJohn Marino   ppl_new_Constraint (&new_cstr, le, PPL_CONSTRAINT_TYPE_EQUAL);
133*e4b17023SJohn Marino   ppl_Pointset_Powerset_C_Polyhedron_add_constraint (*p, new_cstr);
134*e4b17023SJohn Marino   ppl_delete_Linear_Expression (le);
135*e4b17023SJohn Marino   ppl_delete_Constraint (new_cstr);
136*e4b17023SJohn Marino 
137*e4b17023SJohn Marino   /* Add equalities:
138*e4b17023SJohn Marino      | t1 = t1'
139*e4b17023SJohn Marino      | ...
140*e4b17023SJohn Marino      | t_{time_depth-1} = t'_{time_depth-1}
141*e4b17023SJohn Marino      | t_{time_depth+1} = t'_{time_depth+1}
142*e4b17023SJohn Marino      | ...
143*e4b17023SJohn Marino      | t_{dim_sctr} = t'_{dim_sctr}
144*e4b17023SJohn Marino 
145*e4b17023SJohn Marino      This means that all the time dimensions are equal except for
146*e4b17023SJohn Marino      time_depth, where the constraint is t_{depth} = t'_{depth} + 1
147*e4b17023SJohn Marino      step.  More to this: we should be carefull not to add equalities
148*e4b17023SJohn Marino      to the 'coupled' dimensions, which happens when the one dimension
149*e4b17023SJohn Marino      is stripmined dimension, and the other dimension corresponds
150*e4b17023SJohn Marino      to the point loop inside stripmined dimension.  */
151*e4b17023SJohn Marino 
152*e4b17023SJohn Marino   ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron (&temp, *p);
153*e4b17023SJohn Marino 
154*e4b17023SJohn Marino   for (i = 0; i < dim_sctr; i++)
155*e4b17023SJohn Marino     if (i != time_depth)
156*e4b17023SJohn Marino       {
157*e4b17023SJohn Marino         ppl_new_Linear_Expression_with_dimension (&le, dim);
158*e4b17023SJohn Marino         ppl_set_coef (le, i, 1);
159*e4b17023SJohn Marino         ppl_set_coef (le, i + offset, -1);
160*e4b17023SJohn Marino         ppl_new_Constraint (&new_cstr, le, PPL_CONSTRAINT_TYPE_EQUAL);
161*e4b17023SJohn Marino         ppl_Pointset_Powerset_C_Polyhedron_add_constraint (temp, new_cstr);
162*e4b17023SJohn Marino 
163*e4b17023SJohn Marino         if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (temp))
164*e4b17023SJohn Marino           {
165*e4b17023SJohn Marino             ppl_delete_Pointset_Powerset_C_Polyhedron (temp);
166*e4b17023SJohn Marino             ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron (&temp, *p);
167*e4b17023SJohn Marino           }
168*e4b17023SJohn Marino         else
169*e4b17023SJohn Marino           ppl_Pointset_Powerset_C_Polyhedron_add_constraint (*p, new_cstr);
170*e4b17023SJohn Marino         ppl_delete_Linear_Expression (le);
171*e4b17023SJohn Marino         ppl_delete_Constraint (new_cstr);
172*e4b17023SJohn Marino       }
173*e4b17023SJohn Marino 
174*e4b17023SJohn Marino   ppl_delete_Pointset_Powerset_C_Polyhedron (temp);
175*e4b17023SJohn Marino }
176*e4b17023SJohn Marino 
177*e4b17023SJohn Marino 
178*e4b17023SJohn Marino /* Set STRIDE to the stride of PDR in memory by advancing by one in
179*e4b17023SJohn Marino    the loop at DEPTH.  */
180*e4b17023SJohn Marino 
181*e4b17023SJohn Marino static void
pdr_stride_in_loop(mpz_t stride,graphite_dim_t depth,poly_dr_p pdr)182*e4b17023SJohn Marino pdr_stride_in_loop (mpz_t stride, graphite_dim_t depth, poly_dr_p pdr)
183*e4b17023SJohn Marino {
184*e4b17023SJohn Marino   ppl_dimension_type time_depth;
185*e4b17023SJohn Marino   ppl_Linear_Expression_t le, lma;
186*e4b17023SJohn Marino   ppl_Constraint_t new_cstr;
187*e4b17023SJohn Marino   ppl_dimension_type i, *map;
188*e4b17023SJohn Marino   ppl_Pointset_Powerset_C_Polyhedron_t p1, p2, sctr;
189*e4b17023SJohn Marino   graphite_dim_t nb_subscripts = PDR_NB_SUBSCRIPTS (pdr) + 1;
190*e4b17023SJohn Marino   poly_bb_p pbb = PDR_PBB (pdr);
191*e4b17023SJohn Marino   ppl_dimension_type offset = pbb_nb_scattering_transform (pbb)
192*e4b17023SJohn Marino                               + pbb_nb_local_vars (pbb)
193*e4b17023SJohn Marino                               + pbb_dim_iter_domain (pbb);
194*e4b17023SJohn Marino   ppl_dimension_type offsetg = offset + pbb_nb_params (pbb);
195*e4b17023SJohn Marino   ppl_dimension_type dim_sctr = pbb_nb_scattering_transform (pbb)
196*e4b17023SJohn Marino                                 + pbb_nb_local_vars (pbb);
197*e4b17023SJohn Marino   ppl_dimension_type dim_L1 = offset + offsetg + 2 * nb_subscripts;
198*e4b17023SJohn Marino   ppl_dimension_type dim_L2 = offset + offsetg + 2 * nb_subscripts + 1;
199*e4b17023SJohn Marino   ppl_dimension_type new_dim = offset + offsetg + 2 * nb_subscripts + 2;
200*e4b17023SJohn Marino 
201*e4b17023SJohn Marino   /* The resulting polyhedron should have the following format:
202*e4b17023SJohn Marino      T|I|T'|I'|G|S|S'|l1|l2
203*e4b17023SJohn Marino      where:
204*e4b17023SJohn Marino      | T = t_1..t_{dim_sctr}
205*e4b17023SJohn Marino      | I = i_1..i_{dim_iter_domain}
206*e4b17023SJohn Marino      | T'= t'_1..t'_{dim_sctr}
207*e4b17023SJohn Marino      | I'= i'_1..i'_{dim_iter_domain}
208*e4b17023SJohn Marino      | G = g_1..g_{nb_params}
209*e4b17023SJohn Marino      | S = s_1..s_{nb_subscripts}
210*e4b17023SJohn Marino      | S'= s'_1..s'_{nb_subscripts}
211*e4b17023SJohn Marino      | l1 and l2 are scalars.
212*e4b17023SJohn Marino 
213*e4b17023SJohn Marino      Some invariants:
214*e4b17023SJohn Marino      offset = dim_sctr + dim_iter_domain + nb_local_vars
215*e4b17023SJohn Marino      offsetg = dim_sctr + dim_iter_domain + nb_local_vars + nb_params.  */
216*e4b17023SJohn Marino 
217*e4b17023SJohn Marino   /* Construct the T|I|0|0|G|0|0|0|0 part.  */
218*e4b17023SJohn Marino   {
219*e4b17023SJohn Marino     ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
220*e4b17023SJohn Marino       (&sctr, PBB_TRANSFORMED_SCATTERING (pbb));
221*e4b17023SJohn Marino     ppl_Pointset_Powerset_C_Polyhedron_add_space_dimensions_and_embed
222*e4b17023SJohn Marino       (sctr, 2 * nb_subscripts + 2);
223*e4b17023SJohn Marino     ppl_insert_dimensions_pointset (sctr, offset, offset);
224*e4b17023SJohn Marino   }
225*e4b17023SJohn Marino 
226*e4b17023SJohn Marino   /* Construct the 0|I|0|0|G|S|0|0|0 part.  */
227*e4b17023SJohn Marino   {
228*e4b17023SJohn Marino     ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
229*e4b17023SJohn Marino       (&p1, PDR_ACCESSES (pdr));
230*e4b17023SJohn Marino     ppl_Pointset_Powerset_C_Polyhedron_add_space_dimensions_and_embed
231*e4b17023SJohn Marino       (p1, nb_subscripts + 2);
232*e4b17023SJohn Marino     ppl_insert_dimensions_pointset (p1, 0, dim_sctr);
233*e4b17023SJohn Marino     ppl_insert_dimensions_pointset (p1, offset, offset);
234*e4b17023SJohn Marino   }
235*e4b17023SJohn Marino 
236*e4b17023SJohn Marino   /* Construct the 0|0|0|0|0|S|0|l1|0 part.  */
237*e4b17023SJohn Marino   {
238*e4b17023SJohn Marino     lma = build_linearized_memory_access (offset + dim_sctr, pdr);
239*e4b17023SJohn Marino     ppl_set_coef (lma, dim_L1, -1);
240*e4b17023SJohn Marino     ppl_new_Constraint (&new_cstr, lma, PPL_CONSTRAINT_TYPE_EQUAL);
241*e4b17023SJohn Marino     ppl_Pointset_Powerset_C_Polyhedron_add_constraint (p1, new_cstr);
242*e4b17023SJohn Marino     ppl_delete_Linear_Expression (lma);
243*e4b17023SJohn Marino     ppl_delete_Constraint (new_cstr);
244*e4b17023SJohn Marino   }
245*e4b17023SJohn Marino 
246*e4b17023SJohn Marino   /* Now intersect all the parts to get the polyhedron P1:
247*e4b17023SJohn Marino      T|I|0|0|G|0|0|0 |0
248*e4b17023SJohn Marino      0|I|0|0|G|S|0|0 |0
249*e4b17023SJohn Marino      0|0|0|0|0|S|0|l1|0
250*e4b17023SJohn Marino      ------------------
251*e4b17023SJohn Marino      T|I|0|0|G|S|0|l1|0.  */
252*e4b17023SJohn Marino 
253*e4b17023SJohn Marino   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (p1, sctr);
254*e4b17023SJohn Marino   ppl_delete_Pointset_Powerset_C_Polyhedron (sctr);
255*e4b17023SJohn Marino 
256*e4b17023SJohn Marino   /* Build P2, which would have the following form:
257*e4b17023SJohn Marino      0|0|T'|I'|G|0|S'|0|l2
258*e4b17023SJohn Marino 
259*e4b17023SJohn Marino      P2 is built, by remapping the P1 polyhedron:
260*e4b17023SJohn Marino      T|I|0|0|G|S|0|l1|0
261*e4b17023SJohn Marino 
262*e4b17023SJohn Marino      using the following mapping:
263*e4b17023SJohn Marino      T->T'
264*e4b17023SJohn Marino      I->I'
265*e4b17023SJohn Marino      S->S'
266*e4b17023SJohn Marino      l1->l2.  */
267*e4b17023SJohn Marino   {
268*e4b17023SJohn Marino     ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
269*e4b17023SJohn Marino       (&p2, p1);
270*e4b17023SJohn Marino 
271*e4b17023SJohn Marino     map = ppl_new_id_map (new_dim);
272*e4b17023SJohn Marino 
273*e4b17023SJohn Marino     /* TI -> T'I'.  */
274*e4b17023SJohn Marino     for (i = 0; i < offset; i++)
275*e4b17023SJohn Marino       ppl_interchange (map, i, i + offset);
276*e4b17023SJohn Marino 
277*e4b17023SJohn Marino     /* l1 -> l2.  */
278*e4b17023SJohn Marino     ppl_interchange (map, dim_L1, dim_L2);
279*e4b17023SJohn Marino 
280*e4b17023SJohn Marino     /* S -> S'.  */
281*e4b17023SJohn Marino     for (i = 0; i < nb_subscripts; i++)
282*e4b17023SJohn Marino       ppl_interchange (map, offset + offsetg + i,
283*e4b17023SJohn Marino 		       offset + offsetg + nb_subscripts + i);
284*e4b17023SJohn Marino 
285*e4b17023SJohn Marino     ppl_Pointset_Powerset_C_Polyhedron_map_space_dimensions (p2, map, new_dim);
286*e4b17023SJohn Marino     free (map);
287*e4b17023SJohn Marino   }
288*e4b17023SJohn Marino 
289*e4b17023SJohn Marino   time_depth = psct_dynamic_dim (pbb, depth);
290*e4b17023SJohn Marino 
291*e4b17023SJohn Marino   /* P1 = P1 inter P2.  */
292*e4b17023SJohn Marino   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (p1, p2);
293*e4b17023SJohn Marino   build_partial_difference (&p1, time_depth, offset, dim_sctr);
294*e4b17023SJohn Marino 
295*e4b17023SJohn Marino   /* Maximise the expression L2 - L1.  */
296*e4b17023SJohn Marino   {
297*e4b17023SJohn Marino     ppl_new_Linear_Expression_with_dimension (&le, new_dim);
298*e4b17023SJohn Marino     ppl_set_coef (le, dim_L2, 1);
299*e4b17023SJohn Marino     ppl_set_coef (le, dim_L1, -1);
300*e4b17023SJohn Marino     ppl_max_for_le_pointset (p1, le, stride);
301*e4b17023SJohn Marino   }
302*e4b17023SJohn Marino 
303*e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
304*e4b17023SJohn Marino     {
305*e4b17023SJohn Marino       char *str;
306*e4b17023SJohn Marino       void (*gmp_free) (void *, size_t);
307*e4b17023SJohn Marino 
308*e4b17023SJohn Marino       fprintf (dump_file, "\nStride in BB_%d, DR_%d, depth %d:",
309*e4b17023SJohn Marino 	       pbb_index (pbb), PDR_ID (pdr), (int) depth);
310*e4b17023SJohn Marino       str = mpz_get_str (0, 10, stride);
311*e4b17023SJohn Marino       fprintf (dump_file, "  %s ", str);
312*e4b17023SJohn Marino       mp_get_memory_functions (NULL, NULL, &gmp_free);
313*e4b17023SJohn Marino       (*gmp_free) (str, strlen (str) + 1);
314*e4b17023SJohn Marino     }
315*e4b17023SJohn Marino 
316*e4b17023SJohn Marino   ppl_delete_Pointset_Powerset_C_Polyhedron (p1);
317*e4b17023SJohn Marino   ppl_delete_Pointset_Powerset_C_Polyhedron (p2);
318*e4b17023SJohn Marino   ppl_delete_Linear_Expression (le);
319*e4b17023SJohn Marino }
320*e4b17023SJohn Marino 
321*e4b17023SJohn Marino 
322*e4b17023SJohn Marino /* Sets STRIDES to the sum of all the strides of the data references
323*e4b17023SJohn Marino    accessed in LOOP at DEPTH.  */
324*e4b17023SJohn Marino 
325*e4b17023SJohn Marino static void
memory_strides_in_loop_1(lst_p loop,graphite_dim_t depth,mpz_t strides)326*e4b17023SJohn Marino memory_strides_in_loop_1 (lst_p loop, graphite_dim_t depth, mpz_t strides)
327*e4b17023SJohn Marino {
328*e4b17023SJohn Marino   int i, j;
329*e4b17023SJohn Marino   lst_p l;
330*e4b17023SJohn Marino   poly_dr_p pdr;
331*e4b17023SJohn Marino   mpz_t s, n;
332*e4b17023SJohn Marino 
333*e4b17023SJohn Marino   mpz_init (s);
334*e4b17023SJohn Marino   mpz_init (n);
335*e4b17023SJohn Marino 
336*e4b17023SJohn Marino   FOR_EACH_VEC_ELT (lst_p, LST_SEQ (loop), j, l)
337*e4b17023SJohn Marino     if (LST_LOOP_P (l))
338*e4b17023SJohn Marino       memory_strides_in_loop_1 (l, depth, strides);
339*e4b17023SJohn Marino     else
340*e4b17023SJohn Marino       FOR_EACH_VEC_ELT (poly_dr_p, PBB_DRS (LST_PBB (l)), i, pdr)
341*e4b17023SJohn Marino 	{
342*e4b17023SJohn Marino 	  pdr_stride_in_loop (s, depth, pdr);
343*e4b17023SJohn Marino 	  mpz_set_si (n, PDR_NB_REFS (pdr));
344*e4b17023SJohn Marino 	  mpz_mul (s, s, n);
345*e4b17023SJohn Marino 	  mpz_add (strides, strides, s);
346*e4b17023SJohn Marino 	}
347*e4b17023SJohn Marino 
348*e4b17023SJohn Marino   mpz_clear (s);
349*e4b17023SJohn Marino   mpz_clear (n);
350*e4b17023SJohn Marino }
351*e4b17023SJohn Marino 
352*e4b17023SJohn Marino /* Sets STRIDES to the sum of all the strides of the data references
353*e4b17023SJohn Marino    accessed in LOOP at DEPTH.  */
354*e4b17023SJohn Marino 
355*e4b17023SJohn Marino static void
memory_strides_in_loop(lst_p loop,graphite_dim_t depth,mpz_t strides)356*e4b17023SJohn Marino memory_strides_in_loop (lst_p loop, graphite_dim_t depth, mpz_t strides)
357*e4b17023SJohn Marino {
358*e4b17023SJohn Marino   if (mpz_cmp_si (loop->memory_strides, -1) == 0)
359*e4b17023SJohn Marino     {
360*e4b17023SJohn Marino       mpz_set_si (strides, 0);
361*e4b17023SJohn Marino       memory_strides_in_loop_1 (loop, depth, strides);
362*e4b17023SJohn Marino     }
363*e4b17023SJohn Marino   else
364*e4b17023SJohn Marino     mpz_set (strides, loop->memory_strides);
365*e4b17023SJohn Marino }
366*e4b17023SJohn Marino 
367*e4b17023SJohn Marino /* Return true when the interchange of loops LOOP1 and LOOP2 is
368*e4b17023SJohn Marino    profitable.
369*e4b17023SJohn Marino 
370*e4b17023SJohn Marino    Example:
371*e4b17023SJohn Marino 
372*e4b17023SJohn Marino    | int a[100][100];
373*e4b17023SJohn Marino    |
374*e4b17023SJohn Marino    | int
375*e4b17023SJohn Marino    | foo (int N)
376*e4b17023SJohn Marino    | {
377*e4b17023SJohn Marino    |   int j;
378*e4b17023SJohn Marino    |   int i;
379*e4b17023SJohn Marino    |
380*e4b17023SJohn Marino    |   for (i = 0; i < N; i++)
381*e4b17023SJohn Marino    |     for (j = 0; j < N; j++)
382*e4b17023SJohn Marino    |       a[j][2 * i] += 1;
383*e4b17023SJohn Marino    |
384*e4b17023SJohn Marino    |   return a[N][12];
385*e4b17023SJohn Marino    | }
386*e4b17023SJohn Marino 
387*e4b17023SJohn Marino    The data access A[j][i] is described like this:
388*e4b17023SJohn Marino 
389*e4b17023SJohn Marino    | i   j   N   a  s0  s1   1
390*e4b17023SJohn Marino    | 0   0   0   1   0   0  -5    = 0
391*e4b17023SJohn Marino    | 0  -1   0   0   1   0   0    = 0
392*e4b17023SJohn Marino    |-2   0   0   0   0   1   0    = 0
393*e4b17023SJohn Marino    | 0   0   0   0   1   0   0   >= 0
394*e4b17023SJohn Marino    | 0   0   0   0   0   1   0   >= 0
395*e4b17023SJohn Marino    | 0   0   0   0  -1   0 100   >= 0
396*e4b17023SJohn Marino    | 0   0   0   0   0  -1 100   >= 0
397*e4b17023SJohn Marino 
398*e4b17023SJohn Marino    The linearized memory access L to A[100][100] is:
399*e4b17023SJohn Marino 
400*e4b17023SJohn Marino    | i   j   N   a  s0  s1   1
401*e4b17023SJohn Marino    | 0   0   0   0 100   1   0
402*e4b17023SJohn Marino 
403*e4b17023SJohn Marino    TODO: the shown format is not valid as it does not show the fact
404*e4b17023SJohn Marino    that the iteration domain "i j" is transformed using the scattering.
405*e4b17023SJohn Marino 
406*e4b17023SJohn Marino    Next, to measure the impact of iterating once in loop "i", we build
407*e4b17023SJohn Marino    a maximization problem: first, we add to DR accesses the dimensions
408*e4b17023SJohn Marino    k, s2, s3, L1 = 100 * s0 + s1, L2, and D1: this is the polyhedron P1.
409*e4b17023SJohn Marino    L1 and L2 are the linearized memory access functions.
410*e4b17023SJohn Marino 
411*e4b17023SJohn Marino    | i   j   N   a  s0  s1   k  s2  s3  L1  L2  D1   1
412*e4b17023SJohn Marino    | 0   0   0   1   0   0   0   0   0   0   0   0  -5    = 0  alias = 5
413*e4b17023SJohn Marino    | 0  -1   0   0   1   0   0   0   0   0   0   0   0    = 0  s0 = j
414*e4b17023SJohn Marino    |-2   0   0   0   0   1   0   0   0   0   0   0   0    = 0  s1 = 2 * i
415*e4b17023SJohn Marino    | 0   0   0   0   1   0   0   0   0   0   0   0   0   >= 0
416*e4b17023SJohn Marino    | 0   0   0   0   0   1   0   0   0   0   0   0   0   >= 0
417*e4b17023SJohn Marino    | 0   0   0   0  -1   0   0   0   0   0   0   0 100   >= 0
418*e4b17023SJohn Marino    | 0   0   0   0   0  -1   0   0   0   0   0   0 100   >= 0
419*e4b17023SJohn Marino    | 0   0   0   0 100   1   0   0   0  -1   0   0   0    = 0  L1 = 100 * s0 + s1
420*e4b17023SJohn Marino 
421*e4b17023SJohn Marino    Then, we generate the polyhedron P2 by interchanging the dimensions
422*e4b17023SJohn Marino    (s0, s2), (s1, s3), (L1, L2), (k, i)
423*e4b17023SJohn Marino 
424*e4b17023SJohn Marino    | i   j   N   a  s0  s1   k  s2  s3  L1  L2  D1   1
425*e4b17023SJohn Marino    | 0   0   0   1   0   0   0   0   0   0   0   0  -5    = 0  alias = 5
426*e4b17023SJohn Marino    | 0  -1   0   0   0   0   0   1   0   0   0   0   0    = 0  s2 = j
427*e4b17023SJohn Marino    | 0   0   0   0   0   0  -2   0   1   0   0   0   0    = 0  s3 = 2 * k
428*e4b17023SJohn Marino    | 0   0   0   0   0   0   0   1   0   0   0   0   0   >= 0
429*e4b17023SJohn Marino    | 0   0   0   0   0   0   0   0   1   0   0   0   0   >= 0
430*e4b17023SJohn Marino    | 0   0   0   0   0   0   0  -1   0   0   0   0 100   >= 0
431*e4b17023SJohn Marino    | 0   0   0   0   0   0   0   0  -1   0   0   0 100   >= 0
432*e4b17023SJohn Marino    | 0   0   0   0   0   0   0 100   1   0  -1   0   0    = 0  L2 = 100 * s2 + s3
433*e4b17023SJohn Marino 
434*e4b17023SJohn Marino    then we add to P2 the equality k = i + 1:
435*e4b17023SJohn Marino 
436*e4b17023SJohn Marino    |-1   0   0   0   0   0   1   0   0   0   0   0  -1    = 0  k = i + 1
437*e4b17023SJohn Marino 
438*e4b17023SJohn Marino    and finally we maximize the expression "D1 = max (P1 inter P2, L2 - L1)".
439*e4b17023SJohn Marino 
440*e4b17023SJohn Marino    Similarly, to determine the impact of one iteration on loop "j", we
441*e4b17023SJohn Marino    interchange (k, j), we add "k = j + 1", and we compute D2 the
442*e4b17023SJohn Marino    maximal value of the difference.
443*e4b17023SJohn Marino 
444*e4b17023SJohn Marino    Finally, the profitability test is D1 < D2: if in the outer loop
445*e4b17023SJohn Marino    the strides are smaller than in the inner loop, then it is
446*e4b17023SJohn Marino    profitable to interchange the loops at DEPTH1 and DEPTH2.  */
447*e4b17023SJohn Marino 
448*e4b17023SJohn Marino static bool
lst_interchange_profitable_p(lst_p nest,int depth1,int depth2)449*e4b17023SJohn Marino lst_interchange_profitable_p (lst_p nest, int depth1, int depth2)
450*e4b17023SJohn Marino {
451*e4b17023SJohn Marino   mpz_t d1, d2;
452*e4b17023SJohn Marino   bool res;
453*e4b17023SJohn Marino 
454*e4b17023SJohn Marino   gcc_assert (depth1 < depth2);
455*e4b17023SJohn Marino 
456*e4b17023SJohn Marino   mpz_init (d1);
457*e4b17023SJohn Marino   mpz_init (d2);
458*e4b17023SJohn Marino 
459*e4b17023SJohn Marino   memory_strides_in_loop (nest, depth1, d1);
460*e4b17023SJohn Marino   memory_strides_in_loop (nest, depth2, d2);
461*e4b17023SJohn Marino 
462*e4b17023SJohn Marino   res = mpz_cmp (d1, d2) < 0;
463*e4b17023SJohn Marino 
464*e4b17023SJohn Marino   mpz_clear (d1);
465*e4b17023SJohn Marino   mpz_clear (d2);
466*e4b17023SJohn Marino 
467*e4b17023SJohn Marino   return res;
468*e4b17023SJohn Marino }
469*e4b17023SJohn Marino 
470*e4b17023SJohn Marino /* Interchanges the loops at DEPTH1 and DEPTH2 of the original
471*e4b17023SJohn Marino    scattering and assigns the resulting polyhedron to the transformed
472*e4b17023SJohn Marino    scattering.  */
473*e4b17023SJohn Marino 
474*e4b17023SJohn Marino static void
pbb_interchange_loop_depths(graphite_dim_t depth1,graphite_dim_t depth2,poly_bb_p pbb)475*e4b17023SJohn Marino pbb_interchange_loop_depths (graphite_dim_t depth1, graphite_dim_t depth2,
476*e4b17023SJohn Marino 			     poly_bb_p pbb)
477*e4b17023SJohn Marino {
478*e4b17023SJohn Marino   ppl_dimension_type i, dim;
479*e4b17023SJohn Marino   ppl_dimension_type *map;
480*e4b17023SJohn Marino   ppl_Polyhedron_t poly = PBB_TRANSFORMED_SCATTERING (pbb);
481*e4b17023SJohn Marino   ppl_dimension_type dim1 = psct_dynamic_dim (pbb, depth1);
482*e4b17023SJohn Marino   ppl_dimension_type dim2 = psct_dynamic_dim (pbb, depth2);
483*e4b17023SJohn Marino 
484*e4b17023SJohn Marino   ppl_Polyhedron_space_dimension (poly, &dim);
485*e4b17023SJohn Marino   map = (ppl_dimension_type *) XNEWVEC (ppl_dimension_type, dim);
486*e4b17023SJohn Marino 
487*e4b17023SJohn Marino   for (i = 0; i < dim; i++)
488*e4b17023SJohn Marino     map[i] = i;
489*e4b17023SJohn Marino 
490*e4b17023SJohn Marino   map[dim1] = dim2;
491*e4b17023SJohn Marino   map[dim2] = dim1;
492*e4b17023SJohn Marino 
493*e4b17023SJohn Marino   ppl_Polyhedron_map_space_dimensions (poly, map, dim);
494*e4b17023SJohn Marino   free (map);
495*e4b17023SJohn Marino }
496*e4b17023SJohn Marino 
497*e4b17023SJohn Marino /* Apply the interchange of loops at depths DEPTH1 and DEPTH2 to all
498*e4b17023SJohn Marino    the statements below LST.  */
499*e4b17023SJohn Marino 
500*e4b17023SJohn Marino static void
lst_apply_interchange(lst_p lst,int depth1,int depth2)501*e4b17023SJohn Marino lst_apply_interchange (lst_p lst, int depth1, int depth2)
502*e4b17023SJohn Marino {
503*e4b17023SJohn Marino   if (!lst)
504*e4b17023SJohn Marino     return;
505*e4b17023SJohn Marino 
506*e4b17023SJohn Marino   if (LST_LOOP_P (lst))
507*e4b17023SJohn Marino     {
508*e4b17023SJohn Marino       int i;
509*e4b17023SJohn Marino       lst_p l;
510*e4b17023SJohn Marino 
511*e4b17023SJohn Marino       FOR_EACH_VEC_ELT (lst_p, LST_SEQ (lst), i, l)
512*e4b17023SJohn Marino 	lst_apply_interchange (l, depth1, depth2);
513*e4b17023SJohn Marino     }
514*e4b17023SJohn Marino   else
515*e4b17023SJohn Marino     pbb_interchange_loop_depths (depth1, depth2, LST_PBB (lst));
516*e4b17023SJohn Marino }
517*e4b17023SJohn Marino 
518*e4b17023SJohn Marino /* Return true when the nest starting at LOOP1 and ending on LOOP2 is
519*e4b17023SJohn Marino    perfect: i.e. there are no sequence of statements.  */
520*e4b17023SJohn Marino 
521*e4b17023SJohn Marino static bool
lst_perfectly_nested_p(lst_p loop1,lst_p loop2)522*e4b17023SJohn Marino lst_perfectly_nested_p (lst_p loop1, lst_p loop2)
523*e4b17023SJohn Marino {
524*e4b17023SJohn Marino   if (loop1 == loop2)
525*e4b17023SJohn Marino     return true;
526*e4b17023SJohn Marino 
527*e4b17023SJohn Marino   if (!LST_LOOP_P (loop1))
528*e4b17023SJohn Marino     return false;
529*e4b17023SJohn Marino 
530*e4b17023SJohn Marino   return VEC_length (lst_p, LST_SEQ (loop1)) == 1
531*e4b17023SJohn Marino     && lst_perfectly_nested_p (VEC_index (lst_p, LST_SEQ (loop1), 0), loop2);
532*e4b17023SJohn Marino }
533*e4b17023SJohn Marino 
534*e4b17023SJohn Marino /* Transform the loop nest between LOOP1 and LOOP2 into a perfect
535*e4b17023SJohn Marino    nest.  To continue the naming tradition, this function is called
536*e4b17023SJohn Marino    after perfect_nestify.  NEST is set to the perfectly nested loop
537*e4b17023SJohn Marino    that is created.  BEFORE/AFTER are set to the loops distributed
538*e4b17023SJohn Marino    before/after the loop NEST.  */
539*e4b17023SJohn Marino 
540*e4b17023SJohn Marino static void
lst_perfect_nestify(lst_p loop1,lst_p loop2,lst_p * before,lst_p * nest,lst_p * after)541*e4b17023SJohn Marino lst_perfect_nestify (lst_p loop1, lst_p loop2, lst_p *before,
542*e4b17023SJohn Marino 		     lst_p *nest, lst_p *after)
543*e4b17023SJohn Marino {
544*e4b17023SJohn Marino   poly_bb_p first, last;
545*e4b17023SJohn Marino 
546*e4b17023SJohn Marino   gcc_assert (loop1 && loop2
547*e4b17023SJohn Marino 	      && loop1 != loop2
548*e4b17023SJohn Marino 	      && LST_LOOP_P (loop1) && LST_LOOP_P (loop2));
549*e4b17023SJohn Marino 
550*e4b17023SJohn Marino   first = LST_PBB (lst_find_first_pbb (loop2));
551*e4b17023SJohn Marino   last = LST_PBB (lst_find_last_pbb (loop2));
552*e4b17023SJohn Marino 
553*e4b17023SJohn Marino   *before = copy_lst (loop1);
554*e4b17023SJohn Marino   *nest = copy_lst (loop1);
555*e4b17023SJohn Marino   *after = copy_lst (loop1);
556*e4b17023SJohn Marino 
557*e4b17023SJohn Marino   lst_remove_all_before_including_pbb (*before, first, false);
558*e4b17023SJohn Marino   lst_remove_all_before_including_pbb (*after, last, true);
559*e4b17023SJohn Marino 
560*e4b17023SJohn Marino   lst_remove_all_before_excluding_pbb (*nest, first, true);
561*e4b17023SJohn Marino   lst_remove_all_before_excluding_pbb (*nest, last, false);
562*e4b17023SJohn Marino 
563*e4b17023SJohn Marino   if (lst_empty_p (*before))
564*e4b17023SJohn Marino     {
565*e4b17023SJohn Marino       free_lst (*before);
566*e4b17023SJohn Marino       *before = NULL;
567*e4b17023SJohn Marino     }
568*e4b17023SJohn Marino   if (lst_empty_p (*after))
569*e4b17023SJohn Marino     {
570*e4b17023SJohn Marino       free_lst (*after);
571*e4b17023SJohn Marino       *after = NULL;
572*e4b17023SJohn Marino     }
573*e4b17023SJohn Marino   if (lst_empty_p (*nest))
574*e4b17023SJohn Marino     {
575*e4b17023SJohn Marino       free_lst (*nest);
576*e4b17023SJohn Marino       *nest = NULL;
577*e4b17023SJohn Marino     }
578*e4b17023SJohn Marino }
579*e4b17023SJohn Marino 
580*e4b17023SJohn Marino /* Try to interchange LOOP1 with LOOP2 for all the statements of the
581*e4b17023SJohn Marino    body of LOOP2.  LOOP1 contains LOOP2.  Return true if it did the
582*e4b17023SJohn Marino    interchange.  */
583*e4b17023SJohn Marino 
584*e4b17023SJohn Marino static bool
lst_try_interchange_loops(scop_p scop,lst_p loop1,lst_p loop2)585*e4b17023SJohn Marino lst_try_interchange_loops (scop_p scop, lst_p loop1, lst_p loop2)
586*e4b17023SJohn Marino {
587*e4b17023SJohn Marino   int depth1 = lst_depth (loop1);
588*e4b17023SJohn Marino   int depth2 = lst_depth (loop2);
589*e4b17023SJohn Marino   lst_p transformed;
590*e4b17023SJohn Marino 
591*e4b17023SJohn Marino   lst_p before = NULL, nest = NULL, after = NULL;
592*e4b17023SJohn Marino 
593*e4b17023SJohn Marino   if (!lst_perfectly_nested_p (loop1, loop2))
594*e4b17023SJohn Marino     lst_perfect_nestify (loop1, loop2, &before, &nest, &after);
595*e4b17023SJohn Marino 
596*e4b17023SJohn Marino   if (!lst_interchange_profitable_p (loop2, depth1, depth2))
597*e4b17023SJohn Marino     return false;
598*e4b17023SJohn Marino 
599*e4b17023SJohn Marino   lst_apply_interchange (loop2, depth1, depth2);
600*e4b17023SJohn Marino 
601*e4b17023SJohn Marino   /* Sync the transformed LST information and the PBB scatterings
602*e4b17023SJohn Marino      before using the scatterings in the data dependence analysis.  */
603*e4b17023SJohn Marino   if (before || nest || after)
604*e4b17023SJohn Marino     {
605*e4b17023SJohn Marino       transformed = lst_substitute_3 (SCOP_TRANSFORMED_SCHEDULE (scop), loop1,
606*e4b17023SJohn Marino 				      before, nest, after);
607*e4b17023SJohn Marino       lst_update_scattering (transformed);
608*e4b17023SJohn Marino       free_lst (transformed);
609*e4b17023SJohn Marino     }
610*e4b17023SJohn Marino 
611*e4b17023SJohn Marino   if (graphite_legal_transform (scop))
612*e4b17023SJohn Marino     {
613*e4b17023SJohn Marino       if (dump_file && (dump_flags & TDF_DETAILS))
614*e4b17023SJohn Marino 	fprintf (dump_file,
615*e4b17023SJohn Marino 		 "Loops at depths %d and %d will be interchanged.\n",
616*e4b17023SJohn Marino 		 depth1, depth2);
617*e4b17023SJohn Marino 
618*e4b17023SJohn Marino       /* Transform the SCOP_TRANSFORMED_SCHEDULE of the SCOP.  */
619*e4b17023SJohn Marino       lst_insert_in_sequence (before, loop1, true);
620*e4b17023SJohn Marino       lst_insert_in_sequence (after, loop1, false);
621*e4b17023SJohn Marino 
622*e4b17023SJohn Marino       if (nest)
623*e4b17023SJohn Marino 	{
624*e4b17023SJohn Marino 	  lst_replace (loop1, nest);
625*e4b17023SJohn Marino 	  free_lst (loop1);
626*e4b17023SJohn Marino 	}
627*e4b17023SJohn Marino 
628*e4b17023SJohn Marino       return true;
629*e4b17023SJohn Marino     }
630*e4b17023SJohn Marino 
631*e4b17023SJohn Marino   /* Undo the transform.  */
632*e4b17023SJohn Marino   free_lst (before);
633*e4b17023SJohn Marino   free_lst (nest);
634*e4b17023SJohn Marino   free_lst (after);
635*e4b17023SJohn Marino   lst_apply_interchange (loop2, depth2, depth1);
636*e4b17023SJohn Marino   return false;
637*e4b17023SJohn Marino }
638*e4b17023SJohn Marino 
639*e4b17023SJohn Marino /* Selects the inner loop in LST_SEQ (INNER_FATHER) to be interchanged
640*e4b17023SJohn Marino    with the loop OUTER in LST_SEQ (OUTER_FATHER).  */
641*e4b17023SJohn Marino 
642*e4b17023SJohn Marino static bool
lst_interchange_select_inner(scop_p scop,lst_p outer_father,int outer,lst_p inner_father)643*e4b17023SJohn Marino lst_interchange_select_inner (scop_p scop, lst_p outer_father, int outer,
644*e4b17023SJohn Marino 			      lst_p inner_father)
645*e4b17023SJohn Marino {
646*e4b17023SJohn Marino   int inner;
647*e4b17023SJohn Marino   lst_p loop1, loop2;
648*e4b17023SJohn Marino 
649*e4b17023SJohn Marino   gcc_assert (outer_father
650*e4b17023SJohn Marino 	      && LST_LOOP_P (outer_father)
651*e4b17023SJohn Marino 	      && LST_LOOP_P (VEC_index (lst_p, LST_SEQ (outer_father), outer))
652*e4b17023SJohn Marino 	      && inner_father
653*e4b17023SJohn Marino 	      && LST_LOOP_P (inner_father));
654*e4b17023SJohn Marino 
655*e4b17023SJohn Marino   loop1 = VEC_index (lst_p, LST_SEQ (outer_father), outer);
656*e4b17023SJohn Marino 
657*e4b17023SJohn Marino   FOR_EACH_VEC_ELT (lst_p, LST_SEQ (inner_father), inner, loop2)
658*e4b17023SJohn Marino     if (LST_LOOP_P (loop2)
659*e4b17023SJohn Marino 	&& (lst_try_interchange_loops (scop, loop1, loop2)
660*e4b17023SJohn Marino 	    || lst_interchange_select_inner (scop, outer_father, outer, loop2)))
661*e4b17023SJohn Marino       return true;
662*e4b17023SJohn Marino 
663*e4b17023SJohn Marino   return false;
664*e4b17023SJohn Marino }
665*e4b17023SJohn Marino 
666*e4b17023SJohn Marino /* Interchanges all the loops of LOOP and the loops of its body that
667*e4b17023SJohn Marino    are considered profitable to interchange.  Return the number of
668*e4b17023SJohn Marino    interchanged loops.  OUTER is the index in LST_SEQ (LOOP) that
669*e4b17023SJohn Marino    points to the next outer loop to be considered for interchange.  */
670*e4b17023SJohn Marino 
671*e4b17023SJohn Marino static int
lst_interchange_select_outer(scop_p scop,lst_p loop,int outer)672*e4b17023SJohn Marino lst_interchange_select_outer (scop_p scop, lst_p loop, int outer)
673*e4b17023SJohn Marino {
674*e4b17023SJohn Marino   lst_p l;
675*e4b17023SJohn Marino   int res = 0;
676*e4b17023SJohn Marino   int i = 0;
677*e4b17023SJohn Marino   lst_p father;
678*e4b17023SJohn Marino 
679*e4b17023SJohn Marino   if (!loop || !LST_LOOP_P (loop))
680*e4b17023SJohn Marino     return 0;
681*e4b17023SJohn Marino 
682*e4b17023SJohn Marino   father = LST_LOOP_FATHER (loop);
683*e4b17023SJohn Marino   if (father)
684*e4b17023SJohn Marino     {
685*e4b17023SJohn Marino       while (lst_interchange_select_inner (scop, father, outer, loop))
686*e4b17023SJohn Marino 	{
687*e4b17023SJohn Marino 	  res++;
688*e4b17023SJohn Marino 	  loop = VEC_index (lst_p, LST_SEQ (father), outer);
689*e4b17023SJohn Marino 	}
690*e4b17023SJohn Marino     }
691*e4b17023SJohn Marino 
692*e4b17023SJohn Marino   if (LST_LOOP_P (loop))
693*e4b17023SJohn Marino     FOR_EACH_VEC_ELT (lst_p, LST_SEQ (loop), i, l)
694*e4b17023SJohn Marino       if (LST_LOOP_P (l))
695*e4b17023SJohn Marino 	res += lst_interchange_select_outer (scop, l, i);
696*e4b17023SJohn Marino 
697*e4b17023SJohn Marino   return res;
698*e4b17023SJohn Marino }
699*e4b17023SJohn Marino 
700*e4b17023SJohn Marino /* Interchanges all the loop depths that are considered profitable for
701*e4b17023SJohn Marino    SCOP.  Return the number of interchanged loops.  */
702*e4b17023SJohn Marino 
703*e4b17023SJohn Marino int
scop_do_interchange(scop_p scop)704*e4b17023SJohn Marino scop_do_interchange (scop_p scop)
705*e4b17023SJohn Marino {
706*e4b17023SJohn Marino   int res = lst_interchange_select_outer
707*e4b17023SJohn Marino     (scop, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
708*e4b17023SJohn Marino 
709*e4b17023SJohn Marino   lst_update_scattering (SCOP_TRANSFORMED_SCHEDULE (scop));
710*e4b17023SJohn Marino 
711*e4b17023SJohn Marino   return res;
712*e4b17023SJohn Marino }
713*e4b17023SJohn Marino 
714*e4b17023SJohn Marino 
715*e4b17023SJohn Marino #endif
716*e4b17023SJohn Marino 
717