2009-08-05 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / graphite-poly.c
blob2df04af601880c0b52a9760889bd332df68e3f9c
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)
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "output.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "toplev.h"
33 #include "tree-dump.h"
34 #include "timevar.h"
35 #include "cfgloop.h"
36 #include "tree-chrec.h"
37 #include "tree-data-ref.h"
38 #include "tree-scalar-evolution.h"
39 #include "tree-pass.h"
40 #include "domwalk.h"
41 #include "value-prof.h"
42 #include "pointer-set.h"
43 #include "gimple.h"
44 #include "params.h"
46 #ifdef HAVE_cloog
47 #include "cloog/cloog.h"
48 #include "ppl_c.h"
49 #include "sese.h"
50 #include "graphite-ppl.h"
51 #include "graphite.h"
52 #include "graphite-poly.h"
53 #include "graphite-dependences.h"
55 /* Return the maximal loop depth in SCOP. */
57 int
58 scop_max_loop_depth (scop_p scop)
60 int i;
61 poly_bb_p pbb;
62 int max_nb_loops = 0;
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;
71 return max_nb_loops;
74 /* Extend the scattering matrix of PBB to MAX_SCATTERING scattering
75 dimensions. */
77 static void
78 extend_scattering (poly_bb_p pbb, int max_scattering)
80 ppl_dimension_type nb_old_dims, nb_new_dims;
81 int nb_added_dims, i;
82 ppl_Coefficient_t coef;
83 Value one;
85 nb_added_dims = max_scattering - pbb_nb_scattering_transform (pbb);
86 value_init (one);
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);
116 value_clear (one);
119 /* All scattering matrices in SCOP will have the same number of scattering
120 dimensions. */
123 unify_scattering_dimensions (scop_p scop)
125 int i;
126 poly_bb_p pbb;
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. */
140 void
141 print_scattering_function (FILE *file, poly_bb_p pbb)
143 graphite_dim_t i;
145 if (!PBB_TRANSFORMED_SCATTERING (pbb))
146 return;
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. */
172 void
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. */
180 void
181 print_scattering_functions (FILE *file, scop_p scop)
183 int i;
184 poly_bb_p pbb;
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. */
192 void
193 print_iteration_domains (FILE *file, scop_p scop)
195 int i;
196 poly_bb_p pbb;
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. */
204 void
205 debug_scattering_function (poly_bb_p pbb)
207 print_scattering_function (stderr, pbb);
210 /* Prints to STDERR the iteration domain of PBB. */
212 void
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. */
220 void
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. */
228 void
229 debug_iteration_domains (scop_p scop)
231 print_iteration_domains (stderr, scop);
234 /* Apply graphite transformations to all the basic blocks of SCOP. */
236 bool
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;
254 if (flag_loop_block)
255 gcc_unreachable (); /* Not yet supported. */
257 if (flag_loop_strip_mine)
259 transform_done |= scop_do_strip_mine (scop);
260 gcc_assert (graphite_legal_transform (scop));
263 if (flag_loop_interchange)
265 transform_done |= scop_do_interchange (scop);
266 gcc_assert (graphite_legal_transform (scop));
269 return transform_done;
272 /* Create a new polyhedral data reference and add it to PBB. It is defined by
273 its ACCESSES, its TYPE*/
275 void
276 new_poly_dr (poly_bb_p pbb,
277 ppl_Pointset_Powerset_C_Polyhedron_t accesses,
278 ppl_Pointset_Powerset_C_Polyhedron_t data_container,
279 enum POLY_DR_TYPE type, void *cdr)
281 poly_dr_p pdr = XNEW (struct poly_dr);
283 PDR_PBB (pdr) = pbb;
284 PDR_ACCESSES (pdr) = accesses;
285 PDR_DATA_CONTAINER (pdr) = data_container;
286 PDR_TYPE (pdr) = type;
287 PDR_CDR (pdr) = cdr;
288 VEC_safe_push (poly_dr_p, heap, PBB_DRS (pbb), pdr);
291 /* Free polyhedral data reference PDR. */
293 void
294 free_poly_dr (poly_dr_p pdr)
296 ppl_delete_Pointset_Powerset_C_Polyhedron (PDR_ACCESSES (pdr));
297 ppl_delete_Pointset_Powerset_C_Polyhedron (PDR_DATA_CONTAINER (pdr));
299 XDELETE (pdr);
302 /* Create a new polyhedral black box. */
304 void
305 new_poly_bb (scop_p scop, void *black_box)
307 poly_bb_p pbb = XNEW (struct poly_bb);
309 PBB_DOMAIN (pbb) = NULL;
310 PBB_SCOP (pbb) = scop;
311 pbb_set_black_box (pbb, black_box);
312 PBB_TRANSFORMED_SCATTERING (pbb) = NULL;
313 PBB_ORIGINAL_SCATTERING (pbb) = NULL;
314 PBB_DRS (pbb) = VEC_alloc (poly_dr_p, heap, 3);
315 PBB_NB_SCATTERING_TRANSFORM (pbb) = 0;
316 PBB_NB_LOCAL_VARIABLES (pbb) = 0;
317 VEC_safe_push (poly_bb_p, heap, SCOP_BBS (scop), pbb);
320 /* Free polyhedral black box. */
322 void
323 free_poly_bb (poly_bb_p pbb)
325 int i;
326 poly_dr_p pdr;
328 ppl_delete_Pointset_Powerset_C_Polyhedron (PBB_DOMAIN (pbb));
330 if (PBB_TRANSFORMED_SCATTERING (pbb))
331 ppl_delete_Polyhedron (PBB_TRANSFORMED_SCATTERING (pbb));
333 if (PBB_ORIGINAL_SCATTERING (pbb))
334 ppl_delete_Polyhedron (PBB_ORIGINAL_SCATTERING (pbb));
336 if (PBB_DRS (pbb))
337 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
338 free_poly_dr (pdr);
340 VEC_free (poly_dr_p, heap, PBB_DRS (pbb));
341 XDELETE (pbb);
344 static void
345 print_pdr_access_layout (FILE *file, poly_dr_p pdr)
347 graphite_dim_t i;
349 fprintf (file, "# eq");
351 for (i = 0; i < pdr_dim_iter_domain (pdr); i++)
352 fprintf (file, " i%d", (int) i);
354 for (i = 0; i < pdr_nb_params (pdr); i++)
355 fprintf (file, " p%d", (int) i);
357 fprintf (file, " alias");
359 for (i = 0; i < pdr_nb_subscripts (pdr); i++)
360 fprintf (file, " sub%d", (int) i);
362 fprintf (file, " cst\n");
365 /* Prints to FILE the polyhedral data reference PDR. */
367 void
368 print_pdr (FILE *file, poly_dr_p pdr)
370 fprintf (file, "pdr (");
372 switch (PDR_TYPE (pdr))
374 case PDR_READ:
375 fprintf (file, "read \n");
376 break;
378 case PDR_WRITE:
379 fprintf (file, "write \n");
380 break;
382 case PDR_MAY_WRITE:
383 fprintf (file, "may_write \n");
384 break;
386 default:
387 gcc_unreachable ();
390 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
392 fprintf (file, "data accesses (\n");
393 print_pdr_access_layout (file, pdr);
394 ppl_print_powerset_matrix (file, PDR_ACCESSES (pdr));
395 fprintf (file, ")\n");
397 fprintf (file, "data container (\n");
398 print_pdr_access_layout (file, pdr);
399 ppl_print_powerset_matrix (file, PDR_DATA_CONTAINER (pdr));
400 fprintf (file, ")\n");
402 fprintf (file, ")\n");
405 /* Prints to STDERR the polyhedral data reference PDR. */
407 void
408 debug_pdr (poly_dr_p pdr)
410 print_pdr (stderr, pdr);
413 /* Creates a new SCOP containing REGION. */
415 scop_p
416 new_scop (void *region)
418 scop_p scop = XNEW (struct scop);
420 SCOP_DEP_GRAPH (scop) = NULL;
421 SCOP_CONTEXT (scop) = NULL;
422 scop_set_region (scop, region);
423 SCOP_BBS (scop) = VEC_alloc (poly_bb_p, heap, 3);
424 SCOP_ORIGINAL_PDR_PAIRS (scop) = htab_create (10, hash_poly_dr_pair_p,
425 eq_poly_dr_pair_p, free);
426 return scop;
429 /* Deletes SCOP. */
431 void
432 free_scop (scop_p scop)
434 int i;
435 poly_bb_p pbb;
437 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
438 free_poly_bb (pbb);
440 VEC_free (poly_bb_p, heap, SCOP_BBS (scop));
442 if (SCOP_CONTEXT (scop))
443 ppl_delete_Pointset_Powerset_C_Polyhedron (SCOP_CONTEXT (scop));
445 htab_delete (SCOP_ORIGINAL_PDR_PAIRS (scop));
446 XDELETE (scop);
449 /* Print to FILE the domain of PBB. */
451 void
452 print_pbb_domain (FILE *file, poly_bb_p pbb)
454 graphite_dim_t i;
455 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
457 if (!PBB_DOMAIN (pbb))
458 return;
460 fprintf (file, "domains bb_%d (\n", GBB_BB (gbb)->index);
461 fprintf (file, "# eq");
463 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
464 fprintf (file, " i%d", (int) i);
466 for (i = 0; i < pbb_nb_params (pbb); i++)
467 fprintf (file, " p%d", (int) i);
469 fprintf (file, " cst\n");
471 if (PBB_DOMAIN (pbb))
472 ppl_print_powerset_matrix (file, PBB_DOMAIN (pbb));
474 fprintf (file, ")\n");
477 /* Dump the cases of a graphite basic block GBB on FILE. */
479 static void
480 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
482 int i;
483 gimple stmt;
484 VEC (gimple, heap) *cases;
486 if (!gbb)
487 return;
489 cases = GBB_CONDITION_CASES (gbb);
490 if (VEC_empty (gimple, cases))
491 return;
493 fprintf (file, "cases bb_%d (", GBB_BB (gbb)->index);
495 for (i = 0; VEC_iterate (gimple, cases, i, stmt); i++)
496 print_gimple_stmt (file, stmt, 0, 0);
498 fprintf (file, ")\n");
501 /* Dump conditions of a graphite basic block GBB on FILE. */
503 static void
504 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
506 int i;
507 gimple stmt;
508 VEC (gimple, heap) *conditions;
510 if (!gbb)
511 return;
513 conditions = GBB_CONDITIONS (gbb);
514 if (VEC_empty (gimple, conditions))
515 return;
517 fprintf (file, "conditions bb_%d (", GBB_BB (gbb)->index);
519 for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
520 print_gimple_stmt (file, stmt, 0, 0);
522 fprintf (file, ")\n");
525 /* Print to FILE all the data references of PBB. */
527 void
528 print_pdrs (FILE *file, poly_bb_p pbb)
530 int i;
531 poly_dr_p pdr;
533 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
534 print_pdr (file, pdr);
537 /* Print to STDERR all the data references of PBB. */
539 void
540 debug_pdrs (poly_bb_p pbb)
542 print_pdrs (stderr, pbb);
545 /* Print to FILE the domain and scattering function of PBB. */
547 void
548 print_pbb (FILE *file, poly_bb_p pbb)
550 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
551 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
552 print_pdrs (file, pbb);
553 print_pbb_domain (file, pbb);
554 print_scattering_function (file, pbb);
557 /* Print to FILE the parameters of SCOP. */
559 void
560 print_scop_params (FILE *file, scop_p scop)
562 int i;
563 tree t;
565 fprintf (file, "parameters (\n");
566 for (i = 0; VEC_iterate (tree, SESE_PARAMS (SCOP_REGION (scop)), i, t); i++)
568 fprintf (file, "p_%d -> ", i);
569 print_generic_expr (file, t, 0);
570 fprintf (file, "\n");
572 fprintf (file, ")\n");
575 /* Print to FILE the context of SCoP. */
576 void
577 print_scop_context (FILE *file, scop_p scop)
579 graphite_dim_t i;
581 fprintf (file, "context (\n");
582 fprintf (file, "# eq");
584 for (i = 0; i < scop_nb_params (scop); i++)
585 fprintf (file, " p%d", (int) i);
587 fprintf (file, " cst\n");
589 if (SCOP_CONTEXT (scop))
590 ppl_print_powerset_matrix (file, SCOP_CONTEXT (scop));
592 fprintf (file, ")\n");
595 /* Print to FILE the SCOP. */
597 void
598 print_scop (FILE *file, scop_p scop)
600 int i;
601 poly_bb_p pbb;
603 print_scop_params (file, scop);
604 print_scop_context (file, scop);
606 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
607 print_pbb (file, pbb);
610 /* Print to STDERR the domain of PBB. */
612 void
613 debug_pbb_domain (poly_bb_p pbb)
615 print_pbb_domain (stderr, pbb);
618 /* Print to FILE the domain and scattering function of PBB. */
620 void
621 debug_pbb (poly_bb_p pbb)
623 print_pbb (stderr, pbb);
626 /* Print to STDERR the context of SCOP. */
628 void
629 debug_scop_context (scop_p scop)
631 print_scop_context (stderr, scop);
634 /* Print to STDERR the SCOP. */
636 void
637 debug_scop (scop_p scop)
639 print_scop (stderr, scop);
642 /* Print to STDERR the parameters of SCOP. */
644 void
645 debug_scop_params (scop_p scop)
647 print_scop_params (stderr, scop);
651 /* The dimension in the transformed scattering polyhedron of PBB
652 containing the scattering iterator for the loop at depth LOOP_DEPTH. */
654 ppl_dimension_type
655 psct_scattering_dim_for_loop_depth (poly_bb_p pbb, graphite_dim_t loop_depth)
657 ppl_const_Constraint_System_t pcs;
658 ppl_Constraint_System_const_iterator_t cit, cend;
659 ppl_const_Constraint_t cstr;
660 ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
661 ppl_dimension_type iter = psct_iterator_dim (pbb, loop_depth);
662 ppl_Linear_Expression_t expr;
663 ppl_Coefficient_t coef;
664 Value val;
665 graphite_dim_t i;
667 value_init (val);
668 ppl_new_Coefficient (&coef);
669 ppl_Polyhedron_get_constraints (ph, &pcs);
670 ppl_new_Constraint_System_const_iterator (&cit);
671 ppl_new_Constraint_System_const_iterator (&cend);
673 for (ppl_Constraint_System_begin (pcs, cit),
674 ppl_Constraint_System_end (pcs, cend);
675 !ppl_Constraint_System_const_iterator_equal_test (cit, cend);
676 ppl_Constraint_System_const_iterator_increment (cit))
678 ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
679 ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
680 ppl_Linear_Expression_coefficient (expr, iter, coef);
681 ppl_Coefficient_to_mpz_t (coef, val);
683 if (value_zero_p (val))
685 ppl_delete_Linear_Expression (expr);
686 continue;
689 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
691 ppl_dimension_type scatter = psct_scattering_dim (pbb, i);
693 ppl_Linear_Expression_coefficient (expr, scatter, coef);
694 ppl_Coefficient_to_mpz_t (coef, val);
696 if (value_notzero_p (val))
698 value_clear (val);
699 ppl_delete_Linear_Expression (expr);
700 ppl_delete_Coefficient (coef);
701 ppl_delete_Constraint_System_const_iterator (cit);
702 ppl_delete_Constraint_System_const_iterator (cend);
704 return scatter;
709 gcc_unreachable ();
712 /* Returns the number of iterations NITER of the loop around PBB at
713 depth LOOP_DEPTH. */
715 void
716 pbb_number_of_iterations (poly_bb_p pbb,
717 graphite_dim_t loop_depth,
718 Value niter)
720 ppl_dimension_type loop_iter = pbb_iterator_dim (pbb, loop_depth);
721 ppl_Linear_Expression_t le;
722 ppl_Coefficient_t num, denom;
723 Value dv;
724 int maximum;
725 ppl_dimension_type dim;
727 value_init (dv);
728 ppl_new_Coefficient (&num);
729 ppl_new_Coefficient (&denom);
730 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
731 ppl_new_Linear_Expression_with_dimension (&le, dim);
732 ppl_set_coef (le, loop_iter, 1);
733 ppl_Pointset_Powerset_C_Polyhedron_maximize (PBB_DOMAIN (pbb), le,
734 num, denom, &maximum);
736 if (maximum == 1)
738 ppl_Coefficient_to_mpz_t (num, niter);
739 ppl_Coefficient_to_mpz_t (denom, dv);
740 value_division (niter, niter, dv);
742 else
743 value_set_si (niter, -1);
745 value_clear (dv);
746 ppl_delete_Linear_Expression (le);
747 ppl_delete_Coefficient (num);
748 ppl_delete_Coefficient (denom);
751 #endif