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
41 #include <isl/space.h>
42 #include <isl/local_space.h>
45 #include <isl/union_set.h>
46 #include <isl/union_map.h>
47 #include <isl/printer.h>
56 #include "value_bounds.h"
59 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
61 static char *type_str
[] = {
62 [pet_expr_access
] = "access",
63 [pet_expr_call
] = "call",
64 [pet_expr_cast
] = "cast",
65 [pet_expr_double
] = "double",
66 [pet_expr_int
] = "int",
70 static char *op_str
[] = {
71 [pet_op_add_assign
] = "+=",
72 [pet_op_sub_assign
] = "-=",
73 [pet_op_mul_assign
] = "*=",
74 [pet_op_div_assign
] = "/=",
75 [pet_op_and_assign
] = "&=",
76 [pet_op_xor_assign
] = "^=",
77 [pet_op_or_assign
] = "|=",
78 [pet_op_assign
] = "=",
93 [pet_op_post_inc
] = "++",
94 [pet_op_post_dec
] = "--",
95 [pet_op_pre_inc
] = "++",
96 [pet_op_pre_dec
] = "--",
97 [pet_op_address_of
] = "&",
102 [pet_op_land
] = "&&",
105 [pet_op_cond
] = "?:",
106 [pet_op_assume
] = "assume",
107 [pet_op_kill
] = "kill"
110 const char *pet_op_str(enum pet_op_type op
)
115 int pet_op_is_inc_dec(enum pet_op_type op
)
117 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
118 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
121 const char *pet_type_str(enum pet_expr_type type
)
123 return type_str
[type
];
126 enum pet_op_type
pet_str_op(const char *str
)
130 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
131 if (!strcmp(op_str
[i
], str
))
137 enum pet_expr_type
pet_str_type(const char *str
)
141 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
142 if (!strcmp(type_str
[i
], str
))
148 /* Construct a pet_expr of the given type.
150 __isl_give pet_expr
*pet_expr_alloc(isl_ctx
*ctx
, enum pet_expr_type type
)
154 expr
= isl_calloc_type(ctx
, struct pet_expr
);
166 /* Construct an access pet_expr from an index expression.
167 * By default, the access is considered to be a read access.
168 * The initial depth is set from the index expression and
169 * may still be updated by the caller before the access relation
172 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
179 ctx
= isl_multi_pw_aff_get_ctx(index
);
180 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
187 expr
= pet_expr_access_set_index(expr
, index
);
191 isl_multi_pw_aff_free(index
);
195 /* Extend the range of "access" with "n" dimensions, retaining
196 * the tuple identifier on this range.
198 * If "access" represents a member access, then extend the range
201 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
205 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
207 if (!isl_map_range_is_wrapping(access
)) {
208 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
212 domain
= isl_map_copy(access
);
213 domain
= isl_map_range_factor_domain(domain
);
214 access
= isl_map_range_factor_range(access
);
215 access
= extend_range(access
, n
);
216 access
= isl_map_range_product(domain
, access
);
219 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
224 /* Does the access expression "expr" have any explicit access relation?
226 isl_bool
pet_expr_access_has_any_access_relation(__isl_keep pet_expr
*expr
)
228 enum pet_expr_access_type type
;
231 return isl_bool_error
;
233 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
)
234 if (expr
->acc
.access
[type
])
235 return isl_bool_true
;
237 return isl_bool_false
;
240 /* Are all relevant access relations explicitly available in "expr"?
242 static int has_relevant_access_relations(__isl_keep pet_expr
*expr
)
247 if (expr
->acc
.kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
249 if (expr
->acc
.read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
251 if (expr
->acc
.write
&&
252 (!expr
->acc
.access
[pet_expr_access_may_write
] ||
253 !expr
->acc
.access
[pet_expr_access_must_write
]))
259 /* Replace the depth of the access expr "expr" by "depth".
261 * To avoid inconsistencies between the depth and the access relation,
262 * we currently do not allow the depth to change once the access relation
263 * has been set or computed.
265 __isl_give pet_expr
*pet_expr_access_set_depth(__isl_take pet_expr
*expr
,
270 if (expr
->acc
.depth
== depth
)
272 if (pet_expr_access_has_any_access_relation(expr
))
273 isl_die(pet_expr_get_ctx(expr
), isl_error_unsupported
,
274 "depth cannot be changed after access relation "
275 "has been set or computed", return pet_expr_free(expr
));
277 expr
= pet_expr_cow(expr
);
280 expr
->acc
.depth
= depth
;
285 /* Construct a pet_expr that kills the elements specified by
286 * the index expression "index" and the access relation "access".
288 __isl_give pet_expr
*pet_expr_kill_from_access_and_index(
289 __isl_take isl_map
*access
, __isl_take isl_multi_pw_aff
*index
)
294 if (!access
|| !index
)
297 expr
= pet_expr_from_index(index
);
298 expr
= pet_expr_access_set_read(expr
, 0);
299 expr
= pet_expr_access_set_kill(expr
, 1);
300 depth
= isl_map_dim(access
, isl_dim_out
);
301 expr
= pet_expr_access_set_depth(expr
, depth
);
302 expr
= pet_expr_access_set_access(expr
, pet_expr_access_killed
,
303 isl_union_map_from_map(access
));
304 return pet_expr_new_unary(0, pet_op_kill
, expr
);
306 isl_map_free(access
);
307 isl_multi_pw_aff_free(index
);
311 /* Construct a unary pet_expr that performs "op" on "arg",
312 * where the result is represented using a type of "type_size" bits
313 * (may be zero if unknown or if the type is not an integer).
315 __isl_give pet_expr
*pet_expr_new_unary(int type_size
, enum pet_op_type op
,
316 __isl_take pet_expr
*arg
)
323 ctx
= pet_expr_get_ctx(arg
);
324 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
325 expr
= pet_expr_set_n_arg(expr
, 1);
330 expr
->type_size
= type_size
;
331 expr
->args
[pet_un_arg
] = arg
;
339 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
340 * where the result is represented using a type of "type_size" bits
341 * (may be zero if unknown or if the type is not an integer).
343 __isl_give pet_expr
*pet_expr_new_binary(int type_size
, enum pet_op_type op
,
344 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
351 ctx
= pet_expr_get_ctx(lhs
);
352 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
353 expr
= pet_expr_set_n_arg(expr
, 2);
358 expr
->type_size
= type_size
;
359 expr
->args
[pet_bin_lhs
] = lhs
;
360 expr
->args
[pet_bin_rhs
] = rhs
;
369 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
371 __isl_give pet_expr
*pet_expr_new_ternary(__isl_take pet_expr
*cond
,
372 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
377 if (!cond
|| !lhs
|| !rhs
)
379 ctx
= pet_expr_get_ctx(cond
);
380 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
381 expr
= pet_expr_set_n_arg(expr
, 3);
385 expr
->op
= pet_op_cond
;
386 expr
->args
[pet_ter_cond
] = cond
;
387 expr
->args
[pet_ter_true
] = lhs
;
388 expr
->args
[pet_ter_false
] = rhs
;
398 /* Construct a call pet_expr that calls function "name" with "n_arg"
399 * arguments. The caller is responsible for filling in the arguments.
401 __isl_give pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
406 expr
= pet_expr_alloc(ctx
, pet_expr_call
);
407 expr
= pet_expr_set_n_arg(expr
, n_arg
);
411 expr
->c
.name
= strdup(name
);
413 return pet_expr_free(expr
);
418 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
420 __isl_give pet_expr
*pet_expr_new_cast(const char *type_name
,
421 __isl_take pet_expr
*arg
)
429 ctx
= pet_expr_get_ctx(arg
);
430 expr
= pet_expr_alloc(ctx
, pet_expr_cast
);
431 expr
= pet_expr_set_n_arg(expr
, 1);
435 expr
->type_name
= strdup(type_name
);
436 if (!expr
->type_name
)
448 /* Construct a pet_expr that represents the double "d".
450 __isl_give pet_expr
*pet_expr_new_double(isl_ctx
*ctx
,
451 double val
, const char *s
)
455 expr
= pet_expr_alloc(ctx
, pet_expr_double
);
460 expr
->d
.s
= strdup(s
);
462 return pet_expr_free(expr
);
467 /* Construct a pet_expr that represents the integer value "v".
469 __isl_give pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
477 ctx
= isl_val_get_ctx(v
);
478 expr
= pet_expr_alloc(ctx
, pet_expr_int
);
490 /* Return an independent duplicate of "expr".
492 * In case of an access expression, make sure the depth of the duplicate is set
493 * before the access relation (if any) is set and after the index expression
496 static __isl_give pet_expr
*pet_expr_dup(__isl_keep pet_expr
*expr
)
500 enum pet_expr_access_type type
;
505 dup
= pet_expr_alloc(expr
->ctx
, expr
->type
);
506 dup
= pet_expr_set_type_size(dup
, expr
->type_size
);
507 dup
= pet_expr_set_n_arg(dup
, expr
->n_arg
);
508 for (i
= 0; i
< expr
->n_arg
; ++i
)
509 dup
= pet_expr_set_arg(dup
, i
, pet_expr_copy(expr
->args
[i
]));
511 switch (expr
->type
) {
512 case pet_expr_access
:
513 if (expr
->acc
.ref_id
)
514 dup
= pet_expr_access_set_ref_id(dup
,
515 isl_id_copy(expr
->acc
.ref_id
));
516 dup
= pet_expr_access_set_index(dup
,
517 isl_multi_pw_aff_copy(expr
->acc
.index
));
518 dup
= pet_expr_access_set_depth(dup
, expr
->acc
.depth
);
519 for (type
= pet_expr_access_begin
;
520 type
< pet_expr_access_end
; ++type
) {
521 if (!expr
->acc
.access
[type
])
523 dup
= pet_expr_access_set_access(dup
, type
,
524 isl_union_map_copy(expr
->acc
.access
[type
]));
526 dup
= pet_expr_access_set_read(dup
, expr
->acc
.read
);
527 dup
= pet_expr_access_set_write(dup
, expr
->acc
.write
);
528 dup
= pet_expr_access_set_kill(dup
, expr
->acc
.kill
);
531 dup
= pet_expr_call_set_name(dup
, expr
->c
.name
);
533 dup
= pet_expr_call_set_summary(dup
,
534 pet_function_summary_copy(expr
->c
.summary
));
537 dup
= pet_expr_cast_set_type_name(dup
, expr
->type_name
);
539 case pet_expr_double
:
540 dup
= pet_expr_double_set(dup
, expr
->d
.val
, expr
->d
.s
);
543 dup
= pet_expr_int_set_val(dup
, isl_val_copy(expr
->i
));
546 dup
= pet_expr_op_set_type(dup
, expr
->op
);
549 dup
= pet_expr_free(dup
);
556 /* Return a pet_expr that is equal to "expr" and that has only
557 * a single reference.
559 * If "expr" itself only has one reference, then clear its hash value
560 * since the returned pet_expr will be modified.
562 __isl_give pet_expr
*pet_expr_cow(__isl_take pet_expr
*expr
)
567 if (expr
->ref
== 1) {
572 return pet_expr_dup(expr
);
575 __isl_null pet_expr
*pet_expr_free(__isl_take pet_expr
*expr
)
577 enum pet_expr_access_type type
;
585 for (i
= 0; i
< expr
->n_arg
; ++i
)
586 pet_expr_free(expr
->args
[i
]);
589 switch (expr
->type
) {
590 case pet_expr_access
:
591 isl_id_free(expr
->acc
.ref_id
);
592 for (type
= pet_expr_access_begin
;
593 type
< pet_expr_access_end
; ++type
)
594 isl_union_map_free(expr
->acc
.access
[type
]);
595 isl_multi_pw_aff_free(expr
->acc
.index
);
599 pet_function_summary_free(expr
->c
.summary
);
602 free(expr
->type_name
);
604 case pet_expr_double
:
608 isl_val_free(expr
->i
);
615 isl_ctx_deref(expr
->ctx
);
620 /* Return an additional reference to "expr".
622 __isl_give pet_expr
*pet_expr_copy(__isl_keep pet_expr
*expr
)
631 /* Return the isl_ctx in which "expr" was created.
633 isl_ctx
*pet_expr_get_ctx(__isl_keep pet_expr
*expr
)
635 return expr
? expr
->ctx
: NULL
;
638 /* Return the type of "expr".
640 enum pet_expr_type
pet_expr_get_type(__isl_keep pet_expr
*expr
)
643 return pet_expr_error
;
647 /* Return the number of arguments of "expr".
649 int pet_expr_get_n_arg(__isl_keep pet_expr
*expr
)
657 /* Set the number of arguments of "expr" to "n".
659 * If "expr" originally had more arguments, then remove the extra arguments.
660 * If "expr" originally had fewer arguments, then create space for
661 * the extra arguments ans initialize them to NULL.
663 __isl_give pet_expr
*pet_expr_set_n_arg(__isl_take pet_expr
*expr
, int n
)
670 if (expr
->n_arg
== n
)
672 expr
= pet_expr_cow(expr
);
676 if (n
< expr
->n_arg
) {
677 for (i
= n
; i
< expr
->n_arg
; ++i
)
678 pet_expr_free(expr
->args
[i
]);
683 args
= isl_realloc_array(expr
->ctx
, expr
->args
, pet_expr
*, n
);
685 return pet_expr_free(expr
);
687 for (i
= expr
->n_arg
; i
< n
; ++i
)
688 expr
->args
[i
] = NULL
;
694 /* Return the argument of "expr" at position "pos".
696 __isl_give pet_expr
*pet_expr_get_arg(__isl_keep pet_expr
*expr
, int pos
)
700 if (pos
< 0 || pos
>= expr
->n_arg
)
701 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
702 "position out of bounds", return NULL
);
704 return pet_expr_copy(expr
->args
[pos
]);
707 /* Replace "expr" by its argument at position "pos".
709 __isl_give pet_expr
*pet_expr_arg(__isl_take pet_expr
*expr
, int pos
)
713 arg
= pet_expr_get_arg(expr
, pos
);
719 /* Replace the argument of "expr" at position "pos" by "arg".
721 __isl_give pet_expr
*pet_expr_set_arg(__isl_take pet_expr
*expr
, int pos
,
722 __isl_take pet_expr
*arg
)
726 if (pos
< 0 || pos
>= expr
->n_arg
)
727 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
728 "position out of bounds", goto error
);
729 if (expr
->args
[pos
] == arg
) {
734 expr
= pet_expr_cow(expr
);
738 pet_expr_free(expr
->args
[pos
]);
739 expr
->args
[pos
] = arg
;
748 /* Does "expr" perform a comparison operation?
750 int pet_expr_is_comparison(__isl_keep pet_expr
*expr
)
754 if (expr
->type
!= pet_expr_op
)
769 /* Does "expr" perform a boolean operation?
771 int pet_expr_is_boolean(__isl_keep pet_expr
*expr
)
775 if (expr
->type
!= pet_expr_op
)
787 /* Is "expr" an address-of operation?
789 int pet_expr_is_address_of(__isl_keep pet_expr
*expr
)
793 if (expr
->type
!= pet_expr_op
)
795 return expr
->op
== pet_op_address_of
;
798 /* Is "expr" an assume statement?
800 int pet_expr_is_assume(__isl_keep pet_expr
*expr
)
804 if (expr
->type
!= pet_expr_op
)
806 return expr
->op
== pet_op_assume
;
809 /* Does "expr" perform a min operation?
811 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
815 if (expr
->type
!= pet_expr_call
)
817 if (expr
->n_arg
!= 2)
819 if (strcmp(expr
->c
.name
, "min") != 0)
824 /* Does "expr" perform a max operation?
826 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
830 if (expr
->type
!= pet_expr_call
)
832 if (expr
->n_arg
!= 2)
834 if (strcmp(expr
->c
.name
, "max") != 0)
839 /* Does "expr" represent an access to an unnamed space, i.e.,
840 * does it represent an affine expression?
842 isl_bool
pet_expr_is_affine(__isl_keep pet_expr
*expr
)
847 return isl_bool_error
;
848 if (expr
->type
!= pet_expr_access
)
849 return isl_bool_false
;
851 has_id
= isl_multi_pw_aff_has_tuple_id(expr
->acc
.index
, isl_dim_out
);
853 return isl_bool_error
;
858 /* Given that "expr" represents an affine expression, i.e., that
859 * it is an access to an unnamed (1D) space, return this affine expression.
861 __isl_give isl_pw_aff
*pet_expr_get_affine(__isl_keep pet_expr
*expr
)
865 isl_multi_pw_aff
*mpa
;
867 is_affine
= pet_expr_is_affine(expr
);
871 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
872 "not an affine expression", return NULL
);
874 mpa
= pet_expr_access_get_index(expr
);
875 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
876 isl_multi_pw_aff_free(mpa
);
880 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
881 * not part of any struct?
883 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
887 if (expr
->type
!= pet_expr_access
)
889 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
))
892 return expr
->acc
.depth
== 0;
895 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
898 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
899 __isl_keep isl_multi_pw_aff
*mpa2
)
903 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
904 if (equal
< 0 || equal
)
906 mpa2
= isl_multi_pw_aff_copy(mpa2
);
907 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
908 isl_multi_pw_aff_get_space(mpa1
));
909 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
910 isl_multi_pw_aff_free(mpa2
);
915 /* Construct an access relation from the index expression and
916 * the array depth of the access expression "expr".
918 * If the number of indices is smaller than the depth of the array,
919 * then we assume that all elements of the remaining dimensions
922 static __isl_give isl_union_map
*construct_access_relation(
923 __isl_keep pet_expr
*expr
)
931 access
= isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr
));
935 dim
= isl_map_dim(access
, isl_dim_out
);
936 if (dim
> expr
->acc
.depth
)
937 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
938 "number of indices greater than depth",
939 access
= isl_map_free(access
));
941 if (dim
!= expr
->acc
.depth
)
942 access
= extend_range(access
, expr
->acc
.depth
- dim
);
944 return isl_union_map_from_map(access
);
947 /* Ensure that all relevant access relations are explicitly
948 * available in "expr".
950 * If "expr" does not already have the relevant access relations, then create
951 * them based on the index expression and the array depth.
953 * We do not cow since adding an explicit access relation
954 * does not change the meaning of the expression.
955 * However, the explicit access relations may modify the hash value,
956 * so the cached value is reset.
958 static __isl_give pet_expr
*introduce_access_relations(
959 __isl_take pet_expr
*expr
)
961 isl_union_map
*access
;
962 int kill
, read
, write
;
966 if (has_relevant_access_relations(expr
))
969 access
= construct_access_relation(expr
);
971 return pet_expr_free(expr
);
974 kill
= expr
->acc
.kill
;
975 read
= expr
->acc
.read
;
976 write
= expr
->acc
.write
;
977 if (kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
978 expr
->acc
.access
[pet_expr_access_fake_killed
] =
979 isl_union_map_copy(access
);
980 if (read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
981 expr
->acc
.access
[pet_expr_access_may_read
] =
982 isl_union_map_copy(access
);
983 if (write
&& !expr
->acc
.access
[pet_expr_access_may_write
])
984 expr
->acc
.access
[pet_expr_access_may_write
] =
985 isl_union_map_copy(access
);
986 if (write
&& !expr
->acc
.access
[pet_expr_access_must_write
])
987 expr
->acc
.access
[pet_expr_access_must_write
] =
988 isl_union_map_copy(access
);
990 isl_union_map_free(access
);
992 if (!has_relevant_access_relations(expr
))
993 return pet_expr_free(expr
);
998 /* Return a hash value that digests "expr".
999 * If a hash value was computed already, then return that value.
1000 * Otherwise, compute the hash value and store a copy in expr->hash.
1002 uint32_t pet_expr_get_hash(__isl_keep pet_expr
*expr
)
1005 enum pet_expr_access_type type
;
1006 uint32_t hash
, hash_f
;
1013 hash
= isl_hash_init();
1014 isl_hash_byte(hash
, expr
->type
& 0xFF);
1015 isl_hash_byte(hash
, expr
->n_arg
& 0xFF);
1016 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1018 hash_i
= pet_expr_get_hash(expr
->args
[i
]);
1019 isl_hash_hash(hash
, hash_i
);
1021 switch (expr
->type
) {
1022 case pet_expr_error
:
1024 case pet_expr_double
:
1025 hash
= isl_hash_string(hash
, expr
->d
.s
);
1028 hash_f
= isl_val_get_hash(expr
->i
);
1029 isl_hash_hash(hash
, hash_f
);
1031 case pet_expr_access
:
1032 isl_hash_byte(hash
, expr
->acc
.read
& 0xFF);
1033 isl_hash_byte(hash
, expr
->acc
.write
& 0xFF);
1034 isl_hash_byte(hash
, expr
->acc
.kill
& 0xFF);
1035 hash_f
= isl_id_get_hash(expr
->acc
.ref_id
);
1036 isl_hash_hash(hash
, hash_f
);
1037 hash_f
= isl_multi_pw_aff_get_hash(expr
->acc
.index
);
1038 isl_hash_hash(hash
, hash_f
);
1039 isl_hash_byte(hash
, expr
->acc
.depth
& 0xFF);
1040 for (type
= pet_expr_access_begin
;
1041 type
< pet_expr_access_end
; ++type
) {
1042 hash_f
= isl_union_map_get_hash(expr
->acc
.access
[type
]);
1043 isl_hash_hash(hash
, hash_f
);
1047 isl_hash_byte(hash
, expr
->op
& 0xFF);
1050 hash
= isl_hash_string(hash
, expr
->c
.name
);
1053 hash
= isl_hash_string(hash
, expr
->type_name
);
1060 /* Return 1 if the two pet_exprs are equivalent.
1062 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
1065 enum pet_expr_access_type type
;
1067 if (!expr1
|| !expr2
)
1070 if (expr1
->type
!= expr2
->type
)
1072 if (expr1
->n_arg
!= expr2
->n_arg
)
1074 for (i
= 0; i
< expr1
->n_arg
; ++i
)
1075 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
1077 switch (expr1
->type
) {
1078 case pet_expr_error
:
1080 case pet_expr_double
:
1081 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
1083 if (expr1
->d
.val
!= expr2
->d
.val
)
1087 if (!isl_val_eq(expr1
->i
, expr2
->i
))
1090 case pet_expr_access
:
1091 if (expr1
->acc
.read
!= expr2
->acc
.read
)
1093 if (expr1
->acc
.write
!= expr2
->acc
.write
)
1095 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
1097 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
1099 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
1101 if (!multi_pw_aff_is_equal(expr1
->acc
.index
, expr2
->acc
.index
))
1103 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
1105 if (has_relevant_access_relations(expr1
) !=
1106 has_relevant_access_relations(expr2
)) {
1108 expr1
= pet_expr_copy(expr1
);
1109 expr2
= pet_expr_copy(expr2
);
1110 expr1
= introduce_access_relations(expr1
);
1111 expr2
= introduce_access_relations(expr2
);
1112 equal
= pet_expr_is_equal(expr1
, expr2
);
1113 pet_expr_free(expr1
);
1114 pet_expr_free(expr2
);
1117 for (type
= pet_expr_access_begin
;
1118 type
< pet_expr_access_end
; ++type
) {
1119 if (!expr1
->acc
.access
[type
] !=
1120 !expr2
->acc
.access
[type
])
1122 if (!expr1
->acc
.access
[type
])
1124 if (!isl_union_map_is_equal(expr1
->acc
.access
[type
],
1125 expr2
->acc
.access
[type
]))
1130 if (expr1
->op
!= expr2
->op
)
1134 if (strcmp(expr1
->c
.name
, expr2
->c
.name
))
1138 if (strcmp(expr1
->type_name
, expr2
->type_name
))
1146 /* Do "expr1" and "expr2" represent two accesses to the same array
1147 * that are also of the same type? That is, can these two accesses
1148 * be replaced by a single access?
1150 isl_bool
pet_expr_is_same_access(__isl_keep pet_expr
*expr1
,
1151 __isl_keep pet_expr
*expr2
)
1153 isl_space
*space1
, *space2
;
1156 if (!expr1
|| !expr2
)
1157 return isl_bool_error
;
1158 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1159 return isl_bool_false
;
1160 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1161 return isl_bool_false
;
1162 if (expr1
->acc
.read
!= expr2
->acc
.read
)
1163 return isl_bool_false
;
1164 if (expr1
->acc
.write
!= expr2
->acc
.write
)
1165 return isl_bool_false
;
1166 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
1167 return isl_bool_false
;
1168 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
1169 return isl_bool_false
;
1171 space1
= isl_multi_pw_aff_get_space(expr1
->acc
.index
);
1172 space2
= isl_multi_pw_aff_get_space(expr2
->acc
.index
);
1173 same
= isl_space_tuple_is_equal(space1
, isl_dim_out
,
1174 space2
, isl_dim_out
);
1175 if (same
>= 0 && same
)
1176 same
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
1177 space2
, isl_dim_in
);
1178 isl_space_free(space1
);
1179 isl_space_free(space2
);
1184 /* Does the access expression "expr" read the accessed elements?
1186 isl_bool
pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
1189 return isl_bool_error
;
1190 if (expr
->type
!= pet_expr_access
)
1191 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1192 "not an access expression", return isl_bool_error
);
1194 return expr
->acc
.read
;
1197 /* Does the access expression "expr" write to the accessed elements?
1199 isl_bool
pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
1202 return isl_bool_error
;
1203 if (expr
->type
!= pet_expr_access
)
1204 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1205 "not an access expression", return isl_bool_error
);
1207 return expr
->acc
.write
;
1210 /* Does the access expression "expr" kill the accessed elements?
1212 isl_bool
pet_expr_access_is_kill(__isl_keep pet_expr
*expr
)
1215 return isl_bool_error
;
1216 if (expr
->type
!= pet_expr_access
)
1217 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1218 "not an access expression", return isl_bool_error
);
1220 return expr
->acc
.kill
;
1223 /* Return the identifier of the array accessed by "expr".
1225 * If "expr" represents a member access, then return the identifier
1226 * of the outer structure array.
1228 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
1232 if (expr
->type
!= pet_expr_access
)
1233 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1234 "not an access expression", return NULL
);
1236 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
)) {
1240 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1241 space
= isl_space_range(space
);
1242 while (space
&& isl_space_is_wrapping(space
))
1243 space
= isl_space_domain(isl_space_unwrap(space
));
1244 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
1245 isl_space_free(space
);
1250 return isl_multi_pw_aff_get_tuple_id(expr
->acc
.index
, isl_dim_out
);
1253 /* Return the parameter space of "expr".
1255 __isl_give isl_space
*pet_expr_access_get_parameter_space(
1256 __isl_keep pet_expr
*expr
)
1262 if (expr
->type
!= pet_expr_access
)
1263 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1264 "not an access expression", return NULL
);
1266 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1267 space
= isl_space_params(space
);
1272 /* Return the domain space of "expr", including the arguments (if any).
1274 __isl_give isl_space
*pet_expr_access_get_augmented_domain_space(
1275 __isl_keep pet_expr
*expr
)
1281 if (expr
->type
!= pet_expr_access
)
1282 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1283 "not an access expression", return NULL
);
1285 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1286 space
= isl_space_domain(space
);
1291 /* Return the domain space of "expr", without the arguments (if any).
1293 __isl_give isl_space
*pet_expr_access_get_domain_space(
1294 __isl_keep pet_expr
*expr
)
1298 space
= pet_expr_access_get_augmented_domain_space(expr
);
1299 if (isl_space_is_wrapping(space
))
1300 space
= isl_space_domain(isl_space_unwrap(space
));
1305 /* Internal data structure for pet_expr_access_foreach_data_space.
1307 struct pet_foreach_data_space_data
{
1308 isl_stat (*fn
)(__isl_take isl_space
*space
, void *user
);
1312 /* Given a piece of an access relation, call data->fn on the data
1313 * (i.e., range) space.
1315 static isl_stat
foreach_data_space(__isl_take isl_map
*map
, void *user
)
1317 struct pet_foreach_data_space_data
*data
= user
;
1320 space
= isl_map_get_space(map
);
1321 space
= isl_space_range(space
);
1324 return data
->fn(space
, data
->user
);
1327 /* Call "fn" on the data spaces accessed by "expr".
1328 * In particular, call "fn" on the range space of the index expression,
1329 * but if "expr" keeps track of any explicit access relations,
1330 * then also call "fn" on the corresponding range spaces.
1332 isl_stat
pet_expr_access_foreach_data_space(__isl_keep pet_expr
*expr
,
1333 isl_stat (*fn
)(__isl_take isl_space
*space
, void *user
), void *user
)
1335 struct pet_foreach_data_space_data data
= { fn
, user
};
1336 enum pet_expr_access_type type
;
1340 return isl_stat_error
;
1341 if (expr
->type
!= pet_expr_access
)
1342 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1343 "not an access expression", return isl_stat_error
);
1345 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1346 if (!expr
->acc
.access
[type
])
1348 if (isl_union_map_foreach_map(expr
->acc
.access
[type
],
1349 &foreach_data_space
, &data
) < 0)
1350 return isl_stat_error
;
1353 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1354 space
= isl_space_range(space
);
1355 return fn(space
, user
);
1358 /* Modify all subexpressions of "expr" by calling "fn" on them.
1359 * The subexpressions are traversed in depth first preorder.
1361 __isl_give pet_expr
*pet_expr_map_top_down(__isl_take pet_expr
*expr
,
1362 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1370 expr
= fn(expr
, user
);
1372 n
= pet_expr_get_n_arg(expr
);
1373 for (i
= 0; i
< n
; ++i
) {
1374 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1375 arg
= pet_expr_map_top_down(arg
, fn
, user
);
1376 expr
= pet_expr_set_arg(expr
, i
, arg
);
1382 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1384 static __isl_give pet_expr
*pet_expr_map_expr_of_type(__isl_take pet_expr
*expr
,
1385 enum pet_expr_type type
,
1386 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1391 n
= pet_expr_get_n_arg(expr
);
1392 for (i
= 0; i
< n
; ++i
) {
1393 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1394 arg
= pet_expr_map_expr_of_type(arg
, type
, fn
, user
);
1395 expr
= pet_expr_set_arg(expr
, i
, arg
);
1401 if (expr
->type
== type
)
1402 expr
= fn(expr
, user
);
1407 /* Modify all expressions of type pet_expr_access in "expr"
1408 * by calling "fn" on them.
1410 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
1411 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1414 return pet_expr_map_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1417 /* Modify all expressions of type pet_expr_call in "expr"
1418 * by calling "fn" on them.
1420 __isl_give pet_expr
*pet_expr_map_call(__isl_take pet_expr
*expr
,
1421 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1424 return pet_expr_map_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1427 /* Modify all expressions of type pet_expr_op in "expr"
1428 * by calling "fn" on them.
1430 __isl_give pet_expr
*pet_expr_map_op(__isl_take pet_expr
*expr
,
1431 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1434 return pet_expr_map_expr_of_type(expr
, pet_expr_op
, fn
, user
);
1437 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1439 * Return -1 on error (where fn returning a negative value is treated as
1441 * Otherwise return 0.
1443 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
1444 enum pet_expr_type type
,
1445 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1452 for (i
= 0; i
< expr
->n_arg
; ++i
)
1453 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1454 type
, fn
, user
) < 0)
1457 if (expr
->type
== type
)
1458 return fn(expr
, user
);
1463 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1465 * Return -1 on error (where fn returning a negative value is treated as
1467 * Otherwise return 0.
1469 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1470 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1472 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1475 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1477 * Return -1 on error (where fn returning a negative value is treated as
1479 * Otherwise return 0.
1481 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1482 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1484 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1487 /* Internal data structure for pet_expr_writes.
1488 * "id" is the identifier that we are looking for.
1489 * "found" is set if we have found the identifier being written to.
1491 struct pet_expr_writes_data
{
1496 /* Given an access expression, check if it writes to data->id.
1497 * If so, set data->found and abort the search.
1499 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1501 struct pet_expr_writes_data
*data
= user
;
1504 if (!expr
->acc
.write
)
1506 if (pet_expr_is_affine(expr
))
1509 write_id
= pet_expr_access_get_id(expr
);
1510 isl_id_free(write_id
);
1515 if (write_id
!= data
->id
)
1522 /* Does expression "expr" write to "id"?
1524 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1526 struct pet_expr_writes_data data
;
1530 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1537 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1538 * index expression and access relations of "expr" (if any)
1539 * to dimensions of "dst_type" at "dst_pos".
1541 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1542 enum isl_dim_type dst_type
, unsigned dst_pos
,
1543 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1545 enum pet_expr_access_type type
;
1547 expr
= pet_expr_cow(expr
);
1550 if (expr
->type
!= pet_expr_access
)
1551 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1552 "not an access pet_expr", return pet_expr_free(expr
));
1554 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1555 if (!expr
->acc
.access
[type
])
1557 expr
->acc
.access
[type
] =
1558 pet_union_map_move_dims(expr
->acc
.access
[type
],
1559 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1560 if (!expr
->acc
.access
[type
])
1563 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1564 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1565 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1566 return pet_expr_free(expr
);
1571 /* Replace the index expression and access relations (if any) of "expr"
1572 * by their preimages under the function represented by "ma".
1574 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1575 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1577 enum pet_expr_access_type type
;
1579 expr
= pet_expr_cow(expr
);
1582 if (expr
->type
!= pet_expr_access
)
1583 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1584 "not an access pet_expr", goto error
);
1586 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1587 if (!expr
->acc
.access
[type
])
1589 expr
->acc
.access
[type
] =
1590 isl_union_map_preimage_domain_multi_aff(
1591 expr
->acc
.access
[type
], isl_multi_aff_copy(ma
));
1592 if (!expr
->acc
.access
[type
])
1595 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1597 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1598 return pet_expr_free(expr
);
1602 isl_multi_aff_free(ma
);
1603 pet_expr_free(expr
);
1607 /* Replace the index expression and access relations (if any) of "expr"
1608 * by their preimages under the function represented by "mpa".
1610 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1611 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1613 enum pet_expr_access_type type
;
1615 expr
= pet_expr_cow(expr
);
1618 if (expr
->type
!= pet_expr_access
)
1619 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1620 "not an access pet_expr", goto error
);
1622 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1623 if (!expr
->acc
.access
[type
])
1625 expr
->acc
.access
[type
] =
1626 isl_union_map_preimage_domain_multi_pw_aff(
1627 expr
->acc
.access
[type
], isl_multi_pw_aff_copy(mpa
));
1628 if (!expr
->acc
.access
[type
])
1631 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1632 expr
->acc
.index
, mpa
);
1633 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1634 return pet_expr_free(expr
);
1638 isl_multi_pw_aff_free(mpa
);
1639 pet_expr_free(expr
);
1643 /* Return the index expression of access expression "expr".
1645 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1646 __isl_keep pet_expr
*expr
)
1650 if (expr
->type
!= pet_expr_access
)
1651 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1652 "not an access expression", return NULL
);
1654 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1657 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1659 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1662 enum pet_expr_access_type type
;
1664 expr
= pet_expr_cow(expr
);
1667 if (expr
->type
!= pet_expr_access
)
1668 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1669 "not an access expression", return pet_expr_free(expr
));
1671 if (!pet_expr_access_has_any_access_relation(expr
))
1674 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1675 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1676 if (!expr
->acc
.access
[type
])
1678 space
= isl_space_align_params(space
,
1679 isl_union_map_get_space(expr
->acc
.access
[type
]));
1681 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1682 isl_space_copy(space
));
1683 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1684 if (!expr
->acc
.access
[type
])
1686 expr
->acc
.access
[type
] =
1687 isl_union_map_align_params(expr
->acc
.access
[type
],
1688 isl_space_copy(space
));
1689 if (!expr
->acc
.access
[type
])
1692 isl_space_free(space
);
1693 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1694 return pet_expr_free(expr
);
1699 /* Are "expr1" and "expr2" both array accesses such that
1700 * the access relation of "expr1" is a subset of that of "expr2"?
1701 * Only take into account the first "n_arg" arguments.
1703 * This function is tailored for use by mark_self_dependences in nest.c.
1704 * In particular, the input expressions may have more than "n_arg"
1705 * elements in their arguments arrays, while only the first "n_arg"
1706 * elements are referenced from the access relations.
1708 int pet_expr_is_sub_access(__isl_keep pet_expr
*expr1
,
1709 __isl_keep pet_expr
*expr2
, int n_arg
)
1715 if (!expr1
|| !expr2
)
1717 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1719 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1721 if (pet_expr_is_affine(expr1
))
1723 if (pet_expr_is_affine(expr2
))
1725 n1
= pet_expr_get_n_arg(expr1
);
1728 n2
= pet_expr_get_n_arg(expr2
);
1733 for (i
= 0; i
< n1
; ++i
) {
1735 equal
= pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]);
1736 if (equal
< 0 || !equal
)
1739 id1
= pet_expr_access_get_id(expr1
);
1740 id2
= pet_expr_access_get_id(expr2
);
1748 expr1
= pet_expr_copy(expr1
);
1749 expr2
= pet_expr_copy(expr2
);
1750 expr1
= introduce_access_relations(expr1
);
1751 expr2
= introduce_access_relations(expr2
);
1752 if (!expr1
|| !expr2
)
1755 is_subset
= isl_union_map_is_subset(
1756 expr1
->acc
.access
[pet_expr_access_may_read
],
1757 expr2
->acc
.access
[pet_expr_access_may_read
]);
1759 pet_expr_free(expr1
);
1760 pet_expr_free(expr2
);
1764 pet_expr_free(expr1
);
1765 pet_expr_free(expr2
);
1769 /* Given a set in the iteration space "domain", extend it to live in the space
1770 * of the domain of access relations.
1772 * That, is the number of arguments "n" is 0, then simply return domain.
1773 * Otherwise, return [domain -> [a_1,...,a_n]].
1775 static __isl_give isl_set
*add_arguments(__isl_take isl_set
*domain
, int n
)
1782 map
= isl_map_from_domain(domain
);
1783 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
1784 return isl_map_wrap(map
);
1787 /* Add extra conditions to the domains of all access relations in "expr",
1788 * introducing access relations if they are not already present.
1790 * The conditions are not added to the index expression. Instead, they
1791 * are used to try and simplify the index expression.
1793 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1794 __isl_take isl_set
*cond
)
1797 isl_union_set
*uset
;
1798 enum pet_expr_access_type type
;
1800 expr
= pet_expr_cow(expr
);
1804 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1805 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1806 isl_set_copy(cond
));
1811 if (expr
->type
!= pet_expr_access
) {
1816 expr
= introduce_access_relations(expr
);
1820 cond
= add_arguments(cond
, expr
->n_arg
);
1821 uset
= isl_union_set_from_set(isl_set_copy(cond
));
1822 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1823 if (!expr
->acc
.access
[type
])
1825 expr
->acc
.access
[type
] =
1826 isl_union_map_intersect_domain(expr
->acc
.access
[type
],
1827 isl_union_set_copy(uset
));
1828 if (!expr
->acc
.access
[type
])
1831 isl_union_set_free(uset
);
1832 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, cond
);
1833 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1834 return pet_expr_free(expr
);
1839 return pet_expr_free(expr
);
1842 /* Modify the access relations (if any) and index expression
1843 * of the given access expression
1844 * based on the given iteration space transformation.
1845 * In particular, precompose the access relation and index expression
1846 * with the update function.
1848 * If the access has any arguments then the domain of the access relation
1849 * is a wrapped mapping from the iteration space to the space of
1850 * argument values. We only need to change the domain of this wrapped
1851 * mapping, so we extend the input transformation with an identity mapping
1852 * on the space of argument values.
1854 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1855 __isl_keep isl_multi_pw_aff
*update
)
1857 enum pet_expr_access_type type
;
1859 expr
= pet_expr_cow(expr
);
1862 if (expr
->type
!= pet_expr_access
)
1863 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1864 "not an access expression", return pet_expr_free(expr
));
1866 update
= isl_multi_pw_aff_copy(update
);
1868 if (expr
->n_arg
> 0) {
1870 isl_multi_pw_aff
*id
;
1872 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1873 space
= isl_space_domain(space
);
1874 space
= isl_space_unwrap(space
);
1875 space
= isl_space_range(space
);
1876 space
= isl_space_map_from_set(space
);
1877 id
= isl_multi_pw_aff_identity(space
);
1878 update
= isl_multi_pw_aff_product(update
, id
);
1881 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1882 if (!expr
->acc
.access
[type
])
1884 expr
->acc
.access
[type
] =
1885 isl_union_map_preimage_domain_multi_pw_aff(
1886 expr
->acc
.access
[type
],
1887 isl_multi_pw_aff_copy(update
));
1888 if (!expr
->acc
.access
[type
])
1891 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1892 expr
->acc
.index
, update
);
1893 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1894 return pet_expr_free(expr
);
1899 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1901 isl_multi_pw_aff
*update
= user
;
1903 return pet_expr_access_update_domain(expr
, update
);
1906 /* Modify all access relations in "expr" by precomposing them with
1907 * the given iteration space transformation.
1909 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1910 __isl_take isl_multi_pw_aff
*update
)
1912 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1913 isl_multi_pw_aff_free(update
);
1917 /* Given an expression with accesses that have a 0D anonymous domain,
1918 * replace those domains by "space".
1920 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1921 __isl_take isl_space
*space
)
1923 isl_multi_pw_aff
*mpa
;
1925 space
= isl_space_from_domain(space
);
1926 mpa
= isl_multi_pw_aff_zero(space
);
1927 return pet_expr_update_domain(expr
, mpa
);
1930 /* Add all parameters in "space" to the access relations (if any)
1931 * and index expression of "expr".
1933 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1935 isl_space
*space
= user
;
1936 enum pet_expr_access_type type
;
1938 expr
= pet_expr_cow(expr
);
1941 if (expr
->type
!= pet_expr_access
)
1942 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1943 "not an access expression", return pet_expr_free(expr
));
1945 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1946 if (!expr
->acc
.access
[type
])
1948 expr
->acc
.access
[type
] =
1949 isl_union_map_align_params(expr
->acc
.access
[type
],
1950 isl_space_copy(space
));
1951 if (!expr
->acc
.access
[type
])
1954 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1955 isl_space_copy(space
));
1956 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1957 return pet_expr_free(expr
);
1962 /* Add all parameters in "space" to all access relations and index expressions
1965 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1966 __isl_take isl_space
*space
)
1968 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1969 isl_space_free(space
);
1973 /* Insert an argument expression corresponding to "test" in front
1974 * of the list of arguments described by *n_arg and *args.
1976 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1977 __isl_keep isl_multi_pw_aff
*test
)
1980 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1983 return pet_expr_free(expr
);
1984 expr
= pet_expr_cow(expr
);
1989 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1991 return pet_expr_free(expr
);
1994 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1996 return pet_expr_free(expr
);
1997 for (i
= 0; i
< expr
->n_arg
; ++i
)
1998 ext
[1 + i
] = expr
->args
[i
];
2003 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
2005 return pet_expr_free(expr
);
2010 /* Make the expression "expr" depend on the value of "test"
2011 * being equal to "satisfied".
2013 * If "test" is an affine expression, we simply add the conditions
2014 * on the expression having the value "satisfied" to all access relations
2015 * (introducing access relations if they are missing) and index expressions.
2017 * Otherwise, we add a filter to "expr" (which is then assumed to be
2018 * an access expression) corresponding to "test" being equal to "satisfied".
2020 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
2021 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
2026 isl_pw_multi_aff
*pma
;
2027 enum pet_expr_access_type type
;
2029 expr
= pet_expr_cow(expr
);
2033 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
2037 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
2038 isl_multi_pw_aff_free(test
);
2040 cond
= isl_pw_aff_non_zero_set(pa
);
2042 cond
= isl_pw_aff_zero_set(pa
);
2043 return pet_expr_restrict(expr
, cond
);
2046 ctx
= isl_multi_pw_aff_get_ctx(test
);
2047 if (expr
->type
!= pet_expr_access
)
2048 isl_die(ctx
, isl_error_invalid
,
2049 "can only filter access expressions", goto error
);
2051 expr
= introduce_access_relations(expr
);
2055 space
= isl_space_domain(isl_multi_pw_aff_get_space(expr
->acc
.index
));
2056 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
2057 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
2059 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2060 if (!expr
->acc
.access
[type
])
2062 expr
->acc
.access
[type
] =
2063 isl_union_map_preimage_domain_pw_multi_aff(
2064 expr
->acc
.access
[type
],
2065 isl_pw_multi_aff_copy(pma
));
2066 if (!expr
->acc
.access
[type
])
2069 pma
= isl_pw_multi_aff_gist(pma
,
2070 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
2071 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
2072 expr
->acc
.index
, pma
);
2073 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2076 expr
= insert_access_arg(expr
, test
);
2078 isl_multi_pw_aff_free(test
);
2081 isl_multi_pw_aff_free(test
);
2082 return pet_expr_free(expr
);
2085 /* Add a reference identifier to access expression "expr".
2086 * "user" points to an integer that contains the sequence number
2087 * of the next reference.
2089 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
2096 expr
= pet_expr_cow(expr
);
2099 if (expr
->type
!= pet_expr_access
)
2100 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2101 "not an access expression", return pet_expr_free(expr
));
2103 ctx
= pet_expr_get_ctx(expr
);
2104 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
2105 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
2106 if (!expr
->acc
.ref_id
)
2107 return pet_expr_free(expr
);
2112 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
2114 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
2117 /* Reset the user pointer on all parameter and tuple ids in
2118 * the access relations (if any) and the index expression
2119 * of the access expression "expr".
2121 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
2124 enum pet_expr_access_type type
;
2126 expr
= pet_expr_cow(expr
);
2129 if (expr
->type
!= pet_expr_access
)
2130 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2131 "not an access expression", return pet_expr_free(expr
));
2133 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2134 if (!expr
->acc
.access
[type
])
2136 expr
->acc
.access
[type
] =
2137 isl_union_map_reset_user(expr
->acc
.access
[type
]);
2138 if (!expr
->acc
.access
[type
])
2141 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
2142 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2143 return pet_expr_free(expr
);
2148 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
2150 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
2153 /* Data used in access_gist() callback.
2155 struct pet_access_gist_data
{
2157 isl_union_map
*value_bounds
;
2160 /* Given an expression "expr" of type pet_expr_access, compute
2161 * the gist of the associated access relations (if any) and index expression
2162 * with respect to data->domain and the bounds on the values of the arguments
2163 * of the expression.
2165 * The arguments of "expr" have been gisted right before "expr" itself
2166 * is gisted. The gisted arguments may have become equal where before
2167 * they may not have been (obviously) equal. We therefore take
2168 * the opportunity to remove duplicate arguments here.
2170 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
2172 struct pet_access_gist_data
*data
= user
;
2174 isl_union_set
*uset
;
2175 enum pet_expr_access_type type
;
2177 expr
= pet_expr_remove_duplicate_args(expr
);
2178 expr
= pet_expr_cow(expr
);
2181 if (expr
->type
!= pet_expr_access
)
2182 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2183 "not an access expression", return pet_expr_free(expr
));
2185 domain
= isl_set_copy(data
->domain
);
2186 if (expr
->n_arg
> 0)
2187 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
2188 data
->value_bounds
);
2190 uset
= isl_union_set_from_set(isl_set_copy(domain
));
2191 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2192 if (!expr
->acc
.access
[type
])
2194 expr
->acc
.access
[type
] =
2195 isl_union_map_gist_domain(expr
->acc
.access
[type
],
2196 isl_union_set_copy(uset
));
2197 if (!expr
->acc
.access
[type
])
2200 isl_union_set_free(uset
);
2201 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
2202 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2203 return pet_expr_free(expr
);
2208 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
2209 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2211 struct pet_access_gist_data data
= { context
, value_bounds
};
2213 return pet_expr_map_access(expr
, &access_gist
, &data
);
2216 /* Mark "expr" as a read dependening on "read".
2218 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
2222 return pet_expr_free(expr
);
2223 if (expr
->type
!= pet_expr_access
)
2224 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2225 "not an access expression", return pet_expr_free(expr
));
2226 if (expr
->acc
.read
== read
)
2228 expr
= pet_expr_cow(expr
);
2231 expr
->acc
.read
= read
;
2236 /* Mark "expr" as a write dependening on "write".
2238 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
2242 return pet_expr_free(expr
);
2243 if (expr
->type
!= pet_expr_access
)
2244 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2245 "not an access expression", return pet_expr_free(expr
));
2246 if (expr
->acc
.write
== write
)
2248 expr
= pet_expr_cow(expr
);
2251 expr
->acc
.write
= write
;
2256 /* Mark "expr" as a kill dependening on "kill".
2258 __isl_give pet_expr
*pet_expr_access_set_kill(__isl_take pet_expr
*expr
,
2262 return pet_expr_free(expr
);
2263 if (expr
->type
!= pet_expr_access
)
2264 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2265 "not an access expression", return pet_expr_free(expr
));
2266 if (expr
->acc
.kill
== kill
)
2268 expr
= pet_expr_cow(expr
);
2271 expr
->acc
.kill
= kill
;
2276 /* Map the access type "type" to the corresponding location
2277 * in the access array.
2278 * In particular, the access relation of type pet_expr_access_killed is
2279 * stored in the element at position pet_expr_access_fake_killed.
2281 static enum pet_expr_access_type
internalize_type(
2282 enum pet_expr_access_type type
)
2284 if (type
== pet_expr_access_killed
)
2285 return pet_expr_access_fake_killed
;
2289 /* Replace the access relation of the given "type" of "expr" by "access".
2290 * If the access relation is non-empty and the type is a read or a write,
2291 * then also mark the access expression itself as a read or a write.
2293 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
2294 enum pet_expr_access_type type
, __isl_take isl_union_map
*access
)
2298 expr
= pet_expr_cow(expr
);
2299 if (!expr
|| !access
)
2301 if (expr
->type
!= pet_expr_access
)
2302 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2303 "not an access expression", goto error
);
2304 type
= internalize_type(type
);
2305 isl_union_map_free(expr
->acc
.access
[type
]);
2306 expr
->acc
.access
[type
] = access
;
2311 empty
= isl_union_map_is_empty(access
);
2313 return pet_expr_free(expr
);
2317 if (type
== pet_expr_access_may_read
)
2318 expr
= pet_expr_access_set_read(expr
, 1);
2320 expr
= pet_expr_access_set_write(expr
, 1);
2324 isl_union_map_free(access
);
2325 pet_expr_free(expr
);
2329 /* Replace the index expression of "expr" by "index" and
2330 * set the array depth accordingly.
2332 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
2333 __isl_take isl_multi_pw_aff
*index
)
2335 expr
= pet_expr_cow(expr
);
2336 if (!expr
|| !index
)
2338 if (expr
->type
!= pet_expr_access
)
2339 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2340 "not an access expression", goto error
);
2341 isl_multi_pw_aff_free(expr
->acc
.index
);
2342 expr
->acc
.index
= index
;
2343 expr
->acc
.depth
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
2347 isl_multi_pw_aff_free(index
);
2348 pet_expr_free(expr
);
2352 /* Return the reference identifier of access expression "expr".
2354 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
2358 if (expr
->type
!= pet_expr_access
)
2359 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2360 "not an access expression", return NULL
);
2362 return isl_id_copy(expr
->acc
.ref_id
);
2365 /* Replace the reference identifier of access expression "expr" by "ref_id".
2367 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
2368 __isl_take isl_id
*ref_id
)
2370 expr
= pet_expr_cow(expr
);
2371 if (!expr
|| !ref_id
)
2373 if (expr
->type
!= pet_expr_access
)
2374 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2375 "not an access expression", goto error
);
2376 isl_id_free(expr
->acc
.ref_id
);
2377 expr
->acc
.ref_id
= ref_id
;
2381 isl_id_free(ref_id
);
2382 pet_expr_free(expr
);
2386 /* Tag the access relation "access" with "id".
2387 * That is, insert the id as the range of a wrapped relation
2388 * in the domain of "access".
2390 * If "access" is of the form
2394 * then the result is of the form
2396 * [D[i] -> id[]] -> A[a]
2398 __isl_give isl_union_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
2399 __isl_take isl_union_map
*access
)
2402 isl_multi_aff
*add_tag
;
2405 if (expr
->type
!= pet_expr_access
)
2406 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2407 "not an access expression",
2408 return isl_union_map_free(access
));
2410 id
= isl_id_copy(expr
->acc
.ref_id
);
2411 space
= pet_expr_access_get_domain_space(expr
);
2412 space
= isl_space_from_domain(space
);
2413 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2414 add_tag
= isl_multi_aff_domain_map(space
);
2415 access
= isl_union_map_preimage_domain_multi_aff(access
, add_tag
);
2420 /* Return the access relation of the given "type" associated to "expr"
2421 * that maps pairs of domain iterations and argument values
2422 * to the corresponding accessed data elements.
2424 * If the requested access relation is explicitly available,
2425 * then return a copy. Otherwise, check if it is irrelevant for
2426 * the access expression and return an empty relation if this is the case.
2427 * Otherwise, introduce the requested access relation in "expr" and
2430 __isl_give isl_union_map
*pet_expr_access_get_dependent_access(
2431 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2433 isl_union_map
*access
;
2438 if (expr
->type
!= pet_expr_access
)
2439 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2440 "not an access expression", return NULL
);
2442 type
= internalize_type(type
);
2443 if (expr
->acc
.access
[type
])
2444 return isl_union_map_copy(expr
->acc
.access
[type
]);
2446 if (type
== pet_expr_access_may_read
)
2447 empty
= !expr
->acc
.read
;
2449 empty
= !expr
->acc
.write
;
2452 expr
= pet_expr_copy(expr
);
2453 expr
= introduce_access_relations(expr
);
2456 access
= isl_union_map_copy(expr
->acc
.access
[type
]);
2457 pet_expr_free(expr
);
2462 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr
));
2465 /* Return the may read access relation associated to "expr"
2466 * that maps pairs of domain iterations and argument values
2467 * to the corresponding accessed data elements.
2469 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_read(
2470 __isl_keep pet_expr
*expr
)
2472 return pet_expr_access_get_dependent_access(expr
,
2473 pet_expr_access_may_read
);
2476 /* Return the may write access relation associated to "expr"
2477 * that maps pairs of domain iterations and argument values
2478 * to the corresponding accessed data elements.
2480 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_write(
2481 __isl_keep pet_expr
*expr
)
2483 return pet_expr_access_get_dependent_access(expr
,
2484 pet_expr_access_may_write
);
2487 /* Return the must write access relation associated to "expr"
2488 * that maps pairs of domain iterations and argument values
2489 * to the corresponding accessed data elements.
2491 __isl_give isl_union_map
*pet_expr_access_get_dependent_must_write(
2492 __isl_keep pet_expr
*expr
)
2494 return pet_expr_access_get_dependent_access(expr
,
2495 pet_expr_access_must_write
);
2498 /* Return the relation of the given "type" mapping domain iterations
2499 * to the accessed data elements.
2500 * In particular, take the access relation and, in case of may_read
2501 * or may_write, project out the values of the arguments, if any.
2502 * In case of must_write, return the empty relation if there are
2505 __isl_give isl_union_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
,
2506 enum pet_expr_access_type type
)
2508 isl_union_map
*access
;
2514 if (expr
->type
!= pet_expr_access
)
2515 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2516 "not an access expression", return NULL
);
2518 if (expr
->n_arg
!= 0 && type
== pet_expr_access_must_write
) {
2519 space
= pet_expr_access_get_parameter_space(expr
);
2520 return isl_union_map_empty(space
);
2523 access
= pet_expr_access_get_dependent_access(expr
, type
);
2524 if (expr
->n_arg
== 0)
2527 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
2528 space
= isl_space_domain(space
);
2529 map
= isl_map_universe(isl_space_unwrap(space
));
2530 map
= isl_map_domain_map(map
);
2531 access
= isl_union_map_apply_domain(access
,
2532 isl_union_map_from_map(map
));
2537 /* Return the relation mapping domain iterations to all possibly
2538 * read data elements.
2540 __isl_give isl_union_map
*pet_expr_access_get_may_read(
2541 __isl_keep pet_expr
*expr
)
2543 return pet_expr_access_get_access(expr
, pet_expr_access_may_read
);
2546 /* Return the relation mapping domain iterations to all possibly
2547 * written data elements.
2549 __isl_give isl_union_map
*pet_expr_access_get_may_write(
2550 __isl_keep pet_expr
*expr
)
2552 return pet_expr_access_get_access(expr
, pet_expr_access_may_write
);
2555 /* Return a relation mapping domain iterations to definitely
2556 * written data elements, assuming the statement containing
2557 * the expression is executed.
2559 __isl_give isl_union_map
*pet_expr_access_get_must_write(
2560 __isl_keep pet_expr
*expr
)
2562 return pet_expr_access_get_access(expr
, pet_expr_access_must_write
);
2565 /* Return the relation of the given "type" mapping domain iterations to
2566 * accessed data elements, with its domain tagged with the reference
2569 static __isl_give isl_union_map
*pet_expr_access_get_tagged_access(
2570 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2572 isl_union_map
*access
;
2577 access
= pet_expr_access_get_access(expr
, type
);
2578 access
= pet_expr_tag_access(expr
, access
);
2583 /* Return the relation mapping domain iterations to all possibly
2584 * read data elements, with its domain tagged with the reference
2587 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_read(
2588 __isl_keep pet_expr
*expr
)
2590 return pet_expr_access_get_tagged_access(expr
,
2591 pet_expr_access_may_read
);
2594 /* Return the relation mapping domain iterations to all possibly
2595 * written data elements, with its domain tagged with the reference
2598 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_write(
2599 __isl_keep pet_expr
*expr
)
2601 return pet_expr_access_get_tagged_access(expr
,
2602 pet_expr_access_may_write
);
2605 /* Return the operation type of operation expression "expr".
2607 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
2611 if (expr
->type
!= pet_expr_op
)
2612 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2613 "not an operation expression", return pet_op_last
);
2618 /* Replace the operation type of operation expression "expr" by "type".
2620 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
2621 enum pet_op_type type
)
2624 return pet_expr_free(expr
);
2625 if (expr
->type
!= pet_expr_op
)
2626 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2627 "not an operation expression",
2628 return pet_expr_free(expr
));
2629 if (expr
->op
== type
)
2631 expr
= pet_expr_cow(expr
);
2639 /* Return the name of the function called by "expr".
2641 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
2645 if (expr
->type
!= pet_expr_call
)
2646 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2647 "not a call expression", return NULL
);
2648 return expr
->c
.name
;
2651 /* Replace the name of the function called by "expr" by "name".
2653 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
2654 __isl_keep
const char *name
)
2656 expr
= pet_expr_cow(expr
);
2658 return pet_expr_free(expr
);
2659 if (expr
->type
!= pet_expr_call
)
2660 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2661 "not a call expression", return pet_expr_free(expr
));
2663 expr
->c
.name
= strdup(name
);
2665 return pet_expr_free(expr
);
2669 /* Does the call expression "expr" have an associated function summary?
2671 int pet_expr_call_has_summary(__isl_keep pet_expr
*expr
)
2675 if (expr
->type
!= pet_expr_call
)
2676 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2677 "not a call expression", return -1);
2679 return expr
->c
.summary
!= NULL
;
2682 /* Return a copy of the function summary associated to
2683 * the call expression "expr".
2685 __isl_give pet_function_summary
*pet_expr_call_get_summary(
2686 __isl_keep pet_expr
*expr
)
2690 if (expr
->type
!= pet_expr_call
)
2691 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2692 "not a call expression", return NULL
);
2694 return pet_function_summary_copy(expr
->c
.summary
);
2697 /* Replace the function summary associated to the call expression "expr"
2700 __isl_give pet_expr
*pet_expr_call_set_summary(__isl_take pet_expr
*expr
,
2701 __isl_take pet_function_summary
*summary
)
2703 expr
= pet_expr_cow(expr
);
2704 if (!expr
|| !summary
)
2706 if (expr
->type
!= pet_expr_call
)
2707 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2708 "not a call expression", goto error
);
2709 pet_function_summary_free(expr
->c
.summary
);
2710 expr
->c
.summary
= summary
;
2713 pet_function_summary_free(summary
);
2714 return pet_expr_free(expr
);
2717 /* Replace the type of the cast performed by "expr" by "name".
2719 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
2720 __isl_keep
const char *name
)
2722 expr
= pet_expr_cow(expr
);
2724 return pet_expr_free(expr
);
2725 if (expr
->type
!= pet_expr_cast
)
2726 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2727 "not a cast expression", return pet_expr_free(expr
));
2728 free(expr
->type_name
);
2729 expr
->type_name
= strdup(name
);
2730 if (!expr
->type_name
)
2731 return pet_expr_free(expr
);
2735 /* Return the value of the integer represented by "expr".
2737 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
2741 if (expr
->type
!= pet_expr_int
)
2742 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2743 "not an int expression", return NULL
);
2745 return isl_val_copy(expr
->i
);
2748 /* Replace the value of the integer represented by "expr" by "v".
2750 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
2751 __isl_take isl_val
*v
)
2753 expr
= pet_expr_cow(expr
);
2756 if (expr
->type
!= pet_expr_int
)
2757 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2758 "not an int expression", goto error
);
2759 isl_val_free(expr
->i
);
2765 pet_expr_free(expr
);
2769 /* Replace the value and string representation of the double
2770 * represented by "expr" by "d" and "s".
2772 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2773 double d
, __isl_keep
const char *s
)
2775 expr
= pet_expr_cow(expr
);
2777 return pet_expr_free(expr
);
2778 if (expr
->type
!= pet_expr_double
)
2779 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2780 "not a double expression", return pet_expr_free(expr
));
2783 expr
->d
.s
= strdup(s
);
2785 return pet_expr_free(expr
);
2789 /* Return a string representation of the double expression "expr".
2791 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2795 if (expr
->type
!= pet_expr_double
)
2796 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2797 "not a double expression", return NULL
);
2798 return strdup(expr
->d
.s
);
2801 /* Return a piecewise affine expression defined on the specified domain
2802 * that represents NaN.
2804 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2806 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2809 /* This function is called when we come across an access that is
2810 * nested in what is supposed to be an affine expression.
2811 * "pc" is the context in which the affine expression is created.
2812 * If nesting is allowed in "pc", we return an affine expression that is
2813 * equal to a new parameter corresponding to this nested access.
2814 * Otherwise, we return NaN.
2816 * Note that we currently don't allow nested accesses themselves
2817 * to contain any nested accesses, so we check if "expr" itself
2818 * involves any nested accesses (either explicitly as arguments
2819 * or implicitly through parameters) and return NaN if it does.
2821 * The new parameter is resolved in resolve_nested.
2823 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2824 __isl_keep pet_context
*pc
)
2829 isl_local_space
*ls
;
2835 if (!pet_context_allow_nesting(pc
))
2836 return non_affine(pet_context_get_space(pc
));
2838 if (pet_expr_get_type(expr
) != pet_expr_access
)
2839 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2840 "not an access expression", return NULL
);
2842 if (expr
->n_arg
> 0)
2843 return non_affine(pet_context_get_space(pc
));
2845 space
= pet_expr_access_get_parameter_space(expr
);
2846 nested
= pet_nested_any_in_space(space
);
2847 isl_space_free(space
);
2849 return non_affine(pet_context_get_space(pc
));
2851 ctx
= pet_expr_get_ctx(expr
);
2852 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2853 space
= pet_context_get_space(pc
);
2854 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2856 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2857 ls
= isl_local_space_from_space(space
);
2858 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2860 return isl_pw_aff_from_aff(aff
);
2863 /* Extract an affine expression from the access pet_expr "expr".
2864 * "pc" is the context in which the affine expression is created.
2866 * If "expr" is actually an affine expression rather than
2867 * a real access, then we return that expression.
2868 * Otherwise, we require that "expr" is of an integral type.
2869 * If not, we return NaN.
2871 * If the variable has been assigned a known affine expression,
2872 * then we return that expression.
2874 * Otherwise, we return an expression that is equal to a parameter
2875 * representing "expr" (if "allow_nested" is set).
2877 static __isl_give isl_pw_aff
*extract_affine_from_access(
2878 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2882 if (pet_expr_is_affine(expr
))
2883 return pet_expr_get_affine(expr
);
2885 if (pet_expr_get_type_size(expr
) == 0)
2886 return non_affine(pet_context_get_space(pc
));
2888 if (!pet_expr_is_scalar_access(expr
))
2889 return nested_access(expr
, pc
);
2891 id
= pet_expr_access_get_id(expr
);
2892 if (pet_context_is_assigned(pc
, id
))
2893 return pet_context_get_value(pc
, id
);
2896 return nested_access(expr
, pc
);
2899 /* Construct an affine expression from the integer constant "expr".
2900 * "pc" is the context in which the affine expression is created.
2902 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2903 __isl_keep pet_context
*pc
)
2905 isl_local_space
*ls
;
2911 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2912 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2914 return isl_pw_aff_from_aff(aff
);
2917 /* Extract an affine expression from an addition or subtraction operation.
2918 * Return NaN if we are unable to extract an affine expression.
2920 * "pc" is the context in which the affine expression is created.
2922 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2923 __isl_keep pet_context
*pc
)
2930 if (expr
->n_arg
!= 2)
2931 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2932 "expecting two arguments", return NULL
);
2934 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2935 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2937 switch (pet_expr_op_get_type(expr
)) {
2939 return isl_pw_aff_add(lhs
, rhs
);
2941 return isl_pw_aff_sub(lhs
, rhs
);
2943 isl_pw_aff_free(lhs
);
2944 isl_pw_aff_free(rhs
);
2945 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2946 "not an addition or subtraction operation",
2952 /* Extract an affine expression from an integer division or a modulo operation.
2953 * Return NaN if we are unable to extract an affine expression.
2955 * "pc" is the context in which the affine expression is created.
2957 * In particular, if "expr" is lhs/rhs, then return
2959 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2961 * If "expr" is lhs%rhs, then return
2963 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2965 * If the second argument (rhs) is not a (positive) integer constant,
2966 * then we fail to extract an affine expression.
2968 * We simplify the result in the context of the domain of "pc" in case
2969 * this domain implies that lhs >= 0 (or < 0).
2971 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2972 __isl_keep pet_context
*pc
)
2981 if (expr
->n_arg
!= 2)
2982 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2983 "expecting two arguments", return NULL
);
2985 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2987 is_cst
= isl_pw_aff_is_cst(rhs
);
2988 if (is_cst
< 0 || !is_cst
) {
2989 isl_pw_aff_free(rhs
);
2990 return non_affine(pet_context_get_space(pc
));
2993 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2995 switch (pet_expr_op_get_type(expr
)) {
2997 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
3000 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
3003 isl_pw_aff_free(lhs
);
3004 isl_pw_aff_free(rhs
);
3005 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
3006 "not a div or mod operator", return NULL
);
3009 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
3012 /* Extract an affine expression from a multiplication operation.
3013 * Return NaN if we are unable to extract an affine expression.
3014 * In particular, if neither of the arguments is a (piecewise) constant
3015 * then we return NaN.
3017 * "pc" is the context in which the affine expression is created.
3019 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
3020 __isl_keep pet_context
*pc
)
3022 int lhs_cst
, rhs_cst
;
3028 if (expr
->n_arg
!= 2)
3029 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3030 "expecting two arguments", return NULL
);
3032 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
3033 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
3035 lhs_cst
= isl_pw_aff_is_cst(lhs
);
3036 rhs_cst
= isl_pw_aff_is_cst(rhs
);
3037 if (lhs_cst
>= 0 && rhs_cst
>= 0 && (lhs_cst
|| rhs_cst
))
3038 return isl_pw_aff_mul(lhs
, rhs
);
3040 isl_pw_aff_free(lhs
);
3041 isl_pw_aff_free(rhs
);
3043 if (lhs_cst
< 0 || rhs_cst
< 0)
3046 return non_affine(pet_context_get_space(pc
));
3049 /* Extract an affine expression from a negation operation.
3050 * Return NaN if we are unable to extract an affine expression.
3052 * "pc" is the context in which the affine expression is created.
3054 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
3055 __isl_keep pet_context
*pc
)
3061 if (expr
->n_arg
!= 1)
3062 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3063 "expecting one argument", return NULL
);
3065 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
3066 return isl_pw_aff_neg(res
);
3069 /* Extract an affine expression from a conditional operation.
3070 * Return NaN if we are unable to extract an affine expression.
3072 * "pc" is the context in which the affine expression is created.
3074 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
3075 __isl_keep pet_context
*pc
)
3077 isl_pw_aff
*cond
, *lhs
, *rhs
;
3081 if (expr
->n_arg
!= 3)
3082 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3083 "expecting three arguments", return NULL
);
3085 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3086 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
3087 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
3089 return isl_pw_aff_cond(cond
, lhs
, rhs
);
3092 /* Limit the domain of "pwaff" to those elements where the function
3095 * 2^{width-1} <= pwaff < 2^{width-1}
3097 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
3102 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
3103 isl_local_space
*ls
= isl_local_space_from_space(space
);
3108 ctx
= isl_pw_aff_get_ctx(pwaff
);
3109 v
= isl_val_int_from_ui(ctx
, width
- 1);
3110 v
= isl_val_2exp(v
);
3112 bound
= isl_aff_zero_on_domain(ls
);
3113 bound
= isl_aff_add_constant_val(bound
, v
);
3114 b
= isl_pw_aff_from_aff(bound
);
3116 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
3117 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3119 b
= isl_pw_aff_neg(b
);
3120 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
3121 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3126 /* Handle potential overflows on signed computations.
3128 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
3129 * then we adjust the domain of "pa" to avoid overflows.
3131 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
3135 struct pet_options
*options
;
3140 ctx
= isl_pw_aff_get_ctx(pa
);
3141 options
= isl_ctx_peek_pet_options(ctx
);
3142 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
3143 pa
= avoid_overflow(pa
, width
);
3148 /* Extract an affine expression from some an operation.
3149 * Return NaN if we are unable to extract an affine expression.
3150 * If the result of a binary (non boolean) operation is unsigned,
3151 * then we wrap it based on the size of the type. If the result is signed,
3152 * then we ensure that no overflow occurs.
3154 * "pc" is the context in which the affine expression is created.
3156 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
3157 __isl_keep pet_context
*pc
)
3162 switch (pet_expr_op_get_type(expr
)) {
3165 res
= extract_affine_add_sub(expr
, pc
);
3169 res
= extract_affine_div_mod(expr
, pc
);
3172 res
= extract_affine_mul(expr
, pc
);
3175 return extract_affine_neg(expr
, pc
);
3177 return extract_affine_cond(expr
, pc
);
3187 return pet_expr_extract_affine_condition(expr
, pc
);
3189 return non_affine(pet_context_get_space(pc
));
3194 if (isl_pw_aff_involves_nan(res
)) {
3195 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
3196 isl_pw_aff_free(res
);
3197 return non_affine(space
);
3200 type_size
= pet_expr_get_type_size(expr
);
3202 res
= pet_wrap_pw_aff(res
, type_size
);
3204 res
= signed_overflow(res
, -type_size
);
3209 /* Internal data structure for affine builtin function declarations.
3211 * "pencil" is set if the builtin is pencil specific.
3212 * "n_args" is the number of arguments the function takes.
3213 * "name" is the function name.
3215 struct affine_builtin_decl
{
3221 static struct affine_builtin_decl affine_builtins
[] = {
3229 { 0, 2, "intFloor" },
3230 { 0, 2, "intCeil" },
3235 /* List of min and max builtin functions.
3237 static const char *min_max_builtins
[] = {
3238 "min", "imin", "umin",
3239 "max", "imax", "umax"
3242 /* Is a function call to "name" with "n_args" arguments a call to a
3243 * builtin function for which we can construct an affine expression?
3244 * pencil specific builtins are only recognized if "pencil" is set.
3246 static int is_affine_builtin(int pencil
, int n_args
, const char *name
)
3250 for (i
= 0; i
< ARRAY_SIZE(affine_builtins
); ++i
) {
3251 struct affine_builtin_decl
*decl
= &affine_builtins
[i
];
3253 if (decl
->pencil
&& !pencil
)
3255 if (decl
->n_args
== n_args
&& !strcmp(decl
->name
, name
))
3262 /* Is function "name" a known min or max builtin function?
3264 static int is_min_or_max_builtin(const char *name
)
3268 for (i
= 0; i
< ARRAY_SIZE(min_max_builtins
); ++i
)
3269 if (!strcmp(min_max_builtins
[i
], name
))
3275 /* Extract an affine expression from some special function calls.
3276 * Return NaN if we are unable to extract an affine expression.
3277 * In particular, we handle "min", "max", "ceild", "floord",
3278 * "intMod", "intFloor" and "intCeil".
3279 * In case of the latter five, the second argument needs to be
3280 * a (positive) integer constant.
3281 * If the pencil option is set, then we also handle "{i,u}min" and
3284 * "pc" is the context in which the affine expression is created.
3286 static __isl_give isl_pw_aff
*extract_affine_from_call(
3287 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3290 isl_pw_aff
*aff1
, *aff2
;
3293 struct pet_options
*options
;
3297 ctx
= pet_expr_get_ctx(expr
);
3298 options
= isl_ctx_peek_pet_options(ctx
);
3300 n
= pet_expr_get_n_arg(expr
);
3301 name
= pet_expr_call_get_name(expr
);
3302 if (!is_affine_builtin(options
->pencil
, n
, name
))
3303 return non_affine(pet_context_get_space(pc
));
3305 if (is_min_or_max_builtin(name
)) {
3306 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3307 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
3309 if (strstr(name
, "min"))
3310 aff1
= isl_pw_aff_min(aff1
, aff2
);
3312 aff1
= isl_pw_aff_max(aff1
, aff2
);
3313 } else if (!strcmp(name
, "intMod")) {
3316 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3317 return non_affine(pet_context_get_space(pc
));
3318 v
= pet_expr_int_get_val(expr
->args
[1]);
3319 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3320 aff1
= isl_pw_aff_mod_val(aff1
, v
);
3324 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3325 return non_affine(pet_context_get_space(pc
));
3326 v
= pet_expr_int_get_val(expr
->args
[1]);
3327 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3328 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
3329 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
3330 aff1
= isl_pw_aff_floor(aff1
);
3332 aff1
= isl_pw_aff_ceil(aff1
);
3338 /* Extract an affine expression from "expr", if possible.
3339 * Otherwise return NaN.
3341 * "pc" is the context in which the affine expression is created.
3343 * Store the result in "pc" such that it can be reused in case
3344 * pet_expr_extract_affine is called again on the same pair of
3347 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
3348 __isl_keep pet_context
*pc
)
3350 isl_maybe_isl_pw_aff m
;
3356 m
= pet_context_get_extracted_affine(pc
, expr
);
3357 if (m
.valid
< 0 || m
.valid
)
3360 switch (pet_expr_get_type(expr
)) {
3361 case pet_expr_access
:
3362 pa
= extract_affine_from_access(expr
, pc
);
3365 pa
= extract_affine_from_int(expr
, pc
);
3368 pa
= extract_affine_from_op(expr
, pc
);
3371 pa
= extract_affine_from_call(expr
, pc
);
3374 case pet_expr_double
:
3375 case pet_expr_error
:
3376 pa
= non_affine(pet_context_get_space(pc
));
3380 if (pet_context_set_extracted_affine(pc
, expr
, pa
) < 0)
3381 return isl_pw_aff_free(pa
);
3386 /* Extract an affine expressions representing the comparison "LHS op RHS"
3387 * Return NaN if we are unable to extract such an affine expression.
3389 * "pc" is the context in which the affine expression is created.
3391 * If the comparison is of the form
3395 * then the expression is constructed as the conjunction of
3400 * A similar optimization is performed for max(a,b) <= c.
3401 * We do this because that will lead to simpler representations
3402 * of the expression.
3403 * If isl is ever enhanced to explicitly deal with min and max expressions,
3404 * this optimization can be removed.
3406 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
3407 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
3408 __isl_keep pet_context
*pc
)
3410 isl_pw_aff
*lhs_pa
, *rhs_pa
;
3412 if (op
== pet_op_gt
)
3413 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
3414 if (op
== pet_op_ge
)
3415 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
3417 if (op
== pet_op_lt
|| op
== pet_op_le
) {
3418 if (pet_expr_is_min(rhs
)) {
3419 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3421 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3423 return pet_and(lhs_pa
, rhs_pa
);
3425 if (pet_expr_is_max(lhs
)) {
3426 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
3428 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
3430 return pet_and(lhs_pa
, rhs_pa
);
3434 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
3435 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
3437 return pet_comparison(op
, lhs_pa
, rhs_pa
);
3440 /* Extract an affine expressions from the comparison "expr".
3441 * Return NaN if we are unable to extract such an affine expression.
3443 * "pc" is the context in which the affine expression is created.
3445 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
3446 __isl_keep pet_context
*pc
)
3448 enum pet_op_type type
;
3452 if (expr
->n_arg
!= 2)
3453 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3454 "expecting two arguments", return NULL
);
3456 type
= pet_expr_op_get_type(expr
);
3457 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
3461 /* Extract an affine expression representing the boolean operation
3462 * expressed by "expr".
3463 * Return NaN if we are unable to extract an affine expression.
3465 * "pc" is the context in which the affine expression is created.
3467 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
3468 __isl_keep pet_context
*pc
)
3470 isl_pw_aff
*lhs
, *rhs
;
3476 n
= pet_expr_get_n_arg(expr
);
3477 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3479 return pet_not(lhs
);
3481 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
3482 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
3485 /* Extract the affine expression "expr != 0 ? 1 : 0".
3486 * Return NaN if we are unable to extract an affine expression.
3488 * "pc" is the context in which the affine expression is created.
3490 static __isl_give isl_pw_aff
*extract_implicit_condition(
3491 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3495 res
= pet_expr_extract_affine(expr
, pc
);
3496 return pet_to_bool(res
);
3499 /* Extract a boolean affine expression from "expr".
3500 * Return NaN if we are unable to extract an affine expression.
3502 * "pc" is the context in which the affine expression is created.
3504 * If "expr" is neither a comparison nor a boolean operation,
3505 * then we assume it is an affine expression and return the
3506 * boolean expression "expr != 0 ? 1 : 0".
3508 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
3509 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3514 if (pet_expr_is_comparison(expr
))
3515 return extract_comparison(expr
, pc
);
3516 if (pet_expr_is_boolean(expr
))
3517 return extract_boolean(expr
, pc
);
3519 return extract_implicit_condition(expr
, pc
);
3522 /* Check if "expr" is an assume expression and if its single argument
3523 * can be converted to an affine expression in the context of "pc".
3524 * If so, replace the argument by the affine expression.
3526 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
3527 __isl_keep pet_context
*pc
)
3530 isl_multi_pw_aff
*index
;
3534 if (!pet_expr_is_assume(expr
))
3536 if (expr
->n_arg
!= 1)
3537 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3538 "expecting one argument", return pet_expr_free(expr
));
3540 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3542 return pet_expr_free(expr
);
3543 if (isl_pw_aff_involves_nan(cond
)) {
3544 isl_pw_aff_free(cond
);
3548 index
= isl_multi_pw_aff_from_pw_aff(cond
);
3549 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
3554 /* Return the number of bits needed to represent the type of "expr".
3555 * See the description of the type_size field of pet_expr.
3557 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
3559 return expr
? expr
->type_size
: 0;
3562 /* Replace the number of bits needed to represent the type of "expr"
3564 * See the description of the type_size field of pet_expr.
3566 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
3569 expr
= pet_expr_cow(expr
);
3573 expr
->type_size
= type_size
;
3578 /* Extend an access expression "expr" with an additional index "index".
3579 * In particular, add "index" as an extra argument to "expr" and
3580 * adjust the index expression of "expr" to refer to this extra argument.
3581 * The caller is responsible for calling pet_expr_access_set_depth
3582 * to update the corresponding access relation.
3584 * Note that we only collect the individual index expressions as
3585 * arguments of "expr" here.
3586 * An attempt to integrate them into the index expression of "expr"
3587 * is performed in pet_expr_access_plug_in_args.
3589 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
3590 __isl_take pet_expr
*index
)
3594 isl_local_space
*ls
;
3597 expr
= pet_expr_cow(expr
);
3598 if (!expr
|| !index
)
3600 if (expr
->type
!= pet_expr_access
)
3601 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3602 "not an access pet_expr", goto error
);
3604 n
= pet_expr_get_n_arg(expr
);
3605 expr
= pet_expr_insert_arg(expr
, n
, index
);
3609 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3610 ls
= isl_local_space_from_space(space
);
3611 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
3612 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
3613 if (!expr
->acc
.index
)
3614 return pet_expr_free(expr
);
3618 pet_expr_free(expr
);
3619 pet_expr_free(index
);
3623 /* Extend an access expression "expr" with an additional member acces to "id".
3624 * In particular, extend the index expression of "expr" to include
3625 * the additional member access.
3626 * The caller is responsible for calling pet_expr_access_set_depth
3627 * to update the corresponding access relation.
3629 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
3630 __isl_take isl_id
*id
)
3633 isl_multi_pw_aff
*field_access
;
3635 expr
= pet_expr_cow(expr
);
3638 if (expr
->type
!= pet_expr_access
)
3639 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3640 "not an access pet_expr", goto error
);
3642 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3643 space
= isl_space_from_domain(space
);
3644 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
3645 field_access
= isl_multi_pw_aff_zero(space
);
3646 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
3647 if (!expr
->acc
.index
)
3648 return pet_expr_free(expr
);
3652 pet_expr_free(expr
);
3657 /* Prefix the access expression "expr" with "prefix".
3658 * If "add" is set, then it is not the index expression "prefix" itself
3659 * that was passed to the function, but its address.
3661 __isl_give pet_expr
*pet_expr_access_patch(__isl_take pet_expr
*expr
,
3662 __isl_take isl_multi_pw_aff
*prefix
, int add
)
3664 enum pet_expr_access_type type
;
3666 expr
= pet_expr_cow(expr
);
3667 if (!expr
|| !prefix
)
3669 if (expr
->type
!= pet_expr_access
)
3670 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3671 "not an access pet_expr", goto error
);
3673 expr
->acc
.depth
+= isl_multi_pw_aff_dim(prefix
, isl_dim_out
) - add
;
3674 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
3675 if (!expr
->acc
.access
[type
])
3677 expr
->acc
.access
[type
] = pet_patch_union_map(
3678 isl_multi_pw_aff_copy(prefix
), expr
->acc
.access
[type
],
3680 if (!expr
->acc
.access
[type
])
3683 expr
->acc
.index
= pet_patch_multi_pw_aff(prefix
, expr
->acc
.index
, add
);
3684 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
3685 return pet_expr_free(expr
);
3689 pet_expr_free(expr
);
3690 isl_multi_pw_aff_free(prefix
);
3694 /* Dump the arguments of "expr" to "p" as a YAML sequence keyed
3695 * by "args", if there are any such arguments.
3697 static __isl_give isl_printer
*dump_arguments(__isl_keep pet_expr
*expr
,
3698 __isl_take isl_printer
*p
)
3702 if (expr
->n_arg
== 0)
3705 p
= isl_printer_print_str(p
, "args");
3706 p
= isl_printer_yaml_next(p
);
3707 p
= isl_printer_yaml_start_sequence(p
);
3708 for (i
= 0; i
< expr
->n_arg
; ++i
) {
3709 p
= pet_expr_print(expr
->args
[i
], p
);
3710 p
= isl_printer_yaml_next(p
);
3712 p
= isl_printer_yaml_end_sequence(p
);
3717 /* Print "expr" to "p" in YAML format.
3719 __isl_give isl_printer
*pet_expr_print(__isl_keep pet_expr
*expr
,
3720 __isl_take isl_printer
*p
)
3723 return isl_printer_free(p
);
3725 switch (expr
->type
) {
3726 case pet_expr_double
:
3727 p
= isl_printer_print_str(p
, expr
->d
.s
);
3730 p
= isl_printer_print_val(p
, expr
->i
);
3732 case pet_expr_access
:
3733 p
= isl_printer_yaml_start_mapping(p
);
3734 if (expr
->acc
.ref_id
) {
3735 p
= isl_printer_print_str(p
, "ref_id");
3736 p
= isl_printer_yaml_next(p
);
3737 p
= isl_printer_print_id(p
, expr
->acc
.ref_id
);
3738 p
= isl_printer_yaml_next(p
);
3740 p
= isl_printer_print_str(p
, "index");
3741 p
= isl_printer_yaml_next(p
);
3742 p
= isl_printer_print_multi_pw_aff(p
, expr
->acc
.index
);
3743 p
= isl_printer_yaml_next(p
);
3744 p
= isl_printer_print_str(p
, "depth");
3745 p
= isl_printer_yaml_next(p
);
3746 p
= isl_printer_print_int(p
, expr
->acc
.depth
);
3747 p
= isl_printer_yaml_next(p
);
3748 if (expr
->acc
.kill
) {
3749 p
= isl_printer_print_str(p
, "kill");
3750 p
= isl_printer_yaml_next(p
);
3751 p
= isl_printer_print_int(p
, 1);
3752 p
= isl_printer_yaml_next(p
);
3754 p
= isl_printer_print_str(p
, "read");
3755 p
= isl_printer_yaml_next(p
);
3756 p
= isl_printer_print_int(p
, expr
->acc
.read
);
3757 p
= isl_printer_yaml_next(p
);
3758 p
= isl_printer_print_str(p
, "write");
3759 p
= isl_printer_yaml_next(p
);
3760 p
= isl_printer_print_int(p
, expr
->acc
.write
);
3761 p
= isl_printer_yaml_next(p
);
3763 if (expr
->acc
.access
[pet_expr_access_may_read
]) {
3764 p
= isl_printer_print_str(p
, "may_read");
3765 p
= isl_printer_yaml_next(p
);
3766 p
= isl_printer_print_union_map(p
,
3767 expr
->acc
.access
[pet_expr_access_may_read
]);
3768 p
= isl_printer_yaml_next(p
);
3770 if (expr
->acc
.access
[pet_expr_access_may_write
]) {
3771 p
= isl_printer_print_str(p
, "may_write");
3772 p
= isl_printer_yaml_next(p
);
3773 p
= isl_printer_print_union_map(p
,
3774 expr
->acc
.access
[pet_expr_access_may_write
]);
3775 p
= isl_printer_yaml_next(p
);
3777 if (expr
->acc
.access
[pet_expr_access_must_write
]) {
3778 p
= isl_printer_print_str(p
, "must_write");
3779 p
= isl_printer_yaml_next(p
);
3780 p
= isl_printer_print_union_map(p
,
3781 expr
->acc
.access
[pet_expr_access_must_write
]);
3782 p
= isl_printer_yaml_next(p
);
3784 p
= dump_arguments(expr
, p
);
3785 p
= isl_printer_yaml_end_mapping(p
);
3788 p
= isl_printer_yaml_start_mapping(p
);
3789 p
= isl_printer_print_str(p
, "op");
3790 p
= isl_printer_yaml_next(p
);
3791 p
= isl_printer_print_str(p
, op_str
[expr
->op
]);
3792 p
= isl_printer_yaml_next(p
);
3793 p
= dump_arguments(expr
, p
);
3794 p
= isl_printer_yaml_end_mapping(p
);
3797 p
= isl_printer_yaml_start_mapping(p
);
3798 p
= isl_printer_print_str(p
, "call");
3799 p
= isl_printer_yaml_next(p
);
3800 p
= isl_printer_print_str(p
, expr
->c
.name
);
3801 p
= isl_printer_print_str(p
, "/");
3802 p
= isl_printer_print_int(p
, expr
->n_arg
);
3803 p
= isl_printer_yaml_next(p
);
3804 p
= dump_arguments(expr
, p
);
3805 if (expr
->c
.summary
) {
3806 p
= isl_printer_print_str(p
, "summary");
3807 p
= isl_printer_yaml_next(p
);
3808 p
= pet_function_summary_print(expr
->c
.summary
, p
);
3810 p
= isl_printer_yaml_end_mapping(p
);
3813 p
= isl_printer_yaml_start_mapping(p
);
3814 p
= isl_printer_print_str(p
, "cast");
3815 p
= isl_printer_yaml_next(p
);
3816 p
= isl_printer_print_str(p
, expr
->type_name
);
3817 p
= isl_printer_yaml_next(p
);
3818 p
= dump_arguments(expr
, p
);
3819 p
= isl_printer_yaml_end_mapping(p
);
3821 case pet_expr_error
:
3822 p
= isl_printer_print_str(p
, "ERROR");
3829 /* Dump "expr" to stderr with indentation "indent".
3831 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
3838 p
= isl_printer_to_file(pet_expr_get_ctx(expr
), stderr
);
3839 p
= isl_printer_set_indent(p
, indent
);
3840 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_BLOCK
);
3841 p
= isl_printer_start_line(p
);
3842 p
= pet_expr_print(expr
, p
);
3844 isl_printer_free(p
);
3847 void pet_expr_dump(__isl_keep pet_expr
*expr
)
3849 pet_expr_dump_with_indent(expr
, 0);