OSDN Git Service

2009-09-17 Li Feng <nemokingdom@gmail.com>
[pf3gnuchains/gcc-fork.git] / gcc / graphite-dependences.c
1 /* Data dependence analysis for Graphite.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3    Contributed by Sebastian Pop <sebastian.pop@amd.com> and
4    Konrad Trifunovic <konrad.trifunovic@inria.fr>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "toplev.h"
33 #include "tree-dump.h"
34 #include "timevar.h"
35 #include "cfgloop.h"
36 #include "tree-chrec.h"
37 #include "tree-data-ref.h"
38 #include "tree-scalar-evolution.h"
39 #include "tree-pass.h"
40 #include "domwalk.h"
41 #include "pointer-set.h"
42 #include "gimple.h"
43
44 #ifdef HAVE_cloog
45 #include "cloog/cloog.h"
46 #include "ppl_c.h"
47 #include "sese.h"
48 #include "graphite-ppl.h"
49 #include "graphite.h"
50 #include "graphite-poly.h"
51 #include "graphite-dependences.h"
52
53 /* Returns a new polyhedral Data Dependence Relation (DDR).  SOURCE is
54    the source data reference, SINK is the sink data reference.  SOURCE
55    and SINK define an edge in the Data Dependence Graph (DDG).  */
56
57 static poly_ddr_p
58 new_poly_ddr (poly_dr_p source, poly_dr_p sink,
59               ppl_Pointset_Powerset_C_Polyhedron_t ddp)
60 {
61   poly_ddr_p pddr;
62
63   pddr = XNEW (struct poly_ddr);
64   PDDR_SOURCE (pddr) = source;
65   PDDR_SINK (pddr) = sink;
66   PDDR_DDP (pddr) = ddp;
67   PDDR_KIND (pddr) = unknown_dependence;
68
69   return pddr;
70 }
71
72 /* Free the poly_ddr_p P.  */
73
74 void
75 free_poly_ddr (void *p)
76 {
77   poly_ddr_p pddr = (poly_ddr_p) p;
78   ppl_delete_Pointset_Powerset_C_Polyhedron (PDDR_DDP (pddr));
79   free (pddr);
80 }
81
82 /* Comparison function for poly_ddr hash table.  */
83
84 int
85 eq_poly_ddr_p (const void *pddr1, const void *pddr2)
86 {
87   const struct poly_ddr *p1 = (const struct poly_ddr *) pddr1;
88   const struct poly_ddr *p2 = (const struct poly_ddr *) pddr2;
89
90   return (PDDR_SOURCE (p1) == PDDR_SOURCE (p2)
91           && PDDR_SINK (p1) == PDDR_SINK (p2));
92 }
93
94 /* Hash function for poly_ddr hashtable.  */
95
96 hashval_t
97 hash_poly_ddr_p (const void *pddr)
98 {
99   const struct poly_ddr *p = (const struct poly_ddr *) pddr;
100
101   return (hashval_t) ((long) PDDR_SOURCE (p) + (long) PDDR_SINK (p));
102 }
103
104 /* Returns true when PDDR has no dependence.  */
105
106 static bool
107 pddr_is_empty (poly_ddr_p pddr)
108 {
109   if (PDDR_KIND (pddr) != unknown_dependence)
110     return PDDR_KIND (pddr) == no_dependence ? true : false;
111
112   if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PDDR_DDP (pddr)))
113     {
114       PDDR_KIND (pddr) = no_dependence;
115       return true;
116     }
117
118   PDDR_KIND (pddr) = has_dependence;
119   return false;
120 }
121
122 /* Returns a polyhedron of dimension DIM.
123
124    Maps the dimensions [0, ..., cut - 1] of polyhedron P to OFFSET0
125    and the dimensions [cut, ..., nb_dim] to DIM - GDIM.  */
126
127 static ppl_Pointset_Powerset_C_Polyhedron_t
128 map_into_dep_poly (graphite_dim_t dim, graphite_dim_t gdim,
129                    ppl_Pointset_Powerset_C_Polyhedron_t p,
130                    graphite_dim_t cut,
131                    graphite_dim_t offset)
132 {
133   ppl_Pointset_Powerset_C_Polyhedron_t res;
134
135   ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
136     (&res, p);
137   ppl_insert_dimensions_pointset (res, 0, offset);
138   ppl_insert_dimensions_pointset (res, offset + cut,
139                                   dim - offset - cut - gdim);
140
141   return res;
142 }
143
144 /* Swap [cut0, ..., cut1] to the end of DR: "a CUT0 b CUT1 c" is
145    transformed into "a CUT0 c CUT1' b"
146
147    Add NB0 zeros before "a":  "00...0 a CUT0 c CUT1' b"
148    Add NB1 zeros between "a" and "c":  "00...0 a 00...0 c CUT1' b"
149    Add DIM - NB0 - NB1 - PDIM zeros between "c" and "b":
150    "00...0 a 00...0 c 00...0 b".  */
151
152 static ppl_Pointset_Powerset_C_Polyhedron_t
153 map_dr_into_dep_poly (graphite_dim_t dim,
154                       ppl_Pointset_Powerset_C_Polyhedron_t dr,
155                       graphite_dim_t cut0, graphite_dim_t cut1,
156                       graphite_dim_t nb0, graphite_dim_t nb1)
157 {
158   ppl_dimension_type pdim;
159   ppl_dimension_type *map;
160   ppl_Pointset_Powerset_C_Polyhedron_t res;
161   ppl_dimension_type i;
162
163   ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
164     (&res, dr);
165   ppl_Pointset_Powerset_C_Polyhedron_space_dimension (res, &pdim);
166
167   map = (ppl_dimension_type *) XNEWVEC (ppl_dimension_type, pdim);
168
169   /* First mapping: move 'g' vector to right position.  */
170   for (i = 0; i < cut0; i++)
171     map[i] = i;
172
173   for (i = cut0; i < cut1; i++)
174     map[i] = pdim - cut1 + i;
175
176   for (i = cut1; i < pdim; i++)
177     map[i] = cut0 + i - cut1;
178
179   ppl_Pointset_Powerset_C_Polyhedron_map_space_dimensions (res, map, pdim);
180   free (map);
181
182   /* After swapping 's' and 'g' vectors, we have to update a new cut.  */
183   cut1 = pdim - cut1 + cut0;
184
185   ppl_insert_dimensions_pointset (res, 0, nb0);
186   ppl_insert_dimensions_pointset (res, nb0 + cut0, nb1);
187   ppl_insert_dimensions_pointset (res, nb0 + nb1 + cut1,
188                                   dim - nb0 - nb1 - pdim);
189
190   return res;
191 }
192
193 /* Builds a constraints of the form "POS1 - POS2 CSTR_TYPE C" */
194
195 static ppl_Constraint_t
196 build_pairwise_constraint (graphite_dim_t dim,
197                            graphite_dim_t pos1, graphite_dim_t pos2,
198                            int c, enum ppl_enum_Constraint_Type cstr_type)
199 {
200   ppl_Linear_Expression_t expr;
201   ppl_Constraint_t cstr;
202   ppl_Coefficient_t coef;
203   Value v, v_op, v_c;
204
205   value_init (v);
206   value_init (v_op);
207   value_init (v_c);
208
209   value_set_si (v, 1);
210   value_set_si (v_op, -1);
211   value_set_si (v_c, c);
212
213   ppl_new_Coefficient (&coef);
214   ppl_new_Linear_Expression_with_dimension (&expr, dim);
215
216   ppl_assign_Coefficient_from_mpz_t (coef, v);
217   ppl_Linear_Expression_add_to_coefficient (expr, pos1, coef);
218   ppl_assign_Coefficient_from_mpz_t (coef, v_op);
219   ppl_Linear_Expression_add_to_coefficient (expr, pos2, coef);
220   ppl_assign_Coefficient_from_mpz_t (coef, v_c);
221   ppl_Linear_Expression_add_to_inhomogeneous (expr, coef);
222
223   ppl_new_Constraint (&cstr, expr, cstr_type);
224
225   ppl_delete_Linear_Expression (expr);
226   ppl_delete_Coefficient (coef);
227   value_clear (v);
228   value_clear (v_op);
229   value_clear (v_c);
230
231   return cstr;
232 }
233
234 /* Builds subscript equality constraints.  */
235
236 static ppl_Pointset_Powerset_C_Polyhedron_t
237 dr_equality_constraints (graphite_dim_t dim,
238                          graphite_dim_t pos, graphite_dim_t nb_subscripts)
239 {
240   ppl_Polyhedron_t subscript_equalities;
241   ppl_Pointset_Powerset_C_Polyhedron_t res;
242   Value v, v_op;
243   graphite_dim_t i;
244
245   value_init (v);
246   value_init (v_op);
247   value_set_si (v, 1);
248   value_set_si (v_op, -1);
249
250   ppl_new_C_Polyhedron_from_space_dimension (&subscript_equalities, dim, 0);
251   for (i = 0; i < nb_subscripts; i++)
252     {
253       ppl_Linear_Expression_t expr;
254       ppl_Constraint_t cstr;
255       ppl_Coefficient_t coef;
256
257       ppl_new_Coefficient (&coef);
258       ppl_new_Linear_Expression_with_dimension (&expr, dim);
259
260       ppl_assign_Coefficient_from_mpz_t (coef, v);
261       ppl_Linear_Expression_add_to_coefficient (expr, pos + i, coef);
262       ppl_assign_Coefficient_from_mpz_t (coef, v_op);
263       ppl_Linear_Expression_add_to_coefficient (expr, pos + i + nb_subscripts,
264                                                 coef);
265
266       ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
267       ppl_Polyhedron_add_constraint (subscript_equalities, cstr);
268
269       ppl_delete_Linear_Expression (expr);
270       ppl_delete_Constraint (cstr);
271       ppl_delete_Coefficient (coef);
272     }
273
274   ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
275     (&res, subscript_equalities);
276   value_clear (v);
277   value_clear (v_op);
278   ppl_delete_Polyhedron (subscript_equalities);
279
280   return res;
281 }
282
283 /* Builds scheduling equality constraints.  */
284
285 static ppl_Pointset_Powerset_C_Polyhedron_t
286 build_pairwise_scheduling_equality (graphite_dim_t dim,
287                                     graphite_dim_t pos, graphite_dim_t offset)
288 {
289   ppl_Pointset_Powerset_C_Polyhedron_t res;
290   ppl_Polyhedron_t equalities;
291   ppl_Constraint_t cstr;
292
293   ppl_new_C_Polyhedron_from_space_dimension (&equalities, dim, 0);
294
295   cstr = build_pairwise_constraint (dim, pos, pos + offset, 0,
296                                     PPL_CONSTRAINT_TYPE_EQUAL);
297   ppl_Polyhedron_add_constraint (equalities, cstr);
298   ppl_delete_Constraint (cstr);
299
300   ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&res, equalities);
301   ppl_delete_Polyhedron (equalities);
302   return res;
303 }
304
305 /* Builds scheduling inequality constraints.  */
306
307 static ppl_Pointset_Powerset_C_Polyhedron_t
308 build_pairwise_scheduling_inequality (graphite_dim_t dim,
309                                       graphite_dim_t pos,
310                                       graphite_dim_t offset,
311                                       bool direction)
312 {
313   ppl_Pointset_Powerset_C_Polyhedron_t res;
314   ppl_Polyhedron_t equalities;
315   ppl_Constraint_t cstr;
316
317   ppl_new_C_Polyhedron_from_space_dimension (&equalities, dim, 0);
318
319   if (direction)
320     cstr = build_pairwise_constraint (dim, pos, pos + offset, -1,
321                                       PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
322   else
323     cstr = build_pairwise_constraint (dim, pos, pos + offset, 1,
324                                       PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL);
325
326   ppl_Polyhedron_add_constraint (equalities, cstr);
327   ppl_delete_Constraint (cstr);
328
329   ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&res, equalities);
330   ppl_delete_Polyhedron (equalities);
331   return res;
332 }
333
334 /* Returns true when adding the lexicographical constraints at level I
335    to the RES dependence polyhedron returns an empty polyhedron.  */
336
337 static bool
338 lexicographically_gt_p (ppl_Pointset_Powerset_C_Polyhedron_t res,
339                         graphite_dim_t dim,
340                         graphite_dim_t offset,
341                         bool direction, graphite_dim_t i)
342 {
343   ppl_Pointset_Powerset_C_Polyhedron_t ineq;
344   bool empty_p;
345
346   ineq = build_pairwise_scheduling_inequality (dim, i, offset,
347                                                direction);
348   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (ineq, res);
349   empty_p = ppl_Pointset_Powerset_C_Polyhedron_is_empty (ineq);
350   if (!empty_p)
351     ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (res, ineq);
352   ppl_delete_Pointset_Powerset_C_Polyhedron (ineq);
353
354   return !empty_p;
355 }
356
357 /* Build the precedence constraints for the lexicographical comparison
358    of time vectors RES following the lexicographical order.  */
359
360 static void
361 build_lexicographically_gt_constraint (ppl_Pointset_Powerset_C_Polyhedron_t *res,
362                                        graphite_dim_t dim,
363                                        graphite_dim_t tdim1,
364                                        graphite_dim_t offset,
365                                        bool direction)
366 {
367   graphite_dim_t i;
368
369   if (lexicographically_gt_p (*res, dim, offset, direction, 0))
370     return;
371
372   for (i = 0; i < tdim1 - 1; i++)
373     {
374       ppl_Pointset_Powerset_C_Polyhedron_t sceq;
375
376       sceq = build_pairwise_scheduling_equality (dim, i, offset);
377       ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (*res, sceq);
378       ppl_delete_Pointset_Powerset_C_Polyhedron (sceq);
379
380       if (lexicographically_gt_p (*res, dim, offset, direction, i + 1))
381         return;
382     }
383
384   if (i == tdim1 - 1)
385     {
386       ppl_delete_Pointset_Powerset_C_Polyhedron (*res);
387       ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension (res, dim, 1);
388     }
389 }
390
391 /* Build the dependence polyhedron for data references PDR1 and PDR2.  */
392
393 static poly_ddr_p
394 dependence_polyhedron_1 (poly_bb_p pbb1, poly_bb_p pbb2,
395                          ppl_Pointset_Powerset_C_Polyhedron_t d1,
396                          ppl_Pointset_Powerset_C_Polyhedron_t d2,
397                          poly_dr_p pdr1, poly_dr_p pdr2,
398                          ppl_Polyhedron_t s1, ppl_Polyhedron_t s2,
399                          bool direction,
400                          bool original_scattering_p)
401 {
402   scop_p scop = PBB_SCOP (pbb1);
403   graphite_dim_t tdim1 = original_scattering_p ?
404     pbb_nb_scattering_orig (pbb1) : pbb_nb_scattering_transform (pbb1);
405   graphite_dim_t tdim2 = original_scattering_p ?
406     pbb_nb_scattering_orig (pbb2) : pbb_nb_scattering_transform (pbb2);
407   graphite_dim_t ddim1 = pbb_dim_iter_domain (pbb1);
408   graphite_dim_t ddim2 = pbb_dim_iter_domain (pbb2);
409   graphite_dim_t sdim1 = PDR_NB_SUBSCRIPTS (pdr1) + 1;
410   graphite_dim_t gdim = scop_nb_params (scop);
411   graphite_dim_t dim1 = pdr_dim (pdr1);
412   graphite_dim_t dim2 = pdr_dim (pdr2);
413   graphite_dim_t dim = tdim1 + tdim2 + dim1 + dim2;
414   ppl_Pointset_Powerset_C_Polyhedron_t res;
415   ppl_Pointset_Powerset_C_Polyhedron_t id1, id2, isc1, isc2, idr1, idr2;
416   ppl_Pointset_Powerset_C_Polyhedron_t sc1, sc2, dreq;
417
418   gcc_assert (PBB_SCOP (pbb1) == PBB_SCOP (pbb2));
419   ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&sc1, s1);
420   ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&sc2, s2);
421
422   id1 = map_into_dep_poly (dim, gdim, d1, ddim1, tdim1);
423   id2 = map_into_dep_poly (dim, gdim, d2, ddim2, tdim1 + ddim1 + tdim2);
424   isc1 = map_into_dep_poly (dim, gdim, sc1, ddim1 + tdim1, 0);
425   isc2 = map_into_dep_poly (dim, gdim, sc2, ddim2 + tdim2, tdim1 + ddim1);
426
427   idr1 = map_dr_into_dep_poly (dim, PDR_ACCESSES (pdr1), ddim1, ddim1 + gdim,
428                                tdim1, tdim2 + ddim2);
429   idr2 = map_dr_into_dep_poly (dim, PDR_ACCESSES (pdr2), ddim2, ddim2 + gdim,
430                                tdim1 + ddim1 + tdim2, sdim1);
431
432   /* Now add the subscript equalities.  */
433   dreq = dr_equality_constraints (dim, tdim1 + ddim1 + tdim2 + ddim2, sdim1);
434
435   ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension (&res, dim, 0);
436   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (res, id1);
437   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (res, id2);
438   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (res, isc1);
439   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (res, isc2);
440   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (res, idr1);
441   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (res, idr2);
442   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (res, dreq);
443   ppl_delete_Pointset_Powerset_C_Polyhedron (id1);
444   ppl_delete_Pointset_Powerset_C_Polyhedron (id2);
445   ppl_delete_Pointset_Powerset_C_Polyhedron (sc1);
446   ppl_delete_Pointset_Powerset_C_Polyhedron (sc2);
447   ppl_delete_Pointset_Powerset_C_Polyhedron (isc1);
448   ppl_delete_Pointset_Powerset_C_Polyhedron (isc2);
449   ppl_delete_Pointset_Powerset_C_Polyhedron (idr1);
450   ppl_delete_Pointset_Powerset_C_Polyhedron (idr2);
451   ppl_delete_Pointset_Powerset_C_Polyhedron (dreq);
452
453   if (!ppl_Pointset_Powerset_C_Polyhedron_is_empty (res))
454     build_lexicographically_gt_constraint (&res, dim, MIN (tdim1, tdim2),
455                                            tdim1 + ddim1, direction);
456
457   return new_poly_ddr (pdr1, pdr2, res);
458 }
459
460 /* Build the dependence polyhedron for data references PDR1 and PDR2.
461    If possible use already cached information.  */
462
463 static poly_ddr_p
464 dependence_polyhedron (poly_bb_p pbb1, poly_bb_p pbb2,
465                        ppl_Pointset_Powerset_C_Polyhedron_t d1,
466                        ppl_Pointset_Powerset_C_Polyhedron_t d2,
467                        poly_dr_p pdr1, poly_dr_p pdr2,
468                        ppl_Polyhedron_t s1, ppl_Polyhedron_t s2,
469                        bool direction,
470                        bool original_scattering_p)
471 {
472   PTR *x = NULL;
473   poly_ddr_p res;
474
475   if (original_scattering_p)
476     {
477       struct poly_ddr tmp;
478
479       tmp.source = pdr1;
480       tmp.sink = pdr2;
481       x = htab_find_slot (SCOP_ORIGINAL_PDDRS (PBB_SCOP (pbb1)),
482                           &tmp, INSERT);
483
484       if (x && *x)
485         return (poly_ddr_p) *x;
486     }
487
488   res = dependence_polyhedron_1 (pbb1, pbb2, d1, d2, pdr1, pdr2,
489                                  s1, s2, direction, original_scattering_p);
490
491   if (original_scattering_p)
492     *x = res;
493
494   return res;
495 }
496
497 static bool
498 poly_drs_may_alias_p (poly_dr_p pdr1, poly_dr_p pdr2);
499
500 /* Returns the PDDR corresponding to the original schedule, or NULL if
501    the dependence relation is empty or unknown (Can't judge dependency
502    under polyhedral model.  */
503
504 static poly_ddr_p
505 pddr_original_scattering (poly_bb_p pbb1, poly_bb_p pbb2,
506                           poly_dr_p pdr1, poly_dr_p pdr2)
507 {
508   poly_ddr_p pddr;
509   ppl_Pointset_Powerset_C_Polyhedron_t d1 = PBB_DOMAIN (pbb1);
510   ppl_Pointset_Powerset_C_Polyhedron_t d2 = PBB_DOMAIN (pbb2);
511   ppl_Polyhedron_t so1 = PBB_ORIGINAL_SCATTERING (pbb1);
512   ppl_Polyhedron_t so2 = PBB_ORIGINAL_SCATTERING (pbb2);
513
514   if ((pdr_read_p (pdr1) && pdr_read_p (pdr2))
515       || PDR_BASE_OBJECT_SET (pdr1) != PDR_BASE_OBJECT_SET (pdr2)
516       || PDR_NB_SUBSCRIPTS (pdr1) != PDR_NB_SUBSCRIPTS (pdr2))
517     return NULL;
518
519   pddr = dependence_polyhedron (pbb1, pbb2, d1, d2, pdr1, pdr2, so1, so2,
520                                 true, true);
521   if (pddr_is_empty (pddr))
522     return NULL;
523
524   return pddr;
525 }
526
527 /* Returns true when the PBB_TRANSFORMED_SCATTERING functions of PBB1
528    and PBB2 respect the data dependences of PBB_ORIGINAL_SCATTERING
529    functions.  */
530
531 static bool
532 graphite_legal_transform_dr (poly_bb_p pbb1, poly_bb_p pbb2,
533                              poly_dr_p pdr1, poly_dr_p pdr2)
534 {
535   ppl_Polyhedron_t st1, st2;
536   ppl_Pointset_Powerset_C_Polyhedron_t po, pt;
537   graphite_dim_t ddim1, otdim1, otdim2, ttdim1, ttdim2;
538   ppl_Pointset_Powerset_C_Polyhedron_t temp;
539   ppl_dimension_type pdim;
540   bool is_empty_p;
541   poly_ddr_p pddr;
542   ppl_Pointset_Powerset_C_Polyhedron_t d1 = PBB_DOMAIN (pbb1);
543   ppl_Pointset_Powerset_C_Polyhedron_t d2 = PBB_DOMAIN (pbb2);
544
545   pddr = pddr_original_scattering (pbb1, pbb2, pdr1, pdr2);
546   if (!pddr)
547     return true;
548
549   po = PDDR_DDP (pddr);
550
551   if (dump_file && (dump_flags & TDF_DETAILS))
552     fprintf (dump_file, "\nloop carries dependency.\n");
553
554   st1 = PBB_TRANSFORMED_SCATTERING (pbb1);
555   st2 = PBB_TRANSFORMED_SCATTERING (pbb2);
556   ddim1 = pbb_dim_iter_domain (pbb1);
557   otdim1 = pbb_nb_scattering_orig (pbb1);
558   otdim2 = pbb_nb_scattering_orig (pbb2);
559   ttdim1 = pbb_nb_scattering_transform (pbb1);
560   ttdim2 = pbb_nb_scattering_transform (pbb2);
561
562   /* Copy the PO polyhedron into the TEMP, so it is not destroyed.
563      Keep in mind, that PO polyhedron might be restored from the cache
564      and should not be modified!  */
565   ppl_Pointset_Powerset_C_Polyhedron_space_dimension (po, &pdim);
566   ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension (&temp, pdim, 0);
567   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (temp, po);
568
569   pddr = dependence_polyhedron (pbb1, pbb2, d1, d2, pdr1, pdr2, st1, st2,
570                                 false, false);
571   pt = PDDR_DDP (pddr);
572
573   /* Extend PO and PT to have the same dimensions.  */
574   ppl_insert_dimensions_pointset (temp, otdim1, ttdim1);
575   ppl_insert_dimensions_pointset (temp, otdim1 + ttdim1 + ddim1 + otdim2, ttdim2);
576   ppl_insert_dimensions_pointset (pt, 0, otdim1);
577   ppl_insert_dimensions_pointset (pt, otdim1 + ttdim1 + ddim1, otdim2);
578
579   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (temp, pt);
580   is_empty_p = ppl_Pointset_Powerset_C_Polyhedron_is_empty (temp);
581
582   ppl_delete_Pointset_Powerset_C_Polyhedron (temp);
583   free_poly_ddr (pddr);
584
585   return is_empty_p;
586 }
587
588 /* Iterates over the data references of PBB1 and PBB2 and detect
589    whether the transformed schedule is correct.  */
590
591 static bool
592 graphite_legal_transform_bb (poly_bb_p pbb1, poly_bb_p pbb2)
593 {
594   int i, j;
595   poly_dr_p pdr1, pdr2;
596
597   if (!PBB_PDR_DUPLICATES_REMOVED (pbb1))
598     pbb_remove_duplicate_pdrs (pbb1);
599
600   if (!PBB_PDR_DUPLICATES_REMOVED (pbb2))
601     pbb_remove_duplicate_pdrs (pbb2);
602
603   for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb1), i, pdr1); i++)
604     for (j = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb2), j, pdr2); j++)
605       if (!graphite_legal_transform_dr (pbb1, pbb2, pdr1, pdr2))
606         return false;
607
608   return true;
609 }
610
611 /* Iterates over the SCOP and detect whether the transformed schedule
612    is correct.  */
613
614 bool
615 graphite_legal_transform (scop_p scop)
616 {
617   int i, j;
618   poly_bb_p pbb1, pbb2;
619
620   timevar_push (TV_GRAPHITE_DATA_DEPS);
621
622   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb1); i++)
623     for (j = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), j, pbb2); j++)
624       if (!graphite_legal_transform_bb (pbb1, pbb2))
625         {
626           timevar_pop (TV_GRAPHITE_DATA_DEPS);
627           return false;
628         }
629
630   timevar_pop (TV_GRAPHITE_DATA_DEPS);
631   return true;
632 }
633
634 /* Remove all the dimensions except alias information at dimension
635    ALIAS_DIM.  */
636
637 static void
638 build_alias_set_powerset (ppl_Pointset_Powerset_C_Polyhedron_t alias_powerset,
639                           ppl_dimension_type alias_dim)
640 {
641   ppl_dimension_type *ds;
642   ppl_dimension_type access_dim;
643   unsigned i, pos = 0;
644
645   ppl_Pointset_Powerset_C_Polyhedron_space_dimension (alias_powerset,
646                                                       &access_dim);
647   ds = XNEWVEC (ppl_dimension_type, access_dim-1);
648   for (i = 0; i < access_dim; i++)
649     {
650       if (i == alias_dim)
651         continue;
652
653       ds[pos] = i;
654       pos++;
655     }
656
657   ppl_Pointset_Powerset_C_Polyhedron_remove_space_dimensions (alias_powerset,
658                                                               ds,
659                                                               access_dim - 1);
660   free (ds);
661 }
662
663 /* Return true when PDR1 and PDR2 may alias.  */
664
665 static bool
666 poly_drs_may_alias_p (poly_dr_p pdr1, poly_dr_p pdr2)
667 {
668   ppl_Pointset_Powerset_C_Polyhedron_t alias_powerset1, alias_powerset2;
669   ppl_Pointset_Powerset_C_Polyhedron_t accesses1 = PDR_ACCESSES (pdr1);
670   ppl_Pointset_Powerset_C_Polyhedron_t accesses2 = PDR_ACCESSES (pdr2);
671   ppl_dimension_type alias_dim1 = pdr_alias_set_dim (pdr1);
672   ppl_dimension_type alias_dim2 = pdr_alias_set_dim (pdr2);
673   int empty_p;
674
675   ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
676     (&alias_powerset1, accesses1);
677   ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
678     (&alias_powerset2, accesses2);
679
680   build_alias_set_powerset (alias_powerset1, alias_dim1);
681   build_alias_set_powerset (alias_powerset2, alias_dim2);
682
683   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
684     (alias_powerset1, alias_powerset2);
685
686   empty_p = ppl_Pointset_Powerset_C_Polyhedron_is_empty (alias_powerset1);
687
688   ppl_delete_Pointset_Powerset_C_Polyhedron (alias_powerset1);
689   ppl_delete_Pointset_Powerset_C_Polyhedron (alias_powerset2);
690
691   return !empty_p;
692 }
693
694 /* Returns TRUE when the dependence polyhedron between PDR1 and
695    PDR2 represents a loop carried dependence at level LEVEL.  */
696
697 static bool
698 graphite_carried_dependence_level_k (poly_dr_p pdr1, poly_dr_p pdr2,
699                                      int level)
700 {
701   poly_bb_p pbb1 = PDR_PBB (pdr1);
702   poly_bb_p pbb2 = PDR_PBB (pdr2);
703   ppl_Pointset_Powerset_C_Polyhedron_t d1 = PBB_DOMAIN (pbb1);
704   ppl_Pointset_Powerset_C_Polyhedron_t d2 = PBB_DOMAIN (pbb2);
705   ppl_Polyhedron_t so1 = PBB_TRANSFORMED_SCATTERING (pbb1);
706   ppl_Polyhedron_t so2 = PBB_TRANSFORMED_SCATTERING (pbb2);
707   ppl_Pointset_Powerset_C_Polyhedron_t po;
708   ppl_Pointset_Powerset_C_Polyhedron_t eqpp;
709   graphite_dim_t tdim1 = pbb_nb_scattering_transform (pbb1);
710   graphite_dim_t ddim1 = pbb_dim_iter_domain (pbb1);
711   ppl_dimension_type dim;
712   bool empty_p;
713   poly_ddr_p pddr;
714   int obj_base_set1 = PDR_BASE_OBJECT_SET (pdr1);
715   int obj_base_set2 = PDR_BASE_OBJECT_SET (pdr2);
716
717   if ((pdr_read_p (pdr1) && pdr_read_p (pdr2))
718       || !poly_drs_may_alias_p (pdr1, pdr2))
719     return false;
720
721   if (obj_base_set1 != obj_base_set2)
722     return true;
723
724   if (PDR_NB_SUBSCRIPTS (pdr1) != PDR_NB_SUBSCRIPTS (pdr2))
725     return false;
726
727   pddr = dependence_polyhedron (pbb1, pbb2, d1, d2, pdr1, pdr2, so1, so2,
728                                 true, false);
729
730   if (pddr_is_empty (pddr))
731     return false;
732
733   po = PDDR_DDP (pddr);
734   ppl_Pointset_Powerset_C_Polyhedron_space_dimension (po, &dim);
735   eqpp = build_pairwise_scheduling_inequality (dim, level, tdim1 + ddim1, 1);
736
737   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (eqpp, po);
738   empty_p = ppl_Pointset_Powerset_C_Polyhedron_is_empty (eqpp);
739
740   ppl_delete_Pointset_Powerset_C_Polyhedron (eqpp);
741   return !empty_p;
742 }
743
744 /* Check data dependency between PBB1 and PBB2 at level LEVEL.  */
745
746 bool
747 dependency_between_pbbs_p (poly_bb_p pbb1, poly_bb_p pbb2, int level)
748 {
749   int i, j;
750   poly_dr_p pdr1, pdr2;
751
752   timevar_push (TV_GRAPHITE_DATA_DEPS);
753
754   for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb1), i, pdr1); i++)
755     for (j = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb2), j, pdr2); j++)
756       if (graphite_carried_dependence_level_k (pdr1, pdr2, level))
757         {
758           timevar_pop (TV_GRAPHITE_DATA_DEPS);
759           return true;
760         }
761
762   timevar_pop (TV_GRAPHITE_DATA_DEPS);
763   return false;
764 }
765
766 /* Pretty print to FILE all the data dependences of SCoP in DOT
767    format.  */
768
769 static void
770 dot_deps_1 (FILE *file, scop_p scop)
771 {
772   int i, j, k, l;
773   poly_bb_p pbb1, pbb2;
774   poly_dr_p pdr1, pdr2;
775
776   fputs ("digraph all {\n", file);
777
778   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb1); i++)
779     for (j = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), j, pbb2); j++)
780       for (k = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb1), k, pdr1); k++)
781         for (l = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb2), l, pdr2); l++)
782           if (pddr_original_scattering (pbb1, pbb2, pdr1, pdr2))
783             fprintf (file, "S%d_D%d -> S%d_D%d\n",
784                      pbb_index (pbb1), PDR_ID (pdr1),
785                      pbb_index (pbb2), PDR_ID (pdr2));
786
787   fputs ("}\n\n", file);
788 }
789
790 /* Display all the data dependences in SCoP using dotty.  */
791
792 void
793 dot_deps (scop_p scop)
794 {
795   /* When debugging, enable the following code.  This cannot be used
796      in production compilers because it calls "system".  */
797 #if 0
798   int x;
799   FILE *stream = fopen ("/tmp/scopdeps.dot", "w");
800   gcc_assert (stream);
801
802   dot_deps_1 (stream, scop);
803   fclose (stream);
804
805   x = system ("dotty /tmp/scopdeps.dot");
806 #else
807   dot_deps_1 (stderr, scop);
808 #endif
809 }
810
811
812 #endif