* tree-if-conv.c: Fix various typos in comments.
[official-gcc.git] / gcc / graphite-poly.c
blobdd4fcee15227bbab9be1db59ba143bda83b74426
1 /* Graphite polyhedral representation.
2 Copyright (C) 2009-2015 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/>. */
22 #include "config.h"
24 #ifdef HAVE_isl
25 /* Workaround for GMP 5.1.3 bug, see PR56019. */
26 #include <stddef.h>
28 #include <isl/constraint.h>
29 #include <isl/set.h>
30 #include <isl/map.h>
31 #include <isl/union_map.h>
32 #include <isl/constraint.h>
33 #include <isl/ilp.h>
34 #include <isl/aff.h>
35 #include <isl/val.h>
37 /* Since ISL-0.13, the extern is in val_gmp.h. */
38 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
39 extern "C" {
40 #endif
41 #include <isl/val_gmp.h>
42 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
44 #endif
46 #include "system.h"
47 #include "coretypes.h"
48 #include "backend.h"
49 #include "diagnostic-core.h"
50 #include "cfghooks.h"
51 #include "tree.h"
52 #include "gimple.h"
53 #include "fold-const.h"
54 #include "gimple-iterator.h"
55 #include "tree-ssa-loop.h"
56 #include "gimple-pretty-print.h"
57 #include "cfgloop.h"
58 #include "tree-data-ref.h"
59 #include "graphite-poly.h"
61 #define OPENSCOP_MAX_STRING 256
64 /* Print to STDERR the GMP value VAL. */
66 DEBUG_FUNCTION void
67 debug_gmp_value (mpz_t val)
69 gmp_fprintf (stderr, "%Zd", val);
72 /* Return the maximal loop depth in SCOP. */
74 int
75 scop_max_loop_depth (scop_p scop)
77 int i;
78 poly_bb_p pbb;
79 int max_nb_loops = 0;
81 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
83 int nb_loops = pbb_dim_iter_domain (pbb);
84 if (max_nb_loops < nb_loops)
85 max_nb_loops = nb_loops;
88 return max_nb_loops;
91 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
92 level. */
94 static void
95 print_scattering_function_1 (FILE *file, poly_bb_p pbb, int verbosity)
97 graphite_dim_t i;
99 if (verbosity > 0)
101 fprintf (file, "# scattering bb_%d (\n", pbb_index (pbb));
102 fprintf (file, "#eq");
104 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
105 fprintf (file, " s%d", (int) i);
107 for (i = 0; i < pbb_nb_local_vars (pbb); i++)
108 fprintf (file, " lv%d", (int) i);
110 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
111 fprintf (file, " i%d", (int) i);
113 for (i = 0; i < pbb_nb_params (pbb); i++)
114 fprintf (file, " p%d", (int) i);
116 fprintf (file, " cst\n");
119 fprintf (file, "isl\n");
120 print_isl_map (file, pbb->transformed ? pbb->transformed : pbb->schedule);
122 if (verbosity > 0)
123 fprintf (file, "#)\n");
126 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
127 level. */
129 void
130 print_scattering_function (FILE *file, poly_bb_p pbb, int verbosity)
132 if (!PBB_TRANSFORMED (pbb))
133 return;
135 if (pbb->schedule || pbb->transformed)
137 if (verbosity > 0)
138 fprintf (file, "# Scattering function is provided\n");
140 fprintf (file, "1\n");
142 else
144 if (verbosity > 0)
145 fprintf (file, "# Scattering function is not provided\n");
147 fprintf (file, "0\n");
148 return;
151 print_scattering_function_1 (file, pbb, verbosity);
153 if (verbosity > 0)
154 fprintf (file, "# Scattering names are not provided\n");
156 fprintf (file, "0\n");
160 /* Prints to FILE the iteration domain of PBB, at some VERBOSITY
161 level. */
163 void
164 print_iteration_domain (FILE *file, poly_bb_p pbb, int verbosity)
166 print_pbb_domain (file, pbb, verbosity);
169 /* Prints to FILE the scattering functions of every PBB of SCOP. */
171 void
172 print_scattering_functions (FILE *file, scop_p scop, int verbosity)
174 int i;
175 poly_bb_p pbb;
177 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
178 print_scattering_function (file, pbb, verbosity);
181 /* Prints to FILE the iteration domains of every PBB of SCOP, at some
182 VERBOSITY level. */
184 void
185 print_iteration_domains (FILE *file, scop_p scop, int verbosity)
187 int i;
188 poly_bb_p pbb;
190 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
191 print_iteration_domain (file, pbb, verbosity);
194 /* Prints to STDERR the scattering function of PBB, at some VERBOSITY
195 level. */
197 DEBUG_FUNCTION void
198 debug_scattering_function (poly_bb_p pbb, int verbosity)
200 print_scattering_function (stderr, pbb, verbosity);
203 /* Prints to STDERR the iteration domain of PBB, at some VERBOSITY
204 level. */
206 DEBUG_FUNCTION void
207 debug_iteration_domain (poly_bb_p pbb, int verbosity)
209 print_iteration_domain (stderr, pbb, verbosity);
212 /* Prints to STDERR the scattering functions of every PBB of SCOP, at
213 some VERBOSITY level. */
215 DEBUG_FUNCTION void
216 debug_scattering_functions (scop_p scop, int verbosity)
218 print_scattering_functions (stderr, scop, verbosity);
221 /* Prints to STDERR the iteration domains of every PBB of SCOP, at
222 some VERBOSITY level. */
224 DEBUG_FUNCTION void
225 debug_iteration_domains (scop_p scop, int verbosity)
227 print_iteration_domains (stderr, scop, verbosity);
230 /* Apply graphite transformations to all the basic blocks of SCOP. */
232 bool
233 apply_poly_transforms (scop_p scop)
235 bool transform_done = false;
237 /* Generate code even if we did not apply any real transformation.
238 This also allows to check the performance for the identity
239 transformation: GIMPLE -> GRAPHITE -> GIMPLE
240 Keep in mind that CLooG optimizes in control, so the loop structure
241 may change, even if we only use -fgraphite-identity. */
242 if (flag_graphite_identity)
243 transform_done = true;
245 if (flag_loop_parallelize_all)
246 transform_done = true;
248 if (flag_loop_block)
249 transform_done |= scop_do_block (scop);
250 else
252 if (flag_loop_strip_mine)
253 transform_done |= scop_do_strip_mine (scop, 0);
255 if (flag_loop_interchange)
256 transform_done |= scop_do_interchange (scop);
259 /* This pass needs to be run at the final stage, as it does not
260 update the lst. */
261 if (flag_loop_optimize_isl || flag_loop_unroll_jam)
262 transform_done |= optimize_isl (scop);
264 return transform_done;
267 /* Create a new polyhedral data reference and add it to PBB. It is
268 defined by its ACCESSES, its TYPE, and the number of subscripts
269 NB_SUBSCRIPTS. */
271 void
272 new_poly_dr (poly_bb_p pbb, int dr_base_object_set,
273 enum poly_dr_type type, void *cdr, graphite_dim_t nb_subscripts,
274 isl_map *acc, isl_set *subscript_sizes)
276 static int id = 0;
277 poly_dr_p pdr = XNEW (struct poly_dr);
279 PDR_ID (pdr) = id++;
280 PDR_BASE_OBJECT_SET (pdr) = dr_base_object_set;
281 PDR_NB_REFS (pdr) = 1;
282 PDR_PBB (pdr) = pbb;
283 pdr->accesses = acc;
284 pdr->subscript_sizes = subscript_sizes;
285 PDR_TYPE (pdr) = type;
286 PDR_CDR (pdr) = cdr;
287 PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
288 PBB_DRS (pbb).safe_push (pdr);
291 /* Free polyhedral data reference PDR. */
293 void
294 free_poly_dr (poly_dr_p pdr)
296 isl_map_free (pdr->accesses);
297 isl_set_free (pdr->subscript_sizes);
298 XDELETE (pdr);
301 /* Create a new polyhedral black box. */
303 poly_bb_p
304 new_poly_bb (scop_p scop, void *black_box)
306 poly_bb_p pbb = XNEW (struct poly_bb);
308 pbb->domain = NULL;
309 pbb->schedule = NULL;
310 pbb->transformed = NULL;
311 pbb->saved = NULL;
312 pbb->map_sepclass = NULL;
313 PBB_SCOP (pbb) = scop;
314 pbb_set_black_box (pbb, black_box);
315 PBB_TRANSFORMED (pbb) = NULL;
316 PBB_SAVED (pbb) = NULL;
317 PBB_ORIGINAL (pbb) = NULL;
318 PBB_DRS (pbb).create (3);
319 PBB_IS_REDUCTION (pbb) = false;
320 GBB_PBB ((gimple_bb_p) black_box) = pbb;
322 return pbb;
325 /* Free polyhedral black box. */
327 void
328 free_poly_bb (poly_bb_p pbb)
330 int i;
331 poly_dr_p pdr;
333 isl_set_free (pbb->domain);
334 isl_map_free (pbb->schedule);
335 isl_map_free (pbb->transformed);
336 isl_map_free (pbb->saved);
338 if (PBB_DRS (pbb).exists ())
339 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
340 free_poly_dr (pdr);
342 PBB_DRS (pbb).release ();
343 XDELETE (pbb);
346 static void
347 print_pdr_access_layout (FILE *file, poly_bb_p pbb, poly_dr_p pdr)
349 graphite_dim_t i;
351 fprintf (file, "# eq");
353 fprintf (file, " alias");
355 for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
356 fprintf (file, " sub%d", (int) i);
358 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
359 fprintf (file, " i%d", (int) i);
361 for (i = 0; i < pbb_nb_params (pbb); i++)
362 fprintf (file, " p%d", (int) i);
364 fprintf (file, " cst\n");
367 /* Prints to FILE the polyhedral data reference PDR, at some VERBOSITY
368 level. */
370 void
371 print_pdr (FILE *file, poly_dr_p pdr, int verbosity)
373 if (verbosity > 1)
375 fprintf (file, "# pdr_%d (", PDR_ID (pdr));
377 switch (PDR_TYPE (pdr))
379 case PDR_READ:
380 fprintf (file, "read \n");
381 break;
383 case PDR_WRITE:
384 fprintf (file, "write \n");
385 break;
387 case PDR_MAY_WRITE:
388 fprintf (file, "may_write \n");
389 break;
391 default:
392 gcc_unreachable ();
395 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
398 if (verbosity > 0)
400 fprintf (file, "# data accesses (\n");
401 print_pdr_access_layout (file, PDR_PBB (pdr), pdr);
404 /* XXX isl dump accesses/subscripts */
406 if (verbosity > 0)
407 fprintf (file, "#)\n");
409 if (verbosity > 1)
410 fprintf (file, "#)\n");
413 /* Prints to STDERR the polyhedral data reference PDR, at some
414 VERBOSITY level. */
416 DEBUG_FUNCTION void
417 debug_pdr (poly_dr_p pdr, int verbosity)
419 print_pdr (stderr, pdr, verbosity);
422 /* Creates a new SCOP containing REGION. */
424 scop_p
425 new_scop (void *region)
427 scop_p scop = XNEW (struct scop);
429 scop->context = NULL;
430 scop->must_raw = NULL;
431 scop->may_raw = NULL;
432 scop->must_raw_no_source = NULL;
433 scop->may_raw_no_source = NULL;
434 scop->must_war = NULL;
435 scop->may_war = NULL;
436 scop->must_war_no_source = NULL;
437 scop->may_war_no_source = NULL;
438 scop->must_waw = NULL;
439 scop->may_waw = NULL;
440 scop->must_waw_no_source = NULL;
441 scop->may_waw_no_source = NULL;
442 scop_set_region (scop, region);
443 SCOP_BBS (scop).create (3);
444 SCOP_ORIGINAL_SCHEDULE (scop) = NULL;
445 SCOP_TRANSFORMED_SCHEDULE (scop) = NULL;
446 SCOP_SAVED_SCHEDULE (scop) = NULL;
447 POLY_SCOP_P (scop) = false;
449 return scop;
452 /* Deletes SCOP. */
454 void
455 free_scop (scop_p scop)
457 int i;
458 poly_bb_p pbb;
460 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
461 free_poly_bb (pbb);
463 SCOP_BBS (scop).release ();
465 isl_set_free (scop->context);
466 isl_union_map_free (scop->must_raw);
467 isl_union_map_free (scop->may_raw);
468 isl_union_map_free (scop->must_raw_no_source);
469 isl_union_map_free (scop->may_raw_no_source);
470 isl_union_map_free (scop->must_war);
471 isl_union_map_free (scop->may_war);
472 isl_union_map_free (scop->must_war_no_source);
473 isl_union_map_free (scop->may_war_no_source);
474 isl_union_map_free (scop->must_waw);
475 isl_union_map_free (scop->may_waw);
476 isl_union_map_free (scop->must_waw_no_source);
477 isl_union_map_free (scop->may_waw_no_source);
478 free_lst (SCOP_ORIGINAL_SCHEDULE (scop));
479 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
480 free_lst (SCOP_SAVED_SCHEDULE (scop));
481 XDELETE (scop);
484 /* Print to FILE the domain of PBB in OpenScop format, at some VERBOSITY
485 level. */
487 static void
488 openscop_print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity)
490 graphite_dim_t i;
491 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
493 if (!pbb->domain)
494 return;
496 if (verbosity > 0)
498 fprintf (file, "\n# Iteration domain of bb_%d (\n", GBB_BB (gbb)->index);
499 fprintf (file, "#eq");
501 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
502 fprintf (file, " i%d", (int) i);
504 for (i = 0; i < pbb_nb_params (pbb); i++)
505 fprintf (file, " p%d", (int) i);
507 fprintf (file, " cst\n");
510 fprintf (file, "XXX isl\n");
512 if (verbosity > 0)
513 fprintf (file, "#)\n");
516 /* Print to FILE the domain of PBB, at some VERBOSITY level. */
518 void
519 print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity ATTRIBUTE_UNUSED)
521 print_isl_set (file, pbb->domain);
524 /* Dump the cases of a graphite basic block GBB on FILE. */
526 static void
527 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
529 int i;
530 gimple stmt;
531 vec<gimple> cases;
533 if (!gbb)
534 return;
536 cases = GBB_CONDITION_CASES (gbb);
537 if (cases.is_empty ())
538 return;
540 fprintf (file, "# cases bb_%d (\n", GBB_BB (gbb)->index);
542 FOR_EACH_VEC_ELT (cases, i, stmt)
544 fprintf (file, "# ");
545 print_gimple_stmt (file, stmt, 0, 0);
548 fprintf (file, "#)\n");
551 /* Dump conditions of a graphite basic block GBB on FILE. */
553 static void
554 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
556 int i;
557 gimple stmt;
558 vec<gimple> conditions;
560 if (!gbb)
561 return;
563 conditions = GBB_CONDITIONS (gbb);
564 if (conditions.is_empty ())
565 return;
567 fprintf (file, "# conditions bb_%d (\n", GBB_BB (gbb)->index);
569 FOR_EACH_VEC_ELT (conditions, i, stmt)
571 fprintf (file, "# ");
572 print_gimple_stmt (file, stmt, 0, 0);
575 fprintf (file, "#)\n");
578 /* Print to FILE all the data references of PBB, at some VERBOSITY
579 level. */
581 void
582 print_pdrs (FILE *file, poly_bb_p pbb, int verbosity)
584 int i;
585 poly_dr_p pdr;
586 int nb_reads = 0;
587 int nb_writes = 0;
589 if (PBB_DRS (pbb).length () == 0)
591 if (verbosity > 0)
592 fprintf (file, "# Access informations are not provided\n");\
593 fprintf (file, "0\n");
594 return;
597 if (verbosity > 1)
598 fprintf (file, "# Data references (\n");
600 if (verbosity > 0)
601 fprintf (file, "# Access informations are provided\n");
602 fprintf (file, "1\n");
604 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
605 if (PDR_TYPE (pdr) == PDR_READ)
606 nb_reads++;
607 else
608 nb_writes++;
610 if (verbosity > 1)
611 fprintf (file, "# Read data references (\n");
613 if (verbosity > 0)
614 fprintf (file, "# Read access informations\n");
615 fprintf (file, "%d\n", nb_reads);
617 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
618 if (PDR_TYPE (pdr) == PDR_READ)
619 print_pdr (file, pdr, verbosity);
621 if (verbosity > 1)
622 fprintf (file, "#)\n");
624 if (verbosity > 1)
625 fprintf (file, "# Write data references (\n");
627 if (verbosity > 0)
628 fprintf (file, "# Write access informations\n");
629 fprintf (file, "%d\n", nb_writes);
631 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
632 if (PDR_TYPE (pdr) != PDR_READ)
633 print_pdr (file, pdr, verbosity);
635 if (verbosity > 1)
636 fprintf (file, "#)\n");
638 if (verbosity > 1)
639 fprintf (file, "#)\n");
642 /* Print to STDERR all the data references of PBB. */
644 DEBUG_FUNCTION void
645 debug_pdrs (poly_bb_p pbb, int verbosity)
647 print_pdrs (stderr, pbb, verbosity);
650 /* Print to FILE the body of PBB, at some VERBOSITY level.
651 If statement_body_provided is false statement body is not printed. */
653 static void
654 print_pbb_body (FILE *file, poly_bb_p pbb, int verbosity,
655 bool statement_body_provided)
657 if (verbosity > 1)
658 fprintf (file, "# Body (\n");
660 if (!statement_body_provided)
662 if (verbosity > 0)
663 fprintf (file, "# Statement body is not provided\n");
665 fprintf (file, "0\n");
667 if (verbosity > 1)
668 fprintf (file, "#)\n");
669 return;
672 if (verbosity > 0)
673 fprintf (file, "# Statement body is provided\n");
674 fprintf (file, "1\n");
676 if (verbosity > 0)
677 fprintf (file, "# Original iterator names\n# Iterator names are not provided yet.\n");
679 if (verbosity > 0)
680 fprintf (file, "# Statement body\n");
682 fprintf (file, "{\n");
683 dump_bb (file, pbb_bb (pbb), 0, 0);
684 fprintf (file, "}\n");
686 if (verbosity > 1)
687 fprintf (file, "#)\n");
690 /* Print to FILE the domain and scattering function of PBB, at some
691 VERBOSITY level. */
693 void
694 print_pbb (FILE *file, poly_bb_p pbb, int verbosity)
696 if (verbosity > 1)
698 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
699 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
700 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
703 openscop_print_pbb_domain (file, pbb, verbosity);
704 print_scattering_function (file, pbb, verbosity);
705 print_pdrs (file, pbb, verbosity);
706 print_pbb_body (file, pbb, verbosity, false);
708 if (verbosity > 1)
709 fprintf (file, "#)\n");
712 /* Print to FILE the parameters of SCOP, at some VERBOSITY level. */
714 void
715 print_scop_params (FILE *file, scop_p scop, int verbosity)
717 int i;
718 tree t;
720 if (verbosity > 1)
721 fprintf (file, "# parameters (\n");
723 if (SESE_PARAMS (SCOP_REGION (scop)).length ())
725 if (verbosity > 0)
726 fprintf (file, "# Parameter names are provided\n");
728 fprintf (file, "1\n");
730 if (verbosity > 0)
731 fprintf (file, "# Parameter names\n");
733 else
735 if (verbosity > 0)
736 fprintf (file, "# Parameter names are not provided\n");
737 fprintf (file, "0\n");
740 FOR_EACH_VEC_ELT (SESE_PARAMS (SCOP_REGION (scop)), i, t)
742 print_generic_expr (file, t, 0);
743 fprintf (file, " ");
746 fprintf (file, "\n");
748 if (verbosity > 1)
749 fprintf (file, "#)\n");
752 /* Print to FILE the context of SCoP in OpenScop format, at some VERBOSITY
753 level. */
755 static void
756 openscop_print_scop_context (FILE *file, scop_p scop, int verbosity)
758 graphite_dim_t i;
760 if (verbosity > 0)
762 fprintf (file, "# Context (\n");
763 fprintf (file, "#eq");
765 for (i = 0; i < scop_nb_params (scop); i++)
766 fprintf (file, " p%d", (int) i);
768 fprintf (file, " cst\n");
771 if (scop->context)
772 /* XXX isl print context */
773 fprintf (file, "XXX isl\n");
774 else
775 fprintf (file, "0 %d 0 0 0 %d\n", (int) scop_nb_params (scop) + 2,
776 (int) scop_nb_params (scop));
778 if (verbosity > 0)
779 fprintf (file, "# )\n");
782 /* Print to FILE the context of SCoP, at some VERBOSITY level. */
784 void
785 print_scop_context (FILE *file, scop_p scop, int verbosity)
787 graphite_dim_t i;
789 if (verbosity > 0)
791 fprintf (file, "# Context (\n");
792 fprintf (file, "#eq");
794 for (i = 0; i < scop_nb_params (scop); i++)
795 fprintf (file, " p%d", (int) i);
797 fprintf (file, " cst\n");
800 if (scop->context)
801 print_isl_set (file, scop->context);
802 else
803 fprintf (file, "no isl context %d\n", (int) scop_nb_params (scop) + 2);
805 if (verbosity > 0)
806 fprintf (file, "# )\n");
809 /* Print to FILE the SCOP, at some VERBOSITY level. */
811 void
812 print_scop (FILE *file, scop_p scop, int verbosity)
814 int i;
815 poly_bb_p pbb;
817 fprintf (file, "SCoP 1\n#(\n");
818 fprintf (file, "# Language\nGimple\n");
819 openscop_print_scop_context (file, scop, verbosity);
820 print_scop_params (file, scop, verbosity);
822 if (verbosity > 0)
823 fprintf (file, "# Number of statements\n");
825 fprintf (file, "%d\n", SCOP_BBS (scop).length ());
827 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
828 print_pbb (file, pbb, verbosity);
830 if (verbosity > 1)
832 fprintf (file, "# original_lst (\n");
833 print_lst (file, SCOP_ORIGINAL_SCHEDULE (scop), 0);
834 fprintf (file, "\n#)\n");
836 fprintf (file, "# transformed_lst (\n");
837 print_lst (file, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
838 fprintf (file, "\n#)\n");
841 fprintf (file, "#)\n");
844 /* Print to STDERR the domain of PBB, at some VERBOSITY level. */
846 DEBUG_FUNCTION void
847 debug_pbb_domain (poly_bb_p pbb, int verbosity)
849 print_pbb_domain (stderr, pbb, verbosity);
852 /* Print to FILE the domain and scattering function of PBB, at some
853 VERBOSITY level. */
855 DEBUG_FUNCTION void
856 debug_pbb (poly_bb_p pbb, int verbosity)
858 print_pbb (stderr, pbb, verbosity);
861 /* Print to STDERR the context of SCOP, at some VERBOSITY level. */
863 DEBUG_FUNCTION void
864 debug_scop_context (scop_p scop, int verbosity)
866 print_scop_context (stderr, scop, verbosity);
869 /* Print to STDERR the SCOP, at some VERBOSITY level. */
871 DEBUG_FUNCTION void
872 debug_scop (scop_p scop, int verbosity)
874 print_scop (stderr, scop, verbosity);
877 /* Print to STDERR the parameters of SCOP, at some VERBOSITY
878 level. */
880 DEBUG_FUNCTION void
881 debug_scop_params (scop_p scop, int verbosity)
883 print_scop_params (stderr, scop, verbosity);
886 extern isl_ctx *the_isl_ctx;
887 void
888 print_isl_set (FILE *f, isl_set *set)
890 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
891 p = isl_printer_print_set (p, set);
892 isl_printer_free (p);
895 DEBUG_FUNCTION void
896 debug_isl_set (isl_set *set)
898 print_isl_set (stderr, set);
901 void
902 print_isl_map (FILE *f, isl_map *map)
904 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
905 p = isl_printer_print_map (p, map);
906 isl_printer_free (p);
909 DEBUG_FUNCTION void
910 debug_isl_map (isl_map *map)
912 print_isl_map (stderr, map);
915 void
916 print_isl_aff (FILE *f, isl_aff *aff)
918 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
919 p = isl_printer_print_aff (p, aff);
920 isl_printer_free (p);
923 DEBUG_FUNCTION void
924 debug_isl_aff (isl_aff *aff)
926 print_isl_aff (stderr, aff);
929 void
930 print_isl_constraint (FILE *f, isl_constraint *c)
932 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
933 p = isl_printer_print_constraint (p, c);
934 isl_printer_free (p);
937 DEBUG_FUNCTION void
938 debug_isl_constraint (isl_constraint *c)
940 print_isl_constraint (stderr, c);
943 /* Returns the number of iterations RES of the loop around PBB at
944 time(scattering) dimension TIME_DEPTH. */
946 void
947 pbb_number_of_iterations_at_time (poly_bb_p pbb,
948 graphite_dim_t time_depth,
949 mpz_t res)
951 isl_set *transdomain;
952 isl_space *dc;
953 isl_aff *aff;
954 isl_val *isllb, *islub;
956 /* Map the iteration domain through the current scatter, and work
957 on the resulting set. */
958 transdomain = isl_set_apply (isl_set_copy (pbb->domain),
959 isl_map_copy (pbb->transformed));
961 /* Select the time_depth' dimension via an affine expression. */
962 dc = isl_set_get_space (transdomain);
963 aff = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
964 aff = isl_aff_set_coefficient_si (aff, isl_dim_in, time_depth, 1);
966 /* And find the min/max for that function. */
967 /* XXX isl check results? */
968 isllb = isl_set_min_val (transdomain, aff);
969 islub = isl_set_max_val (transdomain, aff);
971 islub = isl_val_sub (islub, isllb);
972 islub = isl_val_add_ui (islub, 1);
973 isl_val_get_num_gmp (islub, res);
975 isl_val_free (islub);
976 isl_aff_free (aff);
977 isl_set_free (transdomain);
980 /* Translates LOOP to LST. */
982 static lst_p
983 loop_to_lst (loop_p loop, vec<poly_bb_p> bbs, int *i)
985 poly_bb_p pbb;
986 vec<lst_p> seq;
987 seq.create (5);
989 for (; bbs.iterate (*i, &pbb); (*i)++)
991 lst_p stmt;
992 basic_block bb = GBB_BB (PBB_BLACK_BOX (pbb));
994 if (bb->loop_father == loop)
995 stmt = new_lst_stmt (pbb);
996 else if (flow_bb_inside_loop_p (loop, bb))
998 loop_p next = loop->inner;
1000 while (next && !flow_bb_inside_loop_p (next, bb))
1001 next = next->next;
1003 stmt = loop_to_lst (next, bbs, i);
1005 else
1007 (*i)--;
1008 return new_lst_loop (seq);
1011 seq.safe_push (stmt);
1014 return new_lst_loop (seq);
1017 /* Reads the original scattering of the SCOP and returns an LST
1018 representing it. */
1020 void
1021 scop_to_lst (scop_p scop)
1023 lst_p res;
1024 int i, n = SCOP_BBS (scop).length ();
1025 vec<lst_p> seq;
1026 seq.create (5);
1027 sese region = SCOP_REGION (scop);
1029 for (i = 0; i < n; i++)
1031 poly_bb_p pbb = SCOP_BBS (scop)[i];
1032 loop_p loop = outermost_loop_in_sese (region, GBB_BB (PBB_BLACK_BOX (pbb)));
1034 if (loop_in_sese_p (loop, region))
1035 res = loop_to_lst (loop, SCOP_BBS (scop), &i);
1036 else
1037 res = new_lst_stmt (pbb);
1039 seq.safe_push (res);
1042 res = new_lst_loop (seq);
1043 SCOP_ORIGINAL_SCHEDULE (scop) = res;
1044 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (res);
1047 /* Print to FILE on a new line COLUMN white spaces. */
1049 static void
1050 lst_indent_to (FILE *file, int column)
1052 int i;
1054 if (column > 0)
1055 fprintf (file, "\n#");
1057 for (i = 0; i < column; i++)
1058 fprintf (file, " ");
1061 /* Print LST to FILE with INDENT spaces of indentation. */
1063 void
1064 print_lst (FILE *file, lst_p lst, int indent)
1066 if (!lst)
1067 return;
1069 lst_indent_to (file, indent);
1071 if (LST_LOOP_P (lst))
1073 int i;
1074 lst_p l;
1076 if (LST_LOOP_FATHER (lst))
1077 fprintf (file, "%d (loop", lst_dewey_number (lst));
1078 else
1079 fprintf (file, "#(root");
1081 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
1082 print_lst (file, l, indent + 2);
1084 fprintf (file, ")");
1086 else
1087 fprintf (file, "%d stmt_%d", lst_dewey_number (lst), pbb_index (LST_PBB (lst)));
1090 /* Print LST to STDERR. */
1092 DEBUG_FUNCTION void
1093 debug_lst (lst_p lst)
1095 print_lst (stderr, lst, 0);
1098 /* Pretty print to FILE the loop statement tree LST in DOT format. */
1100 static void
1101 dot_lst_1 (FILE *file, lst_p lst)
1103 if (!lst)
1104 return;
1106 if (LST_LOOP_P (lst))
1108 int i;
1109 lst_p l;
1111 if (!LST_LOOP_FATHER (lst))
1112 fprintf (file, "L -> L_%d_%d\n",
1113 lst_depth (lst),
1114 lst_dewey_number (lst));
1115 else
1116 fprintf (file, "L_%d_%d -> L_%d_%d\n",
1117 lst_depth (LST_LOOP_FATHER (lst)),
1118 lst_dewey_number (LST_LOOP_FATHER (lst)),
1119 lst_depth (lst),
1120 lst_dewey_number (lst));
1122 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
1123 dot_lst_1 (file, l);
1126 else
1127 fprintf (file, "L_%d_%d -> S_%d\n",
1128 lst_depth (LST_LOOP_FATHER (lst)),
1129 lst_dewey_number (LST_LOOP_FATHER (lst)),
1130 pbb_index (LST_PBB (lst)));
1134 /* Display the LST using dotty. */
1136 DEBUG_FUNCTION void
1137 dot_lst (lst_p lst)
1139 /* When debugging, enable the following code. This cannot be used
1140 in production compilers because it calls "system". */
1141 #if 0
1142 FILE *stream = fopen ("/tmp/lst.dot", "w");
1143 gcc_assert (stream);
1145 fputs ("digraph all {\n", stream);
1146 dot_lst_1 (stream, lst);
1147 fputs ("}\n\n", stream);
1148 fclose (stream);
1150 system ("dotty /tmp/lst.dot &");
1151 #else
1152 fputs ("digraph all {\n", stderr);
1153 dot_lst_1 (stderr, lst);
1154 fputs ("}\n\n", stderr);
1156 #endif
1159 /* Reverse the loop around PBB at level DEPTH. */
1161 isl_map *
1162 reverse_loop_at_level (poly_bb_p pbb, int depth)
1164 unsigned i, depth_dim = psct_dynamic_dim (pbb, depth);
1165 isl_space *d = isl_map_get_space (pbb->transformed);
1166 isl_space *d1 = isl_space_range (d);
1167 unsigned n = isl_space_dim (d1, isl_dim_out);
1168 isl_space *d2 = isl_space_add_dims (d1, isl_dim_in, n);
1169 isl_map *x = isl_map_universe (isl_space_copy (d2));
1170 isl_constraint *c = isl_equality_alloc (isl_local_space_from_space (d2));
1172 for (i = 0; i < n; i++)
1173 if (i != depth_dim)
1174 x = isl_map_equate (x, isl_dim_in, i, isl_dim_out, i);
1176 c = isl_constraint_set_coefficient_si (c, isl_dim_in, depth_dim, 1);
1177 c = isl_constraint_set_coefficient_si (c, isl_dim_out, depth_dim, 1);
1178 x = isl_map_add_constraint (x, c);
1179 return x;
1182 /* Reverse the loop at level DEPTH for all the PBBS. */
1184 isl_union_map *
1185 reverse_loop_for_pbbs (scop_p scop, vec<poly_bb_p> pbbs, int depth)
1187 poly_bb_p pbb;
1188 int i;
1189 isl_space *space = isl_space_from_domain (isl_set_get_space (scop->context));
1190 isl_union_map *res = isl_union_map_empty (space);
1192 for (i = 0; pbbs.iterate (i, &pbb); i++)
1193 res = isl_union_map_add_map (res, reverse_loop_at_level (pbb, depth));
1195 return res;
1199 #endif /* HAVE_isl */