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
49 limit the expressiveness. */
56 /* An identifier for this PDR. */
59 /* The number of data refs identical to this one in the PBB. */
62 /* A pointer to compiler's data reference description. */
65 /* A pointer to the PBB that contains this data reference. */
68 enum poly_dr_type type
;
70 /* The access polyhedron contains the polyhedral space this data
71 reference will access.
73 The polyhedron contains these dimensions:
76 Every memory access is classified in at least one alias set.
78 - The subscripts (s_0, ..., s_n):
79 The memory is accessed using zero or more subscript dimensions.
81 - The iteration domain (variables and parameters)
83 Do not hardcode the dimensions. Use the following accessor functions:
97 | if (unknown_function ())
104 The data access A[i][j+k] in alias set "5" is described like this:
109 | 0 -1 -1 0 0 1 0 = 0
110 | 0 0 0 0 1 0 0 >= 0 # The last four lines describe the
111 | 0 0 0 0 0 1 0 >= 0 # array size.
112 | 0 0 0 0 -1 0 1335 >= 0
113 | 0 0 0 0 0 -1 123 >= 0
115 The pointer "*p" in alias set "5" and "7" is described as a union of
129 "*p" accesses all of the object allocated with 'malloc'.
131 The scalar data access "m" is represented as an array with zero subscript
136 ppl_Pointset_Powerset_C_Polyhedron_t accesses
;
138 /* Data reference's base object set number, we must assure 2 pdrs are in the
139 same base object set before dependency checking. */
140 int dr_base_object_set
;
142 /* The number of subscripts. */
143 graphite_dim_t nb_subscripts
;
146 #define PDR_ID(PDR) (PDR->id)
147 #define PDR_NB_REFS(PDR) (PDR->nb_refs)
148 #define PDR_CDR(PDR) (PDR->compiler_dr)
149 #define PDR_PBB(PDR) (PDR->pbb)
150 #define PDR_TYPE(PDR) (PDR->type)
151 #define PDR_ACCESSES(PDR) (PDR->accesses)
152 #define PDR_BASE_OBJECT_SET(PDR) (PDR->dr_base_object_set)
153 #define PDR_NB_SUBSCRIPTS(PDR) (PDR->nb_subscripts)
155 void new_poly_dr (poly_bb_p
, int, ppl_Pointset_Powerset_C_Polyhedron_t
,
156 enum poly_dr_type
, void *, graphite_dim_t
);
157 void free_poly_dr (poly_dr_p
);
158 void debug_pdr (poly_dr_p
);
159 void print_pdr (FILE *, poly_dr_p
);
160 static inline scop_p
pdr_scop (poly_dr_p pdr
);
162 /* The dimension of the PDR_ACCESSES polyhedron of PDR. */
164 static inline ppl_dimension_type
165 pdr_dim (poly_dr_p pdr
)
167 ppl_dimension_type dim
;
168 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PDR_ACCESSES (pdr
),
173 /* The dimension of the iteration domain of the scop of PDR. */
175 static inline ppl_dimension_type
176 pdr_dim_iter_domain (poly_dr_p pdr
)
178 return pbb_dim_iter_domain (PDR_PBB (pdr
));
181 /* The number of parameters of the scop of PDR. */
183 static inline ppl_dimension_type
184 pdr_nb_params (poly_dr_p pdr
)
186 return scop_nb_params (pdr_scop (pdr
));
189 /* The dimension of the alias set in PDR. */
191 static inline ppl_dimension_type
192 pdr_alias_set_dim (poly_dr_p pdr
)
194 poly_bb_p pbb
= PDR_PBB (pdr
);
196 return pbb_dim_iter_domain (pbb
) + pbb_nb_params (pbb
);
199 /* The dimension in PDR containing subscript S. */
201 static inline ppl_dimension_type
202 pdr_subscript_dim (poly_dr_p pdr
, graphite_dim_t s
)
204 poly_bb_p pbb
= PDR_PBB (pdr
);
206 return pbb_dim_iter_domain (pbb
) + pbb_nb_params (pbb
) + 1 + s
;
209 /* The dimension in PDR containing the loop iterator ITER. */
211 static inline ppl_dimension_type
212 pdr_iterator_dim (poly_dr_p pdr ATTRIBUTE_UNUSED
, graphite_dim_t iter
)
217 /* The dimension in PDR containing parameter PARAM. */
219 static inline ppl_dimension_type
220 pdr_parameter_dim (poly_dr_p pdr
, graphite_dim_t param
)
222 poly_bb_p pbb
= PDR_PBB (pdr
);
224 return pbb_dim_iter_domain (pbb
) + param
;
227 /* Returns true when PDR is a "read". */
230 pdr_read_p (poly_dr_p pdr
)
232 return PDR_TYPE (pdr
) == PDR_READ
;
235 /* Returns true when PDR is a "write". */
238 pdr_write_p (poly_dr_p pdr
)
240 return PDR_TYPE (pdr
) == PDR_WRITE
;
243 /* Returns true when PDR is a "may write". */
246 pdr_may_write_p (poly_dr_p pdr
)
248 return PDR_TYPE (pdr
) == PDR_MAY_WRITE
;
251 /* Return true when PDR1 and PDR2 are similar data accesses: they have
252 the same base array, and the same access functions. */
255 same_pdr_p (poly_dr_p pdr1
, poly_dr_p pdr2
)
257 return PDR_TYPE (pdr1
) == PDR_TYPE (pdr2
)
258 && PDR_NB_SUBSCRIPTS (pdr1
) == PDR_NB_SUBSCRIPTS (pdr2
)
259 && PDR_BASE_OBJECT_SET (pdr1
) == PDR_BASE_OBJECT_SET (pdr2
);
262 typedef struct poly_scattering
*poly_scattering_p
;
264 struct poly_scattering
266 /* The scattering function containing the transformations: the
267 layout of this polyhedron is: T|I|G with T the transform
268 scattering, I the iteration domain, G the context parameters. */
269 ppl_Polyhedron_t scattering
;
271 /* The number of local variables. */
272 int nb_local_variables
;
274 /* The number of scattering dimensions. */
278 /* POLY_BB represents a blackbox in the polyhedral model. */
282 /* Pointer to a basic block or a statement in the compiler. */
285 /* Pointer to the SCOP containing this PBB. */
288 /* The iteration domain of this bb. The layout of this polyhedron
289 is I|G with I the iteration domain, G the context parameters.
293 for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
294 for (j = 2; j <= 2*i + 5; j++)
295 for (k = 0; k <= 5; k++)
298 Loop iterators: i, j, k
308 The number of variables in the DOMAIN may change and is not
309 related to the number of loops in the original code. */
310 ppl_Pointset_Powerset_C_Polyhedron_t domain
;
312 /* The data references we access. */
313 VEC (poly_dr_p
, heap
) *drs
;
315 /* The original scattering. */
316 poly_scattering_p original
;
318 /* The transformed scattering. */
319 poly_scattering_p transformed
;
321 /* A copy of the transformed scattering. */
322 poly_scattering_p saved
;
324 /* True when the PDR duplicates have already been removed. */
325 bool pdr_duplicates_removed
;
327 /* True when this PBB contains only a reduction statement. */
331 #define PBB_BLACK_BOX(PBB) ((gimple_bb_p) PBB->black_box)
332 #define PBB_SCOP(PBB) (PBB->scop)
333 #define PBB_DOMAIN(PBB) (PBB->domain)
334 #define PBB_DRS(PBB) (PBB->drs)
335 #define PBB_ORIGINAL(PBB) (PBB->original)
336 #define PBB_ORIGINAL_SCATTERING(PBB) (PBB->original->scattering)
337 #define PBB_TRANSFORMED(PBB) (PBB->transformed)
338 #define PBB_TRANSFORMED_SCATTERING(PBB) (PBB->transformed->scattering)
339 #define PBB_SAVED(PBB) (PBB->saved)
340 #define PBB_NB_LOCAL_VARIABLES(PBB) (PBB->transformed->nb_local_variables)
341 #define PBB_NB_SCATTERING_TRANSFORM(PBB) (PBB->transformed->nb_scattering)
342 #define PBB_PDR_DUPLICATES_REMOVED(PBB) (PBB->pdr_duplicates_removed)
343 #define PBB_IS_REDUCTION(PBB) (PBB->is_reduction)
345 extern void new_poly_bb (scop_p
, void *, bool);
346 extern void free_poly_bb (poly_bb_p
);
347 extern void debug_loop_vec (poly_bb_p
);
348 extern void schedule_to_scattering (poly_bb_p
, int);
349 extern void print_pbb_domain (FILE *, poly_bb_p
);
350 extern void print_pbb (FILE *, poly_bb_p
);
351 extern void print_scop_context (FILE *, scop_p
);
352 extern void print_scop (FILE *, scop_p
);
353 extern void debug_pbb_domain (poly_bb_p
);
354 extern void debug_pbb (poly_bb_p
);
355 extern void print_pdrs (FILE *, poly_bb_p
);
356 extern void debug_pdrs (poly_bb_p
);
357 extern void debug_scop_context (scop_p
);
358 extern void debug_scop (scop_p
);
359 extern void print_scop_params (FILE *, scop_p
);
360 extern void debug_scop_params (scop_p
);
361 extern void print_iteration_domain (FILE *, poly_bb_p
);
362 extern void print_iteration_domains (FILE *, scop_p
);
363 extern void debug_iteration_domain (poly_bb_p
);
364 extern void debug_iteration_domains (scop_p
);
365 extern bool scop_do_interchange (scop_p
);
366 extern bool scop_do_strip_mine (scop_p
);
367 extern bool scop_do_block (scop_p
);
368 extern void pbb_number_of_iterations (poly_bb_p
, graphite_dim_t
, Value
);
369 extern void pbb_number_of_iterations_at_time (poly_bb_p
, graphite_dim_t
, Value
);
370 extern void pbb_remove_duplicate_pdrs (poly_bb_p
);
372 /* Return the number of write data references in PBB. */
375 number_of_write_pdrs (poly_bb_p pbb
)
381 for (i
= 0; VEC_iterate (poly_dr_p
, PBB_DRS (pbb
), i
, pdr
); i
++)
382 if (PDR_TYPE (pdr
) == PDR_WRITE
)
388 /* The index of the PBB. */
391 pbb_index (poly_bb_p pbb
)
393 return GBB_BB (PBB_BLACK_BOX (pbb
))->index
;
396 /* The loop of the PBB. */
399 pbb_loop (poly_bb_p pbb
)
401 return gbb_loop (PBB_BLACK_BOX (pbb
));
404 /* The scop that contains the PDR. */
407 pdr_scop (poly_dr_p pdr
)
409 return PBB_SCOP (PDR_PBB (pdr
));
412 /* Set black box of PBB to BLACKBOX. */
415 pbb_set_black_box (poly_bb_p pbb
, void *black_box
)
417 pbb
->black_box
= black_box
;
420 /* The number of loops around PBB: the dimension of the iteration
423 static inline graphite_dim_t
424 pbb_dim_iter_domain (const struct poly_bb
*pbb
)
426 scop_p scop
= PBB_SCOP (pbb
);
427 ppl_dimension_type dim
;
429 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb
), &dim
);
430 return dim
- scop_nb_params (scop
);
433 /* The number of params defined in PBB. */
435 static inline graphite_dim_t
436 pbb_nb_params (const struct poly_bb
*pbb
)
438 scop_p scop
= PBB_SCOP (pbb
);
440 return scop_nb_params (scop
);
443 /* The number of scattering dimensions in the SCATTERING polyhedron
444 of a PBB for a given SCOP. */
446 static inline graphite_dim_t
447 pbb_nb_scattering_orig (const struct poly_bb
*pbb
)
449 return 2 * pbb_dim_iter_domain (pbb
) + 1;
452 /* The number of scattering dimensions in PBB. */
454 static inline graphite_dim_t
455 pbb_nb_scattering_transform (const struct poly_bb
*pbb
)
457 return PBB_NB_SCATTERING_TRANSFORM (pbb
);
460 /* The number of dynamic scattering dimensions in PBB. */
462 static inline graphite_dim_t
463 pbb_nb_dynamic_scattering_transform (const struct poly_bb
*pbb
)
465 /* This function requires the 2d + 1 scattering format to be
466 invariant during all transformations. */
467 gcc_assert (PBB_NB_SCATTERING_TRANSFORM (pbb
) % 2);
468 return PBB_NB_SCATTERING_TRANSFORM (pbb
) / 2;
471 /* Returns the number of local variables used in the transformed
472 scattering polyhedron of PBB. */
474 static inline graphite_dim_t
475 pbb_nb_local_vars (const struct poly_bb
*pbb
)
477 /* For now we do not have any local variables, as we do not do strip
478 mining for example. */
479 return PBB_NB_LOCAL_VARIABLES (pbb
);
482 /* The dimension in the domain of PBB containing the iterator ITER. */
484 static inline ppl_dimension_type
485 pbb_iterator_dim (poly_bb_p pbb ATTRIBUTE_UNUSED
, graphite_dim_t iter
)
490 /* The dimension in the domain of PBB containing the iterator ITER. */
492 static inline ppl_dimension_type
493 pbb_parameter_dim (poly_bb_p pbb
, graphite_dim_t param
)
496 + pbb_dim_iter_domain (pbb
);
499 /* The dimension in the original scattering polyhedron of PBB
500 containing the scattering iterator SCATTER. */
502 static inline ppl_dimension_type
503 psco_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED
, graphite_dim_t scatter
)
505 gcc_assert (scatter
< pbb_nb_scattering_orig (pbb
));
509 /* The dimension in the transformed scattering polyhedron of PBB
510 containing the scattering iterator SCATTER. */
512 static inline ppl_dimension_type
513 psct_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED
, graphite_dim_t scatter
)
515 gcc_assert (scatter
<= pbb_nb_scattering_transform (pbb
));
519 ppl_dimension_type
psct_scattering_dim_for_loop_depth (poly_bb_p
,
522 /* The dimension in the transformed scattering polyhedron of PBB of
523 the local variable LV. */
525 static inline ppl_dimension_type
526 psct_local_var_dim (poly_bb_p pbb
, graphite_dim_t lv
)
528 gcc_assert (lv
<= pbb_nb_local_vars (pbb
));
529 return lv
+ pbb_nb_scattering_transform (pbb
);
532 /* The dimension in the original scattering polyhedron of PBB
533 containing the loop iterator ITER. */
535 static inline ppl_dimension_type
536 psco_iterator_dim (poly_bb_p pbb
, graphite_dim_t iter
)
538 gcc_assert (iter
< pbb_dim_iter_domain (pbb
));
539 return iter
+ pbb_nb_scattering_orig (pbb
);
542 /* The dimension in the transformed scattering polyhedron of PBB
543 containing the loop iterator ITER. */
545 static inline ppl_dimension_type
546 psct_iterator_dim (poly_bb_p pbb
, graphite_dim_t iter
)
548 gcc_assert (iter
< pbb_dim_iter_domain (pbb
));
550 + pbb_nb_scattering_transform (pbb
)
551 + pbb_nb_local_vars (pbb
);
554 /* The dimension in the original scattering polyhedron of PBB
555 containing parameter PARAM. */
557 static inline ppl_dimension_type
558 psco_parameter_dim (poly_bb_p pbb
, graphite_dim_t param
)
560 gcc_assert (param
< pbb_nb_params (pbb
));
562 + pbb_nb_scattering_orig (pbb
)
563 + pbb_dim_iter_domain (pbb
);
566 /* The dimension in the transformed scattering polyhedron of PBB
567 containing parameter PARAM. */
569 static inline ppl_dimension_type
570 psct_parameter_dim (poly_bb_p pbb
, graphite_dim_t param
)
572 gcc_assert (param
< pbb_nb_params (pbb
));
574 + pbb_nb_scattering_transform (pbb
)
575 + pbb_nb_local_vars (pbb
)
576 + pbb_dim_iter_domain (pbb
);
579 /* The scattering dimension of PBB corresponding to the dynamic level
582 static inline ppl_dimension_type
583 psct_dynamic_dim (poly_bb_p pbb
, graphite_dim_t level
)
585 graphite_dim_t result
= 1 + 2 * level
;
587 gcc_assert (result
< pbb_nb_scattering_transform (pbb
));
591 /* The scattering dimension of PBB corresponding to the static
592 sequence of the loop level LEVEL. */
594 static inline ppl_dimension_type
595 psct_static_dim (poly_bb_p pbb
, graphite_dim_t level
)
597 graphite_dim_t result
= 2 * level
;
599 gcc_assert (result
< pbb_nb_scattering_transform (pbb
));
603 /* Adds to the transformed scattering polyhedron of PBB a new local
604 variable and returns its index. */
606 static inline graphite_dim_t
607 psct_add_local_variable (poly_bb_p pbb
)
609 graphite_dim_t nlv
= pbb_nb_local_vars (pbb
);
610 ppl_dimension_type lv_column
= psct_local_var_dim (pbb
, nlv
);
611 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb
), lv_column
, 1);
612 PBB_NB_LOCAL_VARIABLES (pbb
) += 1;
616 /* Adds a dimension to the transformed scattering polyhedron of PBB at
620 psct_add_scattering_dimension (poly_bb_p pbb
, ppl_dimension_type index
)
622 gcc_assert (index
< pbb_nb_scattering_transform (pbb
));
624 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb
), index
, 1);
625 PBB_NB_SCATTERING_TRANSFORM (pbb
) += 1;
628 typedef struct lst
*lst_p
;
630 DEF_VEC_ALLOC_P (lst_p
, heap
);
632 /* Loops and Statements Tree. */
635 /* LOOP_P is true when an LST node is a loop. */
638 /* A pointer to the loop that contains this node. */
641 /* The sum of all the memory strides for an LST loop. */
642 Value memory_strides
;
644 /* Loop nodes contain a sequence SEQ of LST nodes, statements
645 contain a pointer to their polyhedral representation PBB. */
648 VEC (lst_p
, heap
) *seq
;
652 #define LST_LOOP_P(LST) ((LST)->loop_p)
653 #define LST_LOOP_FATHER(LST) ((LST)->loop_father)
654 #define LST_PBB(LST) ((LST)->node.pbb)
655 #define LST_SEQ(LST) ((LST)->node.seq)
656 #define LST_LOOP_MEMORY_STRIDES(LST) ((LST)->memory_strides)
658 void scop_to_lst (scop_p
);
659 void print_lst (FILE *, lst_p
, int);
660 void debug_lst (lst_p
);
661 void dot_lst (lst_p
);
663 /* Creates a new LST loop with SEQ. */
666 new_lst_loop (VEC (lst_p
, heap
) *seq
)
668 lst_p lst
= XNEW (struct lst
);
672 LST_LOOP_P (lst
) = true;
674 LST_LOOP_FATHER (lst
) = NULL
;
675 value_init (LST_LOOP_MEMORY_STRIDES (lst
));
676 value_set_si (LST_LOOP_MEMORY_STRIDES (lst
), -1);
678 for (i
= 0; VEC_iterate (lst_p
, seq
, i
, l
); i
++)
679 LST_LOOP_FATHER (l
) = lst
;
684 /* Creates a new LST statement with PBB. */
687 new_lst_stmt (poly_bb_p pbb
)
689 lst_p lst
= XNEW (struct lst
);
691 LST_LOOP_P (lst
) = false;
693 LST_LOOP_FATHER (lst
) = NULL
;
697 /* Frees the memory used by LST. */
705 if (LST_LOOP_P (lst
))
710 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (lst
), i
, l
); i
++)
713 value_clear (LST_LOOP_MEMORY_STRIDES (lst
));
714 VEC_free (lst_p
, heap
, LST_SEQ (lst
));
720 /* Returns a copy of LST. */
728 if (LST_LOOP_P (lst
))
732 VEC (lst_p
, heap
) *seq
= VEC_alloc (lst_p
, heap
, 5);
734 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (lst
), i
, l
); i
++)
735 VEC_safe_push (lst_p
, heap
, seq
, copy_lst (l
));
737 return new_lst_loop (seq
);
740 return new_lst_stmt (LST_PBB (lst
));
743 /* Adds a new loop under the loop LST. */
746 lst_add_loop_under_loop (lst_p lst
)
748 VEC (lst_p
, heap
) *seq
= VEC_alloc (lst_p
, heap
, 1);
749 lst_p l
= new_lst_loop (LST_SEQ (lst
));
751 gcc_assert (LST_LOOP_P (lst
));
753 LST_LOOP_FATHER (l
) = lst
;
754 VEC_quick_push (lst_p
, seq
, l
);
758 /* Returns the loop depth of LST. */
761 lst_depth (lst_p lst
)
766 /* The depth of the outermost "fake" loop is -1. This outermost
767 loop does not have a loop father and it is just a container, as
768 in the loop representation of GCC. */
769 if (!LST_LOOP_FATHER (lst
))
772 return lst_depth (LST_LOOP_FATHER (lst
)) + 1;
775 /* Returns the Dewey number for LST. */
778 lst_dewey_number (lst_p lst
)
786 if (!LST_LOOP_FATHER (lst
))
789 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (LST_LOOP_FATHER (lst
)), i
, l
); i
++)
796 /* Returns the Dewey number of LST at depth DEPTH. */
799 lst_dewey_number_at_depth (lst_p lst
, int depth
)
801 gcc_assert (lst
&& depth
>= 0 && lst_depth (lst
) <= depth
);
803 if (lst_depth (lst
) == depth
)
804 return lst_dewey_number (lst
);
806 return lst_dewey_number_at_depth (LST_LOOP_FATHER (lst
), depth
);
809 /* Returns the predecessor of LST in the sequence of its loop father.
810 Returns NULL if LST is the first statement in the sequence. */
818 if (!lst
|| !LST_LOOP_FATHER (lst
))
821 dewey
= lst_dewey_number (lst
);
825 father
= LST_LOOP_FATHER (lst
);
826 return VEC_index (lst_p
, LST_SEQ (father
), dewey
- 1);
829 /* Returns the successor of LST in the sequence of its loop father.
830 Returns NULL if there is none. */
838 if (!lst
|| !LST_LOOP_FATHER (lst
))
841 dewey
= lst_dewey_number (lst
);
842 father
= LST_LOOP_FATHER (lst
);
844 if (VEC_length (lst_p
, LST_SEQ (father
)) == (unsigned) dewey
+ 1)
847 return VEC_index (lst_p
, LST_SEQ (father
), dewey
+ 1);
851 /* Return the LST node corresponding to PBB. */
854 lst_find_pbb (lst_p lst
, poly_bb_p pbb
)
862 if (!LST_LOOP_P (lst
))
863 return (pbb
== LST_PBB (lst
)) ? lst
: NULL
;
865 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (lst
), i
, l
); i
++)
867 lst_p res
= lst_find_pbb (l
, pbb
);
875 /* Return the LST node corresponding to the loop around STMT at depth
879 find_lst_loop (lst_p stmt
, int loop_depth
)
881 lst_p loop
= LST_LOOP_FATHER (stmt
);
883 gcc_assert (loop_depth
>= 0);
885 while (loop_depth
< lst_depth (loop
))
886 loop
= LST_LOOP_FATHER (loop
);
891 /* Return the first lst representing a PBB statement in LST. */
894 lst_find_first_pbb (lst_p lst
)
902 if (!LST_LOOP_P (lst
))
905 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (lst
), i
, l
); i
++)
907 lst_p res
= lst_find_first_pbb (l
);
915 /* Returns true when LST is a loop that does not contains
919 lst_empty_p (lst_p lst
)
921 return !lst_find_first_pbb (lst
);
924 /* Return the last lst representing a PBB statement in LST. */
927 lst_find_last_pbb (lst_p lst
)
935 if (!LST_LOOP_P (lst
))
938 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (lst
), i
, l
); i
++)
940 lst_p last
= lst_find_last_pbb (l
);
950 /* Returns true if LOOP contains LST, in other words, if LST is nested
954 lst_contains_p (lst_p loop
, lst_p lst
)
956 if (!loop
|| !lst
|| !LST_LOOP_P (loop
))
962 return lst_contains_p (loop
, LST_LOOP_FATHER (lst
));
965 /* Returns true if LOOP contains PBB, in other words, if PBB is nested
969 lst_contains_pbb (lst_p loop
, poly_bb_p pbb
)
971 return lst_find_pbb (loop
, pbb
) ? true : false;
974 /* Creates a loop nest of depth NB_LOOPS containing LST. */
977 lst_create_nest (int nb_loops
, lst_p lst
)
980 VEC (lst_p
, heap
) *seq
;
985 seq
= VEC_alloc (lst_p
, heap
, 1);
986 loop
= lst_create_nest (nb_loops
- 1, lst
);
987 VEC_quick_push (lst_p
, seq
, loop
);
988 res
= new_lst_loop (seq
);
989 LST_LOOP_FATHER (loop
) = res
;
994 /* Removes LST from the sequence of statements of its loop father. */
997 lst_remove_from_sequence (lst_p lst
)
999 lst_p father
= LST_LOOP_FATHER (lst
);
1000 int dewey
= lst_dewey_number (lst
);
1002 gcc_assert (lst
&& father
&& dewey
>= 0);
1004 VEC_ordered_remove (lst_p
, LST_SEQ (father
), dewey
);
1005 LST_LOOP_FATHER (lst
) = NULL
;
1008 /* Updates the scattering of PBB to be at the DEWEY number in the loop
1012 pbb_update_scattering (poly_bb_p pbb
, graphite_dim_t level
, int dewey
)
1014 ppl_Polyhedron_t ph
= PBB_TRANSFORMED_SCATTERING (pbb
);
1015 ppl_dimension_type sched
= psct_static_dim (pbb
, level
);
1016 ppl_dimension_type ds
[1];
1017 ppl_Constraint_t new_cstr
;
1018 ppl_Linear_Expression_t expr
;
1019 ppl_dimension_type dim
;
1021 ppl_Polyhedron_space_dimension (ph
, &dim
);
1023 ppl_Polyhedron_remove_space_dimensions (ph
, ds
, 1);
1024 ppl_insert_dimensions (ph
, sched
, 1);
1026 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
1027 ppl_set_coef (expr
, sched
, -1);
1028 ppl_set_inhomogeneous (expr
, dewey
);
1029 ppl_new_Constraint (&new_cstr
, expr
, PPL_CONSTRAINT_TYPE_EQUAL
);
1030 ppl_delete_Linear_Expression (expr
);
1031 ppl_Polyhedron_add_constraint (ph
, new_cstr
);
1032 ppl_delete_Constraint (new_cstr
);
1035 /* Updates the scattering of all the PBBs under LST to be at the DEWEY
1036 number in the loop at depth LEVEL. */
1039 lst_update_scattering_under (lst_p lst
, int level
, int dewey
)
1044 gcc_assert (lst
&& level
>= 0 && dewey
>= 0);
1046 if (LST_LOOP_P (lst
))
1047 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (lst
), i
, l
); i
++)
1048 lst_update_scattering_under (l
, level
, dewey
);
1050 pbb_update_scattering (LST_PBB (lst
), level
, dewey
);
1053 /* Updates the scattering of all the PBBs under LST and in sequence
1057 lst_update_scattering_seq (lst_p lst
)
1061 lst_p father
= LST_LOOP_FATHER (lst
);
1062 int dewey
= lst_dewey_number (lst
);
1063 int level
= lst_depth (lst
);
1065 gcc_assert (lst
&& father
&& dewey
>= 0 && level
>= 0);
1067 for (i
= dewey
; VEC_iterate (lst_p
, LST_SEQ (father
), i
, l
); i
++)
1068 lst_update_scattering_under (l
, level
, i
);
1071 /* Updates the all the scattering levels of all the PBBs under
1075 lst_update_scattering (lst_p lst
)
1080 if (!lst
|| !LST_LOOP_P (lst
))
1083 if (LST_LOOP_FATHER (lst
))
1084 lst_update_scattering_seq (lst
);
1086 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (lst
), i
, l
); i
++)
1087 lst_update_scattering (l
);
1090 /* Inserts LST1 before LST2 if BEFORE is true; inserts LST1 after LST2
1091 if BEFORE is false. */
1094 lst_insert_in_sequence (lst_p lst1
, lst_p lst2
, bool before
)
1099 /* Do not insert empty loops. */
1100 if (!lst1
|| lst_empty_p (lst1
))
1103 father
= LST_LOOP_FATHER (lst2
);
1104 dewey
= lst_dewey_number (lst2
);
1106 gcc_assert (lst2
&& father
&& dewey
>= 0);
1108 VEC_safe_insert (lst_p
, heap
, LST_SEQ (father
), before
? dewey
: dewey
+ 1,
1110 LST_LOOP_FATHER (lst1
) = father
;
1113 /* Replaces LST1 with LST2. */
1116 lst_replace (lst_p lst1
, lst_p lst2
)
1121 if (!lst2
|| lst_empty_p (lst2
))
1124 father
= LST_LOOP_FATHER (lst1
);
1125 dewey
= lst_dewey_number (lst1
);
1126 LST_LOOP_FATHER (lst2
) = father
;
1127 VEC_replace (lst_p
, LST_SEQ (father
), dewey
, lst2
);
1130 /* Returns a copy of ROOT where LST has been replaced by a copy of the
1131 LSTs A B C in this sequence. */
1134 lst_substitute_3 (lst_p root
, lst_p lst
, lst_p a
, lst_p b
, lst_p c
)
1138 VEC (lst_p
, heap
) *seq
;
1143 gcc_assert (lst
&& root
!= lst
);
1145 if (!LST_LOOP_P (root
))
1146 return new_lst_stmt (LST_PBB (root
));
1148 seq
= VEC_alloc (lst_p
, heap
, 5);
1150 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (root
), i
, l
); i
++)
1152 VEC_safe_push (lst_p
, heap
, seq
, lst_substitute_3 (l
, lst
, a
, b
, c
));
1155 if (!lst_empty_p (a
))
1156 VEC_safe_push (lst_p
, heap
, seq
, copy_lst (a
));
1157 if (!lst_empty_p (b
))
1158 VEC_safe_push (lst_p
, heap
, seq
, copy_lst (b
));
1159 if (!lst_empty_p (c
))
1160 VEC_safe_push (lst_p
, heap
, seq
, copy_lst (c
));
1163 return new_lst_loop (seq
);
1166 /* Moves LST before LOOP if BEFORE is true, and after the LOOP if
1170 lst_distribute_lst (lst_p loop
, lst_p lst
, bool before
)
1172 int loop_depth
= lst_depth (loop
);
1173 int depth
= lst_depth (lst
);
1174 int nb_loops
= depth
- loop_depth
;
1176 gcc_assert (lst
&& loop
&& LST_LOOP_P (loop
) && nb_loops
> 0);
1178 lst_remove_from_sequence (lst
);
1179 lst_insert_in_sequence (lst_create_nest (nb_loops
, lst
), loop
, before
);
1182 /* Removes from LOOP all the statements before/after and including PBB
1183 if BEFORE is true/false. Returns the negation of BEFORE when the
1184 statement PBB has been found. */
1187 lst_remove_all_before_including_pbb (lst_p loop
, poly_bb_p pbb
, bool before
)
1192 if (!loop
|| !LST_LOOP_P (loop
))
1195 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (loop
), i
, l
);)
1198 before
= lst_remove_all_before_including_pbb (l
, pbb
, before
);
1200 if (VEC_length (lst_p
, LST_SEQ (l
)) == 0)
1202 VEC_ordered_remove (lst_p
, LST_SEQ (loop
), i
);
1212 if (LST_PBB (l
) == pbb
)
1215 VEC_ordered_remove (lst_p
, LST_SEQ (loop
), i
);
1218 else if (LST_PBB (l
) == pbb
)
1221 VEC_ordered_remove (lst_p
, LST_SEQ (loop
), i
);
1231 /* Removes from LOOP all the statements before/after and excluding PBB
1232 if BEFORE is true/false; Returns the negation of BEFORE when the
1233 statement PBB has been found. */
1236 lst_remove_all_before_excluding_pbb (lst_p loop
, poly_bb_p pbb
, bool before
)
1241 if (!loop
|| !LST_LOOP_P (loop
))
1244 for (i
= 0; VEC_iterate (lst_p
, LST_SEQ (loop
), i
, l
);)
1247 before
= lst_remove_all_before_excluding_pbb (l
, pbb
, before
);
1249 if (VEC_length (lst_p
, LST_SEQ (l
)) == 0)
1251 VEC_ordered_remove (lst_p
, LST_SEQ (loop
), i
);
1260 if (before
&& LST_PBB (l
) != pbb
)
1262 VEC_ordered_remove (lst_p
, LST_SEQ (loop
), i
);
1269 if (LST_PBB (l
) == pbb
)
1270 before
= before
? false : true;
1276 /* A SCOP is a Static Control Part of the program, simple enough to be
1277 represented in polyhedral form. */
1280 /* A SCOP is defined as a SESE region. */
1283 /* Number of parameters in SCoP. */
1284 graphite_dim_t nb_params
;
1286 /* All the basic blocks in this scop that contain memory references
1287 and that will be represented as statements in the polyhedral
1289 VEC (poly_bb_p
, heap
) *bbs
;
1291 /* Original, transformed and saved schedules. */
1292 lst_p original_schedule
, transformed_schedule
, saved_schedule
;
1294 /* The context describes known restrictions concerning the parameters
1295 and relations in between the parameters.
1297 void f (int8_t a, uint_16_t b) {
1302 Here we can add these restrictions to the context:
1307 ppl_Pointset_Powerset_C_Polyhedron_t context
;
1309 /* A hashtable of the data dependence relations for the original
1311 htab_t original_pddrs
;
1313 /* True when the scop has been converted to its polyhedral
1318 #define SCOP_BBS(S) (S->bbs)
1319 #define SCOP_REGION(S) ((sese) S->region)
1320 #define SCOP_CONTEXT(S) (S->context)
1321 #define SCOP_ORIGINAL_PDDRS(S) (S->original_pddrs)
1322 #define SCOP_ORIGINAL_SCHEDULE(S) (S->original_schedule)
1323 #define SCOP_TRANSFORMED_SCHEDULE(S) (S->transformed_schedule)
1324 #define SCOP_SAVED_SCHEDULE(S) (S->saved_schedule)
1325 #define POLY_SCOP_P(S) (S->poly_scop_p)
1327 extern scop_p
new_scop (void *);
1328 extern void free_scop (scop_p
);
1329 extern void free_scops (VEC (scop_p
, heap
) *);
1330 extern void print_generated_program (FILE *, scop_p
);
1331 extern void debug_generated_program (scop_p
);
1332 extern void print_scattering_function (FILE *, poly_bb_p
);
1333 extern void print_scattering_functions (FILE *, scop_p
);
1334 extern void debug_scattering_function (poly_bb_p
);
1335 extern void debug_scattering_functions (scop_p
);
1336 extern int scop_max_loop_depth (scop_p
);
1337 extern int unify_scattering_dimensions (scop_p
);
1338 extern bool apply_poly_transforms (scop_p
);
1339 extern bool graphite_legal_transform (scop_p
);
1341 /* Set the region of SCOP to REGION. */
1344 scop_set_region (scop_p scop
, void *region
)
1346 scop
->region
= region
;
1349 /* Returns the number of parameters for SCOP. */
1351 static inline graphite_dim_t
1352 scop_nb_params (scop_p scop
)
1354 return scop
->nb_params
;
1357 /* Set the number of params of SCOP to NB_PARAMS. */
1360 scop_set_nb_params (scop_p scop
, graphite_dim_t nb_params
)
1362 scop
->nb_params
= nb_params
;
1365 /* Allocates a new empty poly_scattering structure. */
1367 static inline poly_scattering_p
1368 poly_scattering_new (void)
1370 poly_scattering_p res
= XNEW (struct poly_scattering
);
1372 res
->scattering
= NULL
;
1373 res
->nb_local_variables
= 0;
1374 res
->nb_scattering
= 0;
1378 /* Free a poly_scattering structure. */
1381 poly_scattering_free (poly_scattering_p s
)
1383 ppl_delete_Polyhedron (s
->scattering
);
1387 /* Copies S and return a new scattering. */
1389 static inline poly_scattering_p
1390 poly_scattering_copy (poly_scattering_p s
)
1392 poly_scattering_p res
= poly_scattering_new ();
1394 ppl_new_C_Polyhedron_from_C_Polyhedron (&(res
->scattering
), s
->scattering
);
1395 res
->nb_local_variables
= s
->nb_local_variables
;
1396 res
->nb_scattering
= s
->nb_scattering
;
1400 /* Saves the transformed scattering of PBB. */
1403 store_scattering_pbb (poly_bb_p pbb
)
1405 gcc_assert (PBB_TRANSFORMED (pbb
));
1407 if (PBB_SAVED (pbb
))
1408 poly_scattering_free (PBB_SAVED (pbb
));
1410 PBB_SAVED (pbb
) = poly_scattering_copy (PBB_TRANSFORMED (pbb
));
1413 /* Stores the SCOP_TRANSFORMED_SCHEDULE to SCOP_SAVED_SCHEDULE. */
1416 store_lst_schedule (scop_p scop
)
1418 if (SCOP_SAVED_SCHEDULE (scop
))
1419 free_lst (SCOP_SAVED_SCHEDULE (scop
));
1421 SCOP_SAVED_SCHEDULE (scop
) = copy_lst (SCOP_TRANSFORMED_SCHEDULE (scop
));
1424 /* Restores the SCOP_TRANSFORMED_SCHEDULE from SCOP_SAVED_SCHEDULE. */
1427 restore_lst_schedule (scop_p scop
)
1429 if (SCOP_TRANSFORMED_SCHEDULE (scop
))
1430 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop
));
1432 SCOP_TRANSFORMED_SCHEDULE (scop
) = copy_lst (SCOP_SAVED_SCHEDULE (scop
));
1435 /* Saves the scattering for all the pbbs in the SCOP. */
1438 store_scattering (scop_p scop
)
1443 for (i
= 0; VEC_iterate (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
); i
++)
1444 store_scattering_pbb (pbb
);
1446 store_lst_schedule (scop
);
1449 /* Restores the scattering of PBB. */
1452 restore_scattering_pbb (poly_bb_p pbb
)
1454 gcc_assert (PBB_SAVED (pbb
));
1456 poly_scattering_free (PBB_TRANSFORMED (pbb
));
1457 PBB_TRANSFORMED (pbb
) = poly_scattering_copy (PBB_SAVED (pbb
));
1460 /* Restores the scattering for all the pbbs in the SCOP. */
1463 restore_scattering (scop_p scop
)
1468 for (i
= 0; VEC_iterate (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
); i
++)
1469 restore_scattering_pbb (pbb
);
1471 restore_lst_schedule (scop
);
1474 /* For a given PBB, add to RES the scop context, the iteration domain,
1475 the original scattering when ORIGINAL_P is true, otherwise add the
1476 transformed scattering. */
1479 combine_context_id_scat (ppl_Pointset_Powerset_C_Polyhedron_t
*res
,
1480 poly_bb_p pbb
, bool original_p
)
1482 ppl_Pointset_Powerset_C_Polyhedron_t context
;
1483 ppl_Pointset_Powerset_C_Polyhedron_t id
;
1485 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1487 PBB_ORIGINAL_SCATTERING (pbb
) : PBB_TRANSFORMED_SCATTERING (pbb
));
1489 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1490 (&context
, SCOP_CONTEXT (PBB_SCOP (pbb
)));
1492 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1493 (&id
, PBB_DOMAIN (pbb
));
1495 /* Extend the context and the iteration domain to the dimension of
1496 the scattering: T|I|G. */
1498 ppl_dimension_type gdim
, tdim
, idim
;
1500 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (*res
, &tdim
);
1501 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (context
, &gdim
);
1502 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (id
, &idim
);
1505 ppl_insert_dimensions_pointset (context
, 0, tdim
- gdim
);
1508 ppl_insert_dimensions_pointset (id
, 0, tdim
- idim
);
1511 /* Add the context and the iteration domain to the result. */
1512 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (*res
, context
);
1513 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (*res
, id
);
1515 ppl_delete_Pointset_Powerset_C_Polyhedron (context
);
1516 ppl_delete_Pointset_Powerset_C_Polyhedron (id
);