1 /* Graphite polyhedral representation.
2 Copyright (C) 2009 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/>. */
23 #include "coretypes.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
33 #include "tree-dump.h"
36 #include "tree-chrec.h"
37 #include "tree-data-ref.h"
38 #include "tree-scalar-evolution.h"
39 #include "tree-pass.h"
41 #include "value-prof.h"
42 #include "pointer-set.h"
47 #include "cloog/cloog.h"
50 #include "graphite-ppl.h"
52 #include "graphite-poly.h"
53 #include "graphite-dependences.h"
55 /* Return the maximal loop depth in SCOP. */
58 scop_max_loop_depth (scop_p scop)
64 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
66 int nb_loops = pbb_dim_iter_domain (pbb);
67 if (max_nb_loops < nb_loops)
68 max_nb_loops = nb_loops;
74 /* Extend the scattering matrix of PBB to MAX_SCATTERING scattering
78 extend_scattering (poly_bb_p pbb, int max_scattering)
80 ppl_dimension_type nb_old_dims, nb_new_dims;
82 ppl_Coefficient_t coef;
85 nb_added_dims = max_scattering - pbb_nb_scattering_transform (pbb);
87 value_set_si (one, 1);
88 ppl_new_Coefficient (&coef);
89 ppl_assign_Coefficient_from_mpz_t (coef, one);
91 gcc_assert (nb_added_dims >= 0);
93 nb_old_dims = pbb_nb_scattering_transform (pbb) + pbb_dim_iter_domain (pbb)
94 + scop_nb_params (PBB_SCOP (pbb));
95 nb_new_dims = nb_old_dims + nb_added_dims;
97 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb),
98 pbb_nb_scattering_transform (pbb), nb_added_dims);
99 PBB_NB_SCATTERING_TRANSFORM (pbb) += nb_added_dims;
101 /* Add identity matrix for the added dimensions. */
102 for (i = max_scattering - nb_added_dims; i < max_scattering; i++)
104 ppl_Constraint_t cstr;
105 ppl_Linear_Expression_t expr;
107 ppl_new_Linear_Expression_with_dimension (&expr, nb_new_dims);
108 ppl_Linear_Expression_add_to_coefficient (expr, i, coef);
109 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
110 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb), cstr);
111 ppl_delete_Constraint (cstr);
112 ppl_delete_Linear_Expression (expr);
115 ppl_delete_Coefficient (coef);
119 /* All scattering matrices in SCOP will have the same number of scattering
123 unify_scattering_dimensions (scop_p scop)
127 graphite_dim_t max_scattering = 0;
129 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
130 max_scattering = MAX (pbb_nb_scattering_transform (pbb), max_scattering);
132 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
133 extend_scattering (pbb, max_scattering);
135 return max_scattering;
138 /* Prints to FILE the scattering function of PBB. */
141 print_scattering_function (FILE *file, poly_bb_p pbb)
145 if (!PBB_TRANSFORMED (pbb))
148 fprintf (file, "scattering bb_%d (\n", GBB_BB (PBB_BLACK_BOX (pbb))->index);
149 fprintf (file, "# eq");
151 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
152 fprintf (file, " s%d", (int) i);
154 for (i = 0; i < pbb_nb_local_vars (pbb); i++)
155 fprintf (file, " lv%d", (int) i);
157 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
158 fprintf (file, " i%d", (int) i);
160 for (i = 0; i < pbb_nb_params (pbb); i++)
161 fprintf (file, " p%d", (int) i);
163 fprintf (file, " cst\n");
165 ppl_print_polyhedron_matrix (file, PBB_TRANSFORMED_SCATTERING (pbb));
167 fprintf (file, ")\n");
170 /* Prints to FILE the iteration domain of PBB. */
173 print_iteration_domain (FILE *file, poly_bb_p pbb)
175 print_pbb_domain (file, pbb);
178 /* Prints to FILE the scattering functions of every PBB of SCOP. */
181 print_scattering_functions (FILE *file, scop_p scop)
186 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
187 print_scattering_function (file, pbb);
190 /* Prints to FILE the iteration domains of every PBB of SCOP. */
193 print_iteration_domains (FILE *file, scop_p scop)
198 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
199 print_iteration_domain (file, pbb);
202 /* Prints to STDERR the scattering function of PBB. */
205 debug_scattering_function (poly_bb_p pbb)
207 print_scattering_function (stderr, pbb);
210 /* Prints to STDERR the iteration domain of PBB. */
213 debug_iteration_domain (poly_bb_p pbb)
215 print_iteration_domain (stderr, pbb);
218 /* Prints to STDERR the scattering functions of every PBB of SCOP. */
221 debug_scattering_functions (scop_p scop)
223 print_scattering_functions (stderr, scop);
226 /* Prints to STDERR the iteration domains of every PBB of SCOP. */
229 debug_iteration_domains (scop_p scop)
231 print_iteration_domains (stderr, scop);
234 /* Apply graphite transformations to all the basic blocks of SCOP. */
237 apply_poly_transforms (scop_p scop)
239 bool transform_done = false;
241 gcc_assert (graphite_legal_transform (scop));
243 /* Generate code even if we did not apply any real transformation.
244 This also allows to check the performance for the identity
245 transformation: GIMPLE -> GRAPHITE -> GIMPLE
246 Keep in mind that CLooG optimizes in control, so the loop structure
247 may change, even if we only use -fgraphite-identity. */
248 if (flag_graphite_identity)
249 transform_done = true;
251 if (flag_loop_parallelize_all)
252 transform_done = true;
255 gcc_unreachable (); /* Not yet supported. */
257 if (flag_loop_strip_mine)
258 transform_done |= scop_do_strip_mine (scop);
260 if (flag_loop_interchange)
261 transform_done |= scop_do_interchange (scop);
263 return transform_done;
266 /* Create a new polyhedral data reference and add it to PBB. It is
267 defined by its ACCESSES, its TYPE, and the number of subscripts
271 new_poly_dr (poly_bb_p pbb,
272 ppl_Pointset_Powerset_C_Polyhedron_t accesses,
273 enum POLY_DR_TYPE type, void *cdr, int nb_subscripts)
275 poly_dr_p pdr = XNEW (struct poly_dr);
280 PDR_ACCESSES (pdr) = accesses;
281 PDR_TYPE (pdr) = type;
283 PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
284 VEC_safe_push (poly_dr_p, heap, PBB_DRS (pbb), pdr);
287 /* Free polyhedral data reference PDR. */
290 free_poly_dr (poly_dr_p pdr)
292 ppl_delete_Pointset_Powerset_C_Polyhedron (PDR_ACCESSES (pdr));
296 /* Create a new polyhedral black box. */
299 new_poly_bb (scop_p scop, void *black_box)
301 poly_bb_p pbb = XNEW (struct poly_bb);
303 PBB_DOMAIN (pbb) = NULL;
304 PBB_SCOP (pbb) = scop;
305 pbb_set_black_box (pbb, black_box);
306 PBB_TRANSFORMED (pbb) = NULL;
307 PBB_SAVED (pbb) = NULL;
308 PBB_ORIGINAL (pbb) = NULL;
309 PBB_DRS (pbb) = VEC_alloc (poly_dr_p, heap, 3);
310 VEC_safe_push (poly_bb_p, heap, SCOP_BBS (scop), pbb);
313 /* Free polyhedral black box. */
316 free_poly_bb (poly_bb_p pbb)
321 ppl_delete_Pointset_Powerset_C_Polyhedron (PBB_DOMAIN (pbb));
323 if (PBB_TRANSFORMED (pbb))
324 poly_scattering_free (PBB_TRANSFORMED (pbb));
327 poly_scattering_free (PBB_SAVED (pbb));
329 if (PBB_ORIGINAL (pbb))
330 poly_scattering_free (PBB_ORIGINAL (pbb));
333 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
336 VEC_free (poly_dr_p, heap, PBB_DRS (pbb));
341 print_pdr_access_layout (FILE *file, poly_dr_p pdr)
345 fprintf (file, "# eq");
347 for (i = 0; i < pdr_dim_iter_domain (pdr); i++)
348 fprintf (file, " i%d", (int) i);
350 for (i = 0; i < pdr_nb_params (pdr); i++)
351 fprintf (file, " p%d", (int) i);
353 fprintf (file, " alias");
355 for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
356 fprintf (file, " sub%d", (int) i);
358 fprintf (file, " cst\n");
361 /* Prints to FILE the polyhedral data reference PDR. */
364 print_pdr (FILE *file, poly_dr_p pdr)
366 fprintf (file, "pdr_%d (", PDR_ID (pdr));
368 switch (PDR_TYPE (pdr))
371 fprintf (file, "read \n");
375 fprintf (file, "write \n");
379 fprintf (file, "may_write \n");
386 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
388 fprintf (file, "data accesses (\n");
389 print_pdr_access_layout (file, pdr);
390 ppl_print_powerset_matrix (file, PDR_ACCESSES (pdr));
391 fprintf (file, ")\n");
393 fprintf (file, ")\n");
396 /* Prints to STDERR the polyhedral data reference PDR. */
399 debug_pdr (poly_dr_p pdr)
401 print_pdr (stderr, pdr);
404 /* Creates a new SCOP containing REGION. */
407 new_scop (void *region)
409 scop_p scop = XNEW (struct scop);
411 SCOP_DEP_GRAPH (scop) = NULL;
412 SCOP_CONTEXT (scop) = NULL;
413 scop_set_region (scop, region);
414 SCOP_BBS (scop) = VEC_alloc (poly_bb_p, heap, 3);
415 SCOP_ORIGINAL_PDDRS (scop) = htab_create (10, hash_poly_ddr_p,
416 eq_poly_ddr_p, free_poly_ddr);
423 free_scop (scop_p scop)
428 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
431 VEC_free (poly_bb_p, heap, SCOP_BBS (scop));
433 if (SCOP_CONTEXT (scop))
434 ppl_delete_Pointset_Powerset_C_Polyhedron (SCOP_CONTEXT (scop));
436 htab_delete (SCOP_ORIGINAL_PDDRS (scop));
440 /* Print to FILE the domain of PBB. */
443 print_pbb_domain (FILE *file, poly_bb_p pbb)
446 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
448 if (!PBB_DOMAIN (pbb))
451 fprintf (file, "domains bb_%d (\n", GBB_BB (gbb)->index);
452 fprintf (file, "# eq");
454 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
455 fprintf (file, " i%d", (int) i);
457 for (i = 0; i < pbb_nb_params (pbb); i++)
458 fprintf (file, " p%d", (int) i);
460 fprintf (file, " cst\n");
462 if (PBB_DOMAIN (pbb))
463 ppl_print_powerset_matrix (file, PBB_DOMAIN (pbb));
465 fprintf (file, ")\n");
468 /* Dump the cases of a graphite basic block GBB on FILE. */
471 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
475 VEC (gimple, heap) *cases;
480 cases = GBB_CONDITION_CASES (gbb);
481 if (VEC_empty (gimple, cases))
484 fprintf (file, "cases bb_%d (", GBB_BB (gbb)->index);
486 for (i = 0; VEC_iterate (gimple, cases, i, stmt); i++)
487 print_gimple_stmt (file, stmt, 0, 0);
489 fprintf (file, ")\n");
492 /* Dump conditions of a graphite basic block GBB on FILE. */
495 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
499 VEC (gimple, heap) *conditions;
504 conditions = GBB_CONDITIONS (gbb);
505 if (VEC_empty (gimple, conditions))
508 fprintf (file, "conditions bb_%d (", GBB_BB (gbb)->index);
510 for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
511 print_gimple_stmt (file, stmt, 0, 0);
513 fprintf (file, ")\n");
516 /* Print to FILE all the data references of PBB. */
519 print_pdrs (FILE *file, poly_bb_p pbb)
524 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
525 print_pdr (file, pdr);
528 /* Print to STDERR all the data references of PBB. */
531 debug_pdrs (poly_bb_p pbb)
533 print_pdrs (stderr, pbb);
536 /* Print to FILE the domain and scattering function of PBB. */
539 print_pbb (FILE *file, poly_bb_p pbb)
541 fprintf (file, "pbb_%d (\n", GBB_BB (PBB_BLACK_BOX (pbb))->index);
542 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
543 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
544 print_pdrs (file, pbb);
545 print_pbb_domain (file, pbb);
546 print_scattering_function (file, pbb);
547 fprintf (file, ")\n");
550 /* Print to FILE the parameters of SCOP. */
553 print_scop_params (FILE *file, scop_p scop)
558 fprintf (file, "parameters (\n");
559 for (i = 0; VEC_iterate (tree, SESE_PARAMS (SCOP_REGION (scop)), i, t); i++)
561 fprintf (file, "p_%d -> ", i);
562 print_generic_expr (file, t, 0);
563 fprintf (file, "\n");
565 fprintf (file, ")\n");
568 /* Print to FILE the context of SCoP. */
570 print_scop_context (FILE *file, scop_p scop)
574 fprintf (file, "context (\n");
575 fprintf (file, "# eq");
577 for (i = 0; i < scop_nb_params (scop); i++)
578 fprintf (file, " p%d", (int) i);
580 fprintf (file, " cst\n");
582 if (SCOP_CONTEXT (scop))
583 ppl_print_powerset_matrix (file, SCOP_CONTEXT (scop));
585 fprintf (file, ")\n");
588 /* Print to FILE the SCOP. */
591 print_scop (FILE *file, scop_p scop)
596 fprintf (file, "scop (\n");
597 print_scop_params (file, scop);
598 print_scop_context (file, scop);
600 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
601 print_pbb (file, pbb);
603 fprintf (file, ")\n");
606 /* Print to STDERR the domain of PBB. */
609 debug_pbb_domain (poly_bb_p pbb)
611 print_pbb_domain (stderr, pbb);
614 /* Print to FILE the domain and scattering function of PBB. */
617 debug_pbb (poly_bb_p pbb)
619 print_pbb (stderr, pbb);
622 /* Print to STDERR the context of SCOP. */
625 debug_scop_context (scop_p scop)
627 print_scop_context (stderr, scop);
630 /* Print to STDERR the SCOP. */
633 debug_scop (scop_p scop)
635 print_scop (stderr, scop);
638 /* Print to STDERR the parameters of SCOP. */
641 debug_scop_params (scop_p scop)
643 print_scop_params (stderr, scop);
647 /* The dimension in the transformed scattering polyhedron of PBB
648 containing the scattering iterator for the loop at depth LOOP_DEPTH. */
651 psct_scattering_dim_for_loop_depth (poly_bb_p pbb, graphite_dim_t loop_depth)
653 ppl_const_Constraint_System_t pcs;
654 ppl_Constraint_System_const_iterator_t cit, cend;
655 ppl_const_Constraint_t cstr;
656 ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
657 ppl_dimension_type iter = psct_iterator_dim (pbb, loop_depth);
658 ppl_Linear_Expression_t expr;
659 ppl_Coefficient_t coef;
664 ppl_new_Coefficient (&coef);
665 ppl_Polyhedron_get_constraints (ph, &pcs);
666 ppl_new_Constraint_System_const_iterator (&cit);
667 ppl_new_Constraint_System_const_iterator (&cend);
669 for (ppl_Constraint_System_begin (pcs, cit),
670 ppl_Constraint_System_end (pcs, cend);
671 !ppl_Constraint_System_const_iterator_equal_test (cit, cend);
672 ppl_Constraint_System_const_iterator_increment (cit))
674 ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
675 ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
676 ppl_Linear_Expression_coefficient (expr, iter, coef);
677 ppl_Coefficient_to_mpz_t (coef, val);
679 if (value_zero_p (val))
681 ppl_delete_Linear_Expression (expr);
685 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
687 ppl_dimension_type scatter = psct_scattering_dim (pbb, i);
689 ppl_Linear_Expression_coefficient (expr, scatter, coef);
690 ppl_Coefficient_to_mpz_t (coef, val);
692 if (value_notzero_p (val))
695 ppl_delete_Linear_Expression (expr);
696 ppl_delete_Coefficient (coef);
697 ppl_delete_Constraint_System_const_iterator (cit);
698 ppl_delete_Constraint_System_const_iterator (cend);
708 /* Returns the number of iterations NITER of the loop around PBB at
712 pbb_number_of_iterations (poly_bb_p pbb,
713 graphite_dim_t loop_depth,
716 ppl_Linear_Expression_t le;
717 ppl_dimension_type dim;
719 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
720 ppl_new_Linear_Expression_with_dimension (&le, dim);
721 ppl_set_coef (le, pbb_iterator_dim (pbb, loop_depth), 1);
722 value_set_si (niter, -1);
723 ppl_max_for_le_pointset (PBB_DOMAIN (pbb), le, niter);
724 ppl_delete_Linear_Expression (le);
727 /* Returns the number of iterations NITER of the loop around PBB at
728 time(scattering) dimension TIME_DEPTH. */
731 pbb_number_of_iterations_at_time (poly_bb_p pbb,
732 graphite_dim_t time_depth,
735 ppl_Pointset_Powerset_C_Polyhedron_t ext_domain, sctr;
736 ppl_Linear_Expression_t le;
737 ppl_dimension_type dim;
739 value_set_si (niter, -1);
741 /* Takes together domain and scattering polyhedrons, and composes
742 them into the bigger polyhedron that has the following format:
743 t0..t_{n-1} | l0..l_{nlcl-1} | i0..i_{niter-1} | g0..g_{nparm-1}.
744 t0..t_{n-1} are time dimensions (scattering dimensions)
745 l0..l_{nclc-1} are local variables in scatterin function
746 i0..i_{niter-1} are original iteration variables
747 g0..g_{nparam-1} are global parameters. */
749 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
750 (&ext_domain, PBB_DOMAIN (pbb));
751 ppl_insert_dimensions_pointset (ext_domain, 0,
752 pbb_nb_scattering_transform (pbb)
753 + pbb_nb_local_vars (pbb));
754 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&sctr,
755 PBB_TRANSFORMED_SCATTERING (pbb));
756 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (sctr, ext_domain);
758 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (sctr, &dim);
759 ppl_new_Linear_Expression_with_dimension (&le, dim);
760 ppl_set_coef (le, time_depth, 1);
761 ppl_max_for_le_pointset (sctr, le, niter);
763 ppl_delete_Linear_Expression (le);
764 ppl_delete_Pointset_Powerset_C_Polyhedron (sctr);
765 ppl_delete_Pointset_Powerset_C_Polyhedron (ext_domain);