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_union_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
1935 __isl_take isl_union_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_union_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_union_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 static __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 an empty access relation for access expression "expr".
1985 static __isl_give isl_union_map
*empty_access_relation(
1986 __isl_keep pet_expr
*expr
)
1988 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr
));
1991 /* Return the may read access relation associated to "expr"
1992 * that maps pairs of domain iterations and argument values
1993 * to the corresponding accessed data elements.
1995 * Since the accesses are currently represented by a single access relation,
1996 * we return the entire access relation if "expr" is a read and
1997 * an empty relation if it is not.
1999 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_read(
2000 __isl_keep pet_expr
*expr
)
2006 if (!pet_expr_access_is_read(expr
))
2007 return empty_access_relation(expr
);
2008 access
= pet_expr_access_get_dependent_access(expr
);
2009 return isl_union_map_from_map(access
);
2012 /* Return the may write access relation associated to "expr"
2013 * that maps pairs of domain iterations and argument values
2014 * to the corresponding accessed data elements.
2016 * Since the accesses are currently represented by a single access relation,
2017 * we return the entire access relation if "expr" is a write and
2018 * an empty relation if it is not.
2020 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_write(
2021 __isl_keep pet_expr
*expr
)
2027 if (!pet_expr_access_is_write(expr
))
2028 return empty_access_relation(expr
);
2029 access
= pet_expr_access_get_dependent_access(expr
);
2030 return isl_union_map_from_map(access
);
2033 /* Return the must write access relation associated to "expr"
2034 * that maps pairs of domain iterations and argument values
2035 * to the corresponding accessed data elements.
2037 * Since the accesses are currently represented by a single access relation,
2038 * we return the entire access relation when "expr" is a write.
2040 __isl_give isl_union_map
*pet_expr_access_get_dependent_must_write(
2041 __isl_keep pet_expr
*expr
)
2047 if (!pet_expr_access_is_write(expr
))
2048 return empty_access_relation(expr
);
2049 access
= pet_expr_access_get_dependent_access(expr
);
2050 return isl_union_map_from_map(access
);
2053 /* Return the relation mapping domain iterations to all possibly
2054 * accessed data elements.
2055 * In particular, take the access relation and project out the values
2056 * of the arguments, if any.
2058 __isl_give isl_map
*pet_expr_access_get_may_access(__isl_keep pet_expr
*expr
)
2066 if (expr
->type
!= pet_expr_access
)
2067 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2068 "not an access expression", return NULL
);
2070 access
= pet_expr_access_get_dependent_access(expr
);
2071 if (expr
->n_arg
== 0)
2074 space
= isl_space_domain(isl_map_get_space(access
));
2075 map
= isl_map_universe(isl_space_unwrap(space
));
2076 map
= isl_map_domain_map(map
);
2077 access
= isl_map_apply_domain(access
, map
);
2082 /* Return the relation mapping domain iterations to all possibly
2083 * read data elements.
2085 * Since the accesses are currently represented by a single access relation,
2086 * we return the may access relation if "expr" is a read and
2087 * an empty relation if it is not.
2089 __isl_give isl_union_map
*pet_expr_access_get_may_read(
2090 __isl_keep pet_expr
*expr
)
2094 if (!pet_expr_access_is_read(expr
))
2095 return empty_access_relation(expr
);
2096 return isl_union_map_from_map(pet_expr_access_get_may_access(expr
));
2099 /* Return the relation mapping domain iterations to all possibly
2100 * written data elements.
2102 * Since the accesses are currently represented by a single access relation,
2103 * we return the may access relation if "expr" is a write and
2104 * an empty relation if it is not.
2106 __isl_give isl_union_map
*pet_expr_access_get_may_write(
2107 __isl_keep pet_expr
*expr
)
2111 if (!pet_expr_access_is_write(expr
))
2112 return empty_access_relation(expr
);
2113 return isl_union_map_from_map(pet_expr_access_get_may_access(expr
));
2116 /* Return a relation mapping domain iterations to definitely
2117 * accessed data elements, assuming the statement containing
2118 * the expression is executed.
2120 * If there are no arguments, then all elements are accessed.
2121 * Otherwise, we conservatively return an empty relation.
2123 static __isl_give isl_map
*pet_expr_access_get_must_access(
2124 __isl_keep pet_expr
*expr
)
2130 if (expr
->type
!= pet_expr_access
)
2131 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2132 "not an access expression", return NULL
);
2134 if (expr
->n_arg
== 0)
2135 return pet_expr_access_get_dependent_access(expr
);
2137 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
2138 space
= isl_space_domain_factor_domain(space
);
2140 return isl_map_empty(space
);
2143 /* Return a relation mapping domain iterations to definitely
2144 * written data elements, assuming the statement containing
2145 * the expression is executed.
2147 * Since the accesses are currently represented by a single access relation,
2148 * we return the must access relation if "expr" is a write and
2149 * an empty relation if it is not.
2151 __isl_give isl_union_map
*pet_expr_access_get_must_write(
2152 __isl_keep pet_expr
*expr
)
2156 if (!pet_expr_access_is_write(expr
))
2157 return empty_access_relation(expr
);
2158 return isl_union_map_from_map(pet_expr_access_get_must_access(expr
));
2161 /* Return the relation mapping domain iterations to all possibly
2162 * read data elements, with its domain tagged with the reference
2165 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_read(
2166 __isl_keep pet_expr
*expr
)
2168 isl_union_map
*access
;
2173 access
= pet_expr_access_get_may_read(expr
);
2174 access
= pet_expr_tag_access(expr
, access
);
2179 /* Return the relation mapping domain iterations to all possibly
2180 * written data elements, with its domain tagged with the reference
2183 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_write(
2184 __isl_keep pet_expr
*expr
)
2186 isl_union_map
*access
;
2191 access
= pet_expr_access_get_may_write(expr
);
2192 access
= pet_expr_tag_access(expr
, access
);
2197 /* Return the operation type of operation expression "expr".
2199 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
2203 if (expr
->type
!= pet_expr_op
)
2204 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2205 "not an operation expression", return pet_op_last
);
2210 /* Replace the operation type of operation expression "expr" by "type".
2212 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
2213 enum pet_op_type type
)
2216 return pet_expr_free(expr
);
2217 if (expr
->type
!= pet_expr_op
)
2218 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2219 "not an operation expression",
2220 return pet_expr_free(expr
));
2221 if (expr
->op
== type
)
2223 expr
= pet_expr_cow(expr
);
2231 /* Return the name of the function called by "expr".
2233 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
2237 if (expr
->type
!= pet_expr_call
)
2238 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2239 "not a call expression", return NULL
);
2243 /* Replace the name of the function called by "expr" by "name".
2245 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
2246 __isl_keep
const char *name
)
2248 expr
= pet_expr_cow(expr
);
2250 return pet_expr_free(expr
);
2251 if (expr
->type
!= pet_expr_call
)
2252 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2253 "not a call expression", return pet_expr_free(expr
));
2255 expr
->name
= strdup(name
);
2257 return pet_expr_free(expr
);
2261 /* Replace the type of the cast performed by "expr" by "name".
2263 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
2264 __isl_keep
const char *name
)
2266 expr
= pet_expr_cow(expr
);
2268 return pet_expr_free(expr
);
2269 if (expr
->type
!= pet_expr_cast
)
2270 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2271 "not a cast expression", return pet_expr_free(expr
));
2272 free(expr
->type_name
);
2273 expr
->type_name
= strdup(name
);
2274 if (!expr
->type_name
)
2275 return pet_expr_free(expr
);
2279 /* Return the value of the integer represented by "expr".
2281 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
2285 if (expr
->type
!= pet_expr_int
)
2286 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2287 "not an int expression", return NULL
);
2289 return isl_val_copy(expr
->i
);
2292 /* Replace the value of the integer represented by "expr" by "v".
2294 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
2295 __isl_take isl_val
*v
)
2297 expr
= pet_expr_cow(expr
);
2300 if (expr
->type
!= pet_expr_int
)
2301 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2302 "not an int expression", goto error
);
2303 isl_val_free(expr
->i
);
2309 pet_expr_free(expr
);
2313 /* Replace the value and string representation of the double
2314 * represented by "expr" by "d" and "s".
2316 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2317 double d
, __isl_keep
const char *s
)
2319 expr
= pet_expr_cow(expr
);
2321 return pet_expr_free(expr
);
2322 if (expr
->type
!= pet_expr_double
)
2323 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2324 "not a double expression", return pet_expr_free(expr
));
2327 expr
->d
.s
= strdup(s
);
2329 return pet_expr_free(expr
);
2333 /* Return a string representation of the double expression "expr".
2335 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2339 if (expr
->type
!= pet_expr_double
)
2340 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2341 "not a double expression", return NULL
);
2342 return strdup(expr
->d
.s
);
2345 /* Return a piecewise affine expression defined on the specified domain
2346 * that represents NaN.
2348 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2350 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2353 /* This function is called when we come across an access that is
2354 * nested in what is supposed to be an affine expression.
2355 * "pc" is the context in which the affine expression is created.
2356 * If nesting is allowed in "pc", we return an affine expression that is
2357 * equal to a new parameter corresponding to this nested access.
2358 * Otherwise, we return NaN.
2360 * Note that we currently don't allow nested accesses themselves
2361 * to contain any nested accesses, so we check if "expr" itself
2362 * involves any nested accesses (either explicitly as arguments
2363 * or implicitly through parameters) and return NaN if it does.
2365 * The new parameter is resolved in resolve_nested.
2367 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2368 __isl_keep pet_context
*pc
)
2373 isl_local_space
*ls
;
2379 if (!pet_context_allow_nesting(pc
))
2380 return non_affine(pet_context_get_space(pc
));
2382 if (pet_expr_get_type(expr
) != pet_expr_access
)
2383 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2384 "not an access expression", return NULL
);
2386 if (expr
->n_arg
> 0)
2387 return non_affine(pet_context_get_space(pc
));
2389 space
= pet_expr_access_get_parameter_space(expr
);
2390 nested
= pet_nested_any_in_space(space
);
2391 isl_space_free(space
);
2393 return non_affine(pet_context_get_space(pc
));
2395 ctx
= pet_expr_get_ctx(expr
);
2396 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2397 space
= pet_context_get_space(pc
);
2398 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2400 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2401 ls
= isl_local_space_from_space(space
);
2402 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2404 return isl_pw_aff_from_aff(aff
);
2407 /* Extract an affine expression from the access pet_expr "expr".
2408 * "pc" is the context in which the affine expression is created.
2410 * If "expr" is actually an affine expression rather than
2411 * a real access, then we return that expression.
2412 * Otherwise, we require that "expr" is of an integral type.
2413 * If not, we return NaN.
2415 * If the variable has been assigned a known affine expression,
2416 * then we return that expression.
2418 * Otherwise, we return an expression that is equal to a parameter
2419 * representing "expr" (if "allow_nested" is set).
2421 static __isl_give isl_pw_aff
*extract_affine_from_access(
2422 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2427 if (pet_expr_is_affine(expr
)) {
2429 isl_multi_pw_aff
*mpa
;
2431 mpa
= pet_expr_access_get_index(expr
);
2432 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
2433 isl_multi_pw_aff_free(mpa
);
2437 if (pet_expr_get_type_size(expr
) == 0)
2438 return non_affine(pet_context_get_space(pc
));
2440 if (!pet_expr_is_scalar_access(expr
))
2441 return nested_access(expr
, pc
);
2443 id
= pet_expr_access_get_id(expr
);
2444 if (pet_context_is_assigned(pc
, id
))
2445 return pet_context_get_value(pc
, id
);
2448 return nested_access(expr
, pc
);
2451 /* Construct an affine expression from the integer constant "expr".
2452 * "pc" is the context in which the affine expression is created.
2454 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2455 __isl_keep pet_context
*pc
)
2457 isl_local_space
*ls
;
2463 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2464 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2466 return isl_pw_aff_from_aff(aff
);
2469 /* Extract an affine expression from an addition or subtraction operation.
2470 * Return NaN if we are unable to extract an affine expression.
2472 * "pc" is the context in which the affine expression is created.
2474 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2475 __isl_keep pet_context
*pc
)
2482 if (expr
->n_arg
!= 2)
2483 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2484 "expecting two arguments", return NULL
);
2486 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2487 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2489 switch (pet_expr_op_get_type(expr
)) {
2491 return isl_pw_aff_add(lhs
, rhs
);
2493 return isl_pw_aff_sub(lhs
, rhs
);
2495 isl_pw_aff_free(lhs
);
2496 isl_pw_aff_free(rhs
);
2497 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2498 "not an addition or subtraction operation",
2504 /* Extract an affine expression from an integer division or a modulo operation.
2505 * Return NaN if we are unable to extract an affine expression.
2507 * "pc" is the context in which the affine expression is created.
2509 * In particular, if "expr" is lhs/rhs, then return
2511 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2513 * If "expr" is lhs%rhs, then return
2515 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2517 * If the second argument (rhs) is not a (positive) integer constant,
2518 * then we fail to extract an affine expression.
2520 * We simplify the result in the context of the domain of "pc" in case
2521 * this domain implies that lhs >= 0 (or < 0).
2523 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2524 __isl_keep pet_context
*pc
)
2533 if (expr
->n_arg
!= 2)
2534 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2535 "expecting two arguments", return NULL
);
2537 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2539 is_cst
= isl_pw_aff_is_cst(rhs
);
2540 if (is_cst
< 0 || !is_cst
) {
2541 isl_pw_aff_free(rhs
);
2542 return non_affine(pet_context_get_space(pc
));
2545 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2547 switch (pet_expr_op_get_type(expr
)) {
2549 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
2552 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
2555 isl_pw_aff_free(lhs
);
2556 isl_pw_aff_free(rhs
);
2557 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2558 "not a div or mod operator", return NULL
);
2561 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
2564 /* Extract an affine expression from a multiplication operation.
2565 * Return NaN if we are unable to extract an affine expression.
2566 * In particular, if neither of the arguments is a (piecewise) constant
2567 * then we return NaN.
2569 * "pc" is the context in which the affine expression is created.
2571 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2572 __isl_keep pet_context
*pc
)
2574 int lhs_cst
, rhs_cst
;
2580 if (expr
->n_arg
!= 2)
2581 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2582 "expecting two arguments", return NULL
);
2584 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2585 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2587 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2588 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2589 if (lhs_cst
< 0 || rhs_cst
< 0 || (!lhs_cst
&& !rhs_cst
)) {
2590 isl_pw_aff_free(lhs
);
2591 isl_pw_aff_free(rhs
);
2592 return non_affine(pet_context_get_space(pc
));
2595 return isl_pw_aff_mul(lhs
, rhs
);
2598 /* Extract an affine expression from a negation operation.
2599 * Return NaN if we are unable to extract an affine expression.
2601 * "pc" is the context in which the affine expression is created.
2603 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
2604 __isl_keep pet_context
*pc
)
2610 if (expr
->n_arg
!= 1)
2611 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2612 "expecting one argument", return NULL
);
2614 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
2615 return isl_pw_aff_neg(res
);
2618 /* Extract an affine expression from a conditional operation.
2619 * Return NaN if we are unable to extract an affine expression.
2621 * "pc" is the context in which the affine expression is created.
2623 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
2624 __isl_keep pet_context
*pc
)
2626 isl_pw_aff
*cond
, *lhs
, *rhs
;
2630 if (expr
->n_arg
!= 3)
2631 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2632 "expecting three arguments", return NULL
);
2634 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2635 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2636 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
2638 return isl_pw_aff_cond(cond
, lhs
, rhs
);
2645 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
, unsigned width
)
2650 ctx
= isl_pw_aff_get_ctx(pwaff
);
2651 mod
= isl_val_int_from_ui(ctx
, width
);
2652 mod
= isl_val_2exp(mod
);
2654 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
2659 /* Limit the domain of "pwaff" to those elements where the function
2662 * 2^{width-1} <= pwaff < 2^{width-1}
2664 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
2669 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
2670 isl_local_space
*ls
= isl_local_space_from_space(space
);
2675 ctx
= isl_pw_aff_get_ctx(pwaff
);
2676 v
= isl_val_int_from_ui(ctx
, width
- 1);
2677 v
= isl_val_2exp(v
);
2679 bound
= isl_aff_zero_on_domain(ls
);
2680 bound
= isl_aff_add_constant_val(bound
, v
);
2681 b
= isl_pw_aff_from_aff(bound
);
2683 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
2684 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2686 b
= isl_pw_aff_neg(b
);
2687 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
2688 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2693 /* Handle potential overflows on signed computations.
2695 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2696 * then we adjust the domain of "pa" to avoid overflows.
2698 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
2702 struct pet_options
*options
;
2707 ctx
= isl_pw_aff_get_ctx(pa
);
2708 options
= isl_ctx_peek_pet_options(ctx
);
2709 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
2710 pa
= avoid_overflow(pa
, width
);
2715 /* Extract an affine expression from some an operation.
2716 * Return NaN if we are unable to extract an affine expression.
2717 * If the result of a binary (non boolean) operation is unsigned,
2718 * then we wrap it based on the size of the type. If the result is signed,
2719 * then we ensure that no overflow occurs.
2721 * "pc" is the context in which the affine expression is created.
2723 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
2724 __isl_keep pet_context
*pc
)
2729 switch (pet_expr_op_get_type(expr
)) {
2732 res
= extract_affine_add_sub(expr
, pc
);
2736 res
= extract_affine_div_mod(expr
, pc
);
2739 res
= extract_affine_mul(expr
, pc
);
2742 return extract_affine_neg(expr
, pc
);
2744 return extract_affine_cond(expr
, pc
);
2754 return pet_expr_extract_affine_condition(expr
, pc
);
2756 return non_affine(pet_context_get_space(pc
));
2761 if (isl_pw_aff_involves_nan(res
)) {
2762 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
2763 isl_pw_aff_free(res
);
2764 return non_affine(space
);
2767 type_size
= pet_expr_get_type_size(expr
);
2769 res
= wrap(res
, type_size
);
2771 res
= signed_overflow(res
, -type_size
);
2776 /* Extract an affine expression from some special function calls.
2777 * Return NaN if we are unable to extract an affine expression.
2778 * In particular, we handle "min", "max", "ceild", "floord",
2779 * "intMod", "intFloor" and "intCeil".
2780 * In case of the latter five, the second argument needs to be
2781 * a (positive) integer constant.
2783 * "pc" is the context in which the affine expression is created.
2785 static __isl_give isl_pw_aff
*extract_affine_from_call(
2786 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2788 isl_pw_aff
*aff1
, *aff2
;
2792 n
= pet_expr_get_n_arg(expr
);
2793 name
= pet_expr_call_get_name(expr
);
2794 if (!(n
== 2 && !strcmp(name
, "min")) &&
2795 !(n
== 2 && !strcmp(name
, "max")) &&
2796 !(n
== 2 && !strcmp(name
, "intMod")) &&
2797 !(n
== 2 && !strcmp(name
, "intFloor")) &&
2798 !(n
== 2 && !strcmp(name
, "intCeil")) &&
2799 !(n
== 2 && !strcmp(name
, "floord")) &&
2800 !(n
== 2 && !strcmp(name
, "ceild")))
2801 return non_affine(pet_context_get_space(pc
));
2803 if (!strcmp(name
, "min") || !strcmp(name
, "max")) {
2804 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2805 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
2807 if (!strcmp(name
, "min"))
2808 aff1
= isl_pw_aff_min(aff1
, aff2
);
2810 aff1
= isl_pw_aff_max(aff1
, aff2
);
2811 } else if (!strcmp(name
, "intMod")) {
2814 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2815 return non_affine(pet_context_get_space(pc
));
2816 v
= pet_expr_int_get_val(expr
->args
[1]);
2817 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2818 aff1
= isl_pw_aff_mod_val(aff1
, v
);
2822 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2823 return non_affine(pet_context_get_space(pc
));
2824 v
= pet_expr_int_get_val(expr
->args
[1]);
2825 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2826 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
2827 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
2828 aff1
= isl_pw_aff_floor(aff1
);
2830 aff1
= isl_pw_aff_ceil(aff1
);
2836 /* Extract an affine expression from "expr", if possible.
2837 * Otherwise return NaN.
2839 * "pc" is the context in which the affine expression is created.
2841 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
2842 __isl_keep pet_context
*pc
)
2847 switch (pet_expr_get_type(expr
)) {
2848 case pet_expr_access
:
2849 return extract_affine_from_access(expr
, pc
);
2851 return extract_affine_from_int(expr
, pc
);
2853 return extract_affine_from_op(expr
, pc
);
2855 return extract_affine_from_call(expr
, pc
);
2857 case pet_expr_double
:
2858 case pet_expr_error
:
2859 return non_affine(pet_context_get_space(pc
));
2863 /* Extract an affine expressions representing the comparison "LHS op RHS"
2864 * Return NaN if we are unable to extract such an affine expression.
2866 * "pc" is the context in which the affine expression is created.
2868 * If the comparison is of the form
2872 * then the expression is constructed as the conjunction of
2877 * A similar optimization is performed for max(a,b) <= c.
2878 * We do this because that will lead to simpler representations
2879 * of the expression.
2880 * If isl is ever enhanced to explicitly deal with min and max expressions,
2881 * this optimization can be removed.
2883 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
2884 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
2885 __isl_keep pet_context
*pc
)
2887 isl_pw_aff
*lhs_pa
, *rhs_pa
;
2889 if (op
== pet_op_gt
)
2890 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
2891 if (op
== pet_op_ge
)
2892 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
2894 if (op
== pet_op_lt
|| op
== pet_op_le
) {
2895 if (pet_expr_is_min(rhs
)) {
2896 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2898 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2900 return pet_and(lhs_pa
, rhs_pa
);
2902 if (pet_expr_is_max(lhs
)) {
2903 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
2905 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
2907 return pet_and(lhs_pa
, rhs_pa
);
2911 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
2912 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
2914 return pet_comparison(op
, lhs_pa
, rhs_pa
);
2917 /* Extract an affine expressions from the comparison "expr".
2918 * Return NaN if we are unable to extract such an affine expression.
2920 * "pc" is the context in which the affine expression is created.
2922 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
2923 __isl_keep pet_context
*pc
)
2925 enum pet_op_type type
;
2929 if (expr
->n_arg
!= 2)
2930 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2931 "expecting two arguments", return NULL
);
2933 type
= pet_expr_op_get_type(expr
);
2934 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
2938 /* Extract an affine expression representing the boolean operation
2939 * expressed by "expr".
2940 * Return NaN if we are unable to extract an affine expression.
2942 * "pc" is the context in which the affine expression is created.
2944 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
2945 __isl_keep pet_context
*pc
)
2947 isl_pw_aff
*lhs
, *rhs
;
2953 n
= pet_expr_get_n_arg(expr
);
2954 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2956 return pet_not(lhs
);
2958 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
2959 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
2962 /* Extract the affine expression "expr != 0 ? 1 : 0".
2963 * Return NaN if we are unable to extract an affine expression.
2965 * "pc" is the context in which the affine expression is created.
2967 static __isl_give isl_pw_aff
*extract_implicit_condition(
2968 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2972 res
= pet_expr_extract_affine(expr
, pc
);
2973 return pet_to_bool(res
);
2976 /* Extract a boolean affine expression from "expr".
2977 * Return NaN if we are unable to extract an affine expression.
2979 * "pc" is the context in which the affine expression is created.
2981 * If "expr" is neither a comparison nor a boolean operation,
2982 * then we assume it is an affine expression and return the
2983 * boolean expression "expr != 0 ? 1 : 0".
2985 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
2986 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2991 if (pet_expr_is_comparison(expr
))
2992 return extract_comparison(expr
, pc
);
2993 if (pet_expr_is_boolean(expr
))
2994 return extract_boolean(expr
, pc
);
2996 return extract_implicit_condition(expr
, pc
);
2999 /* Check if "expr" is an assume expression and if its single argument
3000 * can be converted to an affine expression in the context of "pc".
3001 * If so, replace the argument by the affine expression.
3003 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
3004 __isl_keep pet_context
*pc
)
3007 isl_multi_pw_aff
*index
;
3011 if (!pet_expr_is_assume(expr
))
3013 if (expr
->n_arg
!= 1)
3014 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3015 "expecting one argument", return pet_expr_free(expr
));
3017 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3019 return pet_expr_free(expr
);
3020 if (isl_pw_aff_involves_nan(cond
)) {
3021 isl_pw_aff_free(cond
);
3025 index
= isl_multi_pw_aff_from_pw_aff(cond
);
3026 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
3031 /* Return the number of bits needed to represent the type of "expr".
3032 * See the description of the type_size field of pet_expr.
3034 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
3036 return expr
? expr
->type_size
: 0;
3039 /* Replace the number of bits needed to represent the type of "expr"
3041 * See the description of the type_size field of pet_expr.
3043 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
3046 expr
= pet_expr_cow(expr
);
3050 expr
->type_size
= type_size
;
3055 /* Extend an access expression "expr" with an additional index "index".
3056 * In particular, add "index" as an extra argument to "expr" and
3057 * adjust the index expression of "expr" to refer to this extra argument.
3058 * The caller is responsible for calling pet_expr_access_set_depth
3059 * to update the corresponding access relation.
3061 * Note that we only collect the individual index expressions as
3062 * arguments of "expr" here.
3063 * An attempt to integrate them into the index expression of "expr"
3064 * is performed in pet_expr_access_plug_in_args.
3066 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
3067 __isl_take pet_expr
*index
)
3071 isl_local_space
*ls
;
3074 expr
= pet_expr_cow(expr
);
3075 if (!expr
|| !index
)
3077 if (expr
->type
!= pet_expr_access
)
3078 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3079 "not an access pet_expr", goto error
);
3081 n
= pet_expr_get_n_arg(expr
);
3082 expr
= pet_expr_insert_arg(expr
, n
, index
);
3086 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3087 ls
= isl_local_space_from_space(space
);
3088 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
3089 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
3090 if (!expr
->acc
.index
)
3091 return pet_expr_free(expr
);
3095 pet_expr_free(expr
);
3096 pet_expr_free(index
);
3100 /* Extend an access expression "expr" with an additional member acces to "id".
3101 * In particular, extend the index expression of "expr" to include
3102 * the additional member access.
3103 * The caller is responsible for calling pet_expr_access_set_depth
3104 * to update the corresponding access relation.
3106 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
3107 __isl_take isl_id
*id
)
3110 isl_multi_pw_aff
*field_access
;
3112 expr
= pet_expr_cow(expr
);
3115 if (expr
->type
!= pet_expr_access
)
3116 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3117 "not an access pet_expr", goto error
);
3119 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3120 space
= isl_space_from_domain(space
);
3121 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
3122 field_access
= isl_multi_pw_aff_zero(space
);
3123 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
3124 if (!expr
->acc
.index
)
3125 return pet_expr_free(expr
);
3129 pet_expr_free(expr
);
3134 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
3141 fprintf(stderr
, "%*s", indent
, "");
3143 switch (expr
->type
) {
3144 case pet_expr_double
:
3145 fprintf(stderr
, "%s\n", expr
->d
.s
);
3148 isl_val_dump(expr
->i
);
3150 case pet_expr_access
:
3151 if (expr
->acc
.ref_id
) {
3152 isl_id_dump(expr
->acc
.ref_id
);
3153 fprintf(stderr
, "%*s", indent
, "");
3155 isl_multi_pw_aff_dump(expr
->acc
.index
);
3156 fprintf(stderr
, "%*sdepth: %d\n", indent
+ 2,
3157 "", expr
->acc
.depth
);
3158 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
3159 "", expr
->acc
.read
);
3160 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
3161 "", expr
->acc
.write
);
3162 if (expr
->acc
.access
) {
3163 fprintf(stderr
, "%*saccess: ", indent
+ 2, "");
3164 isl_map_dump(expr
->acc
.access
);
3166 for (i
= 0; i
< expr
->n_arg
; ++i
)
3167 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3170 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
3171 for (i
= 0; i
< expr
->n_arg
; ++i
)
3172 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3175 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
3176 for (i
= 0; i
< expr
->n_arg
; ++i
)
3177 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3180 fprintf(stderr
, "(%s)\n", expr
->type_name
);
3181 for (i
= 0; i
< expr
->n_arg
; ++i
)
3182 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3184 case pet_expr_error
:
3185 fprintf(stderr
, "ERROR\n");
3190 void pet_expr_dump(__isl_keep pet_expr
*expr
)
3192 pet_expr_dump_with_indent(expr
, 0);