Mark ChangeLog
[official-gcc.git] / gcc / graphite-poly.c
blob7e95255e87be647fd0fdc0d9eacb0050c35ee354
1 /* Graphite polyhedral representation.
2 Copyright (C) 2009-2013 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_cloog
25 #include <isl/set.h>
26 #include <isl/map.h>
27 #include <isl/union_map.h>
28 #include <isl/constraint.h>
29 #include <isl/ilp.h>
30 #include <isl/aff.h>
31 #include <cloog/cloog.h>
32 #include <cloog/isl/domain.h>
33 #ifdef HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE
34 #include <isl/deprecated/int.h>
35 #include <isl/deprecated/ilp_int.h>
36 #endif
37 #endif
39 #include "system.h"
40 #include "coretypes.h"
41 #include "diagnostic-core.h"
42 #include "tree-flow.h"
43 #include "dumpfile.h"
44 #include "gimple-pretty-print.h"
45 #include "cfgloop.h"
46 #include "tree-chrec.h"
47 #include "tree-data-ref.h"
48 #include "tree-scalar-evolution.h"
49 #include "sese.h"
51 #ifdef HAVE_cloog
52 #include "graphite-poly.h"
54 #define OPENSCOP_MAX_STRING 256
57 /* Print to STDERR the GMP value VAL. */
59 DEBUG_FUNCTION void
60 debug_gmp_value (mpz_t val)
62 gmp_fprintf (stderr, "%Zd", val);
65 /* Return the maximal loop depth in SCOP. */
67 int
68 scop_max_loop_depth (scop_p scop)
70 int i;
71 poly_bb_p pbb;
72 int max_nb_loops = 0;
74 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
76 int nb_loops = pbb_dim_iter_domain (pbb);
77 if (max_nb_loops < nb_loops)
78 max_nb_loops = nb_loops;
81 return max_nb_loops;
84 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
85 level. */
87 static void
88 print_scattering_function_1 (FILE *file, poly_bb_p pbb, int verbosity)
90 graphite_dim_t i;
92 if (verbosity > 0)
94 fprintf (file, "# scattering bb_%d (\n", pbb_index (pbb));
95 fprintf (file, "#eq");
97 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
98 fprintf (file, " s%d", (int) i);
100 for (i = 0; i < pbb_nb_local_vars (pbb); i++)
101 fprintf (file, " lv%d", (int) i);
103 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
104 fprintf (file, " i%d", (int) i);
106 for (i = 0; i < pbb_nb_params (pbb); i++)
107 fprintf (file, " p%d", (int) i);
109 fprintf (file, " cst\n");
112 fprintf (file, "isl\n");
113 print_isl_map (file, pbb->transformed ? pbb->transformed : pbb->schedule);
115 if (verbosity > 0)
116 fprintf (file, "#)\n");
119 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
120 level. */
122 void
123 print_scattering_function (FILE *file, poly_bb_p pbb, int verbosity)
125 if (!PBB_TRANSFORMED (pbb))
126 return;
128 if (pbb->schedule || pbb->transformed)
130 if (verbosity > 0)
131 fprintf (file, "# Scattering function is provided\n");
133 fprintf (file, "1\n");
135 else
137 if (verbosity > 0)
138 fprintf (file, "# Scattering function is not provided\n");
140 fprintf (file, "0\n");
141 return;
144 print_scattering_function_1 (file, pbb, verbosity);
146 if (verbosity > 0)
147 fprintf (file, "# Scattering names are not provided\n");
149 fprintf (file, "0\n");
153 /* Prints to FILE the iteration domain of PBB, at some VERBOSITY
154 level. */
156 void
157 print_iteration_domain (FILE *file, poly_bb_p pbb, int verbosity)
159 print_pbb_domain (file, pbb, verbosity);
162 /* Prints to FILE the scattering functions of every PBB of SCOP. */
164 void
165 print_scattering_functions (FILE *file, scop_p scop, int verbosity)
167 int i;
168 poly_bb_p pbb;
170 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
171 print_scattering_function (file, pbb, verbosity);
174 /* Prints to FILE the iteration domains of every PBB of SCOP, at some
175 VERBOSITY level. */
177 void
178 print_iteration_domains (FILE *file, scop_p scop, int verbosity)
180 int i;
181 poly_bb_p pbb;
183 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
184 print_iteration_domain (file, pbb, verbosity);
187 /* Prints to STDERR the scattering function of PBB, at some VERBOSITY
188 level. */
190 DEBUG_FUNCTION void
191 debug_scattering_function (poly_bb_p pbb, int verbosity)
193 print_scattering_function (stderr, pbb, verbosity);
196 /* Prints to STDERR the iteration domain of PBB, at some VERBOSITY
197 level. */
199 DEBUG_FUNCTION void
200 debug_iteration_domain (poly_bb_p pbb, int verbosity)
202 print_iteration_domain (stderr, pbb, verbosity);
205 /* Prints to STDERR the scattering functions of every PBB of SCOP, at
206 some VERBOSITY level. */
208 DEBUG_FUNCTION void
209 debug_scattering_functions (scop_p scop, int verbosity)
211 print_scattering_functions (stderr, scop, verbosity);
214 /* Prints to STDERR the iteration domains of every PBB of SCOP, at
215 some VERBOSITY level. */
217 DEBUG_FUNCTION void
218 debug_iteration_domains (scop_p scop, int verbosity)
220 print_iteration_domains (stderr, scop, verbosity);
223 /* Apply graphite transformations to all the basic blocks of SCOP. */
225 bool
226 apply_poly_transforms (scop_p scop)
228 bool transform_done = false;
230 /* Generate code even if we did not apply any real transformation.
231 This also allows to check the performance for the identity
232 transformation: GIMPLE -> GRAPHITE -> GIMPLE
233 Keep in mind that CLooG optimizes in control, so the loop structure
234 may change, even if we only use -fgraphite-identity. */
235 if (flag_graphite_identity)
236 transform_done = true;
238 if (flag_loop_parallelize_all)
239 transform_done = true;
241 if (flag_loop_block)
242 transform_done |= scop_do_block (scop);
243 else
245 if (flag_loop_strip_mine)
246 transform_done |= scop_do_strip_mine (scop, 0);
248 if (flag_loop_interchange)
249 transform_done |= scop_do_interchange (scop);
252 /* This pass needs to be run at the final stage, as it does not
253 update the lst. */
254 if (flag_loop_optimize_isl)
255 transform_done |= optimize_isl (scop);
257 return transform_done;
260 /* Create a new polyhedral data reference and add it to PBB. It is
261 defined by its ACCESSES, its TYPE, and the number of subscripts
262 NB_SUBSCRIPTS. */
264 void
265 new_poly_dr (poly_bb_p pbb, int dr_base_object_set,
266 enum poly_dr_type type, void *cdr, graphite_dim_t nb_subscripts,
267 isl_map *acc, isl_set *extent)
269 static int id = 0;
270 poly_dr_p pdr = XNEW (struct poly_dr);
272 PDR_ID (pdr) = id++;
273 PDR_BASE_OBJECT_SET (pdr) = dr_base_object_set;
274 PDR_NB_REFS (pdr) = 1;
275 PDR_PBB (pdr) = pbb;
276 pdr->accesses = acc;
277 pdr->extent = extent;
278 PDR_TYPE (pdr) = type;
279 PDR_CDR (pdr) = cdr;
280 PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
281 PBB_DRS (pbb).safe_push (pdr);
284 /* Free polyhedral data reference PDR. */
286 void
287 free_poly_dr (poly_dr_p pdr)
289 isl_map_free (pdr->accesses);
290 isl_set_free (pdr->extent);
291 XDELETE (pdr);
294 /* Create a new polyhedral black box. */
296 poly_bb_p
297 new_poly_bb (scop_p scop, void *black_box)
299 poly_bb_p pbb = XNEW (struct poly_bb);
301 pbb->domain = NULL;
302 pbb->schedule = NULL;
303 pbb->transformed = NULL;
304 pbb->saved = NULL;
305 PBB_SCOP (pbb) = scop;
306 pbb_set_black_box (pbb, black_box);
307 PBB_TRANSFORMED (pbb) = NULL;
308 PBB_SAVED (pbb) = NULL;
309 PBB_ORIGINAL (pbb) = NULL;
310 PBB_DRS (pbb).create (3);
311 PBB_IS_REDUCTION (pbb) = false;
312 GBB_PBB ((gimple_bb_p) black_box) = pbb;
314 return pbb;
317 /* Free polyhedral black box. */
319 void
320 free_poly_bb (poly_bb_p pbb)
322 int i;
323 poly_dr_p pdr;
325 isl_set_free (pbb->domain);
326 isl_map_free (pbb->schedule);
327 isl_map_free (pbb->transformed);
328 isl_map_free (pbb->saved);
330 if (PBB_DRS (pbb).exists ())
331 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
332 free_poly_dr (pdr);
334 PBB_DRS (pbb).release ();
335 XDELETE (pbb);
338 static void
339 print_pdr_access_layout (FILE *file, poly_bb_p pbb, poly_dr_p pdr)
341 graphite_dim_t i;
343 fprintf (file, "# eq");
345 fprintf (file, " alias");
347 for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
348 fprintf (file, " sub%d", (int) i);
350 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
351 fprintf (file, " i%d", (int) i);
353 for (i = 0; i < pbb_nb_params (pbb); i++)
354 fprintf (file, " p%d", (int) i);
356 fprintf (file, " cst\n");
359 /* Prints to FILE the polyhedral data reference PDR, at some VERBOSITY
360 level. */
362 void
363 print_pdr (FILE *file, poly_dr_p pdr, int verbosity)
365 if (verbosity > 1)
367 fprintf (file, "# pdr_%d (", PDR_ID (pdr));
369 switch (PDR_TYPE (pdr))
371 case PDR_READ:
372 fprintf (file, "read \n");
373 break;
375 case PDR_WRITE:
376 fprintf (file, "write \n");
377 break;
379 case PDR_MAY_WRITE:
380 fprintf (file, "may_write \n");
381 break;
383 default:
384 gcc_unreachable ();
387 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
390 if (verbosity > 0)
392 fprintf (file, "# data accesses (\n");
393 print_pdr_access_layout (file, PDR_PBB (pdr), pdr);
396 /* XXX isl dump accesses/subscripts */
398 if (verbosity > 0)
399 fprintf (file, "#)\n");
401 if (verbosity > 1)
402 fprintf (file, "#)\n");
405 /* Prints to STDERR the polyhedral data reference PDR, at some
406 VERBOSITY level. */
408 DEBUG_FUNCTION void
409 debug_pdr (poly_dr_p pdr, int verbosity)
411 print_pdr (stderr, pdr, verbosity);
414 /* Creates a new SCOP containing REGION. */
416 scop_p
417 new_scop (void *region)
419 scop_p scop = XNEW (struct scop);
421 scop->context = NULL;
422 scop->must_raw = NULL;
423 scop->may_raw = NULL;
424 scop->must_raw_no_source = NULL;
425 scop->may_raw_no_source = NULL;
426 scop->must_war = NULL;
427 scop->may_war = NULL;
428 scop->must_war_no_source = NULL;
429 scop->may_war_no_source = NULL;
430 scop->must_waw = NULL;
431 scop->may_waw = NULL;
432 scop->must_waw_no_source = NULL;
433 scop->may_waw_no_source = NULL;
434 scop_set_region (scop, region);
435 SCOP_BBS (scop).create (3);
436 SCOP_ORIGINAL_SCHEDULE (scop) = NULL;
437 SCOP_TRANSFORMED_SCHEDULE (scop) = NULL;
438 SCOP_SAVED_SCHEDULE (scop) = NULL;
439 POLY_SCOP_P (scop) = false;
441 return scop;
444 /* Deletes SCOP. */
446 void
447 free_scop (scop_p scop)
449 int i;
450 poly_bb_p pbb;
452 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
453 free_poly_bb (pbb);
455 SCOP_BBS (scop).release ();
457 isl_set_free (scop->context);
458 isl_union_map_free (scop->must_raw);
459 isl_union_map_free (scop->may_raw);
460 isl_union_map_free (scop->must_raw_no_source);
461 isl_union_map_free (scop->may_raw_no_source);
462 isl_union_map_free (scop->must_war);
463 isl_union_map_free (scop->may_war);
464 isl_union_map_free (scop->must_war_no_source);
465 isl_union_map_free (scop->may_war_no_source);
466 isl_union_map_free (scop->must_waw);
467 isl_union_map_free (scop->may_waw);
468 isl_union_map_free (scop->must_waw_no_source);
469 isl_union_map_free (scop->may_waw_no_source);
470 free_lst (SCOP_ORIGINAL_SCHEDULE (scop));
471 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
472 free_lst (SCOP_SAVED_SCHEDULE (scop));
473 XDELETE (scop);
476 /* Print to FILE the domain of PBB in OpenScop format, at some VERBOSITY
477 level. */
479 static void
480 openscop_print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity)
482 graphite_dim_t i;
483 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
485 if (!pbb->domain)
486 return;
488 if (verbosity > 0)
490 fprintf (file, "\n# Iteration domain of bb_%d (\n", GBB_BB (gbb)->index);
491 fprintf (file, "#eq");
493 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
494 fprintf (file, " i%d", (int) i);
496 for (i = 0; i < pbb_nb_params (pbb); i++)
497 fprintf (file, " p%d", (int) i);
499 fprintf (file, " cst\n");
502 fprintf (file, "XXX isl\n");
504 if (verbosity > 0)
505 fprintf (file, "#)\n");
508 /* Print to FILE the domain of PBB, at some VERBOSITY level. */
510 void
511 print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity ATTRIBUTE_UNUSED)
513 print_isl_set (file, pbb->domain);
516 /* Dump the cases of a graphite basic block GBB on FILE. */
518 static void
519 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
521 int i;
522 gimple stmt;
523 vec<gimple> cases;
525 if (!gbb)
526 return;
528 cases = GBB_CONDITION_CASES (gbb);
529 if (cases.is_empty ())
530 return;
532 fprintf (file, "# cases bb_%d (\n", GBB_BB (gbb)->index);
534 FOR_EACH_VEC_ELT (cases, i, stmt)
536 fprintf (file, "# ");
537 print_gimple_stmt (file, stmt, 0, 0);
540 fprintf (file, "#)\n");
543 /* Dump conditions of a graphite basic block GBB on FILE. */
545 static void
546 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
548 int i;
549 gimple stmt;
550 vec<gimple> conditions;
552 if (!gbb)
553 return;
555 conditions = GBB_CONDITIONS (gbb);
556 if (conditions.is_empty ())
557 return;
559 fprintf (file, "# conditions bb_%d (\n", GBB_BB (gbb)->index);
561 FOR_EACH_VEC_ELT (conditions, i, stmt)
563 fprintf (file, "# ");
564 print_gimple_stmt (file, stmt, 0, 0);
567 fprintf (file, "#)\n");
570 /* Print to FILE all the data references of PBB, at some VERBOSITY
571 level. */
573 void
574 print_pdrs (FILE *file, poly_bb_p pbb, int verbosity)
576 int i;
577 poly_dr_p pdr;
578 int nb_reads = 0;
579 int nb_writes = 0;
581 if (PBB_DRS (pbb).length () == 0)
583 if (verbosity > 0)
584 fprintf (file, "# Access informations are not provided\n");\
585 fprintf (file, "0\n");
586 return;
589 if (verbosity > 1)
590 fprintf (file, "# Data references (\n");
592 if (verbosity > 0)
593 fprintf (file, "# Access informations are provided\n");
594 fprintf (file, "1\n");
596 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
597 if (PDR_TYPE (pdr) == PDR_READ)
598 nb_reads++;
599 else
600 nb_writes++;
602 if (verbosity > 1)
603 fprintf (file, "# Read data references (\n");
605 if (verbosity > 0)
606 fprintf (file, "# Read access informations\n");
607 fprintf (file, "%d\n", nb_reads);
609 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
610 if (PDR_TYPE (pdr) == PDR_READ)
611 print_pdr (file, pdr, verbosity);
613 if (verbosity > 1)
614 fprintf (file, "#)\n");
616 if (verbosity > 1)
617 fprintf (file, "# Write data references (\n");
619 if (verbosity > 0)
620 fprintf (file, "# Write access informations\n");
621 fprintf (file, "%d\n", nb_writes);
623 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
624 if (PDR_TYPE (pdr) != PDR_READ)
625 print_pdr (file, pdr, verbosity);
627 if (verbosity > 1)
628 fprintf (file, "#)\n");
630 if (verbosity > 1)
631 fprintf (file, "#)\n");
634 /* Print to STDERR all the data references of PBB. */
636 DEBUG_FUNCTION void
637 debug_pdrs (poly_bb_p pbb, int verbosity)
639 print_pdrs (stderr, pbb, verbosity);
642 /* Print to FILE the body of PBB, at some VERBOSITY level.
643 If statement_body_provided is false statement body is not printed. */
645 static void
646 print_pbb_body (FILE *file, poly_bb_p pbb, int verbosity,
647 bool statement_body_provided)
649 if (verbosity > 1)
650 fprintf (file, "# Body (\n");
652 if (!statement_body_provided)
654 if (verbosity > 0)
655 fprintf (file, "# Statement body is not provided\n");
657 fprintf (file, "0\n");
659 if (verbosity > 1)
660 fprintf (file, "#)\n");
661 return;
664 if (verbosity > 0)
665 fprintf (file, "# Statement body is provided\n");
666 fprintf (file, "1\n");
668 if (verbosity > 0)
669 fprintf (file, "# Original iterator names\n# Iterator names are not provided yet.\n");
671 if (verbosity > 0)
672 fprintf (file, "# Statement body\n");
674 fprintf (file, "{\n");
675 dump_bb (file, pbb_bb (pbb), 0, 0);
676 fprintf (file, "}\n");
678 if (verbosity > 1)
679 fprintf (file, "#)\n");
682 /* Print to FILE the domain and scattering function of PBB, at some
683 VERBOSITY level. */
685 void
686 print_pbb (FILE *file, poly_bb_p pbb, int verbosity)
688 if (verbosity > 1)
690 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
691 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
692 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
695 openscop_print_pbb_domain (file, pbb, verbosity);
696 print_scattering_function (file, pbb, verbosity);
697 print_pdrs (file, pbb, verbosity);
698 print_pbb_body (file, pbb, verbosity, false);
700 if (verbosity > 1)
701 fprintf (file, "#)\n");
704 /* Print to FILE the parameters of SCOP, at some VERBOSITY level. */
706 void
707 print_scop_params (FILE *file, scop_p scop, int verbosity)
709 int i;
710 tree t;
712 if (verbosity > 1)
713 fprintf (file, "# parameters (\n");
715 if (SESE_PARAMS (SCOP_REGION (scop)).length ())
717 if (verbosity > 0)
718 fprintf (file, "# Parameter names are provided\n");
720 fprintf (file, "1\n");
722 if (verbosity > 0)
723 fprintf (file, "# Parameter names\n");
725 else
727 if (verbosity > 0)
728 fprintf (file, "# Parameter names are not provided\n");
729 fprintf (file, "0\n");
732 FOR_EACH_VEC_ELT (SESE_PARAMS (SCOP_REGION (scop)), i, t)
734 print_generic_expr (file, t, 0);
735 fprintf (file, " ");
738 fprintf (file, "\n");
740 if (verbosity > 1)
741 fprintf (file, "#)\n");
744 /* Print to FILE the context of SCoP in OpenScop format, at some VERBOSITY
745 level. */
747 static void
748 openscop_print_scop_context (FILE *file, scop_p scop, int verbosity)
750 graphite_dim_t i;
752 if (verbosity > 0)
754 fprintf (file, "# Context (\n");
755 fprintf (file, "#eq");
757 for (i = 0; i < scop_nb_params (scop); i++)
758 fprintf (file, " p%d", (int) i);
760 fprintf (file, " cst\n");
763 if (scop->context)
764 /* XXX isl print context */
765 fprintf (file, "XXX isl\n");
766 else
767 fprintf (file, "0 %d 0 0 0 %d\n", (int) scop_nb_params (scop) + 2,
768 (int) scop_nb_params (scop));
770 if (verbosity > 0)
771 fprintf (file, "# )\n");
774 /* Print to FILE the context of SCoP, at some VERBOSITY level. */
776 void
777 print_scop_context (FILE *file, scop_p scop, int verbosity)
779 graphite_dim_t i;
781 if (verbosity > 0)
783 fprintf (file, "# Context (\n");
784 fprintf (file, "#eq");
786 for (i = 0; i < scop_nb_params (scop); i++)
787 fprintf (file, " p%d", (int) i);
789 fprintf (file, " cst\n");
792 if (scop->context)
793 print_isl_set (file, scop->context);
794 else
795 fprintf (file, "no isl context %d\n", (int) scop_nb_params (scop) + 2);
797 if (verbosity > 0)
798 fprintf (file, "# )\n");
801 /* Print to FILE the SCOP, at some VERBOSITY level. */
803 void
804 print_scop (FILE *file, scop_p scop, int verbosity)
806 int i;
807 poly_bb_p pbb;
809 fprintf (file, "SCoP 1\n#(\n");
810 fprintf (file, "# Language\nGimple\n");
811 openscop_print_scop_context (file, scop, verbosity);
812 print_scop_params (file, scop, verbosity);
814 if (verbosity > 0)
815 fprintf (file, "# Number of statements\n");
817 fprintf (file, "%d\n", SCOP_BBS (scop).length ());
819 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
820 print_pbb (file, pbb, verbosity);
822 if (verbosity > 1)
824 fprintf (file, "# original_lst (\n");
825 print_lst (file, SCOP_ORIGINAL_SCHEDULE (scop), 0);
826 fprintf (file, "\n#)\n");
828 fprintf (file, "# transformed_lst (\n");
829 print_lst (file, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
830 fprintf (file, "\n#)\n");
833 fprintf (file, "#)\n");
836 /* Print to FILE the input file that CLooG would expect as input, at
837 some VERBOSITY level. */
839 void
840 print_cloog (FILE *file, scop_p scop, int verbosity)
842 int i;
843 poly_bb_p pbb;
845 fprintf (file, "# SCoP (generated by GCC/Graphite\n");
846 if (verbosity > 0)
847 fprintf (file, "# CLooG output language\n");
848 fprintf (file, "c\n");
850 print_scop_context (file, scop, verbosity);
851 print_scop_params (file, scop, verbosity);
853 if (verbosity > 0)
854 fprintf (file, "# Number of statements\n");
856 fprintf (file, "%d\n", SCOP_BBS (scop).length ());
858 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
860 if (verbosity > 1)
861 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
863 print_pbb_domain (file, pbb, verbosity);
864 fprintf (file, "0 0 0");
866 if (verbosity > 0)
867 fprintf (file, "# For future CLooG options.\n");
868 else
869 fprintf (file, "\n");
871 if (verbosity > 1)
872 fprintf (file, "#)\n");
875 fprintf (file, "0");
876 if (verbosity > 0)
877 fprintf (file, "# Don't set the iterator names.\n");
878 else
879 fprintf (file, "\n");
881 if (verbosity > 0)
882 fprintf (file, "# Number of scattering functions\n");
884 fprintf (file, "%d\n", SCOP_BBS (scop).length ());
886 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
888 if (!(pbb->transformed || pbb->schedule))
889 continue;
891 if (verbosity > 1)
892 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
894 print_scattering_function_1 (file, pbb, verbosity);
896 if (verbosity > 1)
897 fprintf (file, "#)\n");
900 fprintf (file, "0");
901 if (verbosity > 0)
902 fprintf (file, "# Don't set the scattering dimension names.\n");
903 else
904 fprintf (file, "\n");
906 fprintf (file, "#)\n");
909 /* Print to STDERR the domain of PBB, at some VERBOSITY level. */
911 DEBUG_FUNCTION void
912 debug_pbb_domain (poly_bb_p pbb, int verbosity)
914 print_pbb_domain (stderr, pbb, verbosity);
917 /* Print to FILE the domain and scattering function of PBB, at some
918 VERBOSITY level. */
920 DEBUG_FUNCTION void
921 debug_pbb (poly_bb_p pbb, int verbosity)
923 print_pbb (stderr, pbb, verbosity);
926 /* Print to STDERR the context of SCOP, at some VERBOSITY level. */
928 DEBUG_FUNCTION void
929 debug_scop_context (scop_p scop, int verbosity)
931 print_scop_context (stderr, scop, verbosity);
934 /* Print to STDERR the SCOP, at some VERBOSITY level. */
936 DEBUG_FUNCTION void
937 debug_scop (scop_p scop, int verbosity)
939 print_scop (stderr, scop, verbosity);
942 /* Print to STDERR the SCOP under CLooG format, at some VERBOSITY
943 level. */
945 DEBUG_FUNCTION void
946 debug_cloog (scop_p scop, int verbosity)
948 print_cloog (stderr, scop, verbosity);
951 /* Print to STDERR the parameters of SCOP, at some VERBOSITY
952 level. */
954 DEBUG_FUNCTION void
955 debug_scop_params (scop_p scop, int verbosity)
957 print_scop_params (stderr, scop, verbosity);
960 extern isl_ctx *the_isl_ctx;
961 void
962 print_isl_set (FILE *f, isl_set *set)
964 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
965 p = isl_printer_print_set (p, set);
966 isl_printer_free (p);
969 DEBUG_FUNCTION void
970 debug_isl_set (isl_set *set)
972 print_isl_set (stderr, set);
975 void
976 print_isl_map (FILE *f, isl_map *map)
978 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
979 p = isl_printer_print_map (p, map);
980 isl_printer_free (p);
983 DEBUG_FUNCTION void
984 debug_isl_map (isl_map *map)
986 print_isl_map (stderr, map);
989 void
990 print_isl_aff (FILE *f, isl_aff *aff)
992 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
993 p = isl_printer_print_aff (p, aff);
994 isl_printer_free (p);
997 DEBUG_FUNCTION void
998 debug_isl_aff (isl_aff *aff)
1000 print_isl_aff (stderr, aff);
1003 void
1004 print_isl_constraint (FILE *f, isl_constraint *c)
1006 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
1007 p = isl_printer_print_constraint (p, c);
1008 isl_printer_free (p);
1011 DEBUG_FUNCTION void
1012 debug_isl_constraint (isl_constraint *c)
1014 print_isl_constraint (stderr, c);
1017 /* Returns the number of iterations RES of the loop around PBB at
1018 time(scattering) dimension TIME_DEPTH. */
1020 void
1021 pbb_number_of_iterations_at_time (poly_bb_p pbb,
1022 graphite_dim_t time_depth,
1023 mpz_t res)
1025 isl_set *transdomain;
1026 isl_space *dc;
1027 isl_aff *aff;
1028 isl_int isllb, islub;
1030 isl_int_init (isllb);
1031 isl_int_init (islub);
1033 /* Map the iteration domain through the current scatter, and work
1034 on the resulting set. */
1035 transdomain = isl_set_apply (isl_set_copy (pbb->domain),
1036 isl_map_copy (pbb->transformed));
1038 /* Select the time_depth' dimension via an affine expression. */
1039 dc = isl_set_get_space (transdomain);
1040 aff = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
1041 aff = isl_aff_set_coefficient_si (aff, isl_dim_in, time_depth, 1);
1043 /* And find the min/max for that function. */
1044 /* XXX isl check results? */
1045 isl_set_min (transdomain, aff, &isllb);
1046 isl_set_max (transdomain, aff, &islub);
1048 isl_int_sub (islub, islub, isllb);
1049 isl_int_add_ui (islub, islub, 1);
1050 isl_int_get_gmp (islub, res);
1052 isl_int_clear (isllb);
1053 isl_int_clear (islub);
1054 isl_aff_free (aff);
1055 isl_set_free (transdomain);
1058 /* Translates LOOP to LST. */
1060 static lst_p
1061 loop_to_lst (loop_p loop, vec<poly_bb_p> bbs, int *i)
1063 poly_bb_p pbb;
1064 vec<lst_p> seq;
1065 seq.create (5);
1067 for (; bbs.iterate (*i, &pbb); (*i)++)
1069 lst_p stmt;
1070 basic_block bb = GBB_BB (PBB_BLACK_BOX (pbb));
1072 if (bb->loop_father == loop)
1073 stmt = new_lst_stmt (pbb);
1074 else if (flow_bb_inside_loop_p (loop, bb))
1076 loop_p next = loop->inner;
1078 while (next && !flow_bb_inside_loop_p (next, bb))
1079 next = next->next;
1081 stmt = loop_to_lst (next, bbs, i);
1083 else
1085 (*i)--;
1086 return new_lst_loop (seq);
1089 seq.safe_push (stmt);
1092 return new_lst_loop (seq);
1095 /* Reads the original scattering of the SCOP and returns an LST
1096 representing it. */
1098 void
1099 scop_to_lst (scop_p scop)
1101 lst_p res;
1102 int i, n = SCOP_BBS (scop).length ();
1103 vec<lst_p> seq;
1104 seq.create (5);
1105 sese region = SCOP_REGION (scop);
1107 for (i = 0; i < n; i++)
1109 poly_bb_p pbb = SCOP_BBS (scop)[i];
1110 loop_p loop = outermost_loop_in_sese (region, GBB_BB (PBB_BLACK_BOX (pbb)));
1112 if (loop_in_sese_p (loop, region))
1113 res = loop_to_lst (loop, SCOP_BBS (scop), &i);
1114 else
1115 res = new_lst_stmt (pbb);
1117 seq.safe_push (res);
1120 res = new_lst_loop (seq);
1121 SCOP_ORIGINAL_SCHEDULE (scop) = res;
1122 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (res);
1125 /* Print to FILE on a new line COLUMN white spaces. */
1127 static void
1128 lst_indent_to (FILE *file, int column)
1130 int i;
1132 if (column > 0)
1133 fprintf (file, "\n#");
1135 for (i = 0; i < column; i++)
1136 fprintf (file, " ");
1139 /* Print LST to FILE with INDENT spaces of indentation. */
1141 void
1142 print_lst (FILE *file, lst_p lst, int indent)
1144 if (!lst)
1145 return;
1147 lst_indent_to (file, indent);
1149 if (LST_LOOP_P (lst))
1151 int i;
1152 lst_p l;
1154 if (LST_LOOP_FATHER (lst))
1155 fprintf (file, "%d (loop", lst_dewey_number (lst));
1156 else
1157 fprintf (file, "#(root");
1159 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
1160 print_lst (file, l, indent + 2);
1162 fprintf (file, ")");
1164 else
1165 fprintf (file, "%d stmt_%d", lst_dewey_number (lst), pbb_index (LST_PBB (lst)));
1168 /* Print LST to STDERR. */
1170 DEBUG_FUNCTION void
1171 debug_lst (lst_p lst)
1173 print_lst (stderr, lst, 0);
1176 /* Pretty print to FILE the loop statement tree LST in DOT format. */
1178 static void
1179 dot_lst_1 (FILE *file, lst_p lst)
1181 if (!lst)
1182 return;
1184 if (LST_LOOP_P (lst))
1186 int i;
1187 lst_p l;
1189 if (!LST_LOOP_FATHER (lst))
1190 fprintf (file, "L -> L_%d_%d\n",
1191 lst_depth (lst),
1192 lst_dewey_number (lst));
1193 else
1194 fprintf (file, "L_%d_%d -> L_%d_%d\n",
1195 lst_depth (LST_LOOP_FATHER (lst)),
1196 lst_dewey_number (LST_LOOP_FATHER (lst)),
1197 lst_depth (lst),
1198 lst_dewey_number (lst));
1200 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
1201 dot_lst_1 (file, l);
1204 else
1205 fprintf (file, "L_%d_%d -> S_%d\n",
1206 lst_depth (LST_LOOP_FATHER (lst)),
1207 lst_dewey_number (LST_LOOP_FATHER (lst)),
1208 pbb_index (LST_PBB (lst)));
1212 /* Display the LST using dotty. */
1214 DEBUG_FUNCTION void
1215 dot_lst (lst_p lst)
1217 /* When debugging, enable the following code. This cannot be used
1218 in production compilers because it calls "system". */
1219 #if 0
1220 FILE *stream = fopen ("/tmp/lst.dot", "w");
1221 gcc_assert (stream);
1223 fputs ("digraph all {\n", stream);
1224 dot_lst_1 (stream, lst);
1225 fputs ("}\n\n", stream);
1226 fclose (stream);
1228 system ("dotty /tmp/lst.dot &");
1229 #else
1230 fputs ("digraph all {\n", stderr);
1231 dot_lst_1 (stderr, lst);
1232 fputs ("}\n\n", stderr);
1234 #endif
1237 /* Computes a checksum for the code generated by CLooG for SCOP. */
1239 DEBUG_FUNCTION void
1240 cloog_checksum (scop_p scop ATTRIBUTE_UNUSED)
1242 /* When debugging, enable the following code. This cannot be used
1243 in production compilers because it calls "system". */
1244 #if 0
1245 FILE *stream = fopen ("/tmp/scop.cloog", "w");
1246 gcc_assert (stream);
1247 print_cloog (stream, scop, 0);
1248 fclose (stream);
1250 fputs ("\n", stdout);
1251 system ("cloog -compilable 1 /tmp/scop.cloog > /tmp/scop.c ; gcc -O0 -g /tmp/scop.c -lm -o /tmp/scop; /tmp/scop | md5sum ");
1252 #endif
1255 /* Reverse the loop around PBB at level DEPTH. */
1257 isl_map *
1258 reverse_loop_at_level (poly_bb_p pbb, int depth)
1260 unsigned i, depth_dim = psct_dynamic_dim (pbb, depth);
1261 isl_space *d = isl_map_get_space (pbb->transformed);
1262 isl_space *d1 = isl_space_range (d);
1263 unsigned n = isl_space_dim (d1, isl_dim_out);
1264 isl_space *d2 = isl_space_add_dims (d1, isl_dim_in, n);
1265 isl_map *x = isl_map_universe (isl_space_copy (d2));
1266 isl_constraint *c = isl_equality_alloc (isl_local_space_from_space (d2));
1268 for (i = 0; i < n; i++)
1269 if (i != depth_dim)
1270 x = isl_map_equate (x, isl_dim_in, i, isl_dim_out, i);
1272 c = isl_constraint_set_coefficient_si (c, isl_dim_in, depth_dim, 1);
1273 c = isl_constraint_set_coefficient_si (c, isl_dim_out, depth_dim, 1);
1274 x = isl_map_add_constraint (x, c);
1275 return x;
1278 /* Reverse the loop at level DEPTH for all the PBBS. */
1280 isl_union_map *
1281 reverse_loop_for_pbbs (scop_p scop, vec<poly_bb_p> pbbs, int depth)
1283 poly_bb_p pbb;
1284 int i;
1285 isl_space *space = isl_space_from_domain (isl_set_get_space (scop->context));
1286 isl_union_map *res = isl_union_map_empty (space);
1288 for (i = 0; pbbs.iterate (i, &pbb); i++)
1289 res = isl_union_map_add_map (res, reverse_loop_at_level (pbb, depth));
1291 return res;
1295 #endif