Fix DealII type problems.
[official-gcc/Ramakrishna.git] / gcc / graphite-poly.h
bloba2969c772163570dd4fab19573d981acee931710
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)
11 any later version.
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;
26 DEF_VEC_P(poly_dr_p);
27 DEF_VEC_ALLOC_P (poly_dr_p, heap);
29 typedef struct poly_bb *poly_bb_p;
30 DEF_VEC_P(poly_bb_p);
31 DEF_VEC_ALLOC_P (poly_bb_p, heap);
33 typedef struct scop *scop_p;
34 DEF_VEC_P(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. */
45 enum poly_dr_type
47 PDR_READ,
48 /* PDR_MAY_READs are represented using PDR_READS. This does not
49 limit the expressiveness. */
50 PDR_WRITE,
51 PDR_MAY_WRITE
54 struct poly_dr
56 /* An identifier for this PDR. */
57 int id;
59 /* The number of data refs identical to this one in the PBB. */
60 int nb_refs;
62 /* A pointer to compiler's data reference description. */
63 void *compiler_dr;
65 /* A pointer to the PBB that contains this data reference. */
66 poly_bb_p pbb;
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:
75 - The alias set (a):
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:
84 - pdr_alias_set_dim
85 - pdr_subscript_dim
86 - pdr_iterator_dim
87 - pdr_parameter_dim
89 Example:
91 | int A[1335][123];
92 | int *p = malloc ();
94 | k = ...
95 | for i
96 | {
97 | if (unknown_function ())
98 | p = A;
99 | ... = p[?][?];
100 | for j
101 | A[i][j+k] = m;
104 The data access A[i][j+k] in alias set "5" is described like this:
106 | i j k a s0 s1 1
107 | 0 0 0 1 0 0 -5 = 0
108 |-1 0 0 0 1 0 0 = 0
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
116 polyhedron:
119 | i k a s0 1
120 | 0 0 1 0 -5 = 0
121 | 0 0 0 1 0 >= 0
123 "or"
125 | i k a s0 1
126 | 0 0 1 0 -7 = 0
127 | 0 0 0 1 0 >= 0
129 "*p" accesses all of the object allocated with 'malloc'.
131 The scalar data access "m" is represented as an array with zero subscript
132 dimensions.
134 | i j k a 1
135 | 0 0 0 -1 15 = 0 */
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),
169 &dim);
170 return dim;
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)
214 return 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". */
229 static inline bool
230 pdr_read_p (poly_dr_p pdr)
232 return PDR_TYPE (pdr) == PDR_READ;
235 /* Returns true when PDR is a "write". */
237 static inline bool
238 pdr_write_p (poly_dr_p pdr)
240 return PDR_TYPE (pdr) == PDR_WRITE;
243 /* Returns true when PDR is a "may write". */
245 static inline bool
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. */
254 static inline bool
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. */
273 int nb_scattering;
276 /* POLY_BB represents a blackbox in the polyhedral model. */
278 struct poly_bb
280 void *black_box;
282 scop_p scop;
284 /* The iteration domain of this bb.
285 Example:
287 for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
288 for (j = 2; j <= 2*i + 5; j++)
289 for (k = 0; k <= 5; k++)
290 S (i,j,k)
292 Loop iterators: i, j, k
293 Parameters: a, b
295 | i >= a - 7b + 8
296 | i <= 3a + 13b + 20
297 | j >= 2
298 | j <= 2i + 5
299 | k >= 0
300 | k <= 5
302 The number of variables in the DOMAIN may change and is not
303 related to the number of loops in the original code. */
304 ppl_Pointset_Powerset_C_Polyhedron_t domain;
306 /* The data references we access. */
307 VEC (poly_dr_p, heap) *drs;
309 /* The original scattering. */
310 poly_scattering_p original;
312 /* The transformed scattering. */
313 poly_scattering_p transformed;
315 /* A copy of the transformed scattering. */
316 poly_scattering_p saved;
318 /* True when the PDR duplicates have already been removed. */
319 bool pdr_duplicates_removed;
321 /* True when this PBB contains only a reduction statement. */
322 bool is_reduction;
325 #define PBB_BLACK_BOX(PBB) ((gimple_bb_p) PBB->black_box)
326 #define PBB_SCOP(PBB) (PBB->scop)
327 #define PBB_DOMAIN(PBB) (PBB->domain)
328 #define PBB_DRS(PBB) (PBB->drs)
329 #define PBB_ORIGINAL(PBB) (PBB->original)
330 #define PBB_ORIGINAL_SCATTERING(PBB) (PBB->original->scattering)
331 #define PBB_TRANSFORMED(PBB) (PBB->transformed)
332 #define PBB_TRANSFORMED_SCATTERING(PBB) (PBB->transformed->scattering)
333 #define PBB_SAVED(PBB) (PBB->saved)
334 #define PBB_NB_LOCAL_VARIABLES(PBB) (PBB->transformed->nb_local_variables)
335 #define PBB_NB_SCATTERING_TRANSFORM(PBB) (PBB->transformed->nb_scattering)
336 #define PBB_PDR_DUPLICATES_REMOVED(PBB) (PBB->pdr_duplicates_removed)
337 #define PBB_IS_REDUCTION(PBB) (PBB->is_reduction)
339 extern void new_poly_bb (scop_p, void *, bool);
340 extern void free_poly_bb (poly_bb_p);
341 extern void debug_loop_vec (poly_bb_p);
342 extern void schedule_to_scattering (poly_bb_p, int);
343 extern void print_pbb_domain (FILE *, poly_bb_p);
344 extern void print_pbb (FILE *, poly_bb_p);
345 extern void print_scop_context (FILE *, scop_p);
346 extern void print_scop (FILE *, scop_p);
347 extern void debug_pbb_domain (poly_bb_p);
348 extern void debug_pbb (poly_bb_p);
349 extern void print_pdrs (FILE *, poly_bb_p);
350 extern void debug_pdrs (poly_bb_p);
351 extern void debug_scop_context (scop_p);
352 extern void debug_scop (scop_p);
353 extern void print_scop_params (FILE *, scop_p);
354 extern void debug_scop_params (scop_p);
355 extern void print_iteration_domain (FILE *, poly_bb_p);
356 extern void print_iteration_domains (FILE *, scop_p);
357 extern void debug_iteration_domain (poly_bb_p);
358 extern void debug_iteration_domains (scop_p);
359 extern bool scop_do_interchange (scop_p);
360 extern bool scop_do_strip_mine (scop_p);
361 extern bool scop_do_block (scop_p);
362 extern void pbb_number_of_iterations (poly_bb_p, graphite_dim_t, Value);
363 extern void pbb_number_of_iterations_at_time (poly_bb_p, graphite_dim_t, Value);
364 extern void pbb_remove_duplicate_pdrs (poly_bb_p);
366 /* Return the number of write data references in PBB. */
368 static inline int
369 number_of_write_pdrs (poly_bb_p pbb)
371 int res = 0;
372 int i;
373 poly_dr_p pdr;
375 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
376 if (PDR_TYPE (pdr) == PDR_WRITE)
377 res++;
379 return res;
382 /* The index of the PBB. */
384 static inline int
385 pbb_index (poly_bb_p pbb)
387 return GBB_BB (PBB_BLACK_BOX (pbb))->index;
390 /* The loop of the PBB. */
392 static inline loop_p
393 pbb_loop (poly_bb_p pbb)
395 return gbb_loop (PBB_BLACK_BOX (pbb));
398 /* The scop that contains the PDR. */
400 static inline scop_p
401 pdr_scop (poly_dr_p pdr)
403 return PBB_SCOP (PDR_PBB (pdr));
406 /* Set black box of PBB to BLACKBOX. */
408 static inline void
409 pbb_set_black_box (poly_bb_p pbb, void *black_box)
411 pbb->black_box = black_box;
414 /* The number of loops around PBB: the dimension of the iteration
415 domain. */
417 static inline graphite_dim_t
418 pbb_dim_iter_domain (const struct poly_bb *pbb)
420 scop_p scop = PBB_SCOP (pbb);
421 ppl_dimension_type dim;
423 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
424 return dim - scop_nb_params (scop);
427 /* The number of params defined in PBB. */
429 static inline graphite_dim_t
430 pbb_nb_params (const struct poly_bb *pbb)
432 scop_p scop = PBB_SCOP (pbb);
434 return scop_nb_params (scop);
437 /* The number of scattering dimensions in the SCATTERING polyhedron
438 of a PBB for a given SCOP. */
440 static inline graphite_dim_t
441 pbb_nb_scattering_orig (const struct poly_bb *pbb)
443 return 2 * pbb_dim_iter_domain (pbb) + 1;
446 /* The number of scattering dimensions in PBB. */
448 static inline graphite_dim_t
449 pbb_nb_scattering_transform (const struct poly_bb *pbb)
451 return PBB_NB_SCATTERING_TRANSFORM (pbb);
454 /* The number of dynamic scattering dimensions in PBB. */
456 static inline graphite_dim_t
457 pbb_nb_dynamic_scattering_transform (const struct poly_bb *pbb)
459 /* This function requires the 2d + 1 scattering format to be
460 invariant during all transformations. */
461 gcc_assert (PBB_NB_SCATTERING_TRANSFORM (pbb) % 2);
462 return PBB_NB_SCATTERING_TRANSFORM (pbb) / 2;
465 /* Returns the number of local variables used in the transformed
466 scattering polyhedron of PBB. */
468 static inline graphite_dim_t
469 pbb_nb_local_vars (const struct poly_bb *pbb)
471 /* For now we do not have any local variables, as we do not do strip
472 mining for example. */
473 return PBB_NB_LOCAL_VARIABLES (pbb);
476 /* The dimension in the domain of PBB containing the iterator ITER. */
478 static inline ppl_dimension_type
479 pbb_iterator_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t iter)
481 return iter;
484 /* The dimension in the domain of PBB containing the iterator ITER. */
486 static inline ppl_dimension_type
487 pbb_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
489 return param
490 + pbb_dim_iter_domain (pbb);
493 /* The dimension in the original scattering polyhedron of PBB
494 containing the scattering iterator SCATTER. */
496 static inline ppl_dimension_type
497 psco_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
499 gcc_assert (scatter < pbb_nb_scattering_orig (pbb));
500 return scatter;
503 /* The dimension in the transformed scattering polyhedron of PBB
504 containing the scattering iterator SCATTER. */
506 static inline ppl_dimension_type
507 psct_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
509 gcc_assert (scatter <= pbb_nb_scattering_transform (pbb));
510 return scatter;
513 ppl_dimension_type psct_scattering_dim_for_loop_depth (poly_bb_p,
514 graphite_dim_t);
516 /* The dimension in the transformed scattering polyhedron of PBB of
517 the local variable LV. */
519 static inline ppl_dimension_type
520 psct_local_var_dim (poly_bb_p pbb, graphite_dim_t lv)
522 gcc_assert (lv <= pbb_nb_local_vars (pbb));
523 return lv + pbb_nb_scattering_transform (pbb);
526 /* The dimension in the original scattering polyhedron of PBB
527 containing the loop iterator ITER. */
529 static inline ppl_dimension_type
530 psco_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
532 gcc_assert (iter < pbb_dim_iter_domain (pbb));
533 return iter + pbb_nb_scattering_orig (pbb);
536 /* The dimension in the transformed scattering polyhedron of PBB
537 containing the loop iterator ITER. */
539 static inline ppl_dimension_type
540 psct_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
542 gcc_assert (iter < pbb_dim_iter_domain (pbb));
543 return iter
544 + pbb_nb_scattering_transform (pbb)
545 + pbb_nb_local_vars (pbb);
548 /* The dimension in the original scattering polyhedron of PBB
549 containing parameter PARAM. */
551 static inline ppl_dimension_type
552 psco_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
554 gcc_assert (param < pbb_nb_params (pbb));
555 return param
556 + pbb_nb_scattering_orig (pbb)
557 + pbb_dim_iter_domain (pbb);
560 /* The dimension in the transformed scattering polyhedron of PBB
561 containing parameter PARAM. */
563 static inline ppl_dimension_type
564 psct_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
566 gcc_assert (param < pbb_nb_params (pbb));
567 return param
568 + pbb_nb_scattering_transform (pbb)
569 + pbb_nb_local_vars (pbb)
570 + pbb_dim_iter_domain (pbb);
573 /* The scattering dimension of PBB corresponding to the dynamic level
574 LEVEL. */
576 static inline ppl_dimension_type
577 psct_dynamic_dim (poly_bb_p pbb, graphite_dim_t level)
579 graphite_dim_t result = 1 + 2 * level;
581 gcc_assert (result < pbb_nb_scattering_transform (pbb));
582 return result;
585 /* The scattering dimension of PBB corresponding to the static
586 sequence of the loop level LEVEL. */
588 static inline ppl_dimension_type
589 psct_static_dim (poly_bb_p pbb, graphite_dim_t level)
591 graphite_dim_t result = 2 * level;
593 gcc_assert (result < pbb_nb_scattering_transform (pbb));
594 return result;
597 /* Adds to the transformed scattering polyhedron of PBB a new local
598 variable and returns its index. */
600 static inline graphite_dim_t
601 psct_add_local_variable (poly_bb_p pbb)
603 graphite_dim_t nlv = pbb_nb_local_vars (pbb);
604 ppl_dimension_type lv_column = psct_local_var_dim (pbb, nlv);
605 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), lv_column, 1);
606 PBB_NB_LOCAL_VARIABLES (pbb) += 1;
607 return nlv;
610 /* Adds a dimension to the transformed scattering polyhedron of PBB at
611 INDEX. */
613 static inline void
614 psct_add_scattering_dimension (poly_bb_p pbb, ppl_dimension_type index)
616 gcc_assert (index < pbb_nb_scattering_transform (pbb));
618 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), index, 1);
619 PBB_NB_SCATTERING_TRANSFORM (pbb) += 1;
622 typedef struct lst *lst_p;
623 DEF_VEC_P(lst_p);
624 DEF_VEC_ALLOC_P (lst_p, heap);
626 /* Loops and Statements Tree. */
627 struct lst {
629 /* LOOP_P is true when an LST node is a loop. */
630 bool loop_p;
632 /* A pointer to the loop that contains this node. */
633 lst_p loop_father;
635 /* The sum of all the memory strides for an LST loop. */
636 Value memory_strides;
638 /* Loop nodes contain a sequence SEQ of LST nodes, statements
639 contain a pointer to their polyhedral representation PBB. */
640 union {
641 poly_bb_p pbb;
642 VEC (lst_p, heap) *seq;
643 } node;
646 #define LST_LOOP_P(LST) ((LST)->loop_p)
647 #define LST_LOOP_FATHER(LST) ((LST)->loop_father)
648 #define LST_PBB(LST) ((LST)->node.pbb)
649 #define LST_SEQ(LST) ((LST)->node.seq)
650 #define LST_LOOP_MEMORY_STRIDES(LST) ((LST)->memory_strides)
652 void scop_to_lst (scop_p);
653 void print_lst (FILE *, lst_p, int);
654 void debug_lst (lst_p);
655 void dot_lst (lst_p);
657 /* Creates a new LST loop with SEQ. */
659 static inline lst_p
660 new_lst_loop (VEC (lst_p, heap) *seq)
662 lst_p lst = XNEW (struct lst);
663 int i;
664 lst_p l;
666 LST_LOOP_P (lst) = true;
667 LST_SEQ (lst) = seq;
668 LST_LOOP_FATHER (lst) = NULL;
669 value_init (LST_LOOP_MEMORY_STRIDES (lst));
670 value_set_si (LST_LOOP_MEMORY_STRIDES (lst), -1);
672 for (i = 0; VEC_iterate (lst_p, seq, i, l); i++)
673 LST_LOOP_FATHER (l) = lst;
675 return lst;
678 /* Creates a new LST statement with PBB. */
680 static inline lst_p
681 new_lst_stmt (poly_bb_p pbb)
683 lst_p lst = XNEW (struct lst);
685 LST_LOOP_P (lst) = false;
686 LST_PBB (lst) = pbb;
687 LST_LOOP_FATHER (lst) = NULL;
688 return lst;
691 /* Frees the memory used by LST. */
693 static inline void
694 free_lst (lst_p lst)
696 if (!lst)
697 return;
699 if (LST_LOOP_P (lst))
701 int i;
702 lst_p l;
704 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
705 free_lst (l);
707 value_clear (LST_LOOP_MEMORY_STRIDES (lst));
708 VEC_free (lst_p, heap, LST_SEQ (lst));
711 free (lst);
714 /* Returns a copy of LST. */
716 static inline lst_p
717 copy_lst (lst_p lst)
719 if (!lst)
720 return NULL;
722 if (LST_LOOP_P (lst))
724 int i;
725 lst_p l;
726 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
728 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
729 VEC_safe_push (lst_p, heap, seq, copy_lst (l));
731 return new_lst_loop (seq);
734 return new_lst_stmt (LST_PBB (lst));
737 /* Adds a new loop under the loop LST. */
739 static inline void
740 lst_add_loop_under_loop (lst_p lst)
742 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 1);
743 lst_p l = new_lst_loop (LST_SEQ (lst));
745 gcc_assert (LST_LOOP_P (lst));
747 LST_LOOP_FATHER (l) = lst;
748 VEC_quick_push (lst_p, seq, l);
749 LST_SEQ (lst) = seq;
752 /* Returns the loop depth of LST. */
754 static inline int
755 lst_depth (lst_p lst)
757 if (!lst)
758 return -2;
760 /* The depth of the outermost "fake" loop is -1. This outermost
761 loop does not have a loop father and it is just a container, as
762 in the loop representation of GCC. */
763 if (!LST_LOOP_FATHER (lst))
764 return -1;
766 return lst_depth (LST_LOOP_FATHER (lst)) + 1;
769 /* Returns the Dewey number for LST. */
771 static inline int
772 lst_dewey_number (lst_p lst)
774 int i;
775 lst_p l;
777 if (!lst)
778 return -1;
780 if (!LST_LOOP_FATHER (lst))
781 return 0;
783 for (i = 0; VEC_iterate (lst_p, LST_SEQ (LST_LOOP_FATHER (lst)), i, l); i++)
784 if (l == lst)
785 return i;
787 return -1;
790 /* Returns the Dewey number of LST at depth DEPTH. */
792 static inline int
793 lst_dewey_number_at_depth (lst_p lst, int depth)
795 gcc_assert (lst && depth >= 0 && lst_depth (lst) <= depth);
797 if (lst_depth (lst) == depth)
798 return lst_dewey_number (lst);
800 return lst_dewey_number_at_depth (LST_LOOP_FATHER (lst), depth);
803 /* Returns the predecessor of LST in the sequence of its loop father.
804 Returns NULL if LST is the first statement in the sequence. */
806 static inline lst_p
807 lst_pred (lst_p lst)
809 int dewey;
810 lst_p father;
812 if (!lst || !LST_LOOP_FATHER (lst))
813 return NULL;
815 dewey = lst_dewey_number (lst);
816 if (dewey == 0)
817 return NULL;
819 father = LST_LOOP_FATHER (lst);
820 return VEC_index (lst_p, LST_SEQ (father), dewey - 1);
823 /* Returns the successor of LST in the sequence of its loop father.
824 Returns NULL if there is none. */
826 static inline lst_p
827 lst_succ (lst_p lst)
829 int dewey;
830 lst_p father;
832 if (!lst || !LST_LOOP_FATHER (lst))
833 return NULL;
835 dewey = lst_dewey_number (lst);
836 father = LST_LOOP_FATHER (lst);
838 if (VEC_length (lst_p, LST_SEQ (father)) == (unsigned) dewey + 1)
839 return NULL;
841 return VEC_index (lst_p, LST_SEQ (father), dewey + 1);
845 /* Return the LST node corresponding to PBB. */
847 static inline lst_p
848 lst_find_pbb (lst_p lst, poly_bb_p pbb)
850 int i;
851 lst_p l;
853 if (!lst)
854 return NULL;
856 if (!LST_LOOP_P (lst))
857 return (pbb == LST_PBB (lst)) ? lst : NULL;
859 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
861 lst_p res = lst_find_pbb (l, pbb);
862 if (res)
863 return res;
866 return NULL;
869 /* Return the LST node corresponding to the loop around STMT at depth
870 LOOP_DEPTH. */
872 static inline lst_p
873 find_lst_loop (lst_p stmt, int loop_depth)
875 lst_p loop = LST_LOOP_FATHER (stmt);
877 gcc_assert (loop_depth >= 0);
879 while (loop_depth < lst_depth (loop))
880 loop = LST_LOOP_FATHER (loop);
882 return loop;
885 /* Return the first lst representing a PBB statement in LST. */
887 static inline lst_p
888 lst_find_first_pbb (lst_p lst)
890 int i;
891 lst_p l;
893 if (!lst)
894 return NULL;
896 if (!LST_LOOP_P (lst))
897 return lst;
899 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
901 lst_p res = lst_find_first_pbb (l);
902 if (res)
903 return res;
906 return NULL;
909 /* Returns true when LST is a loop that does not contains
910 statements. */
912 static inline bool
913 lst_empty_p (lst_p lst)
915 return !lst_find_first_pbb (lst);
918 /* Return the last lst representing a PBB statement in LST. */
920 static inline lst_p
921 lst_find_last_pbb (lst_p lst)
923 int i;
924 lst_p l, res = NULL;
926 if (!lst)
927 return NULL;
929 if (!LST_LOOP_P (lst))
930 return lst;
932 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
934 lst_p last = lst_find_last_pbb (l);
936 if (last)
937 res = last;
940 gcc_assert (res);
941 return res;
944 /* Returns true if LOOP contains LST, in other words, if LST is nested
945 in LOOP. */
947 static inline bool
948 lst_contains_p (lst_p loop, lst_p lst)
950 if (!loop || !lst || !LST_LOOP_P (loop))
951 return false;
953 if (loop == lst)
954 return true;
956 return lst_contains_p (loop, LST_LOOP_FATHER (lst));
959 /* Returns true if LOOP contains PBB, in other words, if PBB is nested
960 in LOOP. */
962 static inline bool
963 lst_contains_pbb (lst_p loop, poly_bb_p pbb)
965 return lst_find_pbb (loop, pbb) ? true : false;
968 /* Creates a loop nest of depth NB_LOOPS containing LST. */
970 static inline lst_p
971 lst_create_nest (int nb_loops, lst_p lst)
973 lst_p res, loop;
974 VEC (lst_p, heap) *seq;
976 if (nb_loops == 0)
977 return lst;
979 seq = VEC_alloc (lst_p, heap, 1);
980 loop = lst_create_nest (nb_loops - 1, lst);
981 VEC_quick_push (lst_p, seq, loop);
982 res = new_lst_loop (seq);
983 LST_LOOP_FATHER (loop) = res;
985 return res;
988 /* Removes LST from the sequence of statements of its loop father. */
990 static inline void
991 lst_remove_from_sequence (lst_p lst)
993 lst_p father = LST_LOOP_FATHER (lst);
994 int dewey = lst_dewey_number (lst);
996 gcc_assert (lst && father && dewey >= 0);
998 VEC_ordered_remove (lst_p, LST_SEQ (father), dewey);
999 LST_LOOP_FATHER (lst) = NULL;
1002 /* Updates the scattering of PBB to be at the DEWEY number in the loop
1003 at depth LEVEL. */
1005 static inline void
1006 pbb_update_scattering (poly_bb_p pbb, graphite_dim_t level, int dewey)
1008 ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
1009 ppl_dimension_type sched = psct_static_dim (pbb, level);
1010 ppl_dimension_type ds[1];
1011 ppl_Constraint_t new_cstr;
1012 ppl_Linear_Expression_t expr;
1013 ppl_dimension_type dim;
1015 ppl_Polyhedron_space_dimension (ph, &dim);
1016 ds[0] = sched;
1017 ppl_Polyhedron_remove_space_dimensions (ph, ds, 1);
1018 ppl_insert_dimensions (ph, sched, 1);
1020 ppl_new_Linear_Expression_with_dimension (&expr, dim);
1021 ppl_set_coef (expr, sched, -1);
1022 ppl_set_inhomogeneous (expr, dewey);
1023 ppl_new_Constraint (&new_cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
1024 ppl_delete_Linear_Expression (expr);
1025 ppl_Polyhedron_add_constraint (ph, new_cstr);
1026 ppl_delete_Constraint (new_cstr);
1029 /* Updates the scattering of all the PBBs under LST to be at the DEWEY
1030 number in the loop at depth LEVEL. */
1032 static inline void
1033 lst_update_scattering_under (lst_p lst, int level, int dewey)
1035 int i;
1036 lst_p l;
1038 gcc_assert (lst && level >= 0 && dewey >= 0);
1040 if (LST_LOOP_P (lst))
1041 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
1042 lst_update_scattering_under (l, level, dewey);
1043 else
1044 pbb_update_scattering (LST_PBB (lst), level, dewey);
1047 /* Updates the scattering of all the PBBs under LST and in sequence
1048 with LST. */
1050 static inline void
1051 lst_update_scattering_seq (lst_p lst)
1053 int i;
1054 lst_p l;
1055 lst_p father = LST_LOOP_FATHER (lst);
1056 int dewey = lst_dewey_number (lst);
1057 int level = lst_depth (lst);
1059 gcc_assert (lst && father && dewey >= 0 && level >= 0);
1061 for (i = dewey; VEC_iterate (lst_p, LST_SEQ (father), i, l); i++)
1062 lst_update_scattering_under (l, level, i);
1065 /* Updates the all the scattering levels of all the PBBs under
1066 LST. */
1068 static inline void
1069 lst_update_scattering (lst_p lst)
1071 int i;
1072 lst_p l;
1074 if (!lst || !LST_LOOP_P (lst))
1075 return;
1077 if (LST_LOOP_FATHER (lst))
1078 lst_update_scattering_seq (lst);
1080 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
1081 lst_update_scattering (l);
1084 /* Inserts LST1 before LST2 if BEFORE is true; inserts LST1 after LST2
1085 if BEFORE is false. */
1087 static inline void
1088 lst_insert_in_sequence (lst_p lst1, lst_p lst2, bool before)
1090 lst_p father;
1091 int dewey;
1093 /* Do not insert empty loops. */
1094 if (!lst1 || lst_empty_p (lst1))
1095 return;
1097 father = LST_LOOP_FATHER (lst2);
1098 dewey = lst_dewey_number (lst2);
1100 gcc_assert (lst2 && father && dewey >= 0);
1102 VEC_safe_insert (lst_p, heap, LST_SEQ (father), before ? dewey : dewey + 1,
1103 lst1);
1104 LST_LOOP_FATHER (lst1) = father;
1107 /* Replaces LST1 with LST2. */
1109 static inline void
1110 lst_replace (lst_p lst1, lst_p lst2)
1112 lst_p father;
1113 int dewey;
1115 if (!lst2 || lst_empty_p (lst2))
1116 return;
1118 father = LST_LOOP_FATHER (lst1);
1119 dewey = lst_dewey_number (lst1);
1120 LST_LOOP_FATHER (lst2) = father;
1121 VEC_replace (lst_p, LST_SEQ (father), dewey, lst2);
1124 /* Returns a copy of ROOT where LST has been replaced by a copy of the
1125 LSTs A B C in this sequence. */
1127 static inline lst_p
1128 lst_substitute_3 (lst_p root, lst_p lst, lst_p a, lst_p b, lst_p c)
1130 int i;
1131 lst_p l;
1132 VEC (lst_p, heap) *seq;
1134 if (!root)
1135 return NULL;
1137 gcc_assert (lst && root != lst);
1139 if (!LST_LOOP_P (root))
1140 return new_lst_stmt (LST_PBB (root));
1142 seq = VEC_alloc (lst_p, heap, 5);
1144 for (i = 0; VEC_iterate (lst_p, LST_SEQ (root), i, l); i++)
1145 if (l != lst)
1146 VEC_safe_push (lst_p, heap, seq, lst_substitute_3 (l, lst, a, b, c));
1147 else
1149 if (!lst_empty_p (a))
1150 VEC_safe_push (lst_p, heap, seq, copy_lst (a));
1151 if (!lst_empty_p (b))
1152 VEC_safe_push (lst_p, heap, seq, copy_lst (b));
1153 if (!lst_empty_p (c))
1154 VEC_safe_push (lst_p, heap, seq, copy_lst (c));
1157 return new_lst_loop (seq);
1160 /* Moves LST before LOOP if BEFORE is true, and after the LOOP if
1161 BEFORE is false. */
1163 static inline void
1164 lst_distribute_lst (lst_p loop, lst_p lst, bool before)
1166 int loop_depth = lst_depth (loop);
1167 int depth = lst_depth (lst);
1168 int nb_loops = depth - loop_depth;
1170 gcc_assert (lst && loop && LST_LOOP_P (loop) && nb_loops > 0);
1172 lst_remove_from_sequence (lst);
1173 lst_insert_in_sequence (lst_create_nest (nb_loops, lst), loop, before);
1176 /* Removes from LOOP all the statements before/after and including PBB
1177 if BEFORE is true/false. Returns the negation of BEFORE when the
1178 statement PBB has been found. */
1180 static inline bool
1181 lst_remove_all_before_including_pbb (lst_p loop, poly_bb_p pbb, bool before)
1183 int i;
1184 lst_p l;
1186 if (!loop || !LST_LOOP_P (loop))
1187 return before;
1189 for (i = 0; VEC_iterate (lst_p, LST_SEQ (loop), i, l);)
1190 if (LST_LOOP_P (l))
1192 before = lst_remove_all_before_including_pbb (l, pbb, before);
1194 if (VEC_length (lst_p, LST_SEQ (l)) == 0)
1196 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1197 free_lst (l);
1199 else
1200 i++;
1202 else
1204 if (before)
1206 if (LST_PBB (l) == pbb)
1207 before = false;
1209 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1210 free_lst (l);
1212 else if (LST_PBB (l) == pbb)
1214 before = true;
1215 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1216 free_lst (l);
1218 else
1219 i++;
1222 return before;
1225 /* Removes from LOOP all the statements before/after and excluding PBB
1226 if BEFORE is true/false; Returns the negation of BEFORE when the
1227 statement PBB has been found. */
1229 static inline bool
1230 lst_remove_all_before_excluding_pbb (lst_p loop, poly_bb_p pbb, bool before)
1232 int i;
1233 lst_p l;
1235 if (!loop || !LST_LOOP_P (loop))
1236 return before;
1238 for (i = 0; VEC_iterate (lst_p, LST_SEQ (loop), i, l);)
1239 if (LST_LOOP_P (l))
1241 before = lst_remove_all_before_excluding_pbb (l, pbb, before);
1243 if (VEC_length (lst_p, LST_SEQ (l)) == 0)
1245 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1246 free_lst (l);
1247 continue;
1250 i++;
1252 else
1254 if (before && LST_PBB (l) != pbb)
1256 VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
1257 free_lst (l);
1258 continue;
1261 i++;
1263 if (LST_PBB (l) == pbb)
1264 before = before ? false : true;
1267 return before;
1270 /* A SCOP is a Static Control Part of the program, simple enough to be
1271 represented in polyhedral form. */
1272 struct scop
1274 /* A SCOP is defined as a SESE region. */
1275 void *region;
1277 /* Number of parameters in SCoP. */
1278 graphite_dim_t nb_params;
1280 /* All the basic blocks in this scop that contain memory references
1281 and that will be represented as statements in the polyhedral
1282 representation. */
1283 VEC (poly_bb_p, heap) *bbs;
1285 /* Original, transformed and saved schedules. */
1286 lst_p original_schedule, transformed_schedule, saved_schedule;
1288 /* The context describes known restrictions concerning the parameters
1289 and relations in between the parameters.
1291 void f (int8_t a, uint_16_t b) {
1292 c = 2 a + b;
1296 Here we can add these restrictions to the context:
1298 -128 >= a >= 127
1299 0 >= b >= 65,535
1300 c = 2a + b */
1301 ppl_Pointset_Powerset_C_Polyhedron_t context;
1303 /* A hashtable of the data dependence relations for the original
1304 scattering. */
1305 htab_t original_pddrs;
1308 #define SCOP_BBS(S) (S->bbs)
1309 #define SCOP_REGION(S) ((sese) S->region)
1310 #define SCOP_CONTEXT(S) (S->context)
1311 #define SCOP_ORIGINAL_PDDRS(S) (S->original_pddrs)
1312 #define SCOP_ORIGINAL_SCHEDULE(S) (S->original_schedule)
1313 #define SCOP_TRANSFORMED_SCHEDULE(S) (S->transformed_schedule)
1314 #define SCOP_SAVED_SCHEDULE(S) (S->saved_schedule)
1316 extern scop_p new_scop (void *);
1317 extern void free_scop (scop_p);
1318 extern void free_scops (VEC (scop_p, heap) *);
1319 extern void print_generated_program (FILE *, scop_p);
1320 extern void debug_generated_program (scop_p);
1321 extern void print_scattering_function (FILE *, poly_bb_p);
1322 extern void print_scattering_functions (FILE *, scop_p);
1323 extern void debug_scattering_function (poly_bb_p);
1324 extern void debug_scattering_functions (scop_p);
1325 extern int scop_max_loop_depth (scop_p);
1326 extern int unify_scattering_dimensions (scop_p);
1327 extern bool apply_poly_transforms (scop_p);
1328 extern bool graphite_legal_transform (scop_p);
1330 /* Set the region of SCOP to REGION. */
1332 static inline void
1333 scop_set_region (scop_p scop, void *region)
1335 scop->region = region;
1338 /* Returns the number of parameters for SCOP. */
1340 static inline graphite_dim_t
1341 scop_nb_params (scop_p scop)
1343 return scop->nb_params;
1346 /* Set the number of params of SCOP to NB_PARAMS. */
1348 static inline void
1349 scop_set_nb_params (scop_p scop, graphite_dim_t nb_params)
1351 scop->nb_params = nb_params;
1354 /* Allocates a new empty poly_scattering structure. */
1356 static inline poly_scattering_p
1357 poly_scattering_new (void)
1359 poly_scattering_p res = XNEW (struct poly_scattering);
1361 res->scattering = NULL;
1362 res->nb_local_variables = 0;
1363 res->nb_scattering = 0;
1364 return res;
1367 /* Free a poly_scattering structure. */
1369 static inline void
1370 poly_scattering_free (poly_scattering_p s)
1372 ppl_delete_Polyhedron (s->scattering);
1373 free (s);
1376 /* Copies S and return a new scattering. */
1378 static inline poly_scattering_p
1379 poly_scattering_copy (poly_scattering_p s)
1381 poly_scattering_p res = poly_scattering_new ();
1383 ppl_new_C_Polyhedron_from_C_Polyhedron (&(res->scattering), s->scattering);
1384 res->nb_local_variables = s->nb_local_variables;
1385 res->nb_scattering = s->nb_scattering;
1386 return res;
1389 /* Saves the transformed scattering of PBB. */
1391 static inline void
1392 store_scattering_pbb (poly_bb_p pbb)
1394 gcc_assert (PBB_TRANSFORMED (pbb));
1396 if (PBB_SAVED (pbb))
1397 poly_scattering_free (PBB_SAVED (pbb));
1399 PBB_SAVED (pbb) = poly_scattering_copy (PBB_TRANSFORMED (pbb));
1402 /* Stores the SCOP_TRANSFORMED_SCHEDULE to SCOP_SAVED_SCHEDULE. */
1404 static inline void
1405 store_lst_schedule (scop_p scop)
1407 if (SCOP_SAVED_SCHEDULE (scop))
1408 free_lst (SCOP_SAVED_SCHEDULE (scop));
1410 SCOP_SAVED_SCHEDULE (scop) = copy_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
1413 /* Restores the SCOP_TRANSFORMED_SCHEDULE from SCOP_SAVED_SCHEDULE. */
1415 static inline void
1416 restore_lst_schedule (scop_p scop)
1418 if (SCOP_TRANSFORMED_SCHEDULE (scop))
1419 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
1421 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (SCOP_SAVED_SCHEDULE (scop));
1424 /* Saves the scattering for all the pbbs in the SCOP. */
1426 static inline void
1427 store_scattering (scop_p scop)
1429 int i;
1430 poly_bb_p pbb;
1432 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1433 store_scattering_pbb (pbb);
1435 store_lst_schedule (scop);
1438 /* Restores the scattering of PBB. */
1440 static inline void
1441 restore_scattering_pbb (poly_bb_p pbb)
1443 gcc_assert (PBB_SAVED (pbb));
1445 poly_scattering_free (PBB_TRANSFORMED (pbb));
1446 PBB_TRANSFORMED (pbb) = poly_scattering_copy (PBB_SAVED (pbb));
1449 /* Restores the scattering for all the pbbs in the SCOP. */
1451 static inline void
1452 restore_scattering (scop_p scop)
1454 int i;
1455 poly_bb_p pbb;
1457 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1458 restore_scattering_pbb (pbb);
1460 restore_lst_schedule (scop);
1463 #endif