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
42 #include "value_bounds.h"
44 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
46 static char *type_str
[] = {
47 [pet_expr_access
] = "access",
48 [pet_expr_call
] = "call",
49 [pet_expr_cast
] = "cast",
50 [pet_expr_double
] = "double",
51 [pet_expr_int
] = "int",
55 static char *op_str
[] = {
56 [pet_op_add_assign
] = "+=",
57 [pet_op_sub_assign
] = "-=",
58 [pet_op_mul_assign
] = "*=",
59 [pet_op_div_assign
] = "/=",
60 [pet_op_assign
] = "=",
75 [pet_op_post_inc
] = "++",
76 [pet_op_post_dec
] = "--",
77 [pet_op_pre_inc
] = "++",
78 [pet_op_pre_dec
] = "--",
79 [pet_op_address_of
] = "&",
88 [pet_op_assume
] = "assume",
89 [pet_op_kill
] = "kill"
92 const char *pet_op_str(enum pet_op_type op
)
97 int pet_op_is_inc_dec(enum pet_op_type op
)
99 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
100 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
103 const char *pet_type_str(enum pet_expr_type type
)
105 return type_str
[type
];
108 enum pet_op_type
pet_str_op(const char *str
)
112 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
113 if (!strcmp(op_str
[i
], str
))
119 enum pet_expr_type
pet_str_type(const char *str
)
123 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
124 if (!strcmp(type_str
[i
], str
))
130 /* Construct a pet_expr of the given type.
132 __isl_give pet_expr
*pet_expr_alloc(isl_ctx
*ctx
, enum pet_expr_type type
)
136 expr
= isl_calloc_type(ctx
, struct pet_expr
);
148 /* Construct an access pet_expr from an access relation and an index expression.
149 * By default, it is considered to be a read access.
151 __isl_give pet_expr
*pet_expr_from_access_and_index( __isl_take isl_map
*access
,
152 __isl_take isl_multi_pw_aff
*index
)
154 isl_ctx
*ctx
= isl_map_get_ctx(access
);
157 if (!index
|| !access
)
159 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
163 expr
->acc
.access
= access
;
164 expr
->acc
.index
= index
;
170 isl_map_free(access
);
171 isl_multi_pw_aff_free(index
);
175 /* Construct an access pet_expr from an index expression.
176 * By default, the access is considered to be a read access.
178 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
182 access
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
183 return pet_expr_from_access_and_index(access
, index
);
186 /* Extend the range of "access" with "n" dimensions, retaining
187 * the tuple identifier on this range.
189 * If "access" represents a member access, then extend the range
192 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
196 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
198 if (!isl_map_range_is_wrapping(access
)) {
199 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
203 domain
= isl_map_copy(access
);
204 domain
= isl_map_range_factor_domain(domain
);
205 access
= isl_map_range_factor_range(access
);
206 access
= extend_range(access
, n
);
207 access
= isl_map_range_product(domain
, access
);
210 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
215 /* Construct an access pet_expr from the number of bits needed to
216 * represent the type of the expression (may be zero if unknown or
217 * if the type is not an integer) an index expression and
218 * the depth of the accessed array.
219 * By default, the access is considered to be a read access.
221 * If the number of indices is smaller than the depth of the array,
222 * then we assume that all elements of the remaining dimensions
225 __isl_give pet_expr
*pet_expr_from_index_and_depth(int type_size
,
226 __isl_take isl_multi_pw_aff
*index
, int depth
)
232 access
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
235 dim
= isl_map_dim(access
, isl_dim_out
);
237 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
238 "number of indices greater than depth",
239 access
= isl_map_free(access
));
242 access
= extend_range(access
, depth
- dim
);
244 expr
= pet_expr_from_access_and_index(access
, index
);
248 expr
->type_size
= type_size
;
252 isl_multi_pw_aff_free(index
);
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 /* Does "expr" perform a min operation?
710 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
714 if (expr
->type
!= pet_expr_call
)
716 if (expr
->n_arg
!= 2)
718 if (strcmp(expr
->name
, "min") != 0)
723 /* Does "expr" perform a max operation?
725 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
729 if (expr
->type
!= pet_expr_call
)
731 if (expr
->n_arg
!= 2)
733 if (strcmp(expr
->name
, "max") != 0)
738 /* Does "expr" represent an access to an unnamed space, i.e.,
739 * does it represent an affine expression?
741 int pet_expr_is_affine(__isl_keep pet_expr
*expr
)
747 if (expr
->type
!= pet_expr_access
)
750 has_id
= isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
);
757 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
758 * not part of any struct?
760 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
764 if (expr
->type
!= pet_expr_access
)
766 if (isl_map_range_is_wrapping(expr
->acc
.access
))
769 return isl_map_dim(expr
->acc
.access
, isl_dim_out
) == 0;
772 /* Return 1 if the two pet_exprs are equivalent.
774 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
778 if (!expr1
|| !expr2
)
781 if (expr1
->type
!= expr2
->type
)
783 if (expr1
->n_arg
!= expr2
->n_arg
)
785 for (i
= 0; i
< expr1
->n_arg
; ++i
)
786 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
788 switch (expr1
->type
) {
791 case pet_expr_double
:
792 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
794 if (expr1
->d
.val
!= expr2
->d
.val
)
798 if (!isl_val_eq(expr1
->i
, expr2
->i
))
801 case pet_expr_access
:
802 if (expr1
->acc
.read
!= expr2
->acc
.read
)
804 if (expr1
->acc
.write
!= expr2
->acc
.write
)
806 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
808 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
810 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
812 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
814 if (!isl_multi_pw_aff_plain_is_equal(expr1
->acc
.index
,
819 if (expr1
->op
!= expr2
->op
)
823 if (strcmp(expr1
->name
, expr2
->name
))
827 if (strcmp(expr1
->type_name
, expr2
->type_name
))
835 /* Does the access expression "expr" read the accessed elements?
837 int pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
841 if (expr
->type
!= pet_expr_access
)
842 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
843 "not an access expression", return -1);
845 return expr
->acc
.read
;
848 /* Does the access expression "expr" write to the accessed elements?
850 int pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
854 if (expr
->type
!= pet_expr_access
)
855 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
856 "not an access expression", return -1);
858 return expr
->acc
.write
;
861 /* Return the identifier of the array accessed by "expr".
863 * If "expr" represents a member access, then return the identifier
864 * of the outer structure array.
866 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
870 if (expr
->type
!= pet_expr_access
)
871 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
872 "not an access expression", return NULL
);
874 if (isl_map_range_is_wrapping(expr
->acc
.access
)) {
878 space
= isl_map_get_space(expr
->acc
.access
);
879 space
= isl_space_range(space
);
880 while (space
&& isl_space_is_wrapping(space
))
881 space
= isl_space_domain(isl_space_unwrap(space
));
882 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
883 isl_space_free(space
);
888 return isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
891 /* Return the parameter space of "expr".
893 __isl_give isl_space
*pet_expr_access_get_parameter_space(
894 __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 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
905 space
= isl_space_params(space
);
910 /* Return the space of the data accessed by "expr".
912 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
918 if (expr
->type
!= pet_expr_access
)
919 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
920 "not an access expression", return NULL
);
922 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
923 space
= isl_space_range(space
);
928 /* Modify all expressions of type pet_expr_access in "expr"
929 * by calling "fn" on them.
931 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
932 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
937 n
= pet_expr_get_n_arg(expr
);
938 for (i
= 0; i
< n
; ++i
) {
939 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
940 arg
= pet_expr_map_access(arg
, fn
, user
);
941 expr
= pet_expr_set_arg(expr
, i
, arg
);
947 if (expr
->type
== pet_expr_access
)
948 expr
= fn(expr
, user
);
953 /* Call "fn" on each of the subexpressions of "expr" of type "type".
955 * Return -1 on error (where fn returning a negative value is treated as
957 * Otherwise return 0.
959 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
960 enum pet_expr_type type
,
961 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
968 for (i
= 0; i
< expr
->n_arg
; ++i
)
969 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
973 if (expr
->type
== type
)
974 return fn(expr
, user
);
979 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
981 * Return -1 on error (where fn returning a negative value is treated as
983 * Otherwise return 0.
985 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
986 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
988 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
991 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
993 * Return -1 on error (where fn returning a negative value is treated as
995 * Otherwise return 0.
997 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
998 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1000 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1003 /* Internal data structure for pet_expr_writes.
1004 * "id" is the identifier that we are looking for.
1005 * "found" is set if we have found the identifier being written to.
1007 struct pet_expr_writes_data
{
1012 /* Given an access expression, check if it writes to data->id.
1013 * If so, set data->found and abort the search.
1015 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1017 struct pet_expr_writes_data
*data
= user
;
1020 if (!expr
->acc
.write
)
1022 if (pet_expr_is_affine(expr
))
1025 write_id
= pet_expr_access_get_id(expr
);
1026 isl_id_free(write_id
);
1031 if (write_id
!= data
->id
)
1038 /* Does expression "expr" write to "id"?
1040 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1042 struct pet_expr_writes_data data
;
1046 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1053 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1054 * index expression and access relation of "expr"
1055 * to dimensions of "dst_type" at "dst_pos".
1057 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1058 enum isl_dim_type dst_type
, unsigned dst_pos
,
1059 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1061 expr
= pet_expr_cow(expr
);
1064 if (expr
->type
!= pet_expr_access
)
1065 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1066 "not an access pet_expr", return pet_expr_free(expr
));
1068 expr
->acc
.access
= isl_map_move_dims(expr
->acc
.access
,
1069 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1070 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1071 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1072 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1073 return pet_expr_free(expr
);
1078 /* Replace the index expression and access relation of "expr"
1079 * by their preimages under the function represented by "ma".
1081 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1082 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1084 expr
= pet_expr_cow(expr
);
1087 if (expr
->type
!= pet_expr_access
)
1088 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1089 "not an access pet_expr", goto error
);
1091 expr
->acc
.access
= isl_map_preimage_domain_multi_aff(expr
->acc
.access
,
1092 isl_multi_aff_copy(ma
));
1093 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1095 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1096 return pet_expr_free(expr
);
1100 isl_multi_aff_free(ma
);
1101 pet_expr_free(expr
);
1105 /* Return the access relation of access expression "expr".
1107 __isl_give isl_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
)
1111 if (expr
->type
!= pet_expr_access
)
1112 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1113 "not an access expression", return NULL
);
1115 return isl_map_copy(expr
->acc
.access
);
1118 /* Return the index expression of access expression "expr".
1120 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1121 __isl_keep pet_expr
*expr
)
1125 if (expr
->type
!= pet_expr_access
)
1126 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1127 "not an access expression", return NULL
);
1129 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1132 /* Align the parameters of expr->acc.index and expr->acc.access.
1134 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1136 expr
= pet_expr_cow(expr
);
1139 if (expr
->type
!= pet_expr_access
)
1140 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1141 "not an access expression", return pet_expr_free(expr
));
1143 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1144 isl_multi_pw_aff_get_space(expr
->acc
.index
));
1145 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1146 isl_map_get_space(expr
->acc
.access
));
1147 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1148 return pet_expr_free(expr
);
1153 /* Add extra conditions on the parameters to all access relations in "expr".
1155 * The conditions are not added to the index expression. Instead, they
1156 * are used to try and simplify the index expression.
1158 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1159 __isl_take isl_set
*cond
)
1163 expr
= pet_expr_cow(expr
);
1167 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1168 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1169 isl_set_copy(cond
));
1174 if (expr
->type
== pet_expr_access
) {
1175 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
1176 isl_set_copy(cond
));
1177 expr
->acc
.index
= isl_multi_pw_aff_gist_params(
1178 expr
->acc
.index
, isl_set_copy(cond
));
1179 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1187 return pet_expr_free(expr
);
1190 /* Modify the access relation and index expression
1191 * of the given access expression
1192 * based on the given iteration space transformation.
1193 * In particular, precompose the access relation and index expression
1194 * with the update function.
1196 * If the access has any arguments then the domain of the access relation
1197 * is a wrapped mapping from the iteration space to the space of
1198 * argument values. We only need to change the domain of this wrapped
1199 * mapping, so we extend the input transformation with an identity mapping
1200 * on the space of argument values.
1202 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1203 __isl_keep isl_multi_pw_aff
*update
)
1207 expr
= pet_expr_cow(expr
);
1210 if (expr
->type
!= pet_expr_access
)
1211 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1212 "not an access expression", return pet_expr_free(expr
));
1214 update
= isl_multi_pw_aff_copy(update
);
1216 space
= isl_map_get_space(expr
->acc
.access
);
1217 space
= isl_space_domain(space
);
1218 if (!isl_space_is_wrapping(space
))
1219 isl_space_free(space
);
1221 isl_multi_pw_aff
*id
;
1222 space
= isl_space_unwrap(space
);
1223 space
= isl_space_range(space
);
1224 space
= isl_space_map_from_set(space
);
1225 id
= isl_multi_pw_aff_identity(space
);
1226 update
= isl_multi_pw_aff_product(update
, id
);
1229 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
1231 isl_multi_pw_aff_copy(update
));
1232 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1233 expr
->acc
.index
, update
);
1234 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1235 return pet_expr_free(expr
);
1240 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1242 isl_multi_pw_aff
*update
= user
;
1244 return pet_expr_access_update_domain(expr
, update
);
1247 /* Modify all access relations in "expr" by precomposing them with
1248 * the given iteration space transformation.
1250 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1251 __isl_take isl_multi_pw_aff
*update
)
1253 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1254 isl_multi_pw_aff_free(update
);
1258 /* Add all parameters in "space" to the access relation and index expression
1261 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1263 isl_space
*space
= user
;
1265 expr
= pet_expr_cow(expr
);
1268 if (expr
->type
!= pet_expr_access
)
1269 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1270 "not an access expression", return pet_expr_free(expr
));
1272 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1273 isl_space_copy(space
));
1274 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1275 isl_space_copy(space
));
1276 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1277 return pet_expr_free(expr
);
1282 /* Add all parameters in "space" to all access relations and index expressions
1285 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1286 __isl_take isl_space
*space
)
1288 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1289 isl_space_free(space
);
1293 /* Insert an argument expression corresponding to "test" in front
1294 * of the list of arguments described by *n_arg and *args.
1296 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1297 __isl_keep isl_multi_pw_aff
*test
)
1300 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1303 return pet_expr_free(expr
);
1304 expr
= pet_expr_cow(expr
);
1309 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1311 return pet_expr_free(expr
);
1314 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1316 return pet_expr_free(expr
);
1317 for (i
= 0; i
< expr
->n_arg
; ++i
)
1318 ext
[1 + i
] = expr
->args
[i
];
1323 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1325 return pet_expr_free(expr
);
1330 /* Make the expression "expr" depend on the value of "test"
1331 * being equal to "satisfied".
1333 * If "test" is an affine expression, we simply add the conditions
1334 * on the expression having the value "satisfied" to all access relations
1335 * and index expressions.
1337 * Otherwise, we add a filter to "expr" (which is then assumed to be
1338 * an access expression) corresponding to "test" being equal to "satisfied".
1340 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1341 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1346 isl_pw_multi_aff
*pma
;
1348 expr
= pet_expr_cow(expr
);
1352 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
1356 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
1357 isl_multi_pw_aff_free(test
);
1359 cond
= isl_pw_aff_non_zero_set(pa
);
1361 cond
= isl_pw_aff_zero_set(pa
);
1362 return pet_expr_restrict(expr
, isl_set_params(cond
));
1365 ctx
= isl_multi_pw_aff_get_ctx(test
);
1366 if (expr
->type
!= pet_expr_access
)
1367 isl_die(ctx
, isl_error_invalid
,
1368 "can only filter access expressions", goto error
);
1370 space
= isl_space_domain(isl_map_get_space(expr
->acc
.access
));
1371 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1372 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
1374 expr
->acc
.access
= isl_map_preimage_domain_pw_multi_aff(
1376 isl_pw_multi_aff_copy(pma
));
1377 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
1378 expr
->acc
.index
, pma
);
1379 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1382 expr
= insert_access_arg(expr
, test
);
1384 isl_multi_pw_aff_free(test
);
1387 isl_multi_pw_aff_free(test
);
1388 return pet_expr_free(expr
);
1391 /* Check if the given index expression accesses a (0D) array that corresponds
1392 * to one of the parameters in "space". If so, replace the array access
1393 * by an access to the set of integers with as index (and value)
1396 static __isl_give isl_multi_pw_aff
*index_detect_parameter(
1397 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_space
*space
)
1399 isl_local_space
*ls
;
1400 isl_id
*array_id
= NULL
;
1404 if (isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
)) {
1405 array_id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
1406 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, array_id
);
1408 isl_space_free(space
);
1411 isl_id_free(array_id
);
1415 space
= isl_multi_pw_aff_get_domain_space(index
);
1416 isl_multi_pw_aff_free(index
);
1418 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, array_id
);
1420 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
1421 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, array_id
);
1424 isl_id_free(array_id
);
1426 ls
= isl_local_space_from_space(space
);
1427 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
1428 index
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1433 /* Check if the given access relation accesses a (0D) array that corresponds
1434 * to one of the parameters in "space". If so, replace the array access
1435 * by an access to the set of integers with as index (and value)
1438 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
1439 __isl_take isl_space
*space
)
1441 isl_id
*array_id
= NULL
;
1444 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
1445 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1446 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, array_id
);
1448 isl_space_free(space
);
1451 isl_id_free(array_id
);
1455 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
1457 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
1458 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
1461 isl_id_free(array_id
);
1463 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1464 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
1469 /* If "expr" accesses a (0D) array that corresponds to one of the parameters
1470 * in "space" then replace it by a value equal to the corresponding parameter.
1472 static __isl_give pet_expr
*detect_parameter_accesses(__isl_take pet_expr
*expr
,
1475 isl_space
*space
= user
;
1477 expr
= pet_expr_cow(expr
);
1480 if (expr
->type
!= pet_expr_access
)
1481 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1482 "not an access expression", return pet_expr_free(expr
));
1484 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
1485 isl_space_copy(space
));
1486 expr
->acc
.index
= index_detect_parameter(expr
->acc
.index
,
1487 isl_space_copy(space
));
1488 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1489 return pet_expr_free(expr
);
1494 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1495 * in "space" by a value equal to the corresponding parameter.
1497 __isl_give pet_expr
*pet_expr_detect_parameter_accesses(
1498 __isl_take pet_expr
*expr
, __isl_take isl_space
*space
)
1500 expr
= pet_expr_map_access(expr
, &detect_parameter_accesses
, space
);
1501 isl_space_free(space
);
1505 /* Add a reference identifier to access expression "expr".
1506 * "user" points to an integer that contains the sequence number
1507 * of the next reference.
1509 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
1516 expr
= pet_expr_cow(expr
);
1519 if (expr
->type
!= pet_expr_access
)
1520 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1521 "not an access expression", return pet_expr_free(expr
));
1523 ctx
= isl_map_get_ctx(expr
->acc
.access
);
1524 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
1525 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
1526 if (!expr
->acc
.ref_id
)
1527 return pet_expr_free(expr
);
1532 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
1534 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
1537 /* Reset the user pointer on all parameter and tuple ids in
1538 * the access relation and the index expressions
1539 * of the access expression "expr".
1541 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
1544 expr
= pet_expr_cow(expr
);
1547 if (expr
->type
!= pet_expr_access
)
1548 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1549 "not an access expression", return pet_expr_free(expr
));
1551 expr
->acc
.access
= isl_map_reset_user(expr
->acc
.access
);
1552 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
1553 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1554 return pet_expr_free(expr
);
1559 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
1561 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
1564 /* Data used in access_gist() callback.
1566 struct pet_access_gist_data
{
1568 isl_union_map
*value_bounds
;
1571 /* Given an expression "expr" of type pet_expr_access, compute
1572 * the gist of the associated access relation and index expression
1573 * with respect to data->domain and the bounds on the values of the arguments
1574 * of the expression.
1576 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
1578 struct pet_access_gist_data
*data
= user
;
1581 expr
= pet_expr_cow(expr
);
1584 if (expr
->type
!= pet_expr_access
)
1585 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1586 "not an access expression", return pet_expr_free(expr
));
1588 domain
= isl_set_copy(data
->domain
);
1589 if (expr
->n_arg
> 0)
1590 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
1591 data
->value_bounds
);
1593 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
,
1594 isl_set_copy(domain
));
1595 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
1596 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1597 return pet_expr_free(expr
);
1602 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
1603 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
1605 struct pet_access_gist_data data
= { context
, value_bounds
};
1607 return pet_expr_map_access(expr
, &access_gist
, &data
);
1610 /* Mark "expr" as a read dependening on "read".
1612 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
1616 return pet_expr_free(expr
);
1617 if (expr
->type
!= pet_expr_access
)
1618 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1619 "not an access expression", return pet_expr_free(expr
));
1620 if (expr
->acc
.read
== read
)
1622 expr
= pet_expr_cow(expr
);
1625 expr
->acc
.read
= read
;
1630 /* Mark "expr" as a write dependening on "write".
1632 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
1636 return pet_expr_free(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
));
1640 if (expr
->acc
.write
== write
)
1642 expr
= pet_expr_cow(expr
);
1645 expr
->acc
.write
= write
;
1650 /* Replace the access relation of "expr" by "access".
1652 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
1653 __isl_take isl_map
*access
)
1655 expr
= pet_expr_cow(expr
);
1656 if (!expr
|| !access
)
1658 if (expr
->type
!= pet_expr_access
)
1659 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1660 "not an access expression", goto error
);
1661 isl_map_free(expr
->acc
.access
);
1662 expr
->acc
.access
= access
;
1666 isl_map_free(access
);
1667 pet_expr_free(expr
);
1671 /* Replace the index expression of "expr" by "index".
1673 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
1674 __isl_take isl_multi_pw_aff
*index
)
1676 expr
= pet_expr_cow(expr
);
1677 if (!expr
|| !index
)
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_multi_pw_aff_free(expr
->acc
.index
);
1683 expr
->acc
.index
= index
;
1687 isl_multi_pw_aff_free(index
);
1688 pet_expr_free(expr
);
1692 /* Return the reference identifier of access expression "expr".
1694 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
1698 if (expr
->type
!= pet_expr_access
)
1699 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1700 "not an access expression", return NULL
);
1702 return isl_id_copy(expr
->acc
.ref_id
);
1705 /* Replace the reference identifier of access expression "expr" by "ref_id".
1707 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
1708 __isl_take isl_id
*ref_id
)
1710 expr
= pet_expr_cow(expr
);
1711 if (!expr
|| !ref_id
)
1713 if (expr
->type
!= pet_expr_access
)
1714 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1715 "not an access expression", goto error
);
1716 isl_id_free(expr
->acc
.ref_id
);
1717 expr
->acc
.ref_id
= ref_id
;
1721 isl_id_free(ref_id
);
1722 pet_expr_free(expr
);
1726 /* Tag the access relation "access" with "id".
1727 * That is, insert the id as the range of a wrapped relation
1728 * in the domain of "access".
1730 * If "access" is of the form
1734 * then the result is of the form
1736 * [D[i] -> id[]] -> A[a]
1738 __isl_give isl_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
1739 __isl_take isl_map
*access
)
1745 if (expr
->type
!= pet_expr_access
)
1746 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1747 "not an access expression",
1748 return isl_map_free(access
));
1750 id
= isl_id_copy(expr
->acc
.ref_id
);
1751 space
= isl_space_range(isl_map_get_space(access
));
1752 space
= isl_space_from_range(space
);
1753 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
1754 add_tag
= isl_map_universe(space
);
1755 access
= isl_map_domain_product(access
, add_tag
);
1760 /* Return the relation mapping pairs of domain iterations and argument
1761 * values to the corresponding accessed data elements.
1763 __isl_give isl_map
*pet_expr_access_get_dependent_access(
1764 __isl_keep pet_expr
*expr
)
1768 if (expr
->type
!= pet_expr_access
)
1769 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1770 "not an access expression", return NULL
);
1772 return isl_map_copy(expr
->acc
.access
);
1775 /* Return the relation mapping domain iterations to all possibly
1776 * accessed data elements.
1777 * In particular, take the access relation and project out the values
1778 * of the arguments, if any.
1780 __isl_give isl_map
*pet_expr_access_get_may_access(__isl_keep pet_expr
*expr
)
1788 if (expr
->type
!= pet_expr_access
)
1789 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1790 "not an access expression", return NULL
);
1792 access
= pet_expr_access_get_dependent_access(expr
);
1793 if (expr
->n_arg
== 0)
1796 space
= isl_space_domain(isl_map_get_space(access
));
1797 map
= isl_map_universe(isl_space_unwrap(space
));
1798 map
= isl_map_domain_map(map
);
1799 access
= isl_map_apply_domain(access
, map
);
1804 /* Return a relation mapping domain iterations to definitely
1805 * accessed data elements, assuming the statement containing
1806 * the expression is executed.
1808 * If there are no arguments, then all elements are accessed.
1809 * Otherwise, we conservatively return an empty relation.
1811 __isl_give isl_map
*pet_expr_access_get_must_access(__isl_keep pet_expr
*expr
)
1817 if (expr
->type
!= pet_expr_access
)
1818 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1819 "not an access expression", return NULL
);
1821 if (expr
->n_arg
== 0)
1822 return pet_expr_access_get_dependent_access(expr
);
1824 space
= isl_map_get_space(expr
->acc
.access
);
1825 space
= isl_space_domain_factor_domain(space
);
1827 return isl_map_empty(space
);
1830 /* Return the relation mapping domain iterations to all possibly
1831 * accessed data elements, with its domain tagged with the reference
1834 __isl_give isl_map
*pet_expr_access_get_tagged_may_access(
1835 __isl_keep pet_expr
*expr
)
1842 access
= pet_expr_access_get_may_access(expr
);
1843 access
= pet_expr_tag_access(expr
, access
);
1848 /* Return the operation type of operation expression "expr".
1850 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
1854 if (expr
->type
!= pet_expr_op
)
1855 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1856 "not an operation expression", return pet_op_last
);
1861 /* Replace the operation type of operation expression "expr" by "type".
1863 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
1864 enum pet_op_type type
)
1867 return pet_expr_free(expr
);
1868 if (expr
->type
!= pet_expr_op
)
1869 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1870 "not an operation expression",
1871 return pet_expr_free(expr
));
1872 if (expr
->op
== type
)
1874 expr
= pet_expr_cow(expr
);
1882 /* Return the name of the function called by "expr".
1884 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
1888 if (expr
->type
!= pet_expr_call
)
1889 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1890 "not a call expression", return NULL
);
1894 /* Replace the name of the function called by "expr" by "name".
1896 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
1897 __isl_keep
const char *name
)
1899 expr
= pet_expr_cow(expr
);
1901 return pet_expr_free(expr
);
1902 if (expr
->type
!= pet_expr_call
)
1903 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1904 "not a call expression", return pet_expr_free(expr
));
1906 expr
->name
= strdup(name
);
1908 return pet_expr_free(expr
);
1912 /* Replace the type of the cast performed by "expr" by "name".
1914 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
1915 __isl_keep
const char *name
)
1917 expr
= pet_expr_cow(expr
);
1919 return pet_expr_free(expr
);
1920 if (expr
->type
!= pet_expr_cast
)
1921 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1922 "not a cast expression", return pet_expr_free(expr
));
1923 free(expr
->type_name
);
1924 expr
->type_name
= strdup(name
);
1925 if (!expr
->type_name
)
1926 return pet_expr_free(expr
);
1930 /* Return the value of the integer represented by "expr".
1932 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
1936 if (expr
->type
!= pet_expr_int
)
1937 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1938 "not an int expression", return NULL
);
1940 return isl_val_copy(expr
->i
);
1943 /* Replace the value of the integer represented by "expr" by "v".
1945 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
1946 __isl_take isl_val
*v
)
1948 expr
= pet_expr_cow(expr
);
1951 if (expr
->type
!= pet_expr_int
)
1952 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1953 "not an int expression", goto error
);
1954 isl_val_free(expr
->i
);
1960 pet_expr_free(expr
);
1964 /* Replace the value and string representation of the double
1965 * represented by "expr" by "d" and "s".
1967 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
1968 double d
, __isl_keep
const char *s
)
1970 expr
= pet_expr_cow(expr
);
1972 return pet_expr_free(expr
);
1973 if (expr
->type
!= pet_expr_double
)
1974 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1975 "not a double expression", return pet_expr_free(expr
));
1978 expr
->d
.s
= strdup(s
);
1980 return pet_expr_free(expr
);
1984 /* Return a string representation of the double expression "expr".
1986 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
1990 if (expr
->type
!= pet_expr_double
)
1991 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1992 "not a double expression", return NULL
);
1993 return strdup(expr
->d
.s
);
1996 /* Return a piecewise affine expression defined on the specified domain
1997 * that represents NaN.
1999 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2001 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2004 /* This function is called when we come across an access that is
2005 * nested in what is supposed to be an affine expression.
2006 * "pc" is the context in which the affine expression is created.
2007 * If nesting is allowed in "pc", we return an affine expression that is
2008 * equal to a new parameter corresponding to this nested access.
2009 * Otherwise, we return NaN.
2011 * Note that we currently don't allow nested accesses themselves
2012 * to contain any nested accesses, so we check if "expr" itself
2013 * involves any nested accesses and return NaN if it does.
2015 * The new parameter is resolved in resolve_nested.
2017 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2018 __isl_keep pet_context
*pc
)
2023 isl_local_space
*ls
;
2029 if (!pet_context_allow_nesting(pc
))
2030 return non_affine(pet_context_get_space(pc
));
2032 if (pet_expr_get_type(expr
) != pet_expr_access
)
2033 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2034 "not an access expression", return NULL
);
2036 space
= pet_expr_access_get_parameter_space(expr
);
2037 nested
= pet_nested_any_in_space(space
);
2038 isl_space_free(space
);
2040 return non_affine(pet_context_get_space(pc
));
2042 ctx
= pet_expr_get_ctx(expr
);
2043 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2044 space
= pet_context_get_space(pc
);
2045 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2047 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2048 ls
= isl_local_space_from_space(space
);
2049 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2051 return isl_pw_aff_from_aff(aff
);
2054 /* Extract an affine expression from the access pet_expr "expr".
2055 * "pc" is the context in which the affine expression is created.
2057 * If "expr" is actually an affine expression rather than
2058 * a real access, then we return that expression.
2059 * Otherwise, we require that "expr" is of an integral type.
2060 * If not, we return NaN.
2062 * If we are accessing a scalar (i.e., not an array and not a member)
2063 * and if that scalar can be treated as a parameter (because it is
2064 * not assigned a known or unknown value in the relevant part of the AST),
2065 * then we return an affine expression equal to that parameter.
2067 * If the variable has been assigned a known affine expression,
2068 * then we return that expression.
2070 * Otherwise, we return an expression that is equal to a parameter
2071 * representing "expr" (if "allow_nested" is set).
2073 static __isl_give isl_pw_aff
*extract_affine_from_access(
2074 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2079 isl_local_space
*ls
;
2082 if (pet_expr_is_affine(expr
)) {
2084 isl_multi_pw_aff
*mpa
;
2086 mpa
= pet_expr_access_get_index(expr
);
2087 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
2088 isl_multi_pw_aff_free(mpa
);
2092 if (pet_expr_get_type_size(expr
) == 0)
2093 return non_affine(pet_context_get_space(pc
));
2095 if (!pet_expr_is_scalar_access(expr
))
2096 return nested_access(expr
, pc
);
2098 id
= pet_expr_access_get_id(expr
);
2099 if (pet_context_is_assigned(pc
, id
)) {
2102 pa
= pet_context_get_value(pc
, id
);
2105 if (!isl_pw_aff_involves_nan(pa
))
2107 isl_pw_aff_free(pa
);
2108 return nested_access(expr
, pc
);
2111 space
= pet_context_get_space(pc
);
2113 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2117 pos
= isl_space_dim(space
, isl_dim_param
);
2118 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
2119 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
2122 ls
= isl_local_space_from_space(space
);
2123 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
2125 return isl_pw_aff_from_aff(aff
);
2128 /* Construct an affine expression from the integer constant "expr".
2129 * "pc" is the context in which the affine expression is created.
2131 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2132 __isl_keep pet_context
*pc
)
2134 isl_local_space
*ls
;
2140 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2141 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2143 return isl_pw_aff_from_aff(aff
);
2146 /* Extract an affine expression from an addition or subtraction operation.
2147 * Return NaN if we are unable to extract an affine expression.
2149 * "pc" is the context in which the affine expression is created.
2151 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2152 __isl_keep pet_context
*pc
)
2159 if (expr
->n_arg
!= 2)
2160 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2161 "expecting two arguments", return NULL
);
2163 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2164 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2166 switch (pet_expr_op_get_type(expr
)) {
2168 return isl_pw_aff_add(lhs
, rhs
);
2170 return isl_pw_aff_sub(lhs
, rhs
);
2172 isl_pw_aff_free(lhs
);
2173 isl_pw_aff_free(rhs
);
2174 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2175 "not an addition or subtraction operation",
2181 /* Extract an affine expression from an integer division or a modulo operation.
2182 * Return NaN if we are unable to extract an affine expression.
2184 * "pc" is the context in which the affine expression is created.
2186 * In particular, if "expr" is lhs/rhs, then return
2188 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2190 * If "expr" is lhs%rhs, then return
2192 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2194 * If the second argument (rhs) is not a (positive) integer constant,
2195 * then we fail to extract an affine expression.
2197 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2198 __isl_keep pet_context
*pc
)
2206 if (expr
->n_arg
!= 2)
2207 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2208 "expecting two arguments", return NULL
);
2210 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2212 is_cst
= isl_pw_aff_is_cst(rhs
);
2213 if (is_cst
< 0 || !is_cst
) {
2214 isl_pw_aff_free(rhs
);
2215 return non_affine(pet_context_get_space(pc
));
2218 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2220 switch (pet_expr_op_get_type(expr
)) {
2222 return isl_pw_aff_tdiv_q(lhs
, rhs
);
2224 return isl_pw_aff_tdiv_r(lhs
, rhs
);
2226 isl_pw_aff_free(lhs
);
2227 isl_pw_aff_free(rhs
);
2228 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2229 "not a div or mod operator", return NULL
);
2234 /* Extract an affine expression from a multiplication operation.
2235 * Return NaN if we are unable to extract an affine expression.
2236 * In particular, if neither of the arguments is a (piecewise) constant
2237 * then we return NaN.
2239 * "pc" is the context in which the affine expression is created.
2241 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2242 __isl_keep pet_context
*pc
)
2244 int lhs_cst
, rhs_cst
;
2250 if (expr
->n_arg
!= 2)
2251 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2252 "expecting two arguments", return NULL
);
2254 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2255 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2257 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2258 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2259 if (lhs_cst
< 0 || rhs_cst
< 0 || (!lhs_cst
&& !rhs_cst
)) {
2260 isl_pw_aff_free(lhs
);
2261 isl_pw_aff_free(rhs
);
2262 return non_affine(pet_context_get_space(pc
));
2265 return isl_pw_aff_mul(lhs
, rhs
);
2268 /* Extract an affine expression from a negation operation.
2269 * Return NaN if we are unable to extract an affine expression.
2271 * "pc" is the context in which the affine expression is created.
2273 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
2274 __isl_keep pet_context
*pc
)
2280 if (expr
->n_arg
!= 1)
2281 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2282 "expecting one argument", return NULL
);
2284 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
2285 return isl_pw_aff_neg(res
);
2288 /* Extract an affine expression from a conditional operation.
2289 * Return NaN if we are unable to extract an affine expression.
2291 * "pc" is the context in which the affine expression is created.
2293 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
2294 __isl_keep pet_context
*pc
)
2296 isl_pw_aff
*cond
, *lhs
, *rhs
;
2300 if (expr
->n_arg
!= 3)
2301 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2302 "expecting three arguments", return NULL
);
2304 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2305 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2306 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
2308 return isl_pw_aff_cond(cond
, lhs
, rhs
);
2315 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
, unsigned width
)
2320 ctx
= isl_pw_aff_get_ctx(pwaff
);
2321 mod
= isl_val_int_from_ui(ctx
, width
);
2322 mod
= isl_val_2exp(mod
);
2324 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
2329 /* Limit the domain of "pwaff" to those elements where the function
2332 * 2^{width-1} <= pwaff < 2^{width-1}
2334 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
2339 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
2340 isl_local_space
*ls
= isl_local_space_from_space(space
);
2345 ctx
= isl_pw_aff_get_ctx(pwaff
);
2346 v
= isl_val_int_from_ui(ctx
, width
- 1);
2347 v
= isl_val_2exp(v
);
2349 bound
= isl_aff_zero_on_domain(ls
);
2350 bound
= isl_aff_add_constant_val(bound
, v
);
2351 b
= isl_pw_aff_from_aff(bound
);
2353 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
2354 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2356 b
= isl_pw_aff_neg(b
);
2357 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
2358 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2363 /* Handle potential overflows on signed computations.
2365 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2366 * then we adjust the domain of "pa" to avoid overflows.
2368 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
2372 struct pet_options
*options
;
2377 ctx
= isl_pw_aff_get_ctx(pa
);
2378 options
= isl_ctx_peek_pet_options(ctx
);
2379 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
2380 pa
= avoid_overflow(pa
, width
);
2385 /* Extract an affine expression from some an operation.
2386 * Return NaN if we are unable to extract an affine expression.
2387 * If the result of a binary (non boolean) operation is unsigned,
2388 * then we wrap it based on the size of the type. If the result is signed,
2389 * then we ensure that no overflow occurs.
2391 * "pc" is the context in which the affine expression is created.
2393 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
2394 __isl_keep pet_context
*pc
)
2399 switch (pet_expr_op_get_type(expr
)) {
2402 res
= extract_affine_add_sub(expr
, pc
);
2406 res
= extract_affine_div_mod(expr
, pc
);
2409 res
= extract_affine_mul(expr
, pc
);
2412 return extract_affine_neg(expr
, pc
);
2414 return extract_affine_cond(expr
, pc
);
2424 return pet_expr_extract_affine_condition(expr
, pc
);
2426 return non_affine(pet_context_get_space(pc
));
2431 if (isl_pw_aff_involves_nan(res
)) {
2432 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
2433 isl_pw_aff_free(res
);
2434 return non_affine(space
);
2437 type_size
= pet_expr_get_type_size(expr
);
2439 res
= wrap(res
, type_size
);
2441 res
= signed_overflow(res
, -type_size
);
2446 /* Extract an affine expression from some special function calls.
2447 * Return NaN if we are unable to extract an affine expression.
2448 * In particular, we handle "min", "max", "ceild", "floord",
2449 * "intMod", "intFloor" and "intCeil".
2450 * In case of the latter five, the second argument needs to be
2451 * a (positive) integer constant.
2453 * "pc" is the context in which the affine expression is created.
2455 static __isl_give isl_pw_aff
*extract_affine_from_call(
2456 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2458 isl_pw_aff
*aff1
, *aff2
;
2462 n
= pet_expr_get_n_arg(expr
);
2463 name
= pet_expr_call_get_name(expr
);
2464 if (!(n
== 2 && !strcmp(name
, "min")) &&
2465 !(n
== 2 && !strcmp(name
, "max")) &&
2466 !(n
== 2 && !strcmp(name
, "intMod")) &&
2467 !(n
== 2 && !strcmp(name
, "intFloor")) &&
2468 !(n
== 2 && !strcmp(name
, "intCeil")) &&
2469 !(n
== 2 && !strcmp(name
, "floord")) &&
2470 !(n
== 2 && !strcmp(name
, "ceild")))
2471 return non_affine(pet_context_get_space(pc
));
2473 if (!strcmp(name
, "min") || !strcmp(name
, "max")) {
2474 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2475 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
2477 if (!strcmp(name
, "min"))
2478 aff1
= isl_pw_aff_min(aff1
, aff2
);
2480 aff1
= isl_pw_aff_max(aff1
, aff2
);
2481 } else if (!strcmp(name
, "intMod")) {
2484 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2485 return non_affine(pet_context_get_space(pc
));
2486 v
= pet_expr_int_get_val(expr
->args
[1]);
2487 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2488 aff1
= isl_pw_aff_mod_val(aff1
, v
);
2492 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2493 return non_affine(pet_context_get_space(pc
));
2494 v
= pet_expr_int_get_val(expr
->args
[1]);
2495 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2496 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
2497 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
2498 aff1
= isl_pw_aff_floor(aff1
);
2500 aff1
= isl_pw_aff_ceil(aff1
);
2506 /* Extract an affine expression from "expr", if possible.
2507 * Otherwise return NaN.
2509 * "pc" is the context in which the affine expression is created.
2511 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
2512 __isl_keep pet_context
*pc
)
2517 switch (pet_expr_get_type(expr
)) {
2518 case pet_expr_access
:
2519 return extract_affine_from_access(expr
, pc
);
2521 return extract_affine_from_int(expr
, pc
);
2523 return extract_affine_from_op(expr
, pc
);
2525 return extract_affine_from_call(expr
, pc
);
2527 case pet_expr_double
:
2528 case pet_expr_error
:
2529 return non_affine(pet_context_get_space(pc
));
2533 /* Extract an affine expressions representing the comparison "LHS op RHS"
2534 * Return NaN if we are unable to extract such an affine expression.
2536 * "pc" is the context in which the affine expression is created.
2538 * If the comparison is of the form
2542 * then the expression is constructed as the conjunction of
2547 * A similar optimization is performed for max(a,b) <= c.
2548 * We do this because that will lead to simpler representations
2549 * of the expression.
2550 * If isl is ever enhanced to explicitly deal with min and max expressions,
2551 * this optimization can be removed.
2553 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
2554 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
2555 __isl_keep pet_context
*pc
)
2557 isl_pw_aff
*lhs_pa
, *rhs_pa
;
2559 if (op
== pet_op_gt
)
2560 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
2561 if (op
== pet_op_ge
)
2562 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
2564 if (op
== pet_op_lt
|| op
== pet_op_le
) {
2565 if (pet_expr_is_min(rhs
)) {
2566 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2568 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2570 return pet_and(lhs_pa
, rhs_pa
);
2572 if (pet_expr_is_max(lhs
)) {
2573 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
2575 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
2577 return pet_and(lhs_pa
, rhs_pa
);
2581 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
2582 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
2584 return pet_comparison(op
, lhs_pa
, rhs_pa
);
2587 /* Extract an affine expressions from the comparison "expr".
2588 * Return NaN if we are unable to extract such an affine expression.
2590 * "pc" is the context in which the affine expression is created.
2592 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
2593 __isl_keep pet_context
*pc
)
2595 enum pet_op_type type
;
2599 if (expr
->n_arg
!= 2)
2600 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2601 "expecting two arguments", return NULL
);
2603 type
= pet_expr_op_get_type(expr
);
2604 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
2608 /* Extract an affine expression representing the boolean operation
2609 * expressed by "expr".
2610 * Return NaN if we are unable to extract an affine expression.
2612 * "pc" is the context in which the affine expression is created.
2614 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
2615 __isl_keep pet_context
*pc
)
2617 isl_pw_aff
*lhs
, *rhs
;
2623 n
= pet_expr_get_n_arg(expr
);
2624 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2626 return pet_not(lhs
);
2628 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
2629 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
2632 /* Extract the affine expression "expr != 0 ? 1 : 0".
2633 * Return NaN if we are unable to extract an affine expression.
2635 * "pc" is the context in which the affine expression is created.
2637 static __isl_give isl_pw_aff
*extract_implicit_condition(
2638 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2642 res
= pet_expr_extract_affine(expr
, pc
);
2643 return pet_to_bool(res
);
2646 /* Extract a boolean affine expression from "expr".
2647 * Return NaN if we are unable to extract an affine expression.
2649 * "pc" is the context in which the affine expression is created.
2651 * If "expr" is neither a comparison nor a boolean operation,
2652 * then we assume it is an affine expression and return the
2653 * boolean expression "expr != 0 ? 1 : 0".
2655 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
2656 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2661 if (pet_expr_is_comparison(expr
))
2662 return extract_comparison(expr
, pc
);
2663 if (pet_expr_is_boolean(expr
))
2664 return extract_boolean(expr
, pc
);
2666 return extract_implicit_condition(expr
, pc
);
2669 /* Return the number of bits needed to represent the type of "expr".
2670 * See the description of the type_size field of pet_expr.
2672 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
2674 return expr
? expr
->type_size
: 0;
2677 /* Replace the number of bits needed to represent the type of "expr"
2679 * See the description of the type_size field of pet_expr.
2681 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
2684 expr
= pet_expr_cow(expr
);
2688 expr
->type_size
= type_size
;
2693 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
2700 fprintf(stderr
, "%*s", indent
, "");
2702 switch (expr
->type
) {
2703 case pet_expr_double
:
2704 fprintf(stderr
, "%s\n", expr
->d
.s
);
2707 isl_val_dump(expr
->i
);
2709 case pet_expr_access
:
2710 if (expr
->acc
.ref_id
) {
2711 isl_id_dump(expr
->acc
.ref_id
);
2712 fprintf(stderr
, "%*s", indent
, "");
2714 isl_map_dump(expr
->acc
.access
);
2715 fprintf(stderr
, "%*s", indent
, "");
2716 isl_multi_pw_aff_dump(expr
->acc
.index
);
2717 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
2718 "", expr
->acc
.read
);
2719 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
2720 "", expr
->acc
.write
);
2721 for (i
= 0; i
< expr
->n_arg
; ++i
)
2722 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2725 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
2726 for (i
= 0; i
< expr
->n_arg
; ++i
)
2727 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2730 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
2731 for (i
= 0; i
< expr
->n_arg
; ++i
)
2732 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2735 fprintf(stderr
, "(%s)\n", expr
->type_name
);
2736 for (i
= 0; i
< expr
->n_arg
; ++i
)
2737 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2739 case pet_expr_error
:
2740 fprintf(stderr
, "ERROR\n");
2745 void pet_expr_dump(__isl_keep pet_expr
*expr
)
2747 pet_expr_dump_with_indent(expr
, 0);