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)
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/>. */
25 /* Workaround for GMP 5.1.3 bug, see PR56019. */
28 #include <isl/constraint.h>
31 #include <isl/union_map.h>
32 #include <isl/constraint.h>
37 /* Since ISL-0.13, the extern is in val_gmp.h. */
38 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
41 #include <isl/val_gmp.h>
42 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
47 #include "coretypes.h"
49 #include "diagnostic-core.h"
53 #include "fold-const.h"
54 #include "gimple-iterator.h"
55 #include "tree-ssa-loop.h"
56 #include "gimple-pretty-print.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. */
67 debug_gmp_value (mpz_t val
)
69 gmp_fprintf (stderr
, "%Zd", val
);
72 /* Prints to FILE the iteration domain of PBB, at some VERBOSITY
76 print_iteration_domain (FILE *file
, poly_bb_p pbb
, int verbosity
)
78 print_pbb_domain (file
, pbb
, verbosity
);
81 /* Prints to FILE the iteration domains of every PBB of SCOP, at some
85 print_iteration_domains (FILE *file
, scop_p scop
, int verbosity
)
90 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
91 print_iteration_domain (file
, pbb
, verbosity
);
94 /* Prints to STDERR the iteration domain of PBB, at some VERBOSITY
98 debug_iteration_domain (poly_bb_p pbb
, int verbosity
)
100 print_iteration_domain (stderr
, pbb
, verbosity
);
103 /* Prints to STDERR the iteration domains of every PBB of SCOP, at
104 some VERBOSITY level. */
107 debug_iteration_domains (scop_p scop
, int verbosity
)
109 print_iteration_domains (stderr
, scop
, verbosity
);
112 /* Apply graphite transformations to all the basic blocks of SCOP. */
115 apply_poly_transforms (scop_p scop
)
117 bool transform_done
= false;
119 /* Generate code even if we did not apply any real transformation.
120 This also allows to check the performance for the identity
121 transformation: GIMPLE -> GRAPHITE -> GIMPLE. */
122 if (flag_graphite_identity
)
123 transform_done
= true;
125 if (flag_loop_parallelize_all
)
126 transform_done
= true;
128 if (flag_loop_optimize_isl
)
129 transform_done
|= optimize_isl (scop
);
131 return transform_done
;
134 /* Create a new polyhedral data reference and add it to PBB. It is
135 defined by its ACCESSES, its TYPE, and the number of subscripts
139 new_poly_dr (poly_bb_p pbb
, enum poly_dr_type type
, data_reference_p cdr
,
140 graphite_dim_t nb_subscripts
,
141 isl_map
*acc
, isl_set
*subscript_sizes
)
144 poly_dr_p pdr
= XNEW (struct poly_dr
);
147 PDR_NB_REFS (pdr
) = 1;
150 pdr
->subscript_sizes
= subscript_sizes
;
151 PDR_TYPE (pdr
) = type
;
153 PDR_NB_SUBSCRIPTS (pdr
) = nb_subscripts
;
154 PBB_DRS (pbb
).safe_push (pdr
);
157 /* Free polyhedral data reference PDR. */
160 free_poly_dr (poly_dr_p pdr
)
162 isl_map_free (pdr
->accesses
);
163 isl_set_free (pdr
->subscript_sizes
);
167 /* Create a new polyhedral black box. */
170 new_poly_bb (scop_p scop
, gimple_poly_bb_p black_box
)
172 poly_bb_p pbb
= XNEW (struct poly_bb
);
175 pbb
->schedule
= NULL
;
176 pbb
->transformed
= NULL
;
178 PBB_SCOP (pbb
) = scop
;
179 pbb_set_black_box (pbb
, black_box
);
180 PBB_DRS (pbb
).create (3);
181 PBB_IS_REDUCTION (pbb
) = false;
182 GBB_PBB ((gimple_poly_bb_p
) black_box
) = pbb
;
187 /* Free polyhedral black box. */
190 free_poly_bb (poly_bb_p pbb
)
195 isl_set_free (pbb
->domain
);
196 isl_map_free (pbb
->schedule
);
197 isl_map_free (pbb
->transformed
);
198 isl_map_free (pbb
->saved
);
200 if (PBB_DRS (pbb
).exists ())
201 FOR_EACH_VEC_ELT (PBB_DRS (pbb
), i
, pdr
)
204 PBB_DRS (pbb
).release ();
208 /* Prints to FILE the polyhedral data reference PDR, at some VERBOSITY
212 print_pdr (FILE *file
, poly_dr_p pdr
, int verbosity
)
216 fprintf (file
, "# pdr_%d (", PDR_ID (pdr
));
218 switch (PDR_TYPE (pdr
))
221 fprintf (file
, "read \n");
225 fprintf (file
, "write \n");
229 fprintf (file
, "may_write \n");
236 dump_data_reference (file
, (data_reference_p
) PDR_CDR (pdr
));
241 fprintf (file
, "# data accesses (\n");
242 print_isl_map (file
, pdr
->accesses
);
243 print_isl_set (file
, pdr
->subscript_sizes
);
244 fprintf (file
, "#)\n");
247 fprintf (file
, "#)\n");
250 /* Prints to STDERR the polyhedral data reference PDR, at some
254 debug_pdr (poly_dr_p pdr
, int verbosity
)
256 print_pdr (stderr
, pdr
, verbosity
);
259 /* Store the GRAPHITE representation of BB. */
262 new_gimple_poly_bb (basic_block bb
, vec
<data_reference_p
> drs
)
264 gimple_poly_bb_p gbb
;
266 gbb
= XNEW (struct gimple_poly_bb
);
269 GBB_DATA_REFS (gbb
) = drs
;
270 GBB_CONDITIONS (gbb
).create (0);
271 GBB_CONDITION_CASES (gbb
).create (0);
279 free_gimple_poly_bb (gimple_poly_bb_p gbb
)
281 free_data_refs (GBB_DATA_REFS (gbb
));
283 GBB_CONDITIONS (gbb
).release ();
284 GBB_CONDITION_CASES (gbb
).release ();
285 GBB_BB (gbb
)->aux
= 0;
289 /* Deletes all gimple bbs in SCOP. */
292 remove_gbbs_in_scop (scop_p scop
)
297 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
298 free_gimple_poly_bb (PBB_BLACK_BOX (pbb
));
301 /* Creates a new SCOP containing the region (ENTRY, EXIT). */
304 new_scop (edge entry
, edge exit
)
306 sese_info_p region
= new_sese_info (entry
, exit
);
307 scop_p scop
= XNEW (struct scop
);
309 scop
->param_context
= NULL
;
310 scop
->must_raw
= NULL
;
311 scop
->may_raw
= NULL
;
312 scop
->must_raw_no_source
= NULL
;
313 scop
->may_raw_no_source
= NULL
;
314 scop
->must_war
= NULL
;
315 scop
->may_war
= NULL
;
316 scop
->must_war_no_source
= NULL
;
317 scop
->may_war_no_source
= NULL
;
318 scop
->must_waw
= NULL
;
319 scop
->may_waw
= NULL
;
320 scop
->must_waw_no_source
= NULL
;
321 scop
->may_waw_no_source
= NULL
;
322 scop_set_region (scop
, region
);
323 scop
->pbbs
.create (3);
324 POLY_SCOP_P (scop
) = false;
325 scop
->drs
.create (3);
333 free_scop (scop_p scop
)
338 remove_gbbs_in_scop (scop
);
339 free_sese_info (SCOP_REGION (scop
));
341 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
344 scop
->pbbs
.release ();
346 isl_set_free (scop
->param_context
);
347 isl_union_map_free (scop
->must_raw
);
348 isl_union_map_free (scop
->may_raw
);
349 isl_union_map_free (scop
->must_raw_no_source
);
350 isl_union_map_free (scop
->may_raw_no_source
);
351 isl_union_map_free (scop
->must_war
);
352 isl_union_map_free (scop
->may_war
);
353 isl_union_map_free (scop
->must_war_no_source
);
354 isl_union_map_free (scop
->may_war_no_source
);
355 isl_union_map_free (scop
->must_waw
);
356 isl_union_map_free (scop
->may_waw
);
357 isl_union_map_free (scop
->must_waw_no_source
);
358 isl_union_map_free (scop
->may_waw_no_source
);
362 /* Print to FILE the domain of PBB, at some VERBOSITY level. */
365 print_pbb_domain (FILE *file
, poly_bb_p pbb
, int verbosity ATTRIBUTE_UNUSED
)
367 print_isl_set (file
, pbb
->domain
);
370 /* Dump the cases of a graphite basic block GBB on FILE. */
373 dump_gbb_cases (FILE *file
, gimple_poly_bb_p gbb
)
382 cases
= GBB_CONDITION_CASES (gbb
);
383 if (cases
.is_empty ())
386 fprintf (file
, "# cases bb_%d (\n", GBB_BB (gbb
)->index
);
388 FOR_EACH_VEC_ELT (cases
, i
, stmt
)
390 fprintf (file
, "# ");
391 print_gimple_stmt (file
, stmt
, 0, 0);
394 fprintf (file
, "#)\n");
397 /* Dump conditions of a graphite basic block GBB on FILE. */
400 dump_gbb_conditions (FILE *file
, gimple_poly_bb_p gbb
)
404 vec
<gimple
*> conditions
;
409 conditions
= GBB_CONDITIONS (gbb
);
410 if (conditions
.is_empty ())
413 fprintf (file
, "# conditions bb_%d (\n", GBB_BB (gbb
)->index
);
415 FOR_EACH_VEC_ELT (conditions
, i
, stmt
)
417 fprintf (file
, "# ");
418 print_gimple_stmt (file
, stmt
, 0, 0);
421 fprintf (file
, "#)\n");
424 /* Print to FILE all the data references of PBB, at some VERBOSITY
428 print_pdrs (FILE *file
, poly_bb_p pbb
, int verbosity
)
435 if (PBB_DRS (pbb
).length () == 0)
438 fprintf (file
, "# Access informations are not provided\n");\
439 fprintf (file
, "0\n");
444 fprintf (file
, "# Data references (\n");
447 fprintf (file
, "# Access informations are provided\n");
448 fprintf (file
, "1\n");
450 FOR_EACH_VEC_ELT (PBB_DRS (pbb
), i
, pdr
)
451 if (PDR_TYPE (pdr
) == PDR_READ
)
457 fprintf (file
, "# Read data references (\n");
460 fprintf (file
, "# Read access informations\n");
461 fprintf (file
, "%d\n", nb_reads
);
463 FOR_EACH_VEC_ELT (PBB_DRS (pbb
), i
, pdr
)
464 if (PDR_TYPE (pdr
) == PDR_READ
)
465 print_pdr (file
, pdr
, verbosity
);
468 fprintf (file
, "#)\n");
471 fprintf (file
, "# Write data references (\n");
474 fprintf (file
, "# Write access informations\n");
475 fprintf (file
, "%d\n", nb_writes
);
477 FOR_EACH_VEC_ELT (PBB_DRS (pbb
), i
, pdr
)
478 if (PDR_TYPE (pdr
) != PDR_READ
)
479 print_pdr (file
, pdr
, verbosity
);
482 fprintf (file
, "#)\n");
485 fprintf (file
, "#)\n");
488 /* Print to STDERR all the data references of PBB. */
491 debug_pdrs (poly_bb_p pbb
, int verbosity
)
493 print_pdrs (stderr
, pbb
, verbosity
);
496 /* Print to FILE the body of PBB, at some VERBOSITY level.
497 If statement_body_provided is false statement body is not printed. */
500 print_pbb_body (FILE *file
, poly_bb_p pbb
, int verbosity
,
501 bool statement_body_provided
)
504 fprintf (file
, "# Body (\n");
506 if (!statement_body_provided
)
509 fprintf (file
, "# Statement body is not provided\n");
511 fprintf (file
, "0\n");
514 fprintf (file
, "#)\n");
519 fprintf (file
, "# Statement body is provided\n");
520 fprintf (file
, "1\n");
523 fprintf (file
, "# Original iterator names\n# Iterator names are not provided yet.\n");
526 fprintf (file
, "# Statement body\n");
528 fprintf (file
, "{\n");
529 dump_bb (file
, pbb_bb (pbb
), 0, 0);
530 fprintf (file
, "}\n");
533 fprintf (file
, "#)\n");
536 /* Print to FILE the domain and scattering function of PBB, at some
540 print_pbb (FILE *file
, poly_bb_p pbb
, int verbosity
)
544 fprintf (file
, "# pbb_%d (\n", pbb_index (pbb
));
545 dump_gbb_conditions (file
, PBB_BLACK_BOX (pbb
));
546 dump_gbb_cases (file
, PBB_BLACK_BOX (pbb
));
549 print_pbb_domain (file
, pbb
, verbosity
);
550 print_pdrs (file
, pbb
, verbosity
);
551 print_pbb_body (file
, pbb
, verbosity
, false);
554 fprintf (file
, "#)\n");
557 /* Print to FILE the parameters of SCOP, at some VERBOSITY level. */
560 print_scop_params (FILE *file
, scop_p scop
, int verbosity
)
566 fprintf (file
, "# parameters (\n");
568 if (SESE_PARAMS (SCOP_REGION (scop
)).length ())
571 fprintf (file
, "# Parameter names are provided\n");
573 fprintf (file
, "1\n");
576 fprintf (file
, "# Parameter names\n");
581 fprintf (file
, "# Parameter names are not provided\n");
582 fprintf (file
, "0\n");
585 FOR_EACH_VEC_ELT (SESE_PARAMS (SCOP_REGION (scop
)), i
, t
)
587 print_generic_expr (file
, t
, 0);
591 fprintf (file
, "\n");
594 fprintf (file
, "#)\n");
597 /* Print to FILE the context of SCoP, at some VERBOSITY level. */
600 print_scop_context (FILE *file
, scop_p scop
, int verbosity
)
603 fprintf (file
, "# Context (\n");
605 if (scop
->param_context
)
606 print_isl_set (file
, scop
->param_context
);
609 fprintf (file
, "# )\n");
612 /* Print to FILE the SCOP, at some VERBOSITY level. */
615 print_scop (FILE *file
, scop_p scop
, int verbosity
)
620 fprintf (file
, "SCoP 1\n#(\n");
621 fprintf (file
, "# Language\nGimple\n");
622 print_scop_context (file
, scop
, verbosity
);
623 print_scop_params (file
, scop
, verbosity
);
626 fprintf (file
, "# Number of statements\n");
628 fprintf (file
, "%d\n", scop
->pbbs
.length ());
630 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
631 print_pbb (file
, pbb
, verbosity
);
633 fprintf (file
, "#)\n");
636 /* Print to STDERR the domain of PBB, at some VERBOSITY level. */
639 debug_pbb_domain (poly_bb_p pbb
, int verbosity
)
641 print_pbb_domain (stderr
, pbb
, verbosity
);
644 /* Print to FILE the domain and scattering function of PBB, at some
648 debug_pbb (poly_bb_p pbb
, int verbosity
)
650 print_pbb (stderr
, pbb
, verbosity
);
653 /* Print to STDERR the context of SCOP, at some VERBOSITY level. */
656 debug_scop_context (scop_p scop
, int verbosity
)
658 print_scop_context (stderr
, scop
, verbosity
);
661 /* Print to STDERR the SCOP, at some VERBOSITY level. */
664 debug_scop (scop_p scop
, int verbosity
)
666 print_scop (stderr
, scop
, verbosity
);
669 /* Print to STDERR the parameters of SCOP, at some VERBOSITY
673 debug_scop_params (scop_p scop
, int verbosity
)
675 print_scop_params (stderr
, scop
, verbosity
);
678 extern isl_ctx
*the_isl_ctx
;
680 print_isl_set (FILE *f
, isl_set
*set
)
682 isl_printer
*p
= isl_printer_to_file (the_isl_ctx
, f
);
683 p
= isl_printer_print_set (p
, set
);
684 p
= isl_printer_print_str (p
, "\n");
685 isl_printer_free (p
);
689 debug_isl_set (isl_set
*set
)
691 print_isl_set (stderr
, set
);
695 print_isl_map (FILE *f
, isl_map
*map
)
697 isl_printer
*p
= isl_printer_to_file (the_isl_ctx
, f
);
698 p
= isl_printer_print_map (p
, map
);
699 p
= isl_printer_print_str (p
, "\n");
700 isl_printer_free (p
);
704 debug_isl_map (isl_map
*map
)
706 print_isl_map (stderr
, map
);
710 print_isl_aff (FILE *f
, isl_aff
*aff
)
712 isl_printer
*p
= isl_printer_to_file (the_isl_ctx
, f
);
713 p
= isl_printer_print_aff (p
, aff
);
714 p
= isl_printer_print_str (p
, "\n");
715 isl_printer_free (p
);
719 debug_isl_aff (isl_aff
*aff
)
721 print_isl_aff (stderr
, aff
);
725 print_isl_constraint (FILE *f
, isl_constraint
*c
)
727 isl_printer
*p
= isl_printer_to_file (the_isl_ctx
, f
);
728 p
= isl_printer_print_constraint (p
, c
);
729 p
= isl_printer_print_str (p
, "\n");
730 isl_printer_free (p
);
734 debug_isl_constraint (isl_constraint
*c
)
736 print_isl_constraint (stderr
, c
);
739 /* Returns the number of iterations RES of the loop around PBB at
740 time(scattering) dimension TIME_DEPTH. */
743 pbb_number_of_iterations_at_time (poly_bb_p pbb
,
744 graphite_dim_t time_depth
,
747 isl_set
*transdomain
;
750 isl_val
*isllb
, *islub
;
752 /* Map the iteration domain through the current scatter, and work
753 on the resulting set. */
754 transdomain
= isl_set_apply (isl_set_copy (pbb
->domain
),
755 isl_map_copy (pbb
->transformed
));
757 /* Select the time_depth' dimension via an affine expression. */
758 dc
= isl_set_get_space (transdomain
);
759 aff
= isl_aff_zero_on_domain (isl_local_space_from_space (dc
));
760 aff
= isl_aff_set_coefficient_si (aff
, isl_dim_in
, time_depth
, 1);
762 /* And find the min/max for that function. */
763 /* XXX isl check results? */
764 isllb
= isl_set_min_val (transdomain
, aff
);
765 islub
= isl_set_max_val (transdomain
, aff
);
767 islub
= isl_val_sub (islub
, isllb
);
768 islub
= isl_val_add_ui (islub
, 1);
769 isl_val_get_num_gmp (islub
, res
);
771 isl_val_free (islub
);
773 isl_set_free (transdomain
);
776 #endif /* HAVE_isl */