/cp
[official-gcc.git] / gcc / graphite-poly.h
blob062d927411e2eb06ad466c6f9b43b5f4371f6fdd
1 /* Graphite polyhedral representation.
2 Copyright (C) 2009-2015 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 #include "sese.h"
27 #ifndef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
28 # define isl_stat int
29 # define isl_stat_ok 0
30 #endif
32 typedef struct poly_dr *poly_dr_p;
34 typedef struct poly_bb *poly_bb_p;
36 typedef struct scop *scop_p;
38 typedef unsigned graphite_dim_t;
40 static inline graphite_dim_t pbb_dim_iter_domain (const struct poly_bb *);
41 static inline graphite_dim_t pbb_nb_params (const struct poly_bb *);
42 static inline graphite_dim_t scop_nb_params (scop_p);
44 /* A data reference can write or read some memory or we
45 just know it may write some memory. */
46 enum poly_dr_type
48 PDR_READ,
49 /* PDR_MAY_READs are represented using PDR_READS. This does not
50 limit the expressiveness. */
51 PDR_WRITE,
52 PDR_MAY_WRITE
55 struct poly_dr
57 /* An identifier for this PDR. */
58 int id;
60 /* The number of data refs identical to this one in the PBB. */
61 int nb_refs;
63 /* A pointer to compiler's data reference description. */
64 void *compiler_dr;
66 /* A pointer to the PBB that contains this data reference. */
67 poly_bb_p pbb;
69 enum poly_dr_type type;
71 /* The access polyhedron contains the polyhedral space this data
72 reference will access.
74 The polyhedron contains these dimensions:
76 - The alias set (a):
77 Every memory access is classified in at least one alias set.
79 - The subscripts (s_0, ..., s_n):
80 The memory is accessed using zero or more subscript dimensions.
82 - The iteration domain (variables and parameters)
84 Do not hardcode the dimensions. Use the following accessor functions:
85 - pdr_alias_set_dim
86 - pdr_subscript_dim
87 - pdr_iterator_dim
88 - pdr_parameter_dim
90 Example:
92 | int A[1335][123];
93 | int *p = malloc ();
95 | k = ...
96 | for i
97 | {
98 | if (unknown_function ())
99 | p = A;
100 | ... = p[?][?];
101 | for j
102 | A[i][j+k] = m;
105 The data access A[i][j+k] in alias set "5" is described like this:
107 | i j k a s0 s1 1
108 | 0 0 0 1 0 0 -5 = 0
109 |-1 0 0 0 1 0 0 = 0
110 | 0 -1 -1 0 0 1 0 = 0
111 | 0 0 0 0 1 0 0 >= 0 # The last four lines describe the
112 | 0 0 0 0 0 1 0 >= 0 # array size.
113 | 0 0 0 0 -1 0 1335 >= 0
114 | 0 0 0 0 0 -1 123 >= 0
116 The pointer "*p" in alias set "5" and "7" is described as a union of
117 polyhedron:
120 | i k a s0 1
121 | 0 0 1 0 -5 = 0
122 | 0 0 0 1 0 >= 0
124 "or"
126 | i k a s0 1
127 | 0 0 1 0 -7 = 0
128 | 0 0 0 1 0 >= 0
130 "*p" accesses all of the object allocated with 'malloc'.
132 The scalar data access "m" is represented as an array with zero subscript
133 dimensions.
135 | i j k a 1
136 | 0 0 0 -1 15 = 0
138 The difference between the graphite internal format for access data and
139 the OpenSop format is in the order of columns.
140 Instead of having:
142 | i j k a s0 s1 1
143 | 0 0 0 1 0 0 -5 = 0
144 |-1 0 0 0 1 0 0 = 0
145 | 0 -1 -1 0 0 1 0 = 0
146 | 0 0 0 0 1 0 0 >= 0 # The last four lines describe the
147 | 0 0 0 0 0 1 0 >= 0 # array size.
148 | 0 0 0 0 -1 0 1335 >= 0
149 | 0 0 0 0 0 -1 123 >= 0
151 In OpenScop we have:
153 | a s0 s1 i j k 1
154 | 1 0 0 0 0 0 -5 = 0
155 | 0 1 0 -1 0 0 0 = 0
156 | 0 0 1 0 -1 -1 0 = 0
157 | 0 1 0 0 0 0 0 >= 0 # The last four lines describe the
158 | 0 0 1 0 0 0 0 >= 0 # array size.
159 | 0 -1 0 0 0 0 1335 >= 0
160 | 0 0 -1 0 0 0 123 >= 0
162 The OpenScop access function is printed as follows:
164 | 1 # The number of disjunct components in a union of access functions.
165 | R C O I L P # Described bellow.
166 | a s0 s1 i j k 1
167 | 1 0 0 0 0 0 -5 = 0
168 | 0 1 0 -1 0 0 0 = 0
169 | 0 0 1 0 -1 -1 0 = 0
170 | 0 1 0 0 0 0 0 >= 0 # The last four lines describe the
171 | 0 0 1 0 0 0 0 >= 0 # array size.
172 | 0 -1 0 0 0 0 1335 >= 0
173 | 0 0 -1 0 0 0 123 >= 0
175 Where:
176 - R: Number of rows.
177 - C: Number of columns.
178 - O: Number of output dimensions = alias set + number of subscripts.
179 - I: Number of input dimensions (iterators).
180 - L: Number of local (existentially quantified) dimensions.
181 - P: Number of parameters.
183 In the example, the vector "R C O I L P" is "7 7 3 2 0 1". */
184 isl_map *accesses;
185 isl_set *subscript_sizes;
187 /* Data reference's base object set number, we must assure 2 pdrs are in the
188 same base object set before dependency checking. */
189 int dr_base_object_set;
191 /* The number of subscripts. */
192 graphite_dim_t nb_subscripts;
195 #define PDR_ID(PDR) (PDR->id)
196 #define PDR_NB_REFS(PDR) (PDR->nb_refs)
197 #define PDR_CDR(PDR) (PDR->compiler_dr)
198 #define PDR_PBB(PDR) (PDR->pbb)
199 #define PDR_TYPE(PDR) (PDR->type)
200 #define PDR_ACCESSES(PDR) (NULL)
201 #define PDR_BASE_OBJECT_SET(PDR) (PDR->dr_base_object_set)
202 #define PDR_NB_SUBSCRIPTS(PDR) (PDR->nb_subscripts)
204 void new_poly_dr (poly_bb_p, int, enum poly_dr_type, void *,
205 graphite_dim_t, isl_map *, isl_set *);
206 void free_poly_dr (poly_dr_p);
207 void debug_pdr (poly_dr_p, int);
208 void print_pdr (FILE *, poly_dr_p, int);
209 static inline scop_p pdr_scop (poly_dr_p pdr);
211 /* The dimension of the iteration domain of the scop of PDR. */
213 static inline graphite_dim_t
214 pdr_dim_iter_domain (poly_dr_p pdr)
216 return pbb_dim_iter_domain (PDR_PBB (pdr));
219 /* The number of parameters of the scop of PDR. */
221 static inline graphite_dim_t
222 pdr_nb_params (poly_dr_p pdr)
224 return scop_nb_params (pdr_scop (pdr));
227 /* The dimension of the alias set in PDR. */
229 static inline graphite_dim_t
230 pdr_alias_set_dim (poly_dr_p pdr)
232 poly_bb_p pbb = PDR_PBB (pdr);
234 return pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
237 /* The dimension in PDR containing subscript S. */
239 static inline graphite_dim_t
240 pdr_subscript_dim (poly_dr_p pdr, graphite_dim_t s)
242 poly_bb_p pbb = PDR_PBB (pdr);
244 return pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb) + 1 + s;
247 /* The dimension in PDR containing the loop iterator ITER. */
249 static inline graphite_dim_t
250 pdr_iterator_dim (poly_dr_p pdr ATTRIBUTE_UNUSED, graphite_dim_t iter)
252 return iter;
255 /* The dimension in PDR containing parameter PARAM. */
257 static inline graphite_dim_t
258 pdr_parameter_dim (poly_dr_p pdr, graphite_dim_t param)
260 poly_bb_p pbb = PDR_PBB (pdr);
262 return pbb_dim_iter_domain (pbb) + param;
265 /* Returns true when PDR is a "read". */
267 static inline bool
268 pdr_read_p (poly_dr_p pdr)
270 return PDR_TYPE (pdr) == PDR_READ;
273 /* Returns true when PDR is a "write". */
275 static inline bool
276 pdr_write_p (poly_dr_p pdr)
278 return PDR_TYPE (pdr) == PDR_WRITE;
281 /* Returns true when PDR is a "may write". */
283 static inline bool
284 pdr_may_write_p (poly_dr_p pdr)
286 return PDR_TYPE (pdr) == PDR_MAY_WRITE;
289 /* Return true when PDR1 and PDR2 are similar data accesses: they have
290 the same base array, and the same access functions. */
292 static inline bool
293 same_pdr_p (poly_dr_p pdr1, poly_dr_p pdr2)
295 return PDR_NB_SUBSCRIPTS (pdr1) == PDR_NB_SUBSCRIPTS (pdr2)
296 && PDR_BASE_OBJECT_SET (pdr1) == PDR_BASE_OBJECT_SET (pdr2);
299 typedef struct poly_scattering *poly_scattering_p;
301 struct poly_scattering
303 /* The number of local variables. */
304 int nb_local_variables;
306 /* The number of scattering dimensions. */
307 int nb_scattering;
310 /* POLY_BB represents a blackbox in the polyhedral model. */
312 struct poly_bb
314 /* Pointer to a basic block or a statement in the compiler. */
315 void *black_box;
317 /* Pointer to the SCOP containing this PBB. */
318 scop_p scop;
320 /* The iteration domain of this bb. The layout of this polyhedron
321 is I|G with I the iteration domain, G the context parameters.
323 Example:
325 for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
326 for (j = 2; j <= 2*i + 5; j++)
327 for (k = 0; k <= 5; k++)
328 S (i,j,k)
330 Loop iterators: i, j, k
331 Parameters: a, b
333 | i >= a - 7b + 8
334 | i <= 3a + 13b + 20
335 | j >= 2
336 | j <= 2i + 5
337 | k >= 0
338 | k <= 5
340 The number of variables in the DOMAIN may change and is not
341 related to the number of loops in the original code. */
342 isl_set *domain;
344 /* The data references we access. */
345 vec<poly_dr_p> drs;
347 /* The original scattering. */
348 poly_scattering_p _original;
349 isl_map *schedule;
351 /* The transformed scattering. */
352 poly_scattering_p _transformed;
353 isl_map *transformed;
355 /* A copy of the transformed scattering. */
356 poly_scattering_p _saved;
357 isl_map *saved;
359 /* For tiling, the map for computing the separating class. */
360 isl_map *map_sepclass;
362 /* True when this PBB contains only a reduction statement. */
363 bool is_reduction;
366 #define PBB_BLACK_BOX(PBB) ((gimple_bb_p) PBB->black_box)
367 #define PBB_SCOP(PBB) (PBB->scop)
368 #define PBB_DOMAIN(PBB) (NULL)
369 #define PBB_DRS(PBB) (PBB->drs)
370 #define PBB_ORIGINAL(PBB) (PBB->_original)
371 #define PBB_ORIGINAL_SCATTERING(PBB) (NULL)
372 #define PBB_TRANSFORMED(PBB) (PBB->_transformed)
373 #define PBB_TRANSFORMED_SCATTERING(PBB) (NULL)
374 #define PBB_SAVED(PBB) (PBB->_saved)
375 /* XXX isl if we ever need local vars in the scatter, we can't use the
376 out dimension of transformed to count the scatterting transform dimension.
378 #define PBB_NB_LOCAL_VARIABLES(PBB) (0)
379 #define PBB_NB_SCATTERING_TRANSFORM(PBB) (isl_map_n_out (PBB->transformed))
380 #define PBB_IS_REDUCTION(PBB) (PBB->is_reduction)
382 extern poly_bb_p new_poly_bb (scop_p, void *);
383 extern void free_poly_bb (poly_bb_p);
384 extern void debug_loop_vec (poly_bb_p);
385 extern void schedule_to_scattering (poly_bb_p, int);
386 extern void print_pbb_domain (FILE *, poly_bb_p, int);
387 extern void print_pbb (FILE *, poly_bb_p, int);
388 extern void print_scop_context (FILE *, scop_p, int);
389 extern void print_scop (FILE *, scop_p, int);
390 extern void debug_pbb_domain (poly_bb_p, int);
391 extern void debug_pbb (poly_bb_p, int);
392 extern void print_pdrs (FILE *, poly_bb_p, int);
393 extern void debug_pdrs (poly_bb_p, int);
394 extern void debug_scop_context (scop_p, int);
395 extern void debug_scop (scop_p, int);
396 extern void print_scop_params (FILE *, scop_p, int);
397 extern void debug_scop_params (scop_p, int);
398 extern void print_iteration_domain (FILE *, poly_bb_p, int);
399 extern void print_iteration_domains (FILE *, scop_p, int);
400 extern void debug_iteration_domain (poly_bb_p, int);
401 extern void debug_iteration_domains (scop_p, int);
402 extern void print_isl_set (FILE *, isl_set *);
403 extern void print_isl_map (FILE *, isl_map *);
404 extern void print_isl_aff (FILE *, isl_aff *);
405 extern void print_isl_constraint (FILE *, isl_constraint *);
406 extern void debug_isl_set (isl_set *);
407 extern void debug_isl_map (isl_map *);
408 extern void debug_isl_aff (isl_aff *);
409 extern void debug_isl_constraint (isl_constraint *);
410 extern int scop_do_interchange (scop_p);
411 extern int scop_do_strip_mine (scop_p, int);
412 extern bool scop_do_block (scop_p);
413 extern bool flatten_all_loops (scop_p);
414 extern bool optimize_isl (scop_p);
415 extern void pbb_number_of_iterations_at_time (poly_bb_p, graphite_dim_t, mpz_t);
416 extern void debug_gmp_value (mpz_t);
418 /* Return the number of write data references in PBB. */
420 static inline int
421 number_of_write_pdrs (poly_bb_p pbb)
423 int res = 0;
424 int i;
425 poly_dr_p pdr;
427 for (i = 0; PBB_DRS (pbb).iterate (i, &pdr); i++)
428 if (PDR_TYPE (pdr) == PDR_WRITE)
429 res++;
431 return res;
434 /* Returns a gimple_bb from BB. */
436 static inline gimple_bb_p
437 gbb_from_bb (basic_block bb)
439 return (gimple_bb_p) bb->aux;
442 /* The poly_bb of the BB. */
444 static inline poly_bb_p
445 pbb_from_bb (basic_block bb)
447 return GBB_PBB (gbb_from_bb (bb));
450 /* The basic block of the PBB. */
452 static inline basic_block
453 pbb_bb (poly_bb_p pbb)
455 return GBB_BB (PBB_BLACK_BOX (pbb));
458 /* The index of the PBB. */
460 static inline int
461 pbb_index (poly_bb_p pbb)
463 return pbb_bb (pbb)->index;
466 /* The loop of the PBB. */
468 static inline loop_p
469 pbb_loop (poly_bb_p pbb)
471 return gbb_loop (PBB_BLACK_BOX (pbb));
474 /* The scop that contains the PDR. */
476 static inline scop_p
477 pdr_scop (poly_dr_p pdr)
479 return PBB_SCOP (PDR_PBB (pdr));
482 /* Set black box of PBB to BLACKBOX. */
484 static inline void
485 pbb_set_black_box (poly_bb_p pbb, void *black_box)
487 pbb->black_box = black_box;
490 /* The number of loops around PBB: the dimension of the iteration
491 domain. */
493 static inline graphite_dim_t
494 pbb_dim_iter_domain (const struct poly_bb *pbb)
496 return isl_set_dim (pbb->domain, isl_dim_set);
499 /* The number of params defined in PBB. */
501 static inline graphite_dim_t
502 pbb_nb_params (const struct poly_bb *pbb)
504 scop_p scop = PBB_SCOP (pbb);
506 return scop_nb_params (scop);
509 /* The number of scattering dimensions in the SCATTERING polyhedron
510 of a PBB for a given SCOP. */
512 static inline graphite_dim_t
513 pbb_nb_scattering_orig (const struct poly_bb *pbb)
515 return 2 * pbb_dim_iter_domain (pbb) + 1;
518 /* The number of scattering dimensions in PBB. */
520 static inline graphite_dim_t
521 pbb_nb_scattering_transform (const struct poly_bb *pbb)
523 return PBB_NB_SCATTERING_TRANSFORM (pbb);
526 /* The number of dynamic scattering dimensions in PBB. */
528 static inline graphite_dim_t
529 pbb_nb_dynamic_scattering_transform (const struct poly_bb *pbb)
531 /* This function requires the 2d + 1 scattering format to be
532 invariant during all transformations. */
533 gcc_assert (PBB_NB_SCATTERING_TRANSFORM (pbb) % 2);
534 return PBB_NB_SCATTERING_TRANSFORM (pbb) / 2;
537 /* Returns the number of local variables used in the transformed
538 scattering polyhedron of PBB. */
540 static inline graphite_dim_t
541 pbb_nb_local_vars (const struct poly_bb *pbb ATTRIBUTE_UNUSED)
543 /* For now we do not have any local variables, as we do not do strip
544 mining for example. */
545 return PBB_NB_LOCAL_VARIABLES (pbb);
548 /* The dimension in the domain of PBB containing the iterator ITER. */
550 static inline graphite_dim_t
551 pbb_iterator_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t iter)
553 return iter;
556 /* The dimension in the domain of PBB containing the iterator ITER. */
558 static inline graphite_dim_t
559 pbb_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
561 return param
562 + pbb_dim_iter_domain (pbb);
565 /* The dimension in the original scattering polyhedron of PBB
566 containing the scattering iterator SCATTER. */
568 static inline graphite_dim_t
569 psco_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
571 gcc_assert (scatter < pbb_nb_scattering_orig (pbb));
572 return scatter;
575 /* The dimension in the transformed scattering polyhedron of PBB
576 containing the scattering iterator SCATTER. */
578 static inline graphite_dim_t
579 psct_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
581 gcc_assert (scatter <= pbb_nb_scattering_transform (pbb));
582 return scatter;
585 /* The dimension in the transformed scattering polyhedron of PBB of
586 the local variable LV. */
588 static inline graphite_dim_t
589 psct_local_var_dim (poly_bb_p pbb, graphite_dim_t lv)
591 gcc_assert (lv <= pbb_nb_local_vars (pbb));
592 return lv + pbb_nb_scattering_transform (pbb);
595 /* The dimension in the original scattering polyhedron of PBB
596 containing the loop iterator ITER. */
598 static inline graphite_dim_t
599 psco_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
601 gcc_assert (iter < pbb_dim_iter_domain (pbb));
602 return iter + pbb_nb_scattering_orig (pbb);
605 /* The dimension in the transformed scattering polyhedron of PBB
606 containing the loop iterator ITER. */
608 static inline graphite_dim_t
609 psct_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
611 gcc_assert (iter < pbb_dim_iter_domain (pbb));
612 return iter
613 + pbb_nb_scattering_transform (pbb)
614 + pbb_nb_local_vars (pbb);
617 /* The dimension in the original scattering polyhedron of PBB
618 containing parameter PARAM. */
620 static inline graphite_dim_t
621 psco_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
623 gcc_assert (param < pbb_nb_params (pbb));
624 return param
625 + pbb_nb_scattering_orig (pbb)
626 + pbb_dim_iter_domain (pbb);
629 /* The dimension in the transformed scattering polyhedron of PBB
630 containing parameter PARAM. */
632 static inline graphite_dim_t
633 psct_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
635 gcc_assert (param < pbb_nb_params (pbb));
636 return param
637 + pbb_nb_scattering_transform (pbb)
638 + pbb_nb_local_vars (pbb)
639 + pbb_dim_iter_domain (pbb);
642 /* The scattering dimension of PBB corresponding to the dynamic level
643 LEVEL. */
645 static inline graphite_dim_t
646 psct_dynamic_dim (poly_bb_p pbb, graphite_dim_t level)
648 graphite_dim_t result = 1 + 2 * level;
650 gcc_assert (result < pbb_nb_scattering_transform (pbb));
651 return result;
654 /* The scattering dimension of PBB corresponding to the static
655 sequence of the loop level LEVEL. */
657 static inline graphite_dim_t
658 psct_static_dim (poly_bb_p pbb, graphite_dim_t level)
660 graphite_dim_t result = 2 * level;
662 gcc_assert (result < pbb_nb_scattering_transform (pbb));
663 return result;
666 /* Adds to the transformed scattering polyhedron of PBB a new local
667 variable and returns its index. */
669 static inline graphite_dim_t
670 psct_add_local_variable (poly_bb_p pbb ATTRIBUTE_UNUSED)
672 gcc_unreachable ();
673 return 0;
676 typedef struct lst *lst_p;
678 /* Loops and Statements Tree. */
679 struct lst {
681 /* LOOP_P is true when an LST node is a loop. */
682 bool loop_p;
684 /* A pointer to the loop that contains this node. */
685 lst_p loop_father;
687 /* The sum of all the memory strides for an LST loop. */
688 mpz_t memory_strides;
690 /* Loop nodes contain a sequence SEQ of LST nodes, statements
691 contain a pointer to their polyhedral representation PBB. */
692 union {
693 poly_bb_p pbb;
694 vec<lst_p> seq;
695 } node;
698 #define LST_LOOP_P(LST) ((LST)->loop_p)
699 #define LST_LOOP_FATHER(LST) ((LST)->loop_father)
700 #define LST_PBB(LST) ((LST)->node.pbb)
701 #define LST_SEQ(LST) ((LST)->node.seq)
702 #define LST_LOOP_MEMORY_STRIDES(LST) ((LST)->memory_strides)
704 void scop_to_lst (scop_p);
705 void print_lst (FILE *, lst_p, int);
706 void debug_lst (lst_p);
707 void dot_lst (lst_p);
709 /* Creates a new LST loop with SEQ. */
711 static inline lst_p
712 new_lst_loop (vec<lst_p> seq)
714 lst_p lst = XNEW (struct lst);
715 int i;
716 lst_p l;
718 LST_LOOP_P (lst) = true;
719 LST_SEQ (lst) = seq;
720 LST_LOOP_FATHER (lst) = NULL;
721 mpz_init (LST_LOOP_MEMORY_STRIDES (lst));
722 mpz_set_si (LST_LOOP_MEMORY_STRIDES (lst), -1);
724 for (i = 0; seq.iterate (i, &l); i++)
725 LST_LOOP_FATHER (l) = lst;
727 return lst;
730 /* Creates a new LST statement with PBB. */
732 static inline lst_p
733 new_lst_stmt (poly_bb_p pbb)
735 lst_p lst = XNEW (struct lst);
737 LST_LOOP_P (lst) = false;
738 LST_PBB (lst) = pbb;
739 LST_LOOP_FATHER (lst) = NULL;
740 return lst;
743 /* Frees the memory used by LST. */
745 static inline void
746 free_lst (lst_p lst)
748 if (!lst)
749 return;
751 if (LST_LOOP_P (lst))
753 int i;
754 lst_p l;
756 for (i = 0; LST_SEQ (lst).iterate (i, &l); i++)
757 free_lst (l);
759 mpz_clear (LST_LOOP_MEMORY_STRIDES (lst));
760 LST_SEQ (lst).release ();
763 free (lst);
766 /* Returns a copy of LST. */
768 static inline lst_p
769 copy_lst (lst_p lst)
771 if (!lst)
772 return NULL;
774 if (LST_LOOP_P (lst))
776 int i;
777 lst_p l;
778 vec<lst_p> seq;
779 seq.create (5);
781 for (i = 0; LST_SEQ (lst).iterate (i, &l); i++)
782 seq.safe_push (copy_lst (l));
784 return new_lst_loop (seq);
787 return new_lst_stmt (LST_PBB (lst));
790 /* Adds a new loop under the loop LST. */
792 static inline void
793 lst_add_loop_under_loop (lst_p lst)
795 vec<lst_p> seq;
796 seq.create (1);
797 lst_p l = new_lst_loop (LST_SEQ (lst));
799 gcc_assert (LST_LOOP_P (lst));
801 LST_LOOP_FATHER (l) = lst;
802 seq.quick_push (l);
803 LST_SEQ (lst) = seq;
806 /* Returns the loop depth of LST. */
808 static inline int
809 lst_depth (lst_p lst)
811 if (!lst)
812 return -2;
814 /* The depth of the outermost "fake" loop is -1. This outermost
815 loop does not have a loop father and it is just a container, as
816 in the loop representation of GCC. */
817 if (!LST_LOOP_FATHER (lst))
818 return -1;
820 return lst_depth (LST_LOOP_FATHER (lst)) + 1;
823 /* Returns the Dewey number for LST. */
825 static inline int
826 lst_dewey_number (lst_p lst)
828 int i;
829 lst_p l;
831 if (!lst)
832 return -1;
834 if (!LST_LOOP_FATHER (lst))
835 return 0;
837 FOR_EACH_VEC_ELT (LST_SEQ (LST_LOOP_FATHER (lst)), i, l)
838 if (l == lst)
839 return i;
841 return -1;
844 /* Returns the Dewey number of LST at depth DEPTH. */
846 static inline int
847 lst_dewey_number_at_depth (lst_p lst, int depth)
849 gcc_assert (lst && depth >= 0 && lst_depth (lst) <= depth);
851 if (lst_depth (lst) == depth)
852 return lst_dewey_number (lst);
854 return lst_dewey_number_at_depth (LST_LOOP_FATHER (lst), depth);
857 /* Returns the predecessor of LST in the sequence of its loop father.
858 Returns NULL if LST is the first statement in the sequence. */
860 static inline lst_p
861 lst_pred (lst_p lst)
863 int dewey;
864 lst_p father;
866 if (!lst || !LST_LOOP_FATHER (lst))
867 return NULL;
869 dewey = lst_dewey_number (lst);
870 if (dewey == 0)
871 return NULL;
873 father = LST_LOOP_FATHER (lst);
874 return LST_SEQ (father)[dewey - 1];
877 /* Returns the successor of LST in the sequence of its loop father.
878 Returns NULL if there is none. */
880 static inline lst_p
881 lst_succ (lst_p lst)
883 int dewey;
884 lst_p father;
886 if (!lst || !LST_LOOP_FATHER (lst))
887 return NULL;
889 dewey = lst_dewey_number (lst);
890 father = LST_LOOP_FATHER (lst);
892 if (LST_SEQ (father).length () == (unsigned) dewey + 1)
893 return NULL;
895 return LST_SEQ (father)[dewey + 1];
899 /* Return the LST node corresponding to PBB. */
901 static inline lst_p
902 lst_find_pbb (lst_p lst, poly_bb_p pbb)
904 int i;
905 lst_p l;
907 if (!lst)
908 return NULL;
910 if (!LST_LOOP_P (lst))
911 return (pbb == LST_PBB (lst)) ? lst : NULL;
913 for (i = 0; LST_SEQ (lst).iterate (i, &l); i++)
915 lst_p res = lst_find_pbb (l, pbb);
916 if (res)
917 return res;
920 return NULL;
923 /* Return the LST node corresponding to the loop around STMT at depth
924 LOOP_DEPTH. */
926 static inline lst_p
927 find_lst_loop (lst_p stmt, int loop_depth)
929 lst_p loop = LST_LOOP_FATHER (stmt);
931 gcc_assert (loop_depth >= 0);
933 while (loop_depth < lst_depth (loop))
934 loop = LST_LOOP_FATHER (loop);
936 return loop;
939 /* Return the first LST representing a PBB statement in LST. */
941 static inline lst_p
942 lst_find_first_pbb (lst_p lst)
944 int i;
945 lst_p l;
947 if (!lst)
948 return NULL;
950 if (!LST_LOOP_P (lst))
951 return lst;
953 for (i = 0; LST_SEQ (lst).iterate (i, &l); i++)
955 lst_p res = lst_find_first_pbb (l);
956 if (res)
957 return res;
960 return NULL;
963 /* Returns true when LST is a loop that does not contain
964 statements. */
966 static inline bool
967 lst_empty_p (lst_p lst)
969 return !lst_find_first_pbb (lst);
972 /* Return the last LST representing a PBB statement in LST. */
974 static inline lst_p
975 lst_find_last_pbb (lst_p lst)
977 int i;
978 lst_p l, res = NULL;
980 if (!lst)
981 return NULL;
983 if (!LST_LOOP_P (lst))
984 return lst;
986 for (i = 0; LST_SEQ (lst).iterate (i, &l); i++)
988 lst_p last = lst_find_last_pbb (l);
990 if (last)
991 res = last;
994 gcc_assert (res);
995 return res;
998 /* Returns true if LOOP contains LST, in other words, if LST is nested
999 in LOOP. */
1001 static inline bool
1002 lst_contains_p (lst_p loop, lst_p lst)
1004 if (!loop || !lst || !LST_LOOP_P (loop))
1005 return false;
1007 if (loop == lst)
1008 return true;
1010 return lst_contains_p (loop, LST_LOOP_FATHER (lst));
1013 /* Returns true if LOOP contains PBB, in other words, if PBB is nested
1014 in LOOP. */
1016 static inline bool
1017 lst_contains_pbb (lst_p loop, poly_bb_p pbb)
1019 return lst_find_pbb (loop, pbb) ? true : false;
1022 /* Creates a loop nest of depth NB_LOOPS containing LST. */
1024 static inline lst_p
1025 lst_create_nest (int nb_loops, lst_p lst)
1027 lst_p res, loop;
1028 vec<lst_p> seq;
1030 if (nb_loops == 0)
1031 return lst;
1033 seq.create (1);
1034 loop = lst_create_nest (nb_loops - 1, lst);
1035 seq.quick_push (loop);
1036 res = new_lst_loop (seq);
1037 LST_LOOP_FATHER (loop) = res;
1039 return res;
1042 /* Removes LST from the sequence of statements of its loop father. */
1044 static inline void
1045 lst_remove_from_sequence (lst_p lst)
1047 lst_p father = LST_LOOP_FATHER (lst);
1048 int dewey = lst_dewey_number (lst);
1050 gcc_assert (lst && father && dewey >= 0);
1052 LST_SEQ (father).ordered_remove (dewey);
1053 LST_LOOP_FATHER (lst) = NULL;
1056 /* Removes the loop LST and inline its body in the father loop. */
1058 static inline void
1059 lst_remove_loop_and_inline_stmts_in_loop_father (lst_p lst)
1061 lst_p l, father = LST_LOOP_FATHER (lst);
1062 int i, dewey = lst_dewey_number (lst);
1064 gcc_assert (lst && father && dewey >= 0);
1066 LST_SEQ (father).ordered_remove (dewey);
1067 LST_LOOP_FATHER (lst) = NULL;
1069 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
1071 LST_SEQ (father).safe_insert (dewey + i, l);
1072 LST_LOOP_FATHER (l) = father;
1076 /* Sets NITER to the upper bound approximation of the number of
1077 iterations of loop LST. */
1079 static inline void
1080 lst_niter_for_loop (lst_p lst, mpz_t niter)
1082 int depth = lst_depth (lst);
1083 poly_bb_p pbb = LST_PBB (lst_find_first_pbb (lst));
1085 gcc_assert (LST_LOOP_P (lst));
1086 pbb_number_of_iterations_at_time (pbb, psct_dynamic_dim (pbb, depth), niter);
1089 /* Updates the scattering of PBB to be at the DEWEY number in the loop
1090 at depth LEVEL. */
1092 static inline void
1093 pbb_update_scattering (poly_bb_p pbb, graphite_dim_t level, int dewey)
1095 graphite_dim_t sched = psct_static_dim (pbb, level);
1096 isl_space *d = isl_map_get_space (pbb->transformed);
1097 isl_space *d1 = isl_space_range (d);
1098 unsigned i, n = isl_space_dim (d1, isl_dim_out);
1099 isl_space *d2 = isl_space_add_dims (d1, isl_dim_in, n);
1100 isl_map *x = isl_map_universe (d2);
1102 x = isl_map_fix_si (x, isl_dim_out, sched, dewey);
1104 for (i = 0; i < n; i++)
1105 if (i != sched)
1106 x = isl_map_equate (x, isl_dim_in, i, isl_dim_out, i);
1108 pbb->transformed = isl_map_apply_range (pbb->transformed, x);
1111 /* Updates the scattering of all the PBBs under LST to be at the DEWEY
1112 number in the loop at depth LEVEL. */
1114 static inline void
1115 lst_update_scattering_under (lst_p lst, int level, int dewey)
1117 int i;
1118 lst_p l;
1120 gcc_assert (lst && level >= 0 && dewey >= 0);
1122 if (LST_LOOP_P (lst))
1123 for (i = 0; LST_SEQ (lst).iterate (i, &l); i++)
1124 lst_update_scattering_under (l, level, dewey);
1125 else
1126 pbb_update_scattering (LST_PBB (lst), level, dewey);
1129 /* Updates the all the scattering levels of all the PBBs under
1130 LST. */
1132 static inline void
1133 lst_update_scattering (lst_p lst)
1135 int i;
1136 lst_p l;
1138 if (!lst)
1139 return;
1141 if (LST_LOOP_FATHER (lst))
1143 lst_p father = LST_LOOP_FATHER (lst);
1144 int dewey = lst_dewey_number (lst);
1145 int level = lst_depth (lst);
1147 gcc_assert (lst && father && dewey >= 0 && level >= 0);
1149 for (i = dewey; LST_SEQ (father).iterate (i, &l); i++)
1150 lst_update_scattering_under (l, level, i);
1153 if (LST_LOOP_P (lst))
1154 for (i = 0; LST_SEQ (lst).iterate (i, &l); i++)
1155 lst_update_scattering (l);
1158 /* Inserts LST1 before LST2 if BEFORE is true; inserts LST1 after LST2
1159 if BEFORE is false. */
1161 static inline void
1162 lst_insert_in_sequence (lst_p lst1, lst_p lst2, bool before)
1164 lst_p father;
1165 int dewey;
1167 /* Do not insert empty loops. */
1168 if (!lst1 || lst_empty_p (lst1))
1169 return;
1171 father = LST_LOOP_FATHER (lst2);
1172 dewey = lst_dewey_number (lst2);
1174 gcc_assert (lst2 && father && dewey >= 0);
1176 LST_SEQ (father).safe_insert (before ? dewey : dewey + 1, lst1);
1177 LST_LOOP_FATHER (lst1) = father;
1180 /* Replaces LST1 with LST2. */
1182 static inline void
1183 lst_replace (lst_p lst1, lst_p lst2)
1185 lst_p father;
1186 int dewey;
1188 if (!lst2 || lst_empty_p (lst2))
1189 return;
1191 father = LST_LOOP_FATHER (lst1);
1192 dewey = lst_dewey_number (lst1);
1193 LST_LOOP_FATHER (lst2) = father;
1194 LST_SEQ (father)[dewey] = lst2;
1197 /* Returns a copy of ROOT where LST has been replaced by a copy of the
1198 LSTs A B C in this sequence. */
1200 static inline lst_p
1201 lst_substitute_3 (lst_p root, lst_p lst, lst_p a, lst_p b, lst_p c)
1203 int i;
1204 lst_p l;
1205 vec<lst_p> seq;
1207 if (!root)
1208 return NULL;
1210 gcc_assert (lst && root != lst);
1212 if (!LST_LOOP_P (root))
1213 return new_lst_stmt (LST_PBB (root));
1215 seq.create (5);
1217 for (i = 0; LST_SEQ (root).iterate (i, &l); i++)
1218 if (l != lst)
1219 seq.safe_push (lst_substitute_3 (l, lst, a, b, c));
1220 else
1222 if (!lst_empty_p (a))
1223 seq.safe_push (copy_lst (a));
1224 if (!lst_empty_p (b))
1225 seq.safe_push (copy_lst (b));
1226 if (!lst_empty_p (c))
1227 seq.safe_push (copy_lst (c));
1230 return new_lst_loop (seq);
1233 /* Moves LST before LOOP if BEFORE is true, and after the LOOP if
1234 BEFORE is false. */
1236 static inline void
1237 lst_distribute_lst (lst_p loop, lst_p lst, bool before)
1239 int loop_depth = lst_depth (loop);
1240 int depth = lst_depth (lst);
1241 int nb_loops = depth - loop_depth;
1243 gcc_assert (lst && loop && LST_LOOP_P (loop) && nb_loops > 0);
1245 lst_remove_from_sequence (lst);
1246 lst_insert_in_sequence (lst_create_nest (nb_loops, lst), loop, before);
1249 /* Removes from LOOP all the statements before/after and including PBB
1250 if BEFORE is true/false. Returns the negation of BEFORE when the
1251 statement PBB has been found. */
1253 static inline bool
1254 lst_remove_all_before_including_pbb (lst_p loop, poly_bb_p pbb, bool before)
1256 int i;
1257 lst_p l;
1259 if (!loop || !LST_LOOP_P (loop))
1260 return before;
1262 for (i = 0; LST_SEQ (loop).iterate (i, &l);)
1263 if (LST_LOOP_P (l))
1265 before = lst_remove_all_before_including_pbb (l, pbb, before);
1267 if (LST_SEQ (l).length () == 0)
1269 LST_SEQ (loop).ordered_remove (i);
1270 free_lst (l);
1272 else
1273 i++;
1275 else
1277 if (before)
1279 if (LST_PBB (l) == pbb)
1280 before = false;
1282 LST_SEQ (loop).ordered_remove (i);
1283 free_lst (l);
1285 else if (LST_PBB (l) == pbb)
1287 before = true;
1288 LST_SEQ (loop).ordered_remove (i);
1289 free_lst (l);
1291 else
1292 i++;
1295 return before;
1298 /* Removes from LOOP all the statements before/after and excluding PBB
1299 if BEFORE is true/false; Returns the negation of BEFORE when the
1300 statement PBB has been found. */
1302 static inline bool
1303 lst_remove_all_before_excluding_pbb (lst_p loop, poly_bb_p pbb, bool before)
1305 int i;
1306 lst_p l;
1308 if (!loop || !LST_LOOP_P (loop))
1309 return before;
1311 for (i = 0; LST_SEQ (loop).iterate (i, &l);)
1312 if (LST_LOOP_P (l))
1314 before = lst_remove_all_before_excluding_pbb (l, pbb, before);
1316 if (LST_SEQ (l).length () == 0)
1318 LST_SEQ (loop).ordered_remove (i);
1319 free_lst (l);
1320 continue;
1323 i++;
1325 else
1327 if (before && LST_PBB (l) != pbb)
1329 LST_SEQ (loop).ordered_remove (i);
1330 free_lst (l);
1331 continue;
1334 i++;
1336 if (LST_PBB (l) == pbb)
1337 before = before ? false : true;
1340 return before;
1343 /* A SCOP is a Static Control Part of the program, simple enough to be
1344 represented in polyhedral form. */
1345 struct scop
1347 /* A SCOP is defined as a SESE region. */
1348 void *region;
1350 /* Number of parameters in SCoP. */
1351 graphite_dim_t nb_params;
1353 /* All the basic blocks in this scop that contain memory references
1354 and that will be represented as statements in the polyhedral
1355 representation. */
1356 vec<poly_bb_p> bbs;
1358 /* Original, transformed and saved schedules. */
1359 lst_p original_schedule, transformed_schedule, saved_schedule;
1361 /* The context describes known restrictions concerning the parameters
1362 and relations in between the parameters.
1364 void f (int8_t a, uint_16_t b) {
1365 c = 2 a + b;
1369 Here we can add these restrictions to the context:
1371 -128 >= a >= 127
1372 0 >= b >= 65,535
1373 c = 2a + b */
1374 isl_set *context;
1376 /* The context used internally by ISL. */
1377 isl_ctx *ctx;
1379 /* The original dependence relations:
1380 RAW are read after write dependences,
1381 WAR are write after read dependences,
1382 WAW are write after write dependences. */
1383 isl_union_map *must_raw, *may_raw, *must_raw_no_source, *may_raw_no_source,
1384 *must_war, *may_war, *must_war_no_source, *may_war_no_source,
1385 *must_waw, *may_waw, *must_waw_no_source, *may_waw_no_source;
1387 /* True when the scop has been converted to its polyhedral
1388 representation. */
1389 bool poly_scop_p;
1392 #define SCOP_BBS(S) (S->bbs)
1393 #define SCOP_REGION(S) ((sese) S->region)
1394 #define SCOP_CONTEXT(S) (NULL)
1395 #define SCOP_ORIGINAL_SCHEDULE(S) (S->original_schedule)
1396 #define SCOP_TRANSFORMED_SCHEDULE(S) (S->transformed_schedule)
1397 #define SCOP_SAVED_SCHEDULE(S) (S->saved_schedule)
1398 #define POLY_SCOP_P(S) (S->poly_scop_p)
1400 extern scop_p new_scop (void *);
1401 extern void free_scop (scop_p);
1402 extern void free_scops (vec<scop_p> );
1403 extern void print_generated_program (FILE *, scop_p);
1404 extern void debug_generated_program (scop_p);
1405 extern void print_scattering_function (FILE *, poly_bb_p, int);
1406 extern void print_scattering_functions (FILE *, scop_p, int);
1407 extern void debug_scattering_function (poly_bb_p, int);
1408 extern void debug_scattering_functions (scop_p, int);
1409 extern int scop_max_loop_depth (scop_p);
1410 extern int unify_scattering_dimensions (scop_p);
1411 extern bool apply_poly_transforms (scop_p);
1412 extern bool graphite_legal_transform (scop_p);
1414 /* Set the region of SCOP to REGION. */
1416 static inline void
1417 scop_set_region (scop_p scop, void *region)
1419 scop->region = region;
1422 /* Returns the number of parameters for SCOP. */
1424 static inline graphite_dim_t
1425 scop_nb_params (scop_p scop)
1427 return scop->nb_params;
1430 /* Set the number of params of SCOP to NB_PARAMS. */
1432 static inline void
1433 scop_set_nb_params (scop_p scop, graphite_dim_t nb_params)
1435 scop->nb_params = nb_params;
1438 /* Allocates a new empty poly_scattering structure. */
1440 static inline poly_scattering_p
1441 poly_scattering_new (void)
1443 poly_scattering_p res = XNEW (struct poly_scattering);
1445 res->nb_local_variables = 0;
1446 res->nb_scattering = 0;
1447 return res;
1450 /* Free a poly_scattering structure. */
1452 static inline void
1453 poly_scattering_free (poly_scattering_p s)
1455 free (s);
1458 /* Copies S and return a new scattering. */
1460 static inline poly_scattering_p
1461 poly_scattering_copy (poly_scattering_p s)
1463 poly_scattering_p res = poly_scattering_new ();
1465 res->nb_local_variables = s->nb_local_variables;
1466 res->nb_scattering = s->nb_scattering;
1467 return res;
1470 /* Saves the transformed scattering of PBB. */
1472 static inline void
1473 store_scattering_pbb (poly_bb_p pbb)
1475 isl_map_free (pbb->saved);
1476 pbb->saved = isl_map_copy (pbb->transformed);
1479 /* Stores the SCOP_TRANSFORMED_SCHEDULE to SCOP_SAVED_SCHEDULE. */
1481 static inline void
1482 store_lst_schedule (scop_p scop)
1484 if (SCOP_SAVED_SCHEDULE (scop))
1485 free_lst (SCOP_SAVED_SCHEDULE (scop));
1487 SCOP_SAVED_SCHEDULE (scop) = copy_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
1490 /* Restores the SCOP_TRANSFORMED_SCHEDULE from SCOP_SAVED_SCHEDULE. */
1492 static inline void
1493 restore_lst_schedule (scop_p scop)
1495 if (SCOP_TRANSFORMED_SCHEDULE (scop))
1496 free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
1498 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (SCOP_SAVED_SCHEDULE (scop));
1501 /* Saves the scattering for all the pbbs in the SCOP. */
1503 static inline void
1504 store_scattering (scop_p scop)
1506 int i;
1507 poly_bb_p pbb;
1509 for (i = 0; SCOP_BBS (scop).iterate (i, &pbb); i++)
1510 store_scattering_pbb (pbb);
1512 store_lst_schedule (scop);
1515 /* Restores the scattering of PBB. */
1517 static inline void
1518 restore_scattering_pbb (poly_bb_p pbb)
1520 gcc_assert (pbb->saved);
1522 isl_map_free (pbb->transformed);
1523 pbb->transformed = isl_map_copy (pbb->saved);
1526 /* Restores the scattering for all the pbbs in the SCOP. */
1528 static inline void
1529 restore_scattering (scop_p scop)
1531 int i;
1532 poly_bb_p pbb;
1534 for (i = 0; SCOP_BBS (scop).iterate (i, &pbb); i++)
1535 restore_scattering_pbb (pbb);
1537 restore_lst_schedule (scop);
1540 bool graphite_legal_transform (scop_p);
1541 isl_map *reverse_loop_at_level (poly_bb_p, int);
1542 isl_union_map *reverse_loop_for_pbbs (scop_p, vec<poly_bb_p> , int);
1543 __isl_give isl_union_map *extend_schedule (__isl_take isl_union_map *);
1546 void
1547 compute_deps (scop_p scop, vec<poly_bb_p> pbbs,
1548 isl_union_map **must_raw,
1549 isl_union_map **may_raw,
1550 isl_union_map **must_raw_no_source,
1551 isl_union_map **may_raw_no_source,
1552 isl_union_map **must_war,
1553 isl_union_map **may_war,
1554 isl_union_map **must_war_no_source,
1555 isl_union_map **may_war_no_source,
1556 isl_union_map **must_waw,
1557 isl_union_map **may_waw,
1558 isl_union_map **must_waw_no_source,
1559 isl_union_map **may_waw_no_source);
1561 isl_union_map *
1562 scop_get_dependences (scop_p scop);
1564 bool
1565 carries_deps (__isl_keep isl_union_map *schedule,
1566 __isl_keep isl_union_map *deps,
1567 int depth);
1569 #endif