Daily bump.
[official-gcc.git] / gcc / graphite-poly.c
blob57f62cd2ba1c3e9819c6767888642692092a2c37
1 /* Graphite polyhedral representation.
2 Copyright (C) 2009-2014 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 #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 <isl/val.h>
32 #if defined(__cplusplus)
33 extern "C" {
34 #endif
35 #include <isl/val_gmp.h>
36 #if defined(__cplusplus)
38 #endif
39 #ifdef HAVE_cloog
40 #include <cloog/cloog.h>
41 #include <cloog/isl/domain.h>
42 #endif
43 #endif
45 #include "system.h"
46 #include "coretypes.h"
47 #include "diagnostic-core.h"
48 #include "tree.h"
49 #include "basic-block.h"
50 #include "tree-ssa-alias.h"
51 #include "internal-fn.h"
52 #include "gimple-expr.h"
53 #include "is-a.h"
54 #include "gimple.h"
55 #include "gimple-iterator.h"
56 #include "tree-ssa-loop.h"
57 #include "dumpfile.h"
58 #include "gimple-pretty-print.h"
59 #include "cfgloop.h"
60 #include "tree-chrec.h"
61 #include "tree-data-ref.h"
62 #include "tree-scalar-evolution.h"
63 #include "sese.h"
65 #ifdef HAVE_isl
66 #include "graphite-poly.h"
68 #define OPENSCOP_MAX_STRING 256
71 /* Print to STDERR the GMP value VAL. */
73 DEBUG_FUNCTION void
74 debug_gmp_value (mpz_t val)
76 gmp_fprintf (stderr, "%Zd", val);
79 /* Return the maximal loop depth in SCOP. */
81 int
82 scop_max_loop_depth (scop_p scop)
84 int i;
85 poly_bb_p pbb;
86 int max_nb_loops = 0;
88 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
90 int nb_loops = pbb_dim_iter_domain (pbb);
91 if (max_nb_loops < nb_loops)
92 max_nb_loops = nb_loops;
95 return max_nb_loops;
98 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
99 level. */
101 static void
102 print_scattering_function_1 (FILE *file, poly_bb_p pbb, int verbosity)
104 graphite_dim_t i;
106 if (verbosity > 0)
108 fprintf (file, "# scattering bb_%d (\n", pbb_index (pbb));
109 fprintf (file, "#eq");
111 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
112 fprintf (file, " s%d", (int) i);
114 for (i = 0; i < pbb_nb_local_vars (pbb); i++)
115 fprintf (file, " lv%d", (int) i);
117 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
118 fprintf (file, " i%d", (int) i);
120 for (i = 0; i < pbb_nb_params (pbb); i++)
121 fprintf (file, " p%d", (int) i);
123 fprintf (file, " cst\n");
126 fprintf (file, "isl\n");
127 print_isl_map (file, pbb->transformed ? pbb->transformed : pbb->schedule);
129 if (verbosity > 0)
130 fprintf (file, "#)\n");
133 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
134 level. */
136 void
137 print_scattering_function (FILE *file, poly_bb_p pbb, int verbosity)
139 if (!PBB_TRANSFORMED (pbb))
140 return;
142 if (pbb->schedule || pbb->transformed)
144 if (verbosity > 0)
145 fprintf (file, "# Scattering function is provided\n");
147 fprintf (file, "1\n");
149 else
151 if (verbosity > 0)
152 fprintf (file, "# Scattering function is not provided\n");
154 fprintf (file, "0\n");
155 return;
158 print_scattering_function_1 (file, pbb, verbosity);
160 if (verbosity > 0)
161 fprintf (file, "# Scattering names are not provided\n");
163 fprintf (file, "0\n");
167 /* Prints to FILE the iteration domain of PBB, at some VERBOSITY
168 level. */
170 void
171 print_iteration_domain (FILE *file, poly_bb_p pbb, int verbosity)
173 print_pbb_domain (file, pbb, verbosity);
176 /* Prints to FILE the scattering functions of every PBB of SCOP. */
178 void
179 print_scattering_functions (FILE *file, scop_p scop, int verbosity)
181 int i;
182 poly_bb_p pbb;
184 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
185 print_scattering_function (file, pbb, verbosity);
188 /* Prints to FILE the iteration domains of every PBB of SCOP, at some
189 VERBOSITY level. */
191 void
192 print_iteration_domains (FILE *file, scop_p scop, int verbosity)
194 int i;
195 poly_bb_p pbb;
197 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
198 print_iteration_domain (file, pbb, verbosity);
201 /* Prints to STDERR the scattering function of PBB, at some VERBOSITY
202 level. */
204 DEBUG_FUNCTION void
205 debug_scattering_function (poly_bb_p pbb, int verbosity)
207 print_scattering_function (stderr, pbb, verbosity);
210 /* Prints to STDERR the iteration domain of PBB, at some VERBOSITY
211 level. */
213 DEBUG_FUNCTION void
214 debug_iteration_domain (poly_bb_p pbb, int verbosity)
216 print_iteration_domain (stderr, pbb, verbosity);
219 /* Prints to STDERR the scattering functions of every PBB of SCOP, at
220 some VERBOSITY level. */
222 DEBUG_FUNCTION void
223 debug_scattering_functions (scop_p scop, int verbosity)
225 print_scattering_functions (stderr, scop, verbosity);
228 /* Prints to STDERR the iteration domains of every PBB of SCOP, at
229 some VERBOSITY level. */
231 DEBUG_FUNCTION void
232 debug_iteration_domains (scop_p scop, int verbosity)
234 print_iteration_domains (stderr, scop, verbosity);
237 /* Apply graphite transformations to all the basic blocks of SCOP. */
239 bool
240 apply_poly_transforms (scop_p scop)
242 bool transform_done = false;
244 /* Generate code even if we did not apply any real transformation.
245 This also allows to check the performance for the identity
246 transformation: GIMPLE -> GRAPHITE -> GIMPLE
247 Keep in mind that CLooG optimizes in control, so the loop structure
248 may change, even if we only use -fgraphite-identity. */
249 if (flag_graphite_identity)
250 transform_done = true;
252 if (flag_loop_parallelize_all)
253 transform_done = true;
255 if (flag_loop_block)
256 transform_done |= scop_do_block (scop);
257 else
259 if (flag_loop_strip_mine)
260 transform_done |= scop_do_strip_mine (scop, 0);
262 if (flag_loop_interchange)
263 transform_done |= scop_do_interchange (scop);
266 /* This pass needs to be run at the final stage, as it does not
267 update the lst. */
268 if (flag_loop_optimize_isl)
269 transform_done |= optimize_isl (scop);
271 return transform_done;
274 /* Create a new polyhedral data reference and add it to PBB. It is
275 defined by its ACCESSES, its TYPE, and the number of subscripts
276 NB_SUBSCRIPTS. */
278 void
279 new_poly_dr (poly_bb_p pbb, int dr_base_object_set,
280 enum poly_dr_type type, void *cdr, graphite_dim_t nb_subscripts,
281 isl_map *acc, isl_set *extent)
283 static int id = 0;
284 poly_dr_p pdr = XNEW (struct poly_dr);
286 PDR_ID (pdr) = id++;
287 PDR_BASE_OBJECT_SET (pdr) = dr_base_object_set;
288 PDR_NB_REFS (pdr) = 1;
289 PDR_PBB (pdr) = pbb;
290 pdr->accesses = acc;
291 pdr->extent = extent;
292 PDR_TYPE (pdr) = type;
293 PDR_CDR (pdr) = cdr;
294 PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
295 PBB_DRS (pbb).safe_push (pdr);
298 /* Free polyhedral data reference PDR. */
300 void
301 free_poly_dr (poly_dr_p pdr)
303 isl_map_free (pdr->accesses);
304 isl_set_free (pdr->extent);
305 XDELETE (pdr);
308 /* Create a new polyhedral black box. */
310 poly_bb_p
311 new_poly_bb (scop_p scop, void *black_box)
313 poly_bb_p pbb = XNEW (struct poly_bb);
315 pbb->domain = NULL;
316 pbb->schedule = NULL;
317 pbb->transformed = NULL;
318 pbb->saved = NULL;
319 PBB_SCOP (pbb) = scop;
320 pbb_set_black_box (pbb, black_box);
321 PBB_TRANSFORMED (pbb) = NULL;
322 PBB_SAVED (pbb) = NULL;
323 PBB_ORIGINAL (pbb) = NULL;
324 PBB_DRS (pbb).create (3);
325 PBB_IS_REDUCTION (pbb) = false;
326 GBB_PBB ((gimple_bb_p) black_box) = pbb;
328 return pbb;
331 /* Free polyhedral black box. */
333 void
334 free_poly_bb (poly_bb_p pbb)
336 int i;
337 poly_dr_p pdr;
339 isl_set_free (pbb->domain);
340 isl_map_free (pbb->schedule);
341 isl_map_free (pbb->transformed);
342 isl_map_free (pbb->saved);
344 if (PBB_DRS (pbb).exists ())
345 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
346 free_poly_dr (pdr);
348 PBB_DRS (pbb).release ();
349 XDELETE (pbb);
352 static void
353 print_pdr_access_layout (FILE *file, poly_bb_p pbb, poly_dr_p pdr)
355 graphite_dim_t i;
357 fprintf (file, "# eq");
359 fprintf (file, " alias");
361 for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
362 fprintf (file, " sub%d", (int) i);
364 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
365 fprintf (file, " i%d", (int) i);
367 for (i = 0; i < pbb_nb_params (pbb); i++)
368 fprintf (file, " p%d", (int) i);
370 fprintf (file, " cst\n");
373 /* Prints to FILE the polyhedral data reference PDR, at some VERBOSITY
374 level. */
376 void
377 print_pdr (FILE *file, poly_dr_p pdr, int verbosity)
379 if (verbosity > 1)
381 fprintf (file, "# pdr_%d (", PDR_ID (pdr));
383 switch (PDR_TYPE (pdr))
385 case PDR_READ:
386 fprintf (file, "read \n");
387 break;
389 case PDR_WRITE:
390 fprintf (file, "write \n");
391 break;
393 case PDR_MAY_WRITE:
394 fprintf (file, "may_write \n");
395 break;
397 default:
398 gcc_unreachable ();
401 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
404 if (verbosity > 0)
406 fprintf (file, "# data accesses (\n");
407 print_pdr_access_layout (file, PDR_PBB (pdr), pdr);
410 /* XXX isl dump accesses/subscripts */
412 if (verbosity > 0)
413 fprintf (file, "#)\n");
415 if (verbosity > 1)
416 fprintf (file, "#)\n");
419 /* Prints to STDERR the polyhedral data reference PDR, at some
420 VERBOSITY level. */
422 DEBUG_FUNCTION void
423 debug_pdr (poly_dr_p pdr, int verbosity)
425 print_pdr (stderr, pdr, verbosity);
428 /* Creates a new SCOP containing REGION. */
430 scop_p
431 new_scop (void *region)
433 scop_p scop = XNEW (struct scop);
435 scop->context = NULL;
436 scop->must_raw = NULL;
437 scop->may_raw = NULL;
438 scop->must_raw_no_source = NULL;
439 scop->may_raw_no_source = NULL;
440 scop->must_war = NULL;
441 scop->may_war = NULL;
442 scop->must_war_no_source = NULL;
443 scop->may_war_no_source = NULL;
444 scop->must_waw = NULL;
445 scop->may_waw = NULL;
446 scop->must_waw_no_source = NULL;
447 scop->may_waw_no_source = NULL;
448 scop_set_region (scop, region);
449 SCOP_BBS (scop).create (3);
450 SCOP_ORIGINAL_SCHEDULE (scop) = NULL;
451 SCOP_TRANSFORMED_SCHEDULE (scop) = NULL;
452 SCOP_SAVED_SCHEDULE (scop) = NULL;
453 POLY_SCOP_P (scop) = false;
455 return scop;
458 /* Deletes SCOP. */
460 void
461 free_scop (scop_p scop)
463 int i;
464 poly_bb_p pbb;
466 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
467 free_poly_bb (pbb);
469 SCOP_BBS (scop).release ();
471 isl_set_free (scop->context);
472 isl_union_map_free (scop->must_raw);
473 isl_union_map_free (scop->may_raw);
474 isl_union_map_free (scop->must_raw_no_source);
475 isl_union_map_free (scop->may_raw_no_source);
476 isl_union_map_free (scop->must_war);
477 isl_union_map_free (scop->may_war);
478 isl_union_map_free (scop->must_war_no_source);
479 isl_union_map_free (scop->may_war_no_source);
480 isl_union_map_free (scop->must_waw);
481 isl_union_map_free (scop->may_waw);
482 isl_union_map_free (scop->must_waw_no_source);
483 isl_union_map_free (scop->may_waw_no_source);
484 free_lst (SCOP_ORIGINAL_SCHEDULE (scop));
485 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
486 free_lst (SCOP_SAVED_SCHEDULE (scop));
487 XDELETE (scop);
490 /* Print to FILE the domain of PBB in OpenScop format, at some VERBOSITY
491 level. */
493 static void
494 openscop_print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity)
496 graphite_dim_t i;
497 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
499 if (!pbb->domain)
500 return;
502 if (verbosity > 0)
504 fprintf (file, "\n# Iteration domain of bb_%d (\n", GBB_BB (gbb)->index);
505 fprintf (file, "#eq");
507 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
508 fprintf (file, " i%d", (int) i);
510 for (i = 0; i < pbb_nb_params (pbb); i++)
511 fprintf (file, " p%d", (int) i);
513 fprintf (file, " cst\n");
516 fprintf (file, "XXX isl\n");
518 if (verbosity > 0)
519 fprintf (file, "#)\n");
522 /* Print to FILE the domain of PBB, at some VERBOSITY level. */
524 void
525 print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity ATTRIBUTE_UNUSED)
527 print_isl_set (file, pbb->domain);
530 /* Dump the cases of a graphite basic block GBB on FILE. */
532 static void
533 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
535 int i;
536 gimple stmt;
537 vec<gimple> cases;
539 if (!gbb)
540 return;
542 cases = GBB_CONDITION_CASES (gbb);
543 if (cases.is_empty ())
544 return;
546 fprintf (file, "# cases bb_%d (\n", GBB_BB (gbb)->index);
548 FOR_EACH_VEC_ELT (cases, i, stmt)
550 fprintf (file, "# ");
551 print_gimple_stmt (file, stmt, 0, 0);
554 fprintf (file, "#)\n");
557 /* Dump conditions of a graphite basic block GBB on FILE. */
559 static void
560 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
562 int i;
563 gimple stmt;
564 vec<gimple> conditions;
566 if (!gbb)
567 return;
569 conditions = GBB_CONDITIONS (gbb);
570 if (conditions.is_empty ())
571 return;
573 fprintf (file, "# conditions bb_%d (\n", GBB_BB (gbb)->index);
575 FOR_EACH_VEC_ELT (conditions, i, stmt)
577 fprintf (file, "# ");
578 print_gimple_stmt (file, stmt, 0, 0);
581 fprintf (file, "#)\n");
584 /* Print to FILE all the data references of PBB, at some VERBOSITY
585 level. */
587 void
588 print_pdrs (FILE *file, poly_bb_p pbb, int verbosity)
590 int i;
591 poly_dr_p pdr;
592 int nb_reads = 0;
593 int nb_writes = 0;
595 if (PBB_DRS (pbb).length () == 0)
597 if (verbosity > 0)
598 fprintf (file, "# Access informations are not provided\n");\
599 fprintf (file, "0\n");
600 return;
603 if (verbosity > 1)
604 fprintf (file, "# Data references (\n");
606 if (verbosity > 0)
607 fprintf (file, "# Access informations are provided\n");
608 fprintf (file, "1\n");
610 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
611 if (PDR_TYPE (pdr) == PDR_READ)
612 nb_reads++;
613 else
614 nb_writes++;
616 if (verbosity > 1)
617 fprintf (file, "# Read data references (\n");
619 if (verbosity > 0)
620 fprintf (file, "# Read access informations\n");
621 fprintf (file, "%d\n", nb_reads);
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, "# Write data references (\n");
633 if (verbosity > 0)
634 fprintf (file, "# Write access informations\n");
635 fprintf (file, "%d\n", nb_writes);
637 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
638 if (PDR_TYPE (pdr) != PDR_READ)
639 print_pdr (file, pdr, verbosity);
641 if (verbosity > 1)
642 fprintf (file, "#)\n");
644 if (verbosity > 1)
645 fprintf (file, "#)\n");
648 /* Print to STDERR all the data references of PBB. */
650 DEBUG_FUNCTION void
651 debug_pdrs (poly_bb_p pbb, int verbosity)
653 print_pdrs (stderr, pbb, verbosity);
656 /* Print to FILE the body of PBB, at some VERBOSITY level.
657 If statement_body_provided is false statement body is not printed. */
659 static void
660 print_pbb_body (FILE *file, poly_bb_p pbb, int verbosity,
661 bool statement_body_provided)
663 if (verbosity > 1)
664 fprintf (file, "# Body (\n");
666 if (!statement_body_provided)
668 if (verbosity > 0)
669 fprintf (file, "# Statement body is not provided\n");
671 fprintf (file, "0\n");
673 if (verbosity > 1)
674 fprintf (file, "#)\n");
675 return;
678 if (verbosity > 0)
679 fprintf (file, "# Statement body is provided\n");
680 fprintf (file, "1\n");
682 if (verbosity > 0)
683 fprintf (file, "# Original iterator names\n# Iterator names are not provided yet.\n");
685 if (verbosity > 0)
686 fprintf (file, "# Statement body\n");
688 fprintf (file, "{\n");
689 dump_bb (file, pbb_bb (pbb), 0, 0);
690 fprintf (file, "}\n");
692 if (verbosity > 1)
693 fprintf (file, "#)\n");
696 /* Print to FILE the domain and scattering function of PBB, at some
697 VERBOSITY level. */
699 void
700 print_pbb (FILE *file, poly_bb_p pbb, int verbosity)
702 if (verbosity > 1)
704 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
705 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
706 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
709 openscop_print_pbb_domain (file, pbb, verbosity);
710 print_scattering_function (file, pbb, verbosity);
711 print_pdrs (file, pbb, verbosity);
712 print_pbb_body (file, pbb, verbosity, false);
714 if (verbosity > 1)
715 fprintf (file, "#)\n");
718 /* Print to FILE the parameters of SCOP, at some VERBOSITY level. */
720 void
721 print_scop_params (FILE *file, scop_p scop, int verbosity)
723 int i;
724 tree t;
726 if (verbosity > 1)
727 fprintf (file, "# parameters (\n");
729 if (SESE_PARAMS (SCOP_REGION (scop)).length ())
731 if (verbosity > 0)
732 fprintf (file, "# Parameter names are provided\n");
734 fprintf (file, "1\n");
736 if (verbosity > 0)
737 fprintf (file, "# Parameter names\n");
739 else
741 if (verbosity > 0)
742 fprintf (file, "# Parameter names are not provided\n");
743 fprintf (file, "0\n");
746 FOR_EACH_VEC_ELT (SESE_PARAMS (SCOP_REGION (scop)), i, t)
748 print_generic_expr (file, t, 0);
749 fprintf (file, " ");
752 fprintf (file, "\n");
754 if (verbosity > 1)
755 fprintf (file, "#)\n");
758 /* Print to FILE the context of SCoP in OpenScop format, at some VERBOSITY
759 level. */
761 static void
762 openscop_print_scop_context (FILE *file, scop_p scop, int verbosity)
764 graphite_dim_t i;
766 if (verbosity > 0)
768 fprintf (file, "# Context (\n");
769 fprintf (file, "#eq");
771 for (i = 0; i < scop_nb_params (scop); i++)
772 fprintf (file, " p%d", (int) i);
774 fprintf (file, " cst\n");
777 if (scop->context)
778 /* XXX isl print context */
779 fprintf (file, "XXX isl\n");
780 else
781 fprintf (file, "0 %d 0 0 0 %d\n", (int) scop_nb_params (scop) + 2,
782 (int) scop_nb_params (scop));
784 if (verbosity > 0)
785 fprintf (file, "# )\n");
788 /* Print to FILE the context of SCoP, at some VERBOSITY level. */
790 void
791 print_scop_context (FILE *file, scop_p scop, int verbosity)
793 graphite_dim_t i;
795 if (verbosity > 0)
797 fprintf (file, "# Context (\n");
798 fprintf (file, "#eq");
800 for (i = 0; i < scop_nb_params (scop); i++)
801 fprintf (file, " p%d", (int) i);
803 fprintf (file, " cst\n");
806 if (scop->context)
807 print_isl_set (file, scop->context);
808 else
809 fprintf (file, "no isl context %d\n", (int) scop_nb_params (scop) + 2);
811 if (verbosity > 0)
812 fprintf (file, "# )\n");
815 /* Print to FILE the SCOP, at some VERBOSITY level. */
817 void
818 print_scop (FILE *file, scop_p scop, int verbosity)
820 int i;
821 poly_bb_p pbb;
823 fprintf (file, "SCoP 1\n#(\n");
824 fprintf (file, "# Language\nGimple\n");
825 openscop_print_scop_context (file, scop, verbosity);
826 print_scop_params (file, scop, verbosity);
828 if (verbosity > 0)
829 fprintf (file, "# Number of statements\n");
831 fprintf (file, "%d\n", SCOP_BBS (scop).length ());
833 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
834 print_pbb (file, pbb, verbosity);
836 if (verbosity > 1)
838 fprintf (file, "# original_lst (\n");
839 print_lst (file, SCOP_ORIGINAL_SCHEDULE (scop), 0);
840 fprintf (file, "\n#)\n");
842 fprintf (file, "# transformed_lst (\n");
843 print_lst (file, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
844 fprintf (file, "\n#)\n");
847 fprintf (file, "#)\n");
850 /* Print to FILE the input file that CLooG would expect as input, at
851 some VERBOSITY level. */
853 void
854 print_cloog (FILE *file, scop_p scop, int verbosity)
856 int i;
857 poly_bb_p pbb;
859 fprintf (file, "# SCoP (generated by GCC/Graphite\n");
860 if (verbosity > 0)
861 fprintf (file, "# CLooG output language\n");
862 fprintf (file, "c\n");
864 print_scop_context (file, scop, verbosity);
865 print_scop_params (file, scop, verbosity);
867 if (verbosity > 0)
868 fprintf (file, "# Number of statements\n");
870 fprintf (file, "%d\n", SCOP_BBS (scop).length ());
872 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
874 if (verbosity > 1)
875 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
877 print_pbb_domain (file, pbb, verbosity);
878 fprintf (file, "0 0 0");
880 if (verbosity > 0)
881 fprintf (file, "# For future CLooG options.\n");
882 else
883 fprintf (file, "\n");
885 if (verbosity > 1)
886 fprintf (file, "#)\n");
889 fprintf (file, "0");
890 if (verbosity > 0)
891 fprintf (file, "# Don't set the iterator names.\n");
892 else
893 fprintf (file, "\n");
895 if (verbosity > 0)
896 fprintf (file, "# Number of scattering functions\n");
898 fprintf (file, "%d\n", SCOP_BBS (scop).length ());
900 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
902 if (!(pbb->transformed || pbb->schedule))
903 continue;
905 if (verbosity > 1)
906 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
908 print_scattering_function_1 (file, pbb, verbosity);
910 if (verbosity > 1)
911 fprintf (file, "#)\n");
914 fprintf (file, "0");
915 if (verbosity > 0)
916 fprintf (file, "# Don't set the scattering dimension names.\n");
917 else
918 fprintf (file, "\n");
920 fprintf (file, "#)\n");
923 /* Print to STDERR the domain of PBB, at some VERBOSITY level. */
925 DEBUG_FUNCTION void
926 debug_pbb_domain (poly_bb_p pbb, int verbosity)
928 print_pbb_domain (stderr, pbb, verbosity);
931 /* Print to FILE the domain and scattering function of PBB, at some
932 VERBOSITY level. */
934 DEBUG_FUNCTION void
935 debug_pbb (poly_bb_p pbb, int verbosity)
937 print_pbb (stderr, pbb, verbosity);
940 /* Print to STDERR the context of SCOP, at some VERBOSITY level. */
942 DEBUG_FUNCTION void
943 debug_scop_context (scop_p scop, int verbosity)
945 print_scop_context (stderr, scop, verbosity);
948 /* Print to STDERR the SCOP, at some VERBOSITY level. */
950 DEBUG_FUNCTION void
951 debug_scop (scop_p scop, int verbosity)
953 print_scop (stderr, scop, verbosity);
956 /* Print to STDERR the SCOP under CLooG format, at some VERBOSITY
957 level. */
959 DEBUG_FUNCTION void
960 debug_cloog (scop_p scop, int verbosity)
962 print_cloog (stderr, scop, verbosity);
965 /* Print to STDERR the parameters of SCOP, at some VERBOSITY
966 level. */
968 DEBUG_FUNCTION void
969 debug_scop_params (scop_p scop, int verbosity)
971 print_scop_params (stderr, scop, verbosity);
974 extern isl_ctx *the_isl_ctx;
975 void
976 print_isl_set (FILE *f, isl_set *set)
978 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
979 p = isl_printer_print_set (p, set);
980 isl_printer_free (p);
983 DEBUG_FUNCTION void
984 debug_isl_set (isl_set *set)
986 print_isl_set (stderr, set);
989 void
990 print_isl_map (FILE *f, isl_map *map)
992 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
993 p = isl_printer_print_map (p, map);
994 isl_printer_free (p);
997 DEBUG_FUNCTION void
998 debug_isl_map (isl_map *map)
1000 print_isl_map (stderr, map);
1003 void
1004 print_isl_aff (FILE *f, isl_aff *aff)
1006 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
1007 p = isl_printer_print_aff (p, aff);
1008 isl_printer_free (p);
1011 DEBUG_FUNCTION void
1012 debug_isl_aff (isl_aff *aff)
1014 print_isl_aff (stderr, aff);
1017 void
1018 print_isl_constraint (FILE *f, isl_constraint *c)
1020 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
1021 p = isl_printer_print_constraint (p, c);
1022 isl_printer_free (p);
1025 DEBUG_FUNCTION void
1026 debug_isl_constraint (isl_constraint *c)
1028 print_isl_constraint (stderr, c);
1031 /* Returns the number of iterations RES of the loop around PBB at
1032 time(scattering) dimension TIME_DEPTH. */
1034 void
1035 pbb_number_of_iterations_at_time (poly_bb_p pbb,
1036 graphite_dim_t time_depth,
1037 mpz_t res)
1039 isl_set *transdomain;
1040 isl_space *dc;
1041 isl_aff *aff;
1042 isl_val *isllb, *islub;
1044 /* Map the iteration domain through the current scatter, and work
1045 on the resulting set. */
1046 transdomain = isl_set_apply (isl_set_copy (pbb->domain),
1047 isl_map_copy (pbb->transformed));
1049 /* Select the time_depth' dimension via an affine expression. */
1050 dc = isl_set_get_space (transdomain);
1051 aff = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
1052 aff = isl_aff_set_coefficient_si (aff, isl_dim_in, time_depth, 1);
1054 /* And find the min/max for that function. */
1055 /* XXX isl check results? */
1056 isllb = isl_set_min_val (transdomain, aff);
1057 islub = isl_set_max_val (transdomain, aff);
1059 islub = isl_val_sub (islub, isllb);
1060 islub = isl_val_add_ui (islub, 1);
1061 isl_val_get_num_gmp (islub, res);
1063 isl_val_free (islub);
1064 isl_aff_free (aff);
1065 isl_set_free (transdomain);
1068 /* Translates LOOP to LST. */
1070 static lst_p
1071 loop_to_lst (loop_p loop, vec<poly_bb_p> bbs, int *i)
1073 poly_bb_p pbb;
1074 vec<lst_p> seq;
1075 seq.create (5);
1077 for (; bbs.iterate (*i, &pbb); (*i)++)
1079 lst_p stmt;
1080 basic_block bb = GBB_BB (PBB_BLACK_BOX (pbb));
1082 if (bb->loop_father == loop)
1083 stmt = new_lst_stmt (pbb);
1084 else if (flow_bb_inside_loop_p (loop, bb))
1086 loop_p next = loop->inner;
1088 while (next && !flow_bb_inside_loop_p (next, bb))
1089 next = next->next;
1091 stmt = loop_to_lst (next, bbs, i);
1093 else
1095 (*i)--;
1096 return new_lst_loop (seq);
1099 seq.safe_push (stmt);
1102 return new_lst_loop (seq);
1105 /* Reads the original scattering of the SCOP and returns an LST
1106 representing it. */
1108 void
1109 scop_to_lst (scop_p scop)
1111 lst_p res;
1112 int i, n = SCOP_BBS (scop).length ();
1113 vec<lst_p> seq;
1114 seq.create (5);
1115 sese region = SCOP_REGION (scop);
1117 for (i = 0; i < n; i++)
1119 poly_bb_p pbb = SCOP_BBS (scop)[i];
1120 loop_p loop = outermost_loop_in_sese (region, GBB_BB (PBB_BLACK_BOX (pbb)));
1122 if (loop_in_sese_p (loop, region))
1123 res = loop_to_lst (loop, SCOP_BBS (scop), &i);
1124 else
1125 res = new_lst_stmt (pbb);
1127 seq.safe_push (res);
1130 res = new_lst_loop (seq);
1131 SCOP_ORIGINAL_SCHEDULE (scop) = res;
1132 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (res);
1135 /* Print to FILE on a new line COLUMN white spaces. */
1137 static void
1138 lst_indent_to (FILE *file, int column)
1140 int i;
1142 if (column > 0)
1143 fprintf (file, "\n#");
1145 for (i = 0; i < column; i++)
1146 fprintf (file, " ");
1149 /* Print LST to FILE with INDENT spaces of indentation. */
1151 void
1152 print_lst (FILE *file, lst_p lst, int indent)
1154 if (!lst)
1155 return;
1157 lst_indent_to (file, indent);
1159 if (LST_LOOP_P (lst))
1161 int i;
1162 lst_p l;
1164 if (LST_LOOP_FATHER (lst))
1165 fprintf (file, "%d (loop", lst_dewey_number (lst));
1166 else
1167 fprintf (file, "#(root");
1169 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
1170 print_lst (file, l, indent + 2);
1172 fprintf (file, ")");
1174 else
1175 fprintf (file, "%d stmt_%d", lst_dewey_number (lst), pbb_index (LST_PBB (lst)));
1178 /* Print LST to STDERR. */
1180 DEBUG_FUNCTION void
1181 debug_lst (lst_p lst)
1183 print_lst (stderr, lst, 0);
1186 /* Pretty print to FILE the loop statement tree LST in DOT format. */
1188 static void
1189 dot_lst_1 (FILE *file, lst_p lst)
1191 if (!lst)
1192 return;
1194 if (LST_LOOP_P (lst))
1196 int i;
1197 lst_p l;
1199 if (!LST_LOOP_FATHER (lst))
1200 fprintf (file, "L -> L_%d_%d\n",
1201 lst_depth (lst),
1202 lst_dewey_number (lst));
1203 else
1204 fprintf (file, "L_%d_%d -> L_%d_%d\n",
1205 lst_depth (LST_LOOP_FATHER (lst)),
1206 lst_dewey_number (LST_LOOP_FATHER (lst)),
1207 lst_depth (lst),
1208 lst_dewey_number (lst));
1210 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
1211 dot_lst_1 (file, l);
1214 else
1215 fprintf (file, "L_%d_%d -> S_%d\n",
1216 lst_depth (LST_LOOP_FATHER (lst)),
1217 lst_dewey_number (LST_LOOP_FATHER (lst)),
1218 pbb_index (LST_PBB (lst)));
1222 /* Display the LST using dotty. */
1224 DEBUG_FUNCTION void
1225 dot_lst (lst_p lst)
1227 /* When debugging, enable the following code. This cannot be used
1228 in production compilers because it calls "system". */
1229 #if 0
1230 FILE *stream = fopen ("/tmp/lst.dot", "w");
1231 gcc_assert (stream);
1233 fputs ("digraph all {\n", stream);
1234 dot_lst_1 (stream, lst);
1235 fputs ("}\n\n", stream);
1236 fclose (stream);
1238 system ("dotty /tmp/lst.dot &");
1239 #else
1240 fputs ("digraph all {\n", stderr);
1241 dot_lst_1 (stderr, lst);
1242 fputs ("}\n\n", stderr);
1244 #endif
1247 /* Computes a checksum for the code generated by CLooG for SCOP. */
1249 DEBUG_FUNCTION void
1250 cloog_checksum (scop_p scop ATTRIBUTE_UNUSED)
1252 /* When debugging, enable the following code. This cannot be used
1253 in production compilers because it calls "system". */
1254 #if 0
1255 FILE *stream = fopen ("/tmp/scop.cloog", "w");
1256 gcc_assert (stream);
1257 print_cloog (stream, scop, 0);
1258 fclose (stream);
1260 fputs ("\n", stdout);
1261 system ("cloog -compilable 1 /tmp/scop.cloog > /tmp/scop.c ; gcc -O0 -g /tmp/scop.c -lm -o /tmp/scop; /tmp/scop | md5sum ");
1262 #endif
1265 /* Reverse the loop around PBB at level DEPTH. */
1267 isl_map *
1268 reverse_loop_at_level (poly_bb_p pbb, int depth)
1270 unsigned i, depth_dim = psct_dynamic_dim (pbb, depth);
1271 isl_space *d = isl_map_get_space (pbb->transformed);
1272 isl_space *d1 = isl_space_range (d);
1273 unsigned n = isl_space_dim (d1, isl_dim_out);
1274 isl_space *d2 = isl_space_add_dims (d1, isl_dim_in, n);
1275 isl_map *x = isl_map_universe (isl_space_copy (d2));
1276 isl_constraint *c = isl_equality_alloc (isl_local_space_from_space (d2));
1278 for (i = 0; i < n; i++)
1279 if (i != depth_dim)
1280 x = isl_map_equate (x, isl_dim_in, i, isl_dim_out, i);
1282 c = isl_constraint_set_coefficient_si (c, isl_dim_in, depth_dim, 1);
1283 c = isl_constraint_set_coefficient_si (c, isl_dim_out, depth_dim, 1);
1284 x = isl_map_add_constraint (x, c);
1285 return x;
1288 /* Reverse the loop at level DEPTH for all the PBBS. */
1290 isl_union_map *
1291 reverse_loop_for_pbbs (scop_p scop, vec<poly_bb_p> pbbs, int depth)
1293 poly_bb_p pbb;
1294 int i;
1295 isl_space *space = isl_space_from_domain (isl_set_get_space (scop->context));
1296 isl_union_map *res = isl_union_map_empty (space);
1298 for (i = 0; pbbs.iterate (i, &pbb); i++)
1299 res = isl_union_map_add_map (res, reverse_loop_at_level (pbb, depth));
1301 return res;
1305 #endif