2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
43 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
45 static char *type_str
[] = {
46 [pet_expr_access
] = "access",
47 [pet_expr_call
] = "call",
48 [pet_expr_cast
] = "cast",
49 [pet_expr_double
] = "double",
50 [pet_expr_int
] = "int",
54 static char *op_str
[] = {
55 [pet_op_add_assign
] = "+=",
56 [pet_op_sub_assign
] = "-=",
57 [pet_op_mul_assign
] = "*=",
58 [pet_op_div_assign
] = "/=",
59 [pet_op_assign
] = "=",
74 [pet_op_post_inc
] = "++",
75 [pet_op_post_dec
] = "--",
76 [pet_op_pre_inc
] = "++",
77 [pet_op_pre_dec
] = "--",
78 [pet_op_address_of
] = "&",
87 [pet_op_assume
] = "assume",
88 [pet_op_kill
] = "kill"
91 /* pet_scop with extra information that is used during parsing and printing.
93 * In particular, we keep track of conditions under which we want
94 * to skip the rest of the current loop iteration (skip[pet_skip_now])
95 * and of conditions under which we want to skip subsequent
96 * loop iterations (skip[pet_skip_later]).
98 * The conditions are represented as index expressions defined
99 * over a zero-dimensional domain. The index expression is either
100 * a boolean affine expression or an access to a variable, which
101 * is assumed to attain values zero and one. The condition holds
102 * if the variable has value one or if the affine expression
103 * has value one (typically for only part of the parameter space).
105 * A missing condition (skip[type] == NULL) means that we don't want
108 * Additionally, we keep track of the original input file
109 * inside pet_transform_C_source.
111 struct pet_scop_ext
{
112 struct pet_scop scop
;
114 isl_multi_pw_aff
*skip
[2];
118 const char *pet_op_str(enum pet_op_type op
)
123 int pet_op_is_inc_dec(enum pet_op_type op
)
125 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
126 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
129 const char *pet_type_str(enum pet_expr_type type
)
131 return type_str
[type
];
134 enum pet_op_type
pet_str_op(const char *str
)
138 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
139 if (!strcmp(op_str
[i
], str
))
145 enum pet_expr_type
pet_str_type(const char *str
)
149 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
150 if (!strcmp(type_str
[i
], str
))
156 /* Construct an access pet_expr from an access relation and an index expression.
157 * By default, it is considered to be a read access.
159 struct pet_expr
*pet_expr_from_access_and_index( __isl_take isl_map
*access
,
160 __isl_take isl_multi_pw_aff
*index
)
162 isl_ctx
*ctx
= isl_map_get_ctx(access
);
163 struct pet_expr
*expr
;
165 if (!index
|| !access
)
167 expr
= isl_calloc_type(ctx
, struct pet_expr
);
171 expr
->type
= pet_expr_access
;
172 expr
->acc
.access
= access
;
173 expr
->acc
.index
= index
;
179 isl_map_free(access
);
180 isl_multi_pw_aff_free(index
);
184 /* Construct an access pet_expr from an index expression.
185 * By default, the access is considered to be a read access.
187 struct pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
191 access
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
192 return pet_expr_from_access_and_index(access
, index
);
195 /* Extend the range of "access" with "n" dimensions, retaining
196 * the tuple identifier on this range.
198 * If "access" represents a member access, then extend the range
201 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
205 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
207 if (!isl_map_range_is_wrapping(access
)) {
208 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
212 domain
= isl_map_copy(access
);
213 domain
= isl_map_range_factor_domain(domain
);
214 access
= isl_map_range_factor_range(access
);
215 access
= extend_range(access
, n
);
216 access
= isl_map_range_product(domain
, access
);
219 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
224 /* Construct an access pet_expr from an index expression and
225 * the depth of the accessed array.
226 * By default, the access is considered to be a read access.
228 * If the number of indices is smaller than the depth of the array,
229 * then we assume that all elements of the remaining dimensions
232 struct pet_expr
*pet_expr_from_index_and_depth(
233 __isl_take isl_multi_pw_aff
*index
, int depth
)
238 access
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
241 dim
= isl_map_dim(access
, isl_dim_out
);
243 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
244 "number of indices greater than depth",
245 access
= isl_map_free(access
));
247 return pet_expr_from_access_and_index(access
, index
);
249 access
= extend_range(access
, depth
- dim
);
251 return pet_expr_from_access_and_index(access
, index
);
253 isl_multi_pw_aff_free(index
);
257 /* Construct a pet_expr that kills the elements specified by
258 * the index expression "index" and the access relation "access".
260 struct pet_expr
*pet_expr_kill_from_access_and_index(__isl_take isl_map
*access
,
261 __isl_take isl_multi_pw_aff
*index
)
264 struct pet_expr
*expr
;
266 if (!access
|| !index
)
269 ctx
= isl_multi_pw_aff_get_ctx(index
);
270 expr
= pet_expr_from_access_and_index(access
, index
);
274 return pet_expr_new_unary(ctx
, pet_op_kill
, expr
);
276 isl_map_free(access
);
277 isl_multi_pw_aff_free(index
);
281 /* Construct a unary pet_expr that performs "op" on "arg".
283 struct pet_expr
*pet_expr_new_unary(isl_ctx
*ctx
, enum pet_op_type op
,
284 struct pet_expr
*arg
)
286 struct pet_expr
*expr
;
290 expr
= isl_alloc_type(ctx
, struct pet_expr
);
294 expr
->type
= pet_expr_op
;
297 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
300 expr
->args
[pet_un_arg
] = arg
;
308 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
310 struct pet_expr
*pet_expr_new_binary(isl_ctx
*ctx
, enum pet_op_type op
,
311 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
313 struct pet_expr
*expr
;
317 expr
= isl_alloc_type(ctx
, struct pet_expr
);
321 expr
->type
= pet_expr_op
;
324 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 2);
327 expr
->args
[pet_bin_lhs
] = lhs
;
328 expr
->args
[pet_bin_rhs
] = rhs
;
337 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
339 struct pet_expr
*pet_expr_new_ternary(isl_ctx
*ctx
, struct pet_expr
*cond
,
340 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
342 struct pet_expr
*expr
;
344 if (!cond
|| !lhs
|| !rhs
)
346 expr
= isl_alloc_type(ctx
, struct pet_expr
);
350 expr
->type
= pet_expr_op
;
351 expr
->op
= pet_op_cond
;
353 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 3);
356 expr
->args
[pet_ter_cond
] = cond
;
357 expr
->args
[pet_ter_true
] = lhs
;
358 expr
->args
[pet_ter_false
] = rhs
;
368 /* Construct a call pet_expr that calls function "name" with "n_arg"
369 * arguments. The caller is responsible for filling in the arguments.
371 struct pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
374 struct pet_expr
*expr
;
376 expr
= isl_alloc_type(ctx
, struct pet_expr
);
380 expr
->type
= pet_expr_call
;
382 expr
->name
= strdup(name
);
383 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n_arg
);
384 if (!expr
->name
|| !expr
->args
)
385 return pet_expr_free(expr
);
390 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
392 struct pet_expr
*pet_expr_new_cast(isl_ctx
*ctx
, const char *type_name
,
393 struct pet_expr
*arg
)
395 struct pet_expr
*expr
;
400 expr
= isl_alloc_type(ctx
, struct pet_expr
);
404 expr
->type
= pet_expr_cast
;
406 expr
->type_name
= strdup(type_name
);
407 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
408 if (!expr
->type_name
|| !expr
->args
)
420 /* Construct a pet_expr that represents the double "d".
422 struct pet_expr
*pet_expr_new_double(isl_ctx
*ctx
, double val
, const char *s
)
424 struct pet_expr
*expr
;
426 expr
= isl_calloc_type(ctx
, struct pet_expr
);
430 expr
->type
= pet_expr_double
;
432 expr
->d
.s
= strdup(s
);
434 return pet_expr_free(expr
);
439 /* Construct a pet_expr that represents the integer value "v".
441 struct pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
444 struct pet_expr
*expr
;
449 ctx
= isl_val_get_ctx(v
);
450 expr
= isl_calloc_type(ctx
, struct pet_expr
);
454 expr
->type
= pet_expr_int
;
463 struct pet_expr
*pet_expr_free(struct pet_expr
*expr
)
470 for (i
= 0; i
< expr
->n_arg
; ++i
)
471 pet_expr_free(expr
->args
[i
]);
474 switch (expr
->type
) {
475 case pet_expr_access
:
476 isl_id_free(expr
->acc
.ref_id
);
477 isl_map_free(expr
->acc
.access
);
478 isl_multi_pw_aff_free(expr
->acc
.index
);
484 free(expr
->type_name
);
486 case pet_expr_double
:
490 isl_val_free(expr
->i
);
500 static void expr_dump(struct pet_expr
*expr
, int indent
)
507 fprintf(stderr
, "%*s", indent
, "");
509 switch (expr
->type
) {
510 case pet_expr_double
:
511 fprintf(stderr
, "%s\n", expr
->d
.s
);
514 isl_val_dump(expr
->i
);
516 case pet_expr_access
:
517 if (expr
->acc
.ref_id
) {
518 isl_id_dump(expr
->acc
.ref_id
);
519 fprintf(stderr
, "%*s", indent
, "");
521 isl_map_dump(expr
->acc
.access
);
522 fprintf(stderr
, "%*s", indent
, "");
523 isl_multi_pw_aff_dump(expr
->acc
.index
);
524 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
526 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
527 "", expr
->acc
.write
);
528 for (i
= 0; i
< expr
->n_arg
; ++i
)
529 expr_dump(expr
->args
[i
], indent
+ 2);
532 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
533 for (i
= 0; i
< expr
->n_arg
; ++i
)
534 expr_dump(expr
->args
[i
], indent
+ 2);
537 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
538 for (i
= 0; i
< expr
->n_arg
; ++i
)
539 expr_dump(expr
->args
[i
], indent
+ 2);
542 fprintf(stderr
, "(%s)\n", expr
->type_name
);
543 for (i
= 0; i
< expr
->n_arg
; ++i
)
544 expr_dump(expr
->args
[i
], indent
+ 2);
549 void pet_expr_dump(struct pet_expr
*expr
)
554 /* Does "expr" represent an access to an unnamed space, i.e.,
555 * does it represent an affine expression?
557 int pet_expr_is_affine(struct pet_expr
*expr
)
563 if (expr
->type
!= pet_expr_access
)
566 has_id
= isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
);
573 /* Return the identifier of the array accessed by "expr".
575 * If "expr" represents a member access, then return the identifier
576 * of the outer structure array.
578 __isl_give isl_id
*pet_expr_access_get_id(struct pet_expr
*expr
)
582 if (expr
->type
!= pet_expr_access
)
585 if (isl_map_range_is_wrapping(expr
->acc
.access
)) {
589 space
= isl_map_get_space(expr
->acc
.access
);
590 space
= isl_space_range(space
);
591 while (space
&& isl_space_is_wrapping(space
))
592 space
= isl_space_domain(isl_space_unwrap(space
));
593 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
594 isl_space_free(space
);
599 return isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
602 /* Align the parameters of expr->acc.index and expr->acc.access.
604 struct pet_expr
*pet_expr_access_align_params(struct pet_expr
*expr
)
608 if (expr
->type
!= pet_expr_access
)
609 return pet_expr_free(expr
);
611 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
612 isl_multi_pw_aff_get_space(expr
->acc
.index
));
613 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
614 isl_map_get_space(expr
->acc
.access
));
615 if (!expr
->acc
.access
|| !expr
->acc
.index
)
616 return pet_expr_free(expr
);
621 /* Does "expr" represent an access to a scalar, i.e., zero-dimensional array?
623 int pet_expr_is_scalar_access(struct pet_expr
*expr
)
627 if (expr
->type
!= pet_expr_access
)
630 return isl_map_dim(expr
->acc
.access
, isl_dim_out
) == 0;
633 /* Return 1 if the two pet_exprs are equivalent.
635 int pet_expr_is_equal(struct pet_expr
*expr1
, struct pet_expr
*expr2
)
639 if (!expr1
|| !expr2
)
642 if (expr1
->type
!= expr2
->type
)
644 if (expr1
->n_arg
!= expr2
->n_arg
)
646 for (i
= 0; i
< expr1
->n_arg
; ++i
)
647 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
649 switch (expr1
->type
) {
650 case pet_expr_double
:
651 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
653 if (expr1
->d
.val
!= expr2
->d
.val
)
657 if (!isl_val_eq(expr1
->i
, expr2
->i
))
660 case pet_expr_access
:
661 if (expr1
->acc
.read
!= expr2
->acc
.read
)
663 if (expr1
->acc
.write
!= expr2
->acc
.write
)
665 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
667 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
669 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
671 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
673 if (!isl_multi_pw_aff_plain_is_equal(expr1
->acc
.index
,
678 if (expr1
->op
!= expr2
->op
)
682 if (strcmp(expr1
->name
, expr2
->name
))
686 if (strcmp(expr1
->type_name
, expr2
->type_name
))
694 /* Add extra conditions on the parameters to all access relations in "expr".
696 * The conditions are not added to the index expression. Instead, they
697 * are used to try and simplify the index expression.
699 struct pet_expr
*pet_expr_restrict(struct pet_expr
*expr
,
700 __isl_take isl_set
*cond
)
707 for (i
= 0; i
< expr
->n_arg
; ++i
) {
708 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
714 if (expr
->type
== pet_expr_access
) {
715 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
717 expr
->acc
.index
= isl_multi_pw_aff_gist_params(
718 expr
->acc
.index
, isl_set_copy(cond
));
719 if (!expr
->acc
.access
|| !expr
->acc
.index
)
727 return pet_expr_free(expr
);
730 /* Tag the access relation "access" with "id".
731 * That is, insert the id as the range of a wrapped relation
732 * in the domain of "access".
734 * If "access" is of the form
738 * then the result is of the form
740 * [D[i] -> id[]] -> A[a]
742 static __isl_give isl_map
*tag_access(__isl_take isl_map
*access
,
743 __isl_take isl_id
*id
)
748 space
= isl_space_range(isl_map_get_space(access
));
749 space
= isl_space_from_range(space
);
750 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
751 add_tag
= isl_map_universe(space
);
752 access
= isl_map_domain_product(access
, add_tag
);
757 /* Modify all expressions of type pet_expr_access in "expr"
758 * by calling "fn" on them.
760 struct pet_expr
*pet_expr_map_access(struct pet_expr
*expr
,
761 struct pet_expr
*(*fn
)(struct pet_expr
*expr
, void *user
),
769 for (i
= 0; i
< expr
->n_arg
; ++i
) {
770 expr
->args
[i
] = pet_expr_map_access(expr
->args
[i
], fn
, user
);
772 return pet_expr_free(expr
);
775 if (expr
->type
== pet_expr_access
)
776 expr
= fn(expr
, user
);
781 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
783 * Return -1 on error (where fn return a negative value is treated as an error).
784 * Otherwise return 0.
786 int pet_expr_foreach_access_expr(struct pet_expr
*expr
,
787 int (*fn
)(struct pet_expr
*expr
, void *user
), void *user
)
794 for (i
= 0; i
< expr
->n_arg
; ++i
)
795 if (pet_expr_foreach_access_expr(expr
->args
[i
], fn
, user
) < 0)
798 if (expr
->type
== pet_expr_access
)
799 return fn(expr
, user
);
804 /* Modify the access relation and index expression
805 * of the given access expression
806 * based on the given iteration space transformation.
807 * In particular, precompose the access relation and index expression
808 * with the update function.
810 * If the access has any arguments then the domain of the access relation
811 * is a wrapped mapping from the iteration space to the space of
812 * argument values. We only need to change the domain of this wrapped
813 * mapping, so we extend the input transformation with an identity mapping
814 * on the space of argument values.
816 static struct pet_expr
*update_domain(struct pet_expr
*expr
, void *user
)
818 isl_multi_pw_aff
*update
= user
;
821 update
= isl_multi_pw_aff_copy(update
);
823 space
= isl_map_get_space(expr
->acc
.access
);
824 space
= isl_space_domain(space
);
825 if (!isl_space_is_wrapping(space
))
826 isl_space_free(space
);
828 isl_multi_pw_aff
*id
;
829 space
= isl_space_unwrap(space
);
830 space
= isl_space_range(space
);
831 space
= isl_space_map_from_set(space
);
832 id
= isl_multi_pw_aff_identity(space
);
833 update
= isl_multi_pw_aff_product(update
, id
);
836 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
838 isl_multi_pw_aff_copy(update
));
839 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
840 expr
->acc
.index
, update
);
841 if (!expr
->acc
.access
|| !expr
->acc
.index
)
842 return pet_expr_free(expr
);
847 /* Modify all access relations in "expr" by precomposing them with
848 * the given iteration space transformation.
850 static struct pet_expr
*expr_update_domain(struct pet_expr
*expr
,
851 __isl_take isl_multi_pw_aff
*update
)
853 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
854 isl_multi_pw_aff_free(update
);
858 /* Construct a pet_stmt with given line number and statement
859 * number from a pet_expr.
860 * The initial iteration domain is the zero-dimensional universe.
861 * The name of the domain is given by "label" if it is non-NULL.
862 * Otherwise, the name is constructed as S_<id>.
863 * The domains of all access relations are modified to refer
864 * to the statement iteration domain.
866 struct pet_stmt
*pet_stmt_from_pet_expr(isl_ctx
*ctx
, int line
,
867 __isl_take isl_id
*label
, int id
, struct pet_expr
*expr
)
869 struct pet_stmt
*stmt
;
873 isl_multi_pw_aff
*add_name
;
879 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
883 dim
= isl_space_set_alloc(ctx
, 0, 0);
885 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, label
);
887 snprintf(name
, sizeof(name
), "S_%d", id
);
888 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, name
);
890 dom
= isl_set_universe(isl_space_copy(dim
));
891 sched
= isl_map_from_domain(isl_set_copy(dom
));
893 dim
= isl_space_from_domain(dim
);
894 add_name
= isl_multi_pw_aff_zero(dim
);
895 expr
= expr_update_domain(expr
, add_name
);
899 stmt
->schedule
= sched
;
902 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
903 return pet_stmt_free(stmt
);
912 void *pet_stmt_free(struct pet_stmt
*stmt
)
919 isl_set_free(stmt
->domain
);
920 isl_map_free(stmt
->schedule
);
921 pet_expr_free(stmt
->body
);
923 for (i
= 0; i
< stmt
->n_arg
; ++i
)
924 pet_expr_free(stmt
->args
[i
]);
931 /* Return the iteration space of "stmt".
933 * If the statement has arguments, then stmt->domain is a wrapped map
934 * mapping the iteration domain to the values of the arguments
935 * for which this statement is executed.
936 * In this case, we need to extract the domain space of this wrapped map.
938 __isl_give isl_space
*pet_stmt_get_space(struct pet_stmt
*stmt
)
945 space
= isl_set_get_space(stmt
->domain
);
946 if (isl_space_is_wrapping(space
))
947 space
= isl_space_domain(isl_space_unwrap(space
));
952 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
959 fprintf(stderr
, "%*s%d\n", indent
, "", stmt
->line
);
960 fprintf(stderr
, "%*s", indent
, "");
961 isl_set_dump(stmt
->domain
);
962 fprintf(stderr
, "%*s", indent
, "");
963 isl_map_dump(stmt
->schedule
);
964 expr_dump(stmt
->body
, indent
);
965 for (i
= 0; i
< stmt
->n_arg
; ++i
)
966 expr_dump(stmt
->args
[i
], indent
+ 2);
969 void pet_stmt_dump(struct pet_stmt
*stmt
)
974 /* Allocate a new pet_type with the given "name" and "definition".
976 struct pet_type
*pet_type_alloc(isl_ctx
*ctx
, const char *name
,
977 const char *definition
)
979 struct pet_type
*type
;
981 type
= isl_alloc_type(ctx
, struct pet_type
);
985 type
->name
= strdup(name
);
986 type
->definition
= strdup(definition
);
988 if (!type
->name
|| !type
->definition
)
989 return pet_type_free(type
);
994 /* Free "type" and return NULL.
996 struct pet_type
*pet_type_free(struct pet_type
*type
)
1002 free(type
->definition
);
1008 struct pet_array
*pet_array_free(struct pet_array
*array
)
1013 isl_set_free(array
->context
);
1014 isl_set_free(array
->extent
);
1015 isl_set_free(array
->value_bounds
);
1016 free(array
->element_type
);
1022 void pet_array_dump(struct pet_array
*array
)
1027 isl_set_dump(array
->context
);
1028 isl_set_dump(array
->extent
);
1029 isl_set_dump(array
->value_bounds
);
1030 fprintf(stderr
, "%s%s%s\n", array
->element_type
,
1031 array
->element_is_record
? " element-is-record" : "",
1032 array
->live_out
? " live-out" : "");
1035 /* Alloc a pet_scop structure, with extra room for information that
1036 * is only used during parsing.
1038 struct pet_scop
*pet_scop_alloc(isl_ctx
*ctx
)
1040 return &isl_calloc_type(ctx
, struct pet_scop_ext
)->scop
;
1043 /* Construct a pet_scop with room for n statements.
1045 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
1048 struct pet_scop
*scop
;
1050 scop
= pet_scop_alloc(ctx
);
1054 space
= isl_space_params_alloc(ctx
, 0);
1055 scop
->context
= isl_set_universe(isl_space_copy(space
));
1056 scop
->context_value
= isl_set_universe(space
);
1057 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
1058 if (!scop
->context
|| !scop
->stmts
)
1059 return pet_scop_free(scop
);
1066 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
1068 return scop_alloc(ctx
, 0);
1071 /* Update "context" with respect to the valid parameter values for "access".
1073 static __isl_give isl_set
*access_extract_context(__isl_keep isl_map
*access
,
1074 __isl_take isl_set
*context
)
1076 context
= isl_set_intersect(context
,
1077 isl_map_params(isl_map_copy(access
)));
1081 /* Update "context" with respect to the valid parameter values for "expr".
1083 * If "expr" represents a conditional operator, then a parameter value
1084 * needs to be valid for the condition and for at least one of the
1085 * remaining two arguments.
1086 * If the condition is an affine expression, then we can be a bit more specific.
1087 * The parameter then has to be valid for the second argument for
1088 * non-zero accesses and valid for the third argument for zero accesses.
1090 static __isl_give isl_set
*expr_extract_context(struct pet_expr
*expr
,
1091 __isl_take isl_set
*context
)
1095 if (expr
->type
== pet_expr_op
&& expr
->op
== pet_op_cond
) {
1097 isl_set
*context1
, *context2
;
1099 is_aff
= pet_expr_is_affine(expr
->args
[0]);
1103 context
= expr_extract_context(expr
->args
[0], context
);
1104 context1
= expr_extract_context(expr
->args
[1],
1105 isl_set_copy(context
));
1106 context2
= expr_extract_context(expr
->args
[2], context
);
1112 access
= isl_map_copy(expr
->args
[0]->acc
.access
);
1113 access
= isl_map_fix_si(access
, isl_dim_out
, 0, 0);
1114 zero_set
= isl_map_params(access
);
1115 context1
= isl_set_subtract(context1
,
1116 isl_set_copy(zero_set
));
1117 context2
= isl_set_intersect(context2
, zero_set
);
1120 context
= isl_set_union(context1
, context2
);
1121 context
= isl_set_coalesce(context
);
1126 for (i
= 0; i
< expr
->n_arg
; ++i
)
1127 context
= expr_extract_context(expr
->args
[i
], context
);
1129 if (expr
->type
== pet_expr_access
)
1130 context
= access_extract_context(expr
->acc
.access
, context
);
1134 isl_set_free(context
);
1138 /* Update "context" with respect to the valid parameter values for "stmt".
1140 * If the statement is an assume statement with an affine expression,
1141 * then intersect "context" with that expression.
1142 * Otherwise, intersect "context" with the contexts of the expressions
1145 static __isl_give isl_set
*stmt_extract_context(struct pet_stmt
*stmt
,
1146 __isl_take isl_set
*context
)
1150 if (pet_stmt_is_assume(stmt
) &&
1151 pet_expr_is_affine(stmt
->body
->args
[0])) {
1152 isl_multi_pw_aff
*index
;
1156 index
= stmt
->body
->args
[0]->acc
.index
;
1157 pa
= isl_multi_pw_aff_get_pw_aff(index
, 0);
1158 cond
= isl_set_params(isl_pw_aff_non_zero_set(pa
));
1159 return isl_set_intersect(context
, cond
);
1162 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1163 context
= expr_extract_context(stmt
->args
[i
], context
);
1165 context
= expr_extract_context(stmt
->body
, context
);
1170 /* Construct a pet_scop that contains the given pet_stmt.
1172 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
1174 struct pet_scop
*scop
;
1179 scop
= scop_alloc(ctx
, 1);
1183 scop
->context
= stmt_extract_context(stmt
, scop
->context
);
1187 scop
->stmts
[0] = stmt
;
1191 pet_stmt_free(stmt
);
1192 pet_scop_free(scop
);
1196 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
1197 * does it represent an affine expression?
1199 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff
*mpa
)
1203 has_id
= isl_multi_pw_aff_has_tuple_id(mpa
, isl_dim_out
);
1210 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
1212 static __isl_give isl_pw_aff
*indicator_function(__isl_take isl_set
*set
,
1213 __isl_take isl_set
*dom
)
1216 pa
= isl_set_indicator_function(set
);
1217 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
1221 /* Return "lhs || rhs", defined on the shared definition domain.
1223 static __isl_give isl_pw_aff
*pw_aff_or(__isl_take isl_pw_aff
*lhs
,
1224 __isl_take isl_pw_aff
*rhs
)
1229 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs
)),
1230 isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
1231 cond
= isl_set_union(isl_pw_aff_non_zero_set(lhs
),
1232 isl_pw_aff_non_zero_set(rhs
));
1233 cond
= isl_set_coalesce(cond
);
1234 return indicator_function(cond
, dom
);
1237 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
1238 * ext may be equal to either ext1 or ext2.
1240 * The two skips that need to be combined are assumed to be affine expressions.
1242 * We need to skip in ext if we need to skip in either ext1 or ext2.
1243 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
1245 static struct pet_scop_ext
*combine_skips(struct pet_scop_ext
*ext
,
1246 struct pet_scop_ext
*ext1
, struct pet_scop_ext
*ext2
,
1249 isl_pw_aff
*skip
, *skip1
, *skip2
;
1253 if (!ext1
->skip
[type
] && !ext2
->skip
[type
])
1255 if (!ext1
->skip
[type
]) {
1258 ext
->skip
[type
] = ext2
->skip
[type
];
1259 ext2
->skip
[type
] = NULL
;
1262 if (!ext2
->skip
[type
]) {
1265 ext
->skip
[type
] = ext1
->skip
[type
];
1266 ext1
->skip
[type
] = NULL
;
1270 if (!multi_pw_aff_is_affine(ext1
->skip
[type
]) ||
1271 !multi_pw_aff_is_affine(ext2
->skip
[type
]))
1272 isl_die(isl_multi_pw_aff_get_ctx(ext1
->skip
[type
]),
1273 isl_error_internal
, "can only combine affine skips",
1276 skip1
= isl_multi_pw_aff_get_pw_aff(ext1
->skip
[type
], 0);
1277 skip2
= isl_multi_pw_aff_get_pw_aff(ext2
->skip
[type
], 0);
1278 skip
= pw_aff_or(skip1
, skip2
);
1279 isl_multi_pw_aff_free(ext1
->skip
[type
]);
1280 ext1
->skip
[type
] = NULL
;
1281 isl_multi_pw_aff_free(ext2
->skip
[type
]);
1282 ext2
->skip
[type
] = NULL
;
1283 ext
->skip
[type
] = isl_multi_pw_aff_from_pw_aff(skip
);
1284 if (!ext
->skip
[type
])
1289 pet_scop_free(&ext
->scop
);
1293 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
1294 * where type takes on the values pet_skip_now and pet_skip_later.
1295 * scop may be equal to either scop1 or scop2.
1297 static struct pet_scop
*scop_combine_skips(struct pet_scop
*scop
,
1298 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
1300 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1301 struct pet_scop_ext
*ext1
= (struct pet_scop_ext
*) scop1
;
1302 struct pet_scop_ext
*ext2
= (struct pet_scop_ext
*) scop2
;
1304 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_now
);
1305 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_later
);
1309 /* Update scop->start and scop->end to include the region from "start"
1310 * to "end". In particular, if scop->end == 0, then "scop" does not
1311 * have any offset information yet and we simply take the information
1312 * from "start" and "end". Otherwise, we update the fields if the
1313 * region from "start" to "end" is not already included.
1315 struct pet_scop
*pet_scop_update_start_end(struct pet_scop
*scop
,
1316 unsigned start
, unsigned end
)
1320 if (scop
->end
== 0) {
1321 scop
->start
= start
;
1324 if (start
< scop
->start
)
1325 scop
->start
= start
;
1326 if (end
> scop
->end
)
1333 /* Does "implication" appear in the list of implications of "scop"?
1335 static int is_known_implication(struct pet_scop
*scop
,
1336 struct pet_implication
*implication
)
1340 for (i
= 0; i
< scop
->n_implication
; ++i
) {
1341 struct pet_implication
*pi
= scop
->implications
[i
];
1344 if (pi
->satisfied
!= implication
->satisfied
)
1346 equal
= isl_map_is_equal(pi
->extension
, implication
->extension
);
1356 /* Store the concatenation of the implications of "scop1" and "scop2"
1357 * in "scop", removing duplicates (i.e., implications in "scop2" that
1358 * already appear in "scop1").
1360 static struct pet_scop
*scop_collect_implications(isl_ctx
*ctx
,
1361 struct pet_scop
*scop
, struct pet_scop
*scop1
, struct pet_scop
*scop2
)
1368 if (scop2
->n_implication
== 0) {
1369 scop
->n_implication
= scop1
->n_implication
;
1370 scop
->implications
= scop1
->implications
;
1371 scop1
->n_implication
= 0;
1372 scop1
->implications
= NULL
;
1376 if (scop1
->n_implication
== 0) {
1377 scop
->n_implication
= scop2
->n_implication
;
1378 scop
->implications
= scop2
->implications
;
1379 scop2
->n_implication
= 0;
1380 scop2
->implications
= NULL
;
1384 scop
->implications
= isl_calloc_array(ctx
, struct pet_implication
*,
1385 scop1
->n_implication
+ scop2
->n_implication
);
1386 if (!scop
->implications
)
1387 return pet_scop_free(scop
);
1389 for (i
= 0; i
< scop1
->n_implication
; ++i
) {
1390 scop
->implications
[i
] = scop1
->implications
[i
];
1391 scop1
->implications
[i
] = NULL
;
1394 scop
->n_implication
= scop1
->n_implication
;
1395 j
= scop1
->n_implication
;
1396 for (i
= 0; i
< scop2
->n_implication
; ++i
) {
1399 known
= is_known_implication(scop
, scop2
->implications
[i
]);
1401 return pet_scop_free(scop
);
1404 scop
->implications
[j
++] = scop2
->implications
[i
];
1405 scop2
->implications
[i
] = NULL
;
1407 scop
->n_implication
= j
;
1412 /* Combine the offset information of "scop1" and "scop2" into "scop".
1414 static struct pet_scop
*scop_combine_start_end(struct pet_scop
*scop
,
1415 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
1418 scop
= pet_scop_update_start_end(scop
,
1419 scop1
->start
, scop1
->end
);
1421 scop
= pet_scop_update_start_end(scop
,
1422 scop2
->start
, scop2
->end
);
1426 /* Construct a pet_scop that contains the offset information,
1427 * arrays, statements and skip information in "scop1" and "scop2".
1429 static struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1430 struct pet_scop
*scop2
)
1433 struct pet_scop
*scop
= NULL
;
1435 if (!scop1
|| !scop2
)
1438 if (scop1
->n_stmt
== 0) {
1439 scop2
= scop_combine_skips(scop2
, scop1
, scop2
);
1440 pet_scop_free(scop1
);
1444 if (scop2
->n_stmt
== 0) {
1445 scop1
= scop_combine_skips(scop1
, scop1
, scop2
);
1446 pet_scop_free(scop2
);
1450 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
1454 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*,
1455 scop1
->n_array
+ scop2
->n_array
);
1458 scop
->n_array
= scop1
->n_array
+ scop2
->n_array
;
1460 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
1461 scop
->stmts
[i
] = scop1
->stmts
[i
];
1462 scop1
->stmts
[i
] = NULL
;
1465 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
1466 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
1467 scop2
->stmts
[i
] = NULL
;
1470 for (i
= 0; i
< scop1
->n_array
; ++i
) {
1471 scop
->arrays
[i
] = scop1
->arrays
[i
];
1472 scop1
->arrays
[i
] = NULL
;
1475 for (i
= 0; i
< scop2
->n_array
; ++i
) {
1476 scop
->arrays
[scop1
->n_array
+ i
] = scop2
->arrays
[i
];
1477 scop2
->arrays
[i
] = NULL
;
1480 scop
= scop_collect_implications(ctx
, scop
, scop1
, scop2
);
1481 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop1
->context
));
1482 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop2
->context
));
1483 scop
= scop_combine_skips(scop
, scop1
, scop2
);
1484 scop
= scop_combine_start_end(scop
, scop1
, scop2
);
1486 pet_scop_free(scop1
);
1487 pet_scop_free(scop2
);
1490 pet_scop_free(scop1
);
1491 pet_scop_free(scop2
);
1492 pet_scop_free(scop
);
1496 /* Apply the skip condition "skip" to "scop".
1497 * That is, make sure "scop" is not executed when the condition holds.
1499 * If "skip" is an affine expression, we add the conditions under
1500 * which the expression is zero to the iteration domains.
1501 * Otherwise, we add a filter on the variable attaining the value zero.
1503 static struct pet_scop
*restrict_skip(struct pet_scop
*scop
,
1504 __isl_take isl_multi_pw_aff
*skip
)
1513 is_aff
= multi_pw_aff_is_affine(skip
);
1518 return pet_scop_filter(scop
, skip
, 0);
1520 pa
= isl_multi_pw_aff_get_pw_aff(skip
, 0);
1521 isl_multi_pw_aff_free(skip
);
1522 zero
= isl_set_params(isl_pw_aff_zero_set(pa
));
1523 scop
= pet_scop_restrict(scop
, zero
);
1527 isl_multi_pw_aff_free(skip
);
1528 return pet_scop_free(scop
);
1531 /* Construct a pet_scop that contains the arrays, statements and
1532 * skip information in "scop1" and "scop2", where the two scops
1533 * are executed "in sequence". That is, breaks and continues
1534 * in scop1 have an effect on scop2.
1536 struct pet_scop
*pet_scop_add_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1537 struct pet_scop
*scop2
)
1539 if (scop1
&& pet_scop_has_skip(scop1
, pet_skip_now
))
1540 scop2
= restrict_skip(scop2
,
1541 pet_scop_get_skip(scop1
, pet_skip_now
));
1542 return pet_scop_add(ctx
, scop1
, scop2
);
1545 /* Construct a pet_scop that contains the arrays, statements and
1546 * skip information in "scop1" and "scop2", where the two scops
1547 * are executed "in parallel". That is, any break or continue
1548 * in scop1 has no effect on scop2.
1550 struct pet_scop
*pet_scop_add_par(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1551 struct pet_scop
*scop2
)
1553 return pet_scop_add(ctx
, scop1
, scop2
);
1556 void *pet_implication_free(struct pet_implication
*implication
)
1563 isl_map_free(implication
->extension
);
1569 struct pet_scop
*pet_scop_free(struct pet_scop
*scop
)
1572 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1576 isl_set_free(scop
->context
);
1577 isl_set_free(scop
->context_value
);
1579 for (i
= 0; i
< scop
->n_type
; ++i
)
1580 pet_type_free(scop
->types
[i
]);
1583 for (i
= 0; i
< scop
->n_array
; ++i
)
1584 pet_array_free(scop
->arrays
[i
]);
1587 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1588 pet_stmt_free(scop
->stmts
[i
]);
1590 if (scop
->implications
)
1591 for (i
= 0; i
< scop
->n_implication
; ++i
)
1592 pet_implication_free(scop
->implications
[i
]);
1593 free(scop
->implications
);
1594 isl_multi_pw_aff_free(ext
->skip
[pet_skip_now
]);
1595 isl_multi_pw_aff_free(ext
->skip
[pet_skip_later
]);
1600 void pet_type_dump(struct pet_type
*type
)
1605 fprintf(stderr
, "%s -> %s\n", type
->name
, type
->definition
);
1608 void pet_implication_dump(struct pet_implication
*implication
)
1613 fprintf(stderr
, "%d\n", implication
->satisfied
);
1614 isl_map_dump(implication
->extension
);
1617 void pet_scop_dump(struct pet_scop
*scop
)
1620 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1625 isl_set_dump(scop
->context
);
1626 isl_set_dump(scop
->context_value
);
1627 for (i
= 0; i
< scop
->n_type
; ++i
)
1628 pet_type_dump(scop
->types
[i
]);
1629 for (i
= 0; i
< scop
->n_array
; ++i
)
1630 pet_array_dump(scop
->arrays
[i
]);
1631 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1632 pet_stmt_dump(scop
->stmts
[i
]);
1633 for (i
= 0; i
< scop
->n_implication
; ++i
)
1634 pet_implication_dump(scop
->implications
[i
]);
1637 fprintf(stderr
, "skip\n");
1638 isl_multi_pw_aff_dump(ext
->skip
[0]);
1639 isl_multi_pw_aff_dump(ext
->skip
[1]);
1643 /* Return 1 if the two pet_arrays are equivalent.
1645 * We don't compare element_size as this may be target dependent.
1647 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
1649 if (!array1
|| !array2
)
1652 if (!isl_set_is_equal(array1
->context
, array2
->context
))
1654 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
1656 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
1658 if (array1
->value_bounds
&&
1659 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
1661 if (strcmp(array1
->element_type
, array2
->element_type
))
1663 if (array1
->element_is_record
!= array2
->element_is_record
)
1665 if (array1
->live_out
!= array2
->live_out
)
1667 if (array1
->uniquely_defined
!= array2
->uniquely_defined
)
1669 if (array1
->declared
!= array2
->declared
)
1671 if (array1
->exposed
!= array2
->exposed
)
1677 /* Return 1 if the two pet_stmts are equivalent.
1679 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
1683 if (!stmt1
|| !stmt2
)
1686 if (stmt1
->line
!= stmt2
->line
)
1688 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
1690 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
1692 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
1694 if (stmt1
->n_arg
!= stmt2
->n_arg
)
1696 for (i
= 0; i
< stmt1
->n_arg
; ++i
) {
1697 if (!pet_expr_is_equal(stmt1
->args
[i
], stmt2
->args
[i
]))
1704 /* Return 1 if the two pet_types are equivalent.
1706 * We only compare the names of the types since the exact representation
1707 * of the definition may depend on the version of clang being used.
1709 int pet_type_is_equal(struct pet_type
*type1
, struct pet_type
*type2
)
1711 if (!type1
|| !type2
)
1714 if (strcmp(type1
->name
, type2
->name
))
1720 /* Return 1 if the two pet_implications are equivalent.
1722 int pet_implication_is_equal(struct pet_implication
*implication1
,
1723 struct pet_implication
*implication2
)
1725 if (!implication1
|| !implication2
)
1728 if (implication1
->satisfied
!= implication2
->satisfied
)
1730 if (!isl_map_is_equal(implication1
->extension
, implication2
->extension
))
1736 /* Return 1 if the two pet_scops are equivalent.
1738 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
1742 if (!scop1
|| !scop2
)
1745 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
1747 if (!isl_set_is_equal(scop1
->context_value
, scop2
->context_value
))
1750 if (scop1
->n_type
!= scop2
->n_type
)
1752 for (i
= 0; i
< scop1
->n_type
; ++i
)
1753 if (!pet_type_is_equal(scop1
->types
[i
], scop2
->types
[i
]))
1756 if (scop1
->n_array
!= scop2
->n_array
)
1758 for (i
= 0; i
< scop1
->n_array
; ++i
)
1759 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
1762 if (scop1
->n_stmt
!= scop2
->n_stmt
)
1764 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
1765 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
1768 if (scop1
->n_implication
!= scop2
->n_implication
)
1770 for (i
= 0; i
< scop1
->n_implication
; ++i
)
1771 if (!pet_implication_is_equal(scop1
->implications
[i
],
1772 scop2
->implications
[i
]))
1778 /* Prefix the schedule of "stmt" with an extra dimension with constant
1781 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
1786 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
1787 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
1788 if (!stmt
->schedule
)
1789 return pet_stmt_free(stmt
);
1794 /* Prefix the schedules of all statements in "scop" with an extra
1795 * dimension with constant value "pos".
1797 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
1804 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1805 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
1806 if (!scop
->stmts
[i
])
1807 return pet_scop_free(scop
);
1813 /* Given a set with a parameter at "param_pos" that refers to the
1814 * iterator, "move" the iterator to the first set dimension.
1815 * That is, essentially equate the parameter to the first set dimension
1816 * and then project it out.
1818 * The first set dimension may however refer to a virtual iterator,
1819 * while the parameter refers to the "real" iterator.
1820 * We therefore need to take into account the affine expression "iv_map", which
1821 * expresses the real iterator in terms of the virtual iterator.
1822 * In particular, we equate the set dimension to the input of the map
1823 * and the parameter to the output of the map and then project out
1824 * everything we don't need anymore.
1826 static __isl_give isl_set
*internalize_iv(__isl_take isl_set
*set
,
1827 int param_pos
, __isl_take isl_aff
*iv_map
)
1829 isl_map
*map
, *map2
;
1830 map
= isl_map_from_domain(set
);
1831 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
1832 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_out
, 0);
1833 map2
= isl_map_from_aff(iv_map
);
1834 map2
= isl_map_align_params(map2
, isl_map_get_space(map
));
1835 map
= isl_map_apply_range(map
, map2
);
1836 map
= isl_map_equate(map
, isl_dim_param
, param_pos
, isl_dim_out
, 0);
1837 map
= isl_map_project_out(map
, isl_dim_param
, param_pos
, 1);
1838 return isl_map_domain(map
);
1841 /* Data used in embed_access.
1842 * extend adds an iterator to the iteration domain (through precomposition).
1843 * iv_map expresses the real iterator in terms of the virtual iterator
1844 * var_id represents the induction variable of the corresponding loop
1846 struct pet_embed_access
{
1847 isl_multi_pw_aff
*extend
;
1852 /* Given an index expression, return an expression for the outer iterator.
1854 static __isl_give isl_aff
*index_outer_iterator(
1855 __isl_take isl_multi_pw_aff
*index
)
1858 isl_local_space
*ls
;
1860 space
= isl_multi_pw_aff_get_domain_space(index
);
1861 isl_multi_pw_aff_free(index
);
1863 ls
= isl_local_space_from_space(space
);
1864 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1867 /* Replace an index expression that references the new (outer) iterator variable
1868 * by one that references the corresponding (real) iterator.
1870 * The input index expression is of the form
1872 * { S[i',...] -> i[] }
1874 * where i' refers to the virtual iterator.
1876 * iv_map is of the form
1880 * Return the index expression
1882 * { S[i',...] -> [i] }
1884 static __isl_give isl_multi_pw_aff
*replace_by_iterator(
1885 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_aff
*iv_map
)
1890 aff
= index_outer_iterator(index
);
1891 space
= isl_aff_get_space(aff
);
1892 iv_map
= isl_aff_align_params(iv_map
, space
);
1893 aff
= isl_aff_pullback_aff(iv_map
, aff
);
1895 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1898 /* Given an index expression "index" that refers to the (real) iterator
1899 * through the parameter at position "pos", plug in "iv_map", expressing
1900 * the real iterator in terms of the virtual (outer) iterator.
1902 * In particular, the index expression is of the form
1904 * [..., i, ...] -> { S[i',...] -> ... i ... }
1906 * where i refers to the real iterator and i' refers to the virtual iterator.
1908 * iv_map is of the form
1912 * Return the index expression
1914 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1917 * We first move the parameter to the input
1919 * [..., ...] -> { [i, i',...] -> ... i ... }
1923 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1925 * and then combine the two to obtain the desired result.
1927 static __isl_give isl_multi_pw_aff
*index_internalize_iv(
1928 __isl_take isl_multi_pw_aff
*index
, int pos
, __isl_take isl_aff
*iv_map
)
1930 isl_space
*space
= isl_multi_pw_aff_get_domain_space(index
);
1933 space
= isl_space_drop_dims(space
, isl_dim_param
, pos
, 1);
1934 index
= isl_multi_pw_aff_move_dims(index
, isl_dim_in
, 0,
1935 isl_dim_param
, pos
, 1);
1937 space
= isl_space_map_from_set(space
);
1938 ma
= isl_multi_aff_identity(isl_space_copy(space
));
1939 iv_map
= isl_aff_align_params(iv_map
, space
);
1940 iv_map
= isl_aff_pullback_aff(iv_map
, isl_multi_aff_get_aff(ma
, 0));
1941 ma
= isl_multi_aff_flat_range_product(
1942 isl_multi_aff_from_aff(iv_map
), ma
);
1943 index
= isl_multi_pw_aff_pullback_multi_aff(index
, ma
);
1948 /* Does the index expression "index" reference a virtual array, i.e.,
1949 * one with user pointer equal to NULL?
1950 * A virtual array does not have any members.
1952 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff
*index
)
1957 if (!isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
1959 if (isl_multi_pw_aff_range_is_wrapping(index
))
1961 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
1962 is_virtual
= !isl_id_get_user(id
);
1968 /* Does the access relation "access" reference a virtual array, i.e.,
1969 * one with user pointer equal to NULL?
1970 * A virtual array does not have any members.
1972 static int access_is_virtual_array(__isl_keep isl_map
*access
)
1977 if (!isl_map_has_tuple_id(access
, isl_dim_out
))
1979 if (isl_map_range_is_wrapping(access
))
1981 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1982 is_virtual
= !isl_id_get_user(id
);
1988 /* Embed the given index expression in an extra outer loop.
1989 * The domain of the index expression has already been updated.
1991 * If the access refers to the induction variable, then it is
1992 * turned into an access to the set of integers with index (and value)
1993 * equal to the induction variable.
1995 * If the accessed array is a virtual array (with user
1996 * pointer equal to NULL), as created by create_test_index,
1997 * then it is extended along with the domain of the index expression.
1999 static __isl_give isl_multi_pw_aff
*embed_index_expression(
2000 __isl_take isl_multi_pw_aff
*index
, struct pet_embed_access
*data
)
2002 isl_id
*array_id
= NULL
;
2005 if (isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
2006 array_id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
2007 if (array_id
== data
->var_id
) {
2008 index
= replace_by_iterator(index
, isl_aff_copy(data
->iv_map
));
2009 } else if (index_is_virtual_array(index
)) {
2011 isl_multi_pw_aff
*mpa
;
2013 aff
= index_outer_iterator(isl_multi_pw_aff_copy(index
));
2014 mpa
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
2015 index
= isl_multi_pw_aff_flat_range_product(mpa
, index
);
2016 index
= isl_multi_pw_aff_set_tuple_id(index
, isl_dim_out
,
2017 isl_id_copy(array_id
));
2019 isl_id_free(array_id
);
2021 pos
= isl_multi_pw_aff_find_dim_by_id(index
,
2022 isl_dim_param
, data
->var_id
);
2024 index
= index_internalize_iv(index
, pos
,
2025 isl_aff_copy(data
->iv_map
));
2026 index
= isl_multi_pw_aff_set_dim_id(index
, isl_dim_in
, 0,
2027 isl_id_copy(data
->var_id
));
2032 /* Embed the given access relation in an extra outer loop.
2033 * The domain of the access relation has already been updated.
2035 * If the access refers to the induction variable, then it is
2036 * turned into an access to the set of integers with index (and value)
2037 * equal to the induction variable.
2039 * If the induction variable appears in the constraints (as a parameter),
2040 * then the parameter is equated to the newly introduced iteration
2041 * domain dimension and subsequently projected out.
2043 * Similarly, if the accessed array is a virtual array (with user
2044 * pointer equal to NULL), as created by create_test_index,
2045 * then it is extended along with the domain of the access.
2047 static __isl_give isl_map
*embed_access_relation(__isl_take isl_map
*access
,
2048 struct pet_embed_access
*data
)
2050 isl_id
*array_id
= NULL
;
2053 if (isl_map_has_tuple_id(access
, isl_dim_out
))
2054 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
2055 if (array_id
== data
->var_id
|| access_is_virtual_array(access
)) {
2056 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
2057 access
= isl_map_equate(access
,
2058 isl_dim_in
, 0, isl_dim_out
, 0);
2059 if (array_id
== data
->var_id
)
2060 access
= isl_map_apply_range(access
,
2061 isl_map_from_aff(isl_aff_copy(data
->iv_map
)));
2063 access
= isl_map_set_tuple_id(access
, isl_dim_out
,
2064 isl_id_copy(array_id
));
2066 isl_id_free(array_id
);
2068 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
2070 isl_set
*set
= isl_map_wrap(access
);
2071 set
= internalize_iv(set
, pos
, isl_aff_copy(data
->iv_map
));
2072 access
= isl_set_unwrap(set
);
2074 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
2075 isl_id_copy(data
->var_id
));
2080 /* Given an access expression, embed the associated access relation and
2081 * index expression in an extra outer loop.
2083 * We first update the domains to insert the extra dimension and
2084 * then update the access relation and index expression to take
2085 * into account the mapping "iv_map" from virtual iterator
2088 static struct pet_expr
*embed_access(struct pet_expr
*expr
, void *user
)
2090 struct pet_embed_access
*data
= user
;
2092 expr
= update_domain(expr
, data
->extend
);
2096 expr
->acc
.access
= embed_access_relation(expr
->acc
.access
, data
);
2097 expr
->acc
.index
= embed_index_expression(expr
->acc
.index
, data
);
2098 if (!expr
->acc
.access
|| !expr
->acc
.index
)
2099 return pet_expr_free(expr
);
2104 /* Embed all access subexpressions of "expr" in an extra loop.
2105 * "extend" inserts an outer loop iterator in the iteration domains
2106 * (through precomposition).
2107 * "iv_map" expresses the real iterator in terms of the virtual iterator
2108 * "var_id" represents the induction variable.
2110 static struct pet_expr
*expr_embed(struct pet_expr
*expr
,
2111 __isl_take isl_multi_pw_aff
*extend
, __isl_take isl_aff
*iv_map
,
2112 __isl_keep isl_id
*var_id
)
2114 struct pet_embed_access data
=
2115 { .extend
= extend
, .iv_map
= iv_map
, .var_id
= var_id
};
2117 expr
= pet_expr_map_access(expr
, &embed_access
, &data
);
2118 isl_aff_free(iv_map
);
2119 isl_multi_pw_aff_free(extend
);
2123 /* Embed the given pet_stmt in an extra outer loop with iteration domain
2124 * "dom" and schedule "sched". "var_id" represents the induction variable
2125 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
2126 * That is, it expresses the iterator that some of the parameters in "stmt"
2127 * may refer to in terms of the iterator used in "dom" and
2128 * the domain of "sched".
2130 * The iteration domain and schedule of the statement are updated
2131 * according to the iteration domain and schedule of the new loop.
2132 * If stmt->domain is a wrapped map, then the iteration domain
2133 * is the domain of this map, so we need to be careful to adjust
2136 * If the induction variable appears in the constraints (as a parameter)
2137 * of the current iteration domain or the schedule of the statement,
2138 * then the parameter is equated to the newly introduced iteration
2139 * domain dimension and subsequently projected out.
2141 * Finally, all access relations are updated based on the extra loop.
2143 static struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
,
2144 __isl_take isl_set
*dom
, __isl_take isl_map
*sched
,
2145 __isl_take isl_aff
*iv_map
, __isl_take isl_id
*var_id
)
2151 isl_multi_pw_aff
*extend
;
2156 if (isl_set_is_wrapping(stmt
->domain
)) {
2161 map
= isl_set_unwrap(stmt
->domain
);
2162 stmt_id
= isl_map_get_tuple_id(map
, isl_dim_in
);
2163 ran_dim
= isl_space_range(isl_map_get_space(map
));
2164 ext
= isl_map_from_domain_and_range(isl_set_copy(dom
),
2165 isl_set_universe(ran_dim
));
2166 map
= isl_map_flat_domain_product(ext
, map
);
2167 map
= isl_map_set_tuple_id(map
, isl_dim_in
,
2168 isl_id_copy(stmt_id
));
2169 dim
= isl_space_domain(isl_map_get_space(map
));
2170 stmt
->domain
= isl_map_wrap(map
);
2172 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
2173 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
),
2175 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
,
2176 isl_id_copy(stmt_id
));
2177 dim
= isl_set_get_space(stmt
->domain
);
2180 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
2182 stmt
->domain
= internalize_iv(stmt
->domain
, pos
,
2183 isl_aff_copy(iv_map
));
2185 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
2186 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
2187 isl_dim_in
, stmt_id
);
2189 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
2191 isl_set
*set
= isl_map_wrap(stmt
->schedule
);
2192 set
= internalize_iv(set
, pos
, isl_aff_copy(iv_map
));
2193 stmt
->schedule
= isl_set_unwrap(set
);
2196 dim
= isl_space_map_from_set(dim
);
2197 extend
= isl_multi_pw_aff_identity(dim
);
2198 extend
= isl_multi_pw_aff_drop_dims(extend
, isl_dim_out
, 0, 1);
2199 extend
= isl_multi_pw_aff_set_tuple_id(extend
, isl_dim_out
,
2200 isl_multi_pw_aff_get_tuple_id(extend
, isl_dim_in
));
2201 for (i
= 0; i
< stmt
->n_arg
; ++i
)
2202 stmt
->args
[i
] = expr_embed(stmt
->args
[i
],
2203 isl_multi_pw_aff_copy(extend
),
2204 isl_aff_copy(iv_map
), var_id
);
2205 stmt
->body
= expr_embed(stmt
->body
, extend
, iv_map
, var_id
);
2208 isl_id_free(var_id
);
2210 for (i
= 0; i
< stmt
->n_arg
; ++i
)
2212 return pet_stmt_free(stmt
);
2213 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2214 return pet_stmt_free(stmt
);
2218 isl_map_free(sched
);
2219 isl_aff_free(iv_map
);
2220 isl_id_free(var_id
);
2224 /* Embed the given pet_array in an extra outer loop with iteration domain
2226 * This embedding only has an effect on virtual arrays (those with
2227 * user pointer equal to NULL), which need to be extended along with
2228 * the iteration domain.
2230 static struct pet_array
*pet_array_embed(struct pet_array
*array
,
2231 __isl_take isl_set
*dom
)
2233 isl_id
*array_id
= NULL
;
2238 if (isl_set_has_tuple_id(array
->extent
))
2239 array_id
= isl_set_get_tuple_id(array
->extent
);
2241 if (array_id
&& !isl_id_get_user(array_id
)) {
2242 array
->extent
= isl_set_flat_product(dom
, array
->extent
);
2243 array
->extent
= isl_set_set_tuple_id(array
->extent
, array_id
);
2245 return pet_array_free(array
);
2248 isl_id_free(array_id
);
2257 /* Project out all unnamed parameters from "set" and return the result.
2259 static __isl_give isl_set
*set_project_out_unnamed_params(
2260 __isl_take isl_set
*set
)
2264 n
= isl_set_dim(set
, isl_dim_param
);
2265 for (i
= n
- 1; i
>= 0; --i
) {
2266 if (isl_set_has_dim_name(set
, isl_dim_param
, i
))
2268 set
= isl_set_project_out(set
, isl_dim_param
, i
, 1);
2274 /* Update the context with respect to an embedding into a loop
2275 * with iteration domain "dom" and induction variable "id".
2276 * "iv_map" expresses the real iterator (parameter "id") in terms
2277 * of a possibly virtual iterator (used in "dom").
2279 * If the current context is independent of "id", we don't need
2281 * Otherwise, a parameter value is invalid for the embedding if
2282 * any of the corresponding iterator values is invalid.
2283 * That is, a parameter value is valid only if all the corresponding
2284 * iterator values are valid.
2285 * We therefore compute the set of parameters
2287 * forall i in dom : valid (i)
2291 * not exists i in dom : not valid(i)
2295 * not exists i in dom \ valid(i)
2297 * Before we subtract valid(i) from dom, we first need to substitute
2298 * the real iterator for the virtual iterator.
2300 * If there are any unnamed parameters in "dom", then we consider
2301 * a parameter value to be valid if it is valid for any value of those
2302 * unnamed parameters. They are therefore projected out at the end.
2304 static __isl_give isl_set
*context_embed(__isl_take isl_set
*context
,
2305 __isl_keep isl_set
*dom
, __isl_keep isl_aff
*iv_map
,
2306 __isl_keep isl_id
*id
)
2311 pos
= isl_set_find_dim_by_id(context
, isl_dim_param
, id
);
2315 context
= isl_set_from_params(context
);
2316 context
= isl_set_add_dims(context
, isl_dim_set
, 1);
2317 context
= isl_set_equate(context
, isl_dim_param
, pos
, isl_dim_set
, 0);
2318 context
= isl_set_project_out(context
, isl_dim_param
, pos
, 1);
2319 ma
= isl_multi_aff_from_aff(isl_aff_copy(iv_map
));
2320 context
= isl_set_preimage_multi_aff(context
, ma
);
2321 context
= isl_set_subtract(isl_set_copy(dom
), context
);
2322 context
= isl_set_params(context
);
2323 context
= isl_set_complement(context
);
2324 context
= set_project_out_unnamed_params(context
);
2328 /* Update the implication with respect to an embedding into a loop
2329 * with iteration domain "dom".
2331 * Since embed_access extends virtual arrays along with the domain
2332 * of the access, we need to do the same with domain and range
2333 * of the implication. Since the original implication is only valid
2334 * within a given iteration of the loop, the extended implication
2335 * maps the extra array dimension corresponding to the extra loop
2338 static struct pet_implication
*pet_implication_embed(
2339 struct pet_implication
*implication
, __isl_take isl_set
*dom
)
2347 map
= isl_set_identity(dom
);
2348 id
= isl_map_get_tuple_id(implication
->extension
, isl_dim_in
);
2349 map
= isl_map_flat_product(map
, implication
->extension
);
2350 map
= isl_map_set_tuple_id(map
, isl_dim_in
, isl_id_copy(id
));
2351 map
= isl_map_set_tuple_id(map
, isl_dim_out
, id
);
2352 implication
->extension
= map
;
2353 if (!implication
->extension
)
2354 return pet_implication_free(implication
);
2362 /* Embed all statements and arrays in "scop" in an extra outer loop
2363 * with iteration domain "dom" and schedule "sched".
2364 * "id" represents the induction variable of the loop.
2365 * "iv_map" maps a possibly virtual iterator to the real iterator.
2366 * That is, it expresses the iterator that some of the parameters in "scop"
2367 * may refer to in terms of the iterator used in "dom" and
2368 * the domain of "sched".
2370 * Any skip conditions within the loop have no effect outside of the loop.
2371 * The caller is responsible for making sure skip[pet_skip_later] has been
2372 * taken into account.
2374 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
2375 __isl_take isl_aff
*sched
, __isl_take isl_aff
*iv_map
,
2376 __isl_take isl_id
*id
)
2381 sched_map
= isl_map_from_aff(sched
);
2386 pet_scop_reset_skip(scop
, pet_skip_now
);
2387 pet_scop_reset_skip(scop
, pet_skip_later
);
2389 scop
->context
= context_embed(scop
->context
, dom
, iv_map
, id
);
2393 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2394 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
2395 isl_set_copy(dom
), isl_map_copy(sched_map
),
2396 isl_aff_copy(iv_map
), isl_id_copy(id
));
2397 if (!scop
->stmts
[i
])
2401 for (i
= 0; i
< scop
->n_array
; ++i
) {
2402 scop
->arrays
[i
] = pet_array_embed(scop
->arrays
[i
],
2404 if (!scop
->arrays
[i
])
2408 for (i
= 0; i
< scop
->n_implication
; ++i
) {
2409 scop
->implications
[i
] =
2410 pet_implication_embed(scop
->implications
[i
],
2412 if (!scop
->implications
[i
])
2417 isl_map_free(sched_map
);
2418 isl_aff_free(iv_map
);
2423 isl_map_free(sched_map
);
2424 isl_aff_free(iv_map
);
2426 return pet_scop_free(scop
);
2429 /* Add extra conditions on the parameters to the iteration domain of "stmt".
2431 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
2432 __isl_take isl_set
*cond
)
2437 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
2442 return pet_stmt_free(stmt
);
2445 /* Add extra conditions to scop->skip[type].
2447 * The new skip condition only holds if it held before
2448 * and the condition is true. It does not hold if it did not hold
2449 * before or the condition is false.
2451 * The skip condition is assumed to be an affine expression.
2453 static struct pet_scop
*pet_scop_restrict_skip(struct pet_scop
*scop
,
2454 enum pet_skip type
, __isl_keep isl_set
*cond
)
2456 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2462 if (!ext
->skip
[type
])
2465 if (!multi_pw_aff_is_affine(ext
->skip
[type
]))
2466 isl_die(isl_multi_pw_aff_get_ctx(ext
->skip
[type
]),
2467 isl_error_internal
, "can only restrict affine skips",
2468 return pet_scop_free(scop
));
2470 skip
= isl_multi_pw_aff_get_pw_aff(ext
->skip
[type
], 0);
2471 dom
= isl_pw_aff_domain(isl_pw_aff_copy(skip
));
2472 cond
= isl_set_copy(cond
);
2473 cond
= isl_set_from_params(cond
);
2474 cond
= isl_set_intersect(cond
, isl_pw_aff_non_zero_set(skip
));
2475 skip
= indicator_function(cond
, dom
);
2476 isl_multi_pw_aff_free(ext
->skip
[type
]);
2477 ext
->skip
[type
] = isl_multi_pw_aff_from_pw_aff(skip
);
2478 if (!ext
->skip
[type
])
2479 return pet_scop_free(scop
);
2484 /* Add extra conditions on the parameters to all iteration domains
2485 * and skip conditions.
2487 * A parameter value is valid for the result if it was valid
2488 * for the original scop and satisfies "cond" or if it does
2489 * not satisfy "cond" as in this case the scop is not executed
2490 * and the original constraints on the parameters are irrelevant.
2492 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
2493 __isl_take isl_set
*cond
)
2497 scop
= pet_scop_restrict_skip(scop
, pet_skip_now
, cond
);
2498 scop
= pet_scop_restrict_skip(scop
, pet_skip_later
, cond
);
2503 scop
->context
= isl_set_intersect(scop
->context
, isl_set_copy(cond
));
2504 scop
->context
= isl_set_union(scop
->context
,
2505 isl_set_complement(isl_set_copy(cond
)));
2506 scop
->context
= isl_set_coalesce(scop
->context
);
2507 scop
->context
= set_project_out_unnamed_params(scop
->context
);
2511 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2512 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
2513 isl_set_copy(cond
));
2514 if (!scop
->stmts
[i
])
2522 return pet_scop_free(scop
);
2525 /* Insert an argument expression corresponding to "test" in front
2526 * of the list of arguments described by *n_arg and *args.
2528 static int args_insert_access(unsigned *n_arg
, struct pet_expr
***args
,
2529 __isl_keep isl_multi_pw_aff
*test
)
2532 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
2538 *args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
2542 struct pet_expr
**ext
;
2543 ext
= isl_calloc_array(ctx
, struct pet_expr
*, 1 + *n_arg
);
2546 for (i
= 0; i
< *n_arg
; ++i
)
2547 ext
[1 + i
] = (*args
)[i
];
2552 (*args
)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
2559 /* Make the expression "expr" depend on the value of "test"
2560 * being equal to "satisfied".
2562 * If "test" is an affine expression, we simply add the conditions
2563 * on the expression having the value "satisfied" to all access relations
2564 * and index expressions.
2566 * Otherwise, we add a filter to "expr" (which is then assumed to be
2567 * an access expression) corresponding to "test" being equal to "satisfied".
2569 struct pet_expr
*pet_expr_filter(struct pet_expr
*expr
,
2570 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
2575 isl_pw_multi_aff
*pma
;
2580 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
2584 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
2585 isl_multi_pw_aff_free(test
);
2587 cond
= isl_pw_aff_non_zero_set(pa
);
2589 cond
= isl_pw_aff_zero_set(pa
);
2590 return pet_expr_restrict(expr
, isl_set_params(cond
));
2593 ctx
= isl_multi_pw_aff_get_ctx(test
);
2594 if (expr
->type
!= pet_expr_access
)
2595 isl_die(ctx
, isl_error_invalid
,
2596 "can only filter access expressions", goto error
);
2598 space
= isl_space_domain(isl_map_get_space(expr
->acc
.access
));
2599 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
2600 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
2602 expr
->acc
.access
= isl_map_preimage_domain_pw_multi_aff(
2604 isl_pw_multi_aff_copy(pma
));
2605 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
2606 expr
->acc
.index
, pma
);
2607 if (!expr
->acc
.access
|| !expr
->acc
.index
)
2610 if (args_insert_access(&expr
->n_arg
, &expr
->args
, test
) < 0)
2613 isl_multi_pw_aff_free(test
);
2616 isl_multi_pw_aff_free(test
);
2617 return pet_expr_free(expr
);
2620 /* Look through the applications in "scop" for any that can be
2621 * applied to the filter expressed by "map" and "satisified".
2622 * If there is any, then apply it to "map" and return the result.
2623 * Otherwise, return "map".
2624 * "id" is the identifier of the virtual array.
2626 * We only introduce at most one implication for any given virtual array,
2627 * so we can apply the implication and return as soon as we find one.
2629 static __isl_give isl_map
*apply_implications(struct pet_scop
*scop
,
2630 __isl_take isl_map
*map
, __isl_keep isl_id
*id
, int satisfied
)
2634 for (i
= 0; i
< scop
->n_implication
; ++i
) {
2635 struct pet_implication
*pi
= scop
->implications
[i
];
2638 if (pi
->satisfied
!= satisfied
)
2640 pi_id
= isl_map_get_tuple_id(pi
->extension
, isl_dim_in
);
2645 return isl_map_apply_range(map
, isl_map_copy(pi
->extension
));
2651 /* Is the filter expressed by "test" and "satisfied" implied
2652 * by filter "pos" on "domain", with filter "expr", taking into
2653 * account the implications of "scop"?
2655 * For filter on domain implying that expressed by "test" and "satisfied",
2656 * the filter needs to be an access to the same (virtual) array as "test" and
2657 * the filter value needs to be equal to "satisfied".
2658 * Moreover, the filter access relation, possibly extended by
2659 * the implications in "scop" needs to contain "test".
2661 static int implies_filter(struct pet_scop
*scop
,
2662 __isl_keep isl_map
*domain
, int pos
, struct pet_expr
*expr
,
2663 __isl_keep isl_map
*test
, int satisfied
)
2665 isl_id
*test_id
, *arg_id
;
2672 if (expr
->type
!= pet_expr_access
)
2674 test_id
= isl_map_get_tuple_id(test
, isl_dim_out
);
2675 arg_id
= pet_expr_access_get_id(expr
);
2676 isl_id_free(arg_id
);
2677 isl_id_free(test_id
);
2678 if (test_id
!= arg_id
)
2680 val
= isl_map_plain_get_val_if_fixed(domain
, isl_dim_out
, pos
);
2681 is_int
= isl_val_is_int(val
);
2683 s
= isl_val_get_num_si(val
);
2692 implied
= isl_map_copy(expr
->acc
.access
);
2693 implied
= apply_implications(scop
, implied
, test_id
, satisfied
);
2694 is_subset
= isl_map_is_subset(test
, implied
);
2695 isl_map_free(implied
);
2700 /* Is the filter expressed by "test" and "satisfied" implied
2701 * by any of the filters on the domain of "stmt", taking into
2702 * account the implications of "scop"?
2704 static int filter_implied(struct pet_scop
*scop
,
2705 struct pet_stmt
*stmt
, __isl_keep isl_multi_pw_aff
*test
, int satisfied
)
2713 if (!scop
|| !stmt
|| !test
)
2715 if (scop
->n_implication
== 0)
2717 if (stmt
->n_arg
== 0)
2720 domain
= isl_set_unwrap(isl_set_copy(stmt
->domain
));
2721 test_map
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test
));
2724 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2725 implied
= implies_filter(scop
, domain
, i
, stmt
->args
[i
],
2726 test_map
, satisfied
);
2727 if (implied
< 0 || implied
)
2731 isl_map_free(test_map
);
2732 isl_map_free(domain
);
2736 /* Make the statement "stmt" depend on the value of "test"
2737 * being equal to "satisfied" by adjusting stmt->domain.
2739 * The domain of "test" corresponds to the (zero or more) outer dimensions
2740 * of the iteration domain.
2742 * We first extend "test" to apply to the entire iteration domain and
2743 * then check if the filter that we are about to add is implied
2744 * by any of the current filters, possibly taking into account
2745 * the implications in "scop". If so, we leave "stmt" untouched and return.
2747 * Otherwise, we insert an argument corresponding to a read to "test"
2748 * from the iteration domain of "stmt" in front of the list of arguments.
2749 * We also insert a corresponding output dimension in the wrapped
2750 * map contained in stmt->domain, with value set to "satisfied".
2752 static struct pet_stmt
*stmt_filter(struct pet_scop
*scop
,
2753 struct pet_stmt
*stmt
, __isl_take isl_multi_pw_aff
*test
, int satisfied
)
2759 isl_pw_multi_aff
*pma
;
2760 isl_multi_aff
*add_dom
;
2762 isl_local_space
*ls
;
2768 space
= pet_stmt_get_space(stmt
);
2769 n_test_dom
= isl_multi_pw_aff_dim(test
, isl_dim_in
);
2770 space
= isl_space_from_domain(space
);
2771 space
= isl_space_add_dims(space
, isl_dim_out
, n_test_dom
);
2772 add_dom
= isl_multi_aff_zero(isl_space_copy(space
));
2773 ls
= isl_local_space_from_space(isl_space_domain(space
));
2774 for (i
= 0; i
< n_test_dom
; ++i
) {
2776 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
2778 add_dom
= isl_multi_aff_set_aff(add_dom
, i
, aff
);
2780 isl_local_space_free(ls
);
2781 test
= isl_multi_pw_aff_pullback_multi_aff(test
, add_dom
);
2783 implied
= filter_implied(scop
, stmt
, test
, satisfied
);
2787 isl_multi_pw_aff_free(test
);
2791 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
2792 pma
= pet_filter_insert_pma(isl_set_get_space(stmt
->domain
),
2794 stmt
->domain
= isl_set_preimage_pw_multi_aff(stmt
->domain
, pma
);
2796 if (args_insert_access(&stmt
->n_arg
, &stmt
->args
, test
) < 0)
2799 isl_multi_pw_aff_free(test
);
2802 isl_multi_pw_aff_free(test
);
2803 return pet_stmt_free(stmt
);
2806 /* Does "scop" have a skip condition of the given "type"?
2808 int pet_scop_has_skip(struct pet_scop
*scop
, enum pet_skip type
)
2810 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2814 return ext
->skip
[type
] != NULL
;
2817 /* Does "scop" have a skip condition of the given "type" that
2818 * is an affine expression?
2820 int pet_scop_has_affine_skip(struct pet_scop
*scop
, enum pet_skip type
)
2822 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2826 if (!ext
->skip
[type
])
2828 return multi_pw_aff_is_affine(ext
->skip
[type
]);
2831 /* Does "scop" have a skip condition of the given "type" that
2832 * is not an affine expression?
2834 int pet_scop_has_var_skip(struct pet_scop
*scop
, enum pet_skip type
)
2836 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2841 if (!ext
->skip
[type
])
2843 aff
= multi_pw_aff_is_affine(ext
->skip
[type
]);
2849 /* Does "scop" have a skip condition of the given "type" that
2850 * is affine and holds on the entire domain?
2852 int pet_scop_has_universal_skip(struct pet_scop
*scop
, enum pet_skip type
)
2854 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2860 is_aff
= pet_scop_has_affine_skip(scop
, type
);
2861 if (is_aff
< 0 || !is_aff
)
2864 pa
= isl_multi_pw_aff_get_pw_aff(ext
->skip
[type
], 0);
2865 set
= isl_pw_aff_non_zero_set(pa
);
2866 is_univ
= isl_set_plain_is_universe(set
);
2872 /* Replace scop->skip[type] by "skip".
2874 struct pet_scop
*pet_scop_set_skip(struct pet_scop
*scop
,
2875 enum pet_skip type
, __isl_take isl_multi_pw_aff
*skip
)
2877 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2882 isl_multi_pw_aff_free(ext
->skip
[type
]);
2883 ext
->skip
[type
] = skip
;
2887 isl_multi_pw_aff_free(skip
);
2888 return pet_scop_free(scop
);
2891 /* Return a copy of scop->skip[type].
2893 __isl_give isl_multi_pw_aff
*pet_scop_get_skip(struct pet_scop
*scop
,
2896 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2901 return isl_multi_pw_aff_copy(ext
->skip
[type
]);
2904 /* Assuming scop->skip[type] is an affine expression,
2905 * return the constraints on the parameters for which the skip condition
2908 __isl_give isl_set
*pet_scop_get_affine_skip_domain(struct pet_scop
*scop
,
2911 isl_multi_pw_aff
*skip
;
2914 skip
= pet_scop_get_skip(scop
, type
);
2915 pa
= isl_multi_pw_aff_get_pw_aff(skip
, 0);
2916 isl_multi_pw_aff_free(skip
);
2917 return isl_set_params(isl_pw_aff_non_zero_set(pa
));
2920 /* Return the identifier of the variable that is accessed by
2921 * the skip condition of the given type.
2923 * The skip condition is assumed not to be an affine condition.
2925 __isl_give isl_id
*pet_scop_get_skip_id(struct pet_scop
*scop
,
2928 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2933 return isl_multi_pw_aff_get_tuple_id(ext
->skip
[type
], isl_dim_out
);
2936 /* Return an access pet_expr corresponding to the skip condition
2937 * of the given type.
2939 struct pet_expr
*pet_scop_get_skip_expr(struct pet_scop
*scop
,
2942 return pet_expr_from_index(pet_scop_get_skip(scop
, type
));
2945 /* Drop the the skip condition scop->skip[type].
2947 void pet_scop_reset_skip(struct pet_scop
*scop
, enum pet_skip type
)
2949 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2954 isl_multi_pw_aff_free(ext
->skip
[type
]);
2955 ext
->skip
[type
] = NULL
;
2958 /* Make the skip condition (if any) depend on the value of "test" being
2959 * equal to "satisfied".
2961 * We only support the case where the original skip condition is universal,
2962 * i.e., where skipping is unconditional, and where satisfied == 1.
2963 * In this case, the skip condition is changed to skip only when
2964 * "test" is equal to one.
2966 static struct pet_scop
*pet_scop_filter_skip(struct pet_scop
*scop
,
2967 enum pet_skip type
, __isl_keep isl_multi_pw_aff
*test
, int satisfied
)
2973 if (!pet_scop_has_skip(scop
, type
))
2977 is_univ
= pet_scop_has_universal_skip(scop
, type
);
2979 return pet_scop_free(scop
);
2980 if (satisfied
&& is_univ
) {
2981 isl_multi_pw_aff
*skip
;
2982 skip
= isl_multi_pw_aff_copy(test
);
2983 scop
= pet_scop_set_skip(scop
, type
, skip
);
2987 isl_die(isl_multi_pw_aff_get_ctx(test
), isl_error_internal
,
2988 "skip expression cannot be filtered",
2989 return pet_scop_free(scop
));
2995 /* Make all statements in "scop" depend on the value of "test"
2996 * being equal to "satisfied" by adjusting their domains.
2998 struct pet_scop
*pet_scop_filter(struct pet_scop
*scop
,
2999 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
3003 scop
= pet_scop_filter_skip(scop
, pet_skip_now
, test
, satisfied
);
3004 scop
= pet_scop_filter_skip(scop
, pet_skip_later
, test
, satisfied
);
3009 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3010 scop
->stmts
[i
] = stmt_filter(scop
, scop
->stmts
[i
],
3011 isl_multi_pw_aff_copy(test
), satisfied
);
3012 if (!scop
->stmts
[i
])
3016 isl_multi_pw_aff_free(test
);
3019 isl_multi_pw_aff_free(test
);
3020 return pet_scop_free(scop
);
3023 /* Add all parameters in "expr" to "space" and return the result.
3025 static __isl_give isl_space
*expr_collect_params(struct pet_expr
*expr
,
3026 __isl_take isl_space
*space
)
3032 for (i
= 0; i
< expr
->n_arg
; ++i
)
3033 space
= expr_collect_params(expr
->args
[i
], space
);
3035 if (expr
->type
== pet_expr_access
)
3036 space
= isl_space_align_params(space
,
3037 isl_map_get_space(expr
->acc
.access
));
3041 pet_expr_free(expr
);
3042 return isl_space_free(space
);
3045 /* Add all parameters in "stmt" to "space" and return the result.
3047 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
3048 __isl_take isl_space
*space
)
3053 return isl_space_free(space
);
3055 space
= isl_space_align_params(space
, isl_set_get_space(stmt
->domain
));
3056 space
= isl_space_align_params(space
,
3057 isl_map_get_space(stmt
->schedule
));
3058 for (i
= 0; i
< stmt
->n_arg
; ++i
)
3059 space
= expr_collect_params(stmt
->args
[i
], space
);
3060 space
= expr_collect_params(stmt
->body
, space
);
3065 /* Add all parameters in "array" to "space" and return the result.
3067 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
3068 __isl_take isl_space
*space
)
3071 return isl_space_free(space
);
3073 space
= isl_space_align_params(space
,
3074 isl_set_get_space(array
->context
));
3075 space
= isl_space_align_params(space
, isl_set_get_space(array
->extent
));
3080 /* Add all parameters in "scop" to "space" and return the result.
3082 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
3083 __isl_take isl_space
*space
)
3088 return isl_space_free(space
);
3090 for (i
= 0; i
< scop
->n_array
; ++i
)
3091 space
= array_collect_params(scop
->arrays
[i
], space
);
3093 for (i
= 0; i
< scop
->n_stmt
; ++i
)
3094 space
= stmt_collect_params(scop
->stmts
[i
], space
);
3099 /* Add all parameters in "space" to all access relations and index expressions
3102 static struct pet_expr
*expr_propagate_params(struct pet_expr
*expr
,
3103 __isl_take isl_space
*space
)
3110 for (i
= 0; i
< expr
->n_arg
; ++i
) {
3112 expr_propagate_params(expr
->args
[i
],
3113 isl_space_copy(space
));
3118 if (expr
->type
== pet_expr_access
) {
3119 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
3120 isl_space_copy(space
));
3121 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
3122 isl_space_copy(space
));
3123 if (!expr
->acc
.access
|| !expr
->acc
.index
)
3127 isl_space_free(space
);
3130 isl_space_free(space
);
3131 return pet_expr_free(expr
);
3134 /* Add all parameters in "space" to the domain, schedule and
3135 * all access relations in "stmt".
3137 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
3138 __isl_take isl_space
*space
)
3145 stmt
->domain
= isl_set_align_params(stmt
->domain
,
3146 isl_space_copy(space
));
3147 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
3148 isl_space_copy(space
));
3150 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
3151 stmt
->args
[i
] = expr_propagate_params(stmt
->args
[i
],
3152 isl_space_copy(space
));
3156 stmt
->body
= expr_propagate_params(stmt
->body
, isl_space_copy(space
));
3158 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
3161 isl_space_free(space
);
3164 isl_space_free(space
);
3165 return pet_stmt_free(stmt
);
3168 /* Add all parameters in "space" to "array".
3170 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
3171 __isl_take isl_space
*space
)
3176 array
->context
= isl_set_align_params(array
->context
,
3177 isl_space_copy(space
));
3178 array
->extent
= isl_set_align_params(array
->extent
,
3179 isl_space_copy(space
));
3180 if (array
->value_bounds
) {
3181 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
3182 isl_space_copy(space
));
3183 if (!array
->value_bounds
)
3187 if (!array
->context
|| !array
->extent
)
3190 isl_space_free(space
);
3193 isl_space_free(space
);
3194 return pet_array_free(array
);
3197 /* Add all parameters in "space" to "scop".
3199 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
3200 __isl_take isl_space
*space
)
3207 for (i
= 0; i
< scop
->n_array
; ++i
) {
3208 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
3209 isl_space_copy(space
));
3210 if (!scop
->arrays
[i
])
3214 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3215 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
3216 isl_space_copy(space
));
3217 if (!scop
->stmts
[i
])
3221 isl_space_free(space
);
3224 isl_space_free(space
);
3225 return pet_scop_free(scop
);
3228 /* Update all isl_sets and isl_maps in "scop" such that they all
3229 * have the same parameters.
3231 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
3238 space
= isl_set_get_space(scop
->context
);
3239 space
= scop_collect_params(scop
, space
);
3241 scop
->context
= isl_set_align_params(scop
->context
,
3242 isl_space_copy(space
));
3243 scop
= scop_propagate_params(scop
, space
);
3245 if (scop
&& !scop
->context
)
3246 return pet_scop_free(scop
);
3251 /* Check if the given index expression accesses a (0D) array that corresponds
3252 * to one of the parameters in "dim". If so, replace the array access
3253 * by an access to the set of integers with as index (and value)
3256 static __isl_give isl_multi_pw_aff
*index_detect_parameter(
3257 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_space
*space
)
3259 isl_local_space
*ls
;
3260 isl_id
*array_id
= NULL
;
3264 if (isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
)) {
3265 array_id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
3266 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, array_id
);
3268 isl_space_free(space
);
3271 isl_id_free(array_id
);
3275 space
= isl_multi_pw_aff_get_domain_space(index
);
3276 isl_multi_pw_aff_free(index
);
3278 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, array_id
);
3280 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
3281 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, array_id
);
3284 isl_id_free(array_id
);
3286 ls
= isl_local_space_from_space(space
);
3287 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
3288 index
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
3293 /* Check if the given access relation accesses a (0D) array that corresponds
3294 * to one of the parameters in "dim". If so, replace the array access
3295 * by an access to the set of integers with as index (and value)
3298 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
3299 __isl_take isl_space
*dim
)
3301 isl_id
*array_id
= NULL
;
3304 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
3305 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
3306 pos
= isl_space_find_dim_by_id(dim
, isl_dim_param
, array_id
);
3308 isl_space_free(dim
);
3311 isl_id_free(array_id
);
3315 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
3317 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
3318 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
3321 isl_id_free(array_id
);
3323 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
3324 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
3329 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3330 * in "dim" by a value equal to the corresponding parameter.
3332 static struct pet_expr
*expr_detect_parameter_accesses(struct pet_expr
*expr
,
3333 __isl_take isl_space
*dim
)
3340 for (i
= 0; i
< expr
->n_arg
; ++i
) {
3342 expr_detect_parameter_accesses(expr
->args
[i
],
3343 isl_space_copy(dim
));
3348 if (expr
->type
== pet_expr_access
) {
3349 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
3350 isl_space_copy(dim
));
3351 expr
->acc
.index
= index_detect_parameter(expr
->acc
.index
,
3352 isl_space_copy(dim
));
3353 if (!expr
->acc
.access
|| !expr
->acc
.index
)
3357 isl_space_free(dim
);
3360 isl_space_free(dim
);
3361 return pet_expr_free(expr
);
3364 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3365 * in "dim" by a value equal to the corresponding parameter.
3367 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
3368 __isl_take isl_space
*dim
)
3373 stmt
->body
= expr_detect_parameter_accesses(stmt
->body
,
3374 isl_space_copy(dim
));
3376 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
3379 isl_space_free(dim
);
3382 isl_space_free(dim
);
3383 return pet_stmt_free(stmt
);
3386 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3387 * in "dim" by a value equal to the corresponding parameter.
3389 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
3390 __isl_take isl_space
*dim
)
3397 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3398 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
3399 isl_space_copy(dim
));
3400 if (!scop
->stmts
[i
])
3404 isl_space_free(dim
);
3407 isl_space_free(dim
);
3408 return pet_scop_free(scop
);
3411 /* Replace all accesses to (0D) arrays that correspond to any of
3412 * the parameters used in "scop" by a value equal
3413 * to the corresponding parameter.
3415 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
3422 dim
= isl_set_get_space(scop
->context
);
3423 dim
= scop_collect_params(scop
, dim
);
3425 scop
= scop_detect_parameter_accesses(scop
, dim
);
3430 /* Return the relation mapping domain iterations to all possibly
3431 * accessed data elements.
3432 * In particular, take the access relation and project out the values
3433 * of the arguments, if any.
3435 __isl_give isl_map
*pet_expr_access_get_may_access(struct pet_expr
*expr
)
3443 if (expr
->type
!= pet_expr_access
)
3446 access
= isl_map_copy(expr
->acc
.access
);
3447 if (expr
->n_arg
== 0)
3450 space
= isl_space_domain(isl_map_get_space(access
));
3451 map
= isl_map_universe(isl_space_unwrap(space
));
3452 map
= isl_map_domain_map(map
);
3453 access
= isl_map_apply_domain(access
, map
);
3458 /* Return the relation mapping domain iterations to all possibly
3459 * accessed data elements, with its domain tagged with the reference
3462 __isl_give isl_map
*pet_expr_access_get_tagged_may_access(
3463 struct pet_expr
*expr
)
3470 access
= pet_expr_access_get_may_access(expr
);
3471 access
= tag_access(access
, isl_id_copy(expr
->acc
.ref_id
));
3476 /* Add the access relation of the access expression "expr" to "accesses" and
3477 * return the result.
3478 * The domain of the access relation is intersected with "domain".
3479 * If "tag" is set, then the access relation is tagged with
3480 * the corresponding reference identifier.
3482 static __isl_give isl_union_map
*expr_collect_access(struct pet_expr
*expr
,
3483 int tag
, __isl_take isl_union_map
*accesses
, __isl_keep isl_set
*domain
)
3487 access
= pet_expr_access_get_may_access(expr
);
3488 access
= isl_map_intersect_domain(access
, isl_set_copy(domain
));
3490 access
= tag_access(access
, isl_id_copy(expr
->acc
.ref_id
));
3491 return isl_union_map_add_map(accesses
, access
);
3494 /* Add all read access relations (if "read" is set) and/or all write
3495 * access relations (if "write" is set) to "accesses" and return the result.
3496 * The domains of the access relations are intersected with "domain".
3497 * If "tag" is set, then the access relations are tagged with
3498 * the corresponding reference identifiers.
3500 * If "must" is set, then we only add the accesses that are definitely
3501 * performed. Otherwise, we add all potential accesses.
3502 * In particular, if the access has any arguments, then if "must" is
3503 * set we currently skip the access completely. If "must" is not set,
3504 * we project out the values of the access arguments.
3506 static __isl_give isl_union_map
*expr_collect_accesses(struct pet_expr
*expr
,
3507 int read
, int write
, int must
, int tag
,
3508 __isl_take isl_union_map
*accesses
, __isl_keep isl_set
*domain
)
3515 return isl_union_map_free(accesses
);
3517 for (i
= 0; i
< expr
->n_arg
; ++i
)
3518 accesses
= expr_collect_accesses(expr
->args
[i
],
3519 read
, write
, must
, tag
, accesses
, domain
);
3521 if (expr
->type
== pet_expr_access
&& !pet_expr_is_affine(expr
) &&
3522 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)) &&
3523 (!must
|| expr
->n_arg
== 0)) {
3524 accesses
= expr_collect_access(expr
, tag
, accesses
, domain
);
3530 /* Collect and return all read access relations (if "read" is set)
3531 * and/or all write access relations (if "write" is set) in "stmt".
3532 * If "tag" is set, then the access relations are tagged with
3533 * the corresponding reference identifiers.
3534 * If "kill" is set, then "stmt" is a kill statement and we simply
3535 * add the argument of the kill operation.
3537 * If "must" is set, then we only add the accesses that are definitely
3538 * performed. Otherwise, we add all potential accesses.
3539 * In particular, if the statement has any arguments, then if "must" is
3540 * set we currently skip the statement completely. If "must" is not set,
3541 * we project out the values of the statement arguments.
3543 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
3544 int read
, int write
, int kill
, int must
, int tag
,
3545 __isl_take isl_space
*dim
)
3547 isl_union_map
*accesses
;
3553 accesses
= isl_union_map_empty(dim
);
3555 if (must
&& stmt
->n_arg
> 0)
3558 domain
= isl_set_copy(stmt
->domain
);
3559 if (isl_set_is_wrapping(domain
))
3560 domain
= isl_map_domain(isl_set_unwrap(domain
));
3563 accesses
= expr_collect_access(stmt
->body
->args
[0], tag
,
3566 accesses
= expr_collect_accesses(stmt
->body
, read
, write
,
3567 must
, tag
, accesses
, domain
);
3568 isl_set_free(domain
);
3573 /* Is "stmt" an assignment statement?
3575 int pet_stmt_is_assign(struct pet_stmt
*stmt
)
3579 if (stmt
->body
->type
!= pet_expr_op
)
3581 return stmt
->body
->op
== pet_op_assign
;
3584 /* Is "stmt" a kill statement?
3586 int pet_stmt_is_kill(struct pet_stmt
*stmt
)
3590 if (stmt
->body
->type
!= pet_expr_op
)
3592 return stmt
->body
->op
== pet_op_kill
;
3595 /* Is "stmt" an assume statement?
3597 int pet_stmt_is_assume(struct pet_stmt
*stmt
)
3599 if (stmt
->body
->type
!= pet_expr_op
)
3601 return stmt
->body
->op
== pet_op_assume
;
3604 /* Compute a mapping from all arrays (of structs) in scop
3605 * to their innermost arrays.
3607 * In particular, for each array of a primitive type, the result
3608 * contains the identity mapping on that array.
3609 * For each array involving member accesses, the result
3610 * contains a mapping from the elements of any intermediate array of structs
3611 * to all corresponding elements of the innermost nested arrays.
3613 static __isl_give isl_union_map
*compute_to_inner(struct pet_scop
*scop
)
3616 isl_union_map
*to_inner
;
3618 to_inner
= isl_union_map_empty(isl_set_get_space(scop
->context
));
3620 for (i
= 0; i
< scop
->n_array
; ++i
) {
3621 struct pet_array
*array
= scop
->arrays
[i
];
3623 isl_map
*map
, *gist
;
3625 if (array
->element_is_record
)
3628 map
= isl_set_identity(isl_set_copy(array
->extent
));
3630 set
= isl_map_domain(isl_map_copy(map
));
3631 gist
= isl_map_copy(map
);
3632 gist
= isl_map_gist_domain(gist
, isl_set_copy(set
));
3633 to_inner
= isl_union_map_add_map(to_inner
, gist
);
3635 while (set
&& isl_set_is_wrapping(set
)) {
3639 id
= isl_set_get_tuple_id(set
);
3640 wrapped
= isl_set_unwrap(set
);
3641 wrapped
= isl_map_domain_map(wrapped
);
3642 wrapped
= isl_map_set_tuple_id(wrapped
, isl_dim_in
, id
);
3643 map
= isl_map_apply_domain(map
, wrapped
);
3644 set
= isl_map_domain(isl_map_copy(map
));
3645 gist
= isl_map_copy(map
);
3646 gist
= isl_map_gist_domain(gist
, isl_set_copy(set
));
3647 to_inner
= isl_union_map_add_map(to_inner
, gist
);
3657 /* Collect and return all read access relations (if "read" is set)
3658 * and/or all write access relations (if "write" is set) in "scop".
3659 * If "kill" is set, then we only add the arguments of kill operations.
3660 * If "must" is set, then we only add the accesses that are definitely
3661 * performed. Otherwise, we add all potential accesses.
3662 * If "tag" is set, then the access relations are tagged with
3663 * the corresponding reference identifiers.
3664 * For accesses to structures, the returned access relation accesses
3665 * all individual fields in the structures.
3667 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
3668 int read
, int write
, int kill
, int must
, int tag
)
3671 isl_union_map
*accesses
;
3672 isl_union_set
*arrays
;
3673 isl_union_map
*to_inner
;
3678 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
3680 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3681 struct pet_stmt
*stmt
= scop
->stmts
[i
];
3682 isl_union_map
*accesses_i
;
3685 if (kill
&& !pet_stmt_is_kill(stmt
))
3688 space
= isl_set_get_space(scop
->context
);
3689 accesses_i
= stmt_collect_accesses(stmt
, read
, write
, kill
,
3691 accesses
= isl_union_map_union(accesses
, accesses_i
);
3694 arrays
= isl_union_set_empty(isl_union_map_get_space(accesses
));
3695 for (i
= 0; i
< scop
->n_array
; ++i
) {
3696 isl_set
*extent
= isl_set_copy(scop
->arrays
[i
]->extent
);
3697 arrays
= isl_union_set_add_set(arrays
, extent
);
3699 accesses
= isl_union_map_intersect_range(accesses
, arrays
);
3701 to_inner
= compute_to_inner(scop
);
3702 accesses
= isl_union_map_apply_range(accesses
, to_inner
);
3707 /* Collect all potential read access relations.
3709 __isl_give isl_union_map
*pet_scop_collect_may_reads(struct pet_scop
*scop
)
3711 return scop_collect_accesses(scop
, 1, 0, 0, 0, 0);
3714 /* Collect all potential write access relations.
3716 __isl_give isl_union_map
*pet_scop_collect_may_writes(struct pet_scop
*scop
)
3718 return scop_collect_accesses(scop
, 0, 1, 0, 0, 0);
3721 /* Collect all definite write access relations.
3723 __isl_give isl_union_map
*pet_scop_collect_must_writes(struct pet_scop
*scop
)
3725 return scop_collect_accesses(scop
, 0, 1, 0, 1, 0);
3728 /* Collect all definite kill access relations.
3730 __isl_give isl_union_map
*pet_scop_collect_must_kills(struct pet_scop
*scop
)
3732 return scop_collect_accesses(scop
, 0, 0, 1, 1, 0);
3735 /* Collect all tagged potential read access relations.
3737 __isl_give isl_union_map
*pet_scop_collect_tagged_may_reads(
3738 struct pet_scop
*scop
)
3740 return scop_collect_accesses(scop
, 1, 0, 0, 0, 1);
3743 /* Collect all tagged potential write access relations.
3745 __isl_give isl_union_map
*pet_scop_collect_tagged_may_writes(
3746 struct pet_scop
*scop
)
3748 return scop_collect_accesses(scop
, 0, 1, 0, 0, 1);
3751 /* Collect all tagged definite write access relations.
3753 __isl_give isl_union_map
*pet_scop_collect_tagged_must_writes(
3754 struct pet_scop
*scop
)
3756 return scop_collect_accesses(scop
, 0, 1, 0, 1, 1);
3759 /* Collect all tagged definite kill access relations.
3761 __isl_give isl_union_map
*pet_scop_collect_tagged_must_kills(
3762 struct pet_scop
*scop
)
3764 return scop_collect_accesses(scop
, 0, 0, 1, 1, 1);
3767 /* Collect and return the union of iteration domains in "scop".
3769 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
3773 isl_union_set
*domain
;
3778 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
3780 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3781 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
3782 domain
= isl_union_set_add_set(domain
, domain_i
);
3788 /* Collect and return the schedules of the statements in "scop".
3789 * The range is normalized to the maximal number of scheduling
3792 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
3795 isl_map
*schedule_i
;
3796 isl_union_map
*schedule
;
3797 int depth
, max_depth
= 0;
3802 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
3804 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3805 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
3806 if (depth
> max_depth
)
3810 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3811 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
3812 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
3813 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
3815 for (j
= depth
; j
< max_depth
; ++j
)
3816 schedule_i
= isl_map_fix_si(schedule_i
,
3818 schedule
= isl_union_map_add_map(schedule
, schedule_i
);
3824 /* Does expression "expr" write to "id"?
3826 static int expr_writes(struct pet_expr
*expr
, __isl_keep isl_id
*id
)
3831 for (i
= 0; i
< expr
->n_arg
; ++i
) {
3832 int writes
= expr_writes(expr
->args
[i
], id
);
3833 if (writes
< 0 || writes
)
3837 if (expr
->type
!= pet_expr_access
)
3839 if (!expr
->acc
.write
)
3841 if (pet_expr_is_affine(expr
))
3844 write_id
= pet_expr_access_get_id(expr
);
3845 isl_id_free(write_id
);
3850 return write_id
== id
;
3853 /* Does statement "stmt" write to "id"?
3855 static int stmt_writes(struct pet_stmt
*stmt
, __isl_keep isl_id
*id
)
3857 return expr_writes(stmt
->body
, id
);
3860 /* Is there any write access in "scop" that accesses "id"?
3862 int pet_scop_writes(struct pet_scop
*scop
, __isl_keep isl_id
*id
)
3869 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3870 int writes
= stmt_writes(scop
->stmts
[i
], id
);
3871 if (writes
< 0 || writes
)
3878 /* Add a reference identifier to access expression "expr".
3879 * "user" points to an integer that contains the sequence number
3880 * of the next reference.
3882 static struct pet_expr
*access_add_ref_id(struct pet_expr
*expr
, void *user
)
3891 ctx
= isl_map_get_ctx(expr
->acc
.access
);
3892 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
3893 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
3894 if (!expr
->acc
.ref_id
)
3895 return pet_expr_free(expr
);
3900 /* Add a reference identifier to all access expressions in "stmt".
3901 * "n_ref" points to an integer that contains the sequence number
3902 * of the next reference.
3904 static struct pet_stmt
*stmt_add_ref_ids(struct pet_stmt
*stmt
, int *n_ref
)
3911 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
3912 stmt
->args
[i
] = pet_expr_map_access(stmt
->args
[i
],
3913 &access_add_ref_id
, n_ref
);
3915 return pet_stmt_free(stmt
);
3918 stmt
->body
= pet_expr_map_access(stmt
->body
, &access_add_ref_id
, n_ref
);
3920 return pet_stmt_free(stmt
);
3925 /* Add a reference identifier to all access expressions in "scop".
3927 struct pet_scop
*pet_scop_add_ref_ids(struct pet_scop
*scop
)
3936 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3937 scop
->stmts
[i
] = stmt_add_ref_ids(scop
->stmts
[i
], &n_ref
);
3938 if (!scop
->stmts
[i
])
3939 return pet_scop_free(scop
);
3945 /* Reset the user pointer on all parameter ids in "array".
3947 static struct pet_array
*array_anonymize(struct pet_array
*array
)
3952 array
->context
= isl_set_reset_user(array
->context
);
3953 array
->extent
= isl_set_reset_user(array
->extent
);
3954 if (!array
->context
|| !array
->extent
)
3955 return pet_array_free(array
);
3960 /* Reset the user pointer on all parameter and tuple ids in
3961 * the access relation and the index expressions
3962 * of the access expression "expr".
3964 static struct pet_expr
*access_anonymize(struct pet_expr
*expr
, void *user
)
3966 expr
->acc
.access
= isl_map_reset_user(expr
->acc
.access
);
3967 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
3968 if (!expr
->acc
.access
|| !expr
->acc
.index
)
3969 return pet_expr_free(expr
);
3974 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3976 static struct pet_stmt
*stmt_anonymize(struct pet_stmt
*stmt
)
3985 stmt
->domain
= isl_set_reset_user(stmt
->domain
);
3986 stmt
->schedule
= isl_map_reset_user(stmt
->schedule
);
3987 if (!stmt
->domain
|| !stmt
->schedule
)
3988 return pet_stmt_free(stmt
);
3990 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
3991 stmt
->args
[i
] = pet_expr_map_access(stmt
->args
[i
],
3992 &access_anonymize
, NULL
);
3994 return pet_stmt_free(stmt
);
3997 stmt
->body
= pet_expr_map_access(stmt
->body
,
3998 &access_anonymize
, NULL
);
4000 return pet_stmt_free(stmt
);
4005 /* Reset the user pointer on the tuple ids and all parameter ids
4008 static struct pet_implication
*implication_anonymize(
4009 struct pet_implication
*implication
)
4014 implication
->extension
= isl_map_reset_user(implication
->extension
);
4015 if (!implication
->extension
)
4016 return pet_implication_free(implication
);
4021 /* Reset the user pointer on all parameter and tuple ids in "scop".
4023 struct pet_scop
*pet_scop_anonymize(struct pet_scop
*scop
)
4030 scop
->context
= isl_set_reset_user(scop
->context
);
4031 scop
->context_value
= isl_set_reset_user(scop
->context_value
);
4032 if (!scop
->context
|| !scop
->context_value
)
4033 return pet_scop_free(scop
);
4035 for (i
= 0; i
< scop
->n_array
; ++i
) {
4036 scop
->arrays
[i
] = array_anonymize(scop
->arrays
[i
]);
4037 if (!scop
->arrays
[i
])
4038 return pet_scop_free(scop
);
4041 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
4042 scop
->stmts
[i
] = stmt_anonymize(scop
->stmts
[i
]);
4043 if (!scop
->stmts
[i
])
4044 return pet_scop_free(scop
);
4047 for (i
= 0; i
< scop
->n_implication
; ++i
) {
4048 scop
->implications
[i
] =
4049 implication_anonymize(scop
->implications
[i
]);
4050 if (!scop
->implications
[i
])
4051 return pet_scop_free(scop
);
4057 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
4058 * then intersect the range of "map" with the valid set of values.
4060 static __isl_give isl_map
*access_apply_value_bounds(__isl_take isl_map
*map
,
4061 struct pet_expr
*arg
, __isl_keep isl_union_map
*value_bounds
)
4066 isl_ctx
*ctx
= isl_map_get_ctx(map
);
4068 id
= pet_expr_access_get_id(arg
);
4069 space
= isl_space_alloc(ctx
, 0, 0, 1);
4070 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
4071 vb
= isl_union_map_extract_map(value_bounds
, space
);
4072 if (!isl_map_plain_is_empty(vb
))
4073 map
= isl_map_intersect_range(map
, isl_map_range(vb
));
4080 /* Given a set "domain", return a wrapped relation with the given set
4081 * as domain and a range of dimension "n_arg", where each coordinate
4082 * is either unbounded or, if the corresponding element of args is of
4083 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
4085 static __isl_give isl_set
*apply_value_bounds(__isl_take isl_set
*domain
,
4086 unsigned n_arg
, struct pet_expr
**args
,
4087 __isl_keep isl_union_map
*value_bounds
)
4093 map
= isl_map_from_domain(domain
);
4094 space
= isl_map_get_space(map
);
4095 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
4097 for (i
= 0; i
< n_arg
; ++i
) {
4099 struct pet_expr
*arg
= args
[i
];
4101 map_i
= isl_map_universe(isl_space_copy(space
));
4102 if (arg
->type
== pet_expr_access
)
4103 map_i
= access_apply_value_bounds(map_i
, arg
,
4105 map
= isl_map_flat_range_product(map
, map_i
);
4107 isl_space_free(space
);
4109 return isl_map_wrap(map
);
4112 /* Data used in access_gist() callback.
4114 struct pet_access_gist_data
{
4116 isl_union_map
*value_bounds
;
4119 /* Given an expression "expr" of type pet_expr_access, compute
4120 * the gist of the associated access relation and index expression
4121 * with respect to data->domain and the bounds on the values of the arguments
4122 * of the expression.
4124 static struct pet_expr
*access_gist(struct pet_expr
*expr
, void *user
)
4126 struct pet_access_gist_data
*data
= user
;
4129 domain
= isl_set_copy(data
->domain
);
4130 if (expr
->n_arg
> 0)
4131 domain
= apply_value_bounds(domain
, expr
->n_arg
, expr
->args
,
4132 data
->value_bounds
);
4134 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
,
4135 isl_set_copy(domain
));
4136 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
4137 if (!expr
->acc
.access
|| !expr
->acc
.index
)
4138 return pet_expr_free(expr
);
4143 /* Compute the gist of the iteration domain and all access relations
4144 * of "stmt" based on the constraints on the parameters specified by "context"
4145 * and the constraints on the values of nested accesses specified
4146 * by "value_bounds".
4148 static struct pet_stmt
*stmt_gist(struct pet_stmt
*stmt
,
4149 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
4153 struct pet_access_gist_data data
;
4158 data
.domain
= isl_set_copy(stmt
->domain
);
4159 data
.value_bounds
= value_bounds
;
4160 if (stmt
->n_arg
> 0)
4161 data
.domain
= isl_map_domain(isl_set_unwrap(data
.domain
));
4163 data
.domain
= isl_set_intersect_params(data
.domain
,
4164 isl_set_copy(context
));
4166 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
4167 stmt
->args
[i
] = pet_expr_map_access(stmt
->args
[i
],
4168 &access_gist
, &data
);
4173 stmt
->body
= pet_expr_map_access(stmt
->body
, &access_gist
, &data
);
4177 isl_set_free(data
.domain
);
4179 domain
= isl_set_universe(pet_stmt_get_space(stmt
));
4180 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
4181 if (stmt
->n_arg
> 0)
4182 domain
= apply_value_bounds(domain
, stmt
->n_arg
, stmt
->args
,
4184 stmt
->domain
= isl_set_gist(stmt
->domain
, domain
);
4186 return pet_stmt_free(stmt
);
4190 isl_set_free(data
.domain
);
4191 return pet_stmt_free(stmt
);
4194 /* Compute the gist of the extent of the array
4195 * based on the constraints on the parameters specified by "context".
4197 static struct pet_array
*array_gist(struct pet_array
*array
,
4198 __isl_keep isl_set
*context
)
4203 array
->extent
= isl_set_gist_params(array
->extent
,
4204 isl_set_copy(context
));
4206 return pet_array_free(array
);
4211 /* Compute the gist of all sets and relations in "scop"
4212 * based on the constraints on the parameters specified by "scop->context"
4213 * and the constraints on the values of nested accesses specified
4214 * by "value_bounds".
4216 struct pet_scop
*pet_scop_gist(struct pet_scop
*scop
,
4217 __isl_keep isl_union_map
*value_bounds
)
4224 scop
->context
= isl_set_coalesce(scop
->context
);
4226 return pet_scop_free(scop
);
4228 for (i
= 0; i
< scop
->n_array
; ++i
) {
4229 scop
->arrays
[i
] = array_gist(scop
->arrays
[i
], scop
->context
);
4230 if (!scop
->arrays
[i
])
4231 return pet_scop_free(scop
);
4234 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
4235 scop
->stmts
[i
] = stmt_gist(scop
->stmts
[i
], scop
->context
,
4237 if (!scop
->stmts
[i
])
4238 return pet_scop_free(scop
);
4244 /* Intersect the context of "scop" with "context".
4245 * To ensure that we don't introduce any unnamed parameters in
4246 * the context of "scop", we first remove the unnamed parameters
4249 struct pet_scop
*pet_scop_restrict_context(struct pet_scop
*scop
,
4250 __isl_take isl_set
*context
)
4255 context
= set_project_out_unnamed_params(context
);
4256 scop
->context
= isl_set_intersect(scop
->context
, context
);
4258 return pet_scop_free(scop
);
4262 isl_set_free(context
);
4263 return pet_scop_free(scop
);
4266 /* Drop the current context of "scop". That is, replace the context
4267 * by a universal set.
4269 struct pet_scop
*pet_scop_reset_context(struct pet_scop
*scop
)
4276 space
= isl_set_get_space(scop
->context
);
4277 isl_set_free(scop
->context
);
4278 scop
->context
= isl_set_universe(space
);
4280 return pet_scop_free(scop
);
4285 /* Append "array" to the arrays of "scop".
4287 struct pet_scop
*pet_scop_add_array(struct pet_scop
*scop
,
4288 struct pet_array
*array
)
4291 struct pet_array
**arrays
;
4293 if (!array
|| !scop
)
4296 ctx
= isl_set_get_ctx(scop
->context
);
4297 arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
4301 scop
->arrays
= arrays
;
4302 scop
->arrays
[scop
->n_array
] = array
;
4307 pet_array_free(array
);
4308 return pet_scop_free(scop
);
4311 /* Create and return an implication on filter values equal to "satisfied"
4312 * with extension "map".
4314 static struct pet_implication
*new_implication(__isl_take isl_map
*map
,
4318 struct pet_implication
*implication
;
4322 ctx
= isl_map_get_ctx(map
);
4323 implication
= isl_alloc_type(ctx
, struct pet_implication
);
4327 implication
->extension
= map
;
4328 implication
->satisfied
= satisfied
;
4336 /* Add an implication on filter values equal to "satisfied"
4337 * with extension "map" to "scop".
4339 struct pet_scop
*pet_scop_add_implication(struct pet_scop
*scop
,
4340 __isl_take isl_map
*map
, int satisfied
)
4343 struct pet_implication
*implication
;
4344 struct pet_implication
**implications
;
4346 implication
= new_implication(map
, satisfied
);
4347 if (!scop
|| !implication
)
4350 ctx
= isl_set_get_ctx(scop
->context
);
4351 implications
= isl_realloc_array(ctx
, scop
->implications
,
4352 struct pet_implication
*,
4353 scop
->n_implication
+ 1);
4356 scop
->implications
= implications
;
4357 scop
->implications
[scop
->n_implication
] = implication
;
4358 scop
->n_implication
++;
4362 pet_implication_free(implication
);
4363 return pet_scop_free(scop
);
4366 /* Given an access expression, check if it is data dependent.
4367 * If so, set *found and abort the search.
4369 static int is_data_dependent(struct pet_expr
*expr
, void *user
)
4381 /* Does "scop" contain any data dependent accesses?
4383 * Check the body of each statement for such accesses.
4385 int pet_scop_has_data_dependent_accesses(struct pet_scop
*scop
)
4393 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
4394 int r
= pet_expr_foreach_access_expr(scop
->stmts
[i
]->body
,
4395 &is_data_dependent
, &found
);
4396 if (r
< 0 && !found
)
4405 /* Does "scop" contain and data dependent conditions?
4407 int pet_scop_has_data_dependent_conditions(struct pet_scop
*scop
)
4414 for (i
= 0; i
< scop
->n_stmt
; ++i
)
4415 if (scop
->stmts
[i
]->n_arg
> 0)
4421 /* Keep track of the "input" file inside the (extended) "scop".
4423 struct pet_scop
*pet_scop_set_input_file(struct pet_scop
*scop
, FILE *input
)
4425 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
4435 /* Print the original code corresponding to "scop" to printer "p".
4437 * pet_scop_print_original can only be called from
4438 * a pet_transform_C_source callback. This means that the input
4439 * file is stored in the extended scop and that the printer prints
4442 __isl_give isl_printer
*pet_scop_print_original(struct pet_scop
*scop
,
4443 __isl_take isl_printer
*p
)
4445 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
4449 return isl_printer_free(p
);
4452 isl_die(isl_printer_get_ctx(p
), isl_error_invalid
,
4453 "no input file stored in scop",
4454 return isl_printer_free(p
));
4456 output
= isl_printer_get_file(p
);
4458 return isl_printer_free(p
);
4460 if (copy(ext
->input
, output
, scop
->start
, scop
->end
) < 0)
4461 return isl_printer_free(p
);