2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
44 #include "value_bounds.h"
46 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
48 static char *type_str
[] = {
49 [pet_expr_access
] = "access",
50 [pet_expr_call
] = "call",
51 [pet_expr_cast
] = "cast",
52 [pet_expr_double
] = "double",
53 [pet_expr_int
] = "int",
57 static char *op_str
[] = {
58 [pet_op_add_assign
] = "+=",
59 [pet_op_sub_assign
] = "-=",
60 [pet_op_mul_assign
] = "*=",
61 [pet_op_div_assign
] = "/=",
62 [pet_op_assign
] = "=",
77 [pet_op_post_inc
] = "++",
78 [pet_op_post_dec
] = "--",
79 [pet_op_pre_inc
] = "++",
80 [pet_op_pre_dec
] = "--",
81 [pet_op_address_of
] = "&",
90 [pet_op_assume
] = "assume",
91 [pet_op_kill
] = "kill"
94 const char *pet_op_str(enum pet_op_type op
)
99 int pet_op_is_inc_dec(enum pet_op_type op
)
101 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
102 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
105 const char *pet_type_str(enum pet_expr_type type
)
107 return type_str
[type
];
110 enum pet_op_type
pet_str_op(const char *str
)
114 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
115 if (!strcmp(op_str
[i
], str
))
121 enum pet_expr_type
pet_str_type(const char *str
)
125 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
126 if (!strcmp(type_str
[i
], str
))
132 /* Construct a pet_expr of the given type.
134 __isl_give pet_expr
*pet_expr_alloc(isl_ctx
*ctx
, enum pet_expr_type type
)
138 expr
= isl_calloc_type(ctx
, struct pet_expr
);
150 /* Construct an access pet_expr from an access relation and an index expression.
151 * By default, it is considered to be a read access.
153 __isl_give pet_expr
*pet_expr_from_access_and_index( __isl_take isl_map
*access
,
154 __isl_take isl_multi_pw_aff
*index
)
156 isl_ctx
*ctx
= isl_map_get_ctx(access
);
159 if (!index
|| !access
)
161 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
165 expr
->acc
.access
= access
;
166 expr
->acc
.index
= index
;
172 isl_map_free(access
);
173 isl_multi_pw_aff_free(index
);
177 /* Construct an access pet_expr from an index expression.
178 * By default, the access is considered to be a read access.
180 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
184 access
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
185 return pet_expr_from_access_and_index(access
, index
);
188 /* Extend the range of "access" with "n" dimensions, retaining
189 * the tuple identifier on this range.
191 * If "access" represents a member access, then extend the range
194 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
198 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
200 if (!isl_map_range_is_wrapping(access
)) {
201 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
205 domain
= isl_map_copy(access
);
206 domain
= isl_map_range_factor_domain(domain
);
207 access
= isl_map_range_factor_range(access
);
208 access
= extend_range(access
, n
);
209 access
= isl_map_range_product(domain
, access
);
212 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
217 /* Finalize the construction of an access expression by setting
218 * the depth of the accessed array.
220 * The index expression may have been updated by
221 * pet_expr_access_subscript and/or pet_expr_access_member
222 * without the access relation having been updated accordingly.
223 * We perform this update here, taking into account the depth
224 * of the accessed array.
226 * If the number of indices is smaller than the depth of the array,
227 * then we assume that all elements of the remaining dimensions
230 __isl_give pet_expr
*pet_expr_access_set_depth(__isl_take pet_expr
*expr
,
236 expr
= pet_expr_cow(expr
);
240 access
= isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr
));
242 return pet_expr_free(expr
);
244 dim
= isl_map_dim(access
, isl_dim_out
);
246 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
247 "number of indices greater than depth",
248 access
= isl_map_free(access
));
251 access
= extend_range(access
, depth
- dim
);
253 return pet_expr_access_set_access(expr
, access
);
256 /* Construct a pet_expr that kills the elements specified by
257 * the index expression "index" and the access relation "access".
259 __isl_give pet_expr
*pet_expr_kill_from_access_and_index(
260 __isl_take isl_map
*access
, __isl_take isl_multi_pw_aff
*index
)
264 if (!access
|| !index
)
267 expr
= pet_expr_from_access_and_index(access
, index
);
268 expr
= pet_expr_access_set_read(expr
, 0);
269 return pet_expr_new_unary(pet_op_kill
, expr
);
271 isl_map_free(access
);
272 isl_multi_pw_aff_free(index
);
276 /* Construct a unary pet_expr that performs "op" on "arg".
278 __isl_give pet_expr
*pet_expr_new_unary(enum pet_op_type op
,
279 __isl_take pet_expr
*arg
)
286 ctx
= pet_expr_get_ctx(arg
);
287 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
288 expr
= pet_expr_set_n_arg(expr
, 1);
293 expr
->args
[pet_un_arg
] = arg
;
301 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
302 * where the result is represented using a type of "type_size" bits
303 * (may be zero if unknown or if the type is not an integer).
305 __isl_give pet_expr
*pet_expr_new_binary(int type_size
, enum pet_op_type op
,
306 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
313 ctx
= pet_expr_get_ctx(lhs
);
314 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
315 expr
= pet_expr_set_n_arg(expr
, 2);
320 expr
->type_size
= type_size
;
321 expr
->args
[pet_bin_lhs
] = lhs
;
322 expr
->args
[pet_bin_rhs
] = rhs
;
331 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
333 __isl_give pet_expr
*pet_expr_new_ternary(__isl_take pet_expr
*cond
,
334 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
339 if (!cond
|| !lhs
|| !rhs
)
341 ctx
= pet_expr_get_ctx(cond
);
342 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
343 expr
= pet_expr_set_n_arg(expr
, 3);
347 expr
->op
= pet_op_cond
;
348 expr
->args
[pet_ter_cond
] = cond
;
349 expr
->args
[pet_ter_true
] = lhs
;
350 expr
->args
[pet_ter_false
] = rhs
;
360 /* Construct a call pet_expr that calls function "name" with "n_arg"
361 * arguments. The caller is responsible for filling in the arguments.
363 __isl_give pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
368 expr
= pet_expr_alloc(ctx
, pet_expr_call
);
369 expr
= pet_expr_set_n_arg(expr
, n_arg
);
373 expr
->name
= strdup(name
);
375 return pet_expr_free(expr
);
380 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
382 __isl_give pet_expr
*pet_expr_new_cast(const char *type_name
,
383 __isl_take pet_expr
*arg
)
391 ctx
= pet_expr_get_ctx(arg
);
392 expr
= pet_expr_alloc(ctx
, pet_expr_cast
);
393 expr
= pet_expr_set_n_arg(expr
, 1);
397 expr
->type_name
= strdup(type_name
);
398 if (!expr
->type_name
)
410 /* Construct a pet_expr that represents the double "d".
412 __isl_give pet_expr
*pet_expr_new_double(isl_ctx
*ctx
,
413 double val
, const char *s
)
417 expr
= pet_expr_alloc(ctx
, pet_expr_double
);
422 expr
->d
.s
= strdup(s
);
424 return pet_expr_free(expr
);
429 /* Construct a pet_expr that represents the integer value "v".
431 __isl_give pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
439 ctx
= isl_val_get_ctx(v
);
440 expr
= pet_expr_alloc(ctx
, pet_expr_int
);
452 static __isl_give pet_expr
*pet_expr_dup(__isl_keep pet_expr
*expr
)
460 dup
= pet_expr_alloc(expr
->ctx
, expr
->type
);
461 dup
= pet_expr_set_type_size(dup
, expr
->type_size
);
462 dup
= pet_expr_set_n_arg(dup
, expr
->n_arg
);
463 for (i
= 0; i
< expr
->n_arg
; ++i
)
464 dup
= pet_expr_set_arg(dup
, i
, pet_expr_copy(expr
->args
[i
]));
466 switch (expr
->type
) {
467 case pet_expr_access
:
468 if (expr
->acc
.ref_id
)
469 dup
= pet_expr_access_set_ref_id(dup
,
470 isl_id_copy(expr
->acc
.ref_id
));
471 dup
= pet_expr_access_set_access(dup
,
472 isl_map_copy(expr
->acc
.access
));
473 dup
= pet_expr_access_set_index(dup
,
474 isl_multi_pw_aff_copy(expr
->acc
.index
));
475 dup
= pet_expr_access_set_read(dup
, expr
->acc
.read
);
476 dup
= pet_expr_access_set_write(dup
, expr
->acc
.write
);
479 dup
= pet_expr_call_set_name(dup
, expr
->name
);
482 dup
= pet_expr_cast_set_type_name(dup
, expr
->type_name
);
484 case pet_expr_double
:
485 dup
= pet_expr_double_set(dup
, expr
->d
.val
, expr
->d
.s
);
488 dup
= pet_expr_int_set_val(dup
, isl_val_copy(expr
->i
));
491 dup
= pet_expr_op_set_type(dup
, expr
->op
);
494 dup
= pet_expr_free(dup
);
501 __isl_give pet_expr
*pet_expr_cow(__isl_take pet_expr
*expr
)
509 return pet_expr_dup(expr
);
512 __isl_null pet_expr
*pet_expr_free(__isl_take pet_expr
*expr
)
521 for (i
= 0; i
< expr
->n_arg
; ++i
)
522 pet_expr_free(expr
->args
[i
]);
525 switch (expr
->type
) {
526 case pet_expr_access
:
527 isl_id_free(expr
->acc
.ref_id
);
528 isl_map_free(expr
->acc
.access
);
529 isl_multi_pw_aff_free(expr
->acc
.index
);
535 free(expr
->type_name
);
537 case pet_expr_double
:
541 isl_val_free(expr
->i
);
548 isl_ctx_deref(expr
->ctx
);
553 /* Return an additional reference to "expr".
555 __isl_give pet_expr
*pet_expr_copy(__isl_keep pet_expr
*expr
)
564 /* Return the isl_ctx in which "expr" was created.
566 isl_ctx
*pet_expr_get_ctx(__isl_keep pet_expr
*expr
)
568 return expr
? expr
->ctx
: NULL
;
571 /* Return the type of "expr".
573 enum pet_expr_type
pet_expr_get_type(__isl_keep pet_expr
*expr
)
576 return pet_expr_error
;
580 /* Return the number of arguments of "expr".
582 int pet_expr_get_n_arg(__isl_keep pet_expr
*expr
)
590 /* Set the number of arguments of "expr" to "n".
592 * If "expr" originally had more arguments, then remove the extra arguments.
593 * If "expr" originally had fewer arguments, then create space for
594 * the extra arguments ans initialize them to NULL.
596 __isl_give pet_expr
*pet_expr_set_n_arg(__isl_take pet_expr
*expr
, int n
)
603 if (expr
->n_arg
== n
)
605 expr
= pet_expr_cow(expr
);
609 if (n
< expr
->n_arg
) {
610 for (i
= n
; i
< expr
->n_arg
; ++i
)
611 pet_expr_free(expr
->args
[i
]);
616 args
= isl_realloc_array(expr
->ctx
, expr
->args
, pet_expr
*, n
);
618 return pet_expr_free(expr
);
620 for (i
= expr
->n_arg
; i
< n
; ++i
)
621 expr
->args
[i
] = NULL
;
627 /* Return the argument of "expr" at position "pos".
629 __isl_give pet_expr
*pet_expr_get_arg(__isl_keep pet_expr
*expr
, int pos
)
633 if (pos
< 0 || pos
>= expr
->n_arg
)
634 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
635 "position out of bounds", return NULL
);
637 return pet_expr_copy(expr
->args
[pos
]);
640 /* Replace the argument of "expr" at position "pos" by "arg".
642 __isl_give pet_expr
*pet_expr_set_arg(__isl_take pet_expr
*expr
, int pos
,
643 __isl_take pet_expr
*arg
)
647 if (pos
< 0 || pos
>= expr
->n_arg
)
648 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
649 "position out of bounds", goto error
);
650 if (expr
->args
[pos
] == arg
) {
655 expr
= pet_expr_cow(expr
);
659 pet_expr_free(expr
->args
[pos
]);
660 expr
->args
[pos
] = arg
;
669 /* Does "expr" perform a comparison operation?
671 int pet_expr_is_comparison(__isl_keep pet_expr
*expr
)
675 if (expr
->type
!= pet_expr_op
)
690 /* Does "expr" perform a boolean operation?
692 int pet_expr_is_boolean(__isl_keep pet_expr
*expr
)
696 if (expr
->type
!= pet_expr_op
)
708 /* Is "expr" an assume statement?
710 int pet_expr_is_assume(__isl_keep pet_expr
*expr
)
714 if (expr
->type
!= pet_expr_op
)
716 return expr
->op
== pet_op_assume
;
719 /* Does "expr" perform a min operation?
721 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
725 if (expr
->type
!= pet_expr_call
)
727 if (expr
->n_arg
!= 2)
729 if (strcmp(expr
->name
, "min") != 0)
734 /* Does "expr" perform a max operation?
736 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
740 if (expr
->type
!= pet_expr_call
)
742 if (expr
->n_arg
!= 2)
744 if (strcmp(expr
->name
, "max") != 0)
749 /* Does "expr" represent an access to an unnamed space, i.e.,
750 * does it represent an affine expression?
752 int pet_expr_is_affine(__isl_keep pet_expr
*expr
)
758 if (expr
->type
!= pet_expr_access
)
761 has_id
= isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
);
768 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
769 * not part of any struct?
771 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
775 if (expr
->type
!= pet_expr_access
)
777 if (isl_map_range_is_wrapping(expr
->acc
.access
))
780 return isl_map_dim(expr
->acc
.access
, isl_dim_out
) == 0;
783 /* Return 1 if the two pet_exprs are equivalent.
785 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
789 if (!expr1
|| !expr2
)
792 if (expr1
->type
!= expr2
->type
)
794 if (expr1
->n_arg
!= expr2
->n_arg
)
796 for (i
= 0; i
< expr1
->n_arg
; ++i
)
797 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
799 switch (expr1
->type
) {
802 case pet_expr_double
:
803 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
805 if (expr1
->d
.val
!= expr2
->d
.val
)
809 if (!isl_val_eq(expr1
->i
, expr2
->i
))
812 case pet_expr_access
:
813 if (expr1
->acc
.read
!= expr2
->acc
.read
)
815 if (expr1
->acc
.write
!= expr2
->acc
.write
)
817 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
819 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
821 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
823 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
825 if (!isl_multi_pw_aff_plain_is_equal(expr1
->acc
.index
,
830 if (expr1
->op
!= expr2
->op
)
834 if (strcmp(expr1
->name
, expr2
->name
))
838 if (strcmp(expr1
->type_name
, expr2
->type_name
))
846 /* Does the access expression "expr" read the accessed elements?
848 int pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
852 if (expr
->type
!= pet_expr_access
)
853 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
854 "not an access expression", return -1);
856 return expr
->acc
.read
;
859 /* Does the access expression "expr" write to the accessed elements?
861 int pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
865 if (expr
->type
!= pet_expr_access
)
866 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
867 "not an access expression", return -1);
869 return expr
->acc
.write
;
872 /* Return the identifier of the array accessed by "expr".
874 * If "expr" represents a member access, then return the identifier
875 * of the outer structure array.
877 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
881 if (expr
->type
!= pet_expr_access
)
882 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
883 "not an access expression", return NULL
);
885 if (isl_map_range_is_wrapping(expr
->acc
.access
)) {
889 space
= isl_map_get_space(expr
->acc
.access
);
890 space
= isl_space_range(space
);
891 while (space
&& isl_space_is_wrapping(space
))
892 space
= isl_space_domain(isl_space_unwrap(space
));
893 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
894 isl_space_free(space
);
899 return isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
902 /* Return the parameter space of "expr".
904 __isl_give isl_space
*pet_expr_access_get_parameter_space(
905 __isl_keep pet_expr
*expr
)
911 if (expr
->type
!= pet_expr_access
)
912 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
913 "not an access expression", return NULL
);
915 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
916 space
= isl_space_params(space
);
921 /* Return the space of the data accessed by "expr".
923 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
929 if (expr
->type
!= pet_expr_access
)
930 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
931 "not an access expression", return NULL
);
933 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
934 space
= isl_space_range(space
);
939 /* Modify all expressions of type pet_expr_access in "expr"
940 * by calling "fn" on them.
942 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
943 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
948 n
= pet_expr_get_n_arg(expr
);
949 for (i
= 0; i
< n
; ++i
) {
950 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
951 arg
= pet_expr_map_access(arg
, fn
, user
);
952 expr
= pet_expr_set_arg(expr
, i
, arg
);
958 if (expr
->type
== pet_expr_access
)
959 expr
= fn(expr
, user
);
964 /* Call "fn" on each of the subexpressions of "expr" of type "type".
966 * Return -1 on error (where fn returning a negative value is treated as
968 * Otherwise return 0.
970 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
971 enum pet_expr_type type
,
972 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
979 for (i
= 0; i
< expr
->n_arg
; ++i
)
980 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
984 if (expr
->type
== type
)
985 return fn(expr
, user
);
990 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
992 * Return -1 on error (where fn returning a negative value is treated as
994 * Otherwise return 0.
996 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
997 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
999 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1002 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1004 * Return -1 on error (where fn returning a negative value is treated as
1006 * Otherwise return 0.
1008 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1009 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1011 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1014 /* Internal data structure for pet_expr_writes.
1015 * "id" is the identifier that we are looking for.
1016 * "found" is set if we have found the identifier being written to.
1018 struct pet_expr_writes_data
{
1023 /* Given an access expression, check if it writes to data->id.
1024 * If so, set data->found and abort the search.
1026 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1028 struct pet_expr_writes_data
*data
= user
;
1031 if (!expr
->acc
.write
)
1033 if (pet_expr_is_affine(expr
))
1036 write_id
= pet_expr_access_get_id(expr
);
1037 isl_id_free(write_id
);
1042 if (write_id
!= data
->id
)
1049 /* Does expression "expr" write to "id"?
1051 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1053 struct pet_expr_writes_data data
;
1057 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1064 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1065 * index expression and access relation of "expr"
1066 * to dimensions of "dst_type" at "dst_pos".
1068 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1069 enum isl_dim_type dst_type
, unsigned dst_pos
,
1070 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1072 expr
= pet_expr_cow(expr
);
1075 if (expr
->type
!= pet_expr_access
)
1076 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1077 "not an access pet_expr", return pet_expr_free(expr
));
1079 expr
->acc
.access
= isl_map_move_dims(expr
->acc
.access
,
1080 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1081 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1082 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1083 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1084 return pet_expr_free(expr
);
1089 /* Replace the index expression and access relation of "expr"
1090 * by their preimages under the function represented by "ma".
1092 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1093 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1095 expr
= pet_expr_cow(expr
);
1098 if (expr
->type
!= pet_expr_access
)
1099 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1100 "not an access pet_expr", goto error
);
1102 expr
->acc
.access
= isl_map_preimage_domain_multi_aff(expr
->acc
.access
,
1103 isl_multi_aff_copy(ma
));
1104 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1106 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1107 return pet_expr_free(expr
);
1111 isl_multi_aff_free(ma
);
1112 pet_expr_free(expr
);
1116 /* Replace the index expression and access relation of "expr"
1117 * by their preimages under the function represented by "mpa".
1119 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1120 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1122 expr
= pet_expr_cow(expr
);
1125 if (expr
->type
!= pet_expr_access
)
1126 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1127 "not an access pet_expr", goto error
);
1129 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
1130 expr
->acc
.access
, isl_multi_pw_aff_copy(mpa
));
1131 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1132 expr
->acc
.index
, mpa
);
1133 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1134 return pet_expr_free(expr
);
1138 isl_multi_pw_aff_free(mpa
);
1139 pet_expr_free(expr
);
1143 /* Return the access relation of access expression "expr".
1145 __isl_give isl_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
)
1149 if (expr
->type
!= pet_expr_access
)
1150 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1151 "not an access expression", return NULL
);
1153 return isl_map_copy(expr
->acc
.access
);
1156 /* Return the index expression of access expression "expr".
1158 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1159 __isl_keep pet_expr
*expr
)
1163 if (expr
->type
!= pet_expr_access
)
1164 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1165 "not an access expression", return NULL
);
1167 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1170 /* Align the parameters of expr->acc.index and expr->acc.access.
1172 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1174 expr
= pet_expr_cow(expr
);
1177 if (expr
->type
!= pet_expr_access
)
1178 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1179 "not an access expression", return pet_expr_free(expr
));
1181 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1182 isl_multi_pw_aff_get_space(expr
->acc
.index
));
1183 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1184 isl_map_get_space(expr
->acc
.access
));
1185 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1186 return pet_expr_free(expr
);
1191 /* Add extra conditions on the parameters to all access relations in "expr".
1193 * The conditions are not added to the index expression. Instead, they
1194 * are used to try and simplify the index expression.
1196 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1197 __isl_take isl_set
*cond
)
1201 expr
= pet_expr_cow(expr
);
1205 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1206 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1207 isl_set_copy(cond
));
1212 if (expr
->type
== pet_expr_access
) {
1213 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
1214 isl_set_copy(cond
));
1215 expr
->acc
.index
= isl_multi_pw_aff_gist_params(
1216 expr
->acc
.index
, isl_set_copy(cond
));
1217 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1225 return pet_expr_free(expr
);
1228 /* Modify the access relation and index expression
1229 * of the given access expression
1230 * based on the given iteration space transformation.
1231 * In particular, precompose the access relation and index expression
1232 * with the update function.
1234 * If the access has any arguments then the domain of the access relation
1235 * is a wrapped mapping from the iteration space to the space of
1236 * argument values. We only need to change the domain of this wrapped
1237 * mapping, so we extend the input transformation with an identity mapping
1238 * on the space of argument values.
1240 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1241 __isl_keep isl_multi_pw_aff
*update
)
1245 expr
= pet_expr_cow(expr
);
1248 if (expr
->type
!= pet_expr_access
)
1249 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1250 "not an access expression", return pet_expr_free(expr
));
1252 update
= isl_multi_pw_aff_copy(update
);
1254 space
= isl_map_get_space(expr
->acc
.access
);
1255 space
= isl_space_domain(space
);
1256 if (!isl_space_is_wrapping(space
))
1257 isl_space_free(space
);
1259 isl_multi_pw_aff
*id
;
1260 space
= isl_space_unwrap(space
);
1261 space
= isl_space_range(space
);
1262 space
= isl_space_map_from_set(space
);
1263 id
= isl_multi_pw_aff_identity(space
);
1264 update
= isl_multi_pw_aff_product(update
, id
);
1267 expr
->acc
.access
= isl_map_preimage_domain_multi_pw_aff(
1269 isl_multi_pw_aff_copy(update
));
1270 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1271 expr
->acc
.index
, update
);
1272 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1273 return pet_expr_free(expr
);
1278 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1280 isl_multi_pw_aff
*update
= user
;
1282 return pet_expr_access_update_domain(expr
, update
);
1285 /* Modify all access relations in "expr" by precomposing them with
1286 * the given iteration space transformation.
1288 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1289 __isl_take isl_multi_pw_aff
*update
)
1291 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1292 isl_multi_pw_aff_free(update
);
1296 /* Add all parameters in "space" to the access relation and index expression
1299 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1301 isl_space
*space
= user
;
1303 expr
= pet_expr_cow(expr
);
1306 if (expr
->type
!= pet_expr_access
)
1307 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1308 "not an access expression", return pet_expr_free(expr
));
1310 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1311 isl_space_copy(space
));
1312 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1313 isl_space_copy(space
));
1314 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1315 return pet_expr_free(expr
);
1320 /* Add all parameters in "space" to all access relations and index expressions
1323 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1324 __isl_take isl_space
*space
)
1326 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1327 isl_space_free(space
);
1331 /* Insert an argument expression corresponding to "test" in front
1332 * of the list of arguments described by *n_arg and *args.
1334 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1335 __isl_keep isl_multi_pw_aff
*test
)
1338 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1341 return pet_expr_free(expr
);
1342 expr
= pet_expr_cow(expr
);
1347 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1349 return pet_expr_free(expr
);
1352 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1354 return pet_expr_free(expr
);
1355 for (i
= 0; i
< expr
->n_arg
; ++i
)
1356 ext
[1 + i
] = expr
->args
[i
];
1361 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1363 return pet_expr_free(expr
);
1368 /* Make the expression "expr" depend on the value of "test"
1369 * being equal to "satisfied".
1371 * If "test" is an affine expression, we simply add the conditions
1372 * on the expression having the value "satisfied" to all access relations
1373 * and index expressions.
1375 * Otherwise, we add a filter to "expr" (which is then assumed to be
1376 * an access expression) corresponding to "test" being equal to "satisfied".
1378 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1379 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1384 isl_pw_multi_aff
*pma
;
1386 expr
= pet_expr_cow(expr
);
1390 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
1394 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
1395 isl_multi_pw_aff_free(test
);
1397 cond
= isl_pw_aff_non_zero_set(pa
);
1399 cond
= isl_pw_aff_zero_set(pa
);
1400 return pet_expr_restrict(expr
, isl_set_params(cond
));
1403 ctx
= isl_multi_pw_aff_get_ctx(test
);
1404 if (expr
->type
!= pet_expr_access
)
1405 isl_die(ctx
, isl_error_invalid
,
1406 "can only filter access expressions", goto error
);
1408 space
= isl_space_domain(isl_map_get_space(expr
->acc
.access
));
1409 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1410 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
1412 expr
->acc
.access
= isl_map_preimage_domain_pw_multi_aff(
1414 isl_pw_multi_aff_copy(pma
));
1415 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
1416 expr
->acc
.index
, pma
);
1417 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1420 expr
= insert_access_arg(expr
, test
);
1422 isl_multi_pw_aff_free(test
);
1425 isl_multi_pw_aff_free(test
);
1426 return pet_expr_free(expr
);
1429 /* Check if the given index expression accesses a (0D) array that corresponds
1430 * to one of the parameters in "space". If so, replace the array access
1431 * by an access to the set of integers with as index (and value)
1434 static __isl_give isl_multi_pw_aff
*index_detect_parameter(
1435 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_space
*space
)
1437 isl_local_space
*ls
;
1438 isl_id
*array_id
= NULL
;
1442 if (isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
)) {
1443 array_id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
1444 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, array_id
);
1446 isl_space_free(space
);
1449 isl_id_free(array_id
);
1453 space
= isl_multi_pw_aff_get_domain_space(index
);
1454 isl_multi_pw_aff_free(index
);
1456 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, array_id
);
1458 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
1459 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, array_id
);
1462 isl_id_free(array_id
);
1464 ls
= isl_local_space_from_space(space
);
1465 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
1466 index
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1471 /* Check if the given access relation accesses a (0D) array that corresponds
1472 * to one of the parameters in "space". If so, replace the array access
1473 * by an access to the set of integers with as index (and value)
1476 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
1477 __isl_take isl_space
*space
)
1479 isl_id
*array_id
= NULL
;
1482 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
1483 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1484 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, array_id
);
1486 isl_space_free(space
);
1489 isl_id_free(array_id
);
1493 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
1495 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
1496 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
1499 isl_id_free(array_id
);
1501 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1502 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
1507 /* If "expr" accesses a (0D) array that corresponds to one of the parameters
1508 * in "space" then replace it by a value equal to the corresponding parameter.
1510 static __isl_give pet_expr
*detect_parameter_accesses(__isl_take pet_expr
*expr
,
1513 isl_space
*space
= user
;
1515 expr
= pet_expr_cow(expr
);
1518 if (expr
->type
!= pet_expr_access
)
1519 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1520 "not an access expression", return pet_expr_free(expr
));
1522 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
1523 isl_space_copy(space
));
1524 expr
->acc
.index
= index_detect_parameter(expr
->acc
.index
,
1525 isl_space_copy(space
));
1526 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1527 return pet_expr_free(expr
);
1532 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1533 * in "space" by a value equal to the corresponding parameter.
1535 __isl_give pet_expr
*pet_expr_detect_parameter_accesses(
1536 __isl_take pet_expr
*expr
, __isl_take isl_space
*space
)
1538 expr
= pet_expr_map_access(expr
, &detect_parameter_accesses
, space
);
1539 isl_space_free(space
);
1543 /* Add a reference identifier to access expression "expr".
1544 * "user" points to an integer that contains the sequence number
1545 * of the next reference.
1547 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
1554 expr
= pet_expr_cow(expr
);
1557 if (expr
->type
!= pet_expr_access
)
1558 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1559 "not an access expression", return pet_expr_free(expr
));
1561 ctx
= isl_map_get_ctx(expr
->acc
.access
);
1562 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
1563 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
1564 if (!expr
->acc
.ref_id
)
1565 return pet_expr_free(expr
);
1570 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
1572 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
1575 /* Reset the user pointer on all parameter and tuple ids in
1576 * the access relation and the index expressions
1577 * of the access expression "expr".
1579 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
1582 expr
= pet_expr_cow(expr
);
1585 if (expr
->type
!= pet_expr_access
)
1586 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1587 "not an access expression", return pet_expr_free(expr
));
1589 expr
->acc
.access
= isl_map_reset_user(expr
->acc
.access
);
1590 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
1591 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1592 return pet_expr_free(expr
);
1597 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
1599 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
1602 /* Data used in access_gist() callback.
1604 struct pet_access_gist_data
{
1606 isl_union_map
*value_bounds
;
1609 /* Given an expression "expr" of type pet_expr_access, compute
1610 * the gist of the associated access relation and index expression
1611 * with respect to data->domain and the bounds on the values of the arguments
1612 * of the expression.
1614 * The arguments of "expr" have been gisted right before "expr" itself
1615 * is gisted. The gisted arguments may have become equal where before
1616 * they may not have been (obviously) equal. We therefore take
1617 * the opportunity to remove duplicate arguments here.
1619 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
1621 struct pet_access_gist_data
*data
= user
;
1624 expr
= pet_expr_remove_duplicate_args(expr
);
1625 expr
= pet_expr_cow(expr
);
1628 if (expr
->type
!= pet_expr_access
)
1629 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1630 "not an access expression", return pet_expr_free(expr
));
1632 domain
= isl_set_copy(data
->domain
);
1633 if (expr
->n_arg
> 0)
1634 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
1635 data
->value_bounds
);
1637 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
,
1638 isl_set_copy(domain
));
1639 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
1640 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1641 return pet_expr_free(expr
);
1646 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
1647 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
1649 struct pet_access_gist_data data
= { context
, value_bounds
};
1651 return pet_expr_map_access(expr
, &access_gist
, &data
);
1654 /* Mark "expr" as a read dependening on "read".
1656 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
1660 return pet_expr_free(expr
);
1661 if (expr
->type
!= pet_expr_access
)
1662 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1663 "not an access expression", return pet_expr_free(expr
));
1664 if (expr
->acc
.read
== read
)
1666 expr
= pet_expr_cow(expr
);
1669 expr
->acc
.read
= read
;
1674 /* Mark "expr" as a write dependening on "write".
1676 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
1680 return pet_expr_free(expr
);
1681 if (expr
->type
!= pet_expr_access
)
1682 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1683 "not an access expression", return pet_expr_free(expr
));
1684 if (expr
->acc
.write
== write
)
1686 expr
= pet_expr_cow(expr
);
1689 expr
->acc
.write
= write
;
1694 /* Replace the access relation of "expr" by "access".
1696 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
1697 __isl_take isl_map
*access
)
1699 expr
= pet_expr_cow(expr
);
1700 if (!expr
|| !access
)
1702 if (expr
->type
!= pet_expr_access
)
1703 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1704 "not an access expression", goto error
);
1705 isl_map_free(expr
->acc
.access
);
1706 expr
->acc
.access
= access
;
1710 isl_map_free(access
);
1711 pet_expr_free(expr
);
1715 /* Replace the index expression of "expr" by "index".
1717 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
1718 __isl_take isl_multi_pw_aff
*index
)
1720 expr
= pet_expr_cow(expr
);
1721 if (!expr
|| !index
)
1723 if (expr
->type
!= pet_expr_access
)
1724 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1725 "not an access expression", goto error
);
1726 isl_multi_pw_aff_free(expr
->acc
.index
);
1727 expr
->acc
.index
= index
;
1731 isl_multi_pw_aff_free(index
);
1732 pet_expr_free(expr
);
1736 /* Return the reference identifier of access expression "expr".
1738 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
1742 if (expr
->type
!= pet_expr_access
)
1743 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1744 "not an access expression", return NULL
);
1746 return isl_id_copy(expr
->acc
.ref_id
);
1749 /* Replace the reference identifier of access expression "expr" by "ref_id".
1751 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
1752 __isl_take isl_id
*ref_id
)
1754 expr
= pet_expr_cow(expr
);
1755 if (!expr
|| !ref_id
)
1757 if (expr
->type
!= pet_expr_access
)
1758 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1759 "not an access expression", goto error
);
1760 isl_id_free(expr
->acc
.ref_id
);
1761 expr
->acc
.ref_id
= ref_id
;
1765 isl_id_free(ref_id
);
1766 pet_expr_free(expr
);
1770 /* Tag the access relation "access" with "id".
1771 * That is, insert the id as the range of a wrapped relation
1772 * in the domain of "access".
1774 * If "access" is of the form
1778 * then the result is of the form
1780 * [D[i] -> id[]] -> A[a]
1782 __isl_give isl_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
1783 __isl_take isl_map
*access
)
1789 if (expr
->type
!= pet_expr_access
)
1790 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1791 "not an access expression",
1792 return isl_map_free(access
));
1794 id
= isl_id_copy(expr
->acc
.ref_id
);
1795 space
= isl_space_range(isl_map_get_space(access
));
1796 space
= isl_space_from_range(space
);
1797 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
1798 add_tag
= isl_map_universe(space
);
1799 access
= isl_map_domain_product(access
, add_tag
);
1804 /* Return the relation mapping pairs of domain iterations and argument
1805 * values to the corresponding accessed data elements.
1807 __isl_give isl_map
*pet_expr_access_get_dependent_access(
1808 __isl_keep pet_expr
*expr
)
1812 if (expr
->type
!= pet_expr_access
)
1813 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1814 "not an access expression", return NULL
);
1816 return isl_map_copy(expr
->acc
.access
);
1819 /* Return the relation mapping domain iterations to all possibly
1820 * accessed data elements.
1821 * In particular, take the access relation and project out the values
1822 * of the arguments, if any.
1824 __isl_give isl_map
*pet_expr_access_get_may_access(__isl_keep pet_expr
*expr
)
1832 if (expr
->type
!= pet_expr_access
)
1833 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1834 "not an access expression", return NULL
);
1836 access
= pet_expr_access_get_dependent_access(expr
);
1837 if (expr
->n_arg
== 0)
1840 space
= isl_space_domain(isl_map_get_space(access
));
1841 map
= isl_map_universe(isl_space_unwrap(space
));
1842 map
= isl_map_domain_map(map
);
1843 access
= isl_map_apply_domain(access
, map
);
1848 /* Return a relation mapping domain iterations to definitely
1849 * accessed data elements, assuming the statement containing
1850 * the expression is executed.
1852 * If there are no arguments, then all elements are accessed.
1853 * Otherwise, we conservatively return an empty relation.
1855 __isl_give isl_map
*pet_expr_access_get_must_access(__isl_keep pet_expr
*expr
)
1861 if (expr
->type
!= pet_expr_access
)
1862 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1863 "not an access expression", return NULL
);
1865 if (expr
->n_arg
== 0)
1866 return pet_expr_access_get_dependent_access(expr
);
1868 space
= isl_map_get_space(expr
->acc
.access
);
1869 space
= isl_space_domain_factor_domain(space
);
1871 return isl_map_empty(space
);
1874 /* Return the relation mapping domain iterations to all possibly
1875 * accessed data elements, with its domain tagged with the reference
1878 __isl_give isl_map
*pet_expr_access_get_tagged_may_access(
1879 __isl_keep pet_expr
*expr
)
1886 access
= pet_expr_access_get_may_access(expr
);
1887 access
= pet_expr_tag_access(expr
, access
);
1892 /* Return the operation type of operation expression "expr".
1894 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
1898 if (expr
->type
!= pet_expr_op
)
1899 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1900 "not an operation expression", return pet_op_last
);
1905 /* Replace the operation type of operation expression "expr" by "type".
1907 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
1908 enum pet_op_type type
)
1911 return pet_expr_free(expr
);
1912 if (expr
->type
!= pet_expr_op
)
1913 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1914 "not an operation expression",
1915 return pet_expr_free(expr
));
1916 if (expr
->op
== type
)
1918 expr
= pet_expr_cow(expr
);
1926 /* Return the name of the function called by "expr".
1928 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
1932 if (expr
->type
!= pet_expr_call
)
1933 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1934 "not a call expression", return NULL
);
1938 /* Replace the name of the function called by "expr" by "name".
1940 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
1941 __isl_keep
const char *name
)
1943 expr
= pet_expr_cow(expr
);
1945 return pet_expr_free(expr
);
1946 if (expr
->type
!= pet_expr_call
)
1947 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1948 "not a call expression", return pet_expr_free(expr
));
1950 expr
->name
= strdup(name
);
1952 return pet_expr_free(expr
);
1956 /* Replace the type of the cast performed by "expr" by "name".
1958 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
1959 __isl_keep
const char *name
)
1961 expr
= pet_expr_cow(expr
);
1963 return pet_expr_free(expr
);
1964 if (expr
->type
!= pet_expr_cast
)
1965 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1966 "not a cast expression", return pet_expr_free(expr
));
1967 free(expr
->type_name
);
1968 expr
->type_name
= strdup(name
);
1969 if (!expr
->type_name
)
1970 return pet_expr_free(expr
);
1974 /* Return the value of the integer represented by "expr".
1976 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
1980 if (expr
->type
!= pet_expr_int
)
1981 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1982 "not an int expression", return NULL
);
1984 return isl_val_copy(expr
->i
);
1987 /* Replace the value of the integer represented by "expr" by "v".
1989 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
1990 __isl_take isl_val
*v
)
1992 expr
= pet_expr_cow(expr
);
1995 if (expr
->type
!= pet_expr_int
)
1996 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1997 "not an int expression", goto error
);
1998 isl_val_free(expr
->i
);
2004 pet_expr_free(expr
);
2008 /* Replace the value and string representation of the double
2009 * represented by "expr" by "d" and "s".
2011 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2012 double d
, __isl_keep
const char *s
)
2014 expr
= pet_expr_cow(expr
);
2016 return pet_expr_free(expr
);
2017 if (expr
->type
!= pet_expr_double
)
2018 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2019 "not a double expression", return pet_expr_free(expr
));
2022 expr
->d
.s
= strdup(s
);
2024 return pet_expr_free(expr
);
2028 /* Return a string representation of the double expression "expr".
2030 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2034 if (expr
->type
!= pet_expr_double
)
2035 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2036 "not a double expression", return NULL
);
2037 return strdup(expr
->d
.s
);
2040 /* Return a piecewise affine expression defined on the specified domain
2041 * that represents NaN.
2043 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2045 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2048 /* This function is called when we come across an access that is
2049 * nested in what is supposed to be an affine expression.
2050 * "pc" is the context in which the affine expression is created.
2051 * If nesting is allowed in "pc", we return an affine expression that is
2052 * equal to a new parameter corresponding to this nested access.
2053 * Otherwise, we return NaN.
2055 * Note that we currently don't allow nested accesses themselves
2056 * to contain any nested accesses, so we check if "expr" itself
2057 * involves any nested accesses (either explicitly as arguments
2058 * or implicitly through parameters) and return NaN if it does.
2060 * The new parameter is resolved in resolve_nested.
2062 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2063 __isl_keep pet_context
*pc
)
2068 isl_local_space
*ls
;
2074 if (!pet_context_allow_nesting(pc
))
2075 return non_affine(pet_context_get_space(pc
));
2077 if (pet_expr_get_type(expr
) != pet_expr_access
)
2078 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2079 "not an access expression", return NULL
);
2081 if (expr
->n_arg
> 0)
2082 return non_affine(pet_context_get_space(pc
));
2084 space
= pet_expr_access_get_parameter_space(expr
);
2085 nested
= pet_nested_any_in_space(space
);
2086 isl_space_free(space
);
2088 return non_affine(pet_context_get_space(pc
));
2090 ctx
= pet_expr_get_ctx(expr
);
2091 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2092 space
= pet_context_get_space(pc
);
2093 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2095 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2096 ls
= isl_local_space_from_space(space
);
2097 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2099 return isl_pw_aff_from_aff(aff
);
2102 /* Extract an affine expression from the access pet_expr "expr".
2103 * "pc" is the context in which the affine expression is created.
2105 * If "expr" is actually an affine expression rather than
2106 * a real access, then we return that expression.
2107 * Otherwise, we require that "expr" is of an integral type.
2108 * If not, we return NaN.
2110 * If we are accessing a scalar (i.e., not an array and not a member)
2111 * and if that scalar can be treated as a parameter (because it is
2112 * not assigned a known or unknown value in the relevant part of the AST),
2113 * then we return an affine expression equal to that parameter.
2115 * If the variable has been assigned a known affine expression,
2116 * then we return that expression.
2118 * Otherwise, we return an expression that is equal to a parameter
2119 * representing "expr" (if "allow_nested" is set).
2121 static __isl_give isl_pw_aff
*extract_affine_from_access(
2122 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2127 isl_local_space
*ls
;
2130 if (pet_expr_is_affine(expr
)) {
2132 isl_multi_pw_aff
*mpa
;
2134 mpa
= pet_expr_access_get_index(expr
);
2135 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
2136 isl_multi_pw_aff_free(mpa
);
2140 if (pet_expr_get_type_size(expr
) == 0)
2141 return non_affine(pet_context_get_space(pc
));
2143 if (!pet_expr_is_scalar_access(expr
))
2144 return nested_access(expr
, pc
);
2146 id
= pet_expr_access_get_id(expr
);
2147 if (pet_context_is_assigned(pc
, id
)) {
2150 pa
= pet_context_get_value(pc
, id
);
2153 if (!isl_pw_aff_involves_nan(pa
))
2155 isl_pw_aff_free(pa
);
2156 return nested_access(expr
, pc
);
2159 space
= pet_context_get_space(pc
);
2161 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2165 pos
= isl_space_dim(space
, isl_dim_param
);
2166 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
2167 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
2170 ls
= isl_local_space_from_space(space
);
2171 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
2173 return isl_pw_aff_from_aff(aff
);
2176 /* Construct an affine expression from the integer constant "expr".
2177 * "pc" is the context in which the affine expression is created.
2179 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2180 __isl_keep pet_context
*pc
)
2182 isl_local_space
*ls
;
2188 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2189 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2191 return isl_pw_aff_from_aff(aff
);
2194 /* Extract an affine expression from an addition or subtraction operation.
2195 * Return NaN if we are unable to extract an affine expression.
2197 * "pc" is the context in which the affine expression is created.
2199 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2200 __isl_keep pet_context
*pc
)
2207 if (expr
->n_arg
!= 2)
2208 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2209 "expecting two arguments", return NULL
);
2211 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2212 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2214 switch (pet_expr_op_get_type(expr
)) {
2216 return isl_pw_aff_add(lhs
, rhs
);
2218 return isl_pw_aff_sub(lhs
, rhs
);
2220 isl_pw_aff_free(lhs
);
2221 isl_pw_aff_free(rhs
);
2222 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2223 "not an addition or subtraction operation",
2229 /* Extract an affine expression from an integer division or a modulo operation.
2230 * Return NaN if we are unable to extract an affine expression.
2232 * "pc" is the context in which the affine expression is created.
2234 * In particular, if "expr" is lhs/rhs, then return
2236 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2238 * If "expr" is lhs%rhs, then return
2240 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2242 * If the second argument (rhs) is not a (positive) integer constant,
2243 * then we fail to extract an affine expression.
2245 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2246 __isl_keep pet_context
*pc
)
2254 if (expr
->n_arg
!= 2)
2255 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2256 "expecting two arguments", return NULL
);
2258 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2260 is_cst
= isl_pw_aff_is_cst(rhs
);
2261 if (is_cst
< 0 || !is_cst
) {
2262 isl_pw_aff_free(rhs
);
2263 return non_affine(pet_context_get_space(pc
));
2266 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2268 switch (pet_expr_op_get_type(expr
)) {
2270 return isl_pw_aff_tdiv_q(lhs
, rhs
);
2272 return isl_pw_aff_tdiv_r(lhs
, rhs
);
2274 isl_pw_aff_free(lhs
);
2275 isl_pw_aff_free(rhs
);
2276 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2277 "not a div or mod operator", return NULL
);
2282 /* Extract an affine expression from a multiplication operation.
2283 * Return NaN if we are unable to extract an affine expression.
2284 * In particular, if neither of the arguments is a (piecewise) constant
2285 * then we return NaN.
2287 * "pc" is the context in which the affine expression is created.
2289 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2290 __isl_keep pet_context
*pc
)
2292 int lhs_cst
, rhs_cst
;
2298 if (expr
->n_arg
!= 2)
2299 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2300 "expecting two arguments", return NULL
);
2302 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2303 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2305 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2306 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2307 if (lhs_cst
< 0 || rhs_cst
< 0 || (!lhs_cst
&& !rhs_cst
)) {
2308 isl_pw_aff_free(lhs
);
2309 isl_pw_aff_free(rhs
);
2310 return non_affine(pet_context_get_space(pc
));
2313 return isl_pw_aff_mul(lhs
, rhs
);
2316 /* Extract an affine expression from a negation operation.
2317 * Return NaN if we are unable to extract an affine expression.
2319 * "pc" is the context in which the affine expression is created.
2321 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
2322 __isl_keep pet_context
*pc
)
2328 if (expr
->n_arg
!= 1)
2329 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2330 "expecting one argument", return NULL
);
2332 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
2333 return isl_pw_aff_neg(res
);
2336 /* Extract an affine expression from a conditional operation.
2337 * Return NaN if we are unable to extract an affine expression.
2339 * "pc" is the context in which the affine expression is created.
2341 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
2342 __isl_keep pet_context
*pc
)
2344 isl_pw_aff
*cond
, *lhs
, *rhs
;
2348 if (expr
->n_arg
!= 3)
2349 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2350 "expecting three arguments", return NULL
);
2352 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2353 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2354 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
2356 return isl_pw_aff_cond(cond
, lhs
, rhs
);
2363 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
, unsigned width
)
2368 ctx
= isl_pw_aff_get_ctx(pwaff
);
2369 mod
= isl_val_int_from_ui(ctx
, width
);
2370 mod
= isl_val_2exp(mod
);
2372 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
2377 /* Limit the domain of "pwaff" to those elements where the function
2380 * 2^{width-1} <= pwaff < 2^{width-1}
2382 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
2387 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
2388 isl_local_space
*ls
= isl_local_space_from_space(space
);
2393 ctx
= isl_pw_aff_get_ctx(pwaff
);
2394 v
= isl_val_int_from_ui(ctx
, width
- 1);
2395 v
= isl_val_2exp(v
);
2397 bound
= isl_aff_zero_on_domain(ls
);
2398 bound
= isl_aff_add_constant_val(bound
, v
);
2399 b
= isl_pw_aff_from_aff(bound
);
2401 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
2402 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2404 b
= isl_pw_aff_neg(b
);
2405 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
2406 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2411 /* Handle potential overflows on signed computations.
2413 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2414 * then we adjust the domain of "pa" to avoid overflows.
2416 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
2420 struct pet_options
*options
;
2425 ctx
= isl_pw_aff_get_ctx(pa
);
2426 options
= isl_ctx_peek_pet_options(ctx
);
2427 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
2428 pa
= avoid_overflow(pa
, width
);
2433 /* Extract an affine expression from some an operation.
2434 * Return NaN if we are unable to extract an affine expression.
2435 * If the result of a binary (non boolean) operation is unsigned,
2436 * then we wrap it based on the size of the type. If the result is signed,
2437 * then we ensure that no overflow occurs.
2439 * "pc" is the context in which the affine expression is created.
2441 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
2442 __isl_keep pet_context
*pc
)
2447 switch (pet_expr_op_get_type(expr
)) {
2450 res
= extract_affine_add_sub(expr
, pc
);
2454 res
= extract_affine_div_mod(expr
, pc
);
2457 res
= extract_affine_mul(expr
, pc
);
2460 return extract_affine_neg(expr
, pc
);
2462 return extract_affine_cond(expr
, pc
);
2472 return pet_expr_extract_affine_condition(expr
, pc
);
2474 return non_affine(pet_context_get_space(pc
));
2479 if (isl_pw_aff_involves_nan(res
)) {
2480 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
2481 isl_pw_aff_free(res
);
2482 return non_affine(space
);
2485 type_size
= pet_expr_get_type_size(expr
);
2487 res
= wrap(res
, type_size
);
2489 res
= signed_overflow(res
, -type_size
);
2494 /* Extract an affine expression from some special function calls.
2495 * Return NaN if we are unable to extract an affine expression.
2496 * In particular, we handle "min", "max", "ceild", "floord",
2497 * "intMod", "intFloor" and "intCeil".
2498 * In case of the latter five, the second argument needs to be
2499 * a (positive) integer constant.
2501 * "pc" is the context in which the affine expression is created.
2503 static __isl_give isl_pw_aff
*extract_affine_from_call(
2504 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2506 isl_pw_aff
*aff1
, *aff2
;
2510 n
= pet_expr_get_n_arg(expr
);
2511 name
= pet_expr_call_get_name(expr
);
2512 if (!(n
== 2 && !strcmp(name
, "min")) &&
2513 !(n
== 2 && !strcmp(name
, "max")) &&
2514 !(n
== 2 && !strcmp(name
, "intMod")) &&
2515 !(n
== 2 && !strcmp(name
, "intFloor")) &&
2516 !(n
== 2 && !strcmp(name
, "intCeil")) &&
2517 !(n
== 2 && !strcmp(name
, "floord")) &&
2518 !(n
== 2 && !strcmp(name
, "ceild")))
2519 return non_affine(pet_context_get_space(pc
));
2521 if (!strcmp(name
, "min") || !strcmp(name
, "max")) {
2522 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2523 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
2525 if (!strcmp(name
, "min"))
2526 aff1
= isl_pw_aff_min(aff1
, aff2
);
2528 aff1
= isl_pw_aff_max(aff1
, aff2
);
2529 } else if (!strcmp(name
, "intMod")) {
2532 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2533 return non_affine(pet_context_get_space(pc
));
2534 v
= pet_expr_int_get_val(expr
->args
[1]);
2535 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2536 aff1
= isl_pw_aff_mod_val(aff1
, v
);
2540 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
2541 return non_affine(pet_context_get_space(pc
));
2542 v
= pet_expr_int_get_val(expr
->args
[1]);
2543 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
2544 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
2545 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
2546 aff1
= isl_pw_aff_floor(aff1
);
2548 aff1
= isl_pw_aff_ceil(aff1
);
2554 /* Extract an affine expression from "expr", if possible.
2555 * Otherwise return NaN.
2557 * "pc" is the context in which the affine expression is created.
2559 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
2560 __isl_keep pet_context
*pc
)
2565 switch (pet_expr_get_type(expr
)) {
2566 case pet_expr_access
:
2567 return extract_affine_from_access(expr
, pc
);
2569 return extract_affine_from_int(expr
, pc
);
2571 return extract_affine_from_op(expr
, pc
);
2573 return extract_affine_from_call(expr
, pc
);
2575 case pet_expr_double
:
2576 case pet_expr_error
:
2577 return non_affine(pet_context_get_space(pc
));
2581 /* Extract an affine expressions representing the comparison "LHS op RHS"
2582 * Return NaN if we are unable to extract such an affine expression.
2584 * "pc" is the context in which the affine expression is created.
2586 * If the comparison is of the form
2590 * then the expression is constructed as the conjunction of
2595 * A similar optimization is performed for max(a,b) <= c.
2596 * We do this because that will lead to simpler representations
2597 * of the expression.
2598 * If isl is ever enhanced to explicitly deal with min and max expressions,
2599 * this optimization can be removed.
2601 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
2602 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
2603 __isl_keep pet_context
*pc
)
2605 isl_pw_aff
*lhs_pa
, *rhs_pa
;
2607 if (op
== pet_op_gt
)
2608 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
2609 if (op
== pet_op_ge
)
2610 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
2612 if (op
== pet_op_lt
|| op
== pet_op_le
) {
2613 if (pet_expr_is_min(rhs
)) {
2614 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2616 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
2618 return pet_and(lhs_pa
, rhs_pa
);
2620 if (pet_expr_is_max(lhs
)) {
2621 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
2623 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
2625 return pet_and(lhs_pa
, rhs_pa
);
2629 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
2630 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
2632 return pet_comparison(op
, lhs_pa
, rhs_pa
);
2635 /* Extract an affine expressions from the comparison "expr".
2636 * Return NaN if we are unable to extract such an affine expression.
2638 * "pc" is the context in which the affine expression is created.
2640 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
2641 __isl_keep pet_context
*pc
)
2643 enum pet_op_type type
;
2647 if (expr
->n_arg
!= 2)
2648 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2649 "expecting two arguments", return NULL
);
2651 type
= pet_expr_op_get_type(expr
);
2652 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
2656 /* Extract an affine expression representing the boolean operation
2657 * expressed by "expr".
2658 * Return NaN if we are unable to extract an affine expression.
2660 * "pc" is the context in which the affine expression is created.
2662 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
2663 __isl_keep pet_context
*pc
)
2665 isl_pw_aff
*lhs
, *rhs
;
2671 n
= pet_expr_get_n_arg(expr
);
2672 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2674 return pet_not(lhs
);
2676 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
2677 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
2680 /* Extract the affine expression "expr != 0 ? 1 : 0".
2681 * Return NaN if we are unable to extract an affine expression.
2683 * "pc" is the context in which the affine expression is created.
2685 static __isl_give isl_pw_aff
*extract_implicit_condition(
2686 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2690 res
= pet_expr_extract_affine(expr
, pc
);
2691 return pet_to_bool(res
);
2694 /* Extract a boolean affine expression from "expr".
2695 * Return NaN if we are unable to extract an affine expression.
2697 * "pc" is the context in which the affine expression is created.
2699 * If "expr" is neither a comparison nor a boolean operation,
2700 * then we assume it is an affine expression and return the
2701 * boolean expression "expr != 0 ? 1 : 0".
2703 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
2704 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2709 if (pet_expr_is_comparison(expr
))
2710 return extract_comparison(expr
, pc
);
2711 if (pet_expr_is_boolean(expr
))
2712 return extract_boolean(expr
, pc
);
2714 return extract_implicit_condition(expr
, pc
);
2717 /* Check if "expr" is an assume expression and if its single argument
2718 * can be converted to an affine expression in the context of "pc".
2719 * If so, replace the argument by the affine expression.
2721 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
2722 __isl_keep pet_context
*pc
)
2725 isl_multi_pw_aff
*index
;
2729 if (!pet_expr_is_assume(expr
))
2731 if (expr
->n_arg
!= 1)
2732 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2733 "expecting one argument", return pet_expr_free(expr
));
2735 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2737 return pet_expr_free(expr
);
2738 if (isl_pw_aff_involves_nan(cond
)) {
2739 isl_pw_aff_free(cond
);
2743 index
= isl_multi_pw_aff_from_pw_aff(cond
);
2744 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
2749 /* Return the number of bits needed to represent the type of "expr".
2750 * See the description of the type_size field of pet_expr.
2752 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
2754 return expr
? expr
->type_size
: 0;
2757 /* Replace the number of bits needed to represent the type of "expr"
2759 * See the description of the type_size field of pet_expr.
2761 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
2764 expr
= pet_expr_cow(expr
);
2768 expr
->type_size
= type_size
;
2773 /* Extend an access expression "expr" with an additional index "index".
2774 * In particular, add "index" as an extra argument to "expr" and
2775 * adjust the index expression of "expr" to refer to this extra argument.
2776 * The caller is responsible for calling pet_expr_access_set_depth
2777 * to update the corresponding access relation.
2779 * Note that we only collect the individual index expressions as
2780 * arguments of "expr" here.
2781 * An attempt to integrate them into the index expression of "expr"
2782 * is performed in pet_expr_access_plug_in_args.
2784 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
2785 __isl_take pet_expr
*index
)
2789 isl_local_space
*ls
;
2792 expr
= pet_expr_cow(expr
);
2793 if (!expr
|| !index
)
2795 if (expr
->type
!= pet_expr_access
)
2796 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2797 "not an access pet_expr", goto error
);
2799 n
= pet_expr_get_n_arg(expr
);
2800 expr
= pet_expr_insert_arg(expr
, n
, index
);
2804 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
2805 ls
= isl_local_space_from_space(space
);
2806 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
2807 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
2808 if (!expr
->acc
.index
)
2809 return pet_expr_free(expr
);
2813 pet_expr_free(expr
);
2814 pet_expr_free(index
);
2818 /* Extend an access expression "expr" with an additional member acces to "id".
2819 * In particular, extend the index expression of "expr" to include
2820 * the additional member access.
2821 * The caller is responsible for calling pet_expr_access_set_depth
2822 * to update the corresponding access relation.
2824 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
2825 __isl_take isl_id
*id
)
2828 isl_multi_pw_aff
*field_access
;
2830 expr
= pet_expr_cow(expr
);
2833 if (expr
->type
!= pet_expr_access
)
2834 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2835 "not an access pet_expr", goto error
);
2837 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
2838 space
= isl_space_from_domain(space
);
2839 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2840 field_access
= isl_multi_pw_aff_zero(space
);
2841 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
2842 if (!expr
->acc
.index
)
2843 return pet_expr_free(expr
);
2847 pet_expr_free(expr
);
2852 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
2859 fprintf(stderr
, "%*s", indent
, "");
2861 switch (expr
->type
) {
2862 case pet_expr_double
:
2863 fprintf(stderr
, "%s\n", expr
->d
.s
);
2866 isl_val_dump(expr
->i
);
2868 case pet_expr_access
:
2869 if (expr
->acc
.ref_id
) {
2870 isl_id_dump(expr
->acc
.ref_id
);
2871 fprintf(stderr
, "%*s", indent
, "");
2873 isl_map_dump(expr
->acc
.access
);
2874 fprintf(stderr
, "%*s", indent
, "");
2875 isl_multi_pw_aff_dump(expr
->acc
.index
);
2876 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
2877 "", expr
->acc
.read
);
2878 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
2879 "", expr
->acc
.write
);
2880 for (i
= 0; i
< expr
->n_arg
; ++i
)
2881 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2884 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
2885 for (i
= 0; i
< expr
->n_arg
; ++i
)
2886 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2889 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
2890 for (i
= 0; i
< expr
->n_arg
; ++i
)
2891 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2894 fprintf(stderr
, "(%s)\n", expr
->type_name
);
2895 for (i
= 0; i
< expr
->n_arg
; ++i
)
2896 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
2898 case pet_expr_error
:
2899 fprintf(stderr
, "ERROR\n");
2904 void pet_expr_dump(__isl_keep pet_expr
*expr
)
2906 pet_expr_dump_with_indent(expr
, 0);