2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
41 #include <isl/space.h>
42 #include <isl/local_space.h>
45 #include <isl/union_set.h>
46 #include <isl/union_map.h>
47 #include <isl/printer.h>
56 #include "value_bounds.h"
59 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
61 static char *type_str
[] = {
62 [pet_expr_access
] = "access",
63 [pet_expr_call
] = "call",
64 [pet_expr_cast
] = "cast",
65 [pet_expr_double
] = "double",
66 [pet_expr_int
] = "int",
70 static char *op_str
[] = {
71 [pet_op_add_assign
] = "+=",
72 [pet_op_sub_assign
] = "-=",
73 [pet_op_mul_assign
] = "*=",
74 [pet_op_div_assign
] = "/=",
75 [pet_op_and_assign
] = "&=",
76 [pet_op_xor_assign
] = "^=",
77 [pet_op_or_assign
] = "|=",
78 [pet_op_assign
] = "=",
93 [pet_op_post_inc
] = "++",
94 [pet_op_post_dec
] = "--",
95 [pet_op_pre_inc
] = "++",
96 [pet_op_pre_dec
] = "--",
97 [pet_op_address_of
] = "&",
102 [pet_op_land
] = "&&",
105 [pet_op_cond
] = "?:",
106 [pet_op_assume
] = "assume",
107 [pet_op_kill
] = "kill"
110 const char *pet_op_str(enum pet_op_type op
)
115 int pet_op_is_inc_dec(enum pet_op_type op
)
117 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
118 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
121 const char *pet_type_str(enum pet_expr_type type
)
123 return type_str
[type
];
126 enum pet_op_type
pet_str_op(const char *str
)
130 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
131 if (!strcmp(op_str
[i
], str
))
137 enum pet_expr_type
pet_str_type(const char *str
)
141 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
142 if (!strcmp(type_str
[i
], str
))
148 /* Construct a pet_expr of the given type.
150 __isl_give pet_expr
*pet_expr_alloc(isl_ctx
*ctx
, enum pet_expr_type type
)
154 expr
= isl_calloc_type(ctx
, struct pet_expr
);
166 /* Construct an access pet_expr from an index expression.
167 * By default, the access is considered to be a read access.
168 * The initial depth is set from the index expression and
169 * may still be updated by the caller before the access relation
172 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
179 ctx
= isl_multi_pw_aff_get_ctx(index
);
180 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
187 expr
= pet_expr_access_set_index(expr
, index
);
191 isl_multi_pw_aff_free(index
);
195 /* Extend the range of "access" with "n" dimensions, retaining
196 * the tuple identifier on this range.
198 * If "access" represents a member access, then extend the range
201 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
205 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
207 if (!isl_map_range_is_wrapping(access
)) {
208 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
212 domain
= isl_map_copy(access
);
213 domain
= isl_map_range_factor_domain(domain
);
214 access
= isl_map_range_factor_range(access
);
215 access
= extend_range(access
, n
);
216 access
= isl_map_range_product(domain
, access
);
219 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
224 /* Does the access expression "expr" have any explicit access relation?
226 isl_bool
pet_expr_access_has_any_access_relation(__isl_keep pet_expr
*expr
)
228 enum pet_expr_access_type type
;
231 return isl_bool_error
;
233 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
)
234 if (expr
->acc
.access
[type
])
235 return isl_bool_true
;
237 return isl_bool_false
;
240 /* Are all relevant access relations explicitly available in "expr"?
242 static int has_relevant_access_relations(__isl_keep pet_expr
*expr
)
247 if (expr
->acc
.kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
249 if (expr
->acc
.read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
251 if (expr
->acc
.write
&&
252 (!expr
->acc
.access
[pet_expr_access_may_write
] ||
253 !expr
->acc
.access
[pet_expr_access_must_write
]))
259 /* Replace the depth of the access expr "expr" by "depth".
261 * To avoid inconsistencies between the depth and the access relation,
262 * we currently do not allow the depth to change once the access relation
263 * has been set or computed.
265 __isl_give pet_expr
*pet_expr_access_set_depth(__isl_take pet_expr
*expr
,
270 if (expr
->acc
.depth
== depth
)
272 if (pet_expr_access_has_any_access_relation(expr
))
273 isl_die(pet_expr_get_ctx(expr
), isl_error_unsupported
,
274 "depth cannot be changed after access relation "
275 "has been set or computed", return pet_expr_free(expr
));
277 expr
= pet_expr_cow(expr
);
280 expr
->acc
.depth
= depth
;
285 /* Construct a pet_expr that kills the elements specified by
286 * the index expression "index" and the access relation "access".
288 __isl_give pet_expr
*pet_expr_kill_from_access_and_index(
289 __isl_take isl_map
*access
, __isl_take isl_multi_pw_aff
*index
)
294 if (!access
|| !index
)
297 expr
= pet_expr_from_index(index
);
298 expr
= pet_expr_access_set_read(expr
, 0);
299 expr
= pet_expr_access_set_kill(expr
, 1);
300 depth
= isl_map_dim(access
, isl_dim_out
);
301 expr
= pet_expr_access_set_depth(expr
, depth
);
302 expr
= pet_expr_access_set_access(expr
, pet_expr_access_killed
,
303 isl_union_map_from_map(access
));
304 return pet_expr_new_unary(0, pet_op_kill
, expr
);
306 isl_map_free(access
);
307 isl_multi_pw_aff_free(index
);
311 /* Construct a unary pet_expr that performs "op" on "arg",
312 * where the result is represented using a type of "type_size" bits
313 * (may be zero if unknown or if the type is not an integer).
315 __isl_give pet_expr
*pet_expr_new_unary(int type_size
, enum pet_op_type op
,
316 __isl_take pet_expr
*arg
)
323 ctx
= pet_expr_get_ctx(arg
);
324 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
325 expr
= pet_expr_set_n_arg(expr
, 1);
330 expr
->type_size
= type_size
;
331 expr
->args
[pet_un_arg
] = arg
;
339 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
340 * where the result is represented using a type of "type_size" bits
341 * (may be zero if unknown or if the type is not an integer).
343 __isl_give pet_expr
*pet_expr_new_binary(int type_size
, enum pet_op_type op
,
344 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
351 ctx
= pet_expr_get_ctx(lhs
);
352 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
353 expr
= pet_expr_set_n_arg(expr
, 2);
358 expr
->type_size
= type_size
;
359 expr
->args
[pet_bin_lhs
] = lhs
;
360 expr
->args
[pet_bin_rhs
] = rhs
;
369 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
371 __isl_give pet_expr
*pet_expr_new_ternary(__isl_take pet_expr
*cond
,
372 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
377 if (!cond
|| !lhs
|| !rhs
)
379 ctx
= pet_expr_get_ctx(cond
);
380 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
381 expr
= pet_expr_set_n_arg(expr
, 3);
385 expr
->op
= pet_op_cond
;
386 expr
->args
[pet_ter_cond
] = cond
;
387 expr
->args
[pet_ter_true
] = lhs
;
388 expr
->args
[pet_ter_false
] = rhs
;
398 /* Construct a call pet_expr that calls function "name" with "n_arg"
399 * arguments. The caller is responsible for filling in the arguments.
401 __isl_give pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
406 expr
= pet_expr_alloc(ctx
, pet_expr_call
);
407 expr
= pet_expr_set_n_arg(expr
, n_arg
);
411 expr
->c
.name
= strdup(name
);
413 return pet_expr_free(expr
);
418 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
420 __isl_give pet_expr
*pet_expr_new_cast(const char *type_name
,
421 __isl_take pet_expr
*arg
)
429 ctx
= pet_expr_get_ctx(arg
);
430 expr
= pet_expr_alloc(ctx
, pet_expr_cast
);
431 expr
= pet_expr_set_n_arg(expr
, 1);
435 expr
->type_name
= strdup(type_name
);
436 if (!expr
->type_name
)
448 /* Construct a pet_expr that represents the double "d".
450 __isl_give pet_expr
*pet_expr_new_double(isl_ctx
*ctx
,
451 double val
, const char *s
)
455 expr
= pet_expr_alloc(ctx
, pet_expr_double
);
460 expr
->d
.s
= strdup(s
);
462 return pet_expr_free(expr
);
467 /* Construct a pet_expr that represents the integer value "v".
469 __isl_give pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
477 ctx
= isl_val_get_ctx(v
);
478 expr
= pet_expr_alloc(ctx
, pet_expr_int
);
490 /* Return an independent duplicate of "expr".
492 * In case of an access expression, make sure the depth of the duplicate is set
493 * before the access relation (if any) is set and after the index expression
496 static __isl_give pet_expr
*pet_expr_dup(__isl_keep pet_expr
*expr
)
500 enum pet_expr_access_type type
;
505 dup
= pet_expr_alloc(expr
->ctx
, expr
->type
);
506 dup
= pet_expr_set_type_size(dup
, expr
->type_size
);
507 dup
= pet_expr_set_n_arg(dup
, expr
->n_arg
);
508 for (i
= 0; i
< expr
->n_arg
; ++i
)
509 dup
= pet_expr_set_arg(dup
, i
, pet_expr_copy(expr
->args
[i
]));
511 switch (expr
->type
) {
512 case pet_expr_access
:
513 if (expr
->acc
.ref_id
)
514 dup
= pet_expr_access_set_ref_id(dup
,
515 isl_id_copy(expr
->acc
.ref_id
));
516 dup
= pet_expr_access_set_index(dup
,
517 isl_multi_pw_aff_copy(expr
->acc
.index
));
518 dup
= pet_expr_access_set_depth(dup
, expr
->acc
.depth
);
519 for (type
= pet_expr_access_begin
;
520 type
< pet_expr_access_end
; ++type
) {
521 if (!expr
->acc
.access
[type
])
523 dup
= pet_expr_access_set_access(dup
, type
,
524 isl_union_map_copy(expr
->acc
.access
[type
]));
526 dup
= pet_expr_access_set_read(dup
, expr
->acc
.read
);
527 dup
= pet_expr_access_set_write(dup
, expr
->acc
.write
);
528 dup
= pet_expr_access_set_kill(dup
, expr
->acc
.kill
);
531 dup
= pet_expr_call_set_name(dup
, expr
->c
.name
);
533 dup
= pet_expr_call_set_summary(dup
,
534 pet_function_summary_copy(expr
->c
.summary
));
537 dup
= pet_expr_cast_set_type_name(dup
, expr
->type_name
);
539 case pet_expr_double
:
540 dup
= pet_expr_double_set(dup
, expr
->d
.val
, expr
->d
.s
);
543 dup
= pet_expr_int_set_val(dup
, isl_val_copy(expr
->i
));
546 dup
= pet_expr_op_set_type(dup
, expr
->op
);
549 dup
= pet_expr_free(dup
);
556 /* Return a pet_expr that is equal to "expr" and that has only
557 * a single reference.
559 * If "expr" itself only has one reference, then clear its hash value
560 * since the returned pet_expr will be modified.
562 __isl_give pet_expr
*pet_expr_cow(__isl_take pet_expr
*expr
)
567 if (expr
->ref
== 1) {
572 return pet_expr_dup(expr
);
575 __isl_null pet_expr
*pet_expr_free(__isl_take pet_expr
*expr
)
577 enum pet_expr_access_type type
;
585 for (i
= 0; i
< expr
->n_arg
; ++i
)
586 pet_expr_free(expr
->args
[i
]);
589 switch (expr
->type
) {
590 case pet_expr_access
:
591 isl_id_free(expr
->acc
.ref_id
);
592 for (type
= pet_expr_access_begin
;
593 type
< pet_expr_access_end
; ++type
)
594 isl_union_map_free(expr
->acc
.access
[type
]);
595 isl_multi_pw_aff_free(expr
->acc
.index
);
599 pet_function_summary_free(expr
->c
.summary
);
602 free(expr
->type_name
);
604 case pet_expr_double
:
608 isl_val_free(expr
->i
);
615 isl_ctx_deref(expr
->ctx
);
620 /* Return an additional reference to "expr".
622 __isl_give pet_expr
*pet_expr_copy(__isl_keep pet_expr
*expr
)
631 /* Return the isl_ctx in which "expr" was created.
633 isl_ctx
*pet_expr_get_ctx(__isl_keep pet_expr
*expr
)
635 return expr
? expr
->ctx
: NULL
;
638 /* Return the type of "expr".
640 enum pet_expr_type
pet_expr_get_type(__isl_keep pet_expr
*expr
)
643 return pet_expr_error
;
647 /* Return the number of arguments of "expr".
649 int pet_expr_get_n_arg(__isl_keep pet_expr
*expr
)
657 /* Set the number of arguments of "expr" to "n".
659 * If "expr" originally had more arguments, then remove the extra arguments.
660 * If "expr" originally had fewer arguments, then create space for
661 * the extra arguments ans initialize them to NULL.
663 __isl_give pet_expr
*pet_expr_set_n_arg(__isl_take pet_expr
*expr
, int n
)
670 if (expr
->n_arg
== n
)
672 expr
= pet_expr_cow(expr
);
676 if (n
< expr
->n_arg
) {
677 for (i
= n
; i
< expr
->n_arg
; ++i
)
678 pet_expr_free(expr
->args
[i
]);
683 args
= isl_realloc_array(expr
->ctx
, expr
->args
, pet_expr
*, n
);
685 return pet_expr_free(expr
);
687 for (i
= expr
->n_arg
; i
< n
; ++i
)
688 expr
->args
[i
] = NULL
;
694 /* Return the argument of "expr" at position "pos".
696 __isl_give pet_expr
*pet_expr_get_arg(__isl_keep pet_expr
*expr
, int pos
)
700 if (pos
< 0 || pos
>= expr
->n_arg
)
701 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
702 "position out of bounds", return NULL
);
704 return pet_expr_copy(expr
->args
[pos
]);
707 /* Replace "expr" by its argument at position "pos".
709 __isl_give pet_expr
*pet_expr_arg(__isl_take pet_expr
*expr
, int pos
)
713 arg
= pet_expr_get_arg(expr
, pos
);
719 /* Replace the argument of "expr" at position "pos" by "arg".
721 __isl_give pet_expr
*pet_expr_set_arg(__isl_take pet_expr
*expr
, int pos
,
722 __isl_take pet_expr
*arg
)
726 if (pos
< 0 || pos
>= expr
->n_arg
)
727 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
728 "position out of bounds", goto error
);
729 if (expr
->args
[pos
] == arg
) {
734 expr
= pet_expr_cow(expr
);
738 pet_expr_free(expr
->args
[pos
]);
739 expr
->args
[pos
] = arg
;
748 /* Does "expr" perform a comparison operation?
750 int pet_expr_is_comparison(__isl_keep pet_expr
*expr
)
754 if (expr
->type
!= pet_expr_op
)
769 /* Does "expr" perform a boolean operation?
771 int pet_expr_is_boolean(__isl_keep pet_expr
*expr
)
775 if (expr
->type
!= pet_expr_op
)
787 /* Is "expr" an address-of operation?
789 int pet_expr_is_address_of(__isl_keep pet_expr
*expr
)
793 if (expr
->type
!= pet_expr_op
)
795 return expr
->op
== pet_op_address_of
;
798 /* Is "expr" an assume statement?
800 int pet_expr_is_assume(__isl_keep pet_expr
*expr
)
804 if (expr
->type
!= pet_expr_op
)
806 return expr
->op
== pet_op_assume
;
809 /* Does "expr" perform a min operation?
811 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
815 if (expr
->type
!= pet_expr_call
)
817 if (expr
->n_arg
!= 2)
819 if (strcmp(expr
->c
.name
, "min") != 0)
824 /* Does "expr" perform a max operation?
826 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
830 if (expr
->type
!= pet_expr_call
)
832 if (expr
->n_arg
!= 2)
834 if (strcmp(expr
->c
.name
, "max") != 0)
839 /* Does "expr" represent an access to an unnamed space, i.e.,
840 * does it represent an affine expression?
842 isl_bool
pet_expr_is_affine(__isl_keep pet_expr
*expr
)
847 return isl_bool_error
;
848 if (expr
->type
!= pet_expr_access
)
849 return isl_bool_false
;
851 has_id
= isl_multi_pw_aff_has_tuple_id(expr
->acc
.index
, isl_dim_out
);
853 return isl_bool_error
;
858 /* Given that "expr" represents an affine expression, i.e., that
859 * it is an access to an unnamed (1D) space, return this affine expression.
861 __isl_give isl_pw_aff
*pet_expr_get_affine(__isl_keep pet_expr
*expr
)
865 isl_multi_pw_aff
*mpa
;
867 is_affine
= pet_expr_is_affine(expr
);
871 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
872 "not an affine expression", return NULL
);
874 mpa
= pet_expr_access_get_index(expr
);
875 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
876 isl_multi_pw_aff_free(mpa
);
880 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
881 * not part of any struct?
883 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
887 if (expr
->type
!= pet_expr_access
)
889 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
))
892 return expr
->acc
.depth
== 0;
895 /* Construct an access relation from the index expression and
896 * the array depth of the access expression "expr".
898 * If the number of indices is smaller than the depth of the array,
899 * then we assume that all elements of the remaining dimensions
902 static __isl_give isl_union_map
*construct_access_relation(
903 __isl_keep pet_expr
*expr
)
911 access
= isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr
));
915 dim
= isl_map_dim(access
, isl_dim_out
);
916 if (dim
> expr
->acc
.depth
)
917 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
918 "number of indices greater than depth",
919 access
= isl_map_free(access
));
921 if (dim
!= expr
->acc
.depth
)
922 access
= extend_range(access
, expr
->acc
.depth
- dim
);
924 return isl_union_map_from_map(access
);
927 /* Ensure that all relevant access relations are explicitly
928 * available in "expr".
930 * If "expr" does not already have the relevant access relations, then create
931 * them based on the index expression and the array depth.
933 * We do not cow since adding an explicit access relation
934 * does not change the meaning of the expression.
935 * However, the explicit access relations may modify the hash value,
936 * so the cached value is reset.
938 static __isl_give pet_expr
*introduce_access_relations(
939 __isl_take pet_expr
*expr
)
941 isl_union_map
*access
;
942 int kill
, read
, write
;
946 if (has_relevant_access_relations(expr
))
949 access
= construct_access_relation(expr
);
951 return pet_expr_free(expr
);
954 kill
= expr
->acc
.kill
;
955 read
= expr
->acc
.read
;
956 write
= expr
->acc
.write
;
957 if (kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
958 expr
->acc
.access
[pet_expr_access_fake_killed
] =
959 isl_union_map_copy(access
);
960 if (read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
961 expr
->acc
.access
[pet_expr_access_may_read
] =
962 isl_union_map_copy(access
);
963 if (write
&& !expr
->acc
.access
[pet_expr_access_may_write
])
964 expr
->acc
.access
[pet_expr_access_may_write
] =
965 isl_union_map_copy(access
);
966 if (write
&& !expr
->acc
.access
[pet_expr_access_must_write
])
967 expr
->acc
.access
[pet_expr_access_must_write
] =
968 isl_union_map_copy(access
);
970 isl_union_map_free(access
);
972 if (!has_relevant_access_relations(expr
))
973 return pet_expr_free(expr
);
978 /* Return a hash value that digests "expr".
979 * If a hash value was computed already, then return that value.
980 * Otherwise, compute the hash value and store a copy in expr->hash.
982 uint32_t pet_expr_get_hash(__isl_keep pet_expr
*expr
)
985 enum pet_expr_access_type type
;
986 uint32_t hash
, hash_f
;
993 hash
= isl_hash_init();
994 isl_hash_byte(hash
, expr
->type
& 0xFF);
995 isl_hash_byte(hash
, expr
->n_arg
& 0xFF);
996 for (i
= 0; i
< expr
->n_arg
; ++i
) {
998 hash_i
= pet_expr_get_hash(expr
->args
[i
]);
999 isl_hash_hash(hash
, hash_i
);
1001 switch (expr
->type
) {
1002 case pet_expr_error
:
1004 case pet_expr_double
:
1005 hash
= isl_hash_string(hash
, expr
->d
.s
);
1008 hash_f
= isl_val_get_hash(expr
->i
);
1009 isl_hash_hash(hash
, hash_f
);
1011 case pet_expr_access
:
1012 isl_hash_byte(hash
, expr
->acc
.read
& 0xFF);
1013 isl_hash_byte(hash
, expr
->acc
.write
& 0xFF);
1014 isl_hash_byte(hash
, expr
->acc
.kill
& 0xFF);
1015 hash_f
= isl_id_get_hash(expr
->acc
.ref_id
);
1016 isl_hash_hash(hash
, hash_f
);
1017 hash_f
= isl_multi_pw_aff_get_hash(expr
->acc
.index
);
1018 isl_hash_hash(hash
, hash_f
);
1019 isl_hash_byte(hash
, expr
->acc
.depth
& 0xFF);
1020 for (type
= pet_expr_access_begin
;
1021 type
< pet_expr_access_end
; ++type
) {
1022 hash_f
= isl_union_map_get_hash(expr
->acc
.access
[type
]);
1023 isl_hash_hash(hash
, hash_f
);
1027 isl_hash_byte(hash
, expr
->op
& 0xFF);
1030 hash
= isl_hash_string(hash
, expr
->c
.name
);
1033 hash
= isl_hash_string(hash
, expr
->type_name
);
1040 /* Return 1 if the two pet_exprs are equivalent.
1042 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
1045 enum pet_expr_access_type type
;
1047 if (!expr1
|| !expr2
)
1050 if (expr1
->type
!= expr2
->type
)
1052 if (expr1
->n_arg
!= expr2
->n_arg
)
1054 for (i
= 0; i
< expr1
->n_arg
; ++i
)
1055 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
1057 switch (expr1
->type
) {
1058 case pet_expr_error
:
1060 case pet_expr_double
:
1061 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
1063 if (expr1
->d
.val
!= expr2
->d
.val
)
1067 if (!isl_val_eq(expr1
->i
, expr2
->i
))
1070 case pet_expr_access
:
1071 if (expr1
->acc
.read
!= expr2
->acc
.read
)
1073 if (expr1
->acc
.write
!= expr2
->acc
.write
)
1075 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
1077 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
1079 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
1081 if (!isl_multi_pw_aff_is_equal(expr1
->acc
.index
,
1084 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
1086 if (has_relevant_access_relations(expr1
) !=
1087 has_relevant_access_relations(expr2
)) {
1089 expr1
= pet_expr_copy(expr1
);
1090 expr2
= pet_expr_copy(expr2
);
1091 expr1
= introduce_access_relations(expr1
);
1092 expr2
= introduce_access_relations(expr2
);
1093 equal
= pet_expr_is_equal(expr1
, expr2
);
1094 pet_expr_free(expr1
);
1095 pet_expr_free(expr2
);
1098 for (type
= pet_expr_access_begin
;
1099 type
< pet_expr_access_end
; ++type
) {
1100 if (!expr1
->acc
.access
[type
] !=
1101 !expr2
->acc
.access
[type
])
1103 if (!expr1
->acc
.access
[type
])
1105 if (!isl_union_map_is_equal(expr1
->acc
.access
[type
],
1106 expr2
->acc
.access
[type
]))
1111 if (expr1
->op
!= expr2
->op
)
1115 if (strcmp(expr1
->c
.name
, expr2
->c
.name
))
1119 if (strcmp(expr1
->type_name
, expr2
->type_name
))
1127 /* Do "expr1" and "expr2" represent two accesses to the same array
1128 * that are also of the same type? That is, can these two accesses
1129 * be replaced by a single access?
1131 isl_bool
pet_expr_is_same_access(__isl_keep pet_expr
*expr1
,
1132 __isl_keep pet_expr
*expr2
)
1134 isl_space
*space1
, *space2
;
1137 if (!expr1
|| !expr2
)
1138 return isl_bool_error
;
1139 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1140 return isl_bool_false
;
1141 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1142 return isl_bool_false
;
1143 if (expr1
->acc
.read
!= expr2
->acc
.read
)
1144 return isl_bool_false
;
1145 if (expr1
->acc
.write
!= expr2
->acc
.write
)
1146 return isl_bool_false
;
1147 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
1148 return isl_bool_false
;
1149 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
1150 return isl_bool_false
;
1152 space1
= isl_multi_pw_aff_get_space(expr1
->acc
.index
);
1153 space2
= isl_multi_pw_aff_get_space(expr2
->acc
.index
);
1154 same
= isl_space_tuple_is_equal(space1
, isl_dim_out
,
1155 space2
, isl_dim_out
);
1156 if (same
>= 0 && same
)
1157 same
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
1158 space2
, isl_dim_in
);
1159 isl_space_free(space1
);
1160 isl_space_free(space2
);
1165 /* Does the access expression "expr" read the accessed elements?
1167 isl_bool
pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
1170 return isl_bool_error
;
1171 if (expr
->type
!= pet_expr_access
)
1172 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1173 "not an access expression", return isl_bool_error
);
1175 return expr
->acc
.read
;
1178 /* Does the access expression "expr" write to the accessed elements?
1180 isl_bool
pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
1183 return isl_bool_error
;
1184 if (expr
->type
!= pet_expr_access
)
1185 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1186 "not an access expression", return isl_bool_error
);
1188 return expr
->acc
.write
;
1191 /* Does the access expression "expr" kill the accessed elements?
1193 isl_bool
pet_expr_access_is_kill(__isl_keep pet_expr
*expr
)
1196 return isl_bool_error
;
1197 if (expr
->type
!= pet_expr_access
)
1198 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1199 "not an access expression", return isl_bool_error
);
1201 return expr
->acc
.kill
;
1204 /* Return the identifier of the array accessed by "expr".
1206 * If "expr" represents a member access, then return the identifier
1207 * of the outer structure array.
1209 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
1213 if (expr
->type
!= pet_expr_access
)
1214 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1215 "not an access expression", return NULL
);
1217 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
)) {
1221 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1222 space
= isl_space_range(space
);
1223 while (space
&& isl_space_is_wrapping(space
))
1224 space
= isl_space_domain(isl_space_unwrap(space
));
1225 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
1226 isl_space_free(space
);
1231 return isl_multi_pw_aff_get_tuple_id(expr
->acc
.index
, isl_dim_out
);
1234 /* Return the parameter space of "expr".
1236 __isl_give isl_space
*pet_expr_access_get_parameter_space(
1237 __isl_keep pet_expr
*expr
)
1243 if (expr
->type
!= pet_expr_access
)
1244 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1245 "not an access expression", return NULL
);
1247 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1248 space
= isl_space_params(space
);
1253 /* Return the domain space of "expr", including the arguments (if any).
1255 __isl_give isl_space
*pet_expr_access_get_augmented_domain_space(
1256 __isl_keep pet_expr
*expr
)
1262 if (expr
->type
!= pet_expr_access
)
1263 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1264 "not an access expression", return NULL
);
1266 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1267 space
= isl_space_domain(space
);
1272 /* Return the domain space of "expr", without the arguments (if any).
1274 __isl_give isl_space
*pet_expr_access_get_domain_space(
1275 __isl_keep pet_expr
*expr
)
1279 space
= pet_expr_access_get_augmented_domain_space(expr
);
1280 if (isl_space_is_wrapping(space
))
1281 space
= isl_space_domain(isl_space_unwrap(space
));
1286 /* Internal data structure for pet_expr_access_foreach_data_space.
1288 struct pet_foreach_data_space_data
{
1289 isl_stat (*fn
)(__isl_take isl_space
*space
, void *user
);
1293 /* Given a piece of an access relation, call data->fn on the data
1294 * (i.e., range) space.
1296 static isl_stat
foreach_data_space(__isl_take isl_map
*map
, void *user
)
1298 struct pet_foreach_data_space_data
*data
= user
;
1301 space
= isl_map_get_space(map
);
1302 space
= isl_space_range(space
);
1305 return data
->fn(space
, data
->user
);
1308 /* Call "fn" on the data spaces accessed by "expr".
1309 * In particular, call "fn" on the range space of the index expression,
1310 * but if "expr" keeps track of any explicit access relations,
1311 * then also call "fn" on the corresponding range spaces.
1313 isl_stat
pet_expr_access_foreach_data_space(__isl_keep pet_expr
*expr
,
1314 isl_stat (*fn
)(__isl_take isl_space
*space
, void *user
), void *user
)
1316 struct pet_foreach_data_space_data data
= { fn
, user
};
1317 enum pet_expr_access_type type
;
1321 return isl_stat_error
;
1322 if (expr
->type
!= pet_expr_access
)
1323 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1324 "not an access expression", return isl_stat_error
);
1326 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1327 if (!expr
->acc
.access
[type
])
1329 if (isl_union_map_foreach_map(expr
->acc
.access
[type
],
1330 &foreach_data_space
, &data
) < 0)
1331 return isl_stat_error
;
1334 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1335 space
= isl_space_range(space
);
1336 return fn(space
, user
);
1339 /* Modify all subexpressions of "expr" by calling "fn" on them.
1340 * The subexpressions are traversed in depth first preorder.
1342 __isl_give pet_expr
*pet_expr_map_top_down(__isl_take pet_expr
*expr
,
1343 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1351 expr
= fn(expr
, user
);
1353 n
= pet_expr_get_n_arg(expr
);
1354 for (i
= 0; i
< n
; ++i
) {
1355 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1356 arg
= pet_expr_map_top_down(arg
, fn
, user
);
1357 expr
= pet_expr_set_arg(expr
, i
, arg
);
1363 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1365 static __isl_give pet_expr
*pet_expr_map_expr_of_type(__isl_take pet_expr
*expr
,
1366 enum pet_expr_type type
,
1367 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1372 n
= pet_expr_get_n_arg(expr
);
1373 for (i
= 0; i
< n
; ++i
) {
1374 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1375 arg
= pet_expr_map_expr_of_type(arg
, type
, fn
, user
);
1376 expr
= pet_expr_set_arg(expr
, i
, arg
);
1382 if (expr
->type
== type
)
1383 expr
= fn(expr
, user
);
1388 /* Modify all expressions of type pet_expr_access in "expr"
1389 * by calling "fn" on them.
1391 __isl_give pet_expr
*pet_expr_map_access(__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_access
, fn
, user
);
1398 /* Modify all expressions of type pet_expr_call in "expr"
1399 * by calling "fn" on them.
1401 __isl_give pet_expr
*pet_expr_map_call(__isl_take pet_expr
*expr
,
1402 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1405 return pet_expr_map_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1408 /* Modify all expressions of type pet_expr_op in "expr"
1409 * by calling "fn" on them.
1411 __isl_give pet_expr
*pet_expr_map_op(__isl_take pet_expr
*expr
,
1412 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1415 return pet_expr_map_expr_of_type(expr
, pet_expr_op
, fn
, user
);
1418 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1420 * Return -1 on error (where fn returning a negative value is treated as
1422 * Otherwise return 0.
1424 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
1425 enum pet_expr_type type
,
1426 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1433 for (i
= 0; i
< expr
->n_arg
; ++i
)
1434 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1435 type
, fn
, user
) < 0)
1438 if (expr
->type
== type
)
1439 return fn(expr
, user
);
1444 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1446 * Return -1 on error (where fn returning a negative value is treated as
1448 * Otherwise return 0.
1450 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1451 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1453 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1456 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1458 * Return -1 on error (where fn returning a negative value is treated as
1460 * Otherwise return 0.
1462 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1463 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1465 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1468 /* Internal data structure for pet_expr_writes.
1469 * "id" is the identifier that we are looking for.
1470 * "found" is set if we have found the identifier being written to.
1472 struct pet_expr_writes_data
{
1477 /* Given an access expression, check if it writes to data->id.
1478 * If so, set data->found and abort the search.
1480 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1482 struct pet_expr_writes_data
*data
= user
;
1485 if (!expr
->acc
.write
)
1487 if (pet_expr_is_affine(expr
))
1490 write_id
= pet_expr_access_get_id(expr
);
1491 isl_id_free(write_id
);
1496 if (write_id
!= data
->id
)
1503 /* Does expression "expr" write to "id"?
1505 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1507 struct pet_expr_writes_data data
;
1511 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1518 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1519 * index expression and access relations of "expr" (if any)
1520 * to dimensions of "dst_type" at "dst_pos".
1522 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1523 enum isl_dim_type dst_type
, unsigned dst_pos
,
1524 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1526 enum pet_expr_access_type type
;
1528 expr
= pet_expr_cow(expr
);
1531 if (expr
->type
!= pet_expr_access
)
1532 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1533 "not an access pet_expr", return pet_expr_free(expr
));
1535 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1536 if (!expr
->acc
.access
[type
])
1538 expr
->acc
.access
[type
] =
1539 pet_union_map_move_dims(expr
->acc
.access
[type
],
1540 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1541 if (!expr
->acc
.access
[type
])
1544 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1545 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1546 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1547 return pet_expr_free(expr
);
1552 /* Replace the index expression and access relations (if any) of "expr"
1553 * by their preimages under the function represented by "ma".
1555 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1556 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1558 enum pet_expr_access_type type
;
1560 expr
= pet_expr_cow(expr
);
1563 if (expr
->type
!= pet_expr_access
)
1564 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1565 "not an access pet_expr", goto error
);
1567 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1568 if (!expr
->acc
.access
[type
])
1570 expr
->acc
.access
[type
] =
1571 isl_union_map_preimage_domain_multi_aff(
1572 expr
->acc
.access
[type
], isl_multi_aff_copy(ma
));
1573 if (!expr
->acc
.access
[type
])
1576 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1578 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1579 return pet_expr_free(expr
);
1583 isl_multi_aff_free(ma
);
1584 pet_expr_free(expr
);
1588 /* Replace the index expression and access relations (if any) of "expr"
1589 * by their preimages under the function represented by "mpa".
1591 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1592 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1594 enum pet_expr_access_type type
;
1596 expr
= pet_expr_cow(expr
);
1599 if (expr
->type
!= pet_expr_access
)
1600 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1601 "not an access pet_expr", goto error
);
1603 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1604 if (!expr
->acc
.access
[type
])
1606 expr
->acc
.access
[type
] =
1607 isl_union_map_preimage_domain_multi_pw_aff(
1608 expr
->acc
.access
[type
], isl_multi_pw_aff_copy(mpa
));
1609 if (!expr
->acc
.access
[type
])
1612 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1613 expr
->acc
.index
, mpa
);
1614 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1615 return pet_expr_free(expr
);
1619 isl_multi_pw_aff_free(mpa
);
1620 pet_expr_free(expr
);
1624 /* Return the index expression of access expression "expr".
1626 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1627 __isl_keep pet_expr
*expr
)
1631 if (expr
->type
!= pet_expr_access
)
1632 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1633 "not an access expression", return NULL
);
1635 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1638 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1640 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1643 enum pet_expr_access_type type
;
1645 expr
= pet_expr_cow(expr
);
1648 if (expr
->type
!= pet_expr_access
)
1649 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1650 "not an access expression", return pet_expr_free(expr
));
1652 if (!pet_expr_access_has_any_access_relation(expr
))
1655 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1656 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1657 if (!expr
->acc
.access
[type
])
1659 space
= isl_space_align_params(space
,
1660 isl_union_map_get_space(expr
->acc
.access
[type
]));
1662 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1663 isl_space_copy(space
));
1664 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1665 if (!expr
->acc
.access
[type
])
1667 expr
->acc
.access
[type
] =
1668 isl_union_map_align_params(expr
->acc
.access
[type
],
1669 isl_space_copy(space
));
1670 if (!expr
->acc
.access
[type
])
1673 isl_space_free(space
);
1674 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1675 return pet_expr_free(expr
);
1680 /* Are "expr1" and "expr2" both array accesses such that
1681 * the access relation of "expr1" is a subset of that of "expr2"?
1682 * Only take into account the first "n_arg" arguments.
1684 * This function is tailored for use by mark_self_dependences in nest.c.
1685 * In particular, the input expressions may have more than "n_arg"
1686 * elements in their arguments arrays, while only the first "n_arg"
1687 * elements are referenced from the access relations.
1689 int pet_expr_is_sub_access(__isl_keep pet_expr
*expr1
,
1690 __isl_keep pet_expr
*expr2
, int n_arg
)
1696 if (!expr1
|| !expr2
)
1698 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1700 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1702 if (pet_expr_is_affine(expr1
))
1704 if (pet_expr_is_affine(expr2
))
1706 n1
= pet_expr_get_n_arg(expr1
);
1709 n2
= pet_expr_get_n_arg(expr2
);
1714 for (i
= 0; i
< n1
; ++i
) {
1716 equal
= pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]);
1717 if (equal
< 0 || !equal
)
1720 id1
= pet_expr_access_get_id(expr1
);
1721 id2
= pet_expr_access_get_id(expr2
);
1729 expr1
= pet_expr_copy(expr1
);
1730 expr2
= pet_expr_copy(expr2
);
1731 expr1
= introduce_access_relations(expr1
);
1732 expr2
= introduce_access_relations(expr2
);
1733 if (!expr1
|| !expr2
)
1736 is_subset
= isl_union_map_is_subset(
1737 expr1
->acc
.access
[pet_expr_access_may_read
],
1738 expr2
->acc
.access
[pet_expr_access_may_read
]);
1740 pet_expr_free(expr1
);
1741 pet_expr_free(expr2
);
1745 pet_expr_free(expr1
);
1746 pet_expr_free(expr2
);
1750 /* Given a set in the iteration space "domain", extend it to live in the space
1751 * of the domain of access relations.
1753 * That, is the number of arguments "n" is 0, then simply return domain.
1754 * Otherwise, return [domain -> [a_1,...,a_n]].
1756 static __isl_give isl_set
*add_arguments(__isl_take isl_set
*domain
, int n
)
1763 map
= isl_map_from_domain(domain
);
1764 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
1765 return isl_map_wrap(map
);
1768 /* Add extra conditions to the domains of all access relations in "expr",
1769 * introducing access relations if they are not already present.
1771 * The conditions are not added to the index expression. Instead, they
1772 * are used to try and simplify the index expression.
1774 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1775 __isl_take isl_set
*cond
)
1778 isl_union_set
*uset
;
1779 enum pet_expr_access_type type
;
1781 expr
= pet_expr_cow(expr
);
1785 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1786 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1787 isl_set_copy(cond
));
1792 if (expr
->type
!= pet_expr_access
) {
1797 expr
= introduce_access_relations(expr
);
1801 cond
= add_arguments(cond
, expr
->n_arg
);
1802 uset
= isl_union_set_from_set(isl_set_copy(cond
));
1803 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1804 if (!expr
->acc
.access
[type
])
1806 expr
->acc
.access
[type
] =
1807 isl_union_map_intersect_domain(expr
->acc
.access
[type
],
1808 isl_union_set_copy(uset
));
1809 if (!expr
->acc
.access
[type
])
1812 isl_union_set_free(uset
);
1813 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, cond
);
1814 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1815 return pet_expr_free(expr
);
1820 return pet_expr_free(expr
);
1823 /* Modify the access relations (if any) and index expression
1824 * of the given access expression
1825 * based on the given iteration space transformation.
1826 * In particular, precompose the access relation and index expression
1827 * with the update function.
1829 * If the access has any arguments then the domain of the access relation
1830 * is a wrapped mapping from the iteration space to the space of
1831 * argument values. We only need to change the domain of this wrapped
1832 * mapping, so we extend the input transformation with an identity mapping
1833 * on the space of argument values.
1835 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1836 __isl_keep isl_multi_pw_aff
*update
)
1838 enum pet_expr_access_type type
;
1840 expr
= pet_expr_cow(expr
);
1843 if (expr
->type
!= pet_expr_access
)
1844 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1845 "not an access expression", return pet_expr_free(expr
));
1847 update
= isl_multi_pw_aff_copy(update
);
1849 if (expr
->n_arg
> 0) {
1851 isl_multi_pw_aff
*id
;
1853 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1854 space
= isl_space_domain(space
);
1855 space
= isl_space_unwrap(space
);
1856 space
= isl_space_range(space
);
1857 space
= isl_space_map_from_set(space
);
1858 id
= isl_multi_pw_aff_identity(space
);
1859 update
= isl_multi_pw_aff_product(update
, id
);
1862 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1863 if (!expr
->acc
.access
[type
])
1865 expr
->acc
.access
[type
] =
1866 isl_union_map_preimage_domain_multi_pw_aff(
1867 expr
->acc
.access
[type
],
1868 isl_multi_pw_aff_copy(update
));
1869 if (!expr
->acc
.access
[type
])
1872 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1873 expr
->acc
.index
, update
);
1874 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1875 return pet_expr_free(expr
);
1880 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1882 isl_multi_pw_aff
*update
= user
;
1884 return pet_expr_access_update_domain(expr
, update
);
1887 /* Modify all access relations in "expr" by precomposing them with
1888 * the given iteration space transformation.
1890 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1891 __isl_take isl_multi_pw_aff
*update
)
1893 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1894 isl_multi_pw_aff_free(update
);
1898 /* Given an expression with accesses that have a 0D anonymous domain,
1899 * replace those domains by "space".
1901 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1902 __isl_take isl_space
*space
)
1904 isl_multi_pw_aff
*mpa
;
1906 space
= isl_space_from_domain(space
);
1907 mpa
= isl_multi_pw_aff_zero(space
);
1908 return pet_expr_update_domain(expr
, mpa
);
1911 /* Add all parameters in "space" to the access relations (if any)
1912 * and index expression of "expr".
1914 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1916 isl_space
*space
= user
;
1917 enum pet_expr_access_type type
;
1919 expr
= pet_expr_cow(expr
);
1922 if (expr
->type
!= pet_expr_access
)
1923 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1924 "not an access expression", return pet_expr_free(expr
));
1926 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1927 if (!expr
->acc
.access
[type
])
1929 expr
->acc
.access
[type
] =
1930 isl_union_map_align_params(expr
->acc
.access
[type
],
1931 isl_space_copy(space
));
1932 if (!expr
->acc
.access
[type
])
1935 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1936 isl_space_copy(space
));
1937 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1938 return pet_expr_free(expr
);
1943 /* Add all parameters in "space" to all access relations and index expressions
1946 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1947 __isl_take isl_space
*space
)
1949 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1950 isl_space_free(space
);
1954 /* Insert an argument expression corresponding to "test" in front
1955 * of the list of arguments described by *n_arg and *args.
1957 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1958 __isl_keep isl_multi_pw_aff
*test
)
1961 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1964 return pet_expr_free(expr
);
1965 expr
= pet_expr_cow(expr
);
1970 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1972 return pet_expr_free(expr
);
1975 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1977 return pet_expr_free(expr
);
1978 for (i
= 0; i
< expr
->n_arg
; ++i
)
1979 ext
[1 + i
] = expr
->args
[i
];
1984 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1986 return pet_expr_free(expr
);
1991 /* Make the expression "expr" depend on the value of "test"
1992 * being equal to "satisfied".
1994 * If "test" is an affine expression, we simply add the conditions
1995 * on the expression having the value "satisfied" to all access relations
1996 * (introducing access relations if they are missing) and index expressions.
1998 * Otherwise, we add a filter to "expr" (which is then assumed to be
1999 * an access expression) corresponding to "test" being equal to "satisfied".
2001 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
2002 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
2007 isl_pw_multi_aff
*pma
;
2008 enum pet_expr_access_type type
;
2010 expr
= pet_expr_cow(expr
);
2014 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
2018 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
2019 isl_multi_pw_aff_free(test
);
2021 cond
= isl_pw_aff_non_zero_set(pa
);
2023 cond
= isl_pw_aff_zero_set(pa
);
2024 return pet_expr_restrict(expr
, cond
);
2027 ctx
= isl_multi_pw_aff_get_ctx(test
);
2028 if (expr
->type
!= pet_expr_access
)
2029 isl_die(ctx
, isl_error_invalid
,
2030 "can only filter access expressions", goto error
);
2032 expr
= introduce_access_relations(expr
);
2036 space
= isl_space_domain(isl_multi_pw_aff_get_space(expr
->acc
.index
));
2037 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
2038 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
2040 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2041 if (!expr
->acc
.access
[type
])
2043 expr
->acc
.access
[type
] =
2044 isl_union_map_preimage_domain_pw_multi_aff(
2045 expr
->acc
.access
[type
],
2046 isl_pw_multi_aff_copy(pma
));
2047 if (!expr
->acc
.access
[type
])
2050 pma
= isl_pw_multi_aff_gist(pma
,
2051 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
2052 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
2053 expr
->acc
.index
, pma
);
2054 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2057 expr
= insert_access_arg(expr
, test
);
2059 isl_multi_pw_aff_free(test
);
2062 isl_multi_pw_aff_free(test
);
2063 return pet_expr_free(expr
);
2066 /* Add a reference identifier to access expression "expr".
2067 * "user" points to an integer that contains the sequence number
2068 * of the next reference.
2070 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
2077 expr
= pet_expr_cow(expr
);
2080 if (expr
->type
!= pet_expr_access
)
2081 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2082 "not an access expression", return pet_expr_free(expr
));
2084 ctx
= pet_expr_get_ctx(expr
);
2085 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
2086 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
2087 if (!expr
->acc
.ref_id
)
2088 return pet_expr_free(expr
);
2093 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
2095 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
2098 /* Reset the user pointer on all parameter and tuple ids in
2099 * the access relations (if any) and the index expression
2100 * of the access expression "expr".
2102 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
2105 enum pet_expr_access_type type
;
2107 expr
= pet_expr_cow(expr
);
2110 if (expr
->type
!= pet_expr_access
)
2111 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2112 "not an access expression", return pet_expr_free(expr
));
2114 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2115 if (!expr
->acc
.access
[type
])
2117 expr
->acc
.access
[type
] =
2118 isl_union_map_reset_user(expr
->acc
.access
[type
]);
2119 if (!expr
->acc
.access
[type
])
2122 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
2123 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2124 return pet_expr_free(expr
);
2129 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
2131 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
2134 /* Data used in access_gist() callback.
2136 struct pet_access_gist_data
{
2138 isl_union_map
*value_bounds
;
2141 /* Given an expression "expr" of type pet_expr_access, compute
2142 * the gist of the associated access relations (if any) and index expression
2143 * with respect to data->domain and the bounds on the values of the arguments
2144 * of the expression.
2146 * The arguments of "expr" have been gisted right before "expr" itself
2147 * is gisted. The gisted arguments may have become equal where before
2148 * they may not have been (obviously) equal. We therefore take
2149 * the opportunity to remove duplicate arguments here.
2151 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
2153 struct pet_access_gist_data
*data
= user
;
2155 isl_union_set
*uset
;
2156 enum pet_expr_access_type type
;
2158 expr
= pet_expr_remove_duplicate_args(expr
);
2159 expr
= pet_expr_cow(expr
);
2162 if (expr
->type
!= pet_expr_access
)
2163 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2164 "not an access expression", return pet_expr_free(expr
));
2166 domain
= isl_set_copy(data
->domain
);
2167 if (expr
->n_arg
> 0)
2168 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
2169 data
->value_bounds
);
2171 uset
= isl_union_set_from_set(isl_set_copy(domain
));
2172 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2173 if (!expr
->acc
.access
[type
])
2175 expr
->acc
.access
[type
] =
2176 isl_union_map_gist_domain(expr
->acc
.access
[type
],
2177 isl_union_set_copy(uset
));
2178 if (!expr
->acc
.access
[type
])
2181 isl_union_set_free(uset
);
2182 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
2183 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2184 return pet_expr_free(expr
);
2189 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
2190 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2192 struct pet_access_gist_data data
= { context
, value_bounds
};
2194 return pet_expr_map_access(expr
, &access_gist
, &data
);
2197 /* Mark "expr" as a read dependening on "read".
2199 __isl_give pet_expr
*pet_expr_access_set_read(__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
.read
== read
)
2209 expr
= pet_expr_cow(expr
);
2212 expr
->acc
.read
= read
;
2217 /* Mark "expr" as a write dependening on "write".
2219 __isl_give pet_expr
*pet_expr_access_set_write(__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
.write
== write
)
2229 expr
= pet_expr_cow(expr
);
2232 expr
->acc
.write
= write
;
2237 /* Mark "expr" as a kill dependening on "kill".
2239 __isl_give pet_expr
*pet_expr_access_set_kill(__isl_take pet_expr
*expr
,
2243 return pet_expr_free(expr
);
2244 if (expr
->type
!= pet_expr_access
)
2245 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2246 "not an access expression", return pet_expr_free(expr
));
2247 if (expr
->acc
.kill
== kill
)
2249 expr
= pet_expr_cow(expr
);
2252 expr
->acc
.kill
= kill
;
2257 /* Map the access type "type" to the corresponding location
2258 * in the access array.
2259 * In particular, the access relation of type pet_expr_access_killed is
2260 * stored in the element at position pet_expr_access_fake_killed.
2262 static enum pet_expr_access_type
internalize_type(
2263 enum pet_expr_access_type type
)
2265 if (type
== pet_expr_access_killed
)
2266 return pet_expr_access_fake_killed
;
2270 /* Replace the access relation of the given "type" of "expr" by "access".
2271 * If the access relation is non-empty and the type is a read or a write,
2272 * then also mark the access expression itself as a read or a write.
2274 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
2275 enum pet_expr_access_type type
, __isl_take isl_union_map
*access
)
2279 expr
= pet_expr_cow(expr
);
2280 if (!expr
|| !access
)
2282 if (expr
->type
!= pet_expr_access
)
2283 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2284 "not an access expression", goto error
);
2285 type
= internalize_type(type
);
2286 isl_union_map_free(expr
->acc
.access
[type
]);
2287 expr
->acc
.access
[type
] = access
;
2292 empty
= isl_union_map_is_empty(access
);
2294 return pet_expr_free(expr
);
2298 if (type
== pet_expr_access_may_read
)
2299 expr
= pet_expr_access_set_read(expr
, 1);
2301 expr
= pet_expr_access_set_write(expr
, 1);
2305 isl_union_map_free(access
);
2306 pet_expr_free(expr
);
2310 /* Replace the index expression of "expr" by "index" and
2311 * set the array depth accordingly.
2313 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
2314 __isl_take isl_multi_pw_aff
*index
)
2316 expr
= pet_expr_cow(expr
);
2317 if (!expr
|| !index
)
2319 if (expr
->type
!= pet_expr_access
)
2320 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2321 "not an access expression", goto error
);
2322 isl_multi_pw_aff_free(expr
->acc
.index
);
2323 expr
->acc
.index
= index
;
2324 expr
->acc
.depth
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
2328 isl_multi_pw_aff_free(index
);
2329 pet_expr_free(expr
);
2333 /* Return the reference identifier of access expression "expr".
2335 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
2339 if (expr
->type
!= pet_expr_access
)
2340 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2341 "not an access expression", return NULL
);
2343 return isl_id_copy(expr
->acc
.ref_id
);
2346 /* Replace the reference identifier of access expression "expr" by "ref_id".
2348 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
2349 __isl_take isl_id
*ref_id
)
2351 expr
= pet_expr_cow(expr
);
2352 if (!expr
|| !ref_id
)
2354 if (expr
->type
!= pet_expr_access
)
2355 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2356 "not an access expression", goto error
);
2357 isl_id_free(expr
->acc
.ref_id
);
2358 expr
->acc
.ref_id
= ref_id
;
2362 isl_id_free(ref_id
);
2363 pet_expr_free(expr
);
2367 /* Tag the access relation "access" with "id".
2368 * That is, insert the id as the range of a wrapped relation
2369 * in the domain of "access".
2371 * If "access" is of the form
2375 * then the result is of the form
2377 * [D[i] -> id[]] -> A[a]
2379 __isl_give isl_union_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
2380 __isl_take isl_union_map
*access
)
2383 isl_multi_aff
*add_tag
;
2386 if (expr
->type
!= pet_expr_access
)
2387 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2388 "not an access expression",
2389 return isl_union_map_free(access
));
2391 id
= isl_id_copy(expr
->acc
.ref_id
);
2392 space
= pet_expr_access_get_domain_space(expr
);
2393 space
= isl_space_from_domain(space
);
2394 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2395 add_tag
= isl_multi_aff_domain_map(space
);
2396 access
= isl_union_map_preimage_domain_multi_aff(access
, add_tag
);
2401 /* Return the access relation of the given "type" associated to "expr"
2402 * that maps pairs of domain iterations and argument values
2403 * to the corresponding accessed data elements.
2405 * If the requested access relation is explicitly available,
2406 * then return a copy. Otherwise, check if it is irrelevant for
2407 * the access expression and return an empty relation if this is the case.
2408 * Otherwise, introduce the requested access relation in "expr" and
2411 __isl_give isl_union_map
*pet_expr_access_get_dependent_access(
2412 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2414 isl_union_map
*access
;
2419 if (expr
->type
!= pet_expr_access
)
2420 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2421 "not an access expression", return NULL
);
2423 type
= internalize_type(type
);
2424 if (expr
->acc
.access
[type
])
2425 return isl_union_map_copy(expr
->acc
.access
[type
]);
2427 if (type
== pet_expr_access_may_read
)
2428 empty
= !expr
->acc
.read
;
2430 empty
= !expr
->acc
.write
;
2433 expr
= pet_expr_copy(expr
);
2434 expr
= introduce_access_relations(expr
);
2437 access
= isl_union_map_copy(expr
->acc
.access
[type
]);
2438 pet_expr_free(expr
);
2443 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr
));
2446 /* Return the may read access relation associated to "expr"
2447 * that maps pairs of domain iterations and argument values
2448 * to the corresponding accessed data elements.
2450 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_read(
2451 __isl_keep pet_expr
*expr
)
2453 return pet_expr_access_get_dependent_access(expr
,
2454 pet_expr_access_may_read
);
2457 /* Return the may write access relation associated to "expr"
2458 * that maps pairs of domain iterations and argument values
2459 * to the corresponding accessed data elements.
2461 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_write(
2462 __isl_keep pet_expr
*expr
)
2464 return pet_expr_access_get_dependent_access(expr
,
2465 pet_expr_access_may_write
);
2468 /* Return the must write access relation associated to "expr"
2469 * that maps pairs of domain iterations and argument values
2470 * to the corresponding accessed data elements.
2472 __isl_give isl_union_map
*pet_expr_access_get_dependent_must_write(
2473 __isl_keep pet_expr
*expr
)
2475 return pet_expr_access_get_dependent_access(expr
,
2476 pet_expr_access_must_write
);
2479 /* Return the relation of the given "type" mapping domain iterations
2480 * to the accessed data elements.
2481 * In particular, take the access relation and, in case of may_read
2482 * or may_write, project out the values of the arguments, if any.
2483 * In case of must_write, return the empty relation if there are
2486 __isl_give isl_union_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
,
2487 enum pet_expr_access_type type
)
2489 isl_union_map
*access
;
2495 if (expr
->type
!= pet_expr_access
)
2496 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2497 "not an access expression", return NULL
);
2499 if (expr
->n_arg
!= 0 && type
== pet_expr_access_must_write
) {
2500 space
= pet_expr_access_get_parameter_space(expr
);
2501 return isl_union_map_empty(space
);
2504 access
= pet_expr_access_get_dependent_access(expr
, type
);
2505 if (expr
->n_arg
== 0)
2508 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
2509 space
= isl_space_domain(space
);
2510 map
= isl_map_universe(isl_space_unwrap(space
));
2511 map
= isl_map_domain_map(map
);
2512 access
= isl_union_map_apply_domain(access
,
2513 isl_union_map_from_map(map
));
2518 /* Return the relation mapping domain iterations to all possibly
2519 * read data elements.
2521 __isl_give isl_union_map
*pet_expr_access_get_may_read(
2522 __isl_keep pet_expr
*expr
)
2524 return pet_expr_access_get_access(expr
, pet_expr_access_may_read
);
2527 /* Return the relation mapping domain iterations to all possibly
2528 * written data elements.
2530 __isl_give isl_union_map
*pet_expr_access_get_may_write(
2531 __isl_keep pet_expr
*expr
)
2533 return pet_expr_access_get_access(expr
, pet_expr_access_may_write
);
2536 /* Return a relation mapping domain iterations to definitely
2537 * written data elements, assuming the statement containing
2538 * the expression is executed.
2540 __isl_give isl_union_map
*pet_expr_access_get_must_write(
2541 __isl_keep pet_expr
*expr
)
2543 return pet_expr_access_get_access(expr
, pet_expr_access_must_write
);
2546 /* Return the relation of the given "type" mapping domain iterations to
2547 * accessed data elements, with its domain tagged with the reference
2550 static __isl_give isl_union_map
*pet_expr_access_get_tagged_access(
2551 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2553 isl_union_map
*access
;
2558 access
= pet_expr_access_get_access(expr
, type
);
2559 access
= pet_expr_tag_access(expr
, access
);
2564 /* Return the relation mapping domain iterations to all possibly
2565 * read data elements, with its domain tagged with the reference
2568 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_read(
2569 __isl_keep pet_expr
*expr
)
2571 return pet_expr_access_get_tagged_access(expr
,
2572 pet_expr_access_may_read
);
2575 /* Return the relation mapping domain iterations to all possibly
2576 * written data elements, with its domain tagged with the reference
2579 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_write(
2580 __isl_keep pet_expr
*expr
)
2582 return pet_expr_access_get_tagged_access(expr
,
2583 pet_expr_access_may_write
);
2586 /* Return the operation type of operation expression "expr".
2588 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
2592 if (expr
->type
!= pet_expr_op
)
2593 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2594 "not an operation expression", return pet_op_last
);
2599 /* Replace the operation type of operation expression "expr" by "type".
2601 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
2602 enum pet_op_type type
)
2605 return pet_expr_free(expr
);
2606 if (expr
->type
!= pet_expr_op
)
2607 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2608 "not an operation expression",
2609 return pet_expr_free(expr
));
2610 if (expr
->op
== type
)
2612 expr
= pet_expr_cow(expr
);
2620 /* Return the name of the function called by "expr".
2622 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
2626 if (expr
->type
!= pet_expr_call
)
2627 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2628 "not a call expression", return NULL
);
2629 return expr
->c
.name
;
2632 /* Replace the name of the function called by "expr" by "name".
2634 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
2635 __isl_keep
const char *name
)
2637 expr
= pet_expr_cow(expr
);
2639 return pet_expr_free(expr
);
2640 if (expr
->type
!= pet_expr_call
)
2641 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2642 "not a call expression", return pet_expr_free(expr
));
2644 expr
->c
.name
= strdup(name
);
2646 return pet_expr_free(expr
);
2650 /* Does the call expression "expr" have an associated function summary?
2652 int pet_expr_call_has_summary(__isl_keep pet_expr
*expr
)
2656 if (expr
->type
!= pet_expr_call
)
2657 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2658 "not a call expression", return -1);
2660 return expr
->c
.summary
!= NULL
;
2663 /* Return a copy of the function summary associated to
2664 * the call expression "expr".
2666 __isl_give pet_function_summary
*pet_expr_call_get_summary(
2667 __isl_keep pet_expr
*expr
)
2671 if (expr
->type
!= pet_expr_call
)
2672 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2673 "not a call expression", return NULL
);
2675 return pet_function_summary_copy(expr
->c
.summary
);
2678 /* Replace the function summary associated to the call expression "expr"
2681 __isl_give pet_expr
*pet_expr_call_set_summary(__isl_take pet_expr
*expr
,
2682 __isl_take pet_function_summary
*summary
)
2684 expr
= pet_expr_cow(expr
);
2685 if (!expr
|| !summary
)
2687 if (expr
->type
!= pet_expr_call
)
2688 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2689 "not a call expression", goto error
);
2690 pet_function_summary_free(expr
->c
.summary
);
2691 expr
->c
.summary
= summary
;
2694 pet_function_summary_free(summary
);
2695 return pet_expr_free(expr
);
2698 /* Replace the type of the cast performed by "expr" by "name".
2700 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
2701 __isl_keep
const char *name
)
2703 expr
= pet_expr_cow(expr
);
2705 return pet_expr_free(expr
);
2706 if (expr
->type
!= pet_expr_cast
)
2707 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2708 "not a cast expression", return pet_expr_free(expr
));
2709 free(expr
->type_name
);
2710 expr
->type_name
= strdup(name
);
2711 if (!expr
->type_name
)
2712 return pet_expr_free(expr
);
2716 /* Return the value of the integer represented by "expr".
2718 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
2722 if (expr
->type
!= pet_expr_int
)
2723 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2724 "not an int expression", return NULL
);
2726 return isl_val_copy(expr
->i
);
2729 /* Replace the value of the integer represented by "expr" by "v".
2731 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
2732 __isl_take isl_val
*v
)
2734 expr
= pet_expr_cow(expr
);
2737 if (expr
->type
!= pet_expr_int
)
2738 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2739 "not an int expression", goto error
);
2740 isl_val_free(expr
->i
);
2746 pet_expr_free(expr
);
2750 /* Replace the value and string representation of the double
2751 * represented by "expr" by "d" and "s".
2753 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2754 double d
, __isl_keep
const char *s
)
2756 expr
= pet_expr_cow(expr
);
2758 return pet_expr_free(expr
);
2759 if (expr
->type
!= pet_expr_double
)
2760 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2761 "not a double expression", return pet_expr_free(expr
));
2764 expr
->d
.s
= strdup(s
);
2766 return pet_expr_free(expr
);
2770 /* Return a string representation of the double expression "expr".
2772 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2776 if (expr
->type
!= pet_expr_double
)
2777 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2778 "not a double expression", return NULL
);
2779 return strdup(expr
->d
.s
);
2782 /* Return a piecewise affine expression defined on the specified domain
2783 * that represents NaN.
2785 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2787 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2790 /* This function is called when we come across an access that is
2791 * nested in what is supposed to be an affine expression.
2792 * "pc" is the context in which the affine expression is created.
2793 * If nesting is allowed in "pc", we return an affine expression that is
2794 * equal to a new parameter corresponding to this nested access.
2795 * Otherwise, we return NaN.
2797 * Note that we currently don't allow nested accesses themselves
2798 * to contain any nested accesses, so we check if "expr" itself
2799 * involves any nested accesses (either explicitly as arguments
2800 * or implicitly through parameters) and return NaN if it does.
2802 * The new parameter is resolved in resolve_nested.
2804 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2805 __isl_keep pet_context
*pc
)
2810 isl_local_space
*ls
;
2816 if (!pet_context_allow_nesting(pc
))
2817 return non_affine(pet_context_get_space(pc
));
2819 if (pet_expr_get_type(expr
) != pet_expr_access
)
2820 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2821 "not an access expression", return NULL
);
2823 if (expr
->n_arg
> 0)
2824 return non_affine(pet_context_get_space(pc
));
2826 space
= pet_expr_access_get_parameter_space(expr
);
2827 nested
= pet_nested_any_in_space(space
);
2828 isl_space_free(space
);
2830 return non_affine(pet_context_get_space(pc
));
2832 ctx
= pet_expr_get_ctx(expr
);
2833 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2834 space
= pet_context_get_space(pc
);
2835 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2837 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2838 ls
= isl_local_space_from_space(space
);
2839 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2841 return isl_pw_aff_from_aff(aff
);
2844 /* Extract an affine expression from the access pet_expr "expr".
2845 * "pc" is the context in which the affine expression is created.
2847 * If "expr" is actually an affine expression rather than
2848 * a real access, then we return that expression.
2849 * Otherwise, we require that "expr" is of an integral type.
2850 * If not, we return NaN.
2852 * If the variable has been assigned a known affine expression,
2853 * then we return that expression.
2855 * Otherwise, we return an expression that is equal to a parameter
2856 * representing "expr" (if "allow_nested" is set).
2858 static __isl_give isl_pw_aff
*extract_affine_from_access(
2859 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2863 if (pet_expr_is_affine(expr
))
2864 return pet_expr_get_affine(expr
);
2866 if (pet_expr_get_type_size(expr
) == 0)
2867 return non_affine(pet_context_get_space(pc
));
2869 if (!pet_expr_is_scalar_access(expr
))
2870 return nested_access(expr
, pc
);
2872 id
= pet_expr_access_get_id(expr
);
2873 if (pet_context_is_assigned(pc
, id
))
2874 return pet_context_get_value(pc
, id
);
2877 return nested_access(expr
, pc
);
2880 /* Construct an affine expression from the integer constant "expr".
2881 * "pc" is the context in which the affine expression is created.
2883 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2884 __isl_keep pet_context
*pc
)
2886 isl_local_space
*ls
;
2892 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2893 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2895 return isl_pw_aff_from_aff(aff
);
2898 /* Extract an affine expression from an addition or subtraction operation.
2899 * Return NaN if we are unable to extract an affine expression.
2901 * "pc" is the context in which the affine expression is created.
2903 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2904 __isl_keep pet_context
*pc
)
2911 if (expr
->n_arg
!= 2)
2912 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2913 "expecting two arguments", return NULL
);
2915 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2916 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2918 switch (pet_expr_op_get_type(expr
)) {
2920 return isl_pw_aff_add(lhs
, rhs
);
2922 return isl_pw_aff_sub(lhs
, rhs
);
2924 isl_pw_aff_free(lhs
);
2925 isl_pw_aff_free(rhs
);
2926 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2927 "not an addition or subtraction operation",
2933 /* Extract an affine expression from an integer division or a modulo operation.
2934 * Return NaN if we are unable to extract an affine expression.
2936 * "pc" is the context in which the affine expression is created.
2938 * In particular, if "expr" is lhs/rhs, then return
2940 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2942 * If "expr" is lhs%rhs, then return
2944 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2946 * If the second argument (rhs) is not a (positive) integer constant,
2947 * then we fail to extract an affine expression.
2949 * We simplify the result in the context of the domain of "pc" in case
2950 * this domain implies that lhs >= 0 (or < 0).
2952 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2953 __isl_keep pet_context
*pc
)
2962 if (expr
->n_arg
!= 2)
2963 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2964 "expecting two arguments", return NULL
);
2966 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2968 is_cst
= isl_pw_aff_is_cst(rhs
);
2969 if (is_cst
< 0 || !is_cst
) {
2970 isl_pw_aff_free(rhs
);
2971 return non_affine(pet_context_get_space(pc
));
2974 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2976 switch (pet_expr_op_get_type(expr
)) {
2978 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
2981 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
2984 isl_pw_aff_free(lhs
);
2985 isl_pw_aff_free(rhs
);
2986 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2987 "not a div or mod operator", return NULL
);
2990 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
2993 /* Extract an affine expression from a multiplication operation.
2994 * Return NaN if we are unable to extract an affine expression.
2995 * In particular, if neither of the arguments is a (piecewise) constant
2996 * then we return NaN.
2998 * "pc" is the context in which the affine expression is created.
3000 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
3001 __isl_keep pet_context
*pc
)
3003 int lhs_cst
, rhs_cst
;
3009 if (expr
->n_arg
!= 2)
3010 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3011 "expecting two arguments", return NULL
);
3013 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
3014 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
3016 lhs_cst
= isl_pw_aff_is_cst(lhs
);
3017 rhs_cst
= isl_pw_aff_is_cst(rhs
);
3018 if (lhs_cst
>= 0 && rhs_cst
>= 0 && (lhs_cst
|| rhs_cst
))
3019 return isl_pw_aff_mul(lhs
, rhs
);
3021 isl_pw_aff_free(lhs
);
3022 isl_pw_aff_free(rhs
);
3024 if (lhs_cst
< 0 || rhs_cst
< 0)
3027 return non_affine(pet_context_get_space(pc
));
3030 /* Extract an affine expression from a negation operation.
3031 * Return NaN if we are unable to extract an affine expression.
3033 * "pc" is the context in which the affine expression is created.
3035 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
3036 __isl_keep pet_context
*pc
)
3042 if (expr
->n_arg
!= 1)
3043 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3044 "expecting one argument", return NULL
);
3046 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
3047 return isl_pw_aff_neg(res
);
3050 /* Extract an affine expression from a conditional operation.
3051 * Return NaN if we are unable to extract an affine expression.
3053 * "pc" is the context in which the affine expression is created.
3055 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
3056 __isl_keep pet_context
*pc
)
3058 isl_pw_aff
*cond
, *lhs
, *rhs
;
3062 if (expr
->n_arg
!= 3)
3063 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3064 "expecting three arguments", return NULL
);
3066 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3067 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
3068 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
3070 return isl_pw_aff_cond(cond
, lhs
, rhs
);
3073 /* Limit the domain of "pwaff" to those elements where the function
3076 * 2^{width-1} <= pwaff < 2^{width-1}
3078 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
3083 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
3084 isl_local_space
*ls
= isl_local_space_from_space(space
);
3089 ctx
= isl_pw_aff_get_ctx(pwaff
);
3090 v
= isl_val_int_from_ui(ctx
, width
- 1);
3091 v
= isl_val_2exp(v
);
3093 bound
= isl_aff_zero_on_domain(ls
);
3094 bound
= isl_aff_add_constant_val(bound
, v
);
3095 b
= isl_pw_aff_from_aff(bound
);
3097 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
3098 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3100 b
= isl_pw_aff_neg(b
);
3101 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
3102 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3107 /* Handle potential overflows on signed computations.
3109 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
3110 * then we adjust the domain of "pa" to avoid overflows.
3112 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
3116 struct pet_options
*options
;
3121 ctx
= isl_pw_aff_get_ctx(pa
);
3122 options
= isl_ctx_peek_pet_options(ctx
);
3123 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
3124 pa
= avoid_overflow(pa
, width
);
3129 /* Extract an affine expression from some an operation.
3130 * Return NaN if we are unable to extract an affine expression.
3131 * If the result of a binary (non boolean) operation is unsigned,
3132 * then we wrap it based on the size of the type. If the result is signed,
3133 * then we ensure that no overflow occurs.
3135 * "pc" is the context in which the affine expression is created.
3137 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
3138 __isl_keep pet_context
*pc
)
3143 switch (pet_expr_op_get_type(expr
)) {
3146 res
= extract_affine_add_sub(expr
, pc
);
3150 res
= extract_affine_div_mod(expr
, pc
);
3153 res
= extract_affine_mul(expr
, pc
);
3156 return extract_affine_neg(expr
, pc
);
3158 return extract_affine_cond(expr
, pc
);
3168 return pet_expr_extract_affine_condition(expr
, pc
);
3170 return non_affine(pet_context_get_space(pc
));
3175 if (isl_pw_aff_involves_nan(res
)) {
3176 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
3177 isl_pw_aff_free(res
);
3178 return non_affine(space
);
3181 type_size
= pet_expr_get_type_size(expr
);
3183 res
= pet_wrap_pw_aff(res
, type_size
);
3185 res
= signed_overflow(res
, -type_size
);
3190 /* Internal data structure for affine builtin function declarations.
3192 * "pencil" is set if the builtin is pencil specific.
3193 * "n_args" is the number of arguments the function takes.
3194 * "name" is the function name.
3196 struct affine_builtin_decl
{
3202 static struct affine_builtin_decl affine_builtins
[] = {
3210 { 0, 2, "intFloor" },
3211 { 0, 2, "intCeil" },
3216 /* List of min and max builtin functions.
3218 static const char *min_max_builtins
[] = {
3219 "min", "imin", "umin",
3220 "max", "imax", "umax"
3223 /* Is a function call to "name" with "n_args" arguments a call to a
3224 * builtin function for which we can construct an affine expression?
3225 * pencil specific builtins are only recognized if "pencil" is set.
3227 static int is_affine_builtin(int pencil
, int n_args
, const char *name
)
3231 for (i
= 0; i
< ARRAY_SIZE(affine_builtins
); ++i
) {
3232 struct affine_builtin_decl
*decl
= &affine_builtins
[i
];
3234 if (decl
->pencil
&& !pencil
)
3236 if (decl
->n_args
== n_args
&& !strcmp(decl
->name
, name
))
3243 /* Is function "name" a known min or max builtin function?
3245 static int is_min_or_max_builtin(const char *name
)
3249 for (i
= 0; i
< ARRAY_SIZE(min_max_builtins
); ++i
)
3250 if (!strcmp(min_max_builtins
[i
], name
))
3256 /* Extract an affine expression from some special function calls.
3257 * Return NaN if we are unable to extract an affine expression.
3258 * In particular, we handle "min", "max", "ceild", "floord",
3259 * "intMod", "intFloor" and "intCeil".
3260 * In case of the latter five, the second argument needs to be
3261 * a (positive) integer constant.
3262 * If the pencil option is set, then we also handle "{i,u}min" and
3265 * "pc" is the context in which the affine expression is created.
3267 static __isl_give isl_pw_aff
*extract_affine_from_call(
3268 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3271 isl_pw_aff
*aff1
, *aff2
;
3274 struct pet_options
*options
;
3278 ctx
= pet_expr_get_ctx(expr
);
3279 options
= isl_ctx_peek_pet_options(ctx
);
3281 n
= pet_expr_get_n_arg(expr
);
3282 name
= pet_expr_call_get_name(expr
);
3283 if (!is_affine_builtin(options
->pencil
, n
, name
))
3284 return non_affine(pet_context_get_space(pc
));
3286 if (is_min_or_max_builtin(name
)) {
3287 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3288 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
3290 if (strstr(name
, "min"))
3291 aff1
= isl_pw_aff_min(aff1
, aff2
);
3293 aff1
= isl_pw_aff_max(aff1
, aff2
);
3294 } else if (!strcmp(name
, "intMod")) {
3297 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3298 return non_affine(pet_context_get_space(pc
));
3299 v
= pet_expr_int_get_val(expr
->args
[1]);
3300 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3301 aff1
= isl_pw_aff_mod_val(aff1
, v
);
3305 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3306 return non_affine(pet_context_get_space(pc
));
3307 v
= pet_expr_int_get_val(expr
->args
[1]);
3308 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3309 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
3310 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
3311 aff1
= isl_pw_aff_floor(aff1
);
3313 aff1
= isl_pw_aff_ceil(aff1
);
3319 /* Extract an affine expression from "expr", if possible.
3320 * Otherwise return NaN.
3322 * "pc" is the context in which the affine expression is created.
3324 * Store the result in "pc" such that it can be reused in case
3325 * pet_expr_extract_affine is called again on the same pair of
3328 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
3329 __isl_keep pet_context
*pc
)
3331 isl_maybe_isl_pw_aff m
;
3337 m
= pet_context_get_extracted_affine(pc
, expr
);
3338 if (m
.valid
< 0 || m
.valid
)
3341 switch (pet_expr_get_type(expr
)) {
3342 case pet_expr_access
:
3343 pa
= extract_affine_from_access(expr
, pc
);
3346 pa
= extract_affine_from_int(expr
, pc
);
3349 pa
= extract_affine_from_op(expr
, pc
);
3352 pa
= extract_affine_from_call(expr
, pc
);
3355 case pet_expr_double
:
3356 case pet_expr_error
:
3357 pa
= non_affine(pet_context_get_space(pc
));
3361 if (pet_context_set_extracted_affine(pc
, expr
, pa
) < 0)
3362 return isl_pw_aff_free(pa
);
3367 /* Extract an affine expressions representing the comparison "LHS op RHS"
3368 * Return NaN if we are unable to extract such an affine expression.
3370 * "pc" is the context in which the affine expression is created.
3372 * If the comparison is of the form
3376 * then the expression is constructed as the conjunction of
3381 * A similar optimization is performed for max(a,b) <= c.
3382 * We do this because that will lead to simpler representations
3383 * of the expression.
3384 * If isl is ever enhanced to explicitly deal with min and max expressions,
3385 * this optimization can be removed.
3387 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
3388 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
3389 __isl_keep pet_context
*pc
)
3391 isl_pw_aff
*lhs_pa
, *rhs_pa
;
3393 if (op
== pet_op_gt
)
3394 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
3395 if (op
== pet_op_ge
)
3396 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
3398 if (op
== pet_op_lt
|| op
== pet_op_le
) {
3399 if (pet_expr_is_min(rhs
)) {
3400 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3402 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3404 return pet_and(lhs_pa
, rhs_pa
);
3406 if (pet_expr_is_max(lhs
)) {
3407 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
3409 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
3411 return pet_and(lhs_pa
, rhs_pa
);
3415 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
3416 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
3418 return pet_comparison(op
, lhs_pa
, rhs_pa
);
3421 /* Extract an affine expressions from the comparison "expr".
3422 * Return NaN if we are unable to extract such an affine expression.
3424 * "pc" is the context in which the affine expression is created.
3426 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
3427 __isl_keep pet_context
*pc
)
3429 enum pet_op_type type
;
3433 if (expr
->n_arg
!= 2)
3434 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3435 "expecting two arguments", return NULL
);
3437 type
= pet_expr_op_get_type(expr
);
3438 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
3442 /* Extract an affine expression representing the boolean operation
3443 * expressed by "expr".
3444 * Return NaN if we are unable to extract an affine expression.
3446 * "pc" is the context in which the affine expression is created.
3448 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
3449 __isl_keep pet_context
*pc
)
3451 isl_pw_aff
*lhs
, *rhs
;
3457 n
= pet_expr_get_n_arg(expr
);
3458 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3460 return pet_not(lhs
);
3462 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
3463 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
3466 /* Extract the affine expression "expr != 0 ? 1 : 0".
3467 * Return NaN if we are unable to extract an affine expression.
3469 * "pc" is the context in which the affine expression is created.
3471 static __isl_give isl_pw_aff
*extract_implicit_condition(
3472 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3476 res
= pet_expr_extract_affine(expr
, pc
);
3477 return pet_to_bool(res
);
3480 /* Extract a boolean affine expression from "expr".
3481 * Return NaN if we are unable to extract an affine expression.
3483 * "pc" is the context in which the affine expression is created.
3485 * If "expr" is neither a comparison nor a boolean operation,
3486 * then we assume it is an affine expression and return the
3487 * boolean expression "expr != 0 ? 1 : 0".
3489 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
3490 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3495 if (pet_expr_is_comparison(expr
))
3496 return extract_comparison(expr
, pc
);
3497 if (pet_expr_is_boolean(expr
))
3498 return extract_boolean(expr
, pc
);
3500 return extract_implicit_condition(expr
, pc
);
3503 /* Check if "expr" is an assume expression and if its single argument
3504 * can be converted to an affine expression in the context of "pc".
3505 * If so, replace the argument by the affine expression.
3507 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
3508 __isl_keep pet_context
*pc
)
3511 isl_multi_pw_aff
*index
;
3515 if (!pet_expr_is_assume(expr
))
3517 if (expr
->n_arg
!= 1)
3518 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3519 "expecting one argument", return pet_expr_free(expr
));
3521 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3523 return pet_expr_free(expr
);
3524 if (isl_pw_aff_involves_nan(cond
)) {
3525 isl_pw_aff_free(cond
);
3529 index
= isl_multi_pw_aff_from_pw_aff(cond
);
3530 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
3535 /* Return the number of bits needed to represent the type of "expr".
3536 * See the description of the type_size field of pet_expr.
3538 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
3540 return expr
? expr
->type_size
: 0;
3543 /* Replace the number of bits needed to represent the type of "expr"
3545 * See the description of the type_size field of pet_expr.
3547 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
3550 expr
= pet_expr_cow(expr
);
3554 expr
->type_size
= type_size
;
3559 /* Extend an access expression "expr" with an additional index "index".
3560 * In particular, add "index" as an extra argument to "expr" and
3561 * adjust the index expression of "expr" to refer to this extra argument.
3562 * The caller is responsible for calling pet_expr_access_set_depth
3563 * to update the corresponding access relation.
3565 * Note that we only collect the individual index expressions as
3566 * arguments of "expr" here.
3567 * An attempt to integrate them into the index expression of "expr"
3568 * is performed in pet_expr_access_plug_in_args.
3570 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
3571 __isl_take pet_expr
*index
)
3575 isl_local_space
*ls
;
3578 expr
= pet_expr_cow(expr
);
3579 if (!expr
|| !index
)
3581 if (expr
->type
!= pet_expr_access
)
3582 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3583 "not an access pet_expr", goto error
);
3585 n
= pet_expr_get_n_arg(expr
);
3586 expr
= pet_expr_insert_arg(expr
, n
, index
);
3590 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3591 ls
= isl_local_space_from_space(space
);
3592 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
3593 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
3594 if (!expr
->acc
.index
)
3595 return pet_expr_free(expr
);
3599 pet_expr_free(expr
);
3600 pet_expr_free(index
);
3604 /* Extend an access expression "expr" with an additional member acces to "id".
3605 * In particular, extend the index expression of "expr" to include
3606 * the additional member access.
3607 * The caller is responsible for calling pet_expr_access_set_depth
3608 * to update the corresponding access relation.
3610 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
3611 __isl_take isl_id
*id
)
3614 isl_multi_pw_aff
*field_access
;
3616 expr
= pet_expr_cow(expr
);
3619 if (expr
->type
!= pet_expr_access
)
3620 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3621 "not an access pet_expr", goto error
);
3623 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3624 space
= isl_space_from_domain(space
);
3625 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
3626 field_access
= isl_multi_pw_aff_zero(space
);
3627 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
3628 if (!expr
->acc
.index
)
3629 return pet_expr_free(expr
);
3633 pet_expr_free(expr
);
3638 /* Prefix the access expression "expr" with "prefix".
3639 * If "add" is set, then it is not the index expression "prefix" itself
3640 * that was passed to the function, but its address.
3642 __isl_give pet_expr
*pet_expr_access_patch(__isl_take pet_expr
*expr
,
3643 __isl_take isl_multi_pw_aff
*prefix
, int add
)
3645 enum pet_expr_access_type type
;
3647 expr
= pet_expr_cow(expr
);
3648 if (!expr
|| !prefix
)
3650 if (expr
->type
!= pet_expr_access
)
3651 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3652 "not an access pet_expr", goto error
);
3654 expr
->acc
.depth
+= isl_multi_pw_aff_dim(prefix
, isl_dim_out
) - add
;
3655 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
3656 if (!expr
->acc
.access
[type
])
3658 expr
->acc
.access
[type
] = pet_patch_union_map(
3659 isl_multi_pw_aff_copy(prefix
), expr
->acc
.access
[type
],
3661 if (!expr
->acc
.access
[type
])
3664 expr
->acc
.index
= pet_patch_multi_pw_aff(prefix
, expr
->acc
.index
, add
);
3665 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
3666 return pet_expr_free(expr
);
3670 pet_expr_free(expr
);
3671 isl_multi_pw_aff_free(prefix
);
3675 /* Dump the arguments of "expr" to "p" as a YAML sequence keyed
3676 * by "args", if there are any such arguments.
3678 static __isl_give isl_printer
*dump_arguments(__isl_keep pet_expr
*expr
,
3679 __isl_take isl_printer
*p
)
3683 if (expr
->n_arg
== 0)
3686 p
= isl_printer_print_str(p
, "args");
3687 p
= isl_printer_yaml_next(p
);
3688 p
= isl_printer_yaml_start_sequence(p
);
3689 for (i
= 0; i
< expr
->n_arg
; ++i
) {
3690 p
= pet_expr_print(expr
->args
[i
], p
);
3691 p
= isl_printer_yaml_next(p
);
3693 p
= isl_printer_yaml_end_sequence(p
);
3698 /* Print "expr" to "p" in YAML format.
3700 __isl_give isl_printer
*pet_expr_print(__isl_keep pet_expr
*expr
,
3701 __isl_take isl_printer
*p
)
3704 return isl_printer_free(p
);
3706 switch (expr
->type
) {
3707 case pet_expr_double
:
3708 p
= isl_printer_print_str(p
, expr
->d
.s
);
3711 p
= isl_printer_print_val(p
, expr
->i
);
3713 case pet_expr_access
:
3714 p
= isl_printer_yaml_start_mapping(p
);
3715 if (expr
->acc
.ref_id
) {
3716 p
= isl_printer_print_str(p
, "ref_id");
3717 p
= isl_printer_yaml_next(p
);
3718 p
= isl_printer_print_id(p
, expr
->acc
.ref_id
);
3719 p
= isl_printer_yaml_next(p
);
3721 p
= isl_printer_print_str(p
, "index");
3722 p
= isl_printer_yaml_next(p
);
3723 p
= isl_printer_print_multi_pw_aff(p
, expr
->acc
.index
);
3724 p
= isl_printer_yaml_next(p
);
3725 p
= isl_printer_print_str(p
, "depth");
3726 p
= isl_printer_yaml_next(p
);
3727 p
= isl_printer_print_int(p
, expr
->acc
.depth
);
3728 p
= isl_printer_yaml_next(p
);
3729 if (expr
->acc
.kill
) {
3730 p
= isl_printer_print_str(p
, "kill");
3731 p
= isl_printer_yaml_next(p
);
3732 p
= isl_printer_print_int(p
, 1);
3733 p
= isl_printer_yaml_next(p
);
3735 p
= isl_printer_print_str(p
, "read");
3736 p
= isl_printer_yaml_next(p
);
3737 p
= isl_printer_print_int(p
, expr
->acc
.read
);
3738 p
= isl_printer_yaml_next(p
);
3739 p
= isl_printer_print_str(p
, "write");
3740 p
= isl_printer_yaml_next(p
);
3741 p
= isl_printer_print_int(p
, expr
->acc
.write
);
3742 p
= isl_printer_yaml_next(p
);
3744 if (expr
->acc
.access
[pet_expr_access_may_read
]) {
3745 p
= isl_printer_print_str(p
, "may_read");
3746 p
= isl_printer_yaml_next(p
);
3747 p
= isl_printer_print_union_map(p
,
3748 expr
->acc
.access
[pet_expr_access_may_read
]);
3749 p
= isl_printer_yaml_next(p
);
3751 if (expr
->acc
.access
[pet_expr_access_may_write
]) {
3752 p
= isl_printer_print_str(p
, "may_write");
3753 p
= isl_printer_yaml_next(p
);
3754 p
= isl_printer_print_union_map(p
,
3755 expr
->acc
.access
[pet_expr_access_may_write
]);
3756 p
= isl_printer_yaml_next(p
);
3758 if (expr
->acc
.access
[pet_expr_access_must_write
]) {
3759 p
= isl_printer_print_str(p
, "must_write");
3760 p
= isl_printer_yaml_next(p
);
3761 p
= isl_printer_print_union_map(p
,
3762 expr
->acc
.access
[pet_expr_access_must_write
]);
3763 p
= isl_printer_yaml_next(p
);
3765 p
= dump_arguments(expr
, p
);
3766 p
= isl_printer_yaml_end_mapping(p
);
3769 p
= isl_printer_yaml_start_mapping(p
);
3770 p
= isl_printer_print_str(p
, "op");
3771 p
= isl_printer_yaml_next(p
);
3772 p
= isl_printer_print_str(p
, op_str
[expr
->op
]);
3773 p
= isl_printer_yaml_next(p
);
3774 p
= dump_arguments(expr
, p
);
3775 p
= isl_printer_yaml_end_mapping(p
);
3778 p
= isl_printer_yaml_start_mapping(p
);
3779 p
= isl_printer_print_str(p
, "call");
3780 p
= isl_printer_yaml_next(p
);
3781 p
= isl_printer_print_str(p
, expr
->c
.name
);
3782 p
= isl_printer_print_str(p
, "/");
3783 p
= isl_printer_print_int(p
, expr
->n_arg
);
3784 p
= isl_printer_yaml_next(p
);
3785 p
= dump_arguments(expr
, p
);
3786 if (expr
->c
.summary
) {
3787 p
= isl_printer_print_str(p
, "summary");
3788 p
= isl_printer_yaml_next(p
);
3789 p
= pet_function_summary_print(expr
->c
.summary
, p
);
3791 p
= isl_printer_yaml_end_mapping(p
);
3794 p
= isl_printer_yaml_start_mapping(p
);
3795 p
= isl_printer_print_str(p
, "cast");
3796 p
= isl_printer_yaml_next(p
);
3797 p
= isl_printer_print_str(p
, expr
->type_name
);
3798 p
= isl_printer_yaml_next(p
);
3799 p
= dump_arguments(expr
, p
);
3800 p
= isl_printer_yaml_end_mapping(p
);
3802 case pet_expr_error
:
3803 p
= isl_printer_print_str(p
, "ERROR");
3810 /* Dump "expr" to stderr with indentation "indent".
3812 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
3819 p
= isl_printer_to_file(pet_expr_get_ctx(expr
), stderr
);
3820 p
= isl_printer_set_indent(p
, indent
);
3821 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_BLOCK
);
3822 p
= isl_printer_start_line(p
);
3823 p
= pet_expr_print(expr
, p
);
3825 isl_printer_free(p
);
3828 void pet_expr_dump(__isl_keep pet_expr
*expr
)
3830 pet_expr_dump_with_indent(expr
, 0);