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 /* A pointer to compiler's data reference description. */
59 /* A pointer to the PBB that contains this data reference. */
62 enum POLY_DR_TYPE type;
64 /* The access polyhedron contains the polyhedral space this data
65 reference will access.
67 The polyhedron contains these dimensions:
70 Every memory access is classified in at least one alias set.
72 - The subscripts (s_0, ..., s_n):
73 The memory is accessed using zero or more subscript dimensions.
75 - The iteration domain (variables and parameters)
77 Do not hardcode the dimensions. Use the following accessor functions:
91 | if (unknown_function ())
98 The data access A[i][j+k] in alias set "5" is described like this:
103 | 0 -1 -1 0 0 1 0 = 0
104 | 0 0 0 0 1 0 0 >= 0 # The last four lines describe the
105 | 0 0 0 0 0 1 0 >= 0 # array size.
106 | 0 0 0 0 -1 0 1335 >= 0
107 | 0 0 0 0 0 -1 123 >= 0
109 The pointer "*p" in alias set "5" and "7" is described as a union of
123 "*p" accesses all of the object allocated with 'malloc'.
125 The scalar data access "m" is represented as an array with zero subscript
130 ppl_Pointset_Powerset_C_Polyhedron_t accesses;
133 #define PDR_CDR(PDR) (PDR->compiler_dr)
134 #define PDR_PBB(PDR) (PDR->pbb)
135 #define PDR_TYPE(PDR) (PDR->type)
136 #define PDR_ACCESSES(PDR) (PDR->accesses)
138 void new_poly_dr (poly_bb_p, ppl_Pointset_Powerset_C_Polyhedron_t,
139 enum POLY_DR_TYPE, void *);
140 void free_poly_dr (poly_dr_p);
141 void debug_pdr (poly_dr_p);
142 void print_pdr (FILE *, poly_dr_p);
143 static inline scop_p pdr_scop (poly_dr_p pdr);
145 /* The number of subscripts of the PDR. */
147 static inline graphite_dim_t
148 pdr_nb_subscripts (poly_dr_p pdr)
150 poly_bb_p pbb = PDR_PBB (pdr);
151 ppl_dimension_type dim;
153 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PDR_ACCESSES (pdr),
155 return dim - pbb_dim_iter_domain (pbb) - pbb_nb_params (pbb) - 1;
158 /* The dimension of the iteration domain of the scop of PDR. */
160 static inline ppl_dimension_type
161 pdr_dim_iter_domain (poly_dr_p pdr)
163 return pbb_dim_iter_domain (PDR_PBB (pdr));
166 /* The number of parameters of the scop of PDR. */
168 static inline ppl_dimension_type
169 pdr_nb_params (poly_dr_p pdr)
171 return scop_nb_params (pdr_scop (pdr));
174 /* The dimension of the accesses polyhedron of PDR. */
176 static inline graphite_dim_t
177 pdr_dim (poly_dr_p pdr)
179 graphite_dim_t alias_nb_dimensions = 1;
181 return pbb_dim_iter_domain (PDR_PBB (pdr)) + alias_nb_dimensions
182 + pdr_nb_subscripts (pdr) + scop_nb_params (pdr_scop (pdr));
185 /* The dimension of the alias set in PDR. */
187 static inline ppl_dimension_type
188 pdr_alias_set_dim (poly_dr_p pdr)
190 poly_bb_p pbb = PDR_PBB (pdr);
192 return pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
195 /* The dimension in PDR containing subscript S. */
197 static inline ppl_dimension_type
198 pdr_subscript_dim (poly_dr_p pdr, graphite_dim_t s)
200 poly_bb_p pbb = PDR_PBB (pdr);
202 return pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb) + 1 + s;
205 /* The dimension in PDR containing the loop iterator ITER. */
207 static inline ppl_dimension_type
208 pdr_iterator_dim (poly_dr_p pdr ATTRIBUTE_UNUSED, graphite_dim_t iter)
213 /* The dimension in PDR containing parameter PARAM. */
215 static inline ppl_dimension_type
216 pdr_parameter_dim (poly_dr_p pdr, graphite_dim_t param)
218 poly_bb_p pbb = PDR_PBB (pdr);
220 return pbb_dim_iter_domain (pbb) + param;
223 typedef struct poly_scattering *poly_scattering_p;
225 struct poly_scattering
227 /* The scattering function containing the transformations. */
228 ppl_Polyhedron_t scattering;
230 /* The number of local variables. */
231 int nb_local_variables;
233 /* The number of scattering dimensions. */
237 /* POLY_BB represents a blackbox in the polyhedral model. */
245 /* The iteration domain of this bb.
248 for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
249 for (j = 2; j <= 2*i + 5; j++)
250 for (k = 0; k <= 5; k++)
253 Loop iterators: i, j, k
263 The number of variables in the DOMAIN may change and is not
264 related to the number of loops in the original code. */
265 ppl_Pointset_Powerset_C_Polyhedron_t domain;
267 /* The data references we access. */
268 VEC (poly_dr_p, heap) *drs;
270 /* The original scattering. */
271 poly_scattering_p original;
273 /* The transformed scattering. */
274 poly_scattering_p transformed;
276 /* A copy of the transformed scattering. */
277 poly_scattering_p saved;
280 #define PBB_BLACK_BOX(PBB) ((gimple_bb_p) PBB->black_box)
281 #define PBB_SCOP(PBB) (PBB->scop)
282 #define PBB_DOMAIN(PBB) (PBB->domain)
283 #define PBB_DRS(PBB) (PBB->drs)
284 #define PBB_ORIGINAL(PBB) (PBB->original)
285 #define PBB_ORIGINAL_SCATTERING(PBB) (PBB->original->scattering)
286 #define PBB_TRANSFORMED(PBB) (PBB->transformed)
287 #define PBB_TRANSFORMED_SCATTERING(PBB) (PBB->transformed->scattering)
288 #define PBB_SAVED(PBB) (PBB->saved)
289 #define PBB_NB_LOCAL_VARIABLES(PBB) (PBB->transformed->nb_local_variables)
290 #define PBB_NB_SCATTERING_TRANSFORM(PBB) (PBB->transformed->nb_scattering)
292 extern void new_poly_bb (scop_p, void *);
293 extern void free_poly_bb (poly_bb_p);
294 extern void debug_loop_vec (poly_bb_p);
295 extern void schedule_to_scattering (poly_bb_p, int);
296 extern void print_pbb_domain (FILE *, poly_bb_p);
297 extern void print_pbb (FILE *, poly_bb_p);
298 extern void print_scop_context (FILE *, scop_p);
299 extern void print_scop (FILE *, scop_p);
300 extern void debug_pbb_domain (poly_bb_p);
301 extern void debug_pbb (poly_bb_p);
302 extern void print_pdrs (FILE *, poly_bb_p);
303 extern void debug_pdrs (poly_bb_p);
304 extern void debug_scop_context (scop_p);
305 extern void debug_scop (scop_p);
306 extern void print_scop_params (FILE *, scop_p);
307 extern void debug_scop_params (scop_p);
308 extern void print_iteration_domain (FILE *, poly_bb_p);
309 extern void print_iteration_domains (FILE *, scop_p);
310 extern void debug_iteration_domain (poly_bb_p);
311 extern void debug_iteration_domains (scop_p);
312 extern bool scop_do_interchange (scop_p);
313 extern bool scop_do_strip_mine (scop_p);
314 extern void pbb_number_of_iterations (poly_bb_p, graphite_dim_t, Value);
316 /* The scop that contains the PDR. */
318 static inline scop_p pdr_scop (poly_dr_p pdr)
320 return PBB_SCOP (PDR_PBB (pdr));
323 /* Set black box of PBB to BLACKBOX. */
326 pbb_set_black_box (poly_bb_p pbb, void *black_box)
328 pbb->black_box = black_box;
331 /* The number of loops around PBB: the dimension of the iteration
334 static inline graphite_dim_t
335 pbb_dim_iter_domain (const struct poly_bb *pbb)
337 scop_p scop = PBB_SCOP (pbb);
338 ppl_dimension_type dim;
340 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
341 return dim - scop_nb_params (scop);
344 /* The number of params defined in PBB. */
346 static inline graphite_dim_t
347 pbb_nb_params (const struct poly_bb *pbb)
349 scop_p scop = PBB_SCOP (pbb);
351 return scop_nb_params (scop);
354 /* The number of scattering dimensions in the SCATTERING polyhedron
355 of a PBB for a given SCOP. */
357 static inline graphite_dim_t
358 pbb_nb_scattering_orig (const struct poly_bb *pbb)
360 return 2 * pbb_dim_iter_domain (pbb) + 1;
363 /* The number of scattering dimensions in PBB. */
365 static inline graphite_dim_t
366 pbb_nb_scattering_transform (const struct poly_bb *pbb)
368 return PBB_NB_SCATTERING_TRANSFORM (pbb);
371 /* Returns the number of local variables used in the transformed
372 scattering polyhedron of PBB. */
374 static inline graphite_dim_t
375 pbb_nb_local_vars (const struct poly_bb *pbb)
377 /* For now we do not have any local variables, as we do not do strip
378 mining for example. */
379 return PBB_NB_LOCAL_VARIABLES (pbb);
382 /* The dimension in the domain of PBB containing the iterator ITER. */
384 static inline ppl_dimension_type
385 pbb_iterator_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t iter)
390 /* The dimension in the domain of PBB containing the iterator ITER. */
392 static inline ppl_dimension_type
393 pbb_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
396 + pbb_dim_iter_domain (pbb);
399 /* The dimension in the original scattering polyhedron of PBB
400 containing the scattering iterator SCATTER. */
402 static inline ppl_dimension_type
403 psco_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
405 gcc_assert (scatter < pbb_nb_scattering_orig (pbb));
409 /* The dimension in the transformed scattering polyhedron of PBB
410 containing the scattering iterator SCATTER. */
412 static inline ppl_dimension_type
413 psct_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
415 gcc_assert (scatter <= pbb_nb_scattering_transform (pbb));
419 ppl_dimension_type psct_scattering_dim_for_loop_depth (poly_bb_p,
422 /* The dimension in the transformed scattering polyhedron of PBB of
423 the local variable LV. */
425 static inline ppl_dimension_type
426 psct_local_var_dim (poly_bb_p pbb, graphite_dim_t lv)
428 gcc_assert (lv <= pbb_nb_local_vars (pbb));
429 return lv + pbb_nb_scattering_transform (pbb);
432 /* The dimension in the original scattering polyhedron of PBB
433 containing the loop iterator ITER. */
435 static inline ppl_dimension_type
436 psco_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
438 gcc_assert (iter < pbb_dim_iter_domain (pbb));
439 return iter + pbb_nb_scattering_orig (pbb);
442 /* The dimension in the transformed scattering polyhedron of PBB
443 containing the loop iterator ITER. */
445 static inline ppl_dimension_type
446 psct_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
448 gcc_assert (iter < pbb_dim_iter_domain (pbb));
450 + pbb_nb_scattering_transform (pbb)
451 + pbb_nb_local_vars (pbb);
454 /* The dimension in the original scattering polyhedron of PBB
455 containing parameter PARAM. */
457 static inline ppl_dimension_type
458 psco_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
460 gcc_assert (param < pbb_nb_params (pbb));
462 + pbb_nb_scattering_orig (pbb)
463 + pbb_dim_iter_domain (pbb);
466 /* The dimension in the transformed scattering polyhedron of PBB
467 containing parameter PARAM. */
469 static inline ppl_dimension_type
470 psct_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
472 gcc_assert (param < pbb_nb_params (pbb));
474 + pbb_nb_scattering_transform (pbb)
475 + pbb_nb_local_vars (pbb)
476 + pbb_dim_iter_domain (pbb);
479 /* Adds to the transformed scattering polyhedron of PBB a new local
480 variable and returns its index. */
482 static inline graphite_dim_t
483 psct_add_local_variable (poly_bb_p pbb)
485 graphite_dim_t nlv = pbb_nb_local_vars (pbb);
486 ppl_dimension_type lv_column = psct_local_var_dim (pbb, nlv);
487 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), lv_column, 1);
488 PBB_NB_LOCAL_VARIABLES (pbb) += 1;
492 /* Adds a dimension to the transformed scattering polyhedron of PBB at
496 psct_add_scattering_dimension (poly_bb_p pbb, ppl_dimension_type index)
498 gcc_assert (index < pbb_nb_scattering_transform (pbb));
500 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), index, 1);
501 PBB_NB_SCATTERING_TRANSFORM (pbb) += 1;
504 /* A SCOP is a Static Control Part of the program, simple enough to be
505 represented in polyhedral form. */
508 /* A SCOP is defined as a SESE region. */
511 /* Number of parameters in SCoP. */
512 graphite_dim_t nb_params;
514 /* All the basic blocks in this scop that contain memory references
515 and that will be represented as statements in the polyhedral
517 VEC (poly_bb_p, heap) *bbs;
519 /* Data dependence graph for this SCoP. */
520 struct graph *dep_graph;
522 /* The context describes known restrictions concerning the parameters
523 and relations in between the parameters.
525 void f (int8_t a, uint_16_t b) {
530 Here we can add these restrictions to the context:
535 ppl_Pointset_Powerset_C_Polyhedron_t context;
537 /* A hashtable of the original pairs of dependent data references.
538 For each pair of dependent data references, the dependence
539 polyhedron is stored also. */
540 htab_t original_pdr_pairs;
543 #define SCOP_BBS(S) (S->bbs)
544 #define SCOP_REGION(S) ((sese) S->region)
545 #define SCOP_DEP_GRAPH(S) (S->dep_graph)
546 #define SCOP_CONTEXT(S) (S->context)
547 #define SCOP_ORIGINAL_PDR_PAIRS(S) (S->original_pdr_pairs)
549 extern scop_p new_scop (void *);
550 extern void free_scop (scop_p);
551 extern void free_scops (VEC (scop_p, heap) *);
552 extern void print_generated_program (FILE *, scop_p);
553 extern void debug_generated_program (scop_p);
554 extern void print_scattering_function (FILE *, poly_bb_p);
555 extern void print_scattering_functions (FILE *, scop_p);
556 extern void debug_scattering_function (poly_bb_p);
557 extern void debug_scattering_functions (scop_p);
558 extern int scop_max_loop_depth (scop_p);
559 extern int unify_scattering_dimensions (scop_p);
560 extern bool apply_poly_transforms (scop_p);
561 extern bool graphite_legal_transform (scop_p);
563 /* Set the region of SCOP to REGION. */
566 scop_set_region (scop_p scop, void *region)
568 scop->region = region;
571 /* Returns the number of parameters for SCOP. */
573 static inline graphite_dim_t
574 scop_nb_params (scop_p scop)
576 return scop->nb_params;
579 /* Set the number of params of SCOP to NB_PARAMS. */
582 scop_set_nb_params (scop_p scop, graphite_dim_t nb_params)
584 scop->nb_params = nb_params;
587 /* Allocates a new empty poly_scattering structure. */
589 static inline poly_scattering_p
590 poly_scattering_new (void)
592 poly_scattering_p res = XNEW (struct poly_scattering);
594 res->scattering = NULL;
595 res->nb_local_variables = 0;
596 res->nb_scattering = 0;
600 /* Free a poly_scattering structure. */
603 poly_scattering_free (poly_scattering_p s)
605 ppl_delete_Polyhedron (s->scattering);
609 /* Copies S and return a new scattering. */
611 static inline poly_scattering_p
612 poly_scattering_copy (poly_scattering_p s)
614 poly_scattering_p res = poly_scattering_new ();
616 ppl_new_C_Polyhedron_from_C_Polyhedron (&(res->scattering), s->scattering);
617 res->nb_local_variables = s->nb_local_variables;
618 res->nb_scattering = s->nb_scattering;
622 /* Saves the transformed scattering of PBB. */
625 store_scattering_pbb (poly_bb_p pbb)
627 gcc_assert (PBB_TRANSFORMED (pbb));
630 poly_scattering_free (PBB_SAVED (pbb));
632 PBB_SAVED (pbb) = poly_scattering_copy (PBB_TRANSFORMED (pbb));
635 /* Saves the scattering for all the pbbs in the SCOP. */
638 store_scattering (scop_p scop)
643 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
644 store_scattering_pbb (pbb);
647 /* Restores the scattering of PBB. */
650 restore_scattering_pbb (poly_bb_p pbb)
652 gcc_assert (PBB_SAVED (pbb));
654 poly_scattering_free (PBB_TRANSFORMED (pbb));
655 PBB_TRANSFORMED (pbb) = poly_scattering_copy (PBB_SAVED (pbb));
658 /* Restores the scattering for all the pbbs in the SCOP. */
661 restore_scattering (scop_p scop)
666 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
667 restore_scattering_pbb (pbb);