* doc/invoke.texi (AVR Options): Document __AVR_ARCH__.
[official-gcc.git] / gcc / graphite-poly.c
blobb66dd507cbf78019a3e465dd35818a059edcc5b6
1 /* Graphite polyhedral representation.
2 Copyright (C) 2009, 2010, 2011 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 #endif
35 #include "system.h"
36 #include "coretypes.h"
37 #include "diagnostic-core.h"
38 #include "tree-flow.h"
39 #include "dumpfile.h"
40 #include "gimple-pretty-print.h"
41 #include "cfgloop.h"
42 #include "tree-chrec.h"
43 #include "tree-data-ref.h"
44 #include "tree-scalar-evolution.h"
45 #include "sese.h"
47 #ifdef HAVE_cloog
48 #include "graphite-poly.h"
50 #define OPENSCOP_MAX_STRING 256
53 /* Print to STDERR the GMP value VAL. */
55 DEBUG_FUNCTION void
56 debug_gmp_value (mpz_t val)
58 char *str = mpz_get_str (0, 10, val);
59 void (*gmp_free) (void *, size_t);
61 fprintf (stderr, "%s", str);
62 mp_get_memory_functions (NULL, NULL, &gmp_free);
63 (*gmp_free) (str, strlen (str) + 1);
66 /* Return the maximal loop depth in SCOP. */
68 int
69 scop_max_loop_depth (scop_p scop)
71 int i;
72 poly_bb_p pbb;
73 int max_nb_loops = 0;
75 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
77 int nb_loops = pbb_dim_iter_domain (pbb);
78 if (max_nb_loops < nb_loops)
79 max_nb_loops = nb_loops;
82 return max_nb_loops;
85 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
86 level. */
88 static void
89 print_scattering_function_1 (FILE *file, poly_bb_p pbb, int verbosity)
91 graphite_dim_t i;
93 if (verbosity > 0)
95 fprintf (file, "# scattering bb_%d (\n", pbb_index (pbb));
96 fprintf (file, "#eq");
98 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
99 fprintf (file, " s%d", (int) i);
101 for (i = 0; i < pbb_nb_local_vars (pbb); i++)
102 fprintf (file, " lv%d", (int) i);
104 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
105 fprintf (file, " i%d", (int) i);
107 for (i = 0; i < pbb_nb_params (pbb); i++)
108 fprintf (file, " p%d", (int) i);
110 fprintf (file, " cst\n");
113 fprintf (file, "isl\n");
114 print_isl_map (file, pbb->transformed ? pbb->transformed : pbb->schedule);
116 if (verbosity > 0)
117 fprintf (file, "#)\n");
120 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
121 level. */
123 void
124 print_scattering_function (FILE *file, poly_bb_p pbb, int verbosity)
126 if (!PBB_TRANSFORMED (pbb))
127 return;
129 if (pbb->schedule || pbb->transformed)
131 if (verbosity > 0)
132 fprintf (file, "# Scattering function is provided\n");
134 fprintf (file, "1\n");
136 else
138 if (verbosity > 0)
139 fprintf (file, "# Scattering function is not provided\n");
141 fprintf (file, "0\n");
142 return;
145 print_scattering_function_1 (file, pbb, verbosity);
147 if (verbosity > 0)
148 fprintf (file, "# Scattering names are not provided\n");
150 fprintf (file, "0\n");
154 /* Prints to FILE the iteration domain of PBB, at some VERBOSITY
155 level. */
157 void
158 print_iteration_domain (FILE *file, poly_bb_p pbb, int verbosity)
160 print_pbb_domain (file, pbb, verbosity);
163 /* Prints to FILE the scattering functions of every PBB of SCOP. */
165 void
166 print_scattering_functions (FILE *file, scop_p scop, int verbosity)
168 int i;
169 poly_bb_p pbb;
171 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
172 print_scattering_function (file, pbb, verbosity);
175 /* Prints to FILE the iteration domains of every PBB of SCOP, at some
176 VERBOSITY level. */
178 void
179 print_iteration_domains (FILE *file, scop_p scop, int verbosity)
181 int i;
182 poly_bb_p pbb;
184 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
185 print_iteration_domain (file, pbb, verbosity);
188 /* Prints to STDERR the scattering function of PBB, at some VERBOSITY
189 level. */
191 DEBUG_FUNCTION void
192 debug_scattering_function (poly_bb_p pbb, int verbosity)
194 print_scattering_function (stderr, pbb, verbosity);
197 /* Prints to STDERR the iteration domain of PBB, at some VERBOSITY
198 level. */
200 DEBUG_FUNCTION void
201 debug_iteration_domain (poly_bb_p pbb, int verbosity)
203 print_iteration_domain (stderr, pbb, verbosity);
206 /* Prints to STDERR the scattering functions of every PBB of SCOP, at
207 some VERBOSITY level. */
209 DEBUG_FUNCTION void
210 debug_scattering_functions (scop_p scop, int verbosity)
212 print_scattering_functions (stderr, scop, verbosity);
215 /* Prints to STDERR the iteration domains of every PBB of SCOP, at
216 some VERBOSITY level. */
218 DEBUG_FUNCTION void
219 debug_iteration_domains (scop_p scop, int verbosity)
221 print_iteration_domains (stderr, scop, verbosity);
224 /* Apply graphite transformations to all the basic blocks of SCOP. */
226 bool
227 apply_poly_transforms (scop_p scop)
229 bool transform_done = false;
231 /* Generate code even if we did not apply any real transformation.
232 This also allows to check the performance for the identity
233 transformation: GIMPLE -> GRAPHITE -> GIMPLE
234 Keep in mind that CLooG optimizes in control, so the loop structure
235 may change, even if we only use -fgraphite-identity. */
236 if (flag_graphite_identity)
237 transform_done = true;
239 if (flag_loop_parallelize_all)
240 transform_done = true;
242 if (flag_loop_block)
243 transform_done |= scop_do_block (scop);
244 else
246 if (flag_loop_strip_mine)
247 transform_done |= scop_do_strip_mine (scop, 0);
249 if (flag_loop_interchange)
250 transform_done |= scop_do_interchange (scop);
253 /* This pass needs to be run at the final stage, as it does not
254 update the lst. */
255 if (flag_loop_optimize_isl)
256 transform_done |= optimize_isl (scop);
258 return transform_done;
261 /* Create a new polyhedral data reference and add it to PBB. It is
262 defined by its ACCESSES, its TYPE, and the number of subscripts
263 NB_SUBSCRIPTS. */
265 void
266 new_poly_dr (poly_bb_p pbb, int dr_base_object_set,
267 enum poly_dr_type type, void *cdr, graphite_dim_t nb_subscripts,
268 isl_map *acc, isl_set *extent)
270 static int id = 0;
271 poly_dr_p pdr = XNEW (struct poly_dr);
273 PDR_ID (pdr) = id++;
274 PDR_BASE_OBJECT_SET (pdr) = dr_base_object_set;
275 PDR_NB_REFS (pdr) = 1;
276 PDR_PBB (pdr) = pbb;
277 pdr->accesses = acc;
278 pdr->extent = extent;
279 PDR_TYPE (pdr) = type;
280 PDR_CDR (pdr) = cdr;
281 PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
282 VEC_safe_push (poly_dr_p, heap, PBB_DRS (pbb), pdr);
285 /* Free polyhedral data reference PDR. */
287 void
288 free_poly_dr (poly_dr_p pdr)
290 isl_map_free (pdr->accesses);
291 isl_set_free (pdr->extent);
292 XDELETE (pdr);
295 /* Create a new polyhedral black box. */
297 poly_bb_p
298 new_poly_bb (scop_p scop, void *black_box)
300 poly_bb_p pbb = XNEW (struct poly_bb);
302 pbb->domain = NULL;
303 pbb->schedule = NULL;
304 pbb->transformed = NULL;
305 pbb->saved = NULL;
306 PBB_SCOP (pbb) = scop;
307 pbb_set_black_box (pbb, black_box);
308 PBB_TRANSFORMED (pbb) = NULL;
309 PBB_SAVED (pbb) = NULL;
310 PBB_ORIGINAL (pbb) = NULL;
311 PBB_DRS (pbb) = VEC_alloc (poly_dr_p, heap, 3);
312 PBB_IS_REDUCTION (pbb) = false;
313 GBB_PBB ((gimple_bb_p) black_box) = pbb;
315 return pbb;
318 /* Free polyhedral black box. */
320 void
321 free_poly_bb (poly_bb_p pbb)
323 int i;
324 poly_dr_p pdr;
326 isl_set_free (pbb->domain);
327 isl_map_free (pbb->schedule);
328 isl_map_free (pbb->transformed);
329 isl_map_free (pbb->saved);
331 if (PBB_DRS (pbb))
332 FOR_EACH_VEC_ELT (poly_dr_p, PBB_DRS (pbb), i, pdr)
333 free_poly_dr (pdr);
335 VEC_free (poly_dr_p, heap, PBB_DRS (pbb));
336 XDELETE (pbb);
339 static void
340 print_pdr_access_layout (FILE *file, poly_bb_p pbb, poly_dr_p pdr)
342 graphite_dim_t i;
344 fprintf (file, "# eq");
346 fprintf (file, " alias");
348 for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
349 fprintf (file, " sub%d", (int) i);
351 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
352 fprintf (file, " i%d", (int) i);
354 for (i = 0; i < pbb_nb_params (pbb); i++)
355 fprintf (file, " p%d", (int) i);
357 fprintf (file, " cst\n");
360 /* Prints to FILE the polyhedral data reference PDR, at some VERBOSITY
361 level. */
363 void
364 print_pdr (FILE *file, poly_dr_p pdr, int verbosity)
366 if (verbosity > 1)
368 fprintf (file, "# pdr_%d (", PDR_ID (pdr));
370 switch (PDR_TYPE (pdr))
372 case PDR_READ:
373 fprintf (file, "read \n");
374 break;
376 case PDR_WRITE:
377 fprintf (file, "write \n");
378 break;
380 case PDR_MAY_WRITE:
381 fprintf (file, "may_write \n");
382 break;
384 default:
385 gcc_unreachable ();
388 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
391 if (verbosity > 0)
393 fprintf (file, "# data accesses (\n");
394 print_pdr_access_layout (file, PDR_PBB (pdr), pdr);
397 /* XXX isl dump accesses/subscripts */
399 if (verbosity > 0)
400 fprintf (file, "#)\n");
402 if (verbosity > 1)
403 fprintf (file, "#)\n");
406 /* Prints to STDERR the polyhedral data reference PDR, at some
407 VERBOSITY level. */
409 DEBUG_FUNCTION void
410 debug_pdr (poly_dr_p pdr, int verbosity)
412 print_pdr (stderr, pdr, verbosity);
415 /* Creates a new SCOP containing REGION. */
417 scop_p
418 new_scop (void *region)
420 scop_p scop = XNEW (struct scop);
422 scop->context = NULL;
423 scop->must_raw = NULL;
424 scop->may_raw = NULL;
425 scop->must_raw_no_source = NULL;
426 scop->may_raw_no_source = NULL;
427 scop->must_war = NULL;
428 scop->may_war = NULL;
429 scop->must_war_no_source = NULL;
430 scop->may_war_no_source = NULL;
431 scop->must_waw = NULL;
432 scop->may_waw = NULL;
433 scop->must_waw_no_source = NULL;
434 scop->may_waw_no_source = NULL;
435 scop_set_region (scop, region);
436 SCOP_BBS (scop) = VEC_alloc (poly_bb_p, heap, 3);
437 SCOP_ORIGINAL_SCHEDULE (scop) = NULL;
438 SCOP_TRANSFORMED_SCHEDULE (scop) = NULL;
439 SCOP_SAVED_SCHEDULE (scop) = NULL;
440 POLY_SCOP_P (scop) = false;
442 return scop;
445 /* Deletes SCOP. */
447 void
448 free_scop (scop_p scop)
450 int i;
451 poly_bb_p pbb;
453 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
454 free_poly_bb (pbb);
456 VEC_free (poly_bb_p, heap, SCOP_BBS (scop));
458 isl_set_free (scop->context);
459 isl_union_map_free (scop->must_raw);
460 isl_union_map_free (scop->may_raw);
461 isl_union_map_free (scop->must_raw_no_source);
462 isl_union_map_free (scop->may_raw_no_source);
463 isl_union_map_free (scop->must_war);
464 isl_union_map_free (scop->may_war);
465 isl_union_map_free (scop->must_war_no_source);
466 isl_union_map_free (scop->may_war_no_source);
467 isl_union_map_free (scop->must_waw);
468 isl_union_map_free (scop->may_waw);
469 isl_union_map_free (scop->must_waw_no_source);
470 isl_union_map_free (scop->may_waw_no_source);
471 free_lst (SCOP_ORIGINAL_SCHEDULE (scop));
472 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
473 free_lst (SCOP_SAVED_SCHEDULE (scop));
474 XDELETE (scop);
477 /* Print to FILE the domain of PBB in OpenScop format, at some VERBOSITY
478 level. */
480 static void
481 openscop_print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity)
483 graphite_dim_t i;
484 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
486 if (!pbb->domain)
487 return;
489 if (verbosity > 0)
491 fprintf (file, "\n# Iteration domain of bb_%d (\n", GBB_BB (gbb)->index);
492 fprintf (file, "#eq");
494 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
495 fprintf (file, " i%d", (int) i);
497 for (i = 0; i < pbb_nb_params (pbb); i++)
498 fprintf (file, " p%d", (int) i);
500 fprintf (file, " cst\n");
503 fprintf (file, "XXX isl\n");
505 if (verbosity > 0)
506 fprintf (file, "#)\n");
509 /* Print to FILE the domain of PBB, at some VERBOSITY level. */
511 void
512 print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity ATTRIBUTE_UNUSED)
514 print_isl_set (file, pbb->domain);
517 /* Dump the cases of a graphite basic block GBB on FILE. */
519 static void
520 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
522 int i;
523 gimple stmt;
524 VEC (gimple, heap) *cases;
526 if (!gbb)
527 return;
529 cases = GBB_CONDITION_CASES (gbb);
530 if (VEC_empty (gimple, cases))
531 return;
533 fprintf (file, "# cases bb_%d (\n", GBB_BB (gbb)->index);
535 FOR_EACH_VEC_ELT (gimple, cases, i, stmt)
537 fprintf (file, "# ");
538 print_gimple_stmt (file, stmt, 0, 0);
541 fprintf (file, "#)\n");
544 /* Dump conditions of a graphite basic block GBB on FILE. */
546 static void
547 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
549 int i;
550 gimple stmt;
551 VEC (gimple, heap) *conditions;
553 if (!gbb)
554 return;
556 conditions = GBB_CONDITIONS (gbb);
557 if (VEC_empty (gimple, conditions))
558 return;
560 fprintf (file, "# conditions bb_%d (\n", GBB_BB (gbb)->index);
562 FOR_EACH_VEC_ELT (gimple, conditions, i, stmt)
564 fprintf (file, "# ");
565 print_gimple_stmt (file, stmt, 0, 0);
568 fprintf (file, "#)\n");
571 /* Print to FILE all the data references of PBB, at some VERBOSITY
572 level. */
574 void
575 print_pdrs (FILE *file, poly_bb_p pbb, int verbosity)
577 int i;
578 poly_dr_p pdr;
579 int nb_reads = 0;
580 int nb_writes = 0;
582 if (VEC_length (poly_dr_p, PBB_DRS (pbb)) == 0)
584 if (verbosity > 0)
585 fprintf (file, "# Access informations are not provided\n");\
586 fprintf (file, "0\n");
587 return;
590 if (verbosity > 1)
591 fprintf (file, "# Data references (\n");
593 if (verbosity > 0)
594 fprintf (file, "# Access informations are provided\n");
595 fprintf (file, "1\n");
597 FOR_EACH_VEC_ELT (poly_dr_p, PBB_DRS (pbb), i, pdr)
598 if (PDR_TYPE (pdr) == PDR_READ)
599 nb_reads++;
600 else
601 nb_writes++;
603 if (verbosity > 1)
604 fprintf (file, "# Read data references (\n");
606 if (verbosity > 0)
607 fprintf (file, "# Read access informations\n");
608 fprintf (file, "%d\n", nb_reads);
610 FOR_EACH_VEC_ELT (poly_dr_p, PBB_DRS (pbb), i, pdr)
611 if (PDR_TYPE (pdr) == PDR_READ)
612 print_pdr (file, pdr, verbosity);
614 if (verbosity > 1)
615 fprintf (file, "#)\n");
617 if (verbosity > 1)
618 fprintf (file, "# Write data references (\n");
620 if (verbosity > 0)
621 fprintf (file, "# Write access informations\n");
622 fprintf (file, "%d\n", nb_writes);
624 FOR_EACH_VEC_ELT (poly_dr_p, PBB_DRS (pbb), i, pdr)
625 if (PDR_TYPE (pdr) != PDR_READ)
626 print_pdr (file, pdr, verbosity);
628 if (verbosity > 1)
629 fprintf (file, "#)\n");
631 if (verbosity > 1)
632 fprintf (file, "#)\n");
635 /* Print to STDERR all the data references of PBB. */
637 DEBUG_FUNCTION void
638 debug_pdrs (poly_bb_p pbb, int verbosity)
640 print_pdrs (stderr, pbb, verbosity);
643 /* Print to FILE the body of PBB, at some VERBOSITY level.
644 If statement_body_provided is false statement body is not printed. */
646 static void
647 print_pbb_body (FILE *file, poly_bb_p pbb, int verbosity,
648 bool statement_body_provided)
650 if (verbosity > 1)
651 fprintf (file, "# Body (\n");
653 if (!statement_body_provided)
655 if (verbosity > 0)
656 fprintf (file, "# Statement body is not provided\n");
658 fprintf (file, "0\n");
660 if (verbosity > 1)
661 fprintf (file, "#)\n");
662 return;
665 if (verbosity > 0)
666 fprintf (file, "# Statement body is provided\n");
667 fprintf (file, "1\n");
669 if (verbosity > 0)
670 fprintf (file, "# Original iterator names\n# Iterator names are not provided yet.\n");
672 if (verbosity > 0)
673 fprintf (file, "# Statement body\n");
675 fprintf (file, "{\n");
676 dump_bb (file, pbb_bb (pbb), 0, 0);
677 fprintf (file, "}\n");
679 if (verbosity > 1)
680 fprintf (file, "#)\n");
683 /* Print to FILE the domain and scattering function of PBB, at some
684 VERBOSITY level. */
686 void
687 print_pbb (FILE *file, poly_bb_p pbb, int verbosity)
689 if (verbosity > 1)
691 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
692 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
693 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
696 openscop_print_pbb_domain (file, pbb, verbosity);
697 print_scattering_function (file, pbb, verbosity);
698 print_pdrs (file, pbb, verbosity);
699 print_pbb_body (file, pbb, verbosity, false);
701 if (verbosity > 1)
702 fprintf (file, "#)\n");
705 /* Print to FILE the parameters of SCOP, at some VERBOSITY level. */
707 void
708 print_scop_params (FILE *file, scop_p scop, int verbosity)
710 int i;
711 tree t;
713 if (verbosity > 1)
714 fprintf (file, "# parameters (\n");
716 if (VEC_length (tree, SESE_PARAMS (SCOP_REGION (scop))))
718 if (verbosity > 0)
719 fprintf (file, "# Parameter names are provided\n");
721 fprintf (file, "1\n");
723 if (verbosity > 0)
724 fprintf (file, "# Parameter names\n");
726 else
728 if (verbosity > 0)
729 fprintf (file, "# Parameter names are not provided\n");
730 fprintf (file, "0\n");
733 FOR_EACH_VEC_ELT (tree, SESE_PARAMS (SCOP_REGION (scop)), i, t)
735 print_generic_expr (file, t, 0);
736 fprintf (file, " ");
739 fprintf (file, "\n");
741 if (verbosity > 1)
742 fprintf (file, "#)\n");
745 /* Print to FILE the context of SCoP in OpenScop format, at some VERBOSITY
746 level. */
748 static void
749 openscop_print_scop_context (FILE *file, scop_p scop, int verbosity)
751 graphite_dim_t i;
753 if (verbosity > 0)
755 fprintf (file, "# Context (\n");
756 fprintf (file, "#eq");
758 for (i = 0; i < scop_nb_params (scop); i++)
759 fprintf (file, " p%d", (int) i);
761 fprintf (file, " cst\n");
764 if (scop->context)
765 /* XXX isl print context */
766 fprintf (file, "XXX isl\n");
767 else
768 fprintf (file, "0 %d 0 0 0 %d\n", (int) scop_nb_params (scop) + 2,
769 (int) scop_nb_params (scop));
771 if (verbosity > 0)
772 fprintf (file, "# )\n");
775 /* Print to FILE the context of SCoP, at some VERBOSITY level. */
777 void
778 print_scop_context (FILE *file, scop_p scop, int verbosity)
780 graphite_dim_t i;
782 if (verbosity > 0)
784 fprintf (file, "# Context (\n");
785 fprintf (file, "#eq");
787 for (i = 0; i < scop_nb_params (scop); i++)
788 fprintf (file, " p%d", (int) i);
790 fprintf (file, " cst\n");
793 if (scop->context)
794 print_isl_set (file, scop->context);
795 else
796 fprintf (file, "no isl context %d\n", (int) scop_nb_params (scop) + 2);
798 if (verbosity > 0)
799 fprintf (file, "# )\n");
802 /* Print to FILE the SCOP, at some VERBOSITY level. */
804 void
805 print_scop (FILE *file, scop_p scop, int verbosity)
807 int i;
808 poly_bb_p pbb;
810 fprintf (file, "SCoP 1\n#(\n");
811 fprintf (file, "# Language\nGimple\n");
812 openscop_print_scop_context (file, scop, verbosity);
813 print_scop_params (file, scop, verbosity);
815 if (verbosity > 0)
816 fprintf (file, "# Number of statements\n");
818 fprintf (file, "%d\n",VEC_length (poly_bb_p, SCOP_BBS (scop)));
820 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
821 print_pbb (file, pbb, verbosity);
823 if (verbosity > 1)
825 fprintf (file, "# original_lst (\n");
826 print_lst (file, SCOP_ORIGINAL_SCHEDULE (scop), 0);
827 fprintf (file, "\n#)\n");
829 fprintf (file, "# transformed_lst (\n");
830 print_lst (file, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
831 fprintf (file, "\n#)\n");
834 fprintf (file, "#)\n");
837 /* Print to FILE the input file that CLooG would expect as input, at
838 some VERBOSITY level. */
840 void
841 print_cloog (FILE *file, scop_p scop, int verbosity)
843 int i;
844 poly_bb_p pbb;
846 fprintf (file, "# SCoP (generated by GCC/Graphite\n");
847 if (verbosity > 0)
848 fprintf (file, "# CLooG output language\n");
849 fprintf (file, "c\n");
851 print_scop_context (file, scop, verbosity);
852 print_scop_params (file, scop, verbosity);
854 if (verbosity > 0)
855 fprintf (file, "# Number of statements\n");
857 fprintf (file, "%d\n", VEC_length (poly_bb_p, SCOP_BBS (scop)));
859 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
861 if (verbosity > 1)
862 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
864 print_pbb_domain (file, pbb, verbosity);
865 fprintf (file, "0 0 0");
867 if (verbosity > 0)
868 fprintf (file, "# For future CLooG options.\n");
869 else
870 fprintf (file, "\n");
872 if (verbosity > 1)
873 fprintf (file, "#)\n");
876 fprintf (file, "0");
877 if (verbosity > 0)
878 fprintf (file, "# Don't set the iterator names.\n");
879 else
880 fprintf (file, "\n");
882 if (verbosity > 0)
883 fprintf (file, "# Number of scattering functions\n");
885 fprintf (file, "%d\n", VEC_length (poly_bb_p, SCOP_BBS (scop)));
887 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
889 if (!(pbb->transformed || pbb->schedule))
890 continue;
892 if (verbosity > 1)
893 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
895 print_scattering_function_1 (file, pbb, verbosity);
897 if (verbosity > 1)
898 fprintf (file, "#)\n");
901 fprintf (file, "0");
902 if (verbosity > 0)
903 fprintf (file, "# Don't set the scattering dimension names.\n");
904 else
905 fprintf (file, "\n");
907 fprintf (file, "#)\n");
910 /* Print to STDERR the domain of PBB, at some VERBOSITY level. */
912 DEBUG_FUNCTION void
913 debug_pbb_domain (poly_bb_p pbb, int verbosity)
915 print_pbb_domain (stderr, pbb, verbosity);
918 /* Print to FILE the domain and scattering function of PBB, at some
919 VERBOSITY level. */
921 DEBUG_FUNCTION void
922 debug_pbb (poly_bb_p pbb, int verbosity)
924 print_pbb (stderr, pbb, verbosity);
927 /* Print to STDERR the context of SCOP, at some VERBOSITY level. */
929 DEBUG_FUNCTION void
930 debug_scop_context (scop_p scop, int verbosity)
932 print_scop_context (stderr, scop, verbosity);
935 /* Print to STDERR the SCOP, at some VERBOSITY level. */
937 DEBUG_FUNCTION void
938 debug_scop (scop_p scop, int verbosity)
940 print_scop (stderr, scop, verbosity);
943 /* Print to STDERR the SCOP under CLooG format, at some VERBOSITY
944 level. */
946 DEBUG_FUNCTION void
947 debug_cloog (scop_p scop, int verbosity)
949 print_cloog (stderr, scop, verbosity);
952 /* Print to STDERR the parameters of SCOP, at some VERBOSITY
953 level. */
955 DEBUG_FUNCTION void
956 debug_scop_params (scop_p scop, int verbosity)
958 print_scop_params (stderr, scop, verbosity);
961 extern isl_ctx *the_isl_ctx;
962 void
963 print_isl_set (FILE *f, isl_set *set)
965 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
966 p = isl_printer_print_set (p, set);
967 isl_printer_free (p);
970 DEBUG_FUNCTION void
971 debug_isl_set (isl_set *set)
973 print_isl_set (stderr, set);
976 void
977 print_isl_map (FILE *f, isl_map *map)
979 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
980 p = isl_printer_print_map (p, map);
981 isl_printer_free (p);
984 DEBUG_FUNCTION void
985 debug_isl_map (isl_map *map)
987 print_isl_map (stderr, map);
990 void
991 print_isl_aff (FILE *f, isl_aff *aff)
993 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
994 p = isl_printer_print_aff (p, aff);
995 isl_printer_free (p);
998 DEBUG_FUNCTION void
999 debug_isl_aff (isl_aff *aff)
1001 print_isl_aff (stderr, aff);
1004 void
1005 print_isl_constraint (FILE *f, isl_constraint *c)
1007 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
1008 p = isl_printer_print_constraint (p, c);
1009 isl_printer_free (p);
1012 DEBUG_FUNCTION void
1013 debug_isl_constraint (isl_constraint *c)
1015 print_isl_constraint (stderr, c);
1018 /* Returns the number of iterations RES of the loop around PBB at
1019 time(scattering) dimension TIME_DEPTH. */
1021 void
1022 pbb_number_of_iterations_at_time (poly_bb_p pbb,
1023 graphite_dim_t time_depth,
1024 mpz_t res)
1026 isl_set *transdomain;
1027 isl_space *dc;
1028 isl_aff *aff;
1029 isl_int isllb, islub;
1031 isl_int_init (isllb);
1032 isl_int_init (islub);
1034 /* Map the iteration domain through the current scatter, and work
1035 on the resulting set. */
1036 transdomain = isl_set_apply (isl_set_copy (pbb->domain),
1037 isl_map_copy (pbb->transformed));
1039 /* Select the time_depth' dimension via an affine expression. */
1040 dc = isl_set_get_space (transdomain);
1041 aff = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
1042 aff = isl_aff_set_coefficient_si (aff, isl_dim_in, time_depth, 1);
1044 /* And find the min/max for that function. */
1045 /* XXX isl check results? */
1046 isl_set_min (transdomain, aff, &isllb);
1047 isl_set_max (transdomain, aff, &islub);
1049 isl_int_sub (islub, islub, isllb);
1050 isl_int_add_ui (islub, islub, 1);
1051 isl_int_get_gmp (islub, res);
1053 isl_int_clear (isllb);
1054 isl_int_clear (islub);
1055 isl_aff_free (aff);
1056 isl_set_free (transdomain);
1059 /* Translates LOOP to LST. */
1061 static lst_p
1062 loop_to_lst (loop_p loop, VEC (poly_bb_p, heap) *bbs, int *i)
1064 poly_bb_p pbb;
1065 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
1067 for (; VEC_iterate (poly_bb_p, bbs, *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 VEC_safe_push (lst_p, heap, seq, 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 = VEC_length (poly_bb_p, SCOP_BBS (scop));
1103 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
1104 sese region = SCOP_REGION (scop);
1106 for (i = 0; i < n; i++)
1108 poly_bb_p pbb = VEC_index (poly_bb_p, SCOP_BBS (scop), i);
1109 loop_p loop = outermost_loop_in_sese (region, GBB_BB (PBB_BLACK_BOX (pbb)));
1111 if (loop_in_sese_p (loop, region))
1112 res = loop_to_lst (loop, SCOP_BBS (scop), &i);
1113 else
1114 res = new_lst_stmt (pbb);
1116 VEC_safe_push (lst_p, heap, seq, res);
1119 res = new_lst_loop (seq);
1120 SCOP_ORIGINAL_SCHEDULE (scop) = res;
1121 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (res);
1124 /* Print to FILE on a new line COLUMN white spaces. */
1126 static void
1127 lst_indent_to (FILE *file, int column)
1129 int i;
1131 if (column > 0)
1132 fprintf (file, "\n#");
1134 for (i = 0; i < column; i++)
1135 fprintf (file, " ");
1138 /* Print LST to FILE with INDENT spaces of indentation. */
1140 void
1141 print_lst (FILE *file, lst_p lst, int indent)
1143 if (!lst)
1144 return;
1146 lst_indent_to (file, indent);
1148 if (LST_LOOP_P (lst))
1150 int i;
1151 lst_p l;
1153 if (LST_LOOP_FATHER (lst))
1154 fprintf (file, "%d (loop", lst_dewey_number (lst));
1155 else
1156 fprintf (file, "#(root");
1158 FOR_EACH_VEC_ELT (lst_p, LST_SEQ (lst), i, l)
1159 print_lst (file, l, indent + 2);
1161 fprintf (file, ")");
1163 else
1164 fprintf (file, "%d stmt_%d", lst_dewey_number (lst), pbb_index (LST_PBB (lst)));
1167 /* Print LST to STDERR. */
1169 DEBUG_FUNCTION void
1170 debug_lst (lst_p lst)
1172 print_lst (stderr, lst, 0);
1175 /* Pretty print to FILE the loop statement tree LST in DOT format. */
1177 static void
1178 dot_lst_1 (FILE *file, lst_p lst)
1180 if (!lst)
1181 return;
1183 if (LST_LOOP_P (lst))
1185 int i;
1186 lst_p l;
1188 if (!LST_LOOP_FATHER (lst))
1189 fprintf (file, "L -> L_%d_%d\n",
1190 lst_depth (lst),
1191 lst_dewey_number (lst));
1192 else
1193 fprintf (file, "L_%d_%d -> L_%d_%d\n",
1194 lst_depth (LST_LOOP_FATHER (lst)),
1195 lst_dewey_number (LST_LOOP_FATHER (lst)),
1196 lst_depth (lst),
1197 lst_dewey_number (lst));
1199 FOR_EACH_VEC_ELT (lst_p, LST_SEQ (lst), i, l)
1200 dot_lst_1 (file, l);
1203 else
1204 fprintf (file, "L_%d_%d -> S_%d\n",
1205 lst_depth (LST_LOOP_FATHER (lst)),
1206 lst_dewey_number (LST_LOOP_FATHER (lst)),
1207 pbb_index (LST_PBB (lst)));
1211 /* Display the LST using dotty. */
1213 DEBUG_FUNCTION void
1214 dot_lst (lst_p lst)
1216 /* When debugging, enable the following code. This cannot be used
1217 in production compilers because it calls "system". */
1218 #if 0
1219 FILE *stream = fopen ("/tmp/lst.dot", "w");
1220 gcc_assert (stream);
1222 fputs ("digraph all {\n", stream);
1223 dot_lst_1 (stream, lst);
1224 fputs ("}\n\n", stream);
1225 fclose (stream);
1227 system ("dotty /tmp/lst.dot &");
1228 #else
1229 fputs ("digraph all {\n", stderr);
1230 dot_lst_1 (stderr, lst);
1231 fputs ("}\n\n", stderr);
1233 #endif
1236 /* Computes a checksum for the code generated by CLooG for SCOP. */
1238 DEBUG_FUNCTION void
1239 cloog_checksum (scop_p scop ATTRIBUTE_UNUSED)
1241 /* When debugging, enable the following code. This cannot be used
1242 in production compilers because it calls "system". */
1243 #if 0
1244 FILE *stream = fopen ("/tmp/scop.cloog", "w");
1245 gcc_assert (stream);
1246 print_cloog (stream, scop, 0);
1247 fclose (stream);
1249 fputs ("\n", stdout);
1250 system ("cloog -compilable 1 /tmp/scop.cloog > /tmp/scop.c ; gcc -O0 -g /tmp/scop.c -lm -o /tmp/scop; /tmp/scop | md5sum ");
1251 #endif
1254 /* Reverse the loop around PBB at level DEPTH. */
1256 isl_map *
1257 reverse_loop_at_level (poly_bb_p pbb, int depth)
1259 unsigned i, depth_dim = psct_dynamic_dim (pbb, depth);
1260 isl_space *d = isl_map_get_space (pbb->transformed);
1261 isl_space *d1 = isl_space_range (d);
1262 unsigned n = isl_space_dim (d1, isl_dim_out);
1263 isl_space *d2 = isl_space_add_dims (d1, isl_dim_in, n);
1264 isl_map *x = isl_map_universe (isl_space_copy (d2));
1265 isl_constraint *c = isl_equality_alloc (isl_local_space_from_space (d2));
1267 for (i = 0; i < n; i++)
1268 if (i != depth_dim)
1269 x = isl_map_equate (x, isl_dim_in, i, isl_dim_out, i);
1271 c = isl_constraint_set_coefficient_si (c, isl_dim_in, depth_dim, 1);
1272 c = isl_constraint_set_coefficient_si (c, isl_dim_out, depth_dim, 1);
1273 x = isl_map_add_constraint (x, c);
1274 return x;
1277 /* Reverse the loop at level DEPTH for all the PBBS. */
1279 isl_union_map *
1280 reverse_loop_for_pbbs (scop_p scop, VEC (poly_bb_p, heap) *pbbs, int depth)
1282 poly_bb_p pbb;
1283 int i;
1284 isl_space *space = isl_space_from_domain (isl_set_get_space (scop->context));
1285 isl_union_map *res = isl_union_map_empty (space);
1287 for (i = 0; VEC_iterate (poly_bb_p, pbbs, i, pbb); i++)
1288 res = isl_union_map_add_map (res, reverse_loop_at_level (pbb, depth));
1290 return res;
1294 #endif