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_map_has_tuple_id(expr
->acc
.access
, 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 /* Return 1 if the two pet_exprs are equivalent.
785 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
789 if (!expr1
|| !expr2
)
792 if (expr1
->type
!= expr2
->type
)
794 if (expr1
->n_arg
!= expr2
->n_arg
)
796 for (i
= 0; i
< expr1
->n_arg
; ++i
)
797 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
799 switch (expr1
->type
) {
802 case pet_expr_double
:
803 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
805 if (expr1
->d
.val
!= expr2
->d
.val
)
809 if (!isl_val_eq(expr1
->i
, expr2
->i
))
812 case pet_expr_access
:
813 if (expr1
->acc
.read
!= expr2
->acc
.read
)
815 if (expr1
->acc
.write
!= expr2
->acc
.write
)
817 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
819 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
821 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
823 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
825 if (!isl_multi_pw_aff_plain_is_equal(expr1
->acc
.index
,
830 if (expr1
->op
!= expr2
->op
)
834 if (strcmp(expr1
->name
, expr2
->name
))
838 if (strcmp(expr1
->type_name
, expr2
->type_name
))
846 /* Does the access expression "expr" read the accessed elements?
848 int pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
852 if (expr
->type
!= pet_expr_access
)
853 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
854 "not an access expression", return -1);
856 return expr
->acc
.read
;
859 /* Does the access expression "expr" write to the accessed elements?
861 int pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
865 if (expr
->type
!= pet_expr_access
)
866 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
867 "not an access expression", return -1);
869 return expr
->acc
.write
;
872 /* Return the identifier of the array accessed by "expr".
874 * If "expr" represents a member access, then return the identifier
875 * of the outer structure array.
877 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
881 if (expr
->type
!= pet_expr_access
)
882 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
883 "not an access expression", return NULL
);
885 if (isl_map_range_is_wrapping(expr
->acc
.access
)) {
889 space
= isl_map_get_space(expr
->acc
.access
);
890 space
= isl_space_range(space
);
891 while (space
&& isl_space_is_wrapping(space
))
892 space
= isl_space_domain(isl_space_unwrap(space
));
893 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
894 isl_space_free(space
);
899 return isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
902 /* Return the parameter space of "expr".
904 __isl_give isl_space
*pet_expr_access_get_parameter_space(
905 __isl_keep pet_expr
*expr
)
911 if (expr
->type
!= pet_expr_access
)
912 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
913 "not an access expression", return NULL
);
915 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
916 space
= isl_space_params(space
);
921 /* Return the domain space of "expr", without the arguments (if any).
923 __isl_give isl_space
*pet_expr_access_get_domain_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_domain(space
);
936 if (isl_space_is_wrapping(space
))
937 space
= isl_space_domain(isl_space_unwrap(space
));
942 /* Return the space of the data accessed by "expr".
944 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
950 if (expr
->type
!= pet_expr_access
)
951 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
952 "not an access expression", return NULL
);
954 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
955 space
= isl_space_range(space
);
960 /* Modify all expressions of type pet_expr_access in "expr"
961 * by calling "fn" on them.
963 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
964 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
969 n
= pet_expr_get_n_arg(expr
);
970 for (i
= 0; i
< n
; ++i
) {
971 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
972 arg
= pet_expr_map_access(arg
, fn
, user
);
973 expr
= pet_expr_set_arg(expr
, i
, arg
);
979 if (expr
->type
== pet_expr_access
)
980 expr
= fn(expr
, user
);
985 /* Call "fn" on each of the subexpressions of "expr" of type "type".
987 * Return -1 on error (where fn returning a negative value is treated as
989 * Otherwise return 0.
991 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
992 enum pet_expr_type type
,
993 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1000 for (i
= 0; i
< expr
->n_arg
; ++i
)
1001 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1002 type
, fn
, user
) < 0)
1005 if (expr
->type
== type
)
1006 return fn(expr
, user
);
1011 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1013 * Return -1 on error (where fn returning a negative value is treated as
1015 * Otherwise return 0.
1017 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1018 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1020 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1023 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1025 * Return -1 on error (where fn returning a negative value is treated as
1027 * Otherwise return 0.
1029 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1030 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1032 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1035 /* Internal data structure for pet_expr_writes.
1036 * "id" is the identifier that we are looking for.
1037 * "found" is set if we have found the identifier being written to.
1039 struct pet_expr_writes_data
{
1044 /* Given an access expression, check if it writes to data->id.
1045 * If so, set data->found and abort the search.
1047 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1049 struct pet_expr_writes_data
*data
= user
;
1052 if (!expr
->acc
.write
)
1054 if (pet_expr_is_affine(expr
))
1057 write_id
= pet_expr_access_get_id(expr
);
1058 isl_id_free(write_id
);
1063 if (write_id
!= data
->id
)
1070 /* Does expression "expr" write to "id"?
1072 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1074 struct pet_expr_writes_data data
;
1078 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1085 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1086 * index expression and access relation of "expr"
1087 * to dimensions of "dst_type" at "dst_pos".
1089 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1090 enum isl_dim_type dst_type
, unsigned dst_pos
,
1091 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1093 expr
= pet_expr_cow(expr
);
1096 if (expr
->type
!= pet_expr_access
)
1097 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1098 "not an access pet_expr", return pet_expr_free(expr
));
1100 expr
->acc
.access
= isl_map_move_dims(expr
->acc
.access
,
1101 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1102 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1103 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1104 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1105 return pet_expr_free(expr
);
1110 /* Replace the index expression and access relation of "expr"
1111 * by their preimages under the function represented by "ma".
1113 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1114 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1116 expr
= pet_expr_cow(expr
);
1119 if (expr
->type
!= pet_expr_access
)
1120 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1121 "not an access pet_expr", goto error
);
1123 expr
->acc
.access
= isl_map_preimage_domain_multi_aff(expr
->acc
.access
,
1124 isl_multi_aff_copy(ma
));
1125 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1127 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1128 return pet_expr_free(expr
);
1132 isl_multi_aff_free(ma
);
1133 pet_expr_free(expr
);
1137 /* Replace the index expression and access relation of "expr"
1138 * by their preimages under the function represented by "mpa".
1140 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1141 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1143 expr
= pet_expr_cow(expr
);
1146 if (expr
->type
!= pet_expr_access
)
1147 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1148 "not an access pet_expr", goto error
);
1150 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
1151 expr
->acc
.access
, isl_multi_pw_aff_copy(mpa
));
1152 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1153 expr
->acc
.index
, mpa
);
1154 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1155 return pet_expr_free(expr
);
1159 isl_multi_pw_aff_free(mpa
);
1160 pet_expr_free(expr
);
1164 /* Return the access relation of access expression "expr".
1166 __isl_give isl_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
)
1170 if (expr
->type
!= pet_expr_access
)
1171 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1172 "not an access expression", return NULL
);
1174 return isl_map_copy(expr
->acc
.access
);
1177 /* Return the index expression of access expression "expr".
1179 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1180 __isl_keep pet_expr
*expr
)
1184 if (expr
->type
!= pet_expr_access
)
1185 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1186 "not an access expression", return NULL
);
1188 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1191 /* Align the parameters of expr->acc.index and expr->acc.access.
1193 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1195 expr
= pet_expr_cow(expr
);
1198 if (expr
->type
!= pet_expr_access
)
1199 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1200 "not an access expression", return pet_expr_free(expr
));
1202 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1203 isl_multi_pw_aff_get_space(expr
->acc
.index
));
1204 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1205 isl_map_get_space(expr
->acc
.access
));
1206 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1207 return pet_expr_free(expr
);
1212 /* Add extra conditions on the parameters to all access relations in "expr".
1214 * The conditions are not added to the index expression. Instead, they
1215 * are used to try and simplify the index expression.
1217 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1218 __isl_take isl_set
*cond
)
1222 expr
= pet_expr_cow(expr
);
1226 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1227 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1228 isl_set_copy(cond
));
1233 if (expr
->type
== pet_expr_access
) {
1234 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
1235 isl_set_copy(cond
));
1236 expr
->acc
.index
= isl_multi_pw_aff_gist_params(
1237 expr
->acc
.index
, isl_set_copy(cond
));
1238 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1246 return pet_expr_free(expr
);
1249 /* Modify the access relation and index expression
1250 * of the given access expression
1251 * based on the given iteration space transformation.
1252 * In particular, precompose the access relation and index expression
1253 * with the update function.
1255 * If the access has any arguments then the domain of the access relation
1256 * is a wrapped mapping from the iteration space to the space of
1257 * argument values. We only need to change the domain of this wrapped
1258 * mapping, so we extend the input transformation with an identity mapping
1259 * on the space of argument values.
1261 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1262 __isl_keep isl_multi_pw_aff
*update
)
1266 expr
= pet_expr_cow(expr
);
1269 if (expr
->type
!= pet_expr_access
)
1270 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1271 "not an access expression", return pet_expr_free(expr
));
1273 update
= isl_multi_pw_aff_copy(update
);
1275 space
= isl_map_get_space(expr
->acc
.access
);
1276 space
= isl_space_domain(space
);
1277 if (!isl_space_is_wrapping(space
))
1278 isl_space_free(space
);
1280 isl_multi_pw_aff
*id
;
1281 space
= isl_space_unwrap(space
);
1282 space
= isl_space_range(space
);
1283 space
= isl_space_map_from_set(space
);
1284 id
= isl_multi_pw_aff_identity(space
);
1285 update
= isl_multi_pw_aff_product(update
, id
);
1288 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
1290 isl_multi_pw_aff_copy(update
));
1291 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1292 expr
->acc
.index
, update
);
1293 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1294 return pet_expr_free(expr
);
1299 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1301 isl_multi_pw_aff
*update
= user
;
1303 return pet_expr_access_update_domain(expr
, update
);
1306 /* Modify all access relations in "expr" by precomposing them with
1307 * the given iteration space transformation.
1309 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1310 __isl_take isl_multi_pw_aff
*update
)
1312 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1313 isl_multi_pw_aff_free(update
);
1317 /* Given an expression with accesses that have a 0D anonymous domain,
1318 * replace those domains by "space".
1320 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1321 __isl_take isl_space
*space
)
1323 isl_multi_pw_aff
*mpa
;
1325 space
= isl_space_from_domain(space
);
1326 mpa
= isl_multi_pw_aff_zero(space
);
1327 return pet_expr_update_domain(expr
, mpa
);
1330 /* Add all parameters in "space" to the access relation and index expression
1333 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1335 isl_space
*space
= user
;
1337 expr
= pet_expr_cow(expr
);
1340 if (expr
->type
!= pet_expr_access
)
1341 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1342 "not an access expression", return pet_expr_free(expr
));
1344 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1345 isl_space_copy(space
));
1346 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1347 isl_space_copy(space
));
1348 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1349 return pet_expr_free(expr
);
1354 /* Add all parameters in "space" to all access relations and index expressions
1357 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1358 __isl_take isl_space
*space
)
1360 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1361 isl_space_free(space
);
1365 /* Insert an argument expression corresponding to "test" in front
1366 * of the list of arguments described by *n_arg and *args.
1368 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1369 __isl_keep isl_multi_pw_aff
*test
)
1372 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1375 return pet_expr_free(expr
);
1376 expr
= pet_expr_cow(expr
);
1381 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1383 return pet_expr_free(expr
);
1386 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1388 return pet_expr_free(expr
);
1389 for (i
= 0; i
< expr
->n_arg
; ++i
)
1390 ext
[1 + i
] = expr
->args
[i
];
1395 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1397 return pet_expr_free(expr
);
1402 /* Make the expression "expr" depend on the value of "test"
1403 * being equal to "satisfied".
1405 * If "test" is an affine expression, we simply add the conditions
1406 * on the expression having the value "satisfied" to all access relations
1407 * and index expressions.
1409 * Otherwise, we add a filter to "expr" (which is then assumed to be
1410 * an access expression) corresponding to "test" being equal to "satisfied".
1412 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1413 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1418 isl_pw_multi_aff
*pma
;
1420 expr
= pet_expr_cow(expr
);
1424 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
1428 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
1429 isl_multi_pw_aff_free(test
);
1431 cond
= isl_pw_aff_non_zero_set(pa
);
1433 cond
= isl_pw_aff_zero_set(pa
);
1434 return pet_expr_restrict(expr
, isl_set_params(cond
));
1437 ctx
= isl_multi_pw_aff_get_ctx(test
);
1438 if (expr
->type
!= pet_expr_access
)
1439 isl_die(ctx
, isl_error_invalid
,
1440 "can only filter access expressions", goto error
);
1442 space
= isl_space_domain(isl_map_get_space(expr
->acc
.access
));
1443 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1444 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
1446 expr
->acc
.access
= isl_map_preimage_domain_pw_multi_aff(
1448 isl_pw_multi_aff_copy(pma
));
1449 pma
= isl_pw_multi_aff_gist(pma
,
1450 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
1451 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
1452 expr
->acc
.index
, pma
);
1453 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1456 expr
= insert_access_arg(expr
, test
);
1458 isl_multi_pw_aff_free(test
);
1461 isl_multi_pw_aff_free(test
);
1462 return pet_expr_free(expr
);
1465 /* Add a reference identifier to access expression "expr".
1466 * "user" points to an integer that contains the sequence number
1467 * of the next reference.
1469 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
1476 expr
= pet_expr_cow(expr
);
1479 if (expr
->type
!= pet_expr_access
)
1480 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1481 "not an access expression", return pet_expr_free(expr
));
1483 ctx
= isl_map_get_ctx(expr
->acc
.access
);
1484 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
1485 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
1486 if (!expr
->acc
.ref_id
)
1487 return pet_expr_free(expr
);
1492 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
1494 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
1497 /* Reset the user pointer on all parameter and tuple ids in
1498 * the access relation and the index expressions
1499 * of the access expression "expr".
1501 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
1504 expr
= pet_expr_cow(expr
);
1507 if (expr
->type
!= pet_expr_access
)
1508 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1509 "not an access expression", return pet_expr_free(expr
));
1511 expr
->acc
.access
= isl_map_reset_user(expr
->acc
.access
);
1512 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
1513 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1514 return pet_expr_free(expr
);
1519 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
1521 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
1524 /* Data used in access_gist() callback.
1526 struct pet_access_gist_data
{
1528 isl_union_map
*value_bounds
;
1531 /* Given an expression "expr" of type pet_expr_access, compute
1532 * the gist of the associated access relation and index expression
1533 * with respect to data->domain and the bounds on the values of the arguments
1534 * of the expression.
1536 * The arguments of "expr" have been gisted right before "expr" itself
1537 * is gisted. The gisted arguments may have become equal where before
1538 * they may not have been (obviously) equal. We therefore take
1539 * the opportunity to remove duplicate arguments here.
1541 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
1543 struct pet_access_gist_data
*data
= user
;
1546 expr
= pet_expr_remove_duplicate_args(expr
);
1547 expr
= pet_expr_cow(expr
);
1550 if (expr
->type
!= pet_expr_access
)
1551 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1552 "not an access expression", return pet_expr_free(expr
));
1554 domain
= isl_set_copy(data
->domain
);
1555 if (expr
->n_arg
> 0)
1556 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
1557 data
->value_bounds
);
1559 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
,
1560 isl_set_copy(domain
));
1561 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
1562 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1563 return pet_expr_free(expr
);
1568 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
1569 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
1571 struct pet_access_gist_data data
= { context
, value_bounds
};
1573 return pet_expr_map_access(expr
, &access_gist
, &data
);
1576 /* Mark "expr" as a read dependening on "read".
1578 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
1582 return pet_expr_free(expr
);
1583 if (expr
->type
!= pet_expr_access
)
1584 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1585 "not an access expression", return pet_expr_free(expr
));
1586 if (expr
->acc
.read
== read
)
1588 expr
= pet_expr_cow(expr
);
1591 expr
->acc
.read
= read
;
1596 /* Mark "expr" as a write dependening on "write".
1598 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
1602 return pet_expr_free(expr
);
1603 if (expr
->type
!= pet_expr_access
)
1604 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1605 "not an access expression", return pet_expr_free(expr
));
1606 if (expr
->acc
.write
== write
)
1608 expr
= pet_expr_cow(expr
);
1611 expr
->acc
.write
= write
;
1616 /* Replace the access relation of "expr" by "access".
1618 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
1619 __isl_take isl_map
*access
)
1621 expr
= pet_expr_cow(expr
);
1622 if (!expr
|| !access
)
1624 if (expr
->type
!= pet_expr_access
)
1625 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1626 "not an access expression", goto error
);
1627 isl_map_free(expr
->acc
.access
);
1628 expr
->acc
.access
= access
;
1632 isl_map_free(access
);
1633 pet_expr_free(expr
);
1637 /* Replace the index expression of "expr" by "index".
1639 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
1640 __isl_take isl_multi_pw_aff
*index
)
1642 expr
= pet_expr_cow(expr
);
1643 if (!expr
|| !index
)
1645 if (expr
->type
!= pet_expr_access
)
1646 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1647 "not an access expression", goto error
);
1648 isl_multi_pw_aff_free(expr
->acc
.index
);
1649 expr
->acc
.index
= index
;
1653 isl_multi_pw_aff_free(index
);
1654 pet_expr_free(expr
);
1658 /* Return the reference identifier of access expression "expr".
1660 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
1664 if (expr
->type
!= pet_expr_access
)
1665 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1666 "not an access expression", return NULL
);
1668 return isl_id_copy(expr
->acc
.ref_id
);
1671 /* Replace the reference identifier of access expression "expr" by "ref_id".
1673 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
1674 __isl_take isl_id
*ref_id
)
1676 expr
= pet_expr_cow(expr
);
1677 if (!expr
|| !ref_id
)
1679 if (expr
->type
!= pet_expr_access
)
1680 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1681 "not an access expression", goto error
);
1682 isl_id_free(expr
->acc
.ref_id
);
1683 expr
->acc
.ref_id
= ref_id
;
1687 isl_id_free(ref_id
);
1688 pet_expr_free(expr
);
1692 /* Tag the access relation "access" with "id".
1693 * That is, insert the id as the range of a wrapped relation
1694 * in the domain of "access".
1696 * If "access" is of the form
1700 * then the result is of the form
1702 * [D[i] -> id[]] -> A[a]
1704 __isl_give isl_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
1705 __isl_take isl_map
*access
)
1711 if (expr
->type
!= pet_expr_access
)
1712 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1713 "not an access expression",
1714 return isl_map_free(access
));
1716 id
= isl_id_copy(expr
->acc
.ref_id
);
1717 space
= isl_space_range(isl_map_get_space(access
));
1718 space
= isl_space_from_range(space
);
1719 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
1720 add_tag
= isl_map_universe(space
);
1721 access
= isl_map_domain_product(access
, add_tag
);
1726 /* Return the relation mapping pairs of domain iterations and argument
1727 * values to the corresponding accessed data elements.
1729 __isl_give isl_map
*pet_expr_access_get_dependent_access(
1730 __isl_keep pet_expr
*expr
)
1734 if (expr
->type
!= pet_expr_access
)
1735 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1736 "not an access expression", return NULL
);
1738 return isl_map_copy(expr
->acc
.access
);
1741 /* Return the relation mapping domain iterations to all possibly
1742 * accessed data elements.
1743 * In particular, take the access relation and project out the values
1744 * of the arguments, if any.
1746 __isl_give isl_map
*pet_expr_access_get_may_access(__isl_keep pet_expr
*expr
)
1754 if (expr
->type
!= pet_expr_access
)
1755 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1756 "not an access expression", return NULL
);
1758 access
= pet_expr_access_get_dependent_access(expr
);
1759 if (expr
->n_arg
== 0)
1762 space
= isl_space_domain(isl_map_get_space(access
));
1763 map
= isl_map_universe(isl_space_unwrap(space
));
1764 map
= isl_map_domain_map(map
);
1765 access
= isl_map_apply_domain(access
, map
);
1770 /* Return a relation mapping domain iterations to definitely
1771 * accessed data elements, assuming the statement containing
1772 * the expression is executed.
1774 * If there are no arguments, then all elements are accessed.
1775 * Otherwise, we conservatively return an empty relation.
1777 __isl_give isl_map
*pet_expr_access_get_must_access(__isl_keep pet_expr
*expr
)
1783 if (expr
->type
!= pet_expr_access
)
1784 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1785 "not an access expression", return NULL
);
1787 if (expr
->n_arg
== 0)
1788 return pet_expr_access_get_dependent_access(expr
);
1790 space
= isl_map_get_space(expr
->acc
.access
);
1791 space
= isl_space_domain_factor_domain(space
);
1793 return isl_map_empty(space
);
1796 /* Return the relation mapping domain iterations to all possibly
1797 * accessed data elements, with its domain tagged with the reference
1800 __isl_give isl_map
*pet_expr_access_get_tagged_may_access(
1801 __isl_keep pet_expr
*expr
)
1808 access
= pet_expr_access_get_may_access(expr
);
1809 access
= pet_expr_tag_access(expr
, access
);
1814 /* Return the operation type of operation expression "expr".
1816 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
1820 if (expr
->type
!= pet_expr_op
)
1821 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1822 "not an operation expression", return pet_op_last
);
1827 /* Replace the operation type of operation expression "expr" by "type".
1829 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
1830 enum pet_op_type type
)
1833 return pet_expr_free(expr
);
1834 if (expr
->type
!= pet_expr_op
)
1835 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1836 "not an operation expression",
1837 return pet_expr_free(expr
));
1838 if (expr
->op
== type
)
1840 expr
= pet_expr_cow(expr
);
1848 /* Return the name of the function called by "expr".
1850 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
1854 if (expr
->type
!= pet_expr_call
)
1855 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1856 "not a call expression", return NULL
);
1860 /* Replace the name of the function called by "expr" by "name".
1862 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
1863 __isl_keep
const char *name
)
1865 expr
= pet_expr_cow(expr
);
1867 return pet_expr_free(expr
);
1868 if (expr
->type
!= pet_expr_call
)
1869 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1870 "not a call expression", return pet_expr_free(expr
));
1872 expr
->name
= strdup(name
);
1874 return pet_expr_free(expr
);
1878 /* Replace the type of the cast performed by "expr" by "name".
1880 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
1881 __isl_keep
const char *name
)
1883 expr
= pet_expr_cow(expr
);
1885 return pet_expr_free(expr
);
1886 if (expr
->type
!= pet_expr_cast
)
1887 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1888 "not a cast expression", return pet_expr_free(expr
));
1889 free(expr
->type_name
);
1890 expr
->type_name
= strdup(name
);
1891 if (!expr
->type_name
)
1892 return pet_expr_free(expr
);
1896 /* Return the value of the integer represented by "expr".
1898 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
1902 if (expr
->type
!= pet_expr_int
)
1903 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1904 "not an int expression", return NULL
);
1906 return isl_val_copy(expr
->i
);
1909 /* Replace the value of the integer represented by "expr" by "v".
1911 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
1912 __isl_take isl_val
*v
)
1914 expr
= pet_expr_cow(expr
);
1917 if (expr
->type
!= pet_expr_int
)
1918 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1919 "not an int expression", goto error
);
1920 isl_val_free(expr
->i
);
1926 pet_expr_free(expr
);
1930 /* Replace the value and string representation of the double
1931 * represented by "expr" by "d" and "s".
1933 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
1934 double d
, __isl_keep
const char *s
)
1936 expr
= pet_expr_cow(expr
);
1938 return pet_expr_free(expr
);
1939 if (expr
->type
!= pet_expr_double
)
1940 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1941 "not a double expression", return pet_expr_free(expr
));
1944 expr
->d
.s
= strdup(s
);
1946 return pet_expr_free(expr
);
1950 /* Return a string representation of the double expression "expr".
1952 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
1956 if (expr
->type
!= pet_expr_double
)
1957 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1958 "not a double expression", return NULL
);
1959 return strdup(expr
->d
.s
);
1962 /* Return a piecewise affine expression defined on the specified domain
1963 * that represents NaN.
1965 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
1967 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
1970 /* This function is called when we come across an access that is
1971 * nested in what is supposed to be an affine expression.
1972 * "pc" is the context in which the affine expression is created.
1973 * If nesting is allowed in "pc", we return an affine expression that is
1974 * equal to a new parameter corresponding to this nested access.
1975 * Otherwise, we return NaN.
1977 * Note that we currently don't allow nested accesses themselves
1978 * to contain any nested accesses, so we check if "expr" itself
1979 * involves any nested accesses (either explicitly as arguments
1980 * or implicitly through parameters) and return NaN if it does.
1982 * The new parameter is resolved in resolve_nested.
1984 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
1985 __isl_keep pet_context
*pc
)
1990 isl_local_space
*ls
;
1996 if (!pet_context_allow_nesting(pc
))
1997 return non_affine(pet_context_get_space(pc
));
1999 if (pet_expr_get_type(expr
) != pet_expr_access
)
2000 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2001 "not an access expression", return NULL
);
2003 if (expr
->n_arg
> 0)
2004 return non_affine(pet_context_get_space(pc
));
2006 space
= pet_expr_access_get_parameter_space(expr
);
2007 nested
= pet_nested_any_in_space(space
);
2008 isl_space_free(space
);
2010 return non_affine(pet_context_get_space(pc
));
2012 ctx
= pet_expr_get_ctx(expr
);
2013 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2014 space
= pet_context_get_space(pc
);
2015 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2017 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2018 ls
= isl_local_space_from_space(space
);
2019 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2021 return isl_pw_aff_from_aff(aff
);
2024 /* Extract an affine expression from the access pet_expr "expr".
2025 * "pc" is the context in which the affine expression is created.
2027 * If "expr" is actually an affine expression rather than
2028 * a real access, then we return that expression.
2029 * Otherwise, we require that "expr" is of an integral type.
2030 * If not, we return NaN.
2032 * If we are accessing a scalar (i.e., not an array and not a member)
2033 * and if that scalar can be treated as a parameter (because it is
2034 * not assigned a known or unknown value in the relevant part of the AST),
2035 * then we return an affine expression equal to that parameter.
2037 * If the variable has been assigned a known affine expression,
2038 * then we return that expression.
2040 * Otherwise, we return an expression that is equal to a parameter
2041 * representing "expr" (if "allow_nested" is set).
2043 static __isl_give isl_pw_aff
*extract_affine_from_access(
2044 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2049 isl_local_space
*ls
;
2052 if (pet_expr_is_affine(expr
)) {
2054 isl_multi_pw_aff
*mpa
;
2056 mpa
= pet_expr_access_get_index(expr
);
2057 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
2058 isl_multi_pw_aff_free(mpa
);
2062 if (pet_expr_get_type_size(expr
) == 0)
2063 return non_affine(pet_context_get_space(pc
));
2065 if (!pet_expr_is_scalar_access(expr
))
2066 return nested_access(expr
, pc
);
2068 id
= pet_expr_access_get_id(expr
);
2069 if (pet_context_is_assigned(pc
, id
)) {
2072 pa
= pet_context_get_value(pc
, id
);
2075 if (!isl_pw_aff_involves_nan(pa
))
2077 isl_pw_aff_free(pa
);
2078 return nested_access(expr
, pc
);
2081 space
= pet_context_get_space(pc
);
2083 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2087 pos
= isl_space_dim(space
, isl_dim_param
);
2088 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
2089 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
2092 ls
= isl_local_space_from_space(space
);
2093 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
2095 return isl_pw_aff_from_aff(aff
);
2098 /* Construct an affine expression from the integer constant "expr".
2099 * "pc" is the context in which the affine expression is created.
2101 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2102 __isl_keep pet_context
*pc
)
2104 isl_local_space
*ls
;
2110 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2111 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2113 return isl_pw_aff_from_aff(aff
);
2116 /* Extract an affine expression from an addition or subtraction operation.
2117 * Return NaN if we are unable to extract an affine expression.
2119 * "pc" is the context in which the affine expression is created.
2121 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2122 __isl_keep pet_context
*pc
)
2129 if (expr
->n_arg
!= 2)
2130 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2131 "expecting two arguments", return NULL
);
2133 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2134 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2136 switch (pet_expr_op_get_type(expr
)) {
2138 return isl_pw_aff_add(lhs
, rhs
);
2140 return isl_pw_aff_sub(lhs
, rhs
);
2142 isl_pw_aff_free(lhs
);
2143 isl_pw_aff_free(rhs
);
2144 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2145 "not an addition or subtraction operation",
2151 /* Extract an affine expression from an integer division or a modulo operation.
2152 * Return NaN if we are unable to extract an affine expression.
2154 * "pc" is the context in which the affine expression is created.
2156 * In particular, if "expr" is lhs/rhs, then return
2158 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2160 * If "expr" is lhs%rhs, then return
2162 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2164 * If the second argument (rhs) is not a (positive) integer constant,
2165 * then we fail to extract an affine expression.
2167 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2168 __isl_keep pet_context
*pc
)
2176 if (expr
->n_arg
!= 2)
2177 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2178 "expecting two arguments", return NULL
);
2180 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2182 is_cst
= isl_pw_aff_is_cst(rhs
);
2183 if (is_cst
< 0 || !is_cst
) {
2184 isl_pw_aff_free(rhs
);
2185 return non_affine(pet_context_get_space(pc
));
2188 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2190 switch (pet_expr_op_get_type(expr
)) {
2192 return isl_pw_aff_tdiv_q(lhs
, rhs
);
2194 return isl_pw_aff_tdiv_r(lhs
, rhs
);
2196 isl_pw_aff_free(lhs
);
2197 isl_pw_aff_free(rhs
);
2198 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2199 "not a div or mod operator", return NULL
);
2204 /* Extract an affine expression from a multiplication operation.
2205 * Return NaN if we are unable to extract an affine expression.
2206 * In particular, if neither of the arguments is a (piecewise) constant
2207 * then we return NaN.
2209 * "pc" is the context in which the affine expression is created.
2211 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2212 __isl_keep pet_context
*pc
)
2214 int lhs_cst
, rhs_cst
;
2220 if (expr
->n_arg
!= 2)
2221 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2222 "expecting two arguments", return NULL
);
2224 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2225 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2227 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2228 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2229 if (lhs_cst
< 0 || rhs_cst
< 0 || (!lhs_cst
&& !rhs_cst
)) {
2230 isl_pw_aff_free(lhs
);
2231 isl_pw_aff_free(rhs
);
2232 return non_affine(pet_context_get_space(pc
));
2235 return isl_pw_aff_mul(lhs
, rhs
);
2238 /* Extract an affine expression from a negation operation.
2239 * Return NaN if we are unable to extract an affine expression.
2241 * "pc" is the context in which the affine expression is created.
2243 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
2244 __isl_keep pet_context
*pc
)
2250 if (expr
->n_arg
!= 1)
2251 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2252 "expecting one argument", return NULL
);
2254 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
2255 return isl_pw_aff_neg(res
);
2258 /* Extract an affine expression from a conditional operation.
2259 * Return NaN if we are unable to extract an affine expression.
2261 * "pc" is the context in which the affine expression is created.
2263 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
2264 __isl_keep pet_context
*pc
)
2266 isl_pw_aff
*cond
, *lhs
, *rhs
;
2270 if (expr
->n_arg
!= 3)
2271 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2272 "expecting three arguments", return NULL
);
2274 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2275 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2276 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
2278 return isl_pw_aff_cond(cond
, lhs
, rhs
);
2285 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
, unsigned width
)
2290 ctx
= isl_pw_aff_get_ctx(pwaff
);
2291 mod
= isl_val_int_from_ui(ctx
, width
);
2292 mod
= isl_val_2exp(mod
);
2294 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
2299 /* Limit the domain of "pwaff" to those elements where the function
2302 * 2^{width-1} <= pwaff < 2^{width-1}
2304 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
2309 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
2310 isl_local_space
*ls
= isl_local_space_from_space(space
);
2315 ctx
= isl_pw_aff_get_ctx(pwaff
);
2316 v
= isl_val_int_from_ui(ctx
, width
- 1);
2317 v
= isl_val_2exp(v
);
2319 bound
= isl_aff_zero_on_domain(ls
);
2320 bound
= isl_aff_add_constant_val(bound
, v
);
2321 b
= isl_pw_aff_from_aff(bound
);
2323 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
2324 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2326 b
= isl_pw_aff_neg(b
);
2327 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
2328 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2333 /* Handle potential overflows on signed computations.
2335 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2336 * then we adjust the domain of "pa" to avoid overflows.
2338 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
2342 struct pet_options
*options
;
2347 ctx
= isl_pw_aff_get_ctx(pa
);
2348 options
= isl_ctx_peek_pet_options(ctx
);
2349 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
2350 pa
= avoid_overflow(pa
, width
);
2355 /* Extract an affine expression from some an operation.
2356 * Return NaN if we are unable to extract an affine expression.
2357 * If the result of a binary (non boolean) operation is unsigned,
2358 * then we wrap it based on the size of the type. If the result is signed,
2359 * then we ensure that no overflow occurs.
2361 * "pc" is the context in which the affine expression is created.
2363 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
2364 __isl_keep pet_context
*pc
)
2369 switch (pet_expr_op_get_type(expr
)) {
2372 res
= extract_affine_add_sub(expr
, pc
);
2376 res
= extract_affine_div_mod(expr
, pc
);
2379 res
= extract_affine_mul(expr
, pc
);
2382 return extract_affine_neg(expr
, pc
);
2384 return extract_affine_cond(expr
, pc
);
2394 return pet_expr_extract_affine_condition(expr
, pc
);
2396 return non_affine(pet_context_get_space(pc
));
2401 if (isl_pw_aff_involves_nan(res
)) {
2402 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
2403 isl_pw_aff_free(res
);
2404 return non_affine(space
);
2407 type_size
= pet_expr_get_type_size(expr
);
2409 res
= wrap(res
, type_size
);
2411 res
= signed_overflow(res
, -type_size
);
2416 /* Extract an affine expression from some special function calls.
2417 * Return NaN if we are unable to extract an affine expression.
2418 * In particular, we handle "min", "max", "ceild", "floord",
2419 * "intMod", "intFloor" and "intCeil".
2420 * In case of the latter five, the second argument needs to be
2421 * a (positive) integer constant.
2423 * "pc" is the context in which the affine expression is created.
2425 static __isl_give isl_pw_aff
*extract_affine_from_call(
2426 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2428 isl_pw_aff
*aff1
, *aff2
;
2432 n
= pet_expr_get_n_arg(expr
);
2433 name
= pet_expr_call_get_name(expr
);
2434 if (!(n
== 2 && !strcmp(name
, "min")) &&
2435 !(n
== 2 && !strcmp(name
, "max")) &&
2436 !(n
== 2 && !strcmp(name
, "intMod")) &&
2437 !(n
== 2 && !strcmp(name
, "intFloor")) &&
2438 !(n
== 2 && !strcmp(name
, "intCeil")) &&
2439 !(n
== 2 && !strcmp(name
, "floord")) &&
2440 !(n
== 2 && !strcmp(name
, "ceild")))
2441 return non_affine(pet_context_get_space(pc
));
2443 if (!strcmp(name
, "min") || !strcmp(name
, "max")) {
2444 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2445 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
2447 if (!strcmp(name
, "min"))
2448 aff1
= isl_pw_aff_min(aff1
, aff2
);
2450 aff1
= isl_pw_aff_max(aff1
, aff2
);
2451 } else if (!strcmp(name
, "intMod")) {
2454 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2455 return non_affine(pet_context_get_space(pc
));
2456 v
= pet_expr_int_get_val(expr
->args
[1]);
2457 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2458 aff1
= isl_pw_aff_mod_val(aff1
, v
);
2462 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2463 return non_affine(pet_context_get_space(pc
));
2464 v
= pet_expr_int_get_val(expr
->args
[1]);
2465 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2466 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
2467 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
2468 aff1
= isl_pw_aff_floor(aff1
);
2470 aff1
= isl_pw_aff_ceil(aff1
);
2476 /* Extract an affine expression from "expr", if possible.
2477 * Otherwise return NaN.
2479 * "pc" is the context in which the affine expression is created.
2481 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
2482 __isl_keep pet_context
*pc
)
2487 switch (pet_expr_get_type(expr
)) {
2488 case pet_expr_access
:
2489 return extract_affine_from_access(expr
, pc
);
2491 return extract_affine_from_int(expr
, pc
);
2493 return extract_affine_from_op(expr
, pc
);
2495 return extract_affine_from_call(expr
, pc
);
2497 case pet_expr_double
:
2498 case pet_expr_error
:
2499 return non_affine(pet_context_get_space(pc
));
2503 /* Extract an affine expressions representing the comparison "LHS op RHS"
2504 * Return NaN if we are unable to extract such an affine expression.
2506 * "pc" is the context in which the affine expression is created.
2508 * If the comparison is of the form
2512 * then the expression is constructed as the conjunction of
2517 * A similar optimization is performed for max(a,b) <= c.
2518 * We do this because that will lead to simpler representations
2519 * of the expression.
2520 * If isl is ever enhanced to explicitly deal with min and max expressions,
2521 * this optimization can be removed.
2523 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
2524 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
2525 __isl_keep pet_context
*pc
)
2527 isl_pw_aff
*lhs_pa
, *rhs_pa
;
2529 if (op
== pet_op_gt
)
2530 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
2531 if (op
== pet_op_ge
)
2532 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
2534 if (op
== pet_op_lt
|| op
== pet_op_le
) {
2535 if (pet_expr_is_min(rhs
)) {
2536 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2538 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2540 return pet_and(lhs_pa
, rhs_pa
);
2542 if (pet_expr_is_max(lhs
)) {
2543 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
2545 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
2547 return pet_and(lhs_pa
, rhs_pa
);
2551 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
2552 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
2554 return pet_comparison(op
, lhs_pa
, rhs_pa
);
2557 /* Extract an affine expressions from the comparison "expr".
2558 * Return NaN if we are unable to extract such an affine expression.
2560 * "pc" is the context in which the affine expression is created.
2562 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
2563 __isl_keep pet_context
*pc
)
2565 enum pet_op_type type
;
2569 if (expr
->n_arg
!= 2)
2570 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2571 "expecting two arguments", return NULL
);
2573 type
= pet_expr_op_get_type(expr
);
2574 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
2578 /* Extract an affine expression representing the boolean operation
2579 * expressed by "expr".
2580 * Return NaN if we are unable to extract an affine expression.
2582 * "pc" is the context in which the affine expression is created.
2584 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
2585 __isl_keep pet_context
*pc
)
2587 isl_pw_aff
*lhs
, *rhs
;
2593 n
= pet_expr_get_n_arg(expr
);
2594 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2596 return pet_not(lhs
);
2598 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
2599 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
2602 /* Extract the affine expression "expr != 0 ? 1 : 0".
2603 * Return NaN if we are unable to extract an affine expression.
2605 * "pc" is the context in which the affine expression is created.
2607 static __isl_give isl_pw_aff
*extract_implicit_condition(
2608 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2612 res
= pet_expr_extract_affine(expr
, pc
);
2613 return pet_to_bool(res
);
2616 /* Extract a boolean affine expression from "expr".
2617 * Return NaN if we are unable to extract an affine expression.
2619 * "pc" is the context in which the affine expression is created.
2621 * If "expr" is neither a comparison nor a boolean operation,
2622 * then we assume it is an affine expression and return the
2623 * boolean expression "expr != 0 ? 1 : 0".
2625 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
2626 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2631 if (pet_expr_is_comparison(expr
))
2632 return extract_comparison(expr
, pc
);
2633 if (pet_expr_is_boolean(expr
))
2634 return extract_boolean(expr
, pc
);
2636 return extract_implicit_condition(expr
, pc
);
2639 /* Check if "expr" is an assume expression and if its single argument
2640 * can be converted to an affine expression in the context of "pc".
2641 * If so, replace the argument by the affine expression.
2643 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
2644 __isl_keep pet_context
*pc
)
2647 isl_multi_pw_aff
*index
;
2651 if (!pet_expr_is_assume(expr
))
2653 if (expr
->n_arg
!= 1)
2654 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2655 "expecting one argument", return pet_expr_free(expr
));
2657 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2659 return pet_expr_free(expr
);
2660 if (isl_pw_aff_involves_nan(cond
)) {
2661 isl_pw_aff_free(cond
);
2665 index
= isl_multi_pw_aff_from_pw_aff(cond
);
2666 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
2671 /* Return the number of bits needed to represent the type of "expr".
2672 * See the description of the type_size field of pet_expr.
2674 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
2676 return expr
? expr
->type_size
: 0;
2679 /* Replace the number of bits needed to represent the type of "expr"
2681 * See the description of the type_size field of pet_expr.
2683 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
2686 expr
= pet_expr_cow(expr
);
2690 expr
->type_size
= type_size
;
2695 /* Extend an access expression "expr" with an additional index "index".
2696 * In particular, add "index" as an extra argument to "expr" and
2697 * adjust the index expression of "expr" to refer to this extra argument.
2698 * The caller is responsible for calling pet_expr_access_set_depth
2699 * to update the corresponding access relation.
2701 * Note that we only collect the individual index expressions as
2702 * arguments of "expr" here.
2703 * An attempt to integrate them into the index expression of "expr"
2704 * is performed in pet_expr_access_plug_in_args.
2706 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
2707 __isl_take pet_expr
*index
)
2711 isl_local_space
*ls
;
2714 expr
= pet_expr_cow(expr
);
2715 if (!expr
|| !index
)
2717 if (expr
->type
!= pet_expr_access
)
2718 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2719 "not an access pet_expr", goto error
);
2721 n
= pet_expr_get_n_arg(expr
);
2722 expr
= pet_expr_insert_arg(expr
, n
, index
);
2726 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
2727 ls
= isl_local_space_from_space(space
);
2728 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
2729 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
2730 if (!expr
->acc
.index
)
2731 return pet_expr_free(expr
);
2735 pet_expr_free(expr
);
2736 pet_expr_free(index
);
2740 /* Extend an access expression "expr" with an additional member acces to "id".
2741 * In particular, extend the index expression of "expr" to include
2742 * the additional member access.
2743 * The caller is responsible for calling pet_expr_access_set_depth
2744 * to update the corresponding access relation.
2746 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
2747 __isl_take isl_id
*id
)
2750 isl_multi_pw_aff
*field_access
;
2752 expr
= pet_expr_cow(expr
);
2755 if (expr
->type
!= pet_expr_access
)
2756 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2757 "not an access pet_expr", goto error
);
2759 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
2760 space
= isl_space_from_domain(space
);
2761 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2762 field_access
= isl_multi_pw_aff_zero(space
);
2763 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
2764 if (!expr
->acc
.index
)
2765 return pet_expr_free(expr
);
2769 pet_expr_free(expr
);
2774 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
2781 fprintf(stderr
, "%*s", indent
, "");
2783 switch (expr
->type
) {
2784 case pet_expr_double
:
2785 fprintf(stderr
, "%s\n", expr
->d
.s
);
2788 isl_val_dump(expr
->i
);
2790 case pet_expr_access
:
2791 if (expr
->acc
.ref_id
) {
2792 isl_id_dump(expr
->acc
.ref_id
);
2793 fprintf(stderr
, "%*s", indent
, "");
2795 isl_map_dump(expr
->acc
.access
);
2796 fprintf(stderr
, "%*s", indent
, "");
2797 isl_multi_pw_aff_dump(expr
->acc
.index
);
2798 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
2799 "", expr
->acc
.read
);
2800 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
2801 "", expr
->acc
.write
);
2802 for (i
= 0; i
< expr
->n_arg
; ++i
)
2803 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2806 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
2807 for (i
= 0; i
< expr
->n_arg
; ++i
)
2808 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2811 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
2812 for (i
= 0; i
< expr
->n_arg
; ++i
)
2813 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2816 fprintf(stderr
, "(%s)\n", expr
->type_name
);
2817 for (i
= 0; i
< expr
->n_arg
; ++i
)
2818 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2820 case pet_expr_error
:
2821 fprintf(stderr
, "ERROR\n");
2826 void pet_expr_dump(__isl_keep pet_expr
*expr
)
2828 pet_expr_dump_with_indent(expr
, 0);