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. */
267 ppl_Polyhedron_t scattering;
269 /* The number of local variables. */
270 int nb_local_variables;
272 /* The number of scattering dimensions. */
276 /* POLY_BB represents a blackbox in the polyhedral model. */
280 /* Pointer to a basic block or a statement in the compiler. */
283 /* Pointer to the SCOP containing this PBB. */
286 /* The iteration domain of this bb.
289 for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
290 for (j = 2; j <= 2*i + 5; j++)
291 for (k = 0; k <= 5; k++)
294 Loop iterators: i, j, k
304 The number of variables in the DOMAIN may change and is not
305 related to the number of loops in the original code. */
306 ppl_Pointset_Powerset_C_Polyhedron_t domain;
308 /* The data references we access. */
309 VEC (poly_dr_p, heap) *drs;
311 /* The original scattering. */
312 poly_scattering_p original;
314 /* The transformed scattering. */
315 poly_scattering_p transformed;
317 /* A copy of the transformed scattering. */
318 poly_scattering_p saved;
320 /* True when the PDR duplicates have already been removed. */
321 bool pdr_duplicates_removed;
323 /* True when this PBB contains only a reduction statement. */
327 #define PBB_BLACK_BOX(PBB) ((gimple_bb_p) PBB->black_box)
328 #define PBB_SCOP(PBB) (PBB->scop)
329 #define PBB_DOMAIN(PBB) (PBB->domain)
330 #define PBB_DRS(PBB) (PBB->drs)
331 #define PBB_ORIGINAL(PBB) (PBB->original)
332 #define PBB_ORIGINAL_SCATTERING(PBB) (PBB->original->scattering)
333 #define PBB_TRANSFORMED(PBB) (PBB->transformed)
334 #define PBB_TRANSFORMED_SCATTERING(PBB) (PBB->transformed->scattering)
335 #define PBB_SAVED(PBB) (PBB->saved)
336 #define PBB_NB_LOCAL_VARIABLES(PBB) (PBB->transformed->nb_local_variables)
337 #define PBB_NB_SCATTERING_TRANSFORM(PBB) (PBB->transformed->nb_scattering)
338 #define PBB_PDR_DUPLICATES_REMOVED(PBB) (PBB->pdr_duplicates_removed)
339 #define PBB_IS_REDUCTION(PBB) (PBB->is_reduction)
341 extern void new_poly_bb (scop_p, void *, bool);
342 extern void free_poly_bb (poly_bb_p);
343 extern void debug_loop_vec (poly_bb_p);
344 extern void schedule_to_scattering (poly_bb_p, int);
345 extern void print_pbb_domain (FILE *, poly_bb_p);
346 extern void print_pbb (FILE *, poly_bb_p);
347 extern void print_scop_context (FILE *, scop_p);
348 extern void print_scop (FILE *, scop_p);
349 extern void debug_pbb_domain (poly_bb_p);
350 extern void debug_pbb (poly_bb_p);
351 extern void print_pdrs (FILE *, poly_bb_p);
352 extern void debug_pdrs (poly_bb_p);
353 extern void debug_scop_context (scop_p);
354 extern void debug_scop (scop_p);
355 extern void print_scop_params (FILE *, scop_p);
356 extern void debug_scop_params (scop_p);
357 extern void print_iteration_domain (FILE *, poly_bb_p);
358 extern void print_iteration_domains (FILE *, scop_p);
359 extern void debug_iteration_domain (poly_bb_p);
360 extern void debug_iteration_domains (scop_p);
361 extern bool scop_do_interchange (scop_p);
362 extern bool scop_do_strip_mine (scop_p);
363 extern bool scop_do_block (scop_p);
364 extern void pbb_number_of_iterations (poly_bb_p, graphite_dim_t, Value);
365 extern void pbb_number_of_iterations_at_time (poly_bb_p, graphite_dim_t, Value);
366 extern void pbb_remove_duplicate_pdrs (poly_bb_p);
368 /* Return the number of write data references in PBB. */
371 number_of_write_pdrs (poly_bb_p pbb)
377 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
378 if (PDR_TYPE (pdr) == PDR_WRITE)
384 /* The index of the PBB. */
387 pbb_index (poly_bb_p pbb)
389 return GBB_BB (PBB_BLACK_BOX (pbb))->index;
392 /* The loop of the PBB. */
395 pbb_loop (poly_bb_p pbb)
397 return gbb_loop (PBB_BLACK_BOX (pbb));
400 /* The scop that contains the PDR. */
403 pdr_scop (poly_dr_p pdr)
405 return PBB_SCOP (PDR_PBB (pdr));
408 /* Set black box of PBB to BLACKBOX. */
411 pbb_set_black_box (poly_bb_p pbb, void *black_box)
413 pbb->black_box = black_box;
416 /* The number of loops around PBB: the dimension of the iteration
419 static inline graphite_dim_t
420 pbb_dim_iter_domain (const struct poly_bb *pbb)
422 scop_p scop = PBB_SCOP (pbb);
423 ppl_dimension_type dim;
425 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
426 return dim - scop_nb_params (scop);
429 /* The number of params defined in PBB. */
431 static inline graphite_dim_t
432 pbb_nb_params (const struct poly_bb *pbb)
434 scop_p scop = PBB_SCOP (pbb);
436 return scop_nb_params (scop);
439 /* The number of scattering dimensions in the SCATTERING polyhedron
440 of a PBB for a given SCOP. */
442 static inline graphite_dim_t
443 pbb_nb_scattering_orig (const struct poly_bb *pbb)
445 return 2 * pbb_dim_iter_domain (pbb) + 1;
448 /* The number of scattering dimensions in PBB. */
450 static inline graphite_dim_t
451 pbb_nb_scattering_transform (const struct poly_bb *pbb)
453 return PBB_NB_SCATTERING_TRANSFORM (pbb);
456 /* The number of dynamic scattering dimensions in PBB. */
458 static inline graphite_dim_t
459 pbb_nb_dynamic_scattering_transform (const struct poly_bb *pbb)
461 /* This function requires the 2d + 1 scattering format to be
462 invariant during all transformations. */
463 gcc_assert (PBB_NB_SCATTERING_TRANSFORM (pbb) % 2);
464 return PBB_NB_SCATTERING_TRANSFORM (pbb) / 2;
467 /* Returns the number of local variables used in the transformed
468 scattering polyhedron of PBB. */
470 static inline graphite_dim_t
471 pbb_nb_local_vars (const struct poly_bb *pbb)
473 /* For now we do not have any local variables, as we do not do strip
474 mining for example. */
475 return PBB_NB_LOCAL_VARIABLES (pbb);
478 /* The dimension in the domain of PBB containing the iterator ITER. */
480 static inline ppl_dimension_type
481 pbb_iterator_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t iter)
486 /* The dimension in the domain of PBB containing the iterator ITER. */
488 static inline ppl_dimension_type
489 pbb_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
492 + pbb_dim_iter_domain (pbb);
495 /* The dimension in the original scattering polyhedron of PBB
496 containing the scattering iterator SCATTER. */
498 static inline ppl_dimension_type
499 psco_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
501 gcc_assert (scatter < pbb_nb_scattering_orig (pbb));
505 /* The dimension in the transformed scattering polyhedron of PBB
506 containing the scattering iterator SCATTER. */
508 static inline ppl_dimension_type
509 psct_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
511 gcc_assert (scatter <= pbb_nb_scattering_transform (pbb));
515 ppl_dimension_type psct_scattering_dim_for_loop_depth (poly_bb_p,
518 /* The dimension in the transformed scattering polyhedron of PBB of
519 the local variable LV. */
521 static inline ppl_dimension_type
522 psct_local_var_dim (poly_bb_p pbb, graphite_dim_t lv)
524 gcc_assert (lv <= pbb_nb_local_vars (pbb));
525 return lv + pbb_nb_scattering_transform (pbb);
528 /* The dimension in the original scattering polyhedron of PBB
529 containing the loop iterator ITER. */
531 static inline ppl_dimension_type
532 psco_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
534 gcc_assert (iter < pbb_dim_iter_domain (pbb));
535 return iter + pbb_nb_scattering_orig (pbb);
538 /* The dimension in the transformed scattering polyhedron of PBB
539 containing the loop iterator ITER. */
541 static inline ppl_dimension_type
542 psct_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
544 gcc_assert (iter < pbb_dim_iter_domain (pbb));
546 + pbb_nb_scattering_transform (pbb)
547 + pbb_nb_local_vars (pbb);
550 /* The dimension in the original scattering polyhedron of PBB
551 containing parameter PARAM. */
553 static inline ppl_dimension_type
554 psco_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
556 gcc_assert (param < pbb_nb_params (pbb));
558 + pbb_nb_scattering_orig (pbb)
559 + pbb_dim_iter_domain (pbb);
562 /* The dimension in the transformed scattering polyhedron of PBB
563 containing parameter PARAM. */
565 static inline ppl_dimension_type
566 psct_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
568 gcc_assert (param < pbb_nb_params (pbb));
570 + pbb_nb_scattering_transform (pbb)
571 + pbb_nb_local_vars (pbb)
572 + pbb_dim_iter_domain (pbb);
575 /* The scattering dimension of PBB corresponding to the dynamic level
578 static inline ppl_dimension_type
579 psct_dynamic_dim (poly_bb_p pbb, graphite_dim_t level)
581 graphite_dim_t result = 1 + 2 * level;
583 gcc_assert (result < pbb_nb_scattering_transform (pbb));
587 /* The scattering dimension of PBB corresponding to the static
588 sequence of the loop level LEVEL. */
590 static inline ppl_dimension_type
591 psct_static_dim (poly_bb_p pbb, graphite_dim_t level)
593 graphite_dim_t result = 2 * level;
595 gcc_assert (result < pbb_nb_scattering_transform (pbb));
599 /* Adds to the transformed scattering polyhedron of PBB a new local
600 variable and returns its index. */
602 static inline graphite_dim_t
603 psct_add_local_variable (poly_bb_p pbb)
605 graphite_dim_t nlv = pbb_nb_local_vars (pbb);
606 ppl_dimension_type lv_column = psct_local_var_dim (pbb, nlv);
607 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), lv_column, 1);
608 PBB_NB_LOCAL_VARIABLES (pbb) += 1;
612 /* Adds a dimension to the transformed scattering polyhedron of PBB at
616 psct_add_scattering_dimension (poly_bb_p pbb, ppl_dimension_type index)
618 gcc_assert (index < pbb_nb_scattering_transform (pbb));
620 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), index, 1);
621 PBB_NB_SCATTERING_TRANSFORM (pbb) += 1;
624 typedef struct lst *lst_p;
626 DEF_VEC_ALLOC_P (lst_p, heap);
628 /* Loops and Statements Tree. */
631 /* LOOP_P is true when an LST node is a loop. */
634 /* A pointer to the loop that contains this node. */
637 /* The sum of all the memory strides for an LST loop. */
638 Value memory_strides;
640 /* Loop nodes contain a sequence SEQ of LST nodes, statements
641 contain a pointer to their polyhedral representation PBB. */
644 VEC (lst_p, heap) *seq;
648 #define LST_LOOP_P(LST) ((LST)->loop_p)
649 #define LST_LOOP_FATHER(LST) ((LST)->loop_father)
650 #define LST_PBB(LST) ((LST)->node.pbb)
651 #define LST_SEQ(LST) ((LST)->node.seq)
652 #define LST_LOOP_MEMORY_STRIDES(LST) ((LST)->memory_strides)
654 void scop_to_lst (scop_p);
655 void print_lst (FILE *, lst_p, int);
656 void debug_lst (lst_p);
657 void dot_lst (lst_p);
659 /* Creates a new LST loop with SEQ. */
662 new_lst_loop (VEC (lst_p, heap) *seq)
664 lst_p lst = XNEW (struct lst);
668 LST_LOOP_P (lst) = true;
670 LST_LOOP_FATHER (lst) = NULL;
671 value_init (LST_LOOP_MEMORY_STRIDES (lst));
672 value_set_si (LST_LOOP_MEMORY_STRIDES (lst), -1);
674 for (i = 0; VEC_iterate (lst_p, seq, i, l); i++)
675 LST_LOOP_FATHER (l) = lst;
680 /* Creates a new LST statement with PBB. */
683 new_lst_stmt (poly_bb_p pbb)
685 lst_p lst = XNEW (struct lst);
687 LST_LOOP_P (lst) = false;
689 LST_LOOP_FATHER (lst) = NULL;
693 /* Frees the memory used by LST. */
701 if (LST_LOOP_P (lst))
706 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
709 value_clear (LST_LOOP_MEMORY_STRIDES (lst));
710 VEC_free (lst_p, heap, LST_SEQ (lst));
716 /* Returns a copy of LST. */
724 if (LST_LOOP_P (lst))
728 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
730 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
731 VEC_safe_push (lst_p, heap, seq, copy_lst (l));
733 return new_lst_loop (seq);
736 return new_lst_stmt (LST_PBB (lst));
739 /* Adds a new loop under the loop LST. */
742 lst_add_loop_under_loop (lst_p lst)
744 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 1);
745 lst_p l = new_lst_loop (LST_SEQ (lst));
747 gcc_assert (LST_LOOP_P (lst));
749 LST_LOOP_FATHER (l) = lst;
750 VEC_quick_push (lst_p, seq, l);
754 /* Returns the loop depth of LST. */
757 lst_depth (lst_p lst)
762 /* The depth of the outermost "fake" loop is -1. This outermost
763 loop does not have a loop father and it is just a container, as
764 in the loop representation of GCC. */
765 if (!LST_LOOP_FATHER (lst))
768 return lst_depth (LST_LOOP_FATHER (lst)) + 1;
771 /* Returns the Dewey number for LST. */
774 lst_dewey_number (lst_p lst)
782 if (!LST_LOOP_FATHER (lst))
785 for (i = 0; VEC_iterate (lst_p, LST_SEQ (LST_LOOP_FATHER (lst)), i, l); i++)
792 /* Returns the Dewey number of LST at depth DEPTH. */
795 lst_dewey_number_at_depth (lst_p lst, int depth)
797 gcc_assert (lst && depth >= 0 && lst_depth (lst) <= depth);
799 if (lst_depth (lst) == depth)
800 return lst_dewey_number (lst);
802 return lst_dewey_number_at_depth (LST_LOOP_FATHER (lst), depth);
805 /* Returns the predecessor of LST in the sequence of its loop father.
806 Returns NULL if LST is the first statement in the sequence. */
814 if (!lst || !LST_LOOP_FATHER (lst))
817 dewey = lst_dewey_number (lst);
821 father = LST_LOOP_FATHER (lst);
822 return VEC_index (lst_p, LST_SEQ (father), dewey - 1);
825 /* Returns the successor of LST in the sequence of its loop father.
826 Returns NULL if there is none. */
834 if (!lst || !LST_LOOP_FATHER (lst))
837 dewey = lst_dewey_number (lst);
838 father = LST_LOOP_FATHER (lst);
840 if (VEC_length (lst_p, LST_SEQ (father)) == (unsigned) dewey + 1)
843 return VEC_index (lst_p, LST_SEQ (father), dewey + 1);
847 /* Return the LST node corresponding to PBB. */
850 lst_find_pbb (lst_p lst, poly_bb_p pbb)
858 if (!LST_LOOP_P (lst))
859 return (pbb == LST_PBB (lst)) ? lst : NULL;
861 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
863 lst_p res = lst_find_pbb (l, pbb);
871 /* Return the LST node corresponding to the loop around STMT at depth
875 find_lst_loop (lst_p stmt, int loop_depth)
877 lst_p loop = LST_LOOP_FATHER (stmt);
879 gcc_assert (loop_depth >= 0);
881 while (loop_depth < lst_depth (loop))
882 loop = LST_LOOP_FATHER (loop);
887 /* Return the first lst representing a PBB statement in LST. */
890 lst_find_first_pbb (lst_p lst)
898 if (!LST_LOOP_P (lst))
901 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
903 lst_p res = lst_find_first_pbb (l);
911 /* Returns true when LST is a loop that does not contains
915 lst_empty_p (lst_p lst)
917 return !lst_find_first_pbb (lst);
920 /* Return the last lst representing a PBB statement in LST. */
923 lst_find_last_pbb (lst_p lst)
931 if (!LST_LOOP_P (lst))
934 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
936 lst_p last = lst_find_last_pbb (l);
946 /* Returns true if LOOP contains LST, in other words, if LST is nested
950 lst_contains_p (lst_p loop, lst_p lst)
952 if (!loop || !lst || !LST_LOOP_P (loop))
958 return lst_contains_p (loop, LST_LOOP_FATHER (lst));
961 /* Returns true if LOOP contains PBB, in other words, if PBB is nested
965 lst_contains_pbb (lst_p loop, poly_bb_p pbb)
967 return lst_find_pbb (loop, pbb) ? true : false;
970 /* Creates a loop nest of depth NB_LOOPS containing LST. */
973 lst_create_nest (int nb_loops, lst_p lst)
976 VEC (lst_p, heap) *seq;
981 seq = VEC_alloc (lst_p, heap, 1);
982 loop = lst_create_nest (nb_loops - 1, lst);
983 VEC_quick_push (lst_p, seq, loop);
984 res = new_lst_loop (seq);
985 LST_LOOP_FATHER (loop) = res;
990 /* Removes LST from the sequence of statements of its loop father. */
993 lst_remove_from_sequence (lst_p lst)
995 lst_p father = LST_LOOP_FATHER (lst);
996 int dewey = lst_dewey_number (lst);
998 gcc_assert (lst && father && dewey >= 0);
1000 VEC_ordered_remove (lst_p, LST_SEQ (father), dewey);
1001 LST_LOOP_FATHER (lst) = NULL;
1004 /* Updates the scattering of PBB to be at the DEWEY number in the loop
1008 pbb_update_scattering (poly_bb_p pbb, graphite_dim_t level, int dewey)
1010 ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
1011 ppl_dimension_type sched = psct_static_dim (pbb, level);
1012 ppl_dimension_type ds[1];
1013 ppl_Constraint_t new_cstr;
1014 ppl_Linear_Expression_t expr;
1015 ppl_dimension_type dim;
1017 ppl_Polyhedron_space_dimension (ph, &dim);
1019 ppl_Polyhedron_remove_space_dimensions (ph, ds, 1);
1020 ppl_insert_dimensions (ph, sched, 1);
1022 ppl_new_Linear_Expression_with_dimension (&expr, dim);
1023 ppl_set_coef (expr, sched, -1);
1024 ppl_set_inhomogeneous (expr, dewey);
1025 ppl_new_Constraint (&new_cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
1026 ppl_delete_Linear_Expression (expr);
1027 ppl_Polyhedron_add_constraint (ph, new_cstr);
1028 ppl_delete_Constraint (new_cstr);
1031 /* Updates the scattering of all the PBBs under LST to be at the DEWEY
1032 number in the loop at depth LEVEL. */
1035 lst_update_scattering_under (lst_p lst, int level, int dewey)
1040 gcc_assert (lst && level >= 0 && dewey >= 0);
1042 if (LST_LOOP_P (lst))
1043 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
1044 lst_update_scattering_under (l, level, dewey);
1046 pbb_update_scattering (LST_PBB (lst), level, dewey);
1049 /* Updates the scattering of all the PBBs under LST and in sequence
1053 lst_update_scattering_seq (lst_p lst)
1057 lst_p father = LST_LOOP_FATHER (lst);
1058 int dewey = lst_dewey_number (lst);
1059 int level = lst_depth (lst);
1061 gcc_assert (lst && father && dewey >= 0 && level >= 0);
1063 for (i = dewey; VEC_iterate (lst_p, LST_SEQ (father), i, l); i++)
1064 lst_update_scattering_under (l, level, i);
1067 /* Updates the all the scattering levels of all the PBBs under
1071 lst_update_scattering (lst_p lst)
1076 if (!lst || !LST_LOOP_P (lst))
1079 if (LST_LOOP_FATHER (lst))
1080 lst_update_scattering_seq (lst);
1082 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
1083 lst_update_scattering (l);
1086 /* Inserts LST1 before LST2 if BEFORE is true; inserts LST1 after LST2
1087 if BEFORE is false. */
1090 lst_insert_in_sequence (lst_p lst1, lst_p lst2, bool before)
1095 /* Do not insert empty loops. */
1096 if (!lst1 || lst_empty_p (lst1))
1099 father = LST_LOOP_FATHER (lst2);
1100 dewey = lst_dewey_number (lst2);
1102 gcc_assert (lst2 && father && dewey >= 0);
1104 VEC_safe_insert (lst_p, heap, LST_SEQ (father), before ? dewey : dewey + 1,
1106 LST_LOOP_FATHER (lst1) = father;
1109 /* Replaces LST1 with LST2. */
1112 lst_replace (lst_p lst1, lst_p lst2)
1117 if (!lst2 || lst_empty_p (lst2))
1120 father = LST_LOOP_FATHER (lst1);
1121 dewey = lst_dewey_number (lst1);
1122 LST_LOOP_FATHER (lst2) = father;
1123 VEC_replace (lst_p, LST_SEQ (father), dewey, lst2);
1126 /* Returns a copy of ROOT where LST has been replaced by a copy of the
1127 LSTs A B C in this sequence. */
1130 lst_substitute_3 (lst_p root, lst_p lst, lst_p a, lst_p b, lst_p c)
1134 VEC (lst_p, heap) *seq;
1139 gcc_assert (lst && root != lst);
1141 if (!LST_LOOP_P (root))
1142 return new_lst_stmt (LST_PBB (root));
1144 seq = VEC_alloc (lst_p, heap, 5);
1146 for (i = 0; VEC_iterate (lst_p, LST_SEQ (root), i, l); i++)
1148 VEC_safe_push (lst_p, heap, seq, lst_substitute_3 (l, lst, a, b, c));
1151 if (!lst_empty_p (a))
1152 VEC_safe_push (lst_p, heap, seq, copy_lst (a));
1153 if (!lst_empty_p (b))
1154 VEC_safe_push (lst_p, heap, seq, copy_lst (b));
1155 if (!lst_empty_p (c))
1156 VEC_safe_push (lst_p, heap, seq, copy_lst (c));
1159 return new_lst_loop (seq);
1162 /* Moves LST before LOOP if BEFORE is true, and after the LOOP if
1166 lst_distribute_lst (lst_p loop, lst_p lst, bool before)
1168 int loop_depth = lst_depth (loop);
1169 int depth = lst_depth (lst);
1170 int nb_loops = depth - loop_depth;
1172 gcc_assert (lst && loop && LST_LOOP_P (loop) && nb_loops > 0);
1174 lst_remove_from_sequence (lst);
1175 lst_insert_in_sequence (lst_create_nest (nb_loops, lst), loop, before);
1178 /* Removes from LOOP all the statements before/after and including PBB
1179 if BEFORE is true/false. Returns the negation of BEFORE when the
1180 statement PBB has been found. */
1183 lst_remove_all_before_including_pbb (lst_p loop, poly_bb_p pbb, bool before)
1188 if (!loop || !LST_LOOP_P (loop))
1191 for (i = 0; VEC_iterate (lst_p, LST_SEQ (loop), i, l);)
1194 before = lst_remove_all_before_including_pbb (l, pbb, before);
1196 if (VEC_length (lst_p, LST_SEQ (l)) == 0)
1198 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1208 if (LST_PBB (l) == pbb)
1211 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1214 else if (LST_PBB (l) == pbb)
1217 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1227 /* Removes from LOOP all the statements before/after and excluding PBB
1228 if BEFORE is true/false; Returns the negation of BEFORE when the
1229 statement PBB has been found. */
1232 lst_remove_all_before_excluding_pbb (lst_p loop, poly_bb_p pbb, bool before)
1237 if (!loop || !LST_LOOP_P (loop))
1240 for (i = 0; VEC_iterate (lst_p, LST_SEQ (loop), i, l);)
1243 before = lst_remove_all_before_excluding_pbb (l, pbb, before);
1245 if (VEC_length (lst_p, LST_SEQ (l)) == 0)
1247 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1256 if (before && LST_PBB (l) != pbb)
1258 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1265 if (LST_PBB (l) == pbb)
1266 before = before ? false : true;
1272 /* A SCOP is a Static Control Part of the program, simple enough to be
1273 represented in polyhedral form. */
1276 /* A SCOP is defined as a SESE region. */
1279 /* Number of parameters in SCoP. */
1280 graphite_dim_t nb_params;
1282 /* All the basic blocks in this scop that contain memory references
1283 and that will be represented as statements in the polyhedral
1285 VEC (poly_bb_p, heap) *bbs;
1287 /* Original, transformed and saved schedules. */
1288 lst_p original_schedule, transformed_schedule, saved_schedule;
1290 /* The context describes known restrictions concerning the parameters
1291 and relations in between the parameters.
1293 void f (int8_t a, uint_16_t b) {
1298 Here we can add these restrictions to the context:
1303 ppl_Pointset_Powerset_C_Polyhedron_t context;
1305 /* A hashtable of the data dependence relations for the original
1307 htab_t original_pddrs;
1309 /* True when the scop has been converted to its polyhedral
1314 #define SCOP_BBS(S) (S->bbs)
1315 #define SCOP_REGION(S) ((sese) S->region)
1316 #define SCOP_CONTEXT(S) (S->context)
1317 #define SCOP_ORIGINAL_PDDRS(S) (S->original_pddrs)
1318 #define SCOP_ORIGINAL_SCHEDULE(S) (S->original_schedule)
1319 #define SCOP_TRANSFORMED_SCHEDULE(S) (S->transformed_schedule)
1320 #define SCOP_SAVED_SCHEDULE(S) (S->saved_schedule)
1321 #define POLY_SCOP_P(S) (S->poly_scop_p)
1323 extern scop_p new_scop (void *);
1324 extern void free_scop (scop_p);
1325 extern void free_scops (VEC (scop_p, heap) *);
1326 extern void print_generated_program (FILE *, scop_p);
1327 extern void debug_generated_program (scop_p);
1328 extern void print_scattering_function (FILE *, poly_bb_p);
1329 extern void print_scattering_functions (FILE *, scop_p);
1330 extern void debug_scattering_function (poly_bb_p);
1331 extern void debug_scattering_functions (scop_p);
1332 extern int scop_max_loop_depth (scop_p);
1333 extern int unify_scattering_dimensions (scop_p);
1334 extern bool apply_poly_transforms (scop_p);
1335 extern bool graphite_legal_transform (scop_p);
1337 /* Set the region of SCOP to REGION. */
1340 scop_set_region (scop_p scop, void *region)
1342 scop->region = region;
1345 /* Returns the number of parameters for SCOP. */
1347 static inline graphite_dim_t
1348 scop_nb_params (scop_p scop)
1350 return scop->nb_params;
1353 /* Set the number of params of SCOP to NB_PARAMS. */
1356 scop_set_nb_params (scop_p scop, graphite_dim_t nb_params)
1358 scop->nb_params = nb_params;
1361 /* Allocates a new empty poly_scattering structure. */
1363 static inline poly_scattering_p
1364 poly_scattering_new (void)
1366 poly_scattering_p res = XNEW (struct poly_scattering);
1368 res->scattering = NULL;
1369 res->nb_local_variables = 0;
1370 res->nb_scattering = 0;
1374 /* Free a poly_scattering structure. */
1377 poly_scattering_free (poly_scattering_p s)
1379 ppl_delete_Polyhedron (s->scattering);
1383 /* Copies S and return a new scattering. */
1385 static inline poly_scattering_p
1386 poly_scattering_copy (poly_scattering_p s)
1388 poly_scattering_p res = poly_scattering_new ();
1390 ppl_new_C_Polyhedron_from_C_Polyhedron (&(res->scattering), s->scattering);
1391 res->nb_local_variables = s->nb_local_variables;
1392 res->nb_scattering = s->nb_scattering;
1396 /* Saves the transformed scattering of PBB. */
1399 store_scattering_pbb (poly_bb_p pbb)
1401 gcc_assert (PBB_TRANSFORMED (pbb));
1403 if (PBB_SAVED (pbb))
1404 poly_scattering_free (PBB_SAVED (pbb));
1406 PBB_SAVED (pbb) = poly_scattering_copy (PBB_TRANSFORMED (pbb));
1409 /* Stores the SCOP_TRANSFORMED_SCHEDULE to SCOP_SAVED_SCHEDULE. */
1412 store_lst_schedule (scop_p scop)
1414 if (SCOP_SAVED_SCHEDULE (scop))
1415 free_lst (SCOP_SAVED_SCHEDULE (scop));
1417 SCOP_SAVED_SCHEDULE (scop) = copy_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
1420 /* Restores the SCOP_TRANSFORMED_SCHEDULE from SCOP_SAVED_SCHEDULE. */
1423 restore_lst_schedule (scop_p scop)
1425 if (SCOP_TRANSFORMED_SCHEDULE (scop))
1426 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
1428 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (SCOP_SAVED_SCHEDULE (scop));
1431 /* Saves the scattering for all the pbbs in the SCOP. */
1434 store_scattering (scop_p scop)
1439 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1440 store_scattering_pbb (pbb);
1442 store_lst_schedule (scop);
1445 /* Restores the scattering of PBB. */
1448 restore_scattering_pbb (poly_bb_p pbb)
1450 gcc_assert (PBB_SAVED (pbb));
1452 poly_scattering_free (PBB_TRANSFORMED (pbb));
1453 PBB_TRANSFORMED (pbb) = poly_scattering_copy (PBB_SAVED (pbb));
1456 /* Restores the scattering for all the pbbs in the SCOP. */
1459 restore_scattering (scop_p scop)
1464 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1465 restore_scattering_pbb (pbb);
1467 restore_lst_schedule (scop);