OSDN Git Service

2009-10-06 Sebastian Pop <sebastian.pop@amd.com>
[pf3gnuchains/gcc-fork.git] / gcc / graphite-poly.c
1 /* Graphite polyhedral representation.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3    Contributed by Sebastian Pop <sebastian.pop@amd.com> and
4    Tobias Grosser <grosser@fim.uni-passau.de>.
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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "output.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 "value-prof.h"
42 #include "pointer-set.h"
43 #include "gimple.h"
44 #include "params.h"
45
46 #ifdef HAVE_cloog
47 #include "cloog/cloog.h"
48 #include "ppl_c.h"
49 #include "sese.h"
50 #include "graphite-ppl.h"
51 #include "graphite.h"
52 #include "graphite-poly.h"
53 #include "graphite-dependences.h"
54
55 /* Return the maximal loop depth in SCOP.  */
56
57 int
58 scop_max_loop_depth (scop_p scop)
59 {
60   int i;
61   poly_bb_p pbb;
62   int max_nb_loops = 0;
63
64   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
65     {
66       int nb_loops = pbb_dim_iter_domain (pbb);
67       if (max_nb_loops < nb_loops)
68         max_nb_loops = nb_loops;
69     }
70
71   return max_nb_loops;
72 }
73
74 /* Extend the scattering matrix of PBB to MAX_SCATTERING scattering
75    dimensions.  */
76
77 static void
78 extend_scattering (poly_bb_p pbb, int max_scattering)
79 {
80   ppl_dimension_type nb_old_dims, nb_new_dims;
81   int nb_added_dims, i;
82   ppl_Coefficient_t coef;
83   Value one;
84
85   nb_added_dims = max_scattering - pbb_nb_scattering_transform (pbb);
86   value_init (one);
87   value_set_si (one, 1);
88   ppl_new_Coefficient (&coef);
89   ppl_assign_Coefficient_from_mpz_t (coef, one);
90
91   gcc_assert (nb_added_dims >= 0);
92
93   nb_old_dims = pbb_nb_scattering_transform (pbb) + pbb_dim_iter_domain (pbb)
94     + scop_nb_params (PBB_SCOP (pbb));
95   nb_new_dims = nb_old_dims + nb_added_dims;
96
97   ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb),
98                          pbb_nb_scattering_transform (pbb), nb_added_dims);
99   PBB_NB_SCATTERING_TRANSFORM (pbb) += nb_added_dims;
100
101   /* Add identity matrix for the added dimensions.  */
102   for (i = max_scattering - nb_added_dims; i < max_scattering; i++)
103     {
104       ppl_Constraint_t cstr;
105       ppl_Linear_Expression_t expr;
106
107       ppl_new_Linear_Expression_with_dimension (&expr, nb_new_dims);
108       ppl_Linear_Expression_add_to_coefficient (expr, i, coef);
109       ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
110       ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb), cstr);
111       ppl_delete_Constraint (cstr);
112       ppl_delete_Linear_Expression (expr);
113     }
114
115   ppl_delete_Coefficient (coef);
116   value_clear (one);
117 }
118
119 /* All scattering matrices in SCOP will have the same number of scattering
120    dimensions.  */
121
122 int
123 unify_scattering_dimensions (scop_p scop)
124 {
125   int i;
126   poly_bb_p pbb;
127   graphite_dim_t max_scattering = 0;
128
129   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
130     max_scattering = MAX (pbb_nb_scattering_transform (pbb), max_scattering);
131
132   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
133     extend_scattering (pbb, max_scattering);
134
135   return max_scattering;
136 }
137
138 /* Prints to FILE the scattering function of PBB.  */
139
140 void
141 print_scattering_function (FILE *file, poly_bb_p pbb)
142 {
143   graphite_dim_t i;
144
145   if (!PBB_TRANSFORMED (pbb))
146     return;
147
148   fprintf (file, "scattering bb_%d (\n", pbb_index (pbb));
149   fprintf (file, "#  eq");
150
151   for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
152     fprintf (file, "     s%d", (int) i);
153
154   for (i = 0; i < pbb_nb_local_vars (pbb); i++)
155     fprintf (file, "    lv%d", (int) i);
156
157   for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
158     fprintf (file, "     i%d", (int) i);
159
160   for (i = 0; i < pbb_nb_params (pbb); i++)
161     fprintf (file, "     p%d", (int) i);
162
163   fprintf (file, "    cst\n");
164
165   ppl_print_polyhedron_matrix (file, PBB_TRANSFORMED_SCATTERING (pbb));
166
167   fprintf (file, ")\n");
168 }
169
170 /* Prints to FILE the iteration domain of PBB.  */
171
172 void
173 print_iteration_domain (FILE *file, poly_bb_p pbb)
174 {
175   print_pbb_domain (file, pbb);
176 }
177
178 /* Prints to FILE the scattering functions of every PBB of SCOP.  */
179
180 void
181 print_scattering_functions (FILE *file, scop_p scop)
182 {
183   int i;
184   poly_bb_p pbb;
185
186   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
187     print_scattering_function (file, pbb);
188 }
189
190 /* Prints to FILE the iteration domains of every PBB of SCOP.  */
191
192 void
193 print_iteration_domains (FILE *file, scop_p scop)
194 {
195   int i;
196   poly_bb_p pbb;
197
198   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
199     print_iteration_domain (file, pbb);
200 }
201
202 /* Prints to STDERR the scattering function of PBB.  */
203
204 void
205 debug_scattering_function (poly_bb_p pbb)
206 {
207   print_scattering_function (stderr, pbb);
208 }
209
210 /* Prints to STDERR the iteration domain of PBB.  */
211
212 void
213 debug_iteration_domain (poly_bb_p pbb)
214 {
215   print_iteration_domain (stderr, pbb);
216 }
217
218 /* Prints to STDERR the scattering functions of every PBB of SCOP.  */
219
220 void
221 debug_scattering_functions (scop_p scop)
222 {
223   print_scattering_functions (stderr, scop);
224 }
225
226 /* Prints to STDERR the iteration domains of every PBB of SCOP.  */
227
228 void
229 debug_iteration_domains (scop_p scop)
230 {
231   print_iteration_domains (stderr, scop);
232 }
233
234 /* Apply graphite transformations to all the basic blocks of SCOP.  */
235
236 bool
237 apply_poly_transforms (scop_p scop)
238 {
239   bool transform_done = false;
240
241   /* Generate code even if we did not apply any real transformation.
242      This also allows to check the performance for the identity
243      transformation: GIMPLE -> GRAPHITE -> GIMPLE
244      Keep in mind that CLooG optimizes in control, so the loop structure
245      may change, even if we only use -fgraphite-identity.  */
246   if (flag_graphite_identity)
247     transform_done = true;
248
249   if (flag_loop_parallelize_all)
250     transform_done = true;
251
252   if (flag_loop_block)
253     gcc_unreachable (); /* Not yet supported.  */
254
255   if (flag_loop_strip_mine)
256     transform_done |= scop_do_strip_mine (scop);
257
258   if (flag_loop_interchange)
259     transform_done |= scop_do_interchange (scop);
260
261   return transform_done;
262 }
263
264 /* Returns true when it PDR1 is a duplicate of PDR2: same PBB, and
265    their ACCESSES, TYPE, and NB_SUBSCRIPTS are the same.  */
266
267 static inline bool
268 can_collapse_pdrs (poly_dr_p pdr1, poly_dr_p pdr2)
269 {
270   bool res;
271   ppl_Pointset_Powerset_C_Polyhedron_t af1, af2, diff;
272
273   if (PDR_PBB (pdr1) != PDR_PBB (pdr2)
274       || PDR_NB_SUBSCRIPTS (pdr1) != PDR_NB_SUBSCRIPTS (pdr2)
275       || PDR_TYPE (pdr1) != PDR_TYPE (pdr2))
276     return false;
277
278   af1 = PDR_ACCESSES (pdr1);
279   af2 = PDR_ACCESSES (pdr2);
280   ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
281     (&diff, af1);
282   ppl_Pointset_Powerset_C_Polyhedron_difference_assign (diff, af2);
283
284   res = ppl_Pointset_Powerset_C_Polyhedron_is_empty (diff);
285   ppl_delete_Pointset_Powerset_C_Polyhedron (diff);
286   return res;
287 }
288
289 /* Removes duplicated data references in PBB.  */
290
291 void
292 pbb_remove_duplicate_pdrs (poly_bb_p pbb)
293 {
294   int i, j;
295   poly_dr_p pdr1, pdr2;
296   unsigned n = VEC_length (poly_dr_p, PBB_DRS (pbb));
297   VEC (poly_dr_p, heap) *collapsed = VEC_alloc (poly_dr_p, heap, n);
298
299   for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr1); i++)
300     for (j = 0; VEC_iterate (poly_dr_p, collapsed, j, pdr2); j++)
301       if (!can_collapse_pdrs (pdr1, pdr2))
302         VEC_quick_push (poly_dr_p, collapsed, pdr1);
303 }
304
305 /* Create a new polyhedral data reference and add it to PBB.  It is
306    defined by its ACCESSES, its TYPE, and the number of subscripts
307    NB_SUBSCRIPTS.  */
308
309 void
310 new_poly_dr (poly_bb_p pbb, int dr_base_object_set,
311              ppl_Pointset_Powerset_C_Polyhedron_t accesses,
312              enum poly_dr_type type, void *cdr, graphite_dim_t nb_subscripts)
313 {
314   static int id = 0;
315   poly_dr_p pdr = XNEW (struct poly_dr);
316
317   PDR_ID (pdr) = id++;
318   PDR_BASE_OBJECT_SET (pdr) = dr_base_object_set;
319   PDR_NB_REFS (pdr) = 1;
320   PDR_PBB (pdr) = pbb;
321   PDR_ACCESSES (pdr) = accesses;
322   PDR_TYPE (pdr) = type;
323   PDR_CDR (pdr) = cdr;
324   PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
325   VEC_safe_push (poly_dr_p, heap, PBB_DRS (pbb), pdr);
326 }
327
328 /* Free polyhedral data reference PDR.  */
329
330 void
331 free_poly_dr (poly_dr_p pdr)
332 {
333   ppl_delete_Pointset_Powerset_C_Polyhedron (PDR_ACCESSES (pdr));
334   XDELETE (pdr);
335 }
336
337 /* Create a new polyhedral black box.  */
338
339 void
340 new_poly_bb (scop_p scop, void *black_box, bool reduction)
341 {
342   poly_bb_p pbb = XNEW (struct poly_bb);
343
344   PBB_DOMAIN (pbb) = NULL;
345   PBB_SCOP (pbb) = scop;
346   pbb_set_black_box (pbb, black_box);
347   PBB_TRANSFORMED (pbb) = NULL;
348   PBB_SAVED (pbb) = NULL;
349   PBB_ORIGINAL (pbb) = NULL;
350   PBB_DRS (pbb) = VEC_alloc (poly_dr_p, heap, 3);
351   PBB_IS_REDUCTION (pbb) = reduction;
352   VEC_safe_push (poly_bb_p, heap, SCOP_BBS (scop), pbb);
353 }
354
355 /* Free polyhedral black box.  */
356
357 void
358 free_poly_bb (poly_bb_p pbb)
359 {
360   int i;
361   poly_dr_p pdr;
362
363   ppl_delete_Pointset_Powerset_C_Polyhedron (PBB_DOMAIN (pbb));
364
365   if (PBB_TRANSFORMED (pbb))
366     poly_scattering_free (PBB_TRANSFORMED (pbb));
367
368   if (PBB_SAVED (pbb))
369     poly_scattering_free (PBB_SAVED (pbb));
370
371   if (PBB_ORIGINAL (pbb))
372     poly_scattering_free (PBB_ORIGINAL (pbb));
373
374   if (PBB_DRS (pbb))
375     for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
376       free_poly_dr (pdr);
377
378   VEC_free (poly_dr_p, heap, PBB_DRS (pbb));
379   XDELETE (pbb);
380 }
381
382 static void
383 print_pdr_access_layout (FILE *file, poly_dr_p pdr)
384 {
385   graphite_dim_t i;
386
387   fprintf (file, "#  eq");
388
389   for (i = 0; i < pdr_dim_iter_domain (pdr); i++)
390     fprintf (file, "     i%d", (int) i);
391
392   for (i = 0; i < pdr_nb_params (pdr); i++)
393     fprintf (file, "     p%d", (int) i);
394
395   fprintf (file, "  alias");
396
397   for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
398     fprintf (file, "   sub%d", (int) i);
399
400   fprintf (file, "    cst\n");
401 }
402
403 /* Prints to FILE the polyhedral data reference PDR.  */
404
405 void
406 print_pdr (FILE *file, poly_dr_p pdr)
407 {
408   fprintf (file, "pdr_%d (", PDR_ID (pdr));
409
410   switch (PDR_TYPE (pdr))
411     {
412     case PDR_READ:
413       fprintf (file, "read \n");
414       break;
415
416     case PDR_WRITE:
417       fprintf (file, "write \n");
418       break;
419
420     case PDR_MAY_WRITE:
421       fprintf (file, "may_write \n");
422       break;
423
424     default:
425       gcc_unreachable ();
426     }
427
428   dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
429
430   fprintf (file, "data accesses (\n");
431   print_pdr_access_layout (file, pdr);
432   ppl_print_powerset_matrix (file, PDR_ACCESSES (pdr));
433   fprintf (file, ")\n");
434
435   fprintf (file, ")\n");
436 }
437
438 /* Prints to STDERR the polyhedral data reference PDR.  */
439
440 void
441 debug_pdr (poly_dr_p pdr)
442 {
443   print_pdr (stderr, pdr);
444 }
445
446 /* Creates a new SCOP containing REGION.  */
447
448 scop_p
449 new_scop (void *region)
450 {
451   scop_p scop = XNEW (struct scop);
452
453   SCOP_DEP_GRAPH (scop) = NULL;
454   SCOP_CONTEXT (scop) = NULL;
455   scop_set_region (scop, region);
456   SCOP_BBS (scop) = VEC_alloc (poly_bb_p, heap, 3);
457   SCOP_ORIGINAL_PDDRS (scop) = htab_create (10, hash_poly_ddr_p,
458                                             eq_poly_ddr_p, free_poly_ddr);
459   return scop;
460 }
461
462 /* Deletes SCOP.  */
463
464 void
465 free_scop (scop_p scop)
466 {
467   int i;
468   poly_bb_p pbb;
469
470   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
471     free_poly_bb (pbb);
472
473   VEC_free (poly_bb_p, heap, SCOP_BBS (scop));
474
475   if (SCOP_CONTEXT (scop))
476     ppl_delete_Pointset_Powerset_C_Polyhedron (SCOP_CONTEXT (scop));
477
478   htab_delete (SCOP_ORIGINAL_PDDRS (scop));
479   XDELETE (scop);
480 }
481
482 /* Print to FILE the domain of PBB.  */
483
484 void
485 print_pbb_domain (FILE *file, poly_bb_p pbb)
486 {
487   graphite_dim_t i;
488   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
489
490   if (!PBB_DOMAIN (pbb))
491     return;
492
493   fprintf (file, "domains bb_%d (\n", GBB_BB (gbb)->index);
494   fprintf (file, "#  eq");
495
496   for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
497     fprintf (file, "     i%d", (int) i);
498
499   for (i = 0; i < pbb_nb_params (pbb); i++)
500     fprintf (file, "     p%d", (int) i);
501
502   fprintf (file, "    cst\n");
503
504   if (PBB_DOMAIN (pbb))
505     ppl_print_powerset_matrix (file, PBB_DOMAIN (pbb));
506
507   fprintf (file, ")\n");
508 }
509
510 /* Dump the cases of a graphite basic block GBB on FILE.  */
511
512 static void
513 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
514 {
515   int i;
516   gimple stmt;
517   VEC (gimple, heap) *cases;
518
519   if (!gbb)
520     return;
521
522   cases = GBB_CONDITION_CASES (gbb);
523   if (VEC_empty (gimple, cases))
524     return;
525
526   fprintf (file, "cases bb_%d (", GBB_BB (gbb)->index);
527
528   for (i = 0; VEC_iterate (gimple, cases, i, stmt); i++)
529     print_gimple_stmt (file, stmt, 0, 0);
530
531   fprintf (file, ")\n");
532 }
533
534 /* Dump conditions of a graphite basic block GBB on FILE.  */
535
536 static void
537 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
538 {
539   int i;
540   gimple stmt;
541   VEC (gimple, heap) *conditions;
542
543   if (!gbb)
544     return;
545
546   conditions = GBB_CONDITIONS (gbb);
547   if (VEC_empty (gimple, conditions))
548     return;
549
550   fprintf (file, "conditions bb_%d (", GBB_BB (gbb)->index);
551
552   for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
553     print_gimple_stmt (file, stmt, 0, 0);
554
555   fprintf (file, ")\n");
556 }
557
558 /* Print to FILE all the data references of PBB.  */
559
560 void
561 print_pdrs (FILE *file, poly_bb_p pbb)
562 {
563   int i;
564   poly_dr_p pdr;
565
566   for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
567     print_pdr (file, pdr);
568 }
569
570 /* Print to STDERR all the data references of PBB.  */
571
572 void
573 debug_pdrs (poly_bb_p pbb)
574 {
575   print_pdrs (stderr, pbb);
576 }
577
578 /* Print to FILE the domain and scattering function of PBB.  */
579
580 void
581 print_pbb (FILE *file, poly_bb_p pbb)
582 {
583   fprintf (file, "pbb_%d (\n", pbb_index (pbb));
584   dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
585   dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
586   print_pdrs (file, pbb);
587   print_pbb_domain (file, pbb);
588   print_scattering_function (file, pbb);
589   fprintf (file, ")\n");
590 }
591
592 /* Print to FILE the parameters of SCOP.  */
593
594 void
595 print_scop_params (FILE *file, scop_p scop)
596 {
597   int i;
598   tree t;
599
600   fprintf (file, "parameters (\n");
601   for (i = 0; VEC_iterate (tree, SESE_PARAMS (SCOP_REGION (scop)), i, t); i++)
602     {
603       fprintf (file, "p_%d -> ", i);
604       print_generic_expr (file, t, 0);
605       fprintf (file, "\n");
606     }
607   fprintf (file, ")\n");
608 }
609
610 /* Print to FILE the context of SCoP.  */
611 void
612 print_scop_context (FILE *file, scop_p scop)
613 {
614   graphite_dim_t i;
615
616   fprintf (file, "context (\n");
617   fprintf (file, "#  eq");
618
619   for (i = 0; i < scop_nb_params (scop); i++)
620     fprintf (file, "     p%d", (int) i);
621
622   fprintf (file, "    cst\n");
623
624   if (SCOP_CONTEXT (scop))
625     ppl_print_powerset_matrix (file, SCOP_CONTEXT (scop));
626
627   fprintf (file, ")\n");
628 }
629
630 /* Print to FILE the SCOP.  */
631
632 void
633 print_scop (FILE *file, scop_p scop)
634 {
635   int i;
636   poly_bb_p pbb;
637
638   fprintf (file, "scop (\n");
639   print_scop_params (file, scop);
640   print_scop_context (file, scop);
641
642   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
643     print_pbb (file, pbb);
644
645   fprintf (file, ")\n");
646
647   fprintf (file, "original_lst (\n");
648   print_lst (file, SCOP_ORIGINAL_SCHEDULE (scop), 0);
649   fprintf (file, ")\n");
650
651   fprintf (file, "transformed_lst (\n");
652   print_lst (file, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
653   fprintf (file, ")\n");
654 }
655
656 /* Print to STDERR the domain of PBB.  */
657
658 void
659 debug_pbb_domain (poly_bb_p pbb)
660 {
661   print_pbb_domain (stderr, pbb);
662 }
663
664 /* Print to FILE the domain and scattering function of PBB.  */
665
666 void
667 debug_pbb (poly_bb_p pbb)
668 {
669   print_pbb (stderr, pbb);
670 }
671
672 /* Print to STDERR the context of SCOP.  */
673
674 void
675 debug_scop_context (scop_p scop)
676 {
677   print_scop_context (stderr, scop);
678 }
679
680 /* Print to STDERR the SCOP.  */
681
682 void
683 debug_scop (scop_p scop)
684 {
685   print_scop (stderr, scop);
686 }
687
688 /* Print to STDERR the parameters of SCOP.  */
689
690 void
691 debug_scop_params (scop_p scop)
692 {
693   print_scop_params (stderr, scop);
694 }
695
696
697 /* The dimension in the transformed scattering polyhedron of PBB
698    containing the scattering iterator for the loop at depth LOOP_DEPTH.  */
699
700 ppl_dimension_type
701 psct_scattering_dim_for_loop_depth (poly_bb_p pbb, graphite_dim_t loop_depth)
702 {
703   ppl_const_Constraint_System_t pcs;
704   ppl_Constraint_System_const_iterator_t cit, cend;
705   ppl_const_Constraint_t cstr;
706   ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
707   ppl_dimension_type iter = psct_iterator_dim (pbb, loop_depth);
708   ppl_Linear_Expression_t expr;
709   ppl_Coefficient_t coef;
710   Value val;
711   graphite_dim_t i;
712
713   value_init (val);
714   ppl_new_Coefficient (&coef);
715   ppl_Polyhedron_get_constraints (ph, &pcs);
716   ppl_new_Constraint_System_const_iterator (&cit);
717   ppl_new_Constraint_System_const_iterator (&cend);
718
719   for (ppl_Constraint_System_begin (pcs, cit),
720          ppl_Constraint_System_end (pcs, cend);
721        !ppl_Constraint_System_const_iterator_equal_test (cit, cend);
722        ppl_Constraint_System_const_iterator_increment (cit))
723     {
724       ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
725       ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
726       ppl_Linear_Expression_coefficient (expr, iter, coef);
727       ppl_Coefficient_to_mpz_t (coef, val);
728
729       if (value_zero_p (val))
730         {
731           ppl_delete_Linear_Expression (expr);
732           continue;
733         }
734
735       for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
736         {
737           ppl_dimension_type scatter = psct_scattering_dim (pbb, i);
738
739           ppl_Linear_Expression_coefficient (expr, scatter, coef);
740           ppl_Coefficient_to_mpz_t (coef, val);
741
742           if (value_notzero_p (val))
743             {
744               value_clear (val);
745               ppl_delete_Linear_Expression (expr);
746               ppl_delete_Coefficient (coef);
747               ppl_delete_Constraint_System_const_iterator (cit);
748               ppl_delete_Constraint_System_const_iterator (cend);
749
750               return scatter;
751             }
752         }
753     }
754
755   gcc_unreachable ();
756 }
757
758 /* Returns the number of iterations NITER of the loop around PBB at
759    depth LOOP_DEPTH.  */
760
761 void
762 pbb_number_of_iterations (poly_bb_p pbb,
763                           graphite_dim_t loop_depth,
764                           Value niter)
765 {
766   ppl_Linear_Expression_t le;
767   ppl_dimension_type dim;
768
769   ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
770   ppl_new_Linear_Expression_with_dimension (&le, dim);
771   ppl_set_coef (le, pbb_iterator_dim (pbb, loop_depth), 1);
772   value_set_si (niter, -1);
773   ppl_max_for_le_pointset (PBB_DOMAIN (pbb), le, niter);
774   ppl_delete_Linear_Expression (le);
775 }
776
777 /* Returns the number of iterations NITER of the loop around PBB at
778    time(scattering) dimension TIME_DEPTH.  */
779
780 void
781 pbb_number_of_iterations_at_time (poly_bb_p pbb,
782                                   graphite_dim_t time_depth,
783                                   Value niter)
784 {
785   ppl_Pointset_Powerset_C_Polyhedron_t ext_domain, sctr;
786   ppl_Linear_Expression_t le;
787   ppl_dimension_type dim;
788
789   value_set_si (niter, -1);
790
791   /* Takes together domain and scattering polyhedrons, and composes
792      them into the bigger polyhedron that has the following format:
793      t0..t_{n-1} | l0..l_{nlcl-1} | i0..i_{niter-1} | g0..g_{nparm-1}.
794      t0..t_{n-1} are time dimensions (scattering dimensions)
795      l0..l_{nclc-1} are local variables in scatterin function
796      i0..i_{niter-1} are original iteration variables
797      g0..g_{nparam-1} are global parameters.  */
798
799   ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
800     (&ext_domain, PBB_DOMAIN (pbb));
801   ppl_insert_dimensions_pointset (ext_domain, 0,
802                                   pbb_nb_scattering_transform (pbb)
803                                   + pbb_nb_local_vars (pbb));
804   ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&sctr,
805       PBB_TRANSFORMED_SCATTERING (pbb));
806   ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (sctr, ext_domain);
807
808   ppl_Pointset_Powerset_C_Polyhedron_space_dimension (sctr, &dim);
809   ppl_new_Linear_Expression_with_dimension (&le, dim);
810   ppl_set_coef (le, time_depth, 1);
811   ppl_max_for_le_pointset (sctr, le, niter);
812
813   ppl_delete_Linear_Expression (le);
814   ppl_delete_Pointset_Powerset_C_Polyhedron (sctr);
815   ppl_delete_Pointset_Powerset_C_Polyhedron (ext_domain);
816 }
817
818 /* Translates LOOP to LST.  */
819
820 static lst_p
821 loop_to_lst (loop_p loop, VEC (poly_bb_p, heap) *bbs, int *i)
822 {
823   poly_bb_p pbb;
824   VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
825
826   for (; VEC_iterate (poly_bb_p, bbs, *i, pbb); (*i)++)
827     {
828       lst_p stmt;
829       basic_block bb = GBB_BB (PBB_BLACK_BOX (pbb));
830
831       if (bb->loop_father == loop)
832         stmt = new_lst_stmt (pbb);
833       else
834         {
835           if (flow_bb_inside_loop_p (loop, bb))
836             stmt = loop_to_lst (loop->inner, bbs, i);
837           else
838             {
839               loop_p next = loop;
840
841               while ((next = next->next)
842                      && !flow_bb_inside_loop_p (next, bb));
843
844               if (!next)
845                 return new_lst_loop (seq);
846
847               stmt = loop_to_lst (next, bbs, i);
848             }
849         }
850
851       VEC_safe_push (lst_p, heap, seq, stmt);
852     }
853
854   return new_lst_loop (seq);
855 }
856
857 /* Reads the original scattering of the SCOP and returns an LST
858    representing it.  */
859
860 void
861 scop_to_lst (scop_p scop)
862 {
863   poly_bb_p pbb = VEC_index (poly_bb_p, SCOP_BBS (scop), 0);
864   loop_p loop = outermost_loop_in_sese (SCOP_REGION (scop), GBB_BB (PBB_BLACK_BOX (pbb)));
865   int i = 0;
866
867   SCOP_ORIGINAL_SCHEDULE (scop) = loop_to_lst (loop, SCOP_BBS (scop), &i);
868   SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (SCOP_ORIGINAL_SCHEDULE (scop));
869 }
870
871 /* Print LST to FILE with INDENT spaces of indentation.  */
872
873 void
874 print_lst (FILE *file, lst_p lst, int indent)
875 {
876   if (!lst)
877     return;
878
879   indent_to (file, indent);
880
881   if (LST_LOOP_P (lst))
882     {
883       int i;
884       lst_p l;
885
886       fprintf (file, "%d (loop", lst_dewey_number (lst));
887
888       for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
889         print_lst (file, l, indent + 2);
890
891       fprintf (file, ")");
892     }
893   else
894     fprintf (file, "%d stmt_%d", lst_dewey_number (lst), pbb_index (LST_PBB (lst)));
895 }
896
897 /* Print LST to STDERR.  */
898
899 void
900 debug_lst (lst_p lst)
901 {
902   print_lst (stderr, lst, 0);
903 }
904
905 #endif
906