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
44 #include "value_bounds.h"
46 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
48 static char *type_str
[] = {
49 [pet_expr_access
] = "access",
50 [pet_expr_call
] = "call",
51 [pet_expr_cast
] = "cast",
52 [pet_expr_double
] = "double",
53 [pet_expr_int
] = "int",
57 static char *op_str
[] = {
58 [pet_op_add_assign
] = "+=",
59 [pet_op_sub_assign
] = "-=",
60 [pet_op_mul_assign
] = "*=",
61 [pet_op_div_assign
] = "/=",
62 [pet_op_assign
] = "=",
77 [pet_op_post_inc
] = "++",
78 [pet_op_post_dec
] = "--",
79 [pet_op_pre_inc
] = "++",
80 [pet_op_pre_dec
] = "--",
81 [pet_op_address_of
] = "&",
90 [pet_op_assume
] = "assume",
91 [pet_op_kill
] = "kill"
94 const char *pet_op_str(enum pet_op_type op
)
99 int pet_op_is_inc_dec(enum pet_op_type op
)
101 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
102 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
105 const char *pet_type_str(enum pet_expr_type type
)
107 return type_str
[type
];
110 enum pet_op_type
pet_str_op(const char *str
)
114 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
115 if (!strcmp(op_str
[i
], str
))
121 enum pet_expr_type
pet_str_type(const char *str
)
125 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
126 if (!strcmp(type_str
[i
], str
))
132 /* Construct a pet_expr of the given type.
134 __isl_give pet_expr
*pet_expr_alloc(isl_ctx
*ctx
, enum pet_expr_type type
)
138 expr
= isl_calloc_type(ctx
, struct pet_expr
);
150 /* Construct an access pet_expr from an access relation and an index expression.
151 * By default, it is considered to be a read access.
153 __isl_give pet_expr
*pet_expr_from_access_and_index( __isl_take isl_map
*access
,
154 __isl_take isl_multi_pw_aff
*index
)
156 isl_ctx
*ctx
= isl_map_get_ctx(access
);
159 if (!index
|| !access
)
161 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
165 expr
->acc
.access
= access
;
166 expr
->acc
.index
= index
;
172 isl_map_free(access
);
173 isl_multi_pw_aff_free(index
);
177 /* Construct an access pet_expr from an index expression.
178 * By default, the access is considered to be a read access.
180 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
184 access
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
185 return pet_expr_from_access_and_index(access
, index
);
188 /* Extend the range of "access" with "n" dimensions, retaining
189 * the tuple identifier on this range.
191 * If "access" represents a member access, then extend the range
194 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
198 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
200 if (!isl_map_range_is_wrapping(access
)) {
201 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
205 domain
= isl_map_copy(access
);
206 domain
= isl_map_range_factor_domain(domain
);
207 access
= isl_map_range_factor_range(access
);
208 access
= extend_range(access
, n
);
209 access
= isl_map_range_product(domain
, access
);
212 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
217 /* Finalize the construction of an access expression by setting
218 * the depth of the accessed array.
220 * The index expression may have been updated by
221 * pet_expr_access_subscript and/or pet_expr_access_member
222 * without the access relation having been updated accordingly.
223 * We perform this update here, taking into account the depth
224 * of the accessed array.
226 * If the number of indices is smaller than the depth of the array,
227 * then we assume that all elements of the remaining dimensions
230 __isl_give pet_expr
*pet_expr_access_set_depth(__isl_take pet_expr
*expr
,
236 expr
= pet_expr_cow(expr
);
240 access
= isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr
));
242 return pet_expr_free(expr
);
244 dim
= isl_map_dim(access
, isl_dim_out
);
246 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
247 "number of indices greater than depth",
248 access
= isl_map_free(access
));
251 access
= extend_range(access
, depth
- dim
);
253 return pet_expr_access_set_access(expr
, access
);
256 /* Construct a pet_expr that kills the elements specified by
257 * the index expression "index" and the access relation "access".
259 __isl_give pet_expr
*pet_expr_kill_from_access_and_index(
260 __isl_take isl_map
*access
, __isl_take isl_multi_pw_aff
*index
)
264 if (!access
|| !index
)
267 expr
= pet_expr_from_access_and_index(access
, index
);
268 expr
= pet_expr_access_set_read(expr
, 0);
269 return pet_expr_new_unary(pet_op_kill
, expr
);
271 isl_map_free(access
);
272 isl_multi_pw_aff_free(index
);
276 /* Construct a unary pet_expr that performs "op" on "arg".
278 __isl_give pet_expr
*pet_expr_new_unary(enum pet_op_type op
,
279 __isl_take pet_expr
*arg
)
286 ctx
= pet_expr_get_ctx(arg
);
287 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
288 expr
= pet_expr_set_n_arg(expr
, 1);
293 expr
->args
[pet_un_arg
] = arg
;
301 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
302 * where the result is represented using a type of "type_size" bits
303 * (may be zero if unknown or if the type is not an integer).
305 __isl_give pet_expr
*pet_expr_new_binary(int type_size
, enum pet_op_type op
,
306 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
313 ctx
= pet_expr_get_ctx(lhs
);
314 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
315 expr
= pet_expr_set_n_arg(expr
, 2);
320 expr
->type_size
= type_size
;
321 expr
->args
[pet_bin_lhs
] = lhs
;
322 expr
->args
[pet_bin_rhs
] = rhs
;
331 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
333 __isl_give pet_expr
*pet_expr_new_ternary(__isl_take pet_expr
*cond
,
334 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
339 if (!cond
|| !lhs
|| !rhs
)
341 ctx
= pet_expr_get_ctx(cond
);
342 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
343 expr
= pet_expr_set_n_arg(expr
, 3);
347 expr
->op
= pet_op_cond
;
348 expr
->args
[pet_ter_cond
] = cond
;
349 expr
->args
[pet_ter_true
] = lhs
;
350 expr
->args
[pet_ter_false
] = rhs
;
360 /* Construct a call pet_expr that calls function "name" with "n_arg"
361 * arguments. The caller is responsible for filling in the arguments.
363 __isl_give pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
368 expr
= pet_expr_alloc(ctx
, pet_expr_call
);
369 expr
= pet_expr_set_n_arg(expr
, n_arg
);
373 expr
->name
= strdup(name
);
375 return pet_expr_free(expr
);
380 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
382 __isl_give pet_expr
*pet_expr_new_cast(const char *type_name
,
383 __isl_take pet_expr
*arg
)
391 ctx
= pet_expr_get_ctx(arg
);
392 expr
= pet_expr_alloc(ctx
, pet_expr_cast
);
393 expr
= pet_expr_set_n_arg(expr
, 1);
397 expr
->type_name
= strdup(type_name
);
398 if (!expr
->type_name
)
410 /* Construct a pet_expr that represents the double "d".
412 __isl_give pet_expr
*pet_expr_new_double(isl_ctx
*ctx
,
413 double val
, const char *s
)
417 expr
= pet_expr_alloc(ctx
, pet_expr_double
);
422 expr
->d
.s
= strdup(s
);
424 return pet_expr_free(expr
);
429 /* Construct a pet_expr that represents the integer value "v".
431 __isl_give pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
439 ctx
= isl_val_get_ctx(v
);
440 expr
= pet_expr_alloc(ctx
, pet_expr_int
);
452 static __isl_give pet_expr
*pet_expr_dup(__isl_keep pet_expr
*expr
)
460 dup
= pet_expr_alloc(expr
->ctx
, expr
->type
);
461 dup
= pet_expr_set_type_size(dup
, expr
->type_size
);
462 dup
= pet_expr_set_n_arg(dup
, expr
->n_arg
);
463 for (i
= 0; i
< expr
->n_arg
; ++i
)
464 dup
= pet_expr_set_arg(dup
, i
, pet_expr_copy(expr
->args
[i
]));
466 switch (expr
->type
) {
467 case pet_expr_access
:
468 if (expr
->acc
.ref_id
)
469 dup
= pet_expr_access_set_ref_id(dup
,
470 isl_id_copy(expr
->acc
.ref_id
));
471 dup
= pet_expr_access_set_access(dup
,
472 isl_map_copy(expr
->acc
.access
));
473 dup
= pet_expr_access_set_index(dup
,
474 isl_multi_pw_aff_copy(expr
->acc
.index
));
475 dup
= pet_expr_access_set_read(dup
, expr
->acc
.read
);
476 dup
= pet_expr_access_set_write(dup
, expr
->acc
.write
);
479 dup
= pet_expr_call_set_name(dup
, expr
->name
);
482 dup
= pet_expr_cast_set_type_name(dup
, expr
->type_name
);
484 case pet_expr_double
:
485 dup
= pet_expr_double_set(dup
, expr
->d
.val
, expr
->d
.s
);
488 dup
= pet_expr_int_set_val(dup
, isl_val_copy(expr
->i
));
491 dup
= pet_expr_op_set_type(dup
, expr
->op
);
494 dup
= pet_expr_free(dup
);
501 __isl_give pet_expr
*pet_expr_cow(__isl_take pet_expr
*expr
)
509 return pet_expr_dup(expr
);
512 __isl_null pet_expr
*pet_expr_free(__isl_take pet_expr
*expr
)
521 for (i
= 0; i
< expr
->n_arg
; ++i
)
522 pet_expr_free(expr
->args
[i
]);
525 switch (expr
->type
) {
526 case pet_expr_access
:
527 isl_id_free(expr
->acc
.ref_id
);
528 isl_map_free(expr
->acc
.access
);
529 isl_multi_pw_aff_free(expr
->acc
.index
);
535 free(expr
->type_name
);
537 case pet_expr_double
:
541 isl_val_free(expr
->i
);
548 isl_ctx_deref(expr
->ctx
);
553 /* Return an additional reference to "expr".
555 __isl_give pet_expr
*pet_expr_copy(__isl_keep pet_expr
*expr
)
564 /* Return the isl_ctx in which "expr" was created.
566 isl_ctx
*pet_expr_get_ctx(__isl_keep pet_expr
*expr
)
568 return expr
? expr
->ctx
: NULL
;
571 /* Return the type of "expr".
573 enum pet_expr_type
pet_expr_get_type(__isl_keep pet_expr
*expr
)
576 return pet_expr_error
;
580 /* Return the number of arguments of "expr".
582 int pet_expr_get_n_arg(__isl_keep pet_expr
*expr
)
590 /* Set the number of arguments of "expr" to "n".
592 * If "expr" originally had more arguments, then remove the extra arguments.
593 * If "expr" originally had fewer arguments, then create space for
594 * the extra arguments ans initialize them to NULL.
596 __isl_give pet_expr
*pet_expr_set_n_arg(__isl_take pet_expr
*expr
, int n
)
603 if (expr
->n_arg
== n
)
605 expr
= pet_expr_cow(expr
);
609 if (n
< expr
->n_arg
) {
610 for (i
= n
; i
< expr
->n_arg
; ++i
)
611 pet_expr_free(expr
->args
[i
]);
616 args
= isl_realloc_array(expr
->ctx
, expr
->args
, pet_expr
*, n
);
618 return pet_expr_free(expr
);
620 for (i
= expr
->n_arg
; i
< n
; ++i
)
621 expr
->args
[i
] = NULL
;
627 /* Return the argument of "expr" at position "pos".
629 __isl_give pet_expr
*pet_expr_get_arg(__isl_keep pet_expr
*expr
, int pos
)
633 if (pos
< 0 || pos
>= expr
->n_arg
)
634 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
635 "position out of bounds", return NULL
);
637 return pet_expr_copy(expr
->args
[pos
]);
640 /* Replace the argument of "expr" at position "pos" by "arg".
642 __isl_give pet_expr
*pet_expr_set_arg(__isl_take pet_expr
*expr
, int pos
,
643 __isl_take pet_expr
*arg
)
647 if (pos
< 0 || pos
>= expr
->n_arg
)
648 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
649 "position out of bounds", goto error
);
650 if (expr
->args
[pos
] == arg
) {
655 expr
= pet_expr_cow(expr
);
659 pet_expr_free(expr
->args
[pos
]);
660 expr
->args
[pos
] = arg
;
669 /* Does "expr" perform a comparison operation?
671 int pet_expr_is_comparison(__isl_keep pet_expr
*expr
)
675 if (expr
->type
!= pet_expr_op
)
690 /* Does "expr" perform a boolean operation?
692 int pet_expr_is_boolean(__isl_keep pet_expr
*expr
)
696 if (expr
->type
!= pet_expr_op
)
708 /* Is "expr" an assume statement?
710 int pet_expr_is_assume(__isl_keep pet_expr
*expr
)
714 if (expr
->type
!= pet_expr_op
)
716 return expr
->op
== pet_op_assume
;
719 /* Does "expr" perform a min operation?
721 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
725 if (expr
->type
!= pet_expr_call
)
727 if (expr
->n_arg
!= 2)
729 if (strcmp(expr
->name
, "min") != 0)
734 /* Does "expr" perform a max operation?
736 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
740 if (expr
->type
!= pet_expr_call
)
742 if (expr
->n_arg
!= 2)
744 if (strcmp(expr
->name
, "max") != 0)
749 /* Does "expr" represent an access to an unnamed space, i.e.,
750 * does it represent an affine expression?
752 int pet_expr_is_affine(__isl_keep pet_expr
*expr
)
758 if (expr
->type
!= pet_expr_access
)
761 has_id
= isl_multi_pw_aff_has_tuple_id(expr
->acc
.index
, isl_dim_out
);
768 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
769 * not part of any struct?
771 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
775 if (expr
->type
!= pet_expr_access
)
777 if (isl_map_range_is_wrapping(expr
->acc
.access
))
780 return isl_map_dim(expr
->acc
.access
, isl_dim_out
) == 0;
783 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
786 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
787 __isl_keep isl_multi_pw_aff
*mpa2
)
791 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
792 if (equal
< 0 || equal
)
794 mpa2
= isl_multi_pw_aff_copy(mpa2
);
795 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
796 isl_multi_pw_aff_get_space(mpa1
));
797 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
798 isl_multi_pw_aff_free(mpa2
);
803 /* Return 1 if the two pet_exprs are equivalent.
805 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
809 if (!expr1
|| !expr2
)
812 if (expr1
->type
!= expr2
->type
)
814 if (expr1
->n_arg
!= expr2
->n_arg
)
816 for (i
= 0; i
< expr1
->n_arg
; ++i
)
817 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
819 switch (expr1
->type
) {
822 case pet_expr_double
:
823 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
825 if (expr1
->d
.val
!= expr2
->d
.val
)
829 if (!isl_val_eq(expr1
->i
, expr2
->i
))
832 case pet_expr_access
:
833 if (expr1
->acc
.read
!= expr2
->acc
.read
)
835 if (expr1
->acc
.write
!= expr2
->acc
.write
)
837 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
839 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
841 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
843 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
845 if (!multi_pw_aff_is_equal(expr1
->acc
.index
, expr2
->acc
.index
))
849 if (expr1
->op
!= expr2
->op
)
853 if (strcmp(expr1
->name
, expr2
->name
))
857 if (strcmp(expr1
->type_name
, expr2
->type_name
))
865 /* Does the access expression "expr" read the accessed elements?
867 int pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
871 if (expr
->type
!= pet_expr_access
)
872 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
873 "not an access expression", return -1);
875 return expr
->acc
.read
;
878 /* Does the access expression "expr" write to the accessed elements?
880 int pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
884 if (expr
->type
!= pet_expr_access
)
885 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
886 "not an access expression", return -1);
888 return expr
->acc
.write
;
891 /* Return the identifier of the array accessed by "expr".
893 * If "expr" represents a member access, then return the identifier
894 * of the outer structure array.
896 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
900 if (expr
->type
!= pet_expr_access
)
901 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
902 "not an access expression", return NULL
);
904 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
)) {
908 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
909 space
= isl_space_range(space
);
910 while (space
&& isl_space_is_wrapping(space
))
911 space
= isl_space_domain(isl_space_unwrap(space
));
912 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
913 isl_space_free(space
);
918 return isl_multi_pw_aff_get_tuple_id(expr
->acc
.index
, isl_dim_out
);
921 /* Return the parameter space of "expr".
923 __isl_give isl_space
*pet_expr_access_get_parameter_space(
924 __isl_keep pet_expr
*expr
)
930 if (expr
->type
!= pet_expr_access
)
931 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
932 "not an access expression", return NULL
);
934 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
935 space
= isl_space_params(space
);
940 /* Return the domain space of "expr", without the arguments (if any).
942 __isl_give isl_space
*pet_expr_access_get_domain_space(
943 __isl_keep pet_expr
*expr
)
949 if (expr
->type
!= pet_expr_access
)
950 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
951 "not an access expression", return NULL
);
953 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
954 space
= isl_space_domain(space
);
955 if (isl_space_is_wrapping(space
))
956 space
= isl_space_domain(isl_space_unwrap(space
));
961 /* Return the space of the data accessed by "expr".
963 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
969 if (expr
->type
!= pet_expr_access
)
970 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
971 "not an access expression", return NULL
);
973 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
974 space
= isl_space_range(space
);
979 /* Modify all expressions of type pet_expr_access in "expr"
980 * by calling "fn" on them.
982 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
983 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
988 n
= pet_expr_get_n_arg(expr
);
989 for (i
= 0; i
< n
; ++i
) {
990 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
991 arg
= pet_expr_map_access(arg
, fn
, user
);
992 expr
= pet_expr_set_arg(expr
, i
, arg
);
998 if (expr
->type
== pet_expr_access
)
999 expr
= fn(expr
, user
);
1004 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1006 * Return -1 on error (where fn returning a negative value is treated as
1008 * Otherwise return 0.
1010 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
1011 enum pet_expr_type type
,
1012 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1019 for (i
= 0; i
< expr
->n_arg
; ++i
)
1020 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1021 type
, fn
, user
) < 0)
1024 if (expr
->type
== type
)
1025 return fn(expr
, user
);
1030 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1032 * Return -1 on error (where fn returning a negative value is treated as
1034 * Otherwise return 0.
1036 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1037 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1039 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1042 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1044 * Return -1 on error (where fn returning a negative value is treated as
1046 * Otherwise return 0.
1048 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1049 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1051 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1054 /* Internal data structure for pet_expr_writes.
1055 * "id" is the identifier that we are looking for.
1056 * "found" is set if we have found the identifier being written to.
1058 struct pet_expr_writes_data
{
1063 /* Given an access expression, check if it writes to data->id.
1064 * If so, set data->found and abort the search.
1066 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1068 struct pet_expr_writes_data
*data
= user
;
1071 if (!expr
->acc
.write
)
1073 if (pet_expr_is_affine(expr
))
1076 write_id
= pet_expr_access_get_id(expr
);
1077 isl_id_free(write_id
);
1082 if (write_id
!= data
->id
)
1089 /* Does expression "expr" write to "id"?
1091 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1093 struct pet_expr_writes_data data
;
1097 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1104 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1105 * index expression and access relation of "expr"
1106 * to dimensions of "dst_type" at "dst_pos".
1108 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1109 enum isl_dim_type dst_type
, unsigned dst_pos
,
1110 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1112 expr
= pet_expr_cow(expr
);
1115 if (expr
->type
!= pet_expr_access
)
1116 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1117 "not an access pet_expr", return pet_expr_free(expr
));
1119 expr
->acc
.access
= isl_map_move_dims(expr
->acc
.access
,
1120 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1121 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1122 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1123 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1124 return pet_expr_free(expr
);
1129 /* Replace the index expression and access relation of "expr"
1130 * by their preimages under the function represented by "ma".
1132 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1133 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1135 expr
= pet_expr_cow(expr
);
1138 if (expr
->type
!= pet_expr_access
)
1139 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1140 "not an access pet_expr", goto error
);
1142 expr
->acc
.access
= isl_map_preimage_domain_multi_aff(expr
->acc
.access
,
1143 isl_multi_aff_copy(ma
));
1144 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1146 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1147 return pet_expr_free(expr
);
1151 isl_multi_aff_free(ma
);
1152 pet_expr_free(expr
);
1156 /* Replace the index expression and access relation of "expr"
1157 * by their preimages under the function represented by "mpa".
1159 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1160 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1162 expr
= pet_expr_cow(expr
);
1165 if (expr
->type
!= pet_expr_access
)
1166 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1167 "not an access pet_expr", goto error
);
1169 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
1170 expr
->acc
.access
, isl_multi_pw_aff_copy(mpa
));
1171 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1172 expr
->acc
.index
, mpa
);
1173 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1174 return pet_expr_free(expr
);
1178 isl_multi_pw_aff_free(mpa
);
1179 pet_expr_free(expr
);
1183 /* Return the access relation of access expression "expr".
1185 __isl_give isl_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
)
1189 if (expr
->type
!= pet_expr_access
)
1190 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1191 "not an access expression", return NULL
);
1193 return isl_map_copy(expr
->acc
.access
);
1196 /* Return the index expression of access expression "expr".
1198 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1199 __isl_keep pet_expr
*expr
)
1203 if (expr
->type
!= pet_expr_access
)
1204 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1205 "not an access expression", return NULL
);
1207 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1210 /* Align the parameters of expr->acc.index and expr->acc.access.
1212 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1214 expr
= pet_expr_cow(expr
);
1217 if (expr
->type
!= pet_expr_access
)
1218 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1219 "not an access expression", return pet_expr_free(expr
));
1221 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1222 isl_multi_pw_aff_get_space(expr
->acc
.index
));
1223 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1224 isl_map_get_space(expr
->acc
.access
));
1225 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1226 return pet_expr_free(expr
);
1231 /* Are "expr1" and "expr2" both array accesses such that
1232 * the access relation of "expr1" is a subset of that of "expr2"?
1233 * Only take into account the first "n_arg" arguments.
1235 * This function is tailored for use by mark_self_dependences in nest.c.
1236 * In particular, the input expressions may have more than "n_arg"
1237 * elements in their arguments arrays, while only the first "n_arg"
1238 * elements are referenced from the access relations.
1240 int pet_expr_is_sub_access(__isl_keep pet_expr
*expr1
,
1241 __isl_keep pet_expr
*expr2
, int n_arg
)
1246 if (!expr1
|| !expr2
)
1248 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1250 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1252 if (pet_expr_is_affine(expr1
))
1254 if (pet_expr_is_affine(expr2
))
1256 n1
= pet_expr_get_n_arg(expr1
);
1259 n2
= pet_expr_get_n_arg(expr2
);
1264 for (i
= 0; i
< n1
; ++i
) {
1266 equal
= pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]);
1267 if (equal
< 0 || !equal
)
1270 id1
= pet_expr_access_get_id(expr1
);
1271 id2
= pet_expr_access_get_id(expr2
);
1279 return isl_map_is_subset(expr1
->acc
.access
, expr2
->acc
.access
);
1282 /* Given a set in the iteration space "domain", extend it to live in the space
1283 * of the domain of access relations.
1285 * That, is the number of arguments "n" is 0, then simply return domain.
1286 * Otherwise, return [domain -> [a_1,...,a_n]].
1288 static __isl_give isl_set
*add_arguments(__isl_take isl_set
*domain
, int n
)
1295 map
= isl_map_from_domain(domain
);
1296 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
1297 return isl_map_wrap(map
);
1300 /* Add extra conditions to the domains of all access relations in "expr".
1302 * The conditions are not added to the index expression. Instead, they
1303 * are used to try and simplify the index expression.
1305 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1306 __isl_take isl_set
*cond
)
1310 expr
= pet_expr_cow(expr
);
1314 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1315 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1316 isl_set_copy(cond
));
1321 if (expr
->type
== pet_expr_access
) {
1322 cond
= add_arguments(cond
, expr
->n_arg
);
1323 expr
->acc
.access
= isl_map_intersect_domain(expr
->acc
.access
,
1324 isl_set_copy(cond
));
1325 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
,
1326 isl_set_copy(cond
));
1327 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1335 return pet_expr_free(expr
);
1338 /* Modify the access relation and index expression
1339 * of the given access expression
1340 * based on the given iteration space transformation.
1341 * In particular, precompose the access relation and index expression
1342 * with the update function.
1344 * If the access has any arguments then the domain of the access relation
1345 * is a wrapped mapping from the iteration space to the space of
1346 * argument values. We only need to change the domain of this wrapped
1347 * mapping, so we extend the input transformation with an identity mapping
1348 * on the space of argument values.
1350 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1351 __isl_keep isl_multi_pw_aff
*update
)
1353 expr
= pet_expr_cow(expr
);
1356 if (expr
->type
!= pet_expr_access
)
1357 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1358 "not an access expression", return pet_expr_free(expr
));
1360 update
= isl_multi_pw_aff_copy(update
);
1362 if (expr
->n_arg
> 0) {
1364 isl_multi_pw_aff
*id
;
1366 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1367 space
= isl_space_domain(space
);
1368 space
= isl_space_unwrap(space
);
1369 space
= isl_space_range(space
);
1370 space
= isl_space_map_from_set(space
);
1371 id
= isl_multi_pw_aff_identity(space
);
1372 update
= isl_multi_pw_aff_product(update
, id
);
1375 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
1377 isl_multi_pw_aff_copy(update
));
1378 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1379 expr
->acc
.index
, update
);
1380 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1381 return pet_expr_free(expr
);
1386 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1388 isl_multi_pw_aff
*update
= user
;
1390 return pet_expr_access_update_domain(expr
, update
);
1393 /* Modify all access relations in "expr" by precomposing them with
1394 * the given iteration space transformation.
1396 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1397 __isl_take isl_multi_pw_aff
*update
)
1399 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1400 isl_multi_pw_aff_free(update
);
1404 /* Given an expression with accesses that have a 0D anonymous domain,
1405 * replace those domains by "space".
1407 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1408 __isl_take isl_space
*space
)
1410 isl_multi_pw_aff
*mpa
;
1412 space
= isl_space_from_domain(space
);
1413 mpa
= isl_multi_pw_aff_zero(space
);
1414 return pet_expr_update_domain(expr
, mpa
);
1417 /* Add all parameters in "space" to the access relation and index expression
1420 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1422 isl_space
*space
= user
;
1424 expr
= pet_expr_cow(expr
);
1427 if (expr
->type
!= pet_expr_access
)
1428 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1429 "not an access expression", return pet_expr_free(expr
));
1431 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1432 isl_space_copy(space
));
1433 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1434 isl_space_copy(space
));
1435 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1436 return pet_expr_free(expr
);
1441 /* Add all parameters in "space" to all access relations and index expressions
1444 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1445 __isl_take isl_space
*space
)
1447 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1448 isl_space_free(space
);
1452 /* Insert an argument expression corresponding to "test" in front
1453 * of the list of arguments described by *n_arg and *args.
1455 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1456 __isl_keep isl_multi_pw_aff
*test
)
1459 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1462 return pet_expr_free(expr
);
1463 expr
= pet_expr_cow(expr
);
1468 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1470 return pet_expr_free(expr
);
1473 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1475 return pet_expr_free(expr
);
1476 for (i
= 0; i
< expr
->n_arg
; ++i
)
1477 ext
[1 + i
] = expr
->args
[i
];
1482 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1484 return pet_expr_free(expr
);
1489 /* Make the expression "expr" depend on the value of "test"
1490 * being equal to "satisfied".
1492 * If "test" is an affine expression, we simply add the conditions
1493 * on the expression having the value "satisfied" to all access relations
1494 * and index expressions.
1496 * Otherwise, we add a filter to "expr" (which is then assumed to be
1497 * an access expression) corresponding to "test" being equal to "satisfied".
1499 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1500 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1505 isl_pw_multi_aff
*pma
;
1507 expr
= pet_expr_cow(expr
);
1511 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
1515 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
1516 isl_multi_pw_aff_free(test
);
1518 cond
= isl_pw_aff_non_zero_set(pa
);
1520 cond
= isl_pw_aff_zero_set(pa
);
1521 return pet_expr_restrict(expr
, cond
);
1524 ctx
= isl_multi_pw_aff_get_ctx(test
);
1525 if (expr
->type
!= pet_expr_access
)
1526 isl_die(ctx
, isl_error_invalid
,
1527 "can only filter access expressions", goto error
);
1529 space
= isl_space_domain(isl_multi_pw_aff_get_space(expr
->acc
.index
));
1530 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1531 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
1533 expr
->acc
.access
= isl_map_preimage_domain_pw_multi_aff(
1535 isl_pw_multi_aff_copy(pma
));
1536 pma
= isl_pw_multi_aff_gist(pma
,
1537 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
1538 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
1539 expr
->acc
.index
, pma
);
1540 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1543 expr
= insert_access_arg(expr
, test
);
1545 isl_multi_pw_aff_free(test
);
1548 isl_multi_pw_aff_free(test
);
1549 return pet_expr_free(expr
);
1552 /* Add a reference identifier to access expression "expr".
1553 * "user" points to an integer that contains the sequence number
1554 * of the next reference.
1556 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
1563 expr
= pet_expr_cow(expr
);
1566 if (expr
->type
!= pet_expr_access
)
1567 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1568 "not an access expression", return pet_expr_free(expr
));
1570 ctx
= pet_expr_get_ctx(expr
);
1571 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
1572 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
1573 if (!expr
->acc
.ref_id
)
1574 return pet_expr_free(expr
);
1579 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
1581 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
1584 /* Reset the user pointer on all parameter and tuple ids in
1585 * the access relation and the index expressions
1586 * of the access expression "expr".
1588 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
1591 expr
= pet_expr_cow(expr
);
1594 if (expr
->type
!= pet_expr_access
)
1595 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1596 "not an access expression", return pet_expr_free(expr
));
1598 expr
->acc
.access
= isl_map_reset_user(expr
->acc
.access
);
1599 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
1600 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1601 return pet_expr_free(expr
);
1606 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
1608 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
1611 /* Data used in access_gist() callback.
1613 struct pet_access_gist_data
{
1615 isl_union_map
*value_bounds
;
1618 /* Given an expression "expr" of type pet_expr_access, compute
1619 * the gist of the associated access relation and index expression
1620 * with respect to data->domain and the bounds on the values of the arguments
1621 * of the expression.
1623 * The arguments of "expr" have been gisted right before "expr" itself
1624 * is gisted. The gisted arguments may have become equal where before
1625 * they may not have been (obviously) equal. We therefore take
1626 * the opportunity to remove duplicate arguments here.
1628 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
1630 struct pet_access_gist_data
*data
= user
;
1633 expr
= pet_expr_remove_duplicate_args(expr
);
1634 expr
= pet_expr_cow(expr
);
1637 if (expr
->type
!= pet_expr_access
)
1638 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1639 "not an access expression", return pet_expr_free(expr
));
1641 domain
= isl_set_copy(data
->domain
);
1642 if (expr
->n_arg
> 0)
1643 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
1644 data
->value_bounds
);
1646 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
,
1647 isl_set_copy(domain
));
1648 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
1649 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1650 return pet_expr_free(expr
);
1655 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
1656 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
1658 struct pet_access_gist_data data
= { context
, value_bounds
};
1660 return pet_expr_map_access(expr
, &access_gist
, &data
);
1663 /* Mark "expr" as a read dependening on "read".
1665 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
1669 return pet_expr_free(expr
);
1670 if (expr
->type
!= pet_expr_access
)
1671 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1672 "not an access expression", return pet_expr_free(expr
));
1673 if (expr
->acc
.read
== read
)
1675 expr
= pet_expr_cow(expr
);
1678 expr
->acc
.read
= read
;
1683 /* Mark "expr" as a write dependening on "write".
1685 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
1689 return pet_expr_free(expr
);
1690 if (expr
->type
!= pet_expr_access
)
1691 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1692 "not an access expression", return pet_expr_free(expr
));
1693 if (expr
->acc
.write
== write
)
1695 expr
= pet_expr_cow(expr
);
1698 expr
->acc
.write
= write
;
1703 /* Replace the access relation of "expr" by "access".
1705 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
1706 __isl_take isl_map
*access
)
1708 expr
= pet_expr_cow(expr
);
1709 if (!expr
|| !access
)
1711 if (expr
->type
!= pet_expr_access
)
1712 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1713 "not an access expression", goto error
);
1714 isl_map_free(expr
->acc
.access
);
1715 expr
->acc
.access
= access
;
1719 isl_map_free(access
);
1720 pet_expr_free(expr
);
1724 /* Replace the index expression of "expr" by "index".
1726 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
1727 __isl_take isl_multi_pw_aff
*index
)
1729 expr
= pet_expr_cow(expr
);
1730 if (!expr
|| !index
)
1732 if (expr
->type
!= pet_expr_access
)
1733 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1734 "not an access expression", goto error
);
1735 isl_multi_pw_aff_free(expr
->acc
.index
);
1736 expr
->acc
.index
= index
;
1740 isl_multi_pw_aff_free(index
);
1741 pet_expr_free(expr
);
1745 /* Return the reference identifier of access expression "expr".
1747 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
1751 if (expr
->type
!= pet_expr_access
)
1752 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1753 "not an access expression", return NULL
);
1755 return isl_id_copy(expr
->acc
.ref_id
);
1758 /* Replace the reference identifier of access expression "expr" by "ref_id".
1760 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
1761 __isl_take isl_id
*ref_id
)
1763 expr
= pet_expr_cow(expr
);
1764 if (!expr
|| !ref_id
)
1766 if (expr
->type
!= pet_expr_access
)
1767 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1768 "not an access expression", goto error
);
1769 isl_id_free(expr
->acc
.ref_id
);
1770 expr
->acc
.ref_id
= ref_id
;
1774 isl_id_free(ref_id
);
1775 pet_expr_free(expr
);
1779 /* Tag the access relation "access" with "id".
1780 * That is, insert the id as the range of a wrapped relation
1781 * in the domain of "access".
1783 * If "access" is of the form
1787 * then the result is of the form
1789 * [D[i] -> id[]] -> A[a]
1791 __isl_give isl_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
1792 __isl_take isl_map
*access
)
1798 if (expr
->type
!= pet_expr_access
)
1799 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1800 "not an access expression",
1801 return isl_map_free(access
));
1803 id
= isl_id_copy(expr
->acc
.ref_id
);
1804 space
= isl_space_range(isl_map_get_space(access
));
1805 space
= isl_space_from_range(space
);
1806 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
1807 add_tag
= isl_map_universe(space
);
1808 access
= isl_map_domain_product(access
, add_tag
);
1813 /* Return the relation mapping pairs of domain iterations and argument
1814 * values to the corresponding accessed data elements.
1816 __isl_give isl_map
*pet_expr_access_get_dependent_access(
1817 __isl_keep pet_expr
*expr
)
1821 if (expr
->type
!= pet_expr_access
)
1822 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1823 "not an access expression", return NULL
);
1825 return isl_map_copy(expr
->acc
.access
);
1828 /* Return the relation mapping domain iterations to all possibly
1829 * accessed data elements.
1830 * In particular, take the access relation and project out the values
1831 * of the arguments, if any.
1833 __isl_give isl_map
*pet_expr_access_get_may_access(__isl_keep pet_expr
*expr
)
1841 if (expr
->type
!= pet_expr_access
)
1842 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1843 "not an access expression", return NULL
);
1845 access
= pet_expr_access_get_dependent_access(expr
);
1846 if (expr
->n_arg
== 0)
1849 space
= isl_space_domain(isl_map_get_space(access
));
1850 map
= isl_map_universe(isl_space_unwrap(space
));
1851 map
= isl_map_domain_map(map
);
1852 access
= isl_map_apply_domain(access
, map
);
1857 /* Return a relation mapping domain iterations to definitely
1858 * accessed data elements, assuming the statement containing
1859 * the expression is executed.
1861 * If there are no arguments, then all elements are accessed.
1862 * Otherwise, we conservatively return an empty relation.
1864 __isl_give isl_map
*pet_expr_access_get_must_access(__isl_keep pet_expr
*expr
)
1870 if (expr
->type
!= pet_expr_access
)
1871 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1872 "not an access expression", return NULL
);
1874 if (expr
->n_arg
== 0)
1875 return pet_expr_access_get_dependent_access(expr
);
1877 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1878 space
= isl_space_domain_factor_domain(space
);
1880 return isl_map_empty(space
);
1883 /* Return the relation mapping domain iterations to all possibly
1884 * accessed data elements, with its domain tagged with the reference
1887 __isl_give isl_map
*pet_expr_access_get_tagged_may_access(
1888 __isl_keep pet_expr
*expr
)
1895 access
= pet_expr_access_get_may_access(expr
);
1896 access
= pet_expr_tag_access(expr
, access
);
1901 /* Return the operation type of operation expression "expr".
1903 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
1907 if (expr
->type
!= pet_expr_op
)
1908 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1909 "not an operation expression", return pet_op_last
);
1914 /* Replace the operation type of operation expression "expr" by "type".
1916 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
1917 enum pet_op_type type
)
1920 return pet_expr_free(expr
);
1921 if (expr
->type
!= pet_expr_op
)
1922 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1923 "not an operation expression",
1924 return pet_expr_free(expr
));
1925 if (expr
->op
== type
)
1927 expr
= pet_expr_cow(expr
);
1935 /* Return the name of the function called by "expr".
1937 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
1941 if (expr
->type
!= pet_expr_call
)
1942 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1943 "not a call expression", return NULL
);
1947 /* Replace the name of the function called by "expr" by "name".
1949 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
1950 __isl_keep
const char *name
)
1952 expr
= pet_expr_cow(expr
);
1954 return pet_expr_free(expr
);
1955 if (expr
->type
!= pet_expr_call
)
1956 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1957 "not a call expression", return pet_expr_free(expr
));
1959 expr
->name
= strdup(name
);
1961 return pet_expr_free(expr
);
1965 /* Replace the type of the cast performed by "expr" by "name".
1967 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
1968 __isl_keep
const char *name
)
1970 expr
= pet_expr_cow(expr
);
1972 return pet_expr_free(expr
);
1973 if (expr
->type
!= pet_expr_cast
)
1974 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1975 "not a cast expression", return pet_expr_free(expr
));
1976 free(expr
->type_name
);
1977 expr
->type_name
= strdup(name
);
1978 if (!expr
->type_name
)
1979 return pet_expr_free(expr
);
1983 /* Return the value of the integer represented by "expr".
1985 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
1989 if (expr
->type
!= pet_expr_int
)
1990 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1991 "not an int expression", return NULL
);
1993 return isl_val_copy(expr
->i
);
1996 /* Replace the value of the integer represented by "expr" by "v".
1998 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
1999 __isl_take isl_val
*v
)
2001 expr
= pet_expr_cow(expr
);
2004 if (expr
->type
!= pet_expr_int
)
2005 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2006 "not an int expression", goto error
);
2007 isl_val_free(expr
->i
);
2013 pet_expr_free(expr
);
2017 /* Replace the value and string representation of the double
2018 * represented by "expr" by "d" and "s".
2020 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2021 double d
, __isl_keep
const char *s
)
2023 expr
= pet_expr_cow(expr
);
2025 return pet_expr_free(expr
);
2026 if (expr
->type
!= pet_expr_double
)
2027 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2028 "not a double expression", return pet_expr_free(expr
));
2031 expr
->d
.s
= strdup(s
);
2033 return pet_expr_free(expr
);
2037 /* Return a string representation of the double expression "expr".
2039 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2043 if (expr
->type
!= pet_expr_double
)
2044 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2045 "not a double expression", return NULL
);
2046 return strdup(expr
->d
.s
);
2049 /* Return a piecewise affine expression defined on the specified domain
2050 * that represents NaN.
2052 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2054 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2057 /* This function is called when we come across an access that is
2058 * nested in what is supposed to be an affine expression.
2059 * "pc" is the context in which the affine expression is created.
2060 * If nesting is allowed in "pc", we return an affine expression that is
2061 * equal to a new parameter corresponding to this nested access.
2062 * Otherwise, we return NaN.
2064 * Note that we currently don't allow nested accesses themselves
2065 * to contain any nested accesses, so we check if "expr" itself
2066 * involves any nested accesses (either explicitly as arguments
2067 * or implicitly through parameters) and return NaN if it does.
2069 * The new parameter is resolved in resolve_nested.
2071 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2072 __isl_keep pet_context
*pc
)
2077 isl_local_space
*ls
;
2083 if (!pet_context_allow_nesting(pc
))
2084 return non_affine(pet_context_get_space(pc
));
2086 if (pet_expr_get_type(expr
) != pet_expr_access
)
2087 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2088 "not an access expression", return NULL
);
2090 if (expr
->n_arg
> 0)
2091 return non_affine(pet_context_get_space(pc
));
2093 space
= pet_expr_access_get_parameter_space(expr
);
2094 nested
= pet_nested_any_in_space(space
);
2095 isl_space_free(space
);
2097 return non_affine(pet_context_get_space(pc
));
2099 ctx
= pet_expr_get_ctx(expr
);
2100 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2101 space
= pet_context_get_space(pc
);
2102 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2104 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2105 ls
= isl_local_space_from_space(space
);
2106 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2108 return isl_pw_aff_from_aff(aff
);
2111 /* Extract an affine expression from the access pet_expr "expr".
2112 * "pc" is the context in which the affine expression is created.
2114 * If "expr" is actually an affine expression rather than
2115 * a real access, then we return that expression.
2116 * Otherwise, we require that "expr" is of an integral type.
2117 * If not, we return NaN.
2119 * If the variable has been assigned a known affine expression,
2120 * then we return that expression.
2122 * Otherwise, we return an expression that is equal to a parameter
2123 * representing "expr" (if "allow_nested" is set).
2125 static __isl_give isl_pw_aff
*extract_affine_from_access(
2126 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2131 if (pet_expr_is_affine(expr
)) {
2133 isl_multi_pw_aff
*mpa
;
2135 mpa
= pet_expr_access_get_index(expr
);
2136 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
2137 isl_multi_pw_aff_free(mpa
);
2141 if (pet_expr_get_type_size(expr
) == 0)
2142 return non_affine(pet_context_get_space(pc
));
2144 if (!pet_expr_is_scalar_access(expr
))
2145 return nested_access(expr
, pc
);
2147 id
= pet_expr_access_get_id(expr
);
2148 if (pet_context_is_assigned(pc
, id
))
2149 return pet_context_get_value(pc
, id
);
2152 return nested_access(expr
, pc
);
2155 /* Construct an affine expression from the integer constant "expr".
2156 * "pc" is the context in which the affine expression is created.
2158 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2159 __isl_keep pet_context
*pc
)
2161 isl_local_space
*ls
;
2167 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2168 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2170 return isl_pw_aff_from_aff(aff
);
2173 /* Extract an affine expression from an addition or subtraction operation.
2174 * Return NaN if we are unable to extract an affine expression.
2176 * "pc" is the context in which the affine expression is created.
2178 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2179 __isl_keep pet_context
*pc
)
2186 if (expr
->n_arg
!= 2)
2187 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2188 "expecting two arguments", return NULL
);
2190 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2191 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2193 switch (pet_expr_op_get_type(expr
)) {
2195 return isl_pw_aff_add(lhs
, rhs
);
2197 return isl_pw_aff_sub(lhs
, rhs
);
2199 isl_pw_aff_free(lhs
);
2200 isl_pw_aff_free(rhs
);
2201 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2202 "not an addition or subtraction operation",
2208 /* Extract an affine expression from an integer division or a modulo operation.
2209 * Return NaN if we are unable to extract an affine expression.
2211 * "pc" is the context in which the affine expression is created.
2213 * In particular, if "expr" is lhs/rhs, then return
2215 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2217 * If "expr" is lhs%rhs, then return
2219 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2221 * If the second argument (rhs) is not a (positive) integer constant,
2222 * then we fail to extract an affine expression.
2224 * We simplify the result in the context of the domain of "pc" in case
2225 * this domain implies that lhs >= 0 (or < 0).
2227 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2228 __isl_keep pet_context
*pc
)
2237 if (expr
->n_arg
!= 2)
2238 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2239 "expecting two arguments", return NULL
);
2241 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2243 is_cst
= isl_pw_aff_is_cst(rhs
);
2244 if (is_cst
< 0 || !is_cst
) {
2245 isl_pw_aff_free(rhs
);
2246 return non_affine(pet_context_get_space(pc
));
2249 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2251 switch (pet_expr_op_get_type(expr
)) {
2253 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
2256 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
2259 isl_pw_aff_free(lhs
);
2260 isl_pw_aff_free(rhs
);
2261 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2262 "not a div or mod operator", return NULL
);
2265 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
2268 /* Extract an affine expression from a multiplication operation.
2269 * Return NaN if we are unable to extract an affine expression.
2270 * In particular, if neither of the arguments is a (piecewise) constant
2271 * then we return NaN.
2273 * "pc" is the context in which the affine expression is created.
2275 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2276 __isl_keep pet_context
*pc
)
2278 int lhs_cst
, rhs_cst
;
2284 if (expr
->n_arg
!= 2)
2285 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2286 "expecting two arguments", return NULL
);
2288 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2289 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2291 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2292 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2293 if (lhs_cst
< 0 || rhs_cst
< 0 || (!lhs_cst
&& !rhs_cst
)) {
2294 isl_pw_aff_free(lhs
);
2295 isl_pw_aff_free(rhs
);
2296 return non_affine(pet_context_get_space(pc
));
2299 return isl_pw_aff_mul(lhs
, rhs
);
2302 /* Extract an affine expression from a negation operation.
2303 * Return NaN if we are unable to extract an affine expression.
2305 * "pc" is the context in which the affine expression is created.
2307 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
2308 __isl_keep pet_context
*pc
)
2314 if (expr
->n_arg
!= 1)
2315 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2316 "expecting one argument", return NULL
);
2318 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
2319 return isl_pw_aff_neg(res
);
2322 /* Extract an affine expression from a conditional operation.
2323 * Return NaN if we are unable to extract an affine expression.
2325 * "pc" is the context in which the affine expression is created.
2327 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
2328 __isl_keep pet_context
*pc
)
2330 isl_pw_aff
*cond
, *lhs
, *rhs
;
2334 if (expr
->n_arg
!= 3)
2335 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2336 "expecting three arguments", return NULL
);
2338 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2339 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2340 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
2342 return isl_pw_aff_cond(cond
, lhs
, rhs
);
2349 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
, unsigned width
)
2354 ctx
= isl_pw_aff_get_ctx(pwaff
);
2355 mod
= isl_val_int_from_ui(ctx
, width
);
2356 mod
= isl_val_2exp(mod
);
2358 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
2363 /* Limit the domain of "pwaff" to those elements where the function
2366 * 2^{width-1} <= pwaff < 2^{width-1}
2368 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
2373 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
2374 isl_local_space
*ls
= isl_local_space_from_space(space
);
2379 ctx
= isl_pw_aff_get_ctx(pwaff
);
2380 v
= isl_val_int_from_ui(ctx
, width
- 1);
2381 v
= isl_val_2exp(v
);
2383 bound
= isl_aff_zero_on_domain(ls
);
2384 bound
= isl_aff_add_constant_val(bound
, v
);
2385 b
= isl_pw_aff_from_aff(bound
);
2387 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
2388 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2390 b
= isl_pw_aff_neg(b
);
2391 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
2392 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2397 /* Handle potential overflows on signed computations.
2399 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2400 * then we adjust the domain of "pa" to avoid overflows.
2402 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
2406 struct pet_options
*options
;
2411 ctx
= isl_pw_aff_get_ctx(pa
);
2412 options
= isl_ctx_peek_pet_options(ctx
);
2413 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
2414 pa
= avoid_overflow(pa
, width
);
2419 /* Extract an affine expression from some an operation.
2420 * Return NaN if we are unable to extract an affine expression.
2421 * If the result of a binary (non boolean) operation is unsigned,
2422 * then we wrap it based on the size of the type. If the result is signed,
2423 * then we ensure that no overflow occurs.
2425 * "pc" is the context in which the affine expression is created.
2427 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
2428 __isl_keep pet_context
*pc
)
2433 switch (pet_expr_op_get_type(expr
)) {
2436 res
= extract_affine_add_sub(expr
, pc
);
2440 res
= extract_affine_div_mod(expr
, pc
);
2443 res
= extract_affine_mul(expr
, pc
);
2446 return extract_affine_neg(expr
, pc
);
2448 return extract_affine_cond(expr
, pc
);
2458 return pet_expr_extract_affine_condition(expr
, pc
);
2460 return non_affine(pet_context_get_space(pc
));
2465 if (isl_pw_aff_involves_nan(res
)) {
2466 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
2467 isl_pw_aff_free(res
);
2468 return non_affine(space
);
2471 type_size
= pet_expr_get_type_size(expr
);
2473 res
= wrap(res
, type_size
);
2475 res
= signed_overflow(res
, -type_size
);
2480 /* Extract an affine expression from some special function calls.
2481 * Return NaN if we are unable to extract an affine expression.
2482 * In particular, we handle "min", "max", "ceild", "floord",
2483 * "intMod", "intFloor" and "intCeil".
2484 * In case of the latter five, the second argument needs to be
2485 * a (positive) integer constant.
2487 * "pc" is the context in which the affine expression is created.
2489 static __isl_give isl_pw_aff
*extract_affine_from_call(
2490 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2492 isl_pw_aff
*aff1
, *aff2
;
2496 n
= pet_expr_get_n_arg(expr
);
2497 name
= pet_expr_call_get_name(expr
);
2498 if (!(n
== 2 && !strcmp(name
, "min")) &&
2499 !(n
== 2 && !strcmp(name
, "max")) &&
2500 !(n
== 2 && !strcmp(name
, "intMod")) &&
2501 !(n
== 2 && !strcmp(name
, "intFloor")) &&
2502 !(n
== 2 && !strcmp(name
, "intCeil")) &&
2503 !(n
== 2 && !strcmp(name
, "floord")) &&
2504 !(n
== 2 && !strcmp(name
, "ceild")))
2505 return non_affine(pet_context_get_space(pc
));
2507 if (!strcmp(name
, "min") || !strcmp(name
, "max")) {
2508 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2509 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
2511 if (!strcmp(name
, "min"))
2512 aff1
= isl_pw_aff_min(aff1
, aff2
);
2514 aff1
= isl_pw_aff_max(aff1
, aff2
);
2515 } else if (!strcmp(name
, "intMod")) {
2518 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2519 return non_affine(pet_context_get_space(pc
));
2520 v
= pet_expr_int_get_val(expr
->args
[1]);
2521 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2522 aff1
= isl_pw_aff_mod_val(aff1
, v
);
2526 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2527 return non_affine(pet_context_get_space(pc
));
2528 v
= pet_expr_int_get_val(expr
->args
[1]);
2529 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2530 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
2531 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
2532 aff1
= isl_pw_aff_floor(aff1
);
2534 aff1
= isl_pw_aff_ceil(aff1
);
2540 /* Extract an affine expression from "expr", if possible.
2541 * Otherwise return NaN.
2543 * "pc" is the context in which the affine expression is created.
2545 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
2546 __isl_keep pet_context
*pc
)
2551 switch (pet_expr_get_type(expr
)) {
2552 case pet_expr_access
:
2553 return extract_affine_from_access(expr
, pc
);
2555 return extract_affine_from_int(expr
, pc
);
2557 return extract_affine_from_op(expr
, pc
);
2559 return extract_affine_from_call(expr
, pc
);
2561 case pet_expr_double
:
2562 case pet_expr_error
:
2563 return non_affine(pet_context_get_space(pc
));
2567 /* Extract an affine expressions representing the comparison "LHS op RHS"
2568 * Return NaN if we are unable to extract such an affine expression.
2570 * "pc" is the context in which the affine expression is created.
2572 * If the comparison is of the form
2576 * then the expression is constructed as the conjunction of
2581 * A similar optimization is performed for max(a,b) <= c.
2582 * We do this because that will lead to simpler representations
2583 * of the expression.
2584 * If isl is ever enhanced to explicitly deal with min and max expressions,
2585 * this optimization can be removed.
2587 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
2588 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
2589 __isl_keep pet_context
*pc
)
2591 isl_pw_aff
*lhs_pa
, *rhs_pa
;
2593 if (op
== pet_op_gt
)
2594 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
2595 if (op
== pet_op_ge
)
2596 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
2598 if (op
== pet_op_lt
|| op
== pet_op_le
) {
2599 if (pet_expr_is_min(rhs
)) {
2600 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2602 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2604 return pet_and(lhs_pa
, rhs_pa
);
2606 if (pet_expr_is_max(lhs
)) {
2607 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
2609 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
2611 return pet_and(lhs_pa
, rhs_pa
);
2615 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
2616 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
2618 return pet_comparison(op
, lhs_pa
, rhs_pa
);
2621 /* Extract an affine expressions from the comparison "expr".
2622 * Return NaN if we are unable to extract such an affine expression.
2624 * "pc" is the context in which the affine expression is created.
2626 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
2627 __isl_keep pet_context
*pc
)
2629 enum pet_op_type type
;
2633 if (expr
->n_arg
!= 2)
2634 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2635 "expecting two arguments", return NULL
);
2637 type
= pet_expr_op_get_type(expr
);
2638 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
2642 /* Extract an affine expression representing the boolean operation
2643 * expressed by "expr".
2644 * Return NaN if we are unable to extract an affine expression.
2646 * "pc" is the context in which the affine expression is created.
2648 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
2649 __isl_keep pet_context
*pc
)
2651 isl_pw_aff
*lhs
, *rhs
;
2657 n
= pet_expr_get_n_arg(expr
);
2658 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2660 return pet_not(lhs
);
2662 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
2663 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
2666 /* Extract the affine expression "expr != 0 ? 1 : 0".
2667 * Return NaN if we are unable to extract an affine expression.
2669 * "pc" is the context in which the affine expression is created.
2671 static __isl_give isl_pw_aff
*extract_implicit_condition(
2672 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2676 res
= pet_expr_extract_affine(expr
, pc
);
2677 return pet_to_bool(res
);
2680 /* Extract a boolean affine expression from "expr".
2681 * Return NaN if we are unable to extract an affine expression.
2683 * "pc" is the context in which the affine expression is created.
2685 * If "expr" is neither a comparison nor a boolean operation,
2686 * then we assume it is an affine expression and return the
2687 * boolean expression "expr != 0 ? 1 : 0".
2689 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
2690 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2695 if (pet_expr_is_comparison(expr
))
2696 return extract_comparison(expr
, pc
);
2697 if (pet_expr_is_boolean(expr
))
2698 return extract_boolean(expr
, pc
);
2700 return extract_implicit_condition(expr
, pc
);
2703 /* Check if "expr" is an assume expression and if its single argument
2704 * can be converted to an affine expression in the context of "pc".
2705 * If so, replace the argument by the affine expression.
2707 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
2708 __isl_keep pet_context
*pc
)
2711 isl_multi_pw_aff
*index
;
2715 if (!pet_expr_is_assume(expr
))
2717 if (expr
->n_arg
!= 1)
2718 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2719 "expecting one argument", return pet_expr_free(expr
));
2721 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2723 return pet_expr_free(expr
);
2724 if (isl_pw_aff_involves_nan(cond
)) {
2725 isl_pw_aff_free(cond
);
2729 index
= isl_multi_pw_aff_from_pw_aff(cond
);
2730 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
2735 /* Return the number of bits needed to represent the type of "expr".
2736 * See the description of the type_size field of pet_expr.
2738 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
2740 return expr
? expr
->type_size
: 0;
2743 /* Replace the number of bits needed to represent the type of "expr"
2745 * See the description of the type_size field of pet_expr.
2747 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
2750 expr
= pet_expr_cow(expr
);
2754 expr
->type_size
= type_size
;
2759 /* Extend an access expression "expr" with an additional index "index".
2760 * In particular, add "index" as an extra argument to "expr" and
2761 * adjust the index expression of "expr" to refer to this extra argument.
2762 * The caller is responsible for calling pet_expr_access_set_depth
2763 * to update the corresponding access relation.
2765 * Note that we only collect the individual index expressions as
2766 * arguments of "expr" here.
2767 * An attempt to integrate them into the index expression of "expr"
2768 * is performed in pet_expr_access_plug_in_args.
2770 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
2771 __isl_take pet_expr
*index
)
2775 isl_local_space
*ls
;
2778 expr
= pet_expr_cow(expr
);
2779 if (!expr
|| !index
)
2781 if (expr
->type
!= pet_expr_access
)
2782 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2783 "not an access pet_expr", goto error
);
2785 n
= pet_expr_get_n_arg(expr
);
2786 expr
= pet_expr_insert_arg(expr
, n
, index
);
2790 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
2791 ls
= isl_local_space_from_space(space
);
2792 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
2793 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
2794 if (!expr
->acc
.index
)
2795 return pet_expr_free(expr
);
2799 pet_expr_free(expr
);
2800 pet_expr_free(index
);
2804 /* Extend an access expression "expr" with an additional member acces to "id".
2805 * In particular, extend the index expression of "expr" to include
2806 * the additional member access.
2807 * The caller is responsible for calling pet_expr_access_set_depth
2808 * to update the corresponding access relation.
2810 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
2811 __isl_take isl_id
*id
)
2814 isl_multi_pw_aff
*field_access
;
2816 expr
= pet_expr_cow(expr
);
2819 if (expr
->type
!= pet_expr_access
)
2820 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2821 "not an access pet_expr", goto error
);
2823 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
2824 space
= isl_space_from_domain(space
);
2825 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2826 field_access
= isl_multi_pw_aff_zero(space
);
2827 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
2828 if (!expr
->acc
.index
)
2829 return pet_expr_free(expr
);
2833 pet_expr_free(expr
);
2838 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
2845 fprintf(stderr
, "%*s", indent
, "");
2847 switch (expr
->type
) {
2848 case pet_expr_double
:
2849 fprintf(stderr
, "%s\n", expr
->d
.s
);
2852 isl_val_dump(expr
->i
);
2854 case pet_expr_access
:
2855 if (expr
->acc
.ref_id
) {
2856 isl_id_dump(expr
->acc
.ref_id
);
2857 fprintf(stderr
, "%*s", indent
, "");
2859 isl_map_dump(expr
->acc
.access
);
2860 fprintf(stderr
, "%*s", indent
, "");
2861 isl_multi_pw_aff_dump(expr
->acc
.index
);
2862 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
2863 "", expr
->acc
.read
);
2864 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
2865 "", expr
->acc
.write
);
2866 for (i
= 0; i
< expr
->n_arg
; ++i
)
2867 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2870 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
2871 for (i
= 0; i
< expr
->n_arg
; ++i
)
2872 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2875 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
2876 for (i
= 0; i
< expr
->n_arg
; ++i
)
2877 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2880 fprintf(stderr
, "(%s)\n", expr
->type_name
);
2881 for (i
= 0; i
< expr
->n_arg
; ++i
)
2882 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2884 case pet_expr_error
:
2885 fprintf(stderr
, "ERROR\n");
2890 void pet_expr_dump(__isl_keep pet_expr
*expr
)
2892 pet_expr_dump_with_indent(expr
, 0);