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 index expression.
151 * By default, the access is considered to be a read access.
152 * The initial depth is set from the index expression and
153 * may still be updated by the caller before the access relation
156 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
163 ctx
= isl_multi_pw_aff_get_ctx(index
);
164 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
171 expr
= pet_expr_access_set_index(expr
, index
);
175 isl_multi_pw_aff_free(index
);
179 /* Construct an access pet_expr from an access relation and an index expression.
180 * By default, it is considered to be a read access.
182 __isl_give pet_expr
*pet_expr_from_access_and_index(__isl_take isl_map
*access
,
183 __isl_take isl_multi_pw_aff
*index
)
188 expr
= pet_expr_from_index(index
);
189 depth
= isl_map_dim(access
, isl_dim_out
);
190 expr
= pet_expr_access_set_depth(expr
, depth
);
191 return pet_expr_access_set_access(expr
, access
);
194 /* Extend the range of "access" with "n" dimensions, retaining
195 * the tuple identifier on this range.
197 * If "access" represents a member access, then extend the range
200 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
204 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
206 if (!isl_map_range_is_wrapping(access
)) {
207 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
211 domain
= isl_map_copy(access
);
212 domain
= isl_map_range_factor_domain(domain
);
213 access
= isl_map_range_factor_range(access
);
214 access
= extend_range(access
, n
);
215 access
= isl_map_range_product(domain
, access
);
218 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
223 /* Does the access expression "expr" have an explicit access relation?
225 static int has_access_relation(__isl_keep pet_expr
*expr
)
230 if (expr
->acc
.access
)
236 /* Replace the depth of the access expr "expr" by "depth".
238 * To avoid inconsistencies between the depth and the access relation,
239 * we currently do not allow the depth to change once the access relation
240 * has been set or computed.
242 __isl_give pet_expr
*pet_expr_access_set_depth(__isl_take pet_expr
*expr
,
250 if (expr
->acc
.depth
== depth
)
252 if (has_access_relation(expr
))
253 isl_die(pet_expr_get_ctx(expr
), isl_error_unsupported
,
254 "depth cannot be changed after access relation "
255 "has been set or computed", return pet_expr_free(expr
));
257 expr
= pet_expr_cow(expr
);
260 expr
->acc
.depth
= depth
;
265 /* Construct a pet_expr that kills the elements specified by
266 * the index expression "index" and the access relation "access".
268 __isl_give pet_expr
*pet_expr_kill_from_access_and_index(
269 __isl_take isl_map
*access
, __isl_take isl_multi_pw_aff
*index
)
273 if (!access
|| !index
)
276 expr
= pet_expr_from_access_and_index(access
, index
);
277 expr
= pet_expr_access_set_read(expr
, 0);
278 return pet_expr_new_unary(pet_op_kill
, expr
);
280 isl_map_free(access
);
281 isl_multi_pw_aff_free(index
);
285 /* Construct a unary pet_expr that performs "op" on "arg".
287 __isl_give pet_expr
*pet_expr_new_unary(enum pet_op_type op
,
288 __isl_take pet_expr
*arg
)
295 ctx
= pet_expr_get_ctx(arg
);
296 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
297 expr
= pet_expr_set_n_arg(expr
, 1);
302 expr
->args
[pet_un_arg
] = arg
;
310 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
311 * where the result is represented using a type of "type_size" bits
312 * (may be zero if unknown or if the type is not an integer).
314 __isl_give pet_expr
*pet_expr_new_binary(int type_size
, enum pet_op_type op
,
315 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
322 ctx
= pet_expr_get_ctx(lhs
);
323 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
324 expr
= pet_expr_set_n_arg(expr
, 2);
329 expr
->type_size
= type_size
;
330 expr
->args
[pet_bin_lhs
] = lhs
;
331 expr
->args
[pet_bin_rhs
] = rhs
;
340 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
342 __isl_give pet_expr
*pet_expr_new_ternary(__isl_take pet_expr
*cond
,
343 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
348 if (!cond
|| !lhs
|| !rhs
)
350 ctx
= pet_expr_get_ctx(cond
);
351 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
352 expr
= pet_expr_set_n_arg(expr
, 3);
356 expr
->op
= pet_op_cond
;
357 expr
->args
[pet_ter_cond
] = cond
;
358 expr
->args
[pet_ter_true
] = lhs
;
359 expr
->args
[pet_ter_false
] = rhs
;
369 /* Construct a call pet_expr that calls function "name" with "n_arg"
370 * arguments. The caller is responsible for filling in the arguments.
372 __isl_give pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
377 expr
= pet_expr_alloc(ctx
, pet_expr_call
);
378 expr
= pet_expr_set_n_arg(expr
, n_arg
);
382 expr
->name
= strdup(name
);
384 return pet_expr_free(expr
);
389 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
391 __isl_give pet_expr
*pet_expr_new_cast(const char *type_name
,
392 __isl_take pet_expr
*arg
)
400 ctx
= pet_expr_get_ctx(arg
);
401 expr
= pet_expr_alloc(ctx
, pet_expr_cast
);
402 expr
= pet_expr_set_n_arg(expr
, 1);
406 expr
->type_name
= strdup(type_name
);
407 if (!expr
->type_name
)
419 /* Construct a pet_expr that represents the double "d".
421 __isl_give pet_expr
*pet_expr_new_double(isl_ctx
*ctx
,
422 double val
, const char *s
)
426 expr
= pet_expr_alloc(ctx
, pet_expr_double
);
431 expr
->d
.s
= strdup(s
);
433 return pet_expr_free(expr
);
438 /* Construct a pet_expr that represents the integer value "v".
440 __isl_give pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
448 ctx
= isl_val_get_ctx(v
);
449 expr
= pet_expr_alloc(ctx
, pet_expr_int
);
461 /* Return an independent duplicate of "expr".
463 * In case of an access expression, make sure the depth of the duplicate is set
464 * before the access relation (if any) is set and after the index expression
467 static __isl_give pet_expr
*pet_expr_dup(__isl_keep pet_expr
*expr
)
475 dup
= pet_expr_alloc(expr
->ctx
, expr
->type
);
476 dup
= pet_expr_set_type_size(dup
, expr
->type_size
);
477 dup
= pet_expr_set_n_arg(dup
, expr
->n_arg
);
478 for (i
= 0; i
< expr
->n_arg
; ++i
)
479 dup
= pet_expr_set_arg(dup
, i
, pet_expr_copy(expr
->args
[i
]));
481 switch (expr
->type
) {
482 case pet_expr_access
:
483 if (expr
->acc
.ref_id
)
484 dup
= pet_expr_access_set_ref_id(dup
,
485 isl_id_copy(expr
->acc
.ref_id
));
486 dup
= pet_expr_access_set_index(dup
,
487 isl_multi_pw_aff_copy(expr
->acc
.index
));
488 dup
= pet_expr_access_set_depth(dup
, expr
->acc
.depth
);
489 if (expr
->acc
.access
)
490 dup
= pet_expr_access_set_access(dup
,
491 isl_map_copy(expr
->acc
.access
));
492 dup
= pet_expr_access_set_read(dup
, expr
->acc
.read
);
493 dup
= pet_expr_access_set_write(dup
, expr
->acc
.write
);
496 dup
= pet_expr_call_set_name(dup
, expr
->name
);
499 dup
= pet_expr_cast_set_type_name(dup
, expr
->type_name
);
501 case pet_expr_double
:
502 dup
= pet_expr_double_set(dup
, expr
->d
.val
, expr
->d
.s
);
505 dup
= pet_expr_int_set_val(dup
, isl_val_copy(expr
->i
));
508 dup
= pet_expr_op_set_type(dup
, expr
->op
);
511 dup
= pet_expr_free(dup
);
518 __isl_give pet_expr
*pet_expr_cow(__isl_take pet_expr
*expr
)
526 return pet_expr_dup(expr
);
529 __isl_null pet_expr
*pet_expr_free(__isl_take pet_expr
*expr
)
538 for (i
= 0; i
< expr
->n_arg
; ++i
)
539 pet_expr_free(expr
->args
[i
]);
542 switch (expr
->type
) {
543 case pet_expr_access
:
544 isl_id_free(expr
->acc
.ref_id
);
545 isl_map_free(expr
->acc
.access
);
546 isl_multi_pw_aff_free(expr
->acc
.index
);
552 free(expr
->type_name
);
554 case pet_expr_double
:
558 isl_val_free(expr
->i
);
565 isl_ctx_deref(expr
->ctx
);
570 /* Return an additional reference to "expr".
572 __isl_give pet_expr
*pet_expr_copy(__isl_keep pet_expr
*expr
)
581 /* Return the isl_ctx in which "expr" was created.
583 isl_ctx
*pet_expr_get_ctx(__isl_keep pet_expr
*expr
)
585 return expr
? expr
->ctx
: NULL
;
588 /* Return the type of "expr".
590 enum pet_expr_type
pet_expr_get_type(__isl_keep pet_expr
*expr
)
593 return pet_expr_error
;
597 /* Return the number of arguments of "expr".
599 int pet_expr_get_n_arg(__isl_keep pet_expr
*expr
)
607 /* Set the number of arguments of "expr" to "n".
609 * If "expr" originally had more arguments, then remove the extra arguments.
610 * If "expr" originally had fewer arguments, then create space for
611 * the extra arguments ans initialize them to NULL.
613 __isl_give pet_expr
*pet_expr_set_n_arg(__isl_take pet_expr
*expr
, int n
)
620 if (expr
->n_arg
== n
)
622 expr
= pet_expr_cow(expr
);
626 if (n
< expr
->n_arg
) {
627 for (i
= n
; i
< expr
->n_arg
; ++i
)
628 pet_expr_free(expr
->args
[i
]);
633 args
= isl_realloc_array(expr
->ctx
, expr
->args
, pet_expr
*, n
);
635 return pet_expr_free(expr
);
637 for (i
= expr
->n_arg
; i
< n
; ++i
)
638 expr
->args
[i
] = NULL
;
644 /* Return the argument of "expr" at position "pos".
646 __isl_give pet_expr
*pet_expr_get_arg(__isl_keep pet_expr
*expr
, int pos
)
650 if (pos
< 0 || pos
>= expr
->n_arg
)
651 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
652 "position out of bounds", return NULL
);
654 return pet_expr_copy(expr
->args
[pos
]);
657 /* Replace the argument of "expr" at position "pos" by "arg".
659 __isl_give pet_expr
*pet_expr_set_arg(__isl_take pet_expr
*expr
, int pos
,
660 __isl_take pet_expr
*arg
)
664 if (pos
< 0 || pos
>= expr
->n_arg
)
665 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
666 "position out of bounds", goto error
);
667 if (expr
->args
[pos
] == arg
) {
672 expr
= pet_expr_cow(expr
);
676 pet_expr_free(expr
->args
[pos
]);
677 expr
->args
[pos
] = arg
;
686 /* Does "expr" perform a comparison operation?
688 int pet_expr_is_comparison(__isl_keep pet_expr
*expr
)
692 if (expr
->type
!= pet_expr_op
)
707 /* Does "expr" perform a boolean operation?
709 int pet_expr_is_boolean(__isl_keep pet_expr
*expr
)
713 if (expr
->type
!= pet_expr_op
)
725 /* Is "expr" an assume statement?
727 int pet_expr_is_assume(__isl_keep pet_expr
*expr
)
731 if (expr
->type
!= pet_expr_op
)
733 return expr
->op
== pet_op_assume
;
736 /* Does "expr" perform a min operation?
738 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
742 if (expr
->type
!= pet_expr_call
)
744 if (expr
->n_arg
!= 2)
746 if (strcmp(expr
->name
, "min") != 0)
751 /* Does "expr" perform a max operation?
753 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
757 if (expr
->type
!= pet_expr_call
)
759 if (expr
->n_arg
!= 2)
761 if (strcmp(expr
->name
, "max") != 0)
766 /* Does "expr" represent an access to an unnamed space, i.e.,
767 * does it represent an affine expression?
769 int pet_expr_is_affine(__isl_keep pet_expr
*expr
)
775 if (expr
->type
!= pet_expr_access
)
778 has_id
= isl_multi_pw_aff_has_tuple_id(expr
->acc
.index
, isl_dim_out
);
785 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
786 * not part of any struct?
788 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
792 if (expr
->type
!= pet_expr_access
)
794 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
))
797 return expr
->acc
.depth
== 0;
800 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
803 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
804 __isl_keep isl_multi_pw_aff
*mpa2
)
808 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
809 if (equal
< 0 || equal
)
811 mpa2
= isl_multi_pw_aff_copy(mpa2
);
812 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
813 isl_multi_pw_aff_get_space(mpa1
));
814 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
815 isl_multi_pw_aff_free(mpa2
);
820 /* Construct an access relation from the index expression and
821 * the array depth of the access expression "expr".
823 * If the number of indices is smaller than the depth of the array,
824 * then we assume that all elements of the remaining dimensions
827 static __isl_give isl_map
*construct_access_relation(__isl_keep pet_expr
*expr
)
836 access
= isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr
));
840 dim
= isl_map_dim(access
, isl_dim_out
);
841 if (dim
> expr
->acc
.depth
)
842 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
843 "number of indices greater than depth",
844 access
= isl_map_free(access
));
846 if (dim
!= expr
->acc
.depth
)
847 access
= extend_range(access
, expr
->acc
.depth
- dim
);
852 /* Ensure that "expr" has an explicit access relation.
854 * If "expr" does not already have an access relation, then create
855 * one based on the index expression and the array depth.
857 * We do not cow since adding an explicit access relation
858 * does not change the meaning of the expression.
860 static __isl_give pet_expr
*introduce_access_relation(
861 __isl_take pet_expr
*expr
)
868 if (has_access_relation(expr
))
871 access
= construct_access_relation(expr
);
873 return pet_expr_free(expr
);
875 expr
->acc
.access
= access
;
880 /* Return 1 if the two pet_exprs are equivalent.
882 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
886 if (!expr1
|| !expr2
)
889 if (expr1
->type
!= expr2
->type
)
891 if (expr1
->n_arg
!= expr2
->n_arg
)
893 for (i
= 0; i
< expr1
->n_arg
; ++i
)
894 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
896 switch (expr1
->type
) {
899 case pet_expr_double
:
900 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
902 if (expr1
->d
.val
!= expr2
->d
.val
)
906 if (!isl_val_eq(expr1
->i
, expr2
->i
))
909 case pet_expr_access
:
910 if (expr1
->acc
.read
!= expr2
->acc
.read
)
912 if (expr1
->acc
.write
!= expr2
->acc
.write
)
914 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
916 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
918 if (!multi_pw_aff_is_equal(expr1
->acc
.index
, expr2
->acc
.index
))
920 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
922 if (has_access_relation(expr1
) != has_access_relation(expr2
)) {
924 expr1
= pet_expr_copy(expr1
);
925 expr2
= pet_expr_copy(expr2
);
926 expr1
= introduce_access_relation(expr1
);
927 expr2
= introduce_access_relation(expr2
);
928 equal
= pet_expr_is_equal(expr1
, expr2
);
929 pet_expr_free(expr1
);
930 pet_expr_free(expr2
);
933 if (expr1
->acc
.access
&&
934 !isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
938 if (expr1
->op
!= expr2
->op
)
942 if (strcmp(expr1
->name
, expr2
->name
))
946 if (strcmp(expr1
->type_name
, expr2
->type_name
))
954 /* Does the access expression "expr" read the accessed elements?
956 int pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
960 if (expr
->type
!= pet_expr_access
)
961 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
962 "not an access expression", return -1);
964 return expr
->acc
.read
;
967 /* Does the access expression "expr" write to the accessed elements?
969 int pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
973 if (expr
->type
!= pet_expr_access
)
974 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
975 "not an access expression", return -1);
977 return expr
->acc
.write
;
980 /* Return the identifier of the array accessed by "expr".
982 * If "expr" represents a member access, then return the identifier
983 * of the outer structure array.
985 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
989 if (expr
->type
!= pet_expr_access
)
990 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
991 "not an access expression", return NULL
);
993 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
)) {
997 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
998 space
= isl_space_range(space
);
999 while (space
&& isl_space_is_wrapping(space
))
1000 space
= isl_space_domain(isl_space_unwrap(space
));
1001 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
1002 isl_space_free(space
);
1007 return isl_multi_pw_aff_get_tuple_id(expr
->acc
.index
, isl_dim_out
);
1010 /* Return the parameter space of "expr".
1012 __isl_give isl_space
*pet_expr_access_get_parameter_space(
1013 __isl_keep pet_expr
*expr
)
1019 if (expr
->type
!= pet_expr_access
)
1020 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1021 "not an access expression", return NULL
);
1023 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1024 space
= isl_space_params(space
);
1029 /* Return the domain space of "expr", without the arguments (if any).
1031 __isl_give isl_space
*pet_expr_access_get_domain_space(
1032 __isl_keep pet_expr
*expr
)
1038 if (expr
->type
!= pet_expr_access
)
1039 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1040 "not an access expression", return NULL
);
1042 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1043 space
= isl_space_domain(space
);
1044 if (isl_space_is_wrapping(space
))
1045 space
= isl_space_domain(isl_space_unwrap(space
));
1050 /* Return the space of the data accessed by "expr".
1052 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
1058 if (expr
->type
!= pet_expr_access
)
1059 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1060 "not an access expression", return NULL
);
1062 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1063 space
= isl_space_range(space
);
1068 /* Modify all expressions of type pet_expr_access in "expr"
1069 * by calling "fn" on them.
1071 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
1072 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1077 n
= pet_expr_get_n_arg(expr
);
1078 for (i
= 0; i
< n
; ++i
) {
1079 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1080 arg
= pet_expr_map_access(arg
, fn
, user
);
1081 expr
= pet_expr_set_arg(expr
, i
, arg
);
1087 if (expr
->type
== pet_expr_access
)
1088 expr
= fn(expr
, user
);
1093 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1095 * Return -1 on error (where fn returning a negative value is treated as
1097 * Otherwise return 0.
1099 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
1100 enum pet_expr_type type
,
1101 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1108 for (i
= 0; i
< expr
->n_arg
; ++i
)
1109 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1110 type
, fn
, user
) < 0)
1113 if (expr
->type
== type
)
1114 return fn(expr
, user
);
1119 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1121 * Return -1 on error (where fn returning a negative value is treated as
1123 * Otherwise return 0.
1125 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1126 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1128 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1131 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1133 * Return -1 on error (where fn returning a negative value is treated as
1135 * Otherwise return 0.
1137 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1138 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1140 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1143 /* Internal data structure for pet_expr_writes.
1144 * "id" is the identifier that we are looking for.
1145 * "found" is set if we have found the identifier being written to.
1147 struct pet_expr_writes_data
{
1152 /* Given an access expression, check if it writes to data->id.
1153 * If so, set data->found and abort the search.
1155 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1157 struct pet_expr_writes_data
*data
= user
;
1160 if (!expr
->acc
.write
)
1162 if (pet_expr_is_affine(expr
))
1165 write_id
= pet_expr_access_get_id(expr
);
1166 isl_id_free(write_id
);
1171 if (write_id
!= data
->id
)
1178 /* Does expression "expr" write to "id"?
1180 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1182 struct pet_expr_writes_data data
;
1186 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1193 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1194 * index expression and access relation of "expr" (if any)
1195 * to dimensions of "dst_type" at "dst_pos".
1197 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1198 enum isl_dim_type dst_type
, unsigned dst_pos
,
1199 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1201 expr
= pet_expr_cow(expr
);
1204 if (expr
->type
!= pet_expr_access
)
1205 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1206 "not an access pet_expr", return pet_expr_free(expr
));
1208 if (expr
->acc
.access
) {
1209 expr
->acc
.access
= isl_map_move_dims(expr
->acc
.access
,
1210 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1211 if (!expr
->acc
.access
)
1213 isl_multi_pw_aff_free(expr
->acc
.index
);
1215 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1216 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1217 if (!expr
->acc
.index
)
1218 return pet_expr_free(expr
);
1223 /* Replace the index expression and access relation (if any) of "expr"
1224 * by their preimages under the function represented by "ma".
1226 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1227 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1229 expr
= pet_expr_cow(expr
);
1232 if (expr
->type
!= pet_expr_access
)
1233 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1234 "not an access pet_expr", goto error
);
1236 if (expr
->acc
.access
) {
1237 expr
->acc
.access
= isl_map_preimage_domain_multi_aff(
1238 expr
->acc
.access
, isl_multi_aff_copy(ma
));
1239 if (!expr
->acc
.access
)
1241 isl_multi_pw_aff_free(expr
->acc
.index
);
1243 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1245 if (!expr
->acc
.index
)
1246 return pet_expr_free(expr
);
1250 isl_multi_aff_free(ma
);
1251 pet_expr_free(expr
);
1255 /* Replace the index expression and access relation (if any) of "expr"
1256 * by their preimages under the function represented by "mpa".
1258 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1259 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1261 expr
= pet_expr_cow(expr
);
1264 if (expr
->type
!= pet_expr_access
)
1265 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1266 "not an access pet_expr", goto error
);
1268 if (expr
->acc
.access
) {
1269 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
1270 expr
->acc
.access
, isl_multi_pw_aff_copy(mpa
));
1271 if (!expr
->acc
.access
)
1273 isl_multi_pw_aff_free(expr
->acc
.index
);
1275 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1276 expr
->acc
.index
, mpa
);
1277 if (!expr
->acc
.index
)
1278 return pet_expr_free(expr
);
1282 isl_multi_pw_aff_free(mpa
);
1283 pet_expr_free(expr
);
1287 /* Return the index expression of access expression "expr".
1289 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1290 __isl_keep pet_expr
*expr
)
1294 if (expr
->type
!= pet_expr_access
)
1295 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1296 "not an access expression", return NULL
);
1298 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1301 /* Align the parameters of expr->acc.index and expr->acc.access (if set).
1303 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1305 expr
= pet_expr_cow(expr
);
1308 if (expr
->type
!= pet_expr_access
)
1309 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1310 "not an access expression", return pet_expr_free(expr
));
1312 if (!has_access_relation(expr
))
1315 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1316 isl_multi_pw_aff_get_space(expr
->acc
.index
));
1317 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1318 isl_map_get_space(expr
->acc
.access
));
1319 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1320 return pet_expr_free(expr
);
1325 /* Are "expr1" and "expr2" both array accesses such that
1326 * the access relation of "expr1" is a subset of that of "expr2"?
1327 * Only take into account the first "n_arg" arguments.
1329 * This function is tailored for use by mark_self_dependences in nest.c.
1330 * In particular, the input expressions may have more than "n_arg"
1331 * elements in their arguments arrays, while only the first "n_arg"
1332 * elements are referenced from the access relations.
1334 int pet_expr_is_sub_access(__isl_keep pet_expr
*expr1
,
1335 __isl_keep pet_expr
*expr2
, int n_arg
)
1341 if (!expr1
|| !expr2
)
1343 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1345 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1347 if (pet_expr_is_affine(expr1
))
1349 if (pet_expr_is_affine(expr2
))
1351 n1
= pet_expr_get_n_arg(expr1
);
1354 n2
= pet_expr_get_n_arg(expr2
);
1359 for (i
= 0; i
< n1
; ++i
) {
1361 equal
= pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]);
1362 if (equal
< 0 || !equal
)
1365 id1
= pet_expr_access_get_id(expr1
);
1366 id2
= pet_expr_access_get_id(expr2
);
1374 expr1
= pet_expr_copy(expr1
);
1375 expr2
= pet_expr_copy(expr2
);
1376 expr1
= introduce_access_relation(expr1
);
1377 expr2
= introduce_access_relation(expr2
);
1378 if (!expr1
|| !expr2
)
1381 is_subset
= isl_map_is_subset(expr1
->acc
.access
, expr2
->acc
.access
);
1383 pet_expr_free(expr1
);
1384 pet_expr_free(expr2
);
1388 pet_expr_free(expr1
);
1389 pet_expr_free(expr2
);
1393 /* Given a set in the iteration space "domain", extend it to live in the space
1394 * of the domain of access relations.
1396 * That, is the number of arguments "n" is 0, then simply return domain.
1397 * Otherwise, return [domain -> [a_1,...,a_n]].
1399 static __isl_give isl_set
*add_arguments(__isl_take isl_set
*domain
, int n
)
1406 map
= isl_map_from_domain(domain
);
1407 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
1408 return isl_map_wrap(map
);
1411 /* Add extra conditions to the domains of all access relations in "expr",
1412 * introducing access relations if they are not already present.
1414 * The conditions are not added to the index expression. Instead, they
1415 * are used to try and simplify the index expression.
1417 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1418 __isl_take isl_set
*cond
)
1422 expr
= pet_expr_cow(expr
);
1426 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1427 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1428 isl_set_copy(cond
));
1433 if (expr
->type
!= pet_expr_access
) {
1438 expr
= introduce_access_relation(expr
);
1442 cond
= add_arguments(cond
, expr
->n_arg
);
1443 expr
->acc
.access
= isl_map_intersect_domain(expr
->acc
.access
,
1444 isl_set_copy(cond
));
1445 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, cond
);
1446 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1447 return pet_expr_free(expr
);
1452 return pet_expr_free(expr
);
1455 /* Modify the access relation (if any) and index expression
1456 * of the given access expression
1457 * based on the given iteration space transformation.
1458 * In particular, precompose the access relation and index expression
1459 * with the update function.
1461 * If the access has any arguments then the domain of the access relation
1462 * is a wrapped mapping from the iteration space to the space of
1463 * argument values. We only need to change the domain of this wrapped
1464 * mapping, so we extend the input transformation with an identity mapping
1465 * on the space of argument values.
1467 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1468 __isl_keep isl_multi_pw_aff
*update
)
1470 expr
= pet_expr_cow(expr
);
1473 if (expr
->type
!= pet_expr_access
)
1474 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1475 "not an access expression", return pet_expr_free(expr
));
1477 update
= isl_multi_pw_aff_copy(update
);
1479 if (expr
->n_arg
> 0) {
1481 isl_multi_pw_aff
*id
;
1483 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1484 space
= isl_space_domain(space
);
1485 space
= isl_space_unwrap(space
);
1486 space
= isl_space_range(space
);
1487 space
= isl_space_map_from_set(space
);
1488 id
= isl_multi_pw_aff_identity(space
);
1489 update
= isl_multi_pw_aff_product(update
, id
);
1492 if (expr
->acc
.access
) {
1493 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
1495 isl_multi_pw_aff_copy(update
));
1496 if (!expr
->acc
.access
)
1498 isl_multi_pw_aff_free(expr
->acc
.index
);
1500 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1501 expr
->acc
.index
, update
);
1502 if (!expr
->acc
.index
)
1503 return pet_expr_free(expr
);
1508 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1510 isl_multi_pw_aff
*update
= user
;
1512 return pet_expr_access_update_domain(expr
, update
);
1515 /* Modify all access relations in "expr" by precomposing them with
1516 * the given iteration space transformation.
1518 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1519 __isl_take isl_multi_pw_aff
*update
)
1521 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1522 isl_multi_pw_aff_free(update
);
1526 /* Given an expression with accesses that have a 0D anonymous domain,
1527 * replace those domains by "space".
1529 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1530 __isl_take isl_space
*space
)
1532 isl_multi_pw_aff
*mpa
;
1534 space
= isl_space_from_domain(space
);
1535 mpa
= isl_multi_pw_aff_zero(space
);
1536 return pet_expr_update_domain(expr
, mpa
);
1539 /* Add all parameters in "space" to the access relation (if any)
1540 * and index expression of "expr".
1542 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1544 isl_space
*space
= user
;
1546 expr
= pet_expr_cow(expr
);
1549 if (expr
->type
!= pet_expr_access
)
1550 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1551 "not an access expression", return pet_expr_free(expr
));
1553 if (expr
->acc
.access
) {
1554 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1555 isl_space_copy(space
));
1556 if (!expr
->acc
.access
)
1558 isl_multi_pw_aff_free(expr
->acc
.index
);
1560 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1561 isl_space_copy(space
));
1562 if (!expr
->acc
.index
)
1563 return pet_expr_free(expr
);
1568 /* Add all parameters in "space" to all access relations and index expressions
1571 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1572 __isl_take isl_space
*space
)
1574 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1575 isl_space_free(space
);
1579 /* Insert an argument expression corresponding to "test" in front
1580 * of the list of arguments described by *n_arg and *args.
1582 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1583 __isl_keep isl_multi_pw_aff
*test
)
1586 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1589 return pet_expr_free(expr
);
1590 expr
= pet_expr_cow(expr
);
1595 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1597 return pet_expr_free(expr
);
1600 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1602 return pet_expr_free(expr
);
1603 for (i
= 0; i
< expr
->n_arg
; ++i
)
1604 ext
[1 + i
] = expr
->args
[i
];
1609 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1611 return pet_expr_free(expr
);
1616 /* Make the expression "expr" depend on the value of "test"
1617 * being equal to "satisfied".
1619 * If "test" is an affine expression, we simply add the conditions
1620 * on the expression having the value "satisfied" to all access relations
1621 * (introducing access relations if they are missing) and index expressions.
1623 * Otherwise, we add a filter to "expr" (which is then assumed to be
1624 * an access expression) corresponding to "test" being equal to "satisfied".
1626 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1627 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1632 isl_pw_multi_aff
*pma
;
1634 expr
= pet_expr_cow(expr
);
1638 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
1642 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
1643 isl_multi_pw_aff_free(test
);
1645 cond
= isl_pw_aff_non_zero_set(pa
);
1647 cond
= isl_pw_aff_zero_set(pa
);
1648 return pet_expr_restrict(expr
, cond
);
1651 ctx
= isl_multi_pw_aff_get_ctx(test
);
1652 if (expr
->type
!= pet_expr_access
)
1653 isl_die(ctx
, isl_error_invalid
,
1654 "can only filter access expressions", goto error
);
1656 expr
= introduce_access_relation(expr
);
1660 space
= isl_space_domain(isl_multi_pw_aff_get_space(expr
->acc
.index
));
1661 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1662 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
1664 expr
->acc
.access
= isl_map_preimage_domain_pw_multi_aff(
1666 isl_pw_multi_aff_copy(pma
));
1667 pma
= isl_pw_multi_aff_gist(pma
,
1668 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
1669 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
1670 expr
->acc
.index
, pma
);
1671 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1674 expr
= insert_access_arg(expr
, test
);
1676 isl_multi_pw_aff_free(test
);
1679 isl_multi_pw_aff_free(test
);
1680 return pet_expr_free(expr
);
1683 /* Add a reference identifier to access expression "expr".
1684 * "user" points to an integer that contains the sequence number
1685 * of the next reference.
1687 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
1694 expr
= pet_expr_cow(expr
);
1697 if (expr
->type
!= pet_expr_access
)
1698 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1699 "not an access expression", return pet_expr_free(expr
));
1701 ctx
= pet_expr_get_ctx(expr
);
1702 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
1703 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
1704 if (!expr
->acc
.ref_id
)
1705 return pet_expr_free(expr
);
1710 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
1712 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
1715 /* Reset the user pointer on all parameter and tuple ids in
1716 * the access relation (if any) and the index expression
1717 * of the access expression "expr".
1719 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
1722 expr
= pet_expr_cow(expr
);
1725 if (expr
->type
!= pet_expr_access
)
1726 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1727 "not an access expression", return pet_expr_free(expr
));
1729 if (expr
->acc
.access
) {
1730 expr
->acc
.access
= isl_map_reset_user(expr
->acc
.access
);
1731 if (!expr
->acc
.access
)
1733 isl_multi_pw_aff_free(expr
->acc
.index
);
1735 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
1736 if (!expr
->acc
.index
)
1737 return pet_expr_free(expr
);
1742 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
1744 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
1747 /* Data used in access_gist() callback.
1749 struct pet_access_gist_data
{
1751 isl_union_map
*value_bounds
;
1754 /* Given an expression "expr" of type pet_expr_access, compute
1755 * the gist of the associated access relation (if any) and index expression
1756 * with respect to data->domain and the bounds on the values of the arguments
1757 * of the expression.
1759 * The arguments of "expr" have been gisted right before "expr" itself
1760 * is gisted. The gisted arguments may have become equal where before
1761 * they may not have been (obviously) equal. We therefore take
1762 * the opportunity to remove duplicate arguments here.
1764 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
1766 struct pet_access_gist_data
*data
= user
;
1769 expr
= pet_expr_remove_duplicate_args(expr
);
1770 expr
= pet_expr_cow(expr
);
1773 if (expr
->type
!= pet_expr_access
)
1774 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1775 "not an access expression", return pet_expr_free(expr
));
1777 domain
= isl_set_copy(data
->domain
);
1778 if (expr
->n_arg
> 0)
1779 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
1780 data
->value_bounds
);
1782 if (expr
->acc
.access
) {
1783 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
,
1784 isl_set_copy(domain
));
1785 if (!expr
->acc
.access
)
1787 isl_multi_pw_aff_free(expr
->acc
.index
);
1789 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
1790 if (!expr
->acc
.index
)
1791 return pet_expr_free(expr
);
1796 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
1797 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
1799 struct pet_access_gist_data data
= { context
, value_bounds
};
1801 return pet_expr_map_access(expr
, &access_gist
, &data
);
1804 /* Mark "expr" as a read dependening on "read".
1806 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
1810 return pet_expr_free(expr
);
1811 if (expr
->type
!= pet_expr_access
)
1812 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1813 "not an access expression", return pet_expr_free(expr
));
1814 if (expr
->acc
.read
== read
)
1816 expr
= pet_expr_cow(expr
);
1819 expr
->acc
.read
= read
;
1824 /* Mark "expr" as a write dependening on "write".
1826 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
1830 return pet_expr_free(expr
);
1831 if (expr
->type
!= pet_expr_access
)
1832 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1833 "not an access expression", return pet_expr_free(expr
));
1834 if (expr
->acc
.write
== write
)
1836 expr
= pet_expr_cow(expr
);
1839 expr
->acc
.write
= write
;
1844 /* Replace the access relation of "expr" by "access".
1846 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
1847 __isl_take isl_map
*access
)
1849 expr
= pet_expr_cow(expr
);
1850 if (!expr
|| !access
)
1852 if (expr
->type
!= pet_expr_access
)
1853 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1854 "not an access expression", goto error
);
1855 isl_map_free(expr
->acc
.access
);
1856 expr
->acc
.access
= access
;
1860 isl_map_free(access
);
1861 pet_expr_free(expr
);
1865 /* Replace the index expression of "expr" by "index" and
1866 * set the array depth accordingly.
1868 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
1869 __isl_take isl_multi_pw_aff
*index
)
1871 expr
= pet_expr_cow(expr
);
1872 if (!expr
|| !index
)
1874 if (expr
->type
!= pet_expr_access
)
1875 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1876 "not an access expression", goto error
);
1877 isl_multi_pw_aff_free(expr
->acc
.index
);
1878 expr
->acc
.index
= index
;
1879 expr
->acc
.depth
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
1883 isl_multi_pw_aff_free(index
);
1884 pet_expr_free(expr
);
1888 /* Return the reference identifier of access expression "expr".
1890 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
1894 if (expr
->type
!= pet_expr_access
)
1895 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1896 "not an access expression", return NULL
);
1898 return isl_id_copy(expr
->acc
.ref_id
);
1901 /* Replace the reference identifier of access expression "expr" by "ref_id".
1903 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
1904 __isl_take isl_id
*ref_id
)
1906 expr
= pet_expr_cow(expr
);
1907 if (!expr
|| !ref_id
)
1909 if (expr
->type
!= pet_expr_access
)
1910 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1911 "not an access expression", goto error
);
1912 isl_id_free(expr
->acc
.ref_id
);
1913 expr
->acc
.ref_id
= ref_id
;
1917 isl_id_free(ref_id
);
1918 pet_expr_free(expr
);
1922 /* Tag the access relation "access" with "id".
1923 * That is, insert the id as the range of a wrapped relation
1924 * in the domain of "access".
1926 * If "access" is of the form
1930 * then the result is of the form
1932 * [D[i] -> id[]] -> A[a]
1934 __isl_give isl_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
1935 __isl_take isl_map
*access
)
1938 isl_multi_aff
*add_tag
;
1941 if (expr
->type
!= pet_expr_access
)
1942 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1943 "not an access expression",
1944 return isl_map_free(access
));
1946 id
= isl_id_copy(expr
->acc
.ref_id
);
1947 space
= pet_expr_access_get_domain_space(expr
);
1948 space
= isl_space_from_domain(space
);
1949 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
1950 add_tag
= isl_multi_aff_domain_map(space
);
1951 access
= isl_map_preimage_domain_multi_aff(access
, add_tag
);
1956 /* Return the relation mapping pairs of domain iterations and argument
1957 * values to the corresponding accessed data elements.
1959 __isl_give isl_map
*pet_expr_access_get_dependent_access(
1960 __isl_keep pet_expr
*expr
)
1966 if (expr
->type
!= pet_expr_access
)
1967 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1968 "not an access expression", return NULL
);
1970 if (expr
->acc
.access
)
1971 return isl_map_copy(expr
->acc
.access
);
1973 expr
= pet_expr_copy(expr
);
1974 expr
= introduce_access_relation(expr
);
1977 access
= isl_map_copy(expr
->acc
.access
);
1978 pet_expr_free(expr
);
1983 /* Return the relation mapping domain iterations to all possibly
1984 * accessed data elements.
1985 * In particular, take the access relation and project out the values
1986 * of the arguments, if any.
1988 __isl_give isl_map
*pet_expr_access_get_may_access(__isl_keep pet_expr
*expr
)
1996 if (expr
->type
!= pet_expr_access
)
1997 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1998 "not an access expression", return NULL
);
2000 access
= pet_expr_access_get_dependent_access(expr
);
2001 if (expr
->n_arg
== 0)
2004 space
= isl_space_domain(isl_map_get_space(access
));
2005 map
= isl_map_universe(isl_space_unwrap(space
));
2006 map
= isl_map_domain_map(map
);
2007 access
= isl_map_apply_domain(access
, map
);
2012 /* Return a relation mapping domain iterations to definitely
2013 * accessed data elements, assuming the statement containing
2014 * the expression is executed.
2016 * If there are no arguments, then all elements are accessed.
2017 * Otherwise, we conservatively return an empty relation.
2019 __isl_give isl_map
*pet_expr_access_get_must_access(__isl_keep pet_expr
*expr
)
2025 if (expr
->type
!= pet_expr_access
)
2026 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2027 "not an access expression", return NULL
);
2029 if (expr
->n_arg
== 0)
2030 return pet_expr_access_get_dependent_access(expr
);
2032 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
2033 space
= isl_space_domain_factor_domain(space
);
2035 return isl_map_empty(space
);
2038 /* Return the relation mapping domain iterations to all possibly
2039 * accessed data elements, with its domain tagged with the reference
2042 __isl_give isl_map
*pet_expr_access_get_tagged_may_access(
2043 __isl_keep pet_expr
*expr
)
2050 access
= pet_expr_access_get_may_access(expr
);
2051 access
= pet_expr_tag_access(expr
, access
);
2056 /* Return the operation type of operation expression "expr".
2058 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
2062 if (expr
->type
!= pet_expr_op
)
2063 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2064 "not an operation expression", return pet_op_last
);
2069 /* Replace the operation type of operation expression "expr" by "type".
2071 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
2072 enum pet_op_type type
)
2075 return pet_expr_free(expr
);
2076 if (expr
->type
!= pet_expr_op
)
2077 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2078 "not an operation expression",
2079 return pet_expr_free(expr
));
2080 if (expr
->op
== type
)
2082 expr
= pet_expr_cow(expr
);
2090 /* Return the name of the function called by "expr".
2092 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
2096 if (expr
->type
!= pet_expr_call
)
2097 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2098 "not a call expression", return NULL
);
2102 /* Replace the name of the function called by "expr" by "name".
2104 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
2105 __isl_keep
const char *name
)
2107 expr
= pet_expr_cow(expr
);
2109 return pet_expr_free(expr
);
2110 if (expr
->type
!= pet_expr_call
)
2111 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2112 "not a call expression", return pet_expr_free(expr
));
2114 expr
->name
= strdup(name
);
2116 return pet_expr_free(expr
);
2120 /* Replace the type of the cast performed by "expr" by "name".
2122 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
2123 __isl_keep
const char *name
)
2125 expr
= pet_expr_cow(expr
);
2127 return pet_expr_free(expr
);
2128 if (expr
->type
!= pet_expr_cast
)
2129 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2130 "not a cast expression", return pet_expr_free(expr
));
2131 free(expr
->type_name
);
2132 expr
->type_name
= strdup(name
);
2133 if (!expr
->type_name
)
2134 return pet_expr_free(expr
);
2138 /* Return the value of the integer represented by "expr".
2140 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
2144 if (expr
->type
!= pet_expr_int
)
2145 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2146 "not an int expression", return NULL
);
2148 return isl_val_copy(expr
->i
);
2151 /* Replace the value of the integer represented by "expr" by "v".
2153 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
2154 __isl_take isl_val
*v
)
2156 expr
= pet_expr_cow(expr
);
2159 if (expr
->type
!= pet_expr_int
)
2160 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2161 "not an int expression", goto error
);
2162 isl_val_free(expr
->i
);
2168 pet_expr_free(expr
);
2172 /* Replace the value and string representation of the double
2173 * represented by "expr" by "d" and "s".
2175 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2176 double d
, __isl_keep
const char *s
)
2178 expr
= pet_expr_cow(expr
);
2180 return pet_expr_free(expr
);
2181 if (expr
->type
!= pet_expr_double
)
2182 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2183 "not a double expression", return pet_expr_free(expr
));
2186 expr
->d
.s
= strdup(s
);
2188 return pet_expr_free(expr
);
2192 /* Return a string representation of the double expression "expr".
2194 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2198 if (expr
->type
!= pet_expr_double
)
2199 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2200 "not a double expression", return NULL
);
2201 return strdup(expr
->d
.s
);
2204 /* Return a piecewise affine expression defined on the specified domain
2205 * that represents NaN.
2207 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2209 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2212 /* This function is called when we come across an access that is
2213 * nested in what is supposed to be an affine expression.
2214 * "pc" is the context in which the affine expression is created.
2215 * If nesting is allowed in "pc", we return an affine expression that is
2216 * equal to a new parameter corresponding to this nested access.
2217 * Otherwise, we return NaN.
2219 * Note that we currently don't allow nested accesses themselves
2220 * to contain any nested accesses, so we check if "expr" itself
2221 * involves any nested accesses (either explicitly as arguments
2222 * or implicitly through parameters) and return NaN if it does.
2224 * The new parameter is resolved in resolve_nested.
2226 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2227 __isl_keep pet_context
*pc
)
2232 isl_local_space
*ls
;
2238 if (!pet_context_allow_nesting(pc
))
2239 return non_affine(pet_context_get_space(pc
));
2241 if (pet_expr_get_type(expr
) != pet_expr_access
)
2242 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2243 "not an access expression", return NULL
);
2245 if (expr
->n_arg
> 0)
2246 return non_affine(pet_context_get_space(pc
));
2248 space
= pet_expr_access_get_parameter_space(expr
);
2249 nested
= pet_nested_any_in_space(space
);
2250 isl_space_free(space
);
2252 return non_affine(pet_context_get_space(pc
));
2254 ctx
= pet_expr_get_ctx(expr
);
2255 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2256 space
= pet_context_get_space(pc
);
2257 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2259 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2260 ls
= isl_local_space_from_space(space
);
2261 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2263 return isl_pw_aff_from_aff(aff
);
2266 /* Extract an affine expression from the access pet_expr "expr".
2267 * "pc" is the context in which the affine expression is created.
2269 * If "expr" is actually an affine expression rather than
2270 * a real access, then we return that expression.
2271 * Otherwise, we require that "expr" is of an integral type.
2272 * If not, we return NaN.
2274 * If the variable has been assigned a known affine expression,
2275 * then we return that expression.
2277 * Otherwise, we return an expression that is equal to a parameter
2278 * representing "expr" (if "allow_nested" is set).
2280 static __isl_give isl_pw_aff
*extract_affine_from_access(
2281 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2286 if (pet_expr_is_affine(expr
)) {
2288 isl_multi_pw_aff
*mpa
;
2290 mpa
= pet_expr_access_get_index(expr
);
2291 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
2292 isl_multi_pw_aff_free(mpa
);
2296 if (pet_expr_get_type_size(expr
) == 0)
2297 return non_affine(pet_context_get_space(pc
));
2299 if (!pet_expr_is_scalar_access(expr
))
2300 return nested_access(expr
, pc
);
2302 id
= pet_expr_access_get_id(expr
);
2303 if (pet_context_is_assigned(pc
, id
))
2304 return pet_context_get_value(pc
, id
);
2307 return nested_access(expr
, pc
);
2310 /* Construct an affine expression from the integer constant "expr".
2311 * "pc" is the context in which the affine expression is created.
2313 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2314 __isl_keep pet_context
*pc
)
2316 isl_local_space
*ls
;
2322 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2323 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2325 return isl_pw_aff_from_aff(aff
);
2328 /* Extract an affine expression from an addition or subtraction operation.
2329 * Return NaN if we are unable to extract an affine expression.
2331 * "pc" is the context in which the affine expression is created.
2333 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2334 __isl_keep pet_context
*pc
)
2341 if (expr
->n_arg
!= 2)
2342 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2343 "expecting two arguments", return NULL
);
2345 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2346 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2348 switch (pet_expr_op_get_type(expr
)) {
2350 return isl_pw_aff_add(lhs
, rhs
);
2352 return isl_pw_aff_sub(lhs
, rhs
);
2354 isl_pw_aff_free(lhs
);
2355 isl_pw_aff_free(rhs
);
2356 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2357 "not an addition or subtraction operation",
2363 /* Extract an affine expression from an integer division or a modulo operation.
2364 * Return NaN if we are unable to extract an affine expression.
2366 * "pc" is the context in which the affine expression is created.
2368 * In particular, if "expr" is lhs/rhs, then return
2370 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2372 * If "expr" is lhs%rhs, then return
2374 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2376 * If the second argument (rhs) is not a (positive) integer constant,
2377 * then we fail to extract an affine expression.
2379 * We simplify the result in the context of the domain of "pc" in case
2380 * this domain implies that lhs >= 0 (or < 0).
2382 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2383 __isl_keep pet_context
*pc
)
2392 if (expr
->n_arg
!= 2)
2393 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2394 "expecting two arguments", return NULL
);
2396 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2398 is_cst
= isl_pw_aff_is_cst(rhs
);
2399 if (is_cst
< 0 || !is_cst
) {
2400 isl_pw_aff_free(rhs
);
2401 return non_affine(pet_context_get_space(pc
));
2404 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2406 switch (pet_expr_op_get_type(expr
)) {
2408 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
2411 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
2414 isl_pw_aff_free(lhs
);
2415 isl_pw_aff_free(rhs
);
2416 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2417 "not a div or mod operator", return NULL
);
2420 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
2423 /* Extract an affine expression from a multiplication operation.
2424 * Return NaN if we are unable to extract an affine expression.
2425 * In particular, if neither of the arguments is a (piecewise) constant
2426 * then we return NaN.
2428 * "pc" is the context in which the affine expression is created.
2430 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2431 __isl_keep pet_context
*pc
)
2433 int lhs_cst
, rhs_cst
;
2439 if (expr
->n_arg
!= 2)
2440 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2441 "expecting two arguments", return NULL
);
2443 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2444 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2446 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2447 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2448 if (lhs_cst
< 0 || rhs_cst
< 0 || (!lhs_cst
&& !rhs_cst
)) {
2449 isl_pw_aff_free(lhs
);
2450 isl_pw_aff_free(rhs
);
2451 return non_affine(pet_context_get_space(pc
));
2454 return isl_pw_aff_mul(lhs
, rhs
);
2457 /* Extract an affine expression from a negation operation.
2458 * Return NaN if we are unable to extract an affine expression.
2460 * "pc" is the context in which the affine expression is created.
2462 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
2463 __isl_keep pet_context
*pc
)
2469 if (expr
->n_arg
!= 1)
2470 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2471 "expecting one argument", return NULL
);
2473 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
2474 return isl_pw_aff_neg(res
);
2477 /* Extract an affine expression from a conditional operation.
2478 * Return NaN if we are unable to extract an affine expression.
2480 * "pc" is the context in which the affine expression is created.
2482 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
2483 __isl_keep pet_context
*pc
)
2485 isl_pw_aff
*cond
, *lhs
, *rhs
;
2489 if (expr
->n_arg
!= 3)
2490 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2491 "expecting three arguments", return NULL
);
2493 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2494 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2495 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
2497 return isl_pw_aff_cond(cond
, lhs
, rhs
);
2504 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
, unsigned width
)
2509 ctx
= isl_pw_aff_get_ctx(pwaff
);
2510 mod
= isl_val_int_from_ui(ctx
, width
);
2511 mod
= isl_val_2exp(mod
);
2513 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
2518 /* Limit the domain of "pwaff" to those elements where the function
2521 * 2^{width-1} <= pwaff < 2^{width-1}
2523 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
2528 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
2529 isl_local_space
*ls
= isl_local_space_from_space(space
);
2534 ctx
= isl_pw_aff_get_ctx(pwaff
);
2535 v
= isl_val_int_from_ui(ctx
, width
- 1);
2536 v
= isl_val_2exp(v
);
2538 bound
= isl_aff_zero_on_domain(ls
);
2539 bound
= isl_aff_add_constant_val(bound
, v
);
2540 b
= isl_pw_aff_from_aff(bound
);
2542 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
2543 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2545 b
= isl_pw_aff_neg(b
);
2546 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
2547 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2552 /* Handle potential overflows on signed computations.
2554 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2555 * then we adjust the domain of "pa" to avoid overflows.
2557 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
2561 struct pet_options
*options
;
2566 ctx
= isl_pw_aff_get_ctx(pa
);
2567 options
= isl_ctx_peek_pet_options(ctx
);
2568 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
2569 pa
= avoid_overflow(pa
, width
);
2574 /* Extract an affine expression from some an operation.
2575 * Return NaN if we are unable to extract an affine expression.
2576 * If the result of a binary (non boolean) operation is unsigned,
2577 * then we wrap it based on the size of the type. If the result is signed,
2578 * then we ensure that no overflow occurs.
2580 * "pc" is the context in which the affine expression is created.
2582 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
2583 __isl_keep pet_context
*pc
)
2588 switch (pet_expr_op_get_type(expr
)) {
2591 res
= extract_affine_add_sub(expr
, pc
);
2595 res
= extract_affine_div_mod(expr
, pc
);
2598 res
= extract_affine_mul(expr
, pc
);
2601 return extract_affine_neg(expr
, pc
);
2603 return extract_affine_cond(expr
, pc
);
2613 return pet_expr_extract_affine_condition(expr
, pc
);
2615 return non_affine(pet_context_get_space(pc
));
2620 if (isl_pw_aff_involves_nan(res
)) {
2621 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
2622 isl_pw_aff_free(res
);
2623 return non_affine(space
);
2626 type_size
= pet_expr_get_type_size(expr
);
2628 res
= wrap(res
, type_size
);
2630 res
= signed_overflow(res
, -type_size
);
2635 /* Extract an affine expression from some special function calls.
2636 * Return NaN if we are unable to extract an affine expression.
2637 * In particular, we handle "min", "max", "ceild", "floord",
2638 * "intMod", "intFloor" and "intCeil".
2639 * In case of the latter five, the second argument needs to be
2640 * a (positive) integer constant.
2642 * "pc" is the context in which the affine expression is created.
2644 static __isl_give isl_pw_aff
*extract_affine_from_call(
2645 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2647 isl_pw_aff
*aff1
, *aff2
;
2651 n
= pet_expr_get_n_arg(expr
);
2652 name
= pet_expr_call_get_name(expr
);
2653 if (!(n
== 2 && !strcmp(name
, "min")) &&
2654 !(n
== 2 && !strcmp(name
, "max")) &&
2655 !(n
== 2 && !strcmp(name
, "intMod")) &&
2656 !(n
== 2 && !strcmp(name
, "intFloor")) &&
2657 !(n
== 2 && !strcmp(name
, "intCeil")) &&
2658 !(n
== 2 && !strcmp(name
, "floord")) &&
2659 !(n
== 2 && !strcmp(name
, "ceild")))
2660 return non_affine(pet_context_get_space(pc
));
2662 if (!strcmp(name
, "min") || !strcmp(name
, "max")) {
2663 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2664 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
2666 if (!strcmp(name
, "min"))
2667 aff1
= isl_pw_aff_min(aff1
, aff2
);
2669 aff1
= isl_pw_aff_max(aff1
, aff2
);
2670 } else if (!strcmp(name
, "intMod")) {
2673 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2674 return non_affine(pet_context_get_space(pc
));
2675 v
= pet_expr_int_get_val(expr
->args
[1]);
2676 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2677 aff1
= isl_pw_aff_mod_val(aff1
, v
);
2681 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2682 return non_affine(pet_context_get_space(pc
));
2683 v
= pet_expr_int_get_val(expr
->args
[1]);
2684 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2685 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
2686 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
2687 aff1
= isl_pw_aff_floor(aff1
);
2689 aff1
= isl_pw_aff_ceil(aff1
);
2695 /* Extract an affine expression from "expr", if possible.
2696 * Otherwise return NaN.
2698 * "pc" is the context in which the affine expression is created.
2700 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
2701 __isl_keep pet_context
*pc
)
2706 switch (pet_expr_get_type(expr
)) {
2707 case pet_expr_access
:
2708 return extract_affine_from_access(expr
, pc
);
2710 return extract_affine_from_int(expr
, pc
);
2712 return extract_affine_from_op(expr
, pc
);
2714 return extract_affine_from_call(expr
, pc
);
2716 case pet_expr_double
:
2717 case pet_expr_error
:
2718 return non_affine(pet_context_get_space(pc
));
2722 /* Extract an affine expressions representing the comparison "LHS op RHS"
2723 * Return NaN if we are unable to extract such an affine expression.
2725 * "pc" is the context in which the affine expression is created.
2727 * If the comparison is of the form
2731 * then the expression is constructed as the conjunction of
2736 * A similar optimization is performed for max(a,b) <= c.
2737 * We do this because that will lead to simpler representations
2738 * of the expression.
2739 * If isl is ever enhanced to explicitly deal with min and max expressions,
2740 * this optimization can be removed.
2742 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
2743 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
2744 __isl_keep pet_context
*pc
)
2746 isl_pw_aff
*lhs_pa
, *rhs_pa
;
2748 if (op
== pet_op_gt
)
2749 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
2750 if (op
== pet_op_ge
)
2751 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
2753 if (op
== pet_op_lt
|| op
== pet_op_le
) {
2754 if (pet_expr_is_min(rhs
)) {
2755 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2757 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2759 return pet_and(lhs_pa
, rhs_pa
);
2761 if (pet_expr_is_max(lhs
)) {
2762 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
2764 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
2766 return pet_and(lhs_pa
, rhs_pa
);
2770 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
2771 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
2773 return pet_comparison(op
, lhs_pa
, rhs_pa
);
2776 /* Extract an affine expressions from the comparison "expr".
2777 * Return NaN if we are unable to extract such an affine expression.
2779 * "pc" is the context in which the affine expression is created.
2781 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
2782 __isl_keep pet_context
*pc
)
2784 enum pet_op_type type
;
2788 if (expr
->n_arg
!= 2)
2789 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2790 "expecting two arguments", return NULL
);
2792 type
= pet_expr_op_get_type(expr
);
2793 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
2797 /* Extract an affine expression representing the boolean operation
2798 * expressed by "expr".
2799 * Return NaN if we are unable to extract an affine expression.
2801 * "pc" is the context in which the affine expression is created.
2803 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
2804 __isl_keep pet_context
*pc
)
2806 isl_pw_aff
*lhs
, *rhs
;
2812 n
= pet_expr_get_n_arg(expr
);
2813 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2815 return pet_not(lhs
);
2817 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
2818 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
2821 /* Extract the affine expression "expr != 0 ? 1 : 0".
2822 * Return NaN if we are unable to extract an affine expression.
2824 * "pc" is the context in which the affine expression is created.
2826 static __isl_give isl_pw_aff
*extract_implicit_condition(
2827 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2831 res
= pet_expr_extract_affine(expr
, pc
);
2832 return pet_to_bool(res
);
2835 /* Extract a boolean affine expression from "expr".
2836 * Return NaN if we are unable to extract an affine expression.
2838 * "pc" is the context in which the affine expression is created.
2840 * If "expr" is neither a comparison nor a boolean operation,
2841 * then we assume it is an affine expression and return the
2842 * boolean expression "expr != 0 ? 1 : 0".
2844 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
2845 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2850 if (pet_expr_is_comparison(expr
))
2851 return extract_comparison(expr
, pc
);
2852 if (pet_expr_is_boolean(expr
))
2853 return extract_boolean(expr
, pc
);
2855 return extract_implicit_condition(expr
, pc
);
2858 /* Check if "expr" is an assume expression and if its single argument
2859 * can be converted to an affine expression in the context of "pc".
2860 * If so, replace the argument by the affine expression.
2862 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
2863 __isl_keep pet_context
*pc
)
2866 isl_multi_pw_aff
*index
;
2870 if (!pet_expr_is_assume(expr
))
2872 if (expr
->n_arg
!= 1)
2873 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2874 "expecting one argument", return pet_expr_free(expr
));
2876 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2878 return pet_expr_free(expr
);
2879 if (isl_pw_aff_involves_nan(cond
)) {
2880 isl_pw_aff_free(cond
);
2884 index
= isl_multi_pw_aff_from_pw_aff(cond
);
2885 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
2890 /* Return the number of bits needed to represent the type of "expr".
2891 * See the description of the type_size field of pet_expr.
2893 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
2895 return expr
? expr
->type_size
: 0;
2898 /* Replace the number of bits needed to represent the type of "expr"
2900 * See the description of the type_size field of pet_expr.
2902 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
2905 expr
= pet_expr_cow(expr
);
2909 expr
->type_size
= type_size
;
2914 /* Extend an access expression "expr" with an additional index "index".
2915 * In particular, add "index" as an extra argument to "expr" and
2916 * adjust the index expression of "expr" to refer to this extra argument.
2917 * The caller is responsible for calling pet_expr_access_set_depth
2918 * to update the corresponding access relation.
2920 * Note that we only collect the individual index expressions as
2921 * arguments of "expr" here.
2922 * An attempt to integrate them into the index expression of "expr"
2923 * is performed in pet_expr_access_plug_in_args.
2925 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
2926 __isl_take pet_expr
*index
)
2930 isl_local_space
*ls
;
2933 expr
= pet_expr_cow(expr
);
2934 if (!expr
|| !index
)
2936 if (expr
->type
!= pet_expr_access
)
2937 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2938 "not an access pet_expr", goto error
);
2940 n
= pet_expr_get_n_arg(expr
);
2941 expr
= pet_expr_insert_arg(expr
, n
, index
);
2945 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
2946 ls
= isl_local_space_from_space(space
);
2947 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
2948 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
2949 if (!expr
->acc
.index
)
2950 return pet_expr_free(expr
);
2954 pet_expr_free(expr
);
2955 pet_expr_free(index
);
2959 /* Extend an access expression "expr" with an additional member acces to "id".
2960 * In particular, extend the index expression of "expr" to include
2961 * the additional member access.
2962 * The caller is responsible for calling pet_expr_access_set_depth
2963 * to update the corresponding access relation.
2965 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
2966 __isl_take isl_id
*id
)
2969 isl_multi_pw_aff
*field_access
;
2971 expr
= pet_expr_cow(expr
);
2974 if (expr
->type
!= pet_expr_access
)
2975 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2976 "not an access pet_expr", goto error
);
2978 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
2979 space
= isl_space_from_domain(space
);
2980 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2981 field_access
= isl_multi_pw_aff_zero(space
);
2982 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
2983 if (!expr
->acc
.index
)
2984 return pet_expr_free(expr
);
2988 pet_expr_free(expr
);
2993 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
3000 fprintf(stderr
, "%*s", indent
, "");
3002 switch (expr
->type
) {
3003 case pet_expr_double
:
3004 fprintf(stderr
, "%s\n", expr
->d
.s
);
3007 isl_val_dump(expr
->i
);
3009 case pet_expr_access
:
3010 if (expr
->acc
.ref_id
) {
3011 isl_id_dump(expr
->acc
.ref_id
);
3012 fprintf(stderr
, "%*s", indent
, "");
3014 isl_multi_pw_aff_dump(expr
->acc
.index
);
3015 fprintf(stderr
, "%*sdepth: %d\n", indent
+ 2,
3016 "", expr
->acc
.depth
);
3017 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
3018 "", expr
->acc
.read
);
3019 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
3020 "", expr
->acc
.write
);
3021 if (expr
->acc
.access
) {
3022 fprintf(stderr
, "%*saccess: ", indent
+ 2, "");
3023 isl_map_dump(expr
->acc
.access
);
3025 for (i
= 0; i
< expr
->n_arg
; ++i
)
3026 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3029 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
3030 for (i
= 0; i
< expr
->n_arg
; ++i
)
3031 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3034 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
3035 for (i
= 0; i
< expr
->n_arg
; ++i
)
3036 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3039 fprintf(stderr
, "(%s)\n", expr
->type_name
);
3040 for (i
= 0; i
< expr
->n_arg
; ++i
)
3041 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3043 case pet_expr_error
:
3044 fprintf(stderr
, "ERROR\n");
3049 void pet_expr_dump(__isl_keep pet_expr
*expr
)
3051 pet_expr_dump_with_indent(expr
, 0);