1 /* Graphite polyhedral representation.
2 Copyright (C) 2009, 2010 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-pretty-print.h"
32 #include "gimple-pretty-print.h"
33 #include "tree-flow.h"
35 #include "tree-dump.h"
38 #include "tree-chrec.h"
39 #include "tree-data-ref.h"
40 #include "tree-scalar-evolution.h"
41 #include "tree-pass.h"
43 #include "value-prof.h"
44 #include "pointer-set.h"
47 #include "graphite-cloog-util.h"
52 #include "graphite-ppl.h"
54 #include "graphite-poly.h"
55 #include "graphite-dependences.h"
57 /* Return the maximal loop depth in SCOP. */
60 scop_max_loop_depth (scop_p scop
)
66 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
68 int nb_loops
= pbb_dim_iter_domain (pbb
);
69 if (max_nb_loops
< nb_loops
)
70 max_nb_loops
= nb_loops
;
76 /* Extend the scattering matrix of PBB to MAX_SCATTERING scattering
80 extend_scattering (poly_bb_p pbb
, int max_scattering
)
82 ppl_dimension_type nb_old_dims
, nb_new_dims
;
84 ppl_Coefficient_t coef
;
87 nb_added_dims
= max_scattering
- pbb_nb_scattering_transform (pbb
);
90 ppl_new_Coefficient (&coef
);
91 ppl_assign_Coefficient_from_mpz_t (coef
, one
);
93 gcc_assert (nb_added_dims
>= 0);
95 nb_old_dims
= pbb_nb_scattering_transform (pbb
) + pbb_dim_iter_domain (pbb
)
96 + scop_nb_params (PBB_SCOP (pbb
));
97 nb_new_dims
= nb_old_dims
+ nb_added_dims
;
99 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb
),
100 pbb_nb_scattering_transform (pbb
), nb_added_dims
);
101 PBB_NB_SCATTERING_TRANSFORM (pbb
) += nb_added_dims
;
103 /* Add identity matrix for the added dimensions. */
104 for (i
= max_scattering
- nb_added_dims
; i
< max_scattering
; i
++)
106 ppl_Constraint_t cstr
;
107 ppl_Linear_Expression_t expr
;
109 ppl_new_Linear_Expression_with_dimension (&expr
, nb_new_dims
);
110 ppl_Linear_Expression_add_to_coefficient (expr
, i
, coef
);
111 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_EQUAL
);
112 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb
), cstr
);
113 ppl_delete_Constraint (cstr
);
114 ppl_delete_Linear_Expression (expr
);
117 ppl_delete_Coefficient (coef
);
121 /* All scattering matrices in SCOP will have the same number of scattering
125 unify_scattering_dimensions (scop_p scop
)
129 graphite_dim_t max_scattering
= 0;
131 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
132 max_scattering
= MAX (pbb_nb_scattering_transform (pbb
), max_scattering
);
134 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
135 extend_scattering (pbb
, max_scattering
);
137 return max_scattering
;
140 /* Print to FILE the pdr PH in OpenScop format. NB_SUBSCRIPTS is the number
141 of subscripts in PH, ALIAS_SET_DIM is the dimension of the alias set and
142 NB_PARAMS is the number of parameters in PH. */
145 openscop_print_pdr_polyhedron (FILE *file
, ppl_const_Polyhedron_t ph
,
146 int nb_subscripts
, int alias_set_dimension
,
149 int input
, locals
, output
;
150 ppl_dimension_type alias_set_dim
= (ppl_dimension_type
) alias_set_dimension
;
151 ppl_dimension_type sub_dim_last
= alias_set_dim
+ nb_subscripts
;
152 ppl_dimension_type
*map
, i
, ph_space_dim
= sub_dim_last
+ 1;
153 ppl_Polyhedron_t pph
;
155 ppl_new_C_Polyhedron_from_C_Polyhedron (&pph
, ph
);
157 map
= (ppl_dimension_type
*) XNEWVEC (ppl_dimension_type
, ph_space_dim
);
159 for (i
= 0; i
< alias_set_dim
- 1; i
++)
160 map
[i
] = nb_subscripts
+ 1 + i
;
162 for (i
= alias_set_dim
- 1; i
< sub_dim_last
; i
++)
163 map
[i
] = i
- alias_set_dim
+ 1;
165 ppl_Polyhedron_map_space_dimensions (pph
, map
, ph_space_dim
- 1);
168 input
= alias_set_dim
- nb_params
- 1;
170 /* According to OpenScop specification, the alias set column is a part of
171 the output columns. */
172 output
= nb_subscripts
+ 1;
174 openscop_print_polyhedron_matrix (file
, pph
, output
, input
, locals
, nb_params
);
177 /* Print to FILE the powerset PDR. NB_SUBSCRIPTS is the number of subscripts
178 in PDR, ALIAS_SET_DIM is the dimension of the alias set in PDR and
179 NB_PARAMS is the number of parameters in PDR. */
182 openscop_print_pdr_powerset (FILE *file
,
183 ppl_Pointset_Powerset_C_Polyhedron_t ps
,
189 ppl_Pointset_Powerset_C_Polyhedron_iterator_t it
, end
;
191 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&it
);
192 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&end
);
194 ppl_Pointset_Powerset_C_Polyhedron_size (ps
, &nb_disjuncts
);
195 fprintf (file
, "%d\n", (int) nb_disjuncts
);
197 for (ppl_Pointset_Powerset_C_Polyhedron_iterator_begin (ps
, it
),
198 ppl_Pointset_Powerset_C_Polyhedron_iterator_end (ps
, end
);
199 !ppl_Pointset_Powerset_C_Polyhedron_iterator_equal_test (it
, end
);
200 ppl_Pointset_Powerset_C_Polyhedron_iterator_increment (it
))
202 ppl_const_Polyhedron_t ph
;
204 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it
, &ph
);
205 openscop_print_pdr_polyhedron (file
, ph
, nb_subscripts
, alias_set_dim
,
209 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (it
);
210 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (end
);
213 /* Print to FILE the powerset PS in its OpenScop matrix form. */
216 openscop_print_powerset_matrix (FILE *file
,
217 ppl_Pointset_Powerset_C_Polyhedron_t ps
,
218 int output
, int input
, int locals
,
222 ppl_Pointset_Powerset_C_Polyhedron_iterator_t it
, end
;
224 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&it
);
225 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&end
);
227 ppl_Pointset_Powerset_C_Polyhedron_size (ps
, &nb_disjuncts
);
228 fprintf (file
, "%d\n", (int) nb_disjuncts
);
230 for (ppl_Pointset_Powerset_C_Polyhedron_iterator_begin (ps
, it
),
231 ppl_Pointset_Powerset_C_Polyhedron_iterator_end (ps
, end
);
232 !ppl_Pointset_Powerset_C_Polyhedron_iterator_equal_test (it
, end
);
233 ppl_Pointset_Powerset_C_Polyhedron_iterator_increment (it
))
235 ppl_const_Polyhedron_t ph
;
237 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it
, &ph
);
238 openscop_print_polyhedron_matrix (file
, ph
, output
, input
, locals
,
242 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (it
);
243 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (end
);
246 /* Prints to FILE the scattering function of PBB in OpenScop format, at some
250 openscop_print_scattering_function_1 (FILE *file
, poly_bb_p pbb
, int verbosity
)
253 ppl_const_Polyhedron_t ph
;
257 fprintf (file
, "# scattering bb_%d (\n", pbb_index (pbb
));
258 fprintf (file
, "# eq");
260 for (i
= 0; i
< pbb_nb_scattering_transform (pbb
); i
++)
261 fprintf (file
, " s%d", (int) i
);
263 for (i
= 0; i
< pbb_nb_local_vars (pbb
); i
++)
264 fprintf (file
, " lv%d", (int) i
);
266 for (i
= 0; i
< pbb_dim_iter_domain (pbb
); i
++)
267 fprintf (file
, " i%d", (int) i
);
269 for (i
= 0; i
< pbb_nb_params (pbb
); i
++)
270 fprintf (file
, " p%d", (int) i
);
272 fprintf (file
, " cst\n");
275 /* Number of disjunct components. Remove this when
276 PBB_TRANSFORMED_SCATTERING will be a pointset_powerset. */
277 fprintf (file
, "1\n");
279 ph
= PBB_TRANSFORMED_SCATTERING (pbb
)
280 ? PBB_TRANSFORMED_SCATTERING (pbb
)
281 : PBB_ORIGINAL_SCATTERING (pbb
);
283 openscop_print_polyhedron_matrix (file
, ph
,
284 pbb_nb_scattering_transform (pbb
),
285 pbb_dim_iter_domain (pbb
),
286 pbb_nb_local_vars (pbb
),
287 pbb_nb_params (pbb
));
290 fprintf (file
, "#)\n");
293 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
297 print_scattering_function_1 (FILE *file
, poly_bb_p pbb
, int verbosity
)
303 fprintf (file
, "# scattering bb_%d (\n", pbb_index (pbb
));
304 fprintf (file
, "# eq");
306 for (i
= 0; i
< pbb_nb_scattering_transform (pbb
); i
++)
307 fprintf (file
, " s%d", (int) i
);
309 for (i
= 0; i
< pbb_nb_local_vars (pbb
); i
++)
310 fprintf (file
, " lv%d", (int) i
);
312 for (i
= 0; i
< pbb_dim_iter_domain (pbb
); i
++)
313 fprintf (file
, " i%d", (int) i
);
315 for (i
= 0; i
< pbb_nb_params (pbb
); i
++)
316 fprintf (file
, " p%d", (int) i
);
318 fprintf (file
, " cst\n");
321 /* Number of disjunct components. Remove this when
322 PBB_TRANSFORMED_SCATTERING will be a pointset_powerset. */
323 fprintf (file
, "1\n");
324 ppl_print_polyhedron_matrix (file
, PBB_TRANSFORMED_SCATTERING (pbb
)
325 ? PBB_TRANSFORMED_SCATTERING (pbb
)
326 : PBB_ORIGINAL_SCATTERING (pbb
));
329 fprintf (file
, "#)\n");
332 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
336 print_scattering_function (FILE *file
, poly_bb_p pbb
, int verbosity
)
338 if (!PBB_TRANSFORMED (pbb
))
341 if (PBB_TRANSFORMED_SCATTERING (pbb
)
342 || PBB_ORIGINAL_SCATTERING (pbb
))
345 fprintf (file
, "# Scattering function is provided\n");
347 fprintf (file
, "1\n");
352 fprintf (file
, "# Scattering function is not provided\n");
354 fprintf (file
, "0\n");
358 openscop_print_scattering_function_1 (file
, pbb
, verbosity
);
361 fprintf (file
, "# Scattering names are not provided\n");
363 fprintf (file
, "0\n");
367 /* Prints to FILE the iteration domain of PBB, at some VERBOSITY
371 print_iteration_domain (FILE *file
, poly_bb_p pbb
, int verbosity
)
373 print_pbb_domain (file
, pbb
, verbosity
);
376 /* Prints to FILE the scattering functions of every PBB of SCOP. */
379 print_scattering_functions (FILE *file
, scop_p scop
, int verbosity
)
384 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
385 print_scattering_function (file
, pbb
, verbosity
);
388 /* Prints to FILE the iteration domains of every PBB of SCOP, at some
392 print_iteration_domains (FILE *file
, scop_p scop
, int verbosity
)
397 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
398 print_iteration_domain (file
, pbb
, verbosity
);
401 /* Prints to STDERR the scattering function of PBB, at some VERBOSITY
405 debug_scattering_function (poly_bb_p pbb
, int verbosity
)
407 print_scattering_function (stderr
, pbb
, verbosity
);
410 /* Prints to STDERR the iteration domain of PBB, at some VERBOSITY
414 debug_iteration_domain (poly_bb_p pbb
, int verbosity
)
416 print_iteration_domain (stderr
, pbb
, verbosity
);
419 /* Prints to STDERR the scattering functions of every PBB of SCOP, at
420 some VERBOSITY level. */
423 debug_scattering_functions (scop_p scop
, int verbosity
)
425 print_scattering_functions (stderr
, scop
, verbosity
);
428 /* Prints to STDERR the iteration domains of every PBB of SCOP, at
429 some VERBOSITY level. */
432 debug_iteration_domains (scop_p scop
, int verbosity
)
434 print_iteration_domains (stderr
, scop
, verbosity
);
438 /* Apply graphite transformations to all the basic blocks of SCOP. */
441 apply_poly_transforms (scop_p scop
)
443 bool transform_done
= false;
445 /* Generate code even if we did not apply any real transformation.
446 This also allows to check the performance for the identity
447 transformation: GIMPLE -> GRAPHITE -> GIMPLE
448 Keep in mind that CLooG optimizes in control, so the loop structure
449 may change, even if we only use -fgraphite-identity. */
450 if (flag_graphite_identity
)
451 transform_done
= true;
453 if (flag_loop_parallelize_all
)
454 transform_done
= true;
457 transform_done
|= scop_do_block (scop
);
460 if (flag_loop_strip_mine
)
461 transform_done
|= scop_do_strip_mine (scop
);
463 if (flag_loop_interchange
)
464 transform_done
|= scop_do_interchange (scop
);
467 return transform_done
;
470 /* Returns true when it PDR1 is a duplicate of PDR2: same PBB, and
471 their ACCESSES, TYPE, and NB_SUBSCRIPTS are the same. */
474 can_collapse_pdrs (poly_dr_p pdr1
, poly_dr_p pdr2
)
477 ppl_Pointset_Powerset_C_Polyhedron_t af1
, af2
, diff
;
479 if (PDR_PBB (pdr1
) != PDR_PBB (pdr2
)
480 || PDR_NB_SUBSCRIPTS (pdr1
) != PDR_NB_SUBSCRIPTS (pdr2
)
481 || PDR_TYPE (pdr1
) != PDR_TYPE (pdr2
))
484 af1
= PDR_ACCESSES (pdr1
);
485 af2
= PDR_ACCESSES (pdr2
);
486 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
488 ppl_Pointset_Powerset_C_Polyhedron_difference_assign (diff
, af2
);
490 res
= ppl_Pointset_Powerset_C_Polyhedron_is_empty (diff
);
491 ppl_delete_Pointset_Powerset_C_Polyhedron (diff
);
495 /* Removes duplicated data references in PBB. */
498 pbb_remove_duplicate_pdrs (poly_bb_p pbb
)
501 poly_dr_p pdr1
, pdr2
;
502 unsigned n
= VEC_length (poly_dr_p
, PBB_DRS (pbb
));
503 VEC (poly_dr_p
, heap
) *collapsed
= VEC_alloc (poly_dr_p
, heap
, n
);
505 FOR_EACH_VEC_ELT (poly_dr_p
, PBB_DRS (pbb
), i
, pdr1
)
506 FOR_EACH_VEC_ELT (poly_dr_p
, collapsed
, j
, pdr2
)
507 if (!can_collapse_pdrs (pdr1
, pdr2
))
508 VEC_quick_push (poly_dr_p
, collapsed
, pdr1
);
510 VEC_free (poly_dr_p
, heap
, collapsed
);
511 PBB_PDR_DUPLICATES_REMOVED (pbb
) = true;
514 /* Create a new polyhedral data reference and add it to PBB. It is
515 defined by its ACCESSES, its TYPE, and the number of subscripts
519 new_poly_dr (poly_bb_p pbb
, int dr_base_object_set
,
520 ppl_Pointset_Powerset_C_Polyhedron_t accesses
,
521 enum poly_dr_type type
, void *cdr
, graphite_dim_t nb_subscripts
)
524 poly_dr_p pdr
= XNEW (struct poly_dr
);
527 PDR_BASE_OBJECT_SET (pdr
) = dr_base_object_set
;
528 PDR_NB_REFS (pdr
) = 1;
530 PDR_ACCESSES (pdr
) = accesses
;
531 PDR_TYPE (pdr
) = type
;
533 PDR_NB_SUBSCRIPTS (pdr
) = nb_subscripts
;
534 VEC_safe_push (poly_dr_p
, heap
, PBB_DRS (pbb
), pdr
);
537 /* Free polyhedral data reference PDR. */
540 free_poly_dr (poly_dr_p pdr
)
542 ppl_delete_Pointset_Powerset_C_Polyhedron (PDR_ACCESSES (pdr
));
546 /* Create a new polyhedral black box. */
549 new_poly_bb (scop_p scop
, void *black_box
, bool reduction
)
551 poly_bb_p pbb
= XNEW (struct poly_bb
);
553 PBB_DOMAIN (pbb
) = NULL
;
554 PBB_SCOP (pbb
) = scop
;
555 pbb_set_black_box (pbb
, black_box
);
556 PBB_TRANSFORMED (pbb
) = NULL
;
557 PBB_SAVED (pbb
) = NULL
;
558 PBB_ORIGINAL (pbb
) = NULL
;
559 PBB_DRS (pbb
) = VEC_alloc (poly_dr_p
, heap
, 3);
560 PBB_IS_REDUCTION (pbb
) = reduction
;
561 PBB_PDR_DUPLICATES_REMOVED (pbb
) = false;
562 VEC_safe_push (poly_bb_p
, heap
, SCOP_BBS (scop
), pbb
);
565 /* Free polyhedral black box. */
568 free_poly_bb (poly_bb_p pbb
)
573 ppl_delete_Pointset_Powerset_C_Polyhedron (PBB_DOMAIN (pbb
));
575 if (PBB_TRANSFORMED (pbb
))
576 poly_scattering_free (PBB_TRANSFORMED (pbb
));
579 poly_scattering_free (PBB_SAVED (pbb
));
581 if (PBB_ORIGINAL (pbb
))
582 poly_scattering_free (PBB_ORIGINAL (pbb
));
585 FOR_EACH_VEC_ELT (poly_dr_p
, PBB_DRS (pbb
), i
, pdr
)
588 VEC_free (poly_dr_p
, heap
, PBB_DRS (pbb
));
593 print_pdr_access_layout (FILE *file
, poly_bb_p pbb
, poly_dr_p pdr
)
597 fprintf (file
, "# eq");
599 fprintf (file
, " alias");
601 for (i
= 0; i
< PDR_NB_SUBSCRIPTS (pdr
); i
++)
602 fprintf (file
, " sub%d", (int) i
);
604 for (i
= 0; i
< pbb_dim_iter_domain (pbb
); i
++)
605 fprintf (file
, " i%d", (int) i
);
607 for (i
= 0; i
< pbb_nb_params (pbb
); i
++)
608 fprintf (file
, " p%d", (int) i
);
610 fprintf (file
, " cst\n");
613 /* Prints to FILE the polyhedral data reference PDR, at some VERBOSITY
617 print_pdr (FILE *file
, poly_dr_p pdr
, int verbosity
)
623 fprintf (file
, "# pdr_%d (", PDR_ID (pdr
));
625 switch (PDR_TYPE (pdr
))
628 fprintf (file
, "read \n");
632 fprintf (file
, "write \n");
636 fprintf (file
, "may_write \n");
643 dump_data_reference (file
, (data_reference_p
) PDR_CDR (pdr
));
648 fprintf (file
, "# data accesses (\n");
649 print_pdr_access_layout (file
, PDR_PBB (pdr
), pdr
);
652 alias_set_dim
= pdr_alias_set_dim (pdr
) + 1;
654 openscop_print_pdr_powerset (file
,
656 PDR_NB_SUBSCRIPTS (pdr
),
658 pbb_nb_params (PDR_PBB (pdr
)));
661 fprintf (file
, "#)\n");
664 fprintf (file
, "#)\n");
667 /* Prints to STDERR the polyhedral data reference PDR, at some
671 debug_pdr (poly_dr_p pdr
, int verbosity
)
673 print_pdr (stderr
, pdr
, verbosity
);
676 /* Creates a new SCOP containing REGION. */
679 new_scop (void *region
)
681 scop_p scop
= XNEW (struct scop
);
683 SCOP_CONTEXT (scop
) = NULL
;
684 scop_set_region (scop
, region
);
685 SCOP_BBS (scop
) = VEC_alloc (poly_bb_p
, heap
, 3);
686 SCOP_ORIGINAL_PDDRS (scop
) = htab_create (10, hash_poly_ddr_p
,
687 eq_poly_ddr_p
, free_poly_ddr
);
688 SCOP_ORIGINAL_SCHEDULE (scop
) = NULL
;
689 SCOP_TRANSFORMED_SCHEDULE (scop
) = NULL
;
690 SCOP_SAVED_SCHEDULE (scop
) = NULL
;
691 POLY_SCOP_P (scop
) = false;
699 free_scop (scop_p scop
)
704 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
707 VEC_free (poly_bb_p
, heap
, SCOP_BBS (scop
));
709 if (SCOP_CONTEXT (scop
))
710 ppl_delete_Pointset_Powerset_C_Polyhedron (SCOP_CONTEXT (scop
));
712 htab_delete (SCOP_ORIGINAL_PDDRS (scop
));
713 free_lst (SCOP_ORIGINAL_SCHEDULE (scop
));
714 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop
));
715 free_lst (SCOP_SAVED_SCHEDULE (scop
));
719 /* Print to FILE the domain of PBB in OpenScop format, at some VERBOSITY
723 openscop_print_pbb_domain (FILE *file
, poly_bb_p pbb
, int verbosity
)
726 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
728 if (!PBB_DOMAIN (pbb
))
733 fprintf (file
, "\n# Iteration domain of bb_%d (\n", GBB_BB (gbb
)->index
);
734 fprintf (file
, "# eq");
736 for (i
= 0; i
< pbb_dim_iter_domain (pbb
); i
++)
737 fprintf (file
, " i%d", (int) i
);
739 for (i
= 0; i
< pbb_nb_params (pbb
); i
++)
740 fprintf (file
, " p%d", (int) i
);
742 fprintf (file
, " cst\n");
745 if (PBB_DOMAIN (pbb
))
746 openscop_print_powerset_matrix (file
, PBB_DOMAIN (pbb
),
747 pbb_dim_iter_domain (pbb
),
750 pbb_nb_params (pbb
));
752 fprintf (file
, "0\n");
755 fprintf (file
, "#)\n");
758 /* Print to FILE the domain of PBB, at some VERBOSITY level. */
761 print_pbb_domain (FILE *file
, poly_bb_p pbb
, int verbosity
)
764 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
766 if (!PBB_DOMAIN (pbb
))
771 fprintf (file
, "# Iteration domain of bb_%d (\n", GBB_BB (gbb
)->index
);
772 fprintf (file
, "# eq");
774 for (i
= 0; i
< pbb_dim_iter_domain (pbb
); i
++)
775 fprintf (file
, " i%d", (int) i
);
777 for (i
= 0; i
< pbb_nb_params (pbb
); i
++)
778 fprintf (file
, " p%d", (int) i
);
780 fprintf (file
, " cst\n");
783 if (PBB_DOMAIN (pbb
))
784 ppl_print_powerset_matrix (file
, PBB_DOMAIN (pbb
));
786 fprintf (file
, "0\n");
789 fprintf (file
, "#)\n");
792 /* Dump the cases of a graphite basic block GBB on FILE. */
795 dump_gbb_cases (FILE *file
, gimple_bb_p gbb
)
799 VEC (gimple
, heap
) *cases
;
804 cases
= GBB_CONDITION_CASES (gbb
);
805 if (VEC_empty (gimple
, cases
))
808 fprintf (file
, "# cases bb_%d (\n", GBB_BB (gbb
)->index
);
810 FOR_EACH_VEC_ELT (gimple
, cases
, i
, stmt
)
812 fprintf (file
, "# ");
813 print_gimple_stmt (file
, stmt
, 0, 0);
816 fprintf (file
, "#)\n");
819 /* Dump conditions of a graphite basic block GBB on FILE. */
822 dump_gbb_conditions (FILE *file
, gimple_bb_p gbb
)
826 VEC (gimple
, heap
) *conditions
;
831 conditions
= GBB_CONDITIONS (gbb
);
832 if (VEC_empty (gimple
, conditions
))
835 fprintf (file
, "# conditions bb_%d (\n", GBB_BB (gbb
)->index
);
837 FOR_EACH_VEC_ELT (gimple
, conditions
, i
, stmt
)
839 fprintf (file
, "# ");
840 print_gimple_stmt (file
, stmt
, 0, 0);
843 fprintf (file
, "#)\n");
846 /* Print to FILE all the data references of PBB, at some VERBOSITY
850 print_pdrs (FILE *file
, poly_bb_p pbb
, int verbosity
)
857 if (VEC_length (poly_dr_p
, PBB_DRS (pbb
)) == 0)
860 fprintf (file
, "# Access informations are not provided\n");\
861 fprintf (file
, "0\n");
866 fprintf (file
, "# Data references (\n");
869 fprintf (file
, "# Access informations are provided\n");
870 fprintf (file
, "1\n");
872 FOR_EACH_VEC_ELT (poly_dr_p
, PBB_DRS (pbb
), i
, pdr
)
873 if (PDR_TYPE (pdr
) == PDR_READ
)
879 fprintf (file
, "# Read data references (\n");
882 fprintf (file
, "# Read access informations\n");
883 fprintf (file
, "%d\n", nb_reads
);
885 FOR_EACH_VEC_ELT (poly_dr_p
, PBB_DRS (pbb
), i
, pdr
)
886 if (PDR_TYPE (pdr
) == PDR_READ
)
887 print_pdr (file
, pdr
, verbosity
);
890 fprintf (file
, "#)\n");
893 fprintf (file
, "# Write data references (\n");
896 fprintf (file
, "# Write access informations\n");
897 fprintf (file
, "%d\n", nb_writes
);
899 FOR_EACH_VEC_ELT (poly_dr_p
, PBB_DRS (pbb
), i
, pdr
)
900 if (PDR_TYPE (pdr
) != PDR_READ
)
901 print_pdr (file
, pdr
, verbosity
);
904 fprintf (file
, "#)\n");
907 fprintf (file
, "#)\n");
910 /* Print to STDERR all the data references of PBB. */
913 debug_pdrs (poly_bb_p pbb
, int verbosity
)
915 print_pdrs (stderr
, pbb
, verbosity
);
918 /* Print to FILE the body of PBB, at some VERBOSITY level.
919 If statement_body_provided is false statement body is not printed. */
922 print_pbb_body (FILE *file
, poly_bb_p pbb
, int verbosity
,
923 bool statement_body_provided
)
926 fprintf (file
, "# Body (\n");
928 if (!statement_body_provided
)
931 fprintf (file
, "# Statement body is not provided\n");
933 fprintf (file
, "0\n");
938 fprintf (file
, "# Statement body is provided\n");
939 fprintf (file
, "1\n");
942 fprintf (file
, "# Original iterator names\n# Iterator names are not provided yet.\n");
945 fprintf (file
, "# Statement body\n");
947 fprintf (file
, "{\n");
948 dump_bb (pbb_bb (pbb
), file
, 0);
949 fprintf (file
, "}\n");
952 fprintf (file
, "#)\n");
955 /* Print to FILE the domain and scattering function of PBB, at some
959 print_pbb (FILE *file
, poly_bb_p pbb
, int verbosity
)
963 fprintf (file
, "# pbb_%d (\n", pbb_index (pbb
));
964 dump_gbb_conditions (file
, PBB_BLACK_BOX (pbb
));
965 dump_gbb_cases (file
, PBB_BLACK_BOX (pbb
));
968 openscop_print_pbb_domain (file
, pbb
, verbosity
);
969 print_scattering_function (file
, pbb
, verbosity
);
970 print_pdrs (file
, pbb
, verbosity
);
971 print_pbb_body (file
, pbb
, verbosity
, false);
974 fprintf (file
, "#)\n");
977 /* Print to FILE the parameters of SCOP, at some VERBOSITY level. */
980 print_scop_params (FILE *file
, scop_p scop
, int verbosity
)
986 fprintf (file
, "# parameters (\n");
988 if (VEC_length (tree
, SESE_PARAMS (SCOP_REGION (scop
))))
991 fprintf (file
, "# Parameter names are provided\n");
993 fprintf (file
, "1\n");
996 fprintf (file
, "# Parameter names\n");
1001 fprintf (file
, "# Parameter names are not provided\n");
1002 fprintf (file
, "0\n");
1005 FOR_EACH_VEC_ELT (tree
, SESE_PARAMS (SCOP_REGION (scop
)), i
, t
)
1007 print_generic_expr (file
, t
, 0);
1008 fprintf (file
, " ");
1011 fprintf (file
, "\n");
1014 fprintf (file
, "#)\n");
1017 /* Print to FILE the context of SCoP in OpenScop format, at some VERBOSITY
1021 openscop_print_scop_context (FILE *file
, scop_p scop
, int verbosity
)
1027 fprintf (file
, "# Context (\n");
1028 fprintf (file
, "# eq");
1030 for (i
= 0; i
< scop_nb_params (scop
); i
++)
1031 fprintf (file
, " p%d", (int) i
);
1033 fprintf (file
, " cst\n");
1036 if (SCOP_CONTEXT (scop
))
1037 openscop_print_powerset_matrix (file
, SCOP_CONTEXT (scop
), 0, 0, 0,
1038 scop_nb_params (scop
));
1040 fprintf (file
, "0 %d 0 0 0 %d\n", (int) scop_nb_params (scop
) + 2,
1041 (int) scop_nb_params (scop
));
1044 fprintf (file
, "# )\n");
1047 /* Print to FILE the context of SCoP, at some VERBOSITY level. */
1050 print_scop_context (FILE *file
, scop_p scop
, int verbosity
)
1056 fprintf (file
, "# Context (\n");
1057 fprintf (file
, "# eq");
1059 for (i
= 0; i
< scop_nb_params (scop
); i
++)
1060 fprintf (file
, " p%d", (int) i
);
1062 fprintf (file
, " cst\n");
1065 if (SCOP_CONTEXT (scop
))
1066 ppl_print_powerset_matrix (file
, SCOP_CONTEXT (scop
));
1068 fprintf (file
, "0 %d\n", (int) scop_nb_params (scop
) + 2);
1071 fprintf (file
, "# )\n");
1074 /* Print to FILE the SCOP header: context, parameters, and statements
1078 print_scop_header (FILE *file
, scop_p scop
, int verbosity
)
1080 fprintf (file
, "SCoP 1\n#(\n");
1081 fprintf (file
, "# Language\nGimple\n");
1082 openscop_print_scop_context (file
, scop
, verbosity
);
1083 print_scop_params (file
, scop
, verbosity
);
1086 fprintf (file
, "# Number of statements\n");
1088 fprintf (file
, "%d\n",VEC_length (poly_bb_p
, SCOP_BBS (scop
)));
1091 /* Print to FILE the SCOP, at some VERBOSITY level. */
1094 print_scop (FILE *file
, scop_p scop
, int verbosity
)
1099 print_scop_header (file
, scop
, verbosity
);
1101 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1102 print_pbb (file
, pbb
, verbosity
);
1106 fprintf (file
, "# original_lst (\n");
1107 print_lst (file
, SCOP_ORIGINAL_SCHEDULE (scop
), 0);
1108 fprintf (file
, "\n#)\n");
1110 fprintf (file
, "# transformed_lst (\n");
1111 print_lst (file
, SCOP_TRANSFORMED_SCHEDULE (scop
), 0);
1112 fprintf (file
, "\n#)\n");
1115 fprintf (file
, "#)\n");
1118 /* Print to FILE the input file that CLooG would expect as input, at
1119 some VERBOSITY level. */
1122 print_cloog (FILE *file
, scop_p scop
, int verbosity
)
1127 fprintf (file
, "# SCoP (generated by GCC/Graphite\n");
1129 fprintf (file
, "# CLooG output language\n");
1130 fprintf (file
, "c\n");
1132 print_scop_context (file
, scop
, verbosity
);
1133 print_scop_params (file
, scop
, verbosity
);
1136 fprintf (file
, "# Number of statements\n");
1138 fprintf (file
, "%d\n", VEC_length (poly_bb_p
, SCOP_BBS (scop
)));
1140 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1143 fprintf (file
, "# pbb_%d (\n", pbb_index (pbb
));
1145 print_pbb_domain (file
, pbb
, verbosity
);
1146 fprintf (file
, "0 0 0");
1149 fprintf (file
, "# For future CLooG options.\n");
1151 fprintf (file
, "\n");
1154 fprintf (file
, "#)\n");
1157 fprintf (file
, "0");
1159 fprintf (file
, "# Don't set the iterator names.\n");
1161 fprintf (file
, "\n");
1164 fprintf (file
, "# Number of scattering functions\n");
1166 fprintf (file
, "%d\n", VEC_length (poly_bb_p
, SCOP_BBS (scop
)));
1167 unify_scattering_dimensions (scop
);
1169 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1171 if (!PBB_TRANSFORMED (pbb
)
1172 || !(PBB_TRANSFORMED_SCATTERING (pbb
)
1173 || PBB_ORIGINAL_SCATTERING (pbb
)))
1177 fprintf (file
, "# pbb_%d (\n", pbb_index (pbb
));
1179 print_scattering_function_1 (file
, pbb
, verbosity
);
1182 fprintf (file
, "#)\n");
1185 fprintf (file
, "0");
1187 fprintf (file
, "# Don't set the scattering dimension names.\n");
1189 fprintf (file
, "\n");
1191 fprintf (file
, "#)\n");
1194 /* Print to STDERR the domain of PBB, at some VERBOSITY level. */
1197 debug_pbb_domain (poly_bb_p pbb
, int verbosity
)
1199 print_pbb_domain (stderr
, pbb
, verbosity
);
1202 /* Print to FILE the domain and scattering function of PBB, at some
1206 debug_pbb (poly_bb_p pbb
, int verbosity
)
1208 print_pbb (stderr
, pbb
, verbosity
);
1211 /* Print to STDERR the context of SCOP, at some VERBOSITY level. */
1214 debug_scop_context (scop_p scop
, int verbosity
)
1216 print_scop_context (stderr
, scop
, verbosity
);
1219 /* Print to STDERR the SCOP, at some VERBOSITY level. */
1222 debug_scop (scop_p scop
, int verbosity
)
1224 print_scop (stderr
, scop
, verbosity
);
1227 /* Print to STDERR the SCOP under CLooG format, at some VERBOSITY
1231 debug_cloog (scop_p scop
, int verbosity
)
1233 print_cloog (stderr
, scop
, verbosity
);
1236 /* Print to STDERR the parameters of SCOP, at some VERBOSITY
1240 debug_scop_params (scop_p scop
, int verbosity
)
1242 print_scop_params (stderr
, scop
, verbosity
);
1246 /* The dimension in the transformed scattering polyhedron of PBB
1247 containing the scattering iterator for the loop at depth LOOP_DEPTH. */
1250 psct_scattering_dim_for_loop_depth (poly_bb_p pbb
, graphite_dim_t loop_depth
)
1252 ppl_const_Constraint_System_t pcs
;
1253 ppl_Constraint_System_const_iterator_t cit
, cend
;
1254 ppl_const_Constraint_t cstr
;
1255 ppl_Polyhedron_t ph
= PBB_TRANSFORMED_SCATTERING (pbb
);
1256 ppl_dimension_type iter
= psct_iterator_dim (pbb
, loop_depth
);
1257 ppl_Linear_Expression_t expr
;
1258 ppl_Coefficient_t coef
;
1263 ppl_new_Coefficient (&coef
);
1264 ppl_Polyhedron_get_constraints (ph
, &pcs
);
1265 ppl_new_Constraint_System_const_iterator (&cit
);
1266 ppl_new_Constraint_System_const_iterator (&cend
);
1268 for (ppl_Constraint_System_begin (pcs
, cit
),
1269 ppl_Constraint_System_end (pcs
, cend
);
1270 !ppl_Constraint_System_const_iterator_equal_test (cit
, cend
);
1271 ppl_Constraint_System_const_iterator_increment (cit
))
1273 ppl_Constraint_System_const_iterator_dereference (cit
, &cstr
);
1274 ppl_new_Linear_Expression_from_Constraint (&expr
, cstr
);
1275 ppl_Linear_Expression_coefficient (expr
, iter
, coef
);
1276 ppl_Coefficient_to_mpz_t (coef
, val
);
1278 if (mpz_sgn (val
) == 0)
1280 ppl_delete_Linear_Expression (expr
);
1284 for (i
= 0; i
< pbb_nb_scattering_transform (pbb
); i
++)
1286 ppl_dimension_type scatter
= psct_scattering_dim (pbb
, i
);
1288 ppl_Linear_Expression_coefficient (expr
, scatter
, coef
);
1289 ppl_Coefficient_to_mpz_t (coef
, val
);
1291 if (mpz_sgn (val
) != 0)
1294 ppl_delete_Linear_Expression (expr
);
1295 ppl_delete_Coefficient (coef
);
1296 ppl_delete_Constraint_System_const_iterator (cit
);
1297 ppl_delete_Constraint_System_const_iterator (cend
);
1307 /* Returns the number of iterations NITER of the loop around PBB at
1308 depth LOOP_DEPTH. */
1311 pbb_number_of_iterations (poly_bb_p pbb
,
1312 graphite_dim_t loop_depth
,
1315 ppl_Linear_Expression_t le
;
1316 ppl_dimension_type dim
;
1318 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb
), &dim
);
1319 ppl_new_Linear_Expression_with_dimension (&le
, dim
);
1320 ppl_set_coef (le
, pbb_iterator_dim (pbb
, loop_depth
), 1);
1321 mpz_set_si (niter
, -1);
1322 ppl_max_for_le_pointset (PBB_DOMAIN (pbb
), le
, niter
);
1323 ppl_delete_Linear_Expression (le
);
1326 /* Returns the number of iterations NITER of the loop around PBB at
1327 time(scattering) dimension TIME_DEPTH. */
1330 pbb_number_of_iterations_at_time (poly_bb_p pbb
,
1331 graphite_dim_t time_depth
,
1334 ppl_Pointset_Powerset_C_Polyhedron_t ext_domain
, sctr
;
1335 ppl_Linear_Expression_t le
;
1336 ppl_dimension_type dim
;
1338 /* Takes together domain and scattering polyhedrons, and composes
1339 them into the bigger polyhedron that has the following format:
1341 t0..t_{n-1} | l0..l_{nlcl-1} | i0..i_{niter-1} | g0..g_{nparm-1}
1344 | t0..t_{n-1} are time dimensions (scattering dimensions)
1345 | l0..l_{nclc-1} are local variables in scattering function
1346 | i0..i_{niter-1} are original iteration variables
1347 | g0..g_{nparam-1} are global parameters. */
1349 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&sctr
,
1350 PBB_TRANSFORMED_SCATTERING (pbb
));
1352 /* Extend the iteration domain with the scattering dimensions:
1353 0..0 | 0..0 | i0..i_{niter-1} | g0..g_{nparm-1}. */
1354 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1355 (&ext_domain
, PBB_DOMAIN (pbb
));
1356 ppl_insert_dimensions_pointset (ext_domain
, 0,
1357 pbb_nb_scattering_transform (pbb
)
1358 + pbb_nb_local_vars (pbb
));
1360 /* Add to sctr the extended domain. */
1361 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (sctr
, ext_domain
);
1363 /* Extract the number of iterations. */
1364 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (sctr
, &dim
);
1365 ppl_new_Linear_Expression_with_dimension (&le
, dim
);
1366 ppl_set_coef (le
, time_depth
, 1);
1367 mpz_set_si (niter
, -1);
1368 ppl_max_for_le_pointset (sctr
, le
, niter
);
1370 ppl_delete_Linear_Expression (le
);
1371 ppl_delete_Pointset_Powerset_C_Polyhedron (sctr
);
1372 ppl_delete_Pointset_Powerset_C_Polyhedron (ext_domain
);
1375 /* Translates LOOP to LST. */
1378 loop_to_lst (loop_p loop
, VEC (poly_bb_p
, heap
) *bbs
, int *i
)
1381 VEC (lst_p
, heap
) *seq
= VEC_alloc (lst_p
, heap
, 5);
1383 for (; VEC_iterate (poly_bb_p
, bbs
, *i
, pbb
); (*i
)++)
1386 basic_block bb
= GBB_BB (PBB_BLACK_BOX (pbb
));
1388 if (bb
->loop_father
== loop
)
1389 stmt
= new_lst_stmt (pbb
);
1390 else if (flow_bb_inside_loop_p (loop
, bb
))
1392 loop_p next
= loop
->inner
;
1394 while (next
&& !flow_bb_inside_loop_p (next
, bb
))
1397 stmt
= loop_to_lst (next
, bbs
, i
);
1402 return new_lst_loop (seq
);
1405 VEC_safe_push (lst_p
, heap
, seq
, stmt
);
1408 return new_lst_loop (seq
);
1411 /* Reads the original scattering of the SCOP and returns an LST
1415 scop_to_lst (scop_p scop
)
1418 int i
, n
= VEC_length (poly_bb_p
, SCOP_BBS (scop
));
1419 VEC (lst_p
, heap
) *seq
= VEC_alloc (lst_p
, heap
, 5);
1420 sese region
= SCOP_REGION (scop
);
1422 for (i
= 0; i
< n
; i
++)
1424 poly_bb_p pbb
= VEC_index (poly_bb_p
, SCOP_BBS (scop
), i
);
1425 loop_p loop
= outermost_loop_in_sese (region
, GBB_BB (PBB_BLACK_BOX (pbb
)));
1427 if (loop_in_sese_p (loop
, region
))
1428 res
= loop_to_lst (loop
, SCOP_BBS (scop
), &i
);
1430 res
= new_lst_stmt (pbb
);
1432 VEC_safe_push (lst_p
, heap
, seq
, res
);
1435 res
= new_lst_loop (seq
);
1436 SCOP_ORIGINAL_SCHEDULE (scop
) = res
;
1437 SCOP_TRANSFORMED_SCHEDULE (scop
) = copy_lst (res
);
1440 /* Print to FILE on a new line COLUMN white spaces. */
1443 lst_indent_to (FILE *file
, int column
)
1448 fprintf (file
, "\n#");
1450 for (i
= 0; i
< column
; i
++)
1451 fprintf (file
, " ");
1454 /* Print LST to FILE with INDENT spaces of indentation. */
1457 print_lst (FILE *file
, lst_p lst
, int indent
)
1462 lst_indent_to (file
, indent
);
1464 if (LST_LOOP_P (lst
))
1469 if (LST_LOOP_FATHER (lst
))
1470 fprintf (file
, "%d (loop", lst_dewey_number (lst
));
1472 fprintf (file
, "#(root");
1474 FOR_EACH_VEC_ELT (lst_p
, LST_SEQ (lst
), i
, l
)
1475 print_lst (file
, l
, indent
+ 2);
1477 fprintf (file
, ")");
1480 fprintf (file
, "%d stmt_%d", lst_dewey_number (lst
), pbb_index (LST_PBB (lst
)));
1483 /* Print LST to STDERR. */
1486 debug_lst (lst_p lst
)
1488 print_lst (stderr
, lst
, 0);
1491 /* Pretty print to FILE the loop statement tree LST in DOT format. */
1494 dot_lst_1 (FILE *file
, lst_p lst
)
1499 if (LST_LOOP_P (lst
))
1504 if (!LST_LOOP_FATHER (lst
))
1505 fprintf (file
, "L -> L_%d_%d\n",
1507 lst_dewey_number (lst
));
1509 fprintf (file
, "L_%d_%d -> L_%d_%d\n",
1510 lst_depth (LST_LOOP_FATHER (lst
)),
1511 lst_dewey_number (LST_LOOP_FATHER (lst
)),
1513 lst_dewey_number (lst
));
1515 FOR_EACH_VEC_ELT (lst_p
, LST_SEQ (lst
), i
, l
)
1516 dot_lst_1 (file
, l
);
1520 fprintf (file
, "L_%d_%d -> S_%d\n",
1521 lst_depth (LST_LOOP_FATHER (lst
)),
1522 lst_dewey_number (LST_LOOP_FATHER (lst
)),
1523 pbb_index (LST_PBB (lst
)));
1527 /* Display the LST using dotty. */
1532 /* When debugging, enable the following code. This cannot be used
1533 in production compilers because it calls "system". */
1536 FILE *stream
= fopen ("/tmp/lst.dot", "w");
1537 gcc_assert (stream
);
1539 fputs ("digraph all {\n", stream
);
1540 dot_lst_1 (stream
, lst
);
1541 fputs ("}\n\n", stream
);
1544 x
= system ("dotty /tmp/lst.dot &");
1546 fputs ("digraph all {\n", stderr
);
1547 dot_lst_1 (stderr
, lst
);
1548 fputs ("}\n\n", stderr
);