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
38 #include <isl/union_set.h>
47 #include "value_bounds.h"
50 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
52 static char *type_str
[] = {
53 [pet_expr_access
] = "access",
54 [pet_expr_call
] = "call",
55 [pet_expr_cast
] = "cast",
56 [pet_expr_double
] = "double",
57 [pet_expr_int
] = "int",
61 static char *op_str
[] = {
62 [pet_op_add_assign
] = "+=",
63 [pet_op_sub_assign
] = "-=",
64 [pet_op_mul_assign
] = "*=",
65 [pet_op_div_assign
] = "/=",
66 [pet_op_assign
] = "=",
81 [pet_op_post_inc
] = "++",
82 [pet_op_post_dec
] = "--",
83 [pet_op_pre_inc
] = "++",
84 [pet_op_pre_dec
] = "--",
85 [pet_op_address_of
] = "&",
94 [pet_op_assume
] = "assume",
95 [pet_op_kill
] = "kill"
98 const char *pet_op_str(enum pet_op_type op
)
103 int pet_op_is_inc_dec(enum pet_op_type op
)
105 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
106 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
109 const char *pet_type_str(enum pet_expr_type type
)
111 return type_str
[type
];
114 enum pet_op_type
pet_str_op(const char *str
)
118 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
119 if (!strcmp(op_str
[i
], str
))
125 enum pet_expr_type
pet_str_type(const char *str
)
129 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
130 if (!strcmp(type_str
[i
], str
))
136 /* Construct a pet_expr of the given type.
138 __isl_give pet_expr
*pet_expr_alloc(isl_ctx
*ctx
, enum pet_expr_type type
)
142 expr
= isl_calloc_type(ctx
, struct pet_expr
);
154 /* Construct an access pet_expr from an index expression.
155 * By default, the access is considered to be a read access.
156 * The initial depth is set from the index expression and
157 * may still be updated by the caller before the access relation
160 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
167 ctx
= isl_multi_pw_aff_get_ctx(index
);
168 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
175 expr
= pet_expr_access_set_index(expr
, index
);
179 isl_multi_pw_aff_free(index
);
183 /* Extend the range of "access" with "n" dimensions, retaining
184 * the tuple identifier on this range.
186 * If "access" represents a member access, then extend the range
189 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
193 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
195 if (!isl_map_range_is_wrapping(access
)) {
196 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
200 domain
= isl_map_copy(access
);
201 domain
= isl_map_range_factor_domain(domain
);
202 access
= isl_map_range_factor_range(access
);
203 access
= extend_range(access
, n
);
204 access
= isl_map_range_product(domain
, access
);
207 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
212 /* Does the access expression "expr" have any explicit access relation?
214 isl_bool
pet_expr_access_has_any_access_relation(__isl_keep pet_expr
*expr
)
216 enum pet_expr_access_type type
;
219 return isl_bool_error
;
221 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
)
222 if (expr
->acc
.access
[type
])
223 return isl_bool_true
;
225 return isl_bool_false
;
228 /* Are all relevant access relations explicitly available in "expr"?
230 static int has_relevant_access_relations(__isl_keep pet_expr
*expr
)
232 enum pet_expr_access_type type
;
237 if (expr
->acc
.kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
239 if (expr
->acc
.read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
241 if (expr
->acc
.write
&&
242 (!expr
->acc
.access
[pet_expr_access_may_write
] ||
243 !expr
->acc
.access
[pet_expr_access_must_write
]))
249 /* Replace the depth of the access expr "expr" by "depth".
251 * To avoid inconsistencies between the depth and the access relation,
252 * we currently do not allow the depth to change once the access relation
253 * has been set or computed.
255 __isl_give pet_expr
*pet_expr_access_set_depth(__isl_take pet_expr
*expr
,
263 if (expr
->acc
.depth
== depth
)
265 if (pet_expr_access_has_any_access_relation(expr
))
266 isl_die(pet_expr_get_ctx(expr
), isl_error_unsupported
,
267 "depth cannot be changed after access relation "
268 "has been set or computed", return pet_expr_free(expr
));
270 expr
= pet_expr_cow(expr
);
273 expr
->acc
.depth
= depth
;
278 /* Construct a pet_expr that kills the elements specified by
279 * the index expression "index" and the access relation "access".
281 __isl_give pet_expr
*pet_expr_kill_from_access_and_index(
282 __isl_take isl_map
*access
, __isl_take isl_multi_pw_aff
*index
)
287 if (!access
|| !index
)
290 expr
= pet_expr_from_index(index
);
291 expr
= pet_expr_access_set_read(expr
, 0);
292 expr
= pet_expr_access_set_kill(expr
, 1);
293 depth
= isl_map_dim(access
, isl_dim_out
);
294 expr
= pet_expr_access_set_depth(expr
, depth
);
295 expr
= pet_expr_access_set_access(expr
, pet_expr_access_killed
,
296 isl_union_map_from_map(access
));
297 return pet_expr_new_unary(0, pet_op_kill
, expr
);
299 isl_map_free(access
);
300 isl_multi_pw_aff_free(index
);
304 /* Construct a unary pet_expr that performs "op" on "arg",
305 * where the result is represented using a type of "type_size" bits
306 * (may be zero if unknown or if the type is not an integer).
308 __isl_give pet_expr
*pet_expr_new_unary(int type_size
, enum pet_op_type op
,
309 __isl_take pet_expr
*arg
)
316 ctx
= pet_expr_get_ctx(arg
);
317 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
318 expr
= pet_expr_set_n_arg(expr
, 1);
323 expr
->type_size
= type_size
;
324 expr
->args
[pet_un_arg
] = arg
;
332 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
333 * where the result is represented using a type of "type_size" bits
334 * (may be zero if unknown or if the type is not an integer).
336 __isl_give pet_expr
*pet_expr_new_binary(int type_size
, enum pet_op_type op
,
337 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
344 ctx
= pet_expr_get_ctx(lhs
);
345 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
346 expr
= pet_expr_set_n_arg(expr
, 2);
351 expr
->type_size
= type_size
;
352 expr
->args
[pet_bin_lhs
] = lhs
;
353 expr
->args
[pet_bin_rhs
] = rhs
;
362 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
364 __isl_give pet_expr
*pet_expr_new_ternary(__isl_take pet_expr
*cond
,
365 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
370 if (!cond
|| !lhs
|| !rhs
)
372 ctx
= pet_expr_get_ctx(cond
);
373 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
374 expr
= pet_expr_set_n_arg(expr
, 3);
378 expr
->op
= pet_op_cond
;
379 expr
->args
[pet_ter_cond
] = cond
;
380 expr
->args
[pet_ter_true
] = lhs
;
381 expr
->args
[pet_ter_false
] = rhs
;
391 /* Construct a call pet_expr that calls function "name" with "n_arg"
392 * arguments. The caller is responsible for filling in the arguments.
394 __isl_give pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
399 expr
= pet_expr_alloc(ctx
, pet_expr_call
);
400 expr
= pet_expr_set_n_arg(expr
, n_arg
);
404 expr
->c
.name
= strdup(name
);
406 return pet_expr_free(expr
);
411 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
413 __isl_give pet_expr
*pet_expr_new_cast(const char *type_name
,
414 __isl_take pet_expr
*arg
)
422 ctx
= pet_expr_get_ctx(arg
);
423 expr
= pet_expr_alloc(ctx
, pet_expr_cast
);
424 expr
= pet_expr_set_n_arg(expr
, 1);
428 expr
->type_name
= strdup(type_name
);
429 if (!expr
->type_name
)
441 /* Construct a pet_expr that represents the double "d".
443 __isl_give pet_expr
*pet_expr_new_double(isl_ctx
*ctx
,
444 double val
, const char *s
)
448 expr
= pet_expr_alloc(ctx
, pet_expr_double
);
453 expr
->d
.s
= strdup(s
);
455 return pet_expr_free(expr
);
460 /* Construct a pet_expr that represents the integer value "v".
462 __isl_give pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
470 ctx
= isl_val_get_ctx(v
);
471 expr
= pet_expr_alloc(ctx
, pet_expr_int
);
483 /* Return an independent duplicate of "expr".
485 * In case of an access expression, make sure the depth of the duplicate is set
486 * before the access relation (if any) is set and after the index expression
489 static __isl_give pet_expr
*pet_expr_dup(__isl_keep pet_expr
*expr
)
493 enum pet_expr_access_type type
;
498 dup
= pet_expr_alloc(expr
->ctx
, expr
->type
);
499 dup
= pet_expr_set_type_size(dup
, expr
->type_size
);
500 dup
= pet_expr_set_n_arg(dup
, expr
->n_arg
);
501 for (i
= 0; i
< expr
->n_arg
; ++i
)
502 dup
= pet_expr_set_arg(dup
, i
, pet_expr_copy(expr
->args
[i
]));
504 switch (expr
->type
) {
505 case pet_expr_access
:
506 if (expr
->acc
.ref_id
)
507 dup
= pet_expr_access_set_ref_id(dup
,
508 isl_id_copy(expr
->acc
.ref_id
));
509 dup
= pet_expr_access_set_index(dup
,
510 isl_multi_pw_aff_copy(expr
->acc
.index
));
511 dup
= pet_expr_access_set_depth(dup
, expr
->acc
.depth
);
512 for (type
= pet_expr_access_begin
;
513 type
< pet_expr_access_end
; ++type
) {
514 if (!expr
->acc
.access
[type
])
516 dup
= pet_expr_access_set_access(dup
, type
,
517 isl_union_map_copy(expr
->acc
.access
[type
]));
519 dup
= pet_expr_access_set_read(dup
, expr
->acc
.read
);
520 dup
= pet_expr_access_set_write(dup
, expr
->acc
.write
);
521 dup
= pet_expr_access_set_kill(dup
, expr
->acc
.kill
);
524 dup
= pet_expr_call_set_name(dup
, expr
->c
.name
);
526 dup
= pet_expr_call_set_summary(dup
,
527 pet_function_summary_copy(expr
->c
.summary
));
530 dup
= pet_expr_cast_set_type_name(dup
, expr
->type_name
);
532 case pet_expr_double
:
533 dup
= pet_expr_double_set(dup
, expr
->d
.val
, expr
->d
.s
);
536 dup
= pet_expr_int_set_val(dup
, isl_val_copy(expr
->i
));
539 dup
= pet_expr_op_set_type(dup
, expr
->op
);
542 dup
= pet_expr_free(dup
);
549 /* Return a pet_expr that is equal to "expr" and that has only
550 * a single reference.
552 * If "expr" itself only has one reference, then clear its hash value
553 * since the returned pet_expr will be modified.
555 __isl_give pet_expr
*pet_expr_cow(__isl_take pet_expr
*expr
)
560 if (expr
->ref
== 1) {
565 return pet_expr_dup(expr
);
568 __isl_null pet_expr
*pet_expr_free(__isl_take pet_expr
*expr
)
570 enum pet_expr_access_type type
;
578 for (i
= 0; i
< expr
->n_arg
; ++i
)
579 pet_expr_free(expr
->args
[i
]);
582 switch (expr
->type
) {
583 case pet_expr_access
:
584 isl_id_free(expr
->acc
.ref_id
);
585 for (type
= pet_expr_access_begin
;
586 type
< pet_expr_access_end
; ++type
)
587 isl_union_map_free(expr
->acc
.access
[type
]);
588 isl_multi_pw_aff_free(expr
->acc
.index
);
592 pet_function_summary_free(expr
->c
.summary
);
595 free(expr
->type_name
);
597 case pet_expr_double
:
601 isl_val_free(expr
->i
);
608 isl_ctx_deref(expr
->ctx
);
613 /* Return an additional reference to "expr".
615 __isl_give pet_expr
*pet_expr_copy(__isl_keep pet_expr
*expr
)
624 /* Return the isl_ctx in which "expr" was created.
626 isl_ctx
*pet_expr_get_ctx(__isl_keep pet_expr
*expr
)
628 return expr
? expr
->ctx
: NULL
;
631 /* Return the type of "expr".
633 enum pet_expr_type
pet_expr_get_type(__isl_keep pet_expr
*expr
)
636 return pet_expr_error
;
640 /* Return the number of arguments of "expr".
642 int pet_expr_get_n_arg(__isl_keep pet_expr
*expr
)
650 /* Set the number of arguments of "expr" to "n".
652 * If "expr" originally had more arguments, then remove the extra arguments.
653 * If "expr" originally had fewer arguments, then create space for
654 * the extra arguments ans initialize them to NULL.
656 __isl_give pet_expr
*pet_expr_set_n_arg(__isl_take pet_expr
*expr
, int n
)
663 if (expr
->n_arg
== n
)
665 expr
= pet_expr_cow(expr
);
669 if (n
< expr
->n_arg
) {
670 for (i
= n
; i
< expr
->n_arg
; ++i
)
671 pet_expr_free(expr
->args
[i
]);
676 args
= isl_realloc_array(expr
->ctx
, expr
->args
, pet_expr
*, n
);
678 return pet_expr_free(expr
);
680 for (i
= expr
->n_arg
; i
< n
; ++i
)
681 expr
->args
[i
] = NULL
;
687 /* Return the argument of "expr" at position "pos".
689 __isl_give pet_expr
*pet_expr_get_arg(__isl_keep pet_expr
*expr
, int pos
)
693 if (pos
< 0 || pos
>= expr
->n_arg
)
694 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
695 "position out of bounds", return NULL
);
697 return pet_expr_copy(expr
->args
[pos
]);
700 /* Replace "expr" by its argument at position "pos".
702 __isl_give pet_expr
*pet_expr_arg(__isl_take pet_expr
*expr
, int pos
)
706 arg
= pet_expr_get_arg(expr
, pos
);
712 /* Replace the argument of "expr" at position "pos" by "arg".
714 __isl_give pet_expr
*pet_expr_set_arg(__isl_take pet_expr
*expr
, int pos
,
715 __isl_take pet_expr
*arg
)
719 if (pos
< 0 || pos
>= expr
->n_arg
)
720 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
721 "position out of bounds", goto error
);
722 if (expr
->args
[pos
] == arg
) {
727 expr
= pet_expr_cow(expr
);
731 pet_expr_free(expr
->args
[pos
]);
732 expr
->args
[pos
] = arg
;
741 /* Does "expr" perform a comparison operation?
743 int pet_expr_is_comparison(__isl_keep pet_expr
*expr
)
747 if (expr
->type
!= pet_expr_op
)
762 /* Does "expr" perform a boolean operation?
764 int pet_expr_is_boolean(__isl_keep pet_expr
*expr
)
768 if (expr
->type
!= pet_expr_op
)
780 /* Is "expr" an address-of operation?
782 int pet_expr_is_address_of(__isl_keep pet_expr
*expr
)
786 if (expr
->type
!= pet_expr_op
)
788 return expr
->op
== pet_op_address_of
;
791 /* Is "expr" an assume statement?
793 int pet_expr_is_assume(__isl_keep pet_expr
*expr
)
797 if (expr
->type
!= pet_expr_op
)
799 return expr
->op
== pet_op_assume
;
802 /* Does "expr" perform a min operation?
804 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
808 if (expr
->type
!= pet_expr_call
)
810 if (expr
->n_arg
!= 2)
812 if (strcmp(expr
->c
.name
, "min") != 0)
817 /* Does "expr" perform a max operation?
819 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
823 if (expr
->type
!= pet_expr_call
)
825 if (expr
->n_arg
!= 2)
827 if (strcmp(expr
->c
.name
, "max") != 0)
832 /* Does "expr" represent an access to an unnamed space, i.e.,
833 * does it represent an affine expression?
835 isl_bool
pet_expr_is_affine(__isl_keep pet_expr
*expr
)
840 return isl_bool_error
;
841 if (expr
->type
!= pet_expr_access
)
842 return isl_bool_false
;
844 has_id
= isl_multi_pw_aff_has_tuple_id(expr
->acc
.index
, isl_dim_out
);
846 return isl_bool_error
;
851 /* Given that "expr" represents an affine expression, i.e., that
852 * it is an access to an unnamed (1D) space, return this affine expression.
854 __isl_give isl_pw_aff
*pet_expr_get_affine(__isl_keep pet_expr
*expr
)
858 isl_multi_pw_aff
*mpa
;
860 is_affine
= pet_expr_is_affine(expr
);
864 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
865 "not an affine expression", return NULL
);
867 mpa
= pet_expr_access_get_index(expr
);
868 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
869 isl_multi_pw_aff_free(mpa
);
873 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
874 * not part of any struct?
876 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
880 if (expr
->type
!= pet_expr_access
)
882 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
))
885 return expr
->acc
.depth
== 0;
888 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
891 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
892 __isl_keep isl_multi_pw_aff
*mpa2
)
896 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
897 if (equal
< 0 || equal
)
899 mpa2
= isl_multi_pw_aff_copy(mpa2
);
900 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
901 isl_multi_pw_aff_get_space(mpa1
));
902 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
903 isl_multi_pw_aff_free(mpa2
);
908 /* Construct an access relation from the index expression and
909 * the array depth of the access expression "expr".
911 * If the number of indices is smaller than the depth of the array,
912 * then we assume that all elements of the remaining dimensions
915 static __isl_give isl_union_map
*construct_access_relation(
916 __isl_keep pet_expr
*expr
)
925 access
= isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr
));
929 dim
= isl_map_dim(access
, isl_dim_out
);
930 if (dim
> expr
->acc
.depth
)
931 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
932 "number of indices greater than depth",
933 access
= isl_map_free(access
));
935 if (dim
!= expr
->acc
.depth
)
936 access
= extend_range(access
, expr
->acc
.depth
- dim
);
938 return isl_union_map_from_map(access
);
941 /* Ensure that all relevant access relations are explicitly
942 * available in "expr".
944 * If "expr" does not already have the relevant access relations, then create
945 * them based on the index expression and the array depth.
947 * We do not cow since adding an explicit access relation
948 * does not change the meaning of the expression.
949 * However, the explicit access relations may modify the hash value,
950 * so the cached value is reset.
952 static __isl_give pet_expr
*introduce_access_relations(
953 __isl_take pet_expr
*expr
)
955 enum pet_expr_access_type type
;
956 isl_union_map
*access
;
958 int kill
, read
, write
;
962 if (has_relevant_access_relations(expr
))
965 access
= construct_access_relation(expr
);
967 return pet_expr_free(expr
);
970 kill
= expr
->acc
.kill
;
971 read
= expr
->acc
.read
;
972 write
= expr
->acc
.write
;
973 if (kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
974 expr
->acc
.access
[pet_expr_access_fake_killed
] =
975 isl_union_map_copy(access
);
976 if (read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
977 expr
->acc
.access
[pet_expr_access_may_read
] =
978 isl_union_map_copy(access
);
979 if (write
&& !expr
->acc
.access
[pet_expr_access_may_write
])
980 expr
->acc
.access
[pet_expr_access_may_write
] =
981 isl_union_map_copy(access
);
982 if (write
&& !expr
->acc
.access
[pet_expr_access_must_write
])
983 expr
->acc
.access
[pet_expr_access_must_write
] =
984 isl_union_map_copy(access
);
986 isl_union_map_free(access
);
988 if (!has_relevant_access_relations(expr
))
989 return pet_expr_free(expr
);
994 /* Return a hash value that digests "expr".
995 * If a hash value was computed already, then return that value.
996 * Otherwise, compute the hash value and store a copy in expr->hash.
998 uint32_t pet_expr_get_hash(__isl_keep pet_expr
*expr
)
1001 enum pet_expr_access_type type
;
1002 uint32_t hash
, hash_f
;
1009 hash
= isl_hash_init();
1010 isl_hash_byte(hash
, expr
->type
& 0xFF);
1011 isl_hash_byte(hash
, expr
->n_arg
& 0xFF);
1012 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1014 hash_i
= pet_expr_get_hash(expr
->args
[i
]);
1015 isl_hash_hash(hash
, hash_i
);
1017 switch (expr
->type
) {
1018 case pet_expr_error
:
1020 case pet_expr_double
:
1021 hash
= isl_hash_string(hash
, expr
->d
.s
);
1024 hash_f
= isl_val_get_hash(expr
->i
);
1025 isl_hash_hash(hash
, hash_f
);
1027 case pet_expr_access
:
1028 isl_hash_byte(hash
, expr
->acc
.read
& 0xFF);
1029 isl_hash_byte(hash
, expr
->acc
.write
& 0xFF);
1030 isl_hash_byte(hash
, expr
->acc
.kill
& 0xFF);
1031 hash_f
= isl_id_get_hash(expr
->acc
.ref_id
);
1032 isl_hash_hash(hash
, hash_f
);
1033 hash_f
= isl_multi_pw_aff_get_hash(expr
->acc
.index
);
1034 isl_hash_hash(hash
, hash_f
);
1035 isl_hash_byte(hash
, expr
->acc
.depth
& 0xFF);
1036 for (type
= pet_expr_access_begin
;
1037 type
< pet_expr_access_end
; ++type
) {
1038 hash_f
= isl_union_map_get_hash(expr
->acc
.access
[type
]);
1039 isl_hash_hash(hash
, hash_f
);
1043 isl_hash_byte(hash
, expr
->op
& 0xFF);
1046 hash
= isl_hash_string(hash
, expr
->c
.name
);
1049 hash
= isl_hash_string(hash
, expr
->type_name
);
1056 /* Return 1 if the two pet_exprs are equivalent.
1058 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
1061 enum pet_expr_access_type type
;
1063 if (!expr1
|| !expr2
)
1066 if (expr1
->type
!= expr2
->type
)
1068 if (expr1
->n_arg
!= expr2
->n_arg
)
1070 for (i
= 0; i
< expr1
->n_arg
; ++i
)
1071 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
1073 switch (expr1
->type
) {
1074 case pet_expr_error
:
1076 case pet_expr_double
:
1077 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
1079 if (expr1
->d
.val
!= expr2
->d
.val
)
1083 if (!isl_val_eq(expr1
->i
, expr2
->i
))
1086 case pet_expr_access
:
1087 if (expr1
->acc
.read
!= expr2
->acc
.read
)
1089 if (expr1
->acc
.write
!= expr2
->acc
.write
)
1091 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
1093 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
1095 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
1097 if (!multi_pw_aff_is_equal(expr1
->acc
.index
, expr2
->acc
.index
))
1099 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
1101 if (has_relevant_access_relations(expr1
) !=
1102 has_relevant_access_relations(expr2
)) {
1104 expr1
= pet_expr_copy(expr1
);
1105 expr2
= pet_expr_copy(expr2
);
1106 expr1
= introduce_access_relations(expr1
);
1107 expr2
= introduce_access_relations(expr2
);
1108 equal
= pet_expr_is_equal(expr1
, expr2
);
1109 pet_expr_free(expr1
);
1110 pet_expr_free(expr2
);
1113 for (type
= pet_expr_access_begin
;
1114 type
< pet_expr_access_end
; ++type
) {
1115 if (!expr1
->acc
.access
[type
] !=
1116 !expr2
->acc
.access
[type
])
1118 if (!expr1
->acc
.access
[type
])
1120 if (!isl_union_map_is_equal(expr1
->acc
.access
[type
],
1121 expr2
->acc
.access
[type
]))
1126 if (expr1
->op
!= expr2
->op
)
1130 if (strcmp(expr1
->c
.name
, expr2
->c
.name
))
1134 if (strcmp(expr1
->type_name
, expr2
->type_name
))
1142 /* Do "expr1" and "expr2" represent two accesses to the same array
1143 * that are also of the same type? That is, can these two accesses
1144 * be replaced by a single access?
1146 isl_bool
pet_expr_is_same_access(__isl_keep pet_expr
*expr1
,
1147 __isl_keep pet_expr
*expr2
)
1149 isl_space
*space1
, *space2
;
1152 if (!expr1
|| !expr2
)
1153 return isl_bool_error
;
1154 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1155 return isl_bool_false
;
1156 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1157 return isl_bool_false
;
1158 if (expr1
->acc
.read
!= expr2
->acc
.read
)
1159 return isl_bool_false
;
1160 if (expr1
->acc
.write
!= expr2
->acc
.write
)
1161 return isl_bool_false
;
1162 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
1163 return isl_bool_false
;
1164 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
1165 return isl_bool_false
;
1167 space1
= isl_multi_pw_aff_get_space(expr1
->acc
.index
);
1168 space2
= isl_multi_pw_aff_get_space(expr2
->acc
.index
);
1169 same
= isl_space_tuple_is_equal(space1
, isl_dim_out
,
1170 space2
, isl_dim_out
);
1171 if (same
>= 0 && same
)
1172 same
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
1173 space2
, isl_dim_in
);
1174 isl_space_free(space1
);
1175 isl_space_free(space2
);
1180 /* Does the access expression "expr" read the accessed elements?
1182 isl_bool
pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
1185 return isl_bool_error
;
1186 if (expr
->type
!= pet_expr_access
)
1187 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1188 "not an access expression", return isl_bool_error
);
1190 return expr
->acc
.read
;
1193 /* Does the access expression "expr" write to the accessed elements?
1195 isl_bool
pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
1198 return isl_bool_error
;
1199 if (expr
->type
!= pet_expr_access
)
1200 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1201 "not an access expression", return isl_bool_error
);
1203 return expr
->acc
.write
;
1206 /* Does the access expression "expr" kill the accessed elements?
1208 isl_bool
pet_expr_access_is_kill(__isl_keep pet_expr
*expr
)
1211 return isl_bool_error
;
1212 if (expr
->type
!= pet_expr_access
)
1213 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1214 "not an access expression", return isl_bool_error
);
1216 return expr
->acc
.kill
;
1219 /* Return the identifier of the array accessed by "expr".
1221 * If "expr" represents a member access, then return the identifier
1222 * of the outer structure array.
1224 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
1228 if (expr
->type
!= pet_expr_access
)
1229 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1230 "not an access expression", return NULL
);
1232 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
)) {
1236 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1237 space
= isl_space_range(space
);
1238 while (space
&& isl_space_is_wrapping(space
))
1239 space
= isl_space_domain(isl_space_unwrap(space
));
1240 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
1241 isl_space_free(space
);
1246 return isl_multi_pw_aff_get_tuple_id(expr
->acc
.index
, isl_dim_out
);
1249 /* Return the parameter space of "expr".
1251 __isl_give isl_space
*pet_expr_access_get_parameter_space(
1252 __isl_keep pet_expr
*expr
)
1258 if (expr
->type
!= pet_expr_access
)
1259 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1260 "not an access expression", return NULL
);
1262 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1263 space
= isl_space_params(space
);
1268 /* Return the domain space of "expr", including the arguments (if any).
1270 __isl_give isl_space
*pet_expr_access_get_augmented_domain_space(
1271 __isl_keep pet_expr
*expr
)
1277 if (expr
->type
!= pet_expr_access
)
1278 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1279 "not an access expression", return NULL
);
1281 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1282 space
= isl_space_domain(space
);
1287 /* Return the domain space of "expr", without the arguments (if any).
1289 __isl_give isl_space
*pet_expr_access_get_domain_space(
1290 __isl_keep pet_expr
*expr
)
1294 space
= pet_expr_access_get_augmented_domain_space(expr
);
1295 if (isl_space_is_wrapping(space
))
1296 space
= isl_space_domain(isl_space_unwrap(space
));
1301 /* Return the space of the data accessed by "expr".
1303 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
1309 if (expr
->type
!= pet_expr_access
)
1310 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1311 "not an access expression", return NULL
);
1313 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1314 space
= isl_space_range(space
);
1319 /* Modify all subexpressions of "expr" by calling "fn" on them.
1320 * The subexpressions are traversed in depth first preorder.
1322 __isl_give pet_expr
*pet_expr_map_top_down(__isl_take pet_expr
*expr
,
1323 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1331 expr
= fn(expr
, user
);
1333 n
= pet_expr_get_n_arg(expr
);
1334 for (i
= 0; i
< n
; ++i
) {
1335 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1336 arg
= pet_expr_map_top_down(arg
, fn
, user
);
1337 expr
= pet_expr_set_arg(expr
, i
, arg
);
1343 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1345 static __isl_give pet_expr
*pet_expr_map_expr_of_type(__isl_take pet_expr
*expr
,
1346 enum pet_expr_type type
,
1347 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1352 n
= pet_expr_get_n_arg(expr
);
1353 for (i
= 0; i
< n
; ++i
) {
1354 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1355 arg
= pet_expr_map_expr_of_type(arg
, type
, fn
, user
);
1356 expr
= pet_expr_set_arg(expr
, i
, arg
);
1362 if (expr
->type
== type
)
1363 expr
= fn(expr
, user
);
1368 /* Modify all expressions of type pet_expr_access in "expr"
1369 * by calling "fn" on them.
1371 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
1372 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1375 return pet_expr_map_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1378 /* Modify all expressions of type pet_expr_call in "expr"
1379 * by calling "fn" on them.
1381 __isl_give pet_expr
*pet_expr_map_call(__isl_take pet_expr
*expr
,
1382 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1385 return pet_expr_map_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1388 /* Modify all expressions of type pet_expr_op in "expr"
1389 * by calling "fn" on them.
1391 __isl_give pet_expr
*pet_expr_map_op(__isl_take pet_expr
*expr
,
1392 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1395 return pet_expr_map_expr_of_type(expr
, pet_expr_op
, fn
, user
);
1398 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1400 * Return -1 on error (where fn returning a negative value is treated as
1402 * Otherwise return 0.
1404 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
1405 enum pet_expr_type type
,
1406 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1413 for (i
= 0; i
< expr
->n_arg
; ++i
)
1414 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1415 type
, fn
, user
) < 0)
1418 if (expr
->type
== type
)
1419 return fn(expr
, user
);
1424 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1426 * Return -1 on error (where fn returning a negative value is treated as
1428 * Otherwise return 0.
1430 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1431 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1433 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1436 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1438 * Return -1 on error (where fn returning a negative value is treated as
1440 * Otherwise return 0.
1442 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1443 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1445 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1448 /* Internal data structure for pet_expr_writes.
1449 * "id" is the identifier that we are looking for.
1450 * "found" is set if we have found the identifier being written to.
1452 struct pet_expr_writes_data
{
1457 /* Given an access expression, check if it writes to data->id.
1458 * If so, set data->found and abort the search.
1460 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1462 struct pet_expr_writes_data
*data
= user
;
1465 if (!expr
->acc
.write
)
1467 if (pet_expr_is_affine(expr
))
1470 write_id
= pet_expr_access_get_id(expr
);
1471 isl_id_free(write_id
);
1476 if (write_id
!= data
->id
)
1483 /* Does expression "expr" write to "id"?
1485 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1487 struct pet_expr_writes_data data
;
1491 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1498 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1499 * index expression and access relations of "expr" (if any)
1500 * to dimensions of "dst_type" at "dst_pos".
1502 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1503 enum isl_dim_type dst_type
, unsigned dst_pos
,
1504 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1506 enum pet_expr_access_type type
;
1508 expr
= pet_expr_cow(expr
);
1511 if (expr
->type
!= pet_expr_access
)
1512 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1513 "not an access pet_expr", return pet_expr_free(expr
));
1515 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1516 if (!expr
->acc
.access
[type
])
1518 expr
->acc
.access
[type
] =
1519 pet_union_map_move_dims(expr
->acc
.access
[type
],
1520 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1521 if (!expr
->acc
.access
[type
])
1524 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1525 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1526 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1527 return pet_expr_free(expr
);
1532 /* Replace the index expression and access relations (if any) of "expr"
1533 * by their preimages under the function represented by "ma".
1535 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1536 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1538 enum pet_expr_access_type type
;
1540 expr
= pet_expr_cow(expr
);
1543 if (expr
->type
!= pet_expr_access
)
1544 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1545 "not an access pet_expr", goto error
);
1547 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1548 if (!expr
->acc
.access
[type
])
1550 expr
->acc
.access
[type
] =
1551 isl_union_map_preimage_domain_multi_aff(
1552 expr
->acc
.access
[type
], isl_multi_aff_copy(ma
));
1553 if (!expr
->acc
.access
[type
])
1556 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1558 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1559 return pet_expr_free(expr
);
1563 isl_multi_aff_free(ma
);
1564 pet_expr_free(expr
);
1568 /* Replace the index expression and access relations (if any) of "expr"
1569 * by their preimages under the function represented by "mpa".
1571 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1572 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1574 enum pet_expr_access_type type
;
1576 expr
= pet_expr_cow(expr
);
1579 if (expr
->type
!= pet_expr_access
)
1580 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1581 "not an access pet_expr", goto error
);
1583 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1584 if (!expr
->acc
.access
[type
])
1586 expr
->acc
.access
[type
] =
1587 isl_union_map_preimage_domain_multi_pw_aff(
1588 expr
->acc
.access
[type
], isl_multi_pw_aff_copy(mpa
));
1589 if (!expr
->acc
.access
[type
])
1592 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1593 expr
->acc
.index
, mpa
);
1594 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1595 return pet_expr_free(expr
);
1599 isl_multi_pw_aff_free(mpa
);
1600 pet_expr_free(expr
);
1604 /* Return the index expression of access expression "expr".
1606 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1607 __isl_keep pet_expr
*expr
)
1611 if (expr
->type
!= pet_expr_access
)
1612 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1613 "not an access expression", return NULL
);
1615 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1618 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1620 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1623 enum pet_expr_access_type type
;
1625 expr
= pet_expr_cow(expr
);
1628 if (expr
->type
!= pet_expr_access
)
1629 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1630 "not an access expression", return pet_expr_free(expr
));
1632 if (!pet_expr_access_has_any_access_relation(expr
))
1635 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1636 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1637 if (!expr
->acc
.access
[type
])
1639 space
= isl_space_align_params(space
,
1640 isl_union_map_get_space(expr
->acc
.access
[type
]));
1642 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1643 isl_space_copy(space
));
1644 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1645 if (!expr
->acc
.access
[type
])
1647 expr
->acc
.access
[type
] =
1648 isl_union_map_align_params(expr
->acc
.access
[type
],
1649 isl_space_copy(space
));
1650 if (!expr
->acc
.access
[type
])
1653 isl_space_free(space
);
1654 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1655 return pet_expr_free(expr
);
1660 /* Are "expr1" and "expr2" both array accesses such that
1661 * the access relation of "expr1" is a subset of that of "expr2"?
1662 * Only take into account the first "n_arg" arguments.
1664 * This function is tailored for use by mark_self_dependences in nest.c.
1665 * In particular, the input expressions may have more than "n_arg"
1666 * elements in their arguments arrays, while only the first "n_arg"
1667 * elements are referenced from the access relations.
1669 int pet_expr_is_sub_access(__isl_keep pet_expr
*expr1
,
1670 __isl_keep pet_expr
*expr2
, int n_arg
)
1676 if (!expr1
|| !expr2
)
1678 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1680 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1682 if (pet_expr_is_affine(expr1
))
1684 if (pet_expr_is_affine(expr2
))
1686 n1
= pet_expr_get_n_arg(expr1
);
1689 n2
= pet_expr_get_n_arg(expr2
);
1694 for (i
= 0; i
< n1
; ++i
) {
1696 equal
= pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]);
1697 if (equal
< 0 || !equal
)
1700 id1
= pet_expr_access_get_id(expr1
);
1701 id2
= pet_expr_access_get_id(expr2
);
1709 expr1
= pet_expr_copy(expr1
);
1710 expr2
= pet_expr_copy(expr2
);
1711 expr1
= introduce_access_relations(expr1
);
1712 expr2
= introduce_access_relations(expr2
);
1713 if (!expr1
|| !expr2
)
1716 is_subset
= isl_union_map_is_subset(
1717 expr1
->acc
.access
[pet_expr_access_may_read
],
1718 expr2
->acc
.access
[pet_expr_access_may_read
]);
1720 pet_expr_free(expr1
);
1721 pet_expr_free(expr2
);
1725 pet_expr_free(expr1
);
1726 pet_expr_free(expr2
);
1730 /* Given a set in the iteration space "domain", extend it to live in the space
1731 * of the domain of access relations.
1733 * That, is the number of arguments "n" is 0, then simply return domain.
1734 * Otherwise, return [domain -> [a_1,...,a_n]].
1736 static __isl_give isl_set
*add_arguments(__isl_take isl_set
*domain
, int n
)
1743 map
= isl_map_from_domain(domain
);
1744 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
1745 return isl_map_wrap(map
);
1748 /* Add extra conditions to the domains of all access relations in "expr",
1749 * introducing access relations if they are not already present.
1751 * The conditions are not added to the index expression. Instead, they
1752 * are used to try and simplify the index expression.
1754 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1755 __isl_take isl_set
*cond
)
1758 isl_union_set
*uset
;
1759 enum pet_expr_access_type type
;
1761 expr
= pet_expr_cow(expr
);
1765 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1766 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1767 isl_set_copy(cond
));
1772 if (expr
->type
!= pet_expr_access
) {
1777 expr
= introduce_access_relations(expr
);
1781 cond
= add_arguments(cond
, expr
->n_arg
);
1782 uset
= isl_union_set_from_set(isl_set_copy(cond
));
1783 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1784 if (!expr
->acc
.access
[type
])
1786 expr
->acc
.access
[type
] =
1787 isl_union_map_intersect_domain(expr
->acc
.access
[type
],
1788 isl_union_set_copy(uset
));
1789 if (!expr
->acc
.access
[type
])
1792 isl_union_set_free(uset
);
1793 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, cond
);
1794 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1795 return pet_expr_free(expr
);
1800 return pet_expr_free(expr
);
1803 /* Modify the access relations (if any) and index expression
1804 * of the given access expression
1805 * based on the given iteration space transformation.
1806 * In particular, precompose the access relation and index expression
1807 * with the update function.
1809 * If the access has any arguments then the domain of the access relation
1810 * is a wrapped mapping from the iteration space to the space of
1811 * argument values. We only need to change the domain of this wrapped
1812 * mapping, so we extend the input transformation with an identity mapping
1813 * on the space of argument values.
1815 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1816 __isl_keep isl_multi_pw_aff
*update
)
1818 enum pet_expr_access_type type
;
1820 expr
= pet_expr_cow(expr
);
1823 if (expr
->type
!= pet_expr_access
)
1824 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1825 "not an access expression", return pet_expr_free(expr
));
1827 update
= isl_multi_pw_aff_copy(update
);
1829 if (expr
->n_arg
> 0) {
1831 isl_multi_pw_aff
*id
;
1833 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1834 space
= isl_space_domain(space
);
1835 space
= isl_space_unwrap(space
);
1836 space
= isl_space_range(space
);
1837 space
= isl_space_map_from_set(space
);
1838 id
= isl_multi_pw_aff_identity(space
);
1839 update
= isl_multi_pw_aff_product(update
, id
);
1842 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1843 if (!expr
->acc
.access
[type
])
1845 expr
->acc
.access
[type
] =
1846 isl_union_map_preimage_domain_multi_pw_aff(
1847 expr
->acc
.access
[type
],
1848 isl_multi_pw_aff_copy(update
));
1849 if (!expr
->acc
.access
[type
])
1852 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1853 expr
->acc
.index
, update
);
1854 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1855 return pet_expr_free(expr
);
1860 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1862 isl_multi_pw_aff
*update
= user
;
1864 return pet_expr_access_update_domain(expr
, update
);
1867 /* Modify all access relations in "expr" by precomposing them with
1868 * the given iteration space transformation.
1870 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1871 __isl_take isl_multi_pw_aff
*update
)
1873 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1874 isl_multi_pw_aff_free(update
);
1878 /* Given an expression with accesses that have a 0D anonymous domain,
1879 * replace those domains by "space".
1881 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1882 __isl_take isl_space
*space
)
1884 isl_multi_pw_aff
*mpa
;
1886 space
= isl_space_from_domain(space
);
1887 mpa
= isl_multi_pw_aff_zero(space
);
1888 return pet_expr_update_domain(expr
, mpa
);
1891 /* Add all parameters in "space" to the access relations (if any)
1892 * and index expression of "expr".
1894 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1896 isl_space
*space
= user
;
1897 enum pet_expr_access_type type
;
1899 expr
= pet_expr_cow(expr
);
1902 if (expr
->type
!= pet_expr_access
)
1903 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1904 "not an access expression", return pet_expr_free(expr
));
1906 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1907 if (!expr
->acc
.access
[type
])
1909 expr
->acc
.access
[type
] =
1910 isl_union_map_align_params(expr
->acc
.access
[type
],
1911 isl_space_copy(space
));
1912 if (!expr
->acc
.access
[type
])
1915 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1916 isl_space_copy(space
));
1917 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1918 return pet_expr_free(expr
);
1923 /* Add all parameters in "space" to all access relations and index expressions
1926 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1927 __isl_take isl_space
*space
)
1929 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1930 isl_space_free(space
);
1934 /* Insert an argument expression corresponding to "test" in front
1935 * of the list of arguments described by *n_arg and *args.
1937 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1938 __isl_keep isl_multi_pw_aff
*test
)
1941 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1944 return pet_expr_free(expr
);
1945 expr
= pet_expr_cow(expr
);
1950 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1952 return pet_expr_free(expr
);
1955 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1957 return pet_expr_free(expr
);
1958 for (i
= 0; i
< expr
->n_arg
; ++i
)
1959 ext
[1 + i
] = expr
->args
[i
];
1964 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1966 return pet_expr_free(expr
);
1971 /* Make the expression "expr" depend on the value of "test"
1972 * being equal to "satisfied".
1974 * If "test" is an affine expression, we simply add the conditions
1975 * on the expression having the value "satisfied" to all access relations
1976 * (introducing access relations if they are missing) and index expressions.
1978 * Otherwise, we add a filter to "expr" (which is then assumed to be
1979 * an access expression) corresponding to "test" being equal to "satisfied".
1981 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1982 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1987 isl_pw_multi_aff
*pma
;
1988 enum pet_expr_access_type type
;
1990 expr
= pet_expr_cow(expr
);
1994 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
1998 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
1999 isl_multi_pw_aff_free(test
);
2001 cond
= isl_pw_aff_non_zero_set(pa
);
2003 cond
= isl_pw_aff_zero_set(pa
);
2004 return pet_expr_restrict(expr
, cond
);
2007 ctx
= isl_multi_pw_aff_get_ctx(test
);
2008 if (expr
->type
!= pet_expr_access
)
2009 isl_die(ctx
, isl_error_invalid
,
2010 "can only filter access expressions", goto error
);
2012 expr
= introduce_access_relations(expr
);
2016 space
= isl_space_domain(isl_multi_pw_aff_get_space(expr
->acc
.index
));
2017 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
2018 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
2020 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2021 if (!expr
->acc
.access
[type
])
2023 expr
->acc
.access
[type
] =
2024 isl_union_map_preimage_domain_pw_multi_aff(
2025 expr
->acc
.access
[type
],
2026 isl_pw_multi_aff_copy(pma
));
2027 if (!expr
->acc
.access
[type
])
2030 pma
= isl_pw_multi_aff_gist(pma
,
2031 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
2032 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
2033 expr
->acc
.index
, pma
);
2034 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2037 expr
= insert_access_arg(expr
, test
);
2039 isl_multi_pw_aff_free(test
);
2042 isl_multi_pw_aff_free(test
);
2043 return pet_expr_free(expr
);
2046 /* Add a reference identifier to access expression "expr".
2047 * "user" points to an integer that contains the sequence number
2048 * of the next reference.
2050 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
2057 expr
= pet_expr_cow(expr
);
2060 if (expr
->type
!= pet_expr_access
)
2061 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2062 "not an access expression", return pet_expr_free(expr
));
2064 ctx
= pet_expr_get_ctx(expr
);
2065 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
2066 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
2067 if (!expr
->acc
.ref_id
)
2068 return pet_expr_free(expr
);
2073 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
2075 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
2078 /* Reset the user pointer on all parameter and tuple ids in
2079 * the access relations (if any) and the index expression
2080 * of the access expression "expr".
2082 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
2085 enum pet_expr_access_type type
;
2087 expr
= pet_expr_cow(expr
);
2090 if (expr
->type
!= pet_expr_access
)
2091 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2092 "not an access expression", return pet_expr_free(expr
));
2094 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2095 if (!expr
->acc
.access
[type
])
2097 expr
->acc
.access
[type
] =
2098 isl_union_map_reset_user(expr
->acc
.access
[type
]);
2099 if (!expr
->acc
.access
[type
])
2102 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
2103 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2104 return pet_expr_free(expr
);
2109 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
2111 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
2114 /* Data used in access_gist() callback.
2116 struct pet_access_gist_data
{
2118 isl_union_map
*value_bounds
;
2121 /* Given an expression "expr" of type pet_expr_access, compute
2122 * the gist of the associated access relations (if any) and index expression
2123 * with respect to data->domain and the bounds on the values of the arguments
2124 * of the expression.
2126 * The arguments of "expr" have been gisted right before "expr" itself
2127 * is gisted. The gisted arguments may have become equal where before
2128 * they may not have been (obviously) equal. We therefore take
2129 * the opportunity to remove duplicate arguments here.
2131 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
2133 struct pet_access_gist_data
*data
= user
;
2135 isl_union_set
*uset
;
2136 enum pet_expr_access_type type
;
2138 expr
= pet_expr_remove_duplicate_args(expr
);
2139 expr
= pet_expr_cow(expr
);
2142 if (expr
->type
!= pet_expr_access
)
2143 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2144 "not an access expression", return pet_expr_free(expr
));
2146 domain
= isl_set_copy(data
->domain
);
2147 if (expr
->n_arg
> 0)
2148 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
2149 data
->value_bounds
);
2151 uset
= isl_union_set_from_set(isl_set_copy(domain
));
2152 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2153 if (!expr
->acc
.access
[type
])
2155 expr
->acc
.access
[type
] =
2156 isl_union_map_gist_domain(expr
->acc
.access
[type
],
2157 isl_union_set_copy(uset
));
2158 if (!expr
->acc
.access
[type
])
2161 isl_union_set_free(uset
);
2162 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
2163 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2164 return pet_expr_free(expr
);
2169 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
2170 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2172 struct pet_access_gist_data data
= { context
, value_bounds
};
2174 return pet_expr_map_access(expr
, &access_gist
, &data
);
2177 /* Mark "expr" as a read dependening on "read".
2179 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
2183 return pet_expr_free(expr
);
2184 if (expr
->type
!= pet_expr_access
)
2185 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2186 "not an access expression", return pet_expr_free(expr
));
2187 if (expr
->acc
.read
== read
)
2189 expr
= pet_expr_cow(expr
);
2192 expr
->acc
.read
= read
;
2197 /* Mark "expr" as a write dependening on "write".
2199 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
2203 return pet_expr_free(expr
);
2204 if (expr
->type
!= pet_expr_access
)
2205 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2206 "not an access expression", return pet_expr_free(expr
));
2207 if (expr
->acc
.write
== write
)
2209 expr
= pet_expr_cow(expr
);
2212 expr
->acc
.write
= write
;
2217 /* Mark "expr" as a kill dependening on "kill".
2219 __isl_give pet_expr
*pet_expr_access_set_kill(__isl_take pet_expr
*expr
,
2223 return pet_expr_free(expr
);
2224 if (expr
->type
!= pet_expr_access
)
2225 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2226 "not an access expression", return pet_expr_free(expr
));
2227 if (expr
->acc
.kill
== kill
)
2229 expr
= pet_expr_cow(expr
);
2232 expr
->acc
.kill
= kill
;
2237 /* Map the access type "type" to the corresponding location
2238 * in the access array.
2239 * In particular, the access relation of type pet_expr_access_killed is
2240 * stored in the element at position pet_expr_access_fake_killed.
2242 static enum pet_expr_access_type
internalize_type(
2243 enum pet_expr_access_type type
)
2245 if (type
== pet_expr_access_killed
)
2246 return pet_expr_access_fake_killed
;
2250 /* Replace the access relation of the given "type" of "expr" by "access".
2251 * If the access relation is non-empty and the type is a read or a write,
2252 * then also mark the access expression itself as a read or a write.
2254 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
2255 enum pet_expr_access_type type
, __isl_take isl_union_map
*access
)
2259 expr
= pet_expr_cow(expr
);
2260 if (!expr
|| !access
)
2262 if (expr
->type
!= pet_expr_access
)
2263 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2264 "not an access expression", goto error
);
2265 type
= internalize_type(type
);
2266 isl_union_map_free(expr
->acc
.access
[type
]);
2267 expr
->acc
.access
[type
] = access
;
2272 empty
= isl_union_map_is_empty(access
);
2274 return pet_expr_free(expr
);
2278 if (type
== pet_expr_access_may_read
)
2279 expr
= pet_expr_access_set_read(expr
, 1);
2281 expr
= pet_expr_access_set_write(expr
, 1);
2285 isl_union_map_free(access
);
2286 pet_expr_free(expr
);
2290 /* Replace the index expression of "expr" by "index" and
2291 * set the array depth accordingly.
2293 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
2294 __isl_take isl_multi_pw_aff
*index
)
2296 expr
= pet_expr_cow(expr
);
2297 if (!expr
|| !index
)
2299 if (expr
->type
!= pet_expr_access
)
2300 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2301 "not an access expression", goto error
);
2302 isl_multi_pw_aff_free(expr
->acc
.index
);
2303 expr
->acc
.index
= index
;
2304 expr
->acc
.depth
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
2308 isl_multi_pw_aff_free(index
);
2309 pet_expr_free(expr
);
2313 /* Return the reference identifier of access expression "expr".
2315 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
2319 if (expr
->type
!= pet_expr_access
)
2320 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2321 "not an access expression", return NULL
);
2323 return isl_id_copy(expr
->acc
.ref_id
);
2326 /* Replace the reference identifier of access expression "expr" by "ref_id".
2328 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
2329 __isl_take isl_id
*ref_id
)
2331 expr
= pet_expr_cow(expr
);
2332 if (!expr
|| !ref_id
)
2334 if (expr
->type
!= pet_expr_access
)
2335 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2336 "not an access expression", goto error
);
2337 isl_id_free(expr
->acc
.ref_id
);
2338 expr
->acc
.ref_id
= ref_id
;
2342 isl_id_free(ref_id
);
2343 pet_expr_free(expr
);
2347 /* Tag the access relation "access" with "id".
2348 * That is, insert the id as the range of a wrapped relation
2349 * in the domain of "access".
2351 * If "access" is of the form
2355 * then the result is of the form
2357 * [D[i] -> id[]] -> A[a]
2359 __isl_give isl_union_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
2360 __isl_take isl_union_map
*access
)
2363 isl_multi_aff
*add_tag
;
2366 if (expr
->type
!= pet_expr_access
)
2367 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2368 "not an access expression",
2369 return isl_union_map_free(access
));
2371 id
= isl_id_copy(expr
->acc
.ref_id
);
2372 space
= pet_expr_access_get_domain_space(expr
);
2373 space
= isl_space_from_domain(space
);
2374 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2375 add_tag
= isl_multi_aff_domain_map(space
);
2376 access
= isl_union_map_preimage_domain_multi_aff(access
, add_tag
);
2381 /* Return the access relation of the given "type" associated to "expr"
2382 * that maps pairs of domain iterations and argument values
2383 * to the corresponding accessed data elements.
2385 * If the requested access relation is explicitly available,
2386 * then return a copy. Otherwise, check if it is irrelevant for
2387 * the access expression and return an empty relation if this is the case.
2388 * Otherwise, introduce the requested access relation in "expr" and
2391 __isl_give isl_union_map
*pet_expr_access_get_dependent_access(
2392 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2394 isl_union_map
*access
;
2399 if (expr
->type
!= pet_expr_access
)
2400 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2401 "not an access expression", return NULL
);
2403 type
= internalize_type(type
);
2404 if (expr
->acc
.access
[type
])
2405 return isl_union_map_copy(expr
->acc
.access
[type
]);
2407 if (type
== pet_expr_access_may_read
)
2408 empty
= !expr
->acc
.read
;
2410 empty
= !expr
->acc
.write
;
2413 expr
= pet_expr_copy(expr
);
2414 expr
= introduce_access_relations(expr
);
2417 access
= isl_union_map_copy(expr
->acc
.access
[type
]);
2418 pet_expr_free(expr
);
2423 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr
));
2426 /* Return the may read access relation associated to "expr"
2427 * that maps pairs of domain iterations and argument values
2428 * to the corresponding accessed data elements.
2430 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_read(
2431 __isl_keep pet_expr
*expr
)
2433 return pet_expr_access_get_dependent_access(expr
,
2434 pet_expr_access_may_read
);
2437 /* Return the may write access relation associated to "expr"
2438 * that maps pairs of domain iterations and argument values
2439 * to the corresponding accessed data elements.
2441 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_write(
2442 __isl_keep pet_expr
*expr
)
2444 return pet_expr_access_get_dependent_access(expr
,
2445 pet_expr_access_may_write
);
2448 /* Return the must write access relation associated to "expr"
2449 * that maps pairs of domain iterations and argument values
2450 * to the corresponding accessed data elements.
2452 __isl_give isl_union_map
*pet_expr_access_get_dependent_must_write(
2453 __isl_keep pet_expr
*expr
)
2455 return pet_expr_access_get_dependent_access(expr
,
2456 pet_expr_access_must_write
);
2459 /* Return the relation of the given "type" mapping domain iterations
2460 * to the accessed data elements.
2461 * In particular, take the access relation and, in case of may_read
2462 * or may_write, project out the values of the arguments, if any.
2463 * In case of must_write, return the empty relation if there are
2466 __isl_give isl_union_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
,
2467 enum pet_expr_access_type type
)
2469 isl_union_map
*access
;
2475 if (expr
->type
!= pet_expr_access
)
2476 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2477 "not an access expression", return NULL
);
2479 if (expr
->n_arg
!= 0 && type
== pet_expr_access_must_write
) {
2480 space
= pet_expr_access_get_parameter_space(expr
);
2481 return isl_union_map_empty(space
);
2484 access
= pet_expr_access_get_dependent_access(expr
, type
);
2485 if (expr
->n_arg
== 0)
2488 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
2489 space
= isl_space_domain(space
);
2490 map
= isl_map_universe(isl_space_unwrap(space
));
2491 map
= isl_map_domain_map(map
);
2492 access
= isl_union_map_apply_domain(access
,
2493 isl_union_map_from_map(map
));
2498 /* Return the relation mapping domain iterations to all possibly
2499 * read data elements.
2501 __isl_give isl_union_map
*pet_expr_access_get_may_read(
2502 __isl_keep pet_expr
*expr
)
2504 return pet_expr_access_get_access(expr
, pet_expr_access_may_read
);
2507 /* Return the relation mapping domain iterations to all possibly
2508 * written data elements.
2510 __isl_give isl_union_map
*pet_expr_access_get_may_write(
2511 __isl_keep pet_expr
*expr
)
2513 return pet_expr_access_get_access(expr
, pet_expr_access_may_write
);
2516 /* Return a relation mapping domain iterations to definitely
2517 * written data elements, assuming the statement containing
2518 * the expression is executed.
2520 __isl_give isl_union_map
*pet_expr_access_get_must_write(
2521 __isl_keep pet_expr
*expr
)
2523 return pet_expr_access_get_access(expr
, pet_expr_access_must_write
);
2526 /* Return the relation of the given "type" mapping domain iterations to
2527 * accessed data elements, with its domain tagged with the reference
2530 static __isl_give isl_union_map
*pet_expr_access_get_tagged_access(
2531 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2533 isl_union_map
*access
;
2538 access
= pet_expr_access_get_access(expr
, type
);
2539 access
= pet_expr_tag_access(expr
, access
);
2544 /* Return the relation mapping domain iterations to all possibly
2545 * read data elements, with its domain tagged with the reference
2548 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_read(
2549 __isl_keep pet_expr
*expr
)
2551 return pet_expr_access_get_tagged_access(expr
,
2552 pet_expr_access_may_read
);
2555 /* Return the relation mapping domain iterations to all possibly
2556 * written data elements, with its domain tagged with the reference
2559 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_write(
2560 __isl_keep pet_expr
*expr
)
2562 return pet_expr_access_get_tagged_access(expr
,
2563 pet_expr_access_may_write
);
2566 /* Return the operation type of operation expression "expr".
2568 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
2572 if (expr
->type
!= pet_expr_op
)
2573 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2574 "not an operation expression", return pet_op_last
);
2579 /* Replace the operation type of operation expression "expr" by "type".
2581 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
2582 enum pet_op_type type
)
2585 return pet_expr_free(expr
);
2586 if (expr
->type
!= pet_expr_op
)
2587 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2588 "not an operation expression",
2589 return pet_expr_free(expr
));
2590 if (expr
->op
== type
)
2592 expr
= pet_expr_cow(expr
);
2600 /* Return the name of the function called by "expr".
2602 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
2606 if (expr
->type
!= pet_expr_call
)
2607 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2608 "not a call expression", return NULL
);
2609 return expr
->c
.name
;
2612 /* Replace the name of the function called by "expr" by "name".
2614 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
2615 __isl_keep
const char *name
)
2617 expr
= pet_expr_cow(expr
);
2619 return pet_expr_free(expr
);
2620 if (expr
->type
!= pet_expr_call
)
2621 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2622 "not a call expression", return pet_expr_free(expr
));
2624 expr
->c
.name
= strdup(name
);
2626 return pet_expr_free(expr
);
2630 /* Does the call expression "expr" have an associated function summary?
2632 int pet_expr_call_has_summary(__isl_keep pet_expr
*expr
)
2636 if (expr
->type
!= pet_expr_call
)
2637 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2638 "not a call expression", return -1);
2640 return expr
->c
.summary
!= NULL
;
2643 /* Return a copy of the function summary associated to
2644 * the call expression "expr".
2646 __isl_give pet_function_summary
*pet_expr_call_get_summary(
2647 __isl_keep pet_expr
*expr
)
2651 if (expr
->type
!= pet_expr_call
)
2652 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2653 "not a call expression", return NULL
);
2655 return pet_function_summary_copy(expr
->c
.summary
);
2658 /* Replace the function summary associated to the call expression "expr"
2661 __isl_give pet_expr
*pet_expr_call_set_summary(__isl_take pet_expr
*expr
,
2662 __isl_take pet_function_summary
*summary
)
2664 expr
= pet_expr_cow(expr
);
2665 if (!expr
|| !summary
)
2667 if (expr
->type
!= pet_expr_call
)
2668 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2669 "not a call expression", goto error
);
2670 pet_function_summary_free(expr
->c
.summary
);
2671 expr
->c
.summary
= summary
;
2674 pet_function_summary_free(summary
);
2675 return pet_expr_free(expr
);
2678 /* Replace the type of the cast performed by "expr" by "name".
2680 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
2681 __isl_keep
const char *name
)
2683 expr
= pet_expr_cow(expr
);
2685 return pet_expr_free(expr
);
2686 if (expr
->type
!= pet_expr_cast
)
2687 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2688 "not a cast expression", return pet_expr_free(expr
));
2689 free(expr
->type_name
);
2690 expr
->type_name
= strdup(name
);
2691 if (!expr
->type_name
)
2692 return pet_expr_free(expr
);
2696 /* Return the value of the integer represented by "expr".
2698 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
2702 if (expr
->type
!= pet_expr_int
)
2703 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2704 "not an int expression", return NULL
);
2706 return isl_val_copy(expr
->i
);
2709 /* Replace the value of the integer represented by "expr" by "v".
2711 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
2712 __isl_take isl_val
*v
)
2714 expr
= pet_expr_cow(expr
);
2717 if (expr
->type
!= pet_expr_int
)
2718 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2719 "not an int expression", goto error
);
2720 isl_val_free(expr
->i
);
2726 pet_expr_free(expr
);
2730 /* Replace the value and string representation of the double
2731 * represented by "expr" by "d" and "s".
2733 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2734 double d
, __isl_keep
const char *s
)
2736 expr
= pet_expr_cow(expr
);
2738 return pet_expr_free(expr
);
2739 if (expr
->type
!= pet_expr_double
)
2740 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2741 "not a double expression", return pet_expr_free(expr
));
2744 expr
->d
.s
= strdup(s
);
2746 return pet_expr_free(expr
);
2750 /* Return a string representation of the double expression "expr".
2752 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2756 if (expr
->type
!= pet_expr_double
)
2757 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2758 "not a double expression", return NULL
);
2759 return strdup(expr
->d
.s
);
2762 /* Return a piecewise affine expression defined on the specified domain
2763 * that represents NaN.
2765 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2767 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2770 /* This function is called when we come across an access that is
2771 * nested in what is supposed to be an affine expression.
2772 * "pc" is the context in which the affine expression is created.
2773 * If nesting is allowed in "pc", we return an affine expression that is
2774 * equal to a new parameter corresponding to this nested access.
2775 * Otherwise, we return NaN.
2777 * Note that we currently don't allow nested accesses themselves
2778 * to contain any nested accesses, so we check if "expr" itself
2779 * involves any nested accesses (either explicitly as arguments
2780 * or implicitly through parameters) and return NaN if it does.
2782 * The new parameter is resolved in resolve_nested.
2784 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2785 __isl_keep pet_context
*pc
)
2790 isl_local_space
*ls
;
2796 if (!pet_context_allow_nesting(pc
))
2797 return non_affine(pet_context_get_space(pc
));
2799 if (pet_expr_get_type(expr
) != pet_expr_access
)
2800 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2801 "not an access expression", return NULL
);
2803 if (expr
->n_arg
> 0)
2804 return non_affine(pet_context_get_space(pc
));
2806 space
= pet_expr_access_get_parameter_space(expr
);
2807 nested
= pet_nested_any_in_space(space
);
2808 isl_space_free(space
);
2810 return non_affine(pet_context_get_space(pc
));
2812 ctx
= pet_expr_get_ctx(expr
);
2813 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2814 space
= pet_context_get_space(pc
);
2815 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2817 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2818 ls
= isl_local_space_from_space(space
);
2819 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2821 return isl_pw_aff_from_aff(aff
);
2824 /* Extract an affine expression from the access pet_expr "expr".
2825 * "pc" is the context in which the affine expression is created.
2827 * If "expr" is actually an affine expression rather than
2828 * a real access, then we return that expression.
2829 * Otherwise, we require that "expr" is of an integral type.
2830 * If not, we return NaN.
2832 * If the variable has been assigned a known affine expression,
2833 * then we return that expression.
2835 * Otherwise, we return an expression that is equal to a parameter
2836 * representing "expr" (if "allow_nested" is set).
2838 static __isl_give isl_pw_aff
*extract_affine_from_access(
2839 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2844 if (pet_expr_is_affine(expr
))
2845 return pet_expr_get_affine(expr
);
2847 if (pet_expr_get_type_size(expr
) == 0)
2848 return non_affine(pet_context_get_space(pc
));
2850 if (!pet_expr_is_scalar_access(expr
))
2851 return nested_access(expr
, pc
);
2853 id
= pet_expr_access_get_id(expr
);
2854 if (pet_context_is_assigned(pc
, id
))
2855 return pet_context_get_value(pc
, id
);
2858 return nested_access(expr
, pc
);
2861 /* Construct an affine expression from the integer constant "expr".
2862 * "pc" is the context in which the affine expression is created.
2864 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2865 __isl_keep pet_context
*pc
)
2867 isl_local_space
*ls
;
2873 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2874 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2876 return isl_pw_aff_from_aff(aff
);
2879 /* Extract an affine expression from an addition or subtraction operation.
2880 * Return NaN if we are unable to extract an affine expression.
2882 * "pc" is the context in which the affine expression is created.
2884 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2885 __isl_keep pet_context
*pc
)
2892 if (expr
->n_arg
!= 2)
2893 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2894 "expecting two arguments", return NULL
);
2896 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2897 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2899 switch (pet_expr_op_get_type(expr
)) {
2901 return isl_pw_aff_add(lhs
, rhs
);
2903 return isl_pw_aff_sub(lhs
, rhs
);
2905 isl_pw_aff_free(lhs
);
2906 isl_pw_aff_free(rhs
);
2907 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2908 "not an addition or subtraction operation",
2914 /* Extract an affine expression from an integer division or a modulo operation.
2915 * Return NaN if we are unable to extract an affine expression.
2917 * "pc" is the context in which the affine expression is created.
2919 * In particular, if "expr" is lhs/rhs, then return
2921 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2923 * If "expr" is lhs%rhs, then return
2925 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2927 * If the second argument (rhs) is not a (positive) integer constant,
2928 * then we fail to extract an affine expression.
2930 * We simplify the result in the context of the domain of "pc" in case
2931 * this domain implies that lhs >= 0 (or < 0).
2933 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2934 __isl_keep pet_context
*pc
)
2943 if (expr
->n_arg
!= 2)
2944 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2945 "expecting two arguments", return NULL
);
2947 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2949 is_cst
= isl_pw_aff_is_cst(rhs
);
2950 if (is_cst
< 0 || !is_cst
) {
2951 isl_pw_aff_free(rhs
);
2952 return non_affine(pet_context_get_space(pc
));
2955 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2957 switch (pet_expr_op_get_type(expr
)) {
2959 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
2962 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
2965 isl_pw_aff_free(lhs
);
2966 isl_pw_aff_free(rhs
);
2967 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2968 "not a div or mod operator", return NULL
);
2971 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
2974 /* Extract an affine expression from a multiplication operation.
2975 * Return NaN if we are unable to extract an affine expression.
2976 * In particular, if neither of the arguments is a (piecewise) constant
2977 * then we return NaN.
2979 * "pc" is the context in which the affine expression is created.
2981 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2982 __isl_keep pet_context
*pc
)
2984 int lhs_cst
, rhs_cst
;
2990 if (expr
->n_arg
!= 2)
2991 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2992 "expecting two arguments", return NULL
);
2994 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2995 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2997 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2998 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2999 if (lhs_cst
>= 0 && rhs_cst
>= 0 && (lhs_cst
|| rhs_cst
))
3000 return isl_pw_aff_mul(lhs
, rhs
);
3002 isl_pw_aff_free(lhs
);
3003 isl_pw_aff_free(rhs
);
3005 if (lhs_cst
< 0 || rhs_cst
< 0)
3008 return non_affine(pet_context_get_space(pc
));
3011 /* Extract an affine expression from a negation operation.
3012 * Return NaN if we are unable to extract an affine expression.
3014 * "pc" is the context in which the affine expression is created.
3016 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
3017 __isl_keep pet_context
*pc
)
3023 if (expr
->n_arg
!= 1)
3024 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3025 "expecting one argument", return NULL
);
3027 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
3028 return isl_pw_aff_neg(res
);
3031 /* Extract an affine expression from a conditional operation.
3032 * Return NaN if we are unable to extract an affine expression.
3034 * "pc" is the context in which the affine expression is created.
3036 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
3037 __isl_keep pet_context
*pc
)
3039 isl_pw_aff
*cond
, *lhs
, *rhs
;
3043 if (expr
->n_arg
!= 3)
3044 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3045 "expecting three arguments", return NULL
);
3047 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3048 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
3049 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
3051 return isl_pw_aff_cond(cond
, lhs
, rhs
);
3054 /* Limit the domain of "pwaff" to those elements where the function
3057 * 2^{width-1} <= pwaff < 2^{width-1}
3059 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
3064 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
3065 isl_local_space
*ls
= isl_local_space_from_space(space
);
3070 ctx
= isl_pw_aff_get_ctx(pwaff
);
3071 v
= isl_val_int_from_ui(ctx
, width
- 1);
3072 v
= isl_val_2exp(v
);
3074 bound
= isl_aff_zero_on_domain(ls
);
3075 bound
= isl_aff_add_constant_val(bound
, v
);
3076 b
= isl_pw_aff_from_aff(bound
);
3078 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
3079 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3081 b
= isl_pw_aff_neg(b
);
3082 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
3083 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3088 /* Handle potential overflows on signed computations.
3090 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
3091 * then we adjust the domain of "pa" to avoid overflows.
3093 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
3097 struct pet_options
*options
;
3102 ctx
= isl_pw_aff_get_ctx(pa
);
3103 options
= isl_ctx_peek_pet_options(ctx
);
3104 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
3105 pa
= avoid_overflow(pa
, width
);
3110 /* Extract an affine expression from some an operation.
3111 * Return NaN if we are unable to extract an affine expression.
3112 * If the result of a binary (non boolean) operation is unsigned,
3113 * then we wrap it based on the size of the type. If the result is signed,
3114 * then we ensure that no overflow occurs.
3116 * "pc" is the context in which the affine expression is created.
3118 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
3119 __isl_keep pet_context
*pc
)
3124 switch (pet_expr_op_get_type(expr
)) {
3127 res
= extract_affine_add_sub(expr
, pc
);
3131 res
= extract_affine_div_mod(expr
, pc
);
3134 res
= extract_affine_mul(expr
, pc
);
3137 return extract_affine_neg(expr
, pc
);
3139 return extract_affine_cond(expr
, pc
);
3149 return pet_expr_extract_affine_condition(expr
, pc
);
3151 return non_affine(pet_context_get_space(pc
));
3156 if (isl_pw_aff_involves_nan(res
)) {
3157 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
3158 isl_pw_aff_free(res
);
3159 return non_affine(space
);
3162 type_size
= pet_expr_get_type_size(expr
);
3164 res
= pet_wrap_pw_aff(res
, type_size
);
3166 res
= signed_overflow(res
, -type_size
);
3171 /* Internal data structure for affine builtin function declarations.
3173 * "pencil" is set if the builtin is pencil specific.
3174 * "n_args" is the number of arguments the function takes.
3175 * "name" is the function name.
3177 struct affine_builtin_decl
{
3183 static struct affine_builtin_decl affine_builtins
[] = {
3191 { 0, 2, "intFloor" },
3192 { 0, 2, "intCeil" },
3197 /* List of min and max builtin functions.
3199 static const char *min_max_builtins
[] = {
3200 "min", "imin", "umin",
3201 "max", "imax", "umax"
3204 /* Is a function call to "name" with "n_args" arguments a call to a
3205 * builtin function for which we can construct an affine expression?
3206 * pencil specific builtins are only recognized if "pencil" is set.
3208 static int is_affine_builtin(int pencil
, int n_args
, const char *name
)
3212 for (i
= 0; i
< ARRAY_SIZE(affine_builtins
); ++i
) {
3213 struct affine_builtin_decl
*decl
= &affine_builtins
[i
];
3215 if (decl
->pencil
&& !pencil
)
3217 if (decl
->n_args
== n_args
&& !strcmp(decl
->name
, name
))
3224 /* Is function "name" a known min or max builtin function?
3226 static int is_min_or_max_builtin(const char *name
)
3230 for (i
= 0; i
< ARRAY_SIZE(min_max_builtins
); ++i
)
3231 if (!strcmp(min_max_builtins
[i
], name
))
3237 /* Extract an affine expression from some special function calls.
3238 * Return NaN if we are unable to extract an affine expression.
3239 * In particular, we handle "min", "max", "ceild", "floord",
3240 * "intMod", "intFloor" and "intCeil".
3241 * In case of the latter five, the second argument needs to be
3242 * a (positive) integer constant.
3243 * If the pencil option is set, then we also handle "{i,u}min" and
3246 * "pc" is the context in which the affine expression is created.
3248 static __isl_give isl_pw_aff
*extract_affine_from_call(
3249 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3252 isl_pw_aff
*aff1
, *aff2
;
3255 struct pet_options
*options
;
3259 ctx
= pet_expr_get_ctx(expr
);
3260 options
= isl_ctx_peek_pet_options(ctx
);
3262 n
= pet_expr_get_n_arg(expr
);
3263 name
= pet_expr_call_get_name(expr
);
3264 if (!is_affine_builtin(options
->pencil
, n
, name
))
3265 return non_affine(pet_context_get_space(pc
));
3267 if (is_min_or_max_builtin(name
)) {
3268 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3269 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
3271 if (strstr(name
, "min"))
3272 aff1
= isl_pw_aff_min(aff1
, aff2
);
3274 aff1
= isl_pw_aff_max(aff1
, aff2
);
3275 } else if (!strcmp(name
, "intMod")) {
3278 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3279 return non_affine(pet_context_get_space(pc
));
3280 v
= pet_expr_int_get_val(expr
->args
[1]);
3281 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3282 aff1
= isl_pw_aff_mod_val(aff1
, v
);
3286 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3287 return non_affine(pet_context_get_space(pc
));
3288 v
= pet_expr_int_get_val(expr
->args
[1]);
3289 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3290 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
3291 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
3292 aff1
= isl_pw_aff_floor(aff1
);
3294 aff1
= isl_pw_aff_ceil(aff1
);
3300 /* Extract an affine expression from "expr", if possible.
3301 * Otherwise return NaN.
3303 * "pc" is the context in which the affine expression is created.
3305 * Store the result in "pc" such that it can be reused in case
3306 * pet_expr_extract_affine is called again on the same pair of
3309 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
3310 __isl_keep pet_context
*pc
)
3312 isl_maybe_isl_pw_aff m
;
3318 m
= pet_context_get_extracted_affine(pc
, expr
);
3319 if (m
.valid
< 0 || m
.valid
)
3322 switch (pet_expr_get_type(expr
)) {
3323 case pet_expr_access
:
3324 pa
= extract_affine_from_access(expr
, pc
);
3327 pa
= extract_affine_from_int(expr
, pc
);
3330 pa
= extract_affine_from_op(expr
, pc
);
3333 pa
= extract_affine_from_call(expr
, pc
);
3336 case pet_expr_double
:
3337 case pet_expr_error
:
3338 pa
= non_affine(pet_context_get_space(pc
));
3342 if (pet_context_set_extracted_affine(pc
, expr
, pa
) < 0)
3343 return isl_pw_aff_free(pa
);
3348 /* Extract an affine expressions representing the comparison "LHS op RHS"
3349 * Return NaN if we are unable to extract such an affine expression.
3351 * "pc" is the context in which the affine expression is created.
3353 * If the comparison is of the form
3357 * then the expression is constructed as the conjunction of
3362 * A similar optimization is performed for max(a,b) <= c.
3363 * We do this because that will lead to simpler representations
3364 * of the expression.
3365 * If isl is ever enhanced to explicitly deal with min and max expressions,
3366 * this optimization can be removed.
3368 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
3369 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
3370 __isl_keep pet_context
*pc
)
3372 isl_pw_aff
*lhs_pa
, *rhs_pa
;
3374 if (op
== pet_op_gt
)
3375 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
3376 if (op
== pet_op_ge
)
3377 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
3379 if (op
== pet_op_lt
|| op
== pet_op_le
) {
3380 if (pet_expr_is_min(rhs
)) {
3381 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3383 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3385 return pet_and(lhs_pa
, rhs_pa
);
3387 if (pet_expr_is_max(lhs
)) {
3388 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
3390 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
3392 return pet_and(lhs_pa
, rhs_pa
);
3396 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
3397 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
3399 return pet_comparison(op
, lhs_pa
, rhs_pa
);
3402 /* Extract an affine expressions from the comparison "expr".
3403 * Return NaN if we are unable to extract such an affine expression.
3405 * "pc" is the context in which the affine expression is created.
3407 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
3408 __isl_keep pet_context
*pc
)
3410 enum pet_op_type type
;
3414 if (expr
->n_arg
!= 2)
3415 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3416 "expecting two arguments", return NULL
);
3418 type
= pet_expr_op_get_type(expr
);
3419 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
3423 /* Extract an affine expression representing the boolean operation
3424 * expressed by "expr".
3425 * Return NaN if we are unable to extract an affine expression.
3427 * "pc" is the context in which the affine expression is created.
3429 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
3430 __isl_keep pet_context
*pc
)
3432 isl_pw_aff
*lhs
, *rhs
;
3438 n
= pet_expr_get_n_arg(expr
);
3439 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3441 return pet_not(lhs
);
3443 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
3444 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
3447 /* Extract the affine expression "expr != 0 ? 1 : 0".
3448 * Return NaN if we are unable to extract an affine expression.
3450 * "pc" is the context in which the affine expression is created.
3452 static __isl_give isl_pw_aff
*extract_implicit_condition(
3453 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3457 res
= pet_expr_extract_affine(expr
, pc
);
3458 return pet_to_bool(res
);
3461 /* Extract a boolean affine expression from "expr".
3462 * Return NaN if we are unable to extract an affine expression.
3464 * "pc" is the context in which the affine expression is created.
3466 * If "expr" is neither a comparison nor a boolean operation,
3467 * then we assume it is an affine expression and return the
3468 * boolean expression "expr != 0 ? 1 : 0".
3470 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
3471 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3476 if (pet_expr_is_comparison(expr
))
3477 return extract_comparison(expr
, pc
);
3478 if (pet_expr_is_boolean(expr
))
3479 return extract_boolean(expr
, pc
);
3481 return extract_implicit_condition(expr
, pc
);
3484 /* Check if "expr" is an assume expression and if its single argument
3485 * can be converted to an affine expression in the context of "pc".
3486 * If so, replace the argument by the affine expression.
3488 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
3489 __isl_keep pet_context
*pc
)
3492 isl_multi_pw_aff
*index
;
3496 if (!pet_expr_is_assume(expr
))
3498 if (expr
->n_arg
!= 1)
3499 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3500 "expecting one argument", return pet_expr_free(expr
));
3502 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3504 return pet_expr_free(expr
);
3505 if (isl_pw_aff_involves_nan(cond
)) {
3506 isl_pw_aff_free(cond
);
3510 index
= isl_multi_pw_aff_from_pw_aff(cond
);
3511 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
3516 /* Return the number of bits needed to represent the type of "expr".
3517 * See the description of the type_size field of pet_expr.
3519 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
3521 return expr
? expr
->type_size
: 0;
3524 /* Replace the number of bits needed to represent the type of "expr"
3526 * See the description of the type_size field of pet_expr.
3528 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
3531 expr
= pet_expr_cow(expr
);
3535 expr
->type_size
= type_size
;
3540 /* Extend an access expression "expr" with an additional index "index".
3541 * In particular, add "index" as an extra argument to "expr" and
3542 * adjust the index expression of "expr" to refer to this extra argument.
3543 * The caller is responsible for calling pet_expr_access_set_depth
3544 * to update the corresponding access relation.
3546 * Note that we only collect the individual index expressions as
3547 * arguments of "expr" here.
3548 * An attempt to integrate them into the index expression of "expr"
3549 * is performed in pet_expr_access_plug_in_args.
3551 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
3552 __isl_take pet_expr
*index
)
3556 isl_local_space
*ls
;
3559 expr
= pet_expr_cow(expr
);
3560 if (!expr
|| !index
)
3562 if (expr
->type
!= pet_expr_access
)
3563 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3564 "not an access pet_expr", goto error
);
3566 n
= pet_expr_get_n_arg(expr
);
3567 expr
= pet_expr_insert_arg(expr
, n
, index
);
3571 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3572 ls
= isl_local_space_from_space(space
);
3573 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
3574 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
3575 if (!expr
->acc
.index
)
3576 return pet_expr_free(expr
);
3580 pet_expr_free(expr
);
3581 pet_expr_free(index
);
3585 /* Extend an access expression "expr" with an additional member acces to "id".
3586 * In particular, extend the index expression of "expr" to include
3587 * the additional member access.
3588 * The caller is responsible for calling pet_expr_access_set_depth
3589 * to update the corresponding access relation.
3591 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
3592 __isl_take isl_id
*id
)
3595 isl_multi_pw_aff
*field_access
;
3597 expr
= pet_expr_cow(expr
);
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 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3605 space
= isl_space_from_domain(space
);
3606 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
3607 field_access
= isl_multi_pw_aff_zero(space
);
3608 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
3609 if (!expr
->acc
.index
)
3610 return pet_expr_free(expr
);
3614 pet_expr_free(expr
);
3619 /* Prefix the access expression "expr" with "prefix".
3620 * If "add" is set, then it is not the index expression "prefix" itself
3621 * that was passed to the function, but its address.
3623 __isl_give pet_expr
*pet_expr_access_patch(__isl_take pet_expr
*expr
,
3624 __isl_take isl_multi_pw_aff
*prefix
, int add
)
3626 enum pet_expr_access_type type
;
3628 expr
= pet_expr_cow(expr
);
3629 if (!expr
|| !prefix
)
3631 if (expr
->type
!= pet_expr_access
)
3632 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3633 "not an access pet_expr", goto error
);
3635 expr
->acc
.depth
+= isl_multi_pw_aff_dim(prefix
, isl_dim_out
) - add
;
3636 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
3637 if (!expr
->acc
.access
[type
])
3639 expr
->acc
.access
[type
] = pet_patch_union_map(
3640 isl_multi_pw_aff_copy(prefix
), expr
->acc
.access
[type
],
3642 if (!expr
->acc
.access
[type
])
3645 expr
->acc
.index
= pet_patch_multi_pw_aff(prefix
, expr
->acc
.index
, add
);
3646 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
3647 return pet_expr_free(expr
);
3651 pet_expr_free(expr
);
3652 isl_multi_pw_aff_free(prefix
);
3656 /* Dump the arguments of "expr" to "p" as a YAML sequence keyed
3657 * by "args", if there are any such arguments.
3659 static __isl_give isl_printer
*dump_arguments(__isl_keep pet_expr
*expr
,
3660 __isl_take isl_printer
*p
)
3664 if (expr
->n_arg
== 0)
3667 p
= isl_printer_print_str(p
, "args");
3668 p
= isl_printer_yaml_next(p
);
3669 p
= isl_printer_yaml_start_sequence(p
);
3670 for (i
= 0; i
< expr
->n_arg
; ++i
) {
3671 p
= pet_expr_print(expr
->args
[i
], p
);
3672 p
= isl_printer_yaml_next(p
);
3674 p
= isl_printer_yaml_end_sequence(p
);
3679 /* Print "expr" to "p" in YAML format.
3681 __isl_give isl_printer
*pet_expr_print(__isl_keep pet_expr
*expr
,
3682 __isl_take isl_printer
*p
)
3685 return isl_printer_free(p
);
3687 switch (expr
->type
) {
3688 case pet_expr_double
:
3689 p
= isl_printer_print_str(p
, expr
->d
.s
);
3692 p
= isl_printer_print_val(p
, expr
->i
);
3694 case pet_expr_access
:
3695 p
= isl_printer_yaml_start_mapping(p
);
3696 if (expr
->acc
.ref_id
) {
3697 p
= isl_printer_print_str(p
, "ref_id");
3698 p
= isl_printer_yaml_next(p
);
3699 p
= isl_printer_print_id(p
, expr
->acc
.ref_id
);
3700 p
= isl_printer_yaml_next(p
);
3702 p
= isl_printer_print_str(p
, "index");
3703 p
= isl_printer_yaml_next(p
);
3704 p
= isl_printer_print_multi_pw_aff(p
, expr
->acc
.index
);
3705 p
= isl_printer_yaml_next(p
);
3706 p
= isl_printer_print_str(p
, "depth");
3707 p
= isl_printer_yaml_next(p
);
3708 p
= isl_printer_print_int(p
, expr
->acc
.depth
);
3709 p
= isl_printer_yaml_next(p
);
3710 if (expr
->acc
.kill
) {
3711 p
= isl_printer_print_str(p
, "kill");
3712 p
= isl_printer_yaml_next(p
);
3713 p
= isl_printer_print_int(p
, 1);
3714 p
= isl_printer_yaml_next(p
);
3716 p
= isl_printer_print_str(p
, "read");
3717 p
= isl_printer_yaml_next(p
);
3718 p
= isl_printer_print_int(p
, expr
->acc
.read
);
3719 p
= isl_printer_yaml_next(p
);
3720 p
= isl_printer_print_str(p
, "write");
3721 p
= isl_printer_yaml_next(p
);
3722 p
= isl_printer_print_int(p
, expr
->acc
.write
);
3723 p
= isl_printer_yaml_next(p
);
3725 if (expr
->acc
.access
[pet_expr_access_may_read
]) {
3726 p
= isl_printer_print_str(p
, "may_read");
3727 p
= isl_printer_yaml_next(p
);
3728 p
= isl_printer_print_union_map(p
,
3729 expr
->acc
.access
[pet_expr_access_may_read
]);
3730 p
= isl_printer_yaml_next(p
);
3732 if (expr
->acc
.access
[pet_expr_access_may_write
]) {
3733 p
= isl_printer_print_str(p
, "may_write");
3734 p
= isl_printer_yaml_next(p
);
3735 p
= isl_printer_print_union_map(p
,
3736 expr
->acc
.access
[pet_expr_access_may_write
]);
3737 p
= isl_printer_yaml_next(p
);
3739 if (expr
->acc
.access
[pet_expr_access_must_write
]) {
3740 p
= isl_printer_print_str(p
, "must_write");
3741 p
= isl_printer_yaml_next(p
);
3742 p
= isl_printer_print_union_map(p
,
3743 expr
->acc
.access
[pet_expr_access_must_write
]);
3744 p
= isl_printer_yaml_next(p
);
3746 p
= dump_arguments(expr
, p
);
3747 p
= isl_printer_yaml_end_mapping(p
);
3750 p
= isl_printer_yaml_start_mapping(p
);
3751 p
= isl_printer_print_str(p
, "op");
3752 p
= isl_printer_yaml_next(p
);
3753 p
= isl_printer_print_str(p
, op_str
[expr
->op
]);
3754 p
= isl_printer_yaml_next(p
);
3755 p
= dump_arguments(expr
, p
);
3756 p
= isl_printer_yaml_end_mapping(p
);
3759 p
= isl_printer_yaml_start_mapping(p
);
3760 p
= isl_printer_print_str(p
, "call");
3761 p
= isl_printer_yaml_next(p
);
3762 p
= isl_printer_print_str(p
, expr
->c
.name
);
3763 p
= isl_printer_print_str(p
, "/");
3764 p
= isl_printer_print_int(p
, expr
->n_arg
);
3765 p
= isl_printer_yaml_next(p
);
3766 p
= dump_arguments(expr
, p
);
3767 if (expr
->c
.summary
) {
3768 p
= isl_printer_print_str(p
, "summary");
3769 p
= isl_printer_yaml_next(p
);
3770 p
= pet_function_summary_print(expr
->c
.summary
, p
);
3772 p
= isl_printer_yaml_end_mapping(p
);
3775 p
= isl_printer_yaml_start_mapping(p
);
3776 p
= isl_printer_print_str(p
, "cast");
3777 p
= isl_printer_yaml_next(p
);
3778 p
= isl_printer_print_str(p
, expr
->type_name
);
3779 p
= isl_printer_yaml_next(p
);
3780 p
= dump_arguments(expr
, p
);
3781 p
= isl_printer_yaml_end_mapping(p
);
3783 case pet_expr_error
:
3784 p
= isl_printer_print_str(p
, "ERROR");
3791 /* Dump "expr" to stderr with indentation "indent".
3793 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
3800 p
= isl_printer_to_file(pet_expr_get_ctx(expr
), stderr
);
3801 p
= isl_printer_set_indent(p
, indent
);
3802 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_BLOCK
);
3803 p
= isl_printer_start_line(p
);
3804 p
= pet_expr_print(expr
, p
);
3806 isl_printer_free(p
);
3809 void pet_expr_dump(__isl_keep pet_expr
*expr
)
3811 pet_expr_dump_with_indent(expr
, 0);