Enable dumping of alias graphs.
[official-gcc/Ramakrishna.git] / gcc / graphite-poly.c
blob49a598f0856b2e67d39538f99b6c4356fb8a2096
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>.
6 This file is part of GCC.
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.
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.
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-scalar-evolution.h"
38 #include "tree-data-ref.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"
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"
55 /* Return the maximal loop depth in SCOP. */
57 int
58 scop_max_loop_depth (scop_p scop)
60 int i;
61 poly_bb_p pbb;
62 int max_nb_loops = 0;
64 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
66 int nb_loops = pbb_dim_iter_domain (pbb);
67 if (max_nb_loops < nb_loops)
68 max_nb_loops = nb_loops;
71 return max_nb_loops;
74 /* Extend the scattering matrix of PBB to MAX_SCATTERING scattering
75 dimensions. */
77 static void
78 extend_scattering (poly_bb_p pbb, int max_scattering)
80 ppl_dimension_type nb_old_dims, nb_new_dims;
81 int nb_added_dims, i;
82 ppl_Coefficient_t coef;
83 Value one;
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);
91 gcc_assert (nb_added_dims >= 0);
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;
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;
101 /* Add identity matrix for the added dimensions. */
102 for (i = max_scattering - nb_added_dims; i < max_scattering; i++)
104 ppl_Constraint_t cstr;
105 ppl_Linear_Expression_t expr;
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);
115 ppl_delete_Coefficient (coef);
116 value_clear (one);
119 /* All scattering matrices in SCOP will have the same number of scattering
120 dimensions. */
123 unify_scattering_dimensions (scop_p scop)
125 int i;
126 poly_bb_p pbb;
127 graphite_dim_t max_scattering = 0;
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);
132 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
133 extend_scattering (pbb, max_scattering);
135 return max_scattering;
138 /* Prints to FILE the scattering function of PBB. */
140 void
141 print_scattering_function (FILE *file, poly_bb_p pbb)
143 graphite_dim_t i;
145 if (!PBB_TRANSFORMED (pbb))
146 return;
148 fprintf (file, "scattering bb_%d (\n", pbb_index (pbb));
149 fprintf (file, "# eq");
151 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
152 fprintf (file, " s%d", (int) i);
154 for (i = 0; i < pbb_nb_local_vars (pbb); i++)
155 fprintf (file, " lv%d", (int) i);
157 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
158 fprintf (file, " i%d", (int) i);
160 for (i = 0; i < pbb_nb_params (pbb); i++)
161 fprintf (file, " p%d", (int) i);
163 fprintf (file, " cst\n");
165 ppl_print_polyhedron_matrix (file, PBB_TRANSFORMED_SCATTERING (pbb));
167 fprintf (file, ")\n");
170 /* Prints to FILE the iteration domain of PBB. */
172 void
173 print_iteration_domain (FILE *file, poly_bb_p pbb)
175 print_pbb_domain (file, pbb);
178 /* Prints to FILE the scattering functions of every PBB of SCOP. */
180 void
181 print_scattering_functions (FILE *file, scop_p scop)
183 int i;
184 poly_bb_p pbb;
186 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
187 print_scattering_function (file, pbb);
190 /* Prints to FILE the iteration domains of every PBB of SCOP. */
192 void
193 print_iteration_domains (FILE *file, scop_p scop)
195 int i;
196 poly_bb_p pbb;
198 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
199 print_iteration_domain (file, pbb);
202 /* Prints to STDERR the scattering function of PBB. */
204 void
205 debug_scattering_function (poly_bb_p pbb)
207 print_scattering_function (stderr, pbb);
210 /* Prints to STDERR the iteration domain of PBB. */
212 void
213 debug_iteration_domain (poly_bb_p pbb)
215 print_iteration_domain (stderr, pbb);
218 /* Prints to STDERR the scattering functions of every PBB of SCOP. */
220 void
221 debug_scattering_functions (scop_p scop)
223 print_scattering_functions (stderr, scop);
226 /* Prints to STDERR the iteration domains of every PBB of SCOP. */
228 void
229 debug_iteration_domains (scop_p scop)
231 print_iteration_domains (stderr, scop);
235 /* Write to file_name.graphite the transforms for SCOP. */
237 static void
238 graphite_write_transforms (scop_p scop)
240 print_scattering_functions (graphite_out_file, scop);
243 /* Read transforms from file_name.graphite and set the transforms on
244 SCOP. */
246 static bool
247 graphite_read_transforms (scop_p scop)
249 int i;
250 poly_bb_p pbb;
252 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
254 ppl_Polyhedron_t newp;
255 ppl_read_polyhedron_matrix (&newp, graphite_in_file);
256 PBB_TRANSFORMED (pbb) = poly_scattering_new ();
257 PBB_TRANSFORMED_SCATTERING (pbb) = newp;
260 return true;
263 /* Apply graphite transformations to all the basic blocks of SCOP. */
265 bool
266 apply_poly_transforms (scop_p scop)
268 bool transform_done = false;
270 if (flag_graphite_read)
272 transform_done |= graphite_read_transforms (scop);
273 gcc_assert (graphite_legal_transform (scop));
276 /* Generate code even if we did not apply any real transformation.
277 This also allows to check the performance for the identity
278 transformation: GIMPLE -> GRAPHITE -> GIMPLE
279 Keep in mind that CLooG optimizes in control, so the loop structure
280 may change, even if we only use -fgraphite-identity. */
281 if (flag_graphite_identity)
282 transform_done = true;
284 if (flag_loop_parallelize_all)
285 transform_done = true;
287 if (flag_loop_block)
289 transform_done |= scop_do_strip_mine (scop);
290 transform_done |= scop_do_interchange (scop);
292 else
294 if (flag_loop_strip_mine)
295 transform_done |= scop_do_strip_mine (scop);
297 if (flag_loop_interchange)
298 transform_done |= scop_do_interchange (scop);
301 if (flag_graphite_write)
302 graphite_write_transforms (scop);
304 return transform_done;
307 /* Returns true when it PDR1 is a duplicate of PDR2: same PBB, and
308 their ACCESSES, TYPE, and NB_SUBSCRIPTS are the same. */
310 static inline bool
311 can_collapse_pdrs (poly_dr_p pdr1, poly_dr_p pdr2)
313 bool res;
314 ppl_Pointset_Powerset_C_Polyhedron_t af1, af2, diff;
316 if (PDR_PBB (pdr1) != PDR_PBB (pdr2)
317 || PDR_NB_SUBSCRIPTS (pdr1) != PDR_NB_SUBSCRIPTS (pdr2)
318 || PDR_TYPE (pdr1) != PDR_TYPE (pdr2))
319 return false;
321 af1 = PDR_ACCESSES (pdr1);
322 af2 = PDR_ACCESSES (pdr2);
323 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
324 (&diff, af1);
325 ppl_Pointset_Powerset_C_Polyhedron_difference_assign (diff, af2);
327 res = ppl_Pointset_Powerset_C_Polyhedron_is_empty (diff);
328 ppl_delete_Pointset_Powerset_C_Polyhedron (diff);
329 return res;
332 /* Removes duplicated data references in PBB. */
334 void
335 pbb_remove_duplicate_pdrs (poly_bb_p pbb)
337 int i, j;
338 poly_dr_p pdr1, pdr2;
339 unsigned n = VEC_length (poly_dr_p, PBB_DRS (pbb));
340 VEC (poly_dr_p, heap) *collapsed = VEC_alloc (poly_dr_p, heap, n);
342 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr1); i++)
343 for (j = 0; VEC_iterate (poly_dr_p, collapsed, j, pdr2); j++)
344 if (!can_collapse_pdrs (pdr1, pdr2))
345 VEC_quick_push (poly_dr_p, collapsed, pdr1);
347 PBB_PDR_DUPLICATES_REMOVED (pbb) = true;
350 /* Create a new polyhedral data reference and add it to PBB. It is
351 defined by its ACCESSES, its TYPE, and the number of subscripts
352 NB_SUBSCRIPTS. */
354 void
355 new_poly_dr (poly_bb_p pbb, int dr_base_object_set,
356 ppl_Pointset_Powerset_C_Polyhedron_t accesses,
357 enum poly_dr_type type, void *cdr, graphite_dim_t nb_subscripts)
359 static int id = 0;
360 poly_dr_p pdr = XNEW (struct poly_dr);
362 PDR_ID (pdr) = id++;
363 PDR_BASE_OBJECT_SET (pdr) = dr_base_object_set;
364 PDR_NB_REFS (pdr) = 1;
365 PDR_PBB (pdr) = pbb;
366 PDR_ACCESSES (pdr) = accesses;
367 PDR_TYPE (pdr) = type;
368 PDR_CDR (pdr) = cdr;
369 PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
370 VEC_safe_push (poly_dr_p, heap, PBB_DRS (pbb), pdr);
373 /* Free polyhedral data reference PDR. */
375 void
376 free_poly_dr (poly_dr_p pdr)
378 ppl_delete_Pointset_Powerset_C_Polyhedron (PDR_ACCESSES (pdr));
379 XDELETE (pdr);
382 /* Create a new polyhedral black box. */
384 void
385 new_poly_bb (scop_p scop, void *black_box, bool reduction)
387 poly_bb_p pbb = XNEW (struct poly_bb);
389 PBB_DOMAIN (pbb) = NULL;
390 PBB_SCOP (pbb) = scop;
391 pbb_set_black_box (pbb, black_box);
392 PBB_TRANSFORMED (pbb) = NULL;
393 PBB_SAVED (pbb) = NULL;
394 PBB_ORIGINAL (pbb) = NULL;
395 PBB_DRS (pbb) = VEC_alloc (poly_dr_p, heap, 3);
396 PBB_IS_REDUCTION (pbb) = reduction;
397 PBB_PDR_DUPLICATES_REMOVED (pbb) = false;
398 VEC_safe_push (poly_bb_p, heap, SCOP_BBS (scop), pbb);
401 /* Free polyhedral black box. */
403 void
404 free_poly_bb (poly_bb_p pbb)
406 int i;
407 poly_dr_p pdr;
409 ppl_delete_Pointset_Powerset_C_Polyhedron (PBB_DOMAIN (pbb));
411 if (PBB_TRANSFORMED (pbb))
412 poly_scattering_free (PBB_TRANSFORMED (pbb));
414 if (PBB_SAVED (pbb))
415 poly_scattering_free (PBB_SAVED (pbb));
417 if (PBB_ORIGINAL (pbb))
418 poly_scattering_free (PBB_ORIGINAL (pbb));
420 if (PBB_DRS (pbb))
421 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
422 free_poly_dr (pdr);
424 VEC_free (poly_dr_p, heap, PBB_DRS (pbb));
425 XDELETE (pbb);
428 static void
429 print_pdr_access_layout (FILE *file, poly_dr_p pdr)
431 graphite_dim_t i;
433 fprintf (file, "# eq");
435 for (i = 0; i < pdr_dim_iter_domain (pdr); i++)
436 fprintf (file, " i%d", (int) i);
438 for (i = 0; i < pdr_nb_params (pdr); i++)
439 fprintf (file, " p%d", (int) i);
441 fprintf (file, " alias");
443 for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
444 fprintf (file, " sub%d", (int) i);
446 fprintf (file, " cst\n");
449 /* Prints to FILE the polyhedral data reference PDR. */
451 void
452 print_pdr (FILE *file, poly_dr_p pdr)
454 fprintf (file, "pdr_%d (", PDR_ID (pdr));
456 switch (PDR_TYPE (pdr))
458 case PDR_READ:
459 fprintf (file, "read \n");
460 break;
462 case PDR_WRITE:
463 fprintf (file, "write \n");
464 break;
466 case PDR_MAY_WRITE:
467 fprintf (file, "may_write \n");
468 break;
470 default:
471 gcc_unreachable ();
474 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
476 fprintf (file, "data accesses (\n");
477 print_pdr_access_layout (file, pdr);
478 ppl_print_powerset_matrix (file, PDR_ACCESSES (pdr));
479 fprintf (file, ")\n");
481 fprintf (file, ")\n");
484 /* Prints to STDERR the polyhedral data reference PDR. */
486 void
487 debug_pdr (poly_dr_p pdr)
489 print_pdr (stderr, pdr);
492 /* Creates a new SCOP containing REGION. */
494 scop_p
495 new_scop (void *region)
497 scop_p scop = XNEW (struct scop);
499 SCOP_CONTEXT (scop) = NULL;
500 scop_set_region (scop, region);
501 SCOP_BBS (scop) = VEC_alloc (poly_bb_p, heap, 3);
502 SCOP_ORIGINAL_PDDRS (scop) = htab_create (10, hash_poly_ddr_p,
503 eq_poly_ddr_p, free_poly_ddr);
504 SCOP_ORIGINAL_SCHEDULE (scop) = NULL;
505 SCOP_TRANSFORMED_SCHEDULE (scop) = NULL;
506 SCOP_SAVED_SCHEDULE (scop) = NULL;
507 return scop;
510 /* Deletes SCOP. */
512 void
513 free_scop (scop_p scop)
515 int i;
516 poly_bb_p pbb;
518 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
519 free_poly_bb (pbb);
521 VEC_free (poly_bb_p, heap, SCOP_BBS (scop));
523 if (SCOP_CONTEXT (scop))
524 ppl_delete_Pointset_Powerset_C_Polyhedron (SCOP_CONTEXT (scop));
526 htab_delete (SCOP_ORIGINAL_PDDRS (scop));
527 free_lst (SCOP_ORIGINAL_SCHEDULE (scop));
528 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
529 free_lst (SCOP_SAVED_SCHEDULE (scop));
530 XDELETE (scop);
533 /* Print to FILE the domain of PBB. */
535 void
536 print_pbb_domain (FILE *file, poly_bb_p pbb)
538 graphite_dim_t i;
539 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
541 if (!PBB_DOMAIN (pbb))
542 return;
544 fprintf (file, "domains bb_%d (\n", GBB_BB (gbb)->index);
545 fprintf (file, "# eq");
547 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
548 fprintf (file, " i%d", (int) i);
550 for (i = 0; i < pbb_nb_params (pbb); i++)
551 fprintf (file, " p%d", (int) i);
553 fprintf (file, " cst\n");
555 if (PBB_DOMAIN (pbb))
556 ppl_print_powerset_matrix (file, PBB_DOMAIN (pbb));
558 fprintf (file, ")\n");
561 /* Dump the cases of a graphite basic block GBB on FILE. */
563 static void
564 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
566 int i;
567 gimple stmt;
568 VEC (gimple, heap) *cases;
570 if (!gbb)
571 return;
573 cases = GBB_CONDITION_CASES (gbb);
574 if (VEC_empty (gimple, cases))
575 return;
577 fprintf (file, "cases bb_%d (", GBB_BB (gbb)->index);
579 for (i = 0; VEC_iterate (gimple, cases, i, stmt); i++)
580 print_gimple_stmt (file, stmt, 0, 0);
582 fprintf (file, ")\n");
585 /* Dump conditions of a graphite basic block GBB on FILE. */
587 static void
588 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
590 int i;
591 gimple stmt;
592 VEC (gimple, heap) *conditions;
594 if (!gbb)
595 return;
597 conditions = GBB_CONDITIONS (gbb);
598 if (VEC_empty (gimple, conditions))
599 return;
601 fprintf (file, "conditions bb_%d (", GBB_BB (gbb)->index);
603 for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
604 print_gimple_stmt (file, stmt, 0, 0);
606 fprintf (file, ")\n");
609 /* Print to FILE all the data references of PBB. */
611 void
612 print_pdrs (FILE *file, poly_bb_p pbb)
614 int i;
615 poly_dr_p pdr;
617 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
618 print_pdr (file, pdr);
621 /* Print to STDERR all the data references of PBB. */
623 void
624 debug_pdrs (poly_bb_p pbb)
626 print_pdrs (stderr, pbb);
629 /* Print to FILE the domain and scattering function of PBB. */
631 void
632 print_pbb (FILE *file, poly_bb_p pbb)
634 fprintf (file, "pbb_%d (\n", pbb_index (pbb));
635 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
636 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
637 print_pdrs (file, pbb);
638 print_pbb_domain (file, pbb);
639 print_scattering_function (file, pbb);
640 fprintf (file, ")\n");
643 /* Print to FILE the parameters of SCOP. */
645 void
646 print_scop_params (FILE *file, scop_p scop)
648 int i;
649 tree t;
651 fprintf (file, "parameters (\n");
652 for (i = 0; VEC_iterate (tree, SESE_PARAMS (SCOP_REGION (scop)), i, t); i++)
654 fprintf (file, "p_%d -> ", i);
655 print_generic_expr (file, t, 0);
656 fprintf (file, "\n");
658 fprintf (file, ")\n");
661 /* Print to FILE the context of SCoP. */
662 void
663 print_scop_context (FILE *file, scop_p scop)
665 graphite_dim_t i;
667 fprintf (file, "context (\n");
668 fprintf (file, "# eq");
670 for (i = 0; i < scop_nb_params (scop); i++)
671 fprintf (file, " p%d", (int) i);
673 fprintf (file, " cst\n");
675 if (SCOP_CONTEXT (scop))
676 ppl_print_powerset_matrix (file, SCOP_CONTEXT (scop));
678 fprintf (file, ")\n");
681 /* Print to FILE the SCOP. */
683 void
684 print_scop (FILE *file, scop_p scop)
686 int i;
687 poly_bb_p pbb;
689 fprintf (file, "scop (\n");
690 print_scop_params (file, scop);
691 print_scop_context (file, scop);
693 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
694 print_pbb (file, pbb);
696 fprintf (file, "original_lst (\n");
697 print_lst (file, SCOP_ORIGINAL_SCHEDULE (scop), 0);
698 fprintf (file, ")\n");
700 fprintf (file, "transformed_lst (\n");
701 print_lst (file, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
702 fprintf (file, ")\n");
704 fprintf (file, ")\n");
707 /* Print to STDERR the domain of PBB. */
709 void
710 debug_pbb_domain (poly_bb_p pbb)
712 print_pbb_domain (stderr, pbb);
715 /* Print to FILE the domain and scattering function of PBB. */
717 void
718 debug_pbb (poly_bb_p pbb)
720 print_pbb (stderr, pbb);
723 /* Print to STDERR the context of SCOP. */
725 void
726 debug_scop_context (scop_p scop)
728 print_scop_context (stderr, scop);
731 /* Print to STDERR the SCOP. */
733 void
734 debug_scop (scop_p scop)
736 print_scop (stderr, scop);
739 /* Print to STDERR the parameters of SCOP. */
741 void
742 debug_scop_params (scop_p scop)
744 print_scop_params (stderr, scop);
748 /* The dimension in the transformed scattering polyhedron of PBB
749 containing the scattering iterator for the loop at depth LOOP_DEPTH. */
751 ppl_dimension_type
752 psct_scattering_dim_for_loop_depth (poly_bb_p pbb, graphite_dim_t loop_depth)
754 ppl_const_Constraint_System_t pcs;
755 ppl_Constraint_System_const_iterator_t cit, cend;
756 ppl_const_Constraint_t cstr;
757 ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
758 ppl_dimension_type iter = psct_iterator_dim (pbb, loop_depth);
759 ppl_Linear_Expression_t expr;
760 ppl_Coefficient_t coef;
761 Value val;
762 graphite_dim_t i;
764 value_init (val);
765 ppl_new_Coefficient (&coef);
766 ppl_Polyhedron_get_constraints (ph, &pcs);
767 ppl_new_Constraint_System_const_iterator (&cit);
768 ppl_new_Constraint_System_const_iterator (&cend);
770 for (ppl_Constraint_System_begin (pcs, cit),
771 ppl_Constraint_System_end (pcs, cend);
772 !ppl_Constraint_System_const_iterator_equal_test (cit, cend);
773 ppl_Constraint_System_const_iterator_increment (cit))
775 ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
776 ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
777 ppl_Linear_Expression_coefficient (expr, iter, coef);
778 ppl_Coefficient_to_mpz_t (coef, val);
780 if (value_zero_p (val))
782 ppl_delete_Linear_Expression (expr);
783 continue;
786 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
788 ppl_dimension_type scatter = psct_scattering_dim (pbb, i);
790 ppl_Linear_Expression_coefficient (expr, scatter, coef);
791 ppl_Coefficient_to_mpz_t (coef, val);
793 if (value_notzero_p (val))
795 value_clear (val);
796 ppl_delete_Linear_Expression (expr);
797 ppl_delete_Coefficient (coef);
798 ppl_delete_Constraint_System_const_iterator (cit);
799 ppl_delete_Constraint_System_const_iterator (cend);
801 return scatter;
806 gcc_unreachable ();
809 /* Returns the number of iterations NITER of the loop around PBB at
810 depth LOOP_DEPTH. */
812 void
813 pbb_number_of_iterations (poly_bb_p pbb,
814 graphite_dim_t loop_depth,
815 Value niter)
817 ppl_Linear_Expression_t le;
818 ppl_dimension_type dim;
820 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
821 ppl_new_Linear_Expression_with_dimension (&le, dim);
822 ppl_set_coef (le, pbb_iterator_dim (pbb, loop_depth), 1);
823 value_set_si (niter, -1);
824 ppl_max_for_le_pointset (PBB_DOMAIN (pbb), le, niter);
825 ppl_delete_Linear_Expression (le);
828 /* Returns the number of iterations NITER of the loop around PBB at
829 time(scattering) dimension TIME_DEPTH. */
831 void
832 pbb_number_of_iterations_at_time (poly_bb_p pbb,
833 graphite_dim_t time_depth,
834 Value niter)
836 ppl_Pointset_Powerset_C_Polyhedron_t ext_domain, sctr;
837 ppl_Linear_Expression_t le;
838 ppl_dimension_type dim;
840 /* Takes together domain and scattering polyhedrons, and composes
841 them into the bigger polyhedron that has the following format:
843 t0..t_{n-1} | l0..l_{nlcl-1} | i0..i_{niter-1} | g0..g_{nparm-1}
845 where
846 | t0..t_{n-1} are time dimensions (scattering dimensions)
847 | l0..l_{nclc-1} are local variables in scattering function
848 | i0..i_{niter-1} are original iteration variables
849 | g0..g_{nparam-1} are global parameters. */
851 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&sctr,
852 PBB_TRANSFORMED_SCATTERING (pbb));
854 /* Extend the iteration domain with the scattering dimensions:
855 0..0 | 0..0 | i0..i_{niter-1} | g0..g_{nparm-1}. */
856 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
857 (&ext_domain, PBB_DOMAIN (pbb));
858 ppl_insert_dimensions_pointset (ext_domain, 0,
859 pbb_nb_scattering_transform (pbb)
860 + pbb_nb_local_vars (pbb));
862 /* Add to sctr the extended domain. */
863 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (sctr, ext_domain);
865 /* Extract the number of iterations. */
866 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (sctr, &dim);
867 ppl_new_Linear_Expression_with_dimension (&le, dim);
868 ppl_set_coef (le, time_depth, 1);
869 value_set_si (niter, -1);
870 ppl_max_for_le_pointset (sctr, le, niter);
872 ppl_delete_Linear_Expression (le);
873 ppl_delete_Pointset_Powerset_C_Polyhedron (sctr);
874 ppl_delete_Pointset_Powerset_C_Polyhedron (ext_domain);
877 /* Translates LOOP to LST. */
879 static lst_p
880 loop_to_lst (loop_p loop, VEC (poly_bb_p, heap) *bbs, int *i)
882 poly_bb_p pbb;
883 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
885 for (; VEC_iterate (poly_bb_p, bbs, *i, pbb); (*i)++)
887 lst_p stmt;
888 basic_block bb = GBB_BB (PBB_BLACK_BOX (pbb));
890 if (bb->loop_father == loop)
891 stmt = new_lst_stmt (pbb);
892 else if (flow_bb_inside_loop_p (loop, bb))
894 loop_p next = loop->inner;
896 while (next && !flow_bb_inside_loop_p (next, bb))
897 next = next->next;
899 stmt = loop_to_lst (next, bbs, i);
901 else
903 (*i)--;
904 return new_lst_loop (seq);
907 VEC_safe_push (lst_p, heap, seq, stmt);
910 return new_lst_loop (seq);
913 /* Reads the original scattering of the SCOP and returns an LST
914 representing it. */
916 void
917 scop_to_lst (scop_p scop)
919 lst_p res;
920 int i, n = VEC_length (poly_bb_p, SCOP_BBS (scop));
921 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
922 sese region = SCOP_REGION (scop);
924 for (i = 0; i < n; i++)
926 poly_bb_p pbb = VEC_index (poly_bb_p, SCOP_BBS (scop), i);
927 loop_p loop = outermost_loop_in_sese (region, GBB_BB (PBB_BLACK_BOX (pbb)));
929 if (loop_in_sese_p (loop, region))
930 res = loop_to_lst (loop, SCOP_BBS (scop), &i);
931 else
932 res = new_lst_stmt (pbb);
934 VEC_safe_push (lst_p, heap, seq, res);
937 res = new_lst_loop (seq);
938 SCOP_ORIGINAL_SCHEDULE (scop) = res;
939 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (res);
942 /* Print LST to FILE with INDENT spaces of indentation. */
944 void
945 print_lst (FILE *file, lst_p lst, int indent)
947 if (!lst)
948 return;
950 indent_to (file, indent);
952 if (LST_LOOP_P (lst))
954 int i;
955 lst_p l;
957 if (LST_LOOP_FATHER (lst))
958 fprintf (file, "%d (loop", lst_dewey_number (lst));
959 else
960 fprintf (file, "(root");
962 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
963 print_lst (file, l, indent + 2);
965 fprintf (file, ")");
967 else
968 fprintf (file, "%d stmt_%d", lst_dewey_number (lst), pbb_index (LST_PBB (lst)));
971 /* Print LST to STDERR. */
973 void
974 debug_lst (lst_p lst)
976 print_lst (stderr, lst, 0);
979 /* Pretty print to FILE the loop statement tree LST in DOT format. */
981 static void
982 dot_lst_1 (FILE *file, lst_p lst)
984 if (!lst)
985 return;
987 if (LST_LOOP_P (lst))
989 int i;
990 lst_p l;
992 if (!LST_LOOP_FATHER (lst))
993 fprintf (file, "L -> L_%d_%d\n",
994 lst_depth (lst),
995 lst_dewey_number (lst));
996 else
997 fprintf (file, "L_%d_%d -> L_%d_%d\n",
998 lst_depth (LST_LOOP_FATHER (lst)),
999 lst_dewey_number (LST_LOOP_FATHER (lst)),
1000 lst_depth (lst),
1001 lst_dewey_number (lst));
1003 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
1004 dot_lst_1 (file, l);
1007 else
1008 fprintf (file, "L_%d_%d -> S_%d\n",
1009 lst_depth (LST_LOOP_FATHER (lst)),
1010 lst_dewey_number (LST_LOOP_FATHER (lst)),
1011 pbb_index (LST_PBB (lst)));
1015 /* Display the LST using dotty. */
1017 void
1018 dot_lst (lst_p lst)
1020 /* When debugging, enable the following code. This cannot be used
1021 in production compilers because it calls "system". */
1022 #if 1
1023 int x;
1024 FILE *stream = fopen ("/tmp/lst.dot", "w");
1025 gcc_assert (stream);
1027 fputs ("digraph all {\n", stream);
1028 dot_lst_1 (stream, lst);
1029 fputs ("}\n\n", stream);
1030 fclose (stream);
1032 x = system ("dotty /tmp/lst.dot");
1033 #else
1034 fputs ("digraph all {\n", stderr);
1035 dot_deps_1 (stderr, lst);
1036 fputs ("}\n\n", stderr);
1038 #endif
1041 #endif