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/>. */
22 #ifndef GCC_GRAPHITE_POLY_H
23 #define GCC_GRAPHITE_POLY_H
25 typedef struct poly_dr *poly_dr_p;
27 DEF_VEC_ALLOC_P (poly_dr_p, heap);
29 typedef struct poly_bb *poly_bb_p;
31 DEF_VEC_ALLOC_P (poly_bb_p, heap);
33 typedef struct scop *scop_p;
35 DEF_VEC_ALLOC_P (scop_p, heap);
37 typedef ppl_dimension_type graphite_dim_t;
39 static inline graphite_dim_t pbb_dim_iter_domain (const struct poly_bb *);
40 static inline graphite_dim_t pbb_nb_params (const struct poly_bb *);
41 static inline graphite_dim_t scop_nb_params (scop_p);
43 /* A data reference can write or read some memory or we
44 just know it may write some memory. */
48 /* PDR_MAY_READs are represented using PDR_READS. This does not limit the
56 /* An identifier for this PDR. */
59 /* A pointer to compiler's data reference description. */
62 /* A pointer to the PBB that contains this data reference. */
65 enum poly_dr_type type;
67 /* The access polyhedron contains the polyhedral space this data
68 reference will access.
70 The polyhedron contains these dimensions:
73 Every memory access is classified in at least one alias set.
75 - The subscripts (s_0, ..., s_n):
76 The memory is accessed using zero or more subscript dimensions.
78 - The iteration domain (variables and parameters)
80 Do not hardcode the dimensions. Use the following accessor functions:
94 | if (unknown_function ())
101 The data access A[i][j+k] in alias set "5" is described like this:
106 | 0 -1 -1 0 0 1 0 = 0
107 | 0 0 0 0 1 0 0 >= 0 # The last four lines describe the
108 | 0 0 0 0 0 1 0 >= 0 # array size.
109 | 0 0 0 0 -1 0 1335 >= 0
110 | 0 0 0 0 0 -1 123 >= 0
112 The pointer "*p" in alias set "5" and "7" is described as a union of
126 "*p" accesses all of the object allocated with 'malloc'.
128 The scalar data access "m" is represented as an array with zero subscript
133 ppl_Pointset_Powerset_C_Polyhedron_t accesses;
135 /* The number of subscripts. */
136 graphite_dim_t nb_subscripts;
139 #define PDR_ID(PDR) (PDR->id)
140 #define PDR_CDR(PDR) (PDR->compiler_dr)
141 #define PDR_PBB(PDR) (PDR->pbb)
142 #define PDR_TYPE(PDR) (PDR->type)
143 #define PDR_ACCESSES(PDR) (PDR->accesses)
144 #define PDR_NB_SUBSCRIPTS(PDR) (PDR->nb_subscripts)
146 void new_poly_dr (poly_bb_p, ppl_Pointset_Powerset_C_Polyhedron_t,
147 enum poly_dr_type, void *, int);
148 void free_poly_dr (poly_dr_p);
149 void debug_pdr (poly_dr_p);
150 void print_pdr (FILE *, poly_dr_p);
151 static inline scop_p pdr_scop (poly_dr_p pdr);
153 /* The dimension of the PDR_ACCESSES polyhedron of PDR. */
155 static inline ppl_dimension_type
156 pdr_dim (poly_dr_p pdr)
158 ppl_dimension_type dim;
159 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PDR_ACCESSES (pdr),
164 /* The dimension of the iteration domain of the scop of PDR. */
166 static inline ppl_dimension_type
167 pdr_dim_iter_domain (poly_dr_p pdr)
169 return pbb_dim_iter_domain (PDR_PBB (pdr));
172 /* The number of parameters of the scop of PDR. */
174 static inline ppl_dimension_type
175 pdr_nb_params (poly_dr_p pdr)
177 return scop_nb_params (pdr_scop (pdr));
180 /* The dimension of the alias set in PDR. */
182 static inline ppl_dimension_type
183 pdr_alias_set_dim (poly_dr_p pdr)
185 poly_bb_p pbb = PDR_PBB (pdr);
187 return pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
190 /* The dimension in PDR containing subscript S. */
192 static inline ppl_dimension_type
193 pdr_subscript_dim (poly_dr_p pdr, graphite_dim_t s)
195 poly_bb_p pbb = PDR_PBB (pdr);
197 return pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb) + 1 + s;
200 /* The dimension in PDR containing the loop iterator ITER. */
202 static inline ppl_dimension_type
203 pdr_iterator_dim (poly_dr_p pdr ATTRIBUTE_UNUSED, graphite_dim_t iter)
208 /* The dimension in PDR containing parameter PARAM. */
210 static inline ppl_dimension_type
211 pdr_parameter_dim (poly_dr_p pdr, graphite_dim_t param)
213 poly_bb_p pbb = PDR_PBB (pdr);
215 return pbb_dim_iter_domain (pbb) + param;
218 typedef struct poly_scattering *poly_scattering_p;
220 struct poly_scattering
222 /* The scattering function containing the transformations. */
223 ppl_Polyhedron_t scattering;
225 /* The number of local variables. */
226 int nb_local_variables;
228 /* The number of scattering dimensions. */
232 /* POLY_BB represents a blackbox in the polyhedral model. */
240 /* The iteration domain of this bb.
243 for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
244 for (j = 2; j <= 2*i + 5; j++)
245 for (k = 0; k <= 5; k++)
248 Loop iterators: i, j, k
258 The number of variables in the DOMAIN may change and is not
259 related to the number of loops in the original code. */
260 ppl_Pointset_Powerset_C_Polyhedron_t domain;
262 /* The data references we access. */
263 VEC (poly_dr_p, heap) *drs;
265 /* The original scattering. */
266 poly_scattering_p original;
268 /* The transformed scattering. */
269 poly_scattering_p transformed;
271 /* A copy of the transformed scattering. */
272 poly_scattering_p saved;
275 #define PBB_BLACK_BOX(PBB) ((gimple_bb_p) PBB->black_box)
276 #define PBB_SCOP(PBB) (PBB->scop)
277 #define PBB_DOMAIN(PBB) (PBB->domain)
278 #define PBB_DRS(PBB) (PBB->drs)
279 #define PBB_ORIGINAL(PBB) (PBB->original)
280 #define PBB_ORIGINAL_SCATTERING(PBB) (PBB->original->scattering)
281 #define PBB_TRANSFORMED(PBB) (PBB->transformed)
282 #define PBB_TRANSFORMED_SCATTERING(PBB) (PBB->transformed->scattering)
283 #define PBB_SAVED(PBB) (PBB->saved)
284 #define PBB_NB_LOCAL_VARIABLES(PBB) (PBB->transformed->nb_local_variables)
285 #define PBB_NB_SCATTERING_TRANSFORM(PBB) (PBB->transformed->nb_scattering)
287 extern void new_poly_bb (scop_p, void *);
288 extern void free_poly_bb (poly_bb_p);
289 extern void debug_loop_vec (poly_bb_p);
290 extern void schedule_to_scattering (poly_bb_p, int);
291 extern void print_pbb_domain (FILE *, poly_bb_p);
292 extern void print_pbb (FILE *, poly_bb_p);
293 extern void print_scop_context (FILE *, scop_p);
294 extern void print_scop (FILE *, scop_p);
295 extern void debug_pbb_domain (poly_bb_p);
296 extern void debug_pbb (poly_bb_p);
297 extern void print_pdrs (FILE *, poly_bb_p);
298 extern void debug_pdrs (poly_bb_p);
299 extern void debug_scop_context (scop_p);
300 extern void debug_scop (scop_p);
301 extern void print_scop_params (FILE *, scop_p);
302 extern void debug_scop_params (scop_p);
303 extern void print_iteration_domain (FILE *, poly_bb_p);
304 extern void print_iteration_domains (FILE *, scop_p);
305 extern void debug_iteration_domain (poly_bb_p);
306 extern void debug_iteration_domains (scop_p);
307 extern bool scop_do_interchange (scop_p);
308 extern bool scop_do_strip_mine (scop_p);
309 extern void pbb_number_of_iterations (poly_bb_p, graphite_dim_t, Value);
310 extern void pbb_number_of_iterations_at_time (poly_bb_p, graphite_dim_t, Value);
312 /* The index of the PBB. */
315 pbb_index (poly_bb_p pbb)
317 return GBB_BB (PBB_BLACK_BOX (pbb))->index;
320 /* The scop that contains the PDR. */
323 pdr_scop (poly_dr_p pdr)
325 return PBB_SCOP (PDR_PBB (pdr));
328 /* Set black box of PBB to BLACKBOX. */
331 pbb_set_black_box (poly_bb_p pbb, void *black_box)
333 pbb->black_box = black_box;
336 /* The number of loops around PBB: the dimension of the iteration
339 static inline graphite_dim_t
340 pbb_dim_iter_domain (const struct poly_bb *pbb)
342 scop_p scop = PBB_SCOP (pbb);
343 ppl_dimension_type dim;
345 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
346 return dim - scop_nb_params (scop);
349 /* The number of params defined in PBB. */
351 static inline graphite_dim_t
352 pbb_nb_params (const struct poly_bb *pbb)
354 scop_p scop = PBB_SCOP (pbb);
356 return scop_nb_params (scop);
359 /* The number of scattering dimensions in the SCATTERING polyhedron
360 of a PBB for a given SCOP. */
362 static inline graphite_dim_t
363 pbb_nb_scattering_orig (const struct poly_bb *pbb)
365 return 2 * pbb_dim_iter_domain (pbb) + 1;
368 /* The number of scattering dimensions in PBB. */
370 static inline graphite_dim_t
371 pbb_nb_scattering_transform (const struct poly_bb *pbb)
373 return PBB_NB_SCATTERING_TRANSFORM (pbb);
376 /* The number of dynamic scattering dimensions in PBB. */
378 static inline graphite_dim_t
379 pbb_nb_dynamic_scattering_transform (const struct poly_bb *pbb)
381 /* This function requires the 2d + 1 scattering format to be
382 invariant during all transformations. */
383 gcc_assert (PBB_NB_SCATTERING_TRANSFORM (pbb) % 2);
384 return PBB_NB_SCATTERING_TRANSFORM (pbb) / 2;
387 /* Returns the number of local variables used in the transformed
388 scattering polyhedron of PBB. */
390 static inline graphite_dim_t
391 pbb_nb_local_vars (const struct poly_bb *pbb)
393 /* For now we do not have any local variables, as we do not do strip
394 mining for example. */
395 return PBB_NB_LOCAL_VARIABLES (pbb);
398 /* The dimension in the domain of PBB containing the iterator ITER. */
400 static inline ppl_dimension_type
401 pbb_iterator_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t iter)
406 /* The dimension in the domain of PBB containing the iterator ITER. */
408 static inline ppl_dimension_type
409 pbb_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
412 + pbb_dim_iter_domain (pbb);
415 /* The dimension in the original scattering polyhedron of PBB
416 containing the scattering iterator SCATTER. */
418 static inline ppl_dimension_type
419 psco_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
421 gcc_assert (scatter < pbb_nb_scattering_orig (pbb));
425 /* The dimension in the transformed scattering polyhedron of PBB
426 containing the scattering iterator SCATTER. */
428 static inline ppl_dimension_type
429 psct_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
431 gcc_assert (scatter <= pbb_nb_scattering_transform (pbb));
435 ppl_dimension_type psct_scattering_dim_for_loop_depth (poly_bb_p,
438 /* The dimension in the transformed scattering polyhedron of PBB of
439 the local variable LV. */
441 static inline ppl_dimension_type
442 psct_local_var_dim (poly_bb_p pbb, graphite_dim_t lv)
444 gcc_assert (lv <= pbb_nb_local_vars (pbb));
445 return lv + pbb_nb_scattering_transform (pbb);
448 /* The dimension in the original scattering polyhedron of PBB
449 containing the loop iterator ITER. */
451 static inline ppl_dimension_type
452 psco_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
454 gcc_assert (iter < pbb_dim_iter_domain (pbb));
455 return iter + pbb_nb_scattering_orig (pbb);
458 /* The dimension in the transformed scattering polyhedron of PBB
459 containing the loop iterator ITER. */
461 static inline ppl_dimension_type
462 psct_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
464 gcc_assert (iter < pbb_dim_iter_domain (pbb));
466 + pbb_nb_scattering_transform (pbb)
467 + pbb_nb_local_vars (pbb);
470 /* The dimension in the original scattering polyhedron of PBB
471 containing parameter PARAM. */
473 static inline ppl_dimension_type
474 psco_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
476 gcc_assert (param < pbb_nb_params (pbb));
478 + pbb_nb_scattering_orig (pbb)
479 + pbb_dim_iter_domain (pbb);
482 /* The dimension in the transformed scattering polyhedron of PBB
483 containing parameter PARAM. */
485 static inline ppl_dimension_type
486 psct_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
488 gcc_assert (param < pbb_nb_params (pbb));
490 + pbb_nb_scattering_transform (pbb)
491 + pbb_nb_local_vars (pbb)
492 + pbb_dim_iter_domain (pbb);
495 /* The scattering dimension of PBB corresponding to the dynamic level
498 static inline ppl_dimension_type
499 psct_dynamic_dim (poly_bb_p pbb, graphite_dim_t level)
501 graphite_dim_t result;
502 result = 1 + 2 * level;
504 gcc_assert (result < pbb_nb_scattering_transform (pbb));
508 /* Adds to the transformed scattering polyhedron of PBB a new local
509 variable and returns its index. */
511 static inline graphite_dim_t
512 psct_add_local_variable (poly_bb_p pbb)
514 graphite_dim_t nlv = pbb_nb_local_vars (pbb);
515 ppl_dimension_type lv_column = psct_local_var_dim (pbb, nlv);
516 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), lv_column, 1);
517 PBB_NB_LOCAL_VARIABLES (pbb) += 1;
521 /* Adds a dimension to the transformed scattering polyhedron of PBB at
525 psct_add_scattering_dimension (poly_bb_p pbb, ppl_dimension_type index)
527 gcc_assert (index < pbb_nb_scattering_transform (pbb));
529 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), index, 1);
530 PBB_NB_SCATTERING_TRANSFORM (pbb) += 1;
533 /* A SCOP is a Static Control Part of the program, simple enough to be
534 represented in polyhedral form. */
537 /* A SCOP is defined as a SESE region. */
540 /* Number of parameters in SCoP. */
541 graphite_dim_t nb_params;
543 /* All the basic blocks in this scop that contain memory references
544 and that will be represented as statements in the polyhedral
546 VEC (poly_bb_p, heap) *bbs;
548 /* Data dependence graph for this SCoP. */
549 struct graph *dep_graph;
551 /* The context describes known restrictions concerning the parameters
552 and relations in between the parameters.
554 void f (int8_t a, uint_16_t b) {
559 Here we can add these restrictions to the context:
564 ppl_Pointset_Powerset_C_Polyhedron_t context;
566 /* A hashtable of the data dependence relations for the original
568 htab_t original_pddrs;
571 #define SCOP_BBS(S) (S->bbs)
572 #define SCOP_REGION(S) ((sese) S->region)
573 #define SCOP_DEP_GRAPH(S) (S->dep_graph)
574 #define SCOP_CONTEXT(S) (S->context)
575 #define SCOP_ORIGINAL_PDDRS(S) (S->original_pddrs)
577 extern scop_p new_scop (void *);
578 extern void free_scop (scop_p);
579 extern void free_scops (VEC (scop_p, heap) *);
580 extern void print_generated_program (FILE *, scop_p);
581 extern void debug_generated_program (scop_p);
582 extern void print_scattering_function (FILE *, poly_bb_p);
583 extern void print_scattering_functions (FILE *, scop_p);
584 extern void debug_scattering_function (poly_bb_p);
585 extern void debug_scattering_functions (scop_p);
586 extern int scop_max_loop_depth (scop_p);
587 extern int unify_scattering_dimensions (scop_p);
588 extern bool apply_poly_transforms (scop_p);
589 extern bool graphite_legal_transform (scop_p);
591 /* Set the region of SCOP to REGION. */
594 scop_set_region (scop_p scop, void *region)
596 scop->region = region;
599 /* Returns the number of parameters for SCOP. */
601 static inline graphite_dim_t
602 scop_nb_params (scop_p scop)
604 return scop->nb_params;
607 /* Set the number of params of SCOP to NB_PARAMS. */
610 scop_set_nb_params (scop_p scop, graphite_dim_t nb_params)
612 scop->nb_params = nb_params;
615 /* Allocates a new empty poly_scattering structure. */
617 static inline poly_scattering_p
618 poly_scattering_new (void)
620 poly_scattering_p res = XNEW (struct poly_scattering);
622 res->scattering = NULL;
623 res->nb_local_variables = 0;
624 res->nb_scattering = 0;
628 /* Free a poly_scattering structure. */
631 poly_scattering_free (poly_scattering_p s)
633 ppl_delete_Polyhedron (s->scattering);
637 /* Copies S and return a new scattering. */
639 static inline poly_scattering_p
640 poly_scattering_copy (poly_scattering_p s)
642 poly_scattering_p res = poly_scattering_new ();
644 ppl_new_C_Polyhedron_from_C_Polyhedron (&(res->scattering), s->scattering);
645 res->nb_local_variables = s->nb_local_variables;
646 res->nb_scattering = s->nb_scattering;
650 /* Saves the transformed scattering of PBB. */
653 store_scattering_pbb (poly_bb_p pbb)
655 gcc_assert (PBB_TRANSFORMED (pbb));
658 poly_scattering_free (PBB_SAVED (pbb));
660 PBB_SAVED (pbb) = poly_scattering_copy (PBB_TRANSFORMED (pbb));
663 /* Saves the scattering for all the pbbs in the SCOP. */
666 store_scattering (scop_p scop)
671 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
672 store_scattering_pbb (pbb);
675 /* Restores the scattering of PBB. */
678 restore_scattering_pbb (poly_bb_p pbb)
680 gcc_assert (PBB_SAVED (pbb));
682 poly_scattering_free (PBB_TRANSFORMED (pbb));
683 PBB_TRANSFORMED (pbb) = poly_scattering_copy (PBB_SAVED (pbb));
686 /* Restores the scattering for all the pbbs in the SCOP. */
689 restore_scattering (scop_p scop)
694 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
695 restore_scattering_pbb (pbb);