rephrase text
[official-gcc.git] / gcc / graphite-poly.c
blob86ef48594ced3e731b73472b522783f259cb1a1f
1 /* Graphite polyhedral representation.
2 Copyright (C) 2009, 2010 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-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"
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, at some VERBOSITY
139 level. */
141 static void
142 print_scattering_function_1 (FILE *file, poly_bb_p pbb, int verbosity)
144 graphite_dim_t i;
146 if (verbosity > 0)
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");
166 /* Number of disjunct components. Remove this when
167 PBB_TRANSFORMED_SCATTERING will be a pointset_powerset. */
168 fprintf (file, "1\n");
169 ppl_print_polyhedron_matrix (file, PBB_TRANSFORMED_SCATTERING (pbb)
170 ? PBB_TRANSFORMED_SCATTERING (pbb)
171 : PBB_ORIGINAL_SCATTERING (pbb));
173 if (verbosity > 0)
174 fprintf (file, "#)\n");
177 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
178 level. */
180 void
181 print_scattering_function (FILE *file, poly_bb_p pbb, int verbosity)
183 if (!PBB_TRANSFORMED (pbb))
184 return;
186 if (PBB_TRANSFORMED_SCATTERING (pbb)
187 || PBB_ORIGINAL_SCATTERING (pbb))
189 if (verbosity > 0)
190 fprintf (file, "# Scattering function is provided\n");
192 fprintf (file, "1\n");
194 else
196 if (verbosity > 0)
197 fprintf (file, "# Scattering function is not provided\n");
199 fprintf (file, "0\n");
200 return;
203 print_scattering_function_1 (file, pbb, verbosity);
206 /* Prints to FILE the iteration domain of PBB, at some VERBOSITY
207 level. */
209 void
210 print_iteration_domain (FILE *file, poly_bb_p pbb, int verbosity)
212 print_pbb_domain (file, pbb, verbosity);
215 /* Prints to FILE the scattering functions of every PBB of SCOP. */
217 void
218 print_scattering_functions (FILE *file, scop_p scop, int verbosity)
220 int i;
221 poly_bb_p pbb;
223 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
224 print_scattering_function (file, pbb, verbosity);
227 /* Prints to FILE the iteration domains of every PBB of SCOP, at some
228 VERBOSITY level. */
230 void
231 print_iteration_domains (FILE *file, scop_p scop, int verbosity)
233 int i;
234 poly_bb_p pbb;
236 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
237 print_iteration_domain (file, pbb, verbosity);
240 /* Prints to STDERR the scattering function of PBB, at some VERBOSITY
241 level. */
243 void
244 debug_scattering_function (poly_bb_p pbb, int verbosity)
246 print_scattering_function (stderr, pbb, verbosity);
249 /* Prints to STDERR the iteration domain of PBB, at some VERBOSITY
250 level. */
252 void
253 debug_iteration_domain (poly_bb_p pbb, int verbosity)
255 print_iteration_domain (stderr, pbb, verbosity);
258 /* Prints to STDERR the scattering functions of every PBB of SCOP, at
259 some VERBOSITY level. */
261 void
262 debug_scattering_functions (scop_p scop, int verbosity)
264 print_scattering_functions (stderr, scop, verbosity);
267 /* Prints to STDERR the iteration domains of every PBB of SCOP, at
268 some VERBOSITY level. */
270 void
271 debug_iteration_domains (scop_p scop, int verbosity)
273 print_iteration_domains (stderr, scop, verbosity);
277 /* Apply graphite transformations to all the basic blocks of SCOP. */
279 bool
280 apply_poly_transforms (scop_p scop)
282 bool transform_done = false;
284 /* Generate code even if we did not apply any real transformation.
285 This also allows to check the performance for the identity
286 transformation: GIMPLE -> GRAPHITE -> GIMPLE
287 Keep in mind that CLooG optimizes in control, so the loop structure
288 may change, even if we only use -fgraphite-identity. */
289 if (flag_graphite_identity)
290 transform_done = true;
292 if (flag_loop_parallelize_all)
293 transform_done = true;
295 if (flag_loop_block)
296 transform_done |= scop_do_block (scop);
297 else
299 if (flag_loop_strip_mine)
300 transform_done |= scop_do_strip_mine (scop);
302 if (flag_loop_interchange)
303 transform_done |= scop_do_interchange (scop);
306 return transform_done;
309 /* Returns true when it PDR1 is a duplicate of PDR2: same PBB, and
310 their ACCESSES, TYPE, and NB_SUBSCRIPTS are the same. */
312 static inline bool
313 can_collapse_pdrs (poly_dr_p pdr1, poly_dr_p pdr2)
315 bool res;
316 ppl_Pointset_Powerset_C_Polyhedron_t af1, af2, diff;
318 if (PDR_PBB (pdr1) != PDR_PBB (pdr2)
319 || PDR_NB_SUBSCRIPTS (pdr1) != PDR_NB_SUBSCRIPTS (pdr2)
320 || PDR_TYPE (pdr1) != PDR_TYPE (pdr2))
321 return false;
323 af1 = PDR_ACCESSES (pdr1);
324 af2 = PDR_ACCESSES (pdr2);
325 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
326 (&diff, af1);
327 ppl_Pointset_Powerset_C_Polyhedron_difference_assign (diff, af2);
329 res = ppl_Pointset_Powerset_C_Polyhedron_is_empty (diff);
330 ppl_delete_Pointset_Powerset_C_Polyhedron (diff);
331 return res;
334 /* Removes duplicated data references in PBB. */
336 void
337 pbb_remove_duplicate_pdrs (poly_bb_p pbb)
339 int i, j;
340 poly_dr_p pdr1, pdr2;
341 unsigned n = VEC_length (poly_dr_p, PBB_DRS (pbb));
342 VEC (poly_dr_p, heap) *collapsed = VEC_alloc (poly_dr_p, heap, n);
344 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr1); i++)
345 for (j = 0; VEC_iterate (poly_dr_p, collapsed, j, pdr2); j++)
346 if (!can_collapse_pdrs (pdr1, pdr2))
347 VEC_quick_push (poly_dr_p, collapsed, pdr1);
349 VEC_free (poly_dr_p, heap, collapsed);
350 PBB_PDR_DUPLICATES_REMOVED (pbb) = true;
353 /* Create a new polyhedral data reference and add it to PBB. It is
354 defined by its ACCESSES, its TYPE, and the number of subscripts
355 NB_SUBSCRIPTS. */
357 void
358 new_poly_dr (poly_bb_p pbb, int dr_base_object_set,
359 ppl_Pointset_Powerset_C_Polyhedron_t accesses,
360 enum poly_dr_type type, void *cdr, graphite_dim_t nb_subscripts)
362 static int id = 0;
363 poly_dr_p pdr = XNEW (struct poly_dr);
365 PDR_ID (pdr) = id++;
366 PDR_BASE_OBJECT_SET (pdr) = dr_base_object_set;
367 PDR_NB_REFS (pdr) = 1;
368 PDR_PBB (pdr) = pbb;
369 PDR_ACCESSES (pdr) = accesses;
370 PDR_TYPE (pdr) = type;
371 PDR_CDR (pdr) = cdr;
372 PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
373 VEC_safe_push (poly_dr_p, heap, PBB_DRS (pbb), pdr);
376 /* Free polyhedral data reference PDR. */
378 void
379 free_poly_dr (poly_dr_p pdr)
381 ppl_delete_Pointset_Powerset_C_Polyhedron (PDR_ACCESSES (pdr));
382 XDELETE (pdr);
385 /* Create a new polyhedral black box. */
387 void
388 new_poly_bb (scop_p scop, void *black_box, bool reduction)
390 poly_bb_p pbb = XNEW (struct poly_bb);
392 PBB_DOMAIN (pbb) = NULL;
393 PBB_SCOP (pbb) = scop;
394 pbb_set_black_box (pbb, black_box);
395 PBB_TRANSFORMED (pbb) = NULL;
396 PBB_SAVED (pbb) = NULL;
397 PBB_ORIGINAL (pbb) = NULL;
398 PBB_DRS (pbb) = VEC_alloc (poly_dr_p, heap, 3);
399 PBB_IS_REDUCTION (pbb) = reduction;
400 PBB_PDR_DUPLICATES_REMOVED (pbb) = false;
401 VEC_safe_push (poly_bb_p, heap, SCOP_BBS (scop), pbb);
404 /* Free polyhedral black box. */
406 void
407 free_poly_bb (poly_bb_p pbb)
409 int i;
410 poly_dr_p pdr;
412 ppl_delete_Pointset_Powerset_C_Polyhedron (PBB_DOMAIN (pbb));
414 if (PBB_TRANSFORMED (pbb))
415 poly_scattering_free (PBB_TRANSFORMED (pbb));
417 if (PBB_SAVED (pbb))
418 poly_scattering_free (PBB_SAVED (pbb));
420 if (PBB_ORIGINAL (pbb))
421 poly_scattering_free (PBB_ORIGINAL (pbb));
423 if (PBB_DRS (pbb))
424 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
425 free_poly_dr (pdr);
427 VEC_free (poly_dr_p, heap, PBB_DRS (pbb));
428 XDELETE (pbb);
431 static void
432 print_pdr_access_layout (FILE *file, poly_dr_p pdr)
434 graphite_dim_t i;
436 fprintf (file, "# eq");
438 for (i = 0; i < pdr_dim_iter_domain (pdr); i++)
439 fprintf (file, " i%d", (int) i);
441 for (i = 0; i < pdr_nb_params (pdr); i++)
442 fprintf (file, " p%d", (int) i);
444 fprintf (file, " alias");
446 for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
447 fprintf (file, " sub%d", (int) i);
449 fprintf (file, " cst\n");
452 /* Prints to FILE the polyhedral data reference PDR, at some VERBOSITY
453 level. */
455 void
456 print_pdr (FILE *file, poly_dr_p pdr, int verbosity)
458 if (verbosity > 1)
460 fprintf (file, "# pdr_%d (", PDR_ID (pdr));
462 switch (PDR_TYPE (pdr))
464 case PDR_READ:
465 fprintf (file, "read \n");
466 break;
468 case PDR_WRITE:
469 fprintf (file, "write \n");
470 break;
472 case PDR_MAY_WRITE:
473 fprintf (file, "may_write \n");
474 break;
476 default:
477 gcc_unreachable ();
480 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
483 if (verbosity > 0)
485 fprintf (file, "# data accesses (\n");
486 print_pdr_access_layout (file, pdr);
489 ppl_print_powerset_matrix (file, PDR_ACCESSES (pdr));
491 if (verbosity > 0)
492 fprintf (file, "#)\n");
494 if (verbosity > 1)
495 fprintf (file, "#)\n");
498 /* Prints to STDERR the polyhedral data reference PDR, at some
499 VERBOSITY level. */
501 void
502 debug_pdr (poly_dr_p pdr, int verbosity)
504 print_pdr (stderr, pdr, verbosity);
507 /* Creates a new SCOP containing REGION. */
509 scop_p
510 new_scop (void *region)
512 scop_p scop = XNEW (struct scop);
514 SCOP_CONTEXT (scop) = NULL;
515 scop_set_region (scop, region);
516 SCOP_BBS (scop) = VEC_alloc (poly_bb_p, heap, 3);
517 SCOP_ORIGINAL_PDDRS (scop) = htab_create (10, hash_poly_ddr_p,
518 eq_poly_ddr_p, free_poly_ddr);
519 SCOP_ORIGINAL_SCHEDULE (scop) = NULL;
520 SCOP_TRANSFORMED_SCHEDULE (scop) = NULL;
521 SCOP_SAVED_SCHEDULE (scop) = NULL;
522 POLY_SCOP_P (scop) = false;
524 return scop;
527 /* Deletes SCOP. */
529 void
530 free_scop (scop_p scop)
532 int i;
533 poly_bb_p pbb;
535 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
536 free_poly_bb (pbb);
538 VEC_free (poly_bb_p, heap, SCOP_BBS (scop));
540 if (SCOP_CONTEXT (scop))
541 ppl_delete_Pointset_Powerset_C_Polyhedron (SCOP_CONTEXT (scop));
543 htab_delete (SCOP_ORIGINAL_PDDRS (scop));
544 free_lst (SCOP_ORIGINAL_SCHEDULE (scop));
545 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
546 free_lst (SCOP_SAVED_SCHEDULE (scop));
547 XDELETE (scop);
550 /* Print to FILE the domain of PBB, at some VERBOSITY level. */
552 void
553 print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity)
555 graphite_dim_t i;
556 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
558 if (!PBB_DOMAIN (pbb))
559 return;
561 if (verbosity > 0)
563 fprintf (file, "# Iteration domain of bb_%d (\n", GBB_BB (gbb)->index);
564 fprintf (file, "# eq");
566 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
567 fprintf (file, " i%d", (int) i);
569 for (i = 0; i < pbb_nb_params (pbb); i++)
570 fprintf (file, " p%d", (int) i);
572 fprintf (file, " cst\n");
575 if (PBB_DOMAIN (pbb))
576 ppl_print_powerset_matrix (file, PBB_DOMAIN (pbb));
577 else
578 fprintf (file, "0\n");
580 if (verbosity > 0)
581 fprintf (file, "#)\n");
584 /* Dump the cases of a graphite basic block GBB on FILE. */
586 static void
587 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
589 int i;
590 gimple stmt;
591 VEC (gimple, heap) *cases;
593 if (!gbb)
594 return;
596 cases = GBB_CONDITION_CASES (gbb);
597 if (VEC_empty (gimple, cases))
598 return;
600 fprintf (file, "# cases bb_%d (\n", GBB_BB (gbb)->index);
602 for (i = 0; VEC_iterate (gimple, cases, i, stmt); i++)
604 fprintf (file, "# ");
605 print_gimple_stmt (file, stmt, 0, 0);
608 fprintf (file, "#)\n");
611 /* Dump conditions of a graphite basic block GBB on FILE. */
613 static void
614 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
616 int i;
617 gimple stmt;
618 VEC (gimple, heap) *conditions;
620 if (!gbb)
621 return;
623 conditions = GBB_CONDITIONS (gbb);
624 if (VEC_empty (gimple, conditions))
625 return;
627 fprintf (file, "# conditions bb_%d (\n", GBB_BB (gbb)->index);
629 for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
631 fprintf (file, "# ");
632 print_gimple_stmt (file, stmt, 0, 0);
635 fprintf (file, "#)\n");
638 /* Print to FILE all the data references of PBB, at some VERBOSITY
639 level. */
641 void
642 print_pdrs (FILE *file, poly_bb_p pbb, int verbosity)
644 int i;
645 poly_dr_p pdr;
646 int nb_reads = 0;
647 int nb_writes = 0;
649 if (VEC_length (poly_dr_p, PBB_DRS (pbb)) == 0)
651 if (verbosity > 0)
652 fprintf (file, "# Access informations are not provided\n");\
653 fprintf (file, "0\n");
654 return;
657 if (verbosity > 1)
658 fprintf (file, "# Data references (\n");
660 if (verbosity > 0)
661 fprintf (file, "# Access informations are provided\n");
662 fprintf (file, "1\n");
664 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
665 if (PDR_TYPE (pdr) == PDR_READ)
666 nb_reads++;
667 else
668 nb_writes++;
670 if (verbosity > 1)
671 fprintf (file, "# Read data references (\n");
673 if (verbosity > 0)
674 fprintf (file, "# Read access informations\n");
675 fprintf (file, "%d\n", nb_reads);
677 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
678 if (PDR_TYPE (pdr) == PDR_READ)
679 print_pdr (file, pdr, verbosity);
681 if (verbosity > 1)
682 fprintf (file, "#)\n");
684 if (verbosity > 1)
685 fprintf (file, "# Write data references (\n");
687 if (verbosity > 0)
688 fprintf (file, "# Write access informations\n");
689 fprintf (file, "%d\n", nb_writes);
691 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
692 if (PDR_TYPE (pdr) != PDR_READ)
693 print_pdr (file, pdr, verbosity);
695 if (verbosity > 1)
696 fprintf (file, "#)\n");
698 if (verbosity > 1)
699 fprintf (file, "#)\n");
702 /* Print to STDERR all the data references of PBB. */
704 void
705 debug_pdrs (poly_bb_p pbb, int verbosity)
707 print_pdrs (stderr, pbb, verbosity);
710 /* Print to FILE the body of PBB, at some VERBOSITY level. */
712 static void
713 print_pbb_body (FILE *file, poly_bb_p pbb, int verbosity)
715 if (verbosity > 1)
716 fprintf (file, "# Body (\n");
718 if (verbosity > 0)
719 fprintf (file, "# Statement body is provided\n");
720 fprintf (file, "1\n");
722 if (verbosity > 0)
723 fprintf (file, "# Original iterator names\n# Iterator names are not provided yet.\n");
725 if (verbosity > 0)
726 fprintf (file, "# Statement body\n");
728 fprintf (file, "{\n");
729 dump_bb (pbb_bb (pbb), file, 0);
730 fprintf (file, "}\n");
732 if (verbosity > 1)
733 fprintf (file, "#)\n");
736 /* Print to FILE the domain and scattering function of PBB, at some
737 VERBOSITY level. */
739 void
740 print_pbb (FILE *file, poly_bb_p pbb, int verbosity)
742 if (verbosity > 1)
744 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
745 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
746 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
749 print_pbb_domain (file, pbb, verbosity);
750 print_scattering_function (file, pbb, verbosity);
751 print_pdrs (file, pbb, verbosity);
752 print_pbb_body (file, pbb, verbosity);
754 if (verbosity > 1)
755 fprintf (file, "#)\n");
758 /* Print to FILE the parameters of SCOP, at some VERBOSITY level. */
760 void
761 print_scop_params (FILE *file, scop_p scop, int verbosity)
763 int i;
764 tree t;
766 if (verbosity > 1)
767 fprintf (file, "# parameters (\n");
769 if (VEC_length (tree, SESE_PARAMS (SCOP_REGION (scop))))
771 if (verbosity > 0)
772 fprintf (file, "# Parameter names are provided\n");
774 fprintf (file, "1\n");
776 if (verbosity > 0)
777 fprintf (file, "# Parameter names\n");
779 else
781 if (verbosity > 0)
782 fprintf (file, "# Parameter names are not provided\n");
783 fprintf (file, "0\n");
786 for (i = 0; VEC_iterate (tree, SESE_PARAMS (SCOP_REGION (scop)), i, t); i++)
788 print_generic_expr (file, t, 0);
789 fprintf (file, " ");
792 fprintf (file, "\n");
794 if (verbosity > 1)
795 fprintf (file, "#)\n");
798 /* Print to FILE the context of SCoP, at some VERBOSITY level. */
800 void
801 print_scop_context (FILE *file, scop_p scop, int verbosity)
803 graphite_dim_t i;
805 if (verbosity > 0)
807 fprintf (file, "# Context (\n");
808 fprintf (file, "# eq");
810 for (i = 0; i < scop_nb_params (scop); i++)
811 fprintf (file, " p%d", (int) i);
813 fprintf (file, " cst\n");
816 if (SCOP_CONTEXT (scop))
817 ppl_print_powerset_matrix (file, SCOP_CONTEXT (scop));
818 else
819 fprintf (file, "0 %d\n", (int) scop_nb_params (scop) + 2);
821 if (verbosity > 0)
822 fprintf (file, "# )\n");
825 /* Print to FILE the SCOP, at some VERBOSITY level. */
827 void
828 print_scop (FILE *file, scop_p scop, int verbosity)
830 int i;
831 poly_bb_p pbb;
833 fprintf (file, "SCoP #(\n");
834 fprintf (file, "# Language\nGimple\n");
835 print_scop_context (file, scop, verbosity);
836 print_scop_params (file, scop, verbosity);
838 if (verbosity > 0)
839 fprintf (file, "# Number of statements\n");
841 fprintf (file, "%d\n",VEC_length (poly_bb_p, SCOP_BBS (scop)));
843 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
844 print_pbb (file, pbb, verbosity);
846 if (verbosity > 1)
848 fprintf (file, "# original_lst (\n");
849 print_lst (file, SCOP_ORIGINAL_SCHEDULE (scop), 0);
850 fprintf (file, "\n#)\n");
852 fprintf (file, "# transformed_lst (\n");
853 print_lst (file, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
854 fprintf (file, "\n#)\n");
857 fprintf (file, "#)\n");
860 /* Print to FILE the input file that CLooG would expect as input, at
861 some VERBOSITY level. */
863 void
864 print_cloog (FILE *file, scop_p scop, int verbosity)
866 int i;
867 poly_bb_p pbb;
869 fprintf (file, "# SCoP (generated by GCC/Graphite\n");
870 if (verbosity > 0)
871 fprintf (file, "# CLooG output language\n");
872 fprintf (file, "c\n");
874 print_scop_context (file, scop, verbosity);
875 print_scop_params (file, scop, verbosity);
877 if (verbosity > 0)
878 fprintf (file, "# Number of statements\n");
880 fprintf (file, "%d\n", VEC_length (poly_bb_p, SCOP_BBS (scop)));
882 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
884 if (verbosity > 1)
885 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
887 print_pbb_domain (file, pbb, verbosity);
888 fprintf (file, "0 0 0");
890 if (verbosity > 0)
891 fprintf (file, "# For future CLooG options.\n");
892 else
893 fprintf (file, "\n");
895 if (verbosity > 1)
896 fprintf (file, "#)\n");
899 fprintf (file, "0");
900 if (verbosity > 0)
901 fprintf (file, "# Don't set the iterator names.\n");
902 else
903 fprintf (file, "\n");
905 if (verbosity > 0)
906 fprintf (file, "# Number of scattering functions\n");
908 fprintf (file, "%d\n", VEC_length (poly_bb_p, SCOP_BBS (scop)));
909 unify_scattering_dimensions (scop);
911 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
913 if (!PBB_TRANSFORMED (pbb)
914 || !(PBB_TRANSFORMED_SCATTERING (pbb)
915 || PBB_ORIGINAL_SCATTERING (pbb)))
916 continue;
918 if (verbosity > 1)
919 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
921 print_scattering_function_1 (file, pbb, verbosity);
923 if (verbosity > 1)
924 fprintf (file, "#)\n");
927 fprintf (file, "0");
928 if (verbosity > 0)
929 fprintf (file, "# Don't set the scattering dimension names.\n");
930 else
931 fprintf (file, "\n");
933 fprintf (file, "#)\n");
936 /* Print to STDERR the domain of PBB, at some VERBOSITY level. */
938 void
939 debug_pbb_domain (poly_bb_p pbb, int verbosity)
941 print_pbb_domain (stderr, pbb, verbosity);
944 /* Print to FILE the domain and scattering function of PBB, at some
945 VERBOSITY level. */
947 void
948 debug_pbb (poly_bb_p pbb, int verbosity)
950 print_pbb (stderr, pbb, verbosity);
953 /* Print to STDERR the context of SCOP, at some VERBOSITY level. */
955 void
956 debug_scop_context (scop_p scop, int verbosity)
958 print_scop_context (stderr, scop, verbosity);
961 /* Print to STDERR the SCOP, at some VERBOSITY level. */
963 void
964 debug_scop (scop_p scop, int verbosity)
966 print_scop (stderr, scop, verbosity);
969 /* Print to STDERR the SCOP under CLooG format, at some VERBOSITY
970 level. */
972 void
973 debug_cloog (scop_p scop, int verbosity)
975 print_cloog (stderr, scop, verbosity);
978 /* Print to STDERR the parameters of SCOP, at some VERBOSITY
979 level. */
981 void
982 debug_scop_params (scop_p scop, int verbosity)
984 print_scop_params (stderr, scop, verbosity);
988 /* The dimension in the transformed scattering polyhedron of PBB
989 containing the scattering iterator for the loop at depth LOOP_DEPTH. */
991 ppl_dimension_type
992 psct_scattering_dim_for_loop_depth (poly_bb_p pbb, graphite_dim_t loop_depth)
994 ppl_const_Constraint_System_t pcs;
995 ppl_Constraint_System_const_iterator_t cit, cend;
996 ppl_const_Constraint_t cstr;
997 ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
998 ppl_dimension_type iter = psct_iterator_dim (pbb, loop_depth);
999 ppl_Linear_Expression_t expr;
1000 ppl_Coefficient_t coef;
1001 Value val;
1002 graphite_dim_t i;
1004 value_init (val);
1005 ppl_new_Coefficient (&coef);
1006 ppl_Polyhedron_get_constraints (ph, &pcs);
1007 ppl_new_Constraint_System_const_iterator (&cit);
1008 ppl_new_Constraint_System_const_iterator (&cend);
1010 for (ppl_Constraint_System_begin (pcs, cit),
1011 ppl_Constraint_System_end (pcs, cend);
1012 !ppl_Constraint_System_const_iterator_equal_test (cit, cend);
1013 ppl_Constraint_System_const_iterator_increment (cit))
1015 ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
1016 ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
1017 ppl_Linear_Expression_coefficient (expr, iter, coef);
1018 ppl_Coefficient_to_mpz_t (coef, val);
1020 if (value_zero_p (val))
1022 ppl_delete_Linear_Expression (expr);
1023 continue;
1026 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
1028 ppl_dimension_type scatter = psct_scattering_dim (pbb, i);
1030 ppl_Linear_Expression_coefficient (expr, scatter, coef);
1031 ppl_Coefficient_to_mpz_t (coef, val);
1033 if (value_notzero_p (val))
1035 value_clear (val);
1036 ppl_delete_Linear_Expression (expr);
1037 ppl_delete_Coefficient (coef);
1038 ppl_delete_Constraint_System_const_iterator (cit);
1039 ppl_delete_Constraint_System_const_iterator (cend);
1041 return scatter;
1046 gcc_unreachable ();
1049 /* Returns the number of iterations NITER of the loop around PBB at
1050 depth LOOP_DEPTH. */
1052 void
1053 pbb_number_of_iterations (poly_bb_p pbb,
1054 graphite_dim_t loop_depth,
1055 Value niter)
1057 ppl_Linear_Expression_t le;
1058 ppl_dimension_type dim;
1060 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
1061 ppl_new_Linear_Expression_with_dimension (&le, dim);
1062 ppl_set_coef (le, pbb_iterator_dim (pbb, loop_depth), 1);
1063 value_set_si (niter, -1);
1064 ppl_max_for_le_pointset (PBB_DOMAIN (pbb), le, niter);
1065 ppl_delete_Linear_Expression (le);
1068 /* Returns the number of iterations NITER of the loop around PBB at
1069 time(scattering) dimension TIME_DEPTH. */
1071 void
1072 pbb_number_of_iterations_at_time (poly_bb_p pbb,
1073 graphite_dim_t time_depth,
1074 Value niter)
1076 ppl_Pointset_Powerset_C_Polyhedron_t ext_domain, sctr;
1077 ppl_Linear_Expression_t le;
1078 ppl_dimension_type dim;
1080 /* Takes together domain and scattering polyhedrons, and composes
1081 them into the bigger polyhedron that has the following format:
1083 t0..t_{n-1} | l0..l_{nlcl-1} | i0..i_{niter-1} | g0..g_{nparm-1}
1085 where
1086 | t0..t_{n-1} are time dimensions (scattering dimensions)
1087 | l0..l_{nclc-1} are local variables in scattering function
1088 | i0..i_{niter-1} are original iteration variables
1089 | g0..g_{nparam-1} are global parameters. */
1091 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&sctr,
1092 PBB_TRANSFORMED_SCATTERING (pbb));
1094 /* Extend the iteration domain with the scattering dimensions:
1095 0..0 | 0..0 | i0..i_{niter-1} | g0..g_{nparm-1}. */
1096 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1097 (&ext_domain, PBB_DOMAIN (pbb));
1098 ppl_insert_dimensions_pointset (ext_domain, 0,
1099 pbb_nb_scattering_transform (pbb)
1100 + pbb_nb_local_vars (pbb));
1102 /* Add to sctr the extended domain. */
1103 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (sctr, ext_domain);
1105 /* Extract the number of iterations. */
1106 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (sctr, &dim);
1107 ppl_new_Linear_Expression_with_dimension (&le, dim);
1108 ppl_set_coef (le, time_depth, 1);
1109 value_set_si (niter, -1);
1110 ppl_max_for_le_pointset (sctr, le, niter);
1112 ppl_delete_Linear_Expression (le);
1113 ppl_delete_Pointset_Powerset_C_Polyhedron (sctr);
1114 ppl_delete_Pointset_Powerset_C_Polyhedron (ext_domain);
1117 /* Translates LOOP to LST. */
1119 static lst_p
1120 loop_to_lst (loop_p loop, VEC (poly_bb_p, heap) *bbs, int *i)
1122 poly_bb_p pbb;
1123 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
1125 for (; VEC_iterate (poly_bb_p, bbs, *i, pbb); (*i)++)
1127 lst_p stmt;
1128 basic_block bb = GBB_BB (PBB_BLACK_BOX (pbb));
1130 if (bb->loop_father == loop)
1131 stmt = new_lst_stmt (pbb);
1132 else if (flow_bb_inside_loop_p (loop, bb))
1134 loop_p next = loop->inner;
1136 while (next && !flow_bb_inside_loop_p (next, bb))
1137 next = next->next;
1139 stmt = loop_to_lst (next, bbs, i);
1141 else
1143 (*i)--;
1144 return new_lst_loop (seq);
1147 VEC_safe_push (lst_p, heap, seq, stmt);
1150 return new_lst_loop (seq);
1153 /* Reads the original scattering of the SCOP and returns an LST
1154 representing it. */
1156 void
1157 scop_to_lst (scop_p scop)
1159 lst_p res;
1160 int i, n = VEC_length (poly_bb_p, SCOP_BBS (scop));
1161 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
1162 sese region = SCOP_REGION (scop);
1164 for (i = 0; i < n; i++)
1166 poly_bb_p pbb = VEC_index (poly_bb_p, SCOP_BBS (scop), i);
1167 loop_p loop = outermost_loop_in_sese (region, GBB_BB (PBB_BLACK_BOX (pbb)));
1169 if (loop_in_sese_p (loop, region))
1170 res = loop_to_lst (loop, SCOP_BBS (scop), &i);
1171 else
1172 res = new_lst_stmt (pbb);
1174 VEC_safe_push (lst_p, heap, seq, res);
1177 res = new_lst_loop (seq);
1178 SCOP_ORIGINAL_SCHEDULE (scop) = res;
1179 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (res);
1182 /* Print to FILE on a new line COLUMN white spaces. */
1184 static void
1185 lst_indent_to (FILE *file, int column)
1187 int i;
1189 if (column > 0)
1190 fprintf (file, "\n#");
1192 for (i = 0; i < column; i++)
1193 fprintf (file, " ");
1196 /* Print LST to FILE with INDENT spaces of indentation. */
1198 void
1199 print_lst (FILE *file, lst_p lst, int indent)
1201 if (!lst)
1202 return;
1204 lst_indent_to (file, indent);
1206 if (LST_LOOP_P (lst))
1208 int i;
1209 lst_p l;
1211 if (LST_LOOP_FATHER (lst))
1212 fprintf (file, "%d (loop", lst_dewey_number (lst));
1213 else
1214 fprintf (file, "#(root");
1216 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
1217 print_lst (file, l, indent + 2);
1219 fprintf (file, ")");
1221 else
1222 fprintf (file, "%d stmt_%d", lst_dewey_number (lst), pbb_index (LST_PBB (lst)));
1225 /* Print LST to STDERR. */
1227 void
1228 debug_lst (lst_p lst)
1230 print_lst (stderr, lst, 0);
1233 /* Pretty print to FILE the loop statement tree LST in DOT format. */
1235 static void
1236 dot_lst_1 (FILE *file, lst_p lst)
1238 if (!lst)
1239 return;
1241 if (LST_LOOP_P (lst))
1243 int i;
1244 lst_p l;
1246 if (!LST_LOOP_FATHER (lst))
1247 fprintf (file, "L -> L_%d_%d\n",
1248 lst_depth (lst),
1249 lst_dewey_number (lst));
1250 else
1251 fprintf (file, "L_%d_%d -> L_%d_%d\n",
1252 lst_depth (LST_LOOP_FATHER (lst)),
1253 lst_dewey_number (LST_LOOP_FATHER (lst)),
1254 lst_depth (lst),
1255 lst_dewey_number (lst));
1257 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
1258 dot_lst_1 (file, l);
1261 else
1262 fprintf (file, "L_%d_%d -> S_%d\n",
1263 lst_depth (LST_LOOP_FATHER (lst)),
1264 lst_dewey_number (LST_LOOP_FATHER (lst)),
1265 pbb_index (LST_PBB (lst)));
1269 /* Display the LST using dotty. */
1271 void
1272 dot_lst (lst_p lst)
1274 /* When debugging, enable the following code. This cannot be used
1275 in production compilers because it calls "system". */
1276 #if 0
1277 int x;
1278 FILE *stream = fopen ("/tmp/lst.dot", "w");
1279 gcc_assert (stream);
1281 fputs ("digraph all {\n", stream);
1282 dot_lst_1 (stream, lst);
1283 fputs ("}\n\n", stream);
1284 fclose (stream);
1286 x = system ("dotty /tmp/lst.dot");
1287 #else
1288 fputs ("digraph all {\n", stderr);
1289 dot_lst_1 (stderr, lst);
1290 fputs ("}\n\n", stderr);
1292 #endif
1295 #endif