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_assign
] = "=",
90 [pet_op_post_inc
] = "++",
91 [pet_op_post_dec
] = "--",
92 [pet_op_pre_inc
] = "++",
93 [pet_op_pre_dec
] = "--",
94 [pet_op_address_of
] = "&",
102 [pet_op_cond
] = "?:",
103 [pet_op_assume
] = "assume",
104 [pet_op_kill
] = "kill"
107 const char *pet_op_str(enum pet_op_type op
)
112 int pet_op_is_inc_dec(enum pet_op_type op
)
114 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
115 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
118 const char *pet_type_str(enum pet_expr_type type
)
120 return type_str
[type
];
123 enum pet_op_type
pet_str_op(const char *str
)
127 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
128 if (!strcmp(op_str
[i
], str
))
134 enum pet_expr_type
pet_str_type(const char *str
)
138 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
139 if (!strcmp(type_str
[i
], str
))
145 /* Construct a pet_expr of the given type.
147 __isl_give pet_expr
*pet_expr_alloc(isl_ctx
*ctx
, enum pet_expr_type type
)
151 expr
= isl_calloc_type(ctx
, struct pet_expr
);
163 /* Construct an access pet_expr from an index expression.
164 * By default, the access is considered to be a read access.
165 * The initial depth is set from the index expression and
166 * may still be updated by the caller before the access relation
169 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
176 ctx
= isl_multi_pw_aff_get_ctx(index
);
177 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
184 expr
= pet_expr_access_set_index(expr
, index
);
188 isl_multi_pw_aff_free(index
);
192 /* Extend the range of "access" with "n" dimensions, retaining
193 * the tuple identifier on this range.
195 * If "access" represents a member access, then extend the range
198 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
202 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
204 if (!isl_map_range_is_wrapping(access
)) {
205 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
209 domain
= isl_map_copy(access
);
210 domain
= isl_map_range_factor_domain(domain
);
211 access
= isl_map_range_factor_range(access
);
212 access
= extend_range(access
, n
);
213 access
= isl_map_range_product(domain
, access
);
216 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
221 /* Does the access expression "expr" have any explicit access relation?
223 isl_bool
pet_expr_access_has_any_access_relation(__isl_keep pet_expr
*expr
)
225 enum pet_expr_access_type type
;
228 return isl_bool_error
;
230 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
)
231 if (expr
->acc
.access
[type
])
232 return isl_bool_true
;
234 return isl_bool_false
;
237 /* Are all relevant access relations explicitly available in "expr"?
239 static int has_relevant_access_relations(__isl_keep pet_expr
*expr
)
244 if (expr
->acc
.kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
246 if (expr
->acc
.read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
248 if (expr
->acc
.write
&&
249 (!expr
->acc
.access
[pet_expr_access_may_write
] ||
250 !expr
->acc
.access
[pet_expr_access_must_write
]))
256 /* Replace the depth of the access expr "expr" by "depth".
258 * To avoid inconsistencies between the depth and the access relation,
259 * we currently do not allow the depth to change once the access relation
260 * has been set or computed.
262 __isl_give pet_expr
*pet_expr_access_set_depth(__isl_take pet_expr
*expr
,
267 if (expr
->acc
.depth
== depth
)
269 if (pet_expr_access_has_any_access_relation(expr
))
270 isl_die(pet_expr_get_ctx(expr
), isl_error_unsupported
,
271 "depth cannot be changed after access relation "
272 "has been set or computed", return pet_expr_free(expr
));
274 expr
= pet_expr_cow(expr
);
277 expr
->acc
.depth
= depth
;
282 /* Construct a pet_expr that kills the elements specified by
283 * the index expression "index" and the access relation "access".
285 __isl_give pet_expr
*pet_expr_kill_from_access_and_index(
286 __isl_take isl_map
*access
, __isl_take isl_multi_pw_aff
*index
)
291 if (!access
|| !index
)
294 expr
= pet_expr_from_index(index
);
295 expr
= pet_expr_access_set_read(expr
, 0);
296 expr
= pet_expr_access_set_kill(expr
, 1);
297 depth
= isl_map_dim(access
, isl_dim_out
);
298 expr
= pet_expr_access_set_depth(expr
, depth
);
299 expr
= pet_expr_access_set_access(expr
, pet_expr_access_killed
,
300 isl_union_map_from_map(access
));
301 return pet_expr_new_unary(0, pet_op_kill
, expr
);
303 isl_map_free(access
);
304 isl_multi_pw_aff_free(index
);
308 /* Construct a unary pet_expr that performs "op" on "arg",
309 * where the result is represented using a type of "type_size" bits
310 * (may be zero if unknown or if the type is not an integer).
312 __isl_give pet_expr
*pet_expr_new_unary(int type_size
, enum pet_op_type op
,
313 __isl_take pet_expr
*arg
)
320 ctx
= pet_expr_get_ctx(arg
);
321 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
322 expr
= pet_expr_set_n_arg(expr
, 1);
327 expr
->type_size
= type_size
;
328 expr
->args
[pet_un_arg
] = arg
;
336 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
337 * where the result is represented using a type of "type_size" bits
338 * (may be zero if unknown or if the type is not an integer).
340 __isl_give pet_expr
*pet_expr_new_binary(int type_size
, enum pet_op_type op
,
341 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
348 ctx
= pet_expr_get_ctx(lhs
);
349 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
350 expr
= pet_expr_set_n_arg(expr
, 2);
355 expr
->type_size
= type_size
;
356 expr
->args
[pet_bin_lhs
] = lhs
;
357 expr
->args
[pet_bin_rhs
] = rhs
;
366 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
368 __isl_give pet_expr
*pet_expr_new_ternary(__isl_take pet_expr
*cond
,
369 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
374 if (!cond
|| !lhs
|| !rhs
)
376 ctx
= pet_expr_get_ctx(cond
);
377 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
378 expr
= pet_expr_set_n_arg(expr
, 3);
382 expr
->op
= pet_op_cond
;
383 expr
->args
[pet_ter_cond
] = cond
;
384 expr
->args
[pet_ter_true
] = lhs
;
385 expr
->args
[pet_ter_false
] = rhs
;
395 /* Construct a call pet_expr that calls function "name" with "n_arg"
396 * arguments. The caller is responsible for filling in the arguments.
398 __isl_give pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
403 expr
= pet_expr_alloc(ctx
, pet_expr_call
);
404 expr
= pet_expr_set_n_arg(expr
, n_arg
);
408 expr
->c
.name
= strdup(name
);
410 return pet_expr_free(expr
);
415 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
417 __isl_give pet_expr
*pet_expr_new_cast(const char *type_name
,
418 __isl_take pet_expr
*arg
)
426 ctx
= pet_expr_get_ctx(arg
);
427 expr
= pet_expr_alloc(ctx
, pet_expr_cast
);
428 expr
= pet_expr_set_n_arg(expr
, 1);
432 expr
->type_name
= strdup(type_name
);
433 if (!expr
->type_name
)
445 /* Construct a pet_expr that represents the double "d".
447 __isl_give pet_expr
*pet_expr_new_double(isl_ctx
*ctx
,
448 double val
, const char *s
)
452 expr
= pet_expr_alloc(ctx
, pet_expr_double
);
457 expr
->d
.s
= strdup(s
);
459 return pet_expr_free(expr
);
464 /* Construct a pet_expr that represents the integer value "v".
466 __isl_give pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
474 ctx
= isl_val_get_ctx(v
);
475 expr
= pet_expr_alloc(ctx
, pet_expr_int
);
487 /* Return an independent duplicate of "expr".
489 * In case of an access expression, make sure the depth of the duplicate is set
490 * before the access relation (if any) is set and after the index expression
493 static __isl_give pet_expr
*pet_expr_dup(__isl_keep pet_expr
*expr
)
497 enum pet_expr_access_type type
;
502 dup
= pet_expr_alloc(expr
->ctx
, expr
->type
);
503 dup
= pet_expr_set_type_size(dup
, expr
->type_size
);
504 dup
= pet_expr_set_n_arg(dup
, expr
->n_arg
);
505 for (i
= 0; i
< expr
->n_arg
; ++i
)
506 dup
= pet_expr_set_arg(dup
, i
, pet_expr_copy(expr
->args
[i
]));
508 switch (expr
->type
) {
509 case pet_expr_access
:
510 if (expr
->acc
.ref_id
)
511 dup
= pet_expr_access_set_ref_id(dup
,
512 isl_id_copy(expr
->acc
.ref_id
));
513 dup
= pet_expr_access_set_index(dup
,
514 isl_multi_pw_aff_copy(expr
->acc
.index
));
515 dup
= pet_expr_access_set_depth(dup
, expr
->acc
.depth
);
516 for (type
= pet_expr_access_begin
;
517 type
< pet_expr_access_end
; ++type
) {
518 if (!expr
->acc
.access
[type
])
520 dup
= pet_expr_access_set_access(dup
, type
,
521 isl_union_map_copy(expr
->acc
.access
[type
]));
523 dup
= pet_expr_access_set_read(dup
, expr
->acc
.read
);
524 dup
= pet_expr_access_set_write(dup
, expr
->acc
.write
);
525 dup
= pet_expr_access_set_kill(dup
, expr
->acc
.kill
);
528 dup
= pet_expr_call_set_name(dup
, expr
->c
.name
);
530 dup
= pet_expr_call_set_summary(dup
,
531 pet_function_summary_copy(expr
->c
.summary
));
534 dup
= pet_expr_cast_set_type_name(dup
, expr
->type_name
);
536 case pet_expr_double
:
537 dup
= pet_expr_double_set(dup
, expr
->d
.val
, expr
->d
.s
);
540 dup
= pet_expr_int_set_val(dup
, isl_val_copy(expr
->i
));
543 dup
= pet_expr_op_set_type(dup
, expr
->op
);
546 dup
= pet_expr_free(dup
);
553 /* Return a pet_expr that is equal to "expr" and that has only
554 * a single reference.
556 * If "expr" itself only has one reference, then clear its hash value
557 * since the returned pet_expr will be modified.
559 __isl_give pet_expr
*pet_expr_cow(__isl_take pet_expr
*expr
)
564 if (expr
->ref
== 1) {
569 return pet_expr_dup(expr
);
572 __isl_null pet_expr
*pet_expr_free(__isl_take pet_expr
*expr
)
574 enum pet_expr_access_type type
;
582 for (i
= 0; i
< expr
->n_arg
; ++i
)
583 pet_expr_free(expr
->args
[i
]);
586 switch (expr
->type
) {
587 case pet_expr_access
:
588 isl_id_free(expr
->acc
.ref_id
);
589 for (type
= pet_expr_access_begin
;
590 type
< pet_expr_access_end
; ++type
)
591 isl_union_map_free(expr
->acc
.access
[type
]);
592 isl_multi_pw_aff_free(expr
->acc
.index
);
596 pet_function_summary_free(expr
->c
.summary
);
599 free(expr
->type_name
);
601 case pet_expr_double
:
605 isl_val_free(expr
->i
);
612 isl_ctx_deref(expr
->ctx
);
617 /* Return an additional reference to "expr".
619 __isl_give pet_expr
*pet_expr_copy(__isl_keep pet_expr
*expr
)
628 /* Return the isl_ctx in which "expr" was created.
630 isl_ctx
*pet_expr_get_ctx(__isl_keep pet_expr
*expr
)
632 return expr
? expr
->ctx
: NULL
;
635 /* Return the type of "expr".
637 enum pet_expr_type
pet_expr_get_type(__isl_keep pet_expr
*expr
)
640 return pet_expr_error
;
644 /* Return the number of arguments of "expr".
646 int pet_expr_get_n_arg(__isl_keep pet_expr
*expr
)
654 /* Set the number of arguments of "expr" to "n".
656 * If "expr" originally had more arguments, then remove the extra arguments.
657 * If "expr" originally had fewer arguments, then create space for
658 * the extra arguments ans initialize them to NULL.
660 __isl_give pet_expr
*pet_expr_set_n_arg(__isl_take pet_expr
*expr
, int n
)
667 if (expr
->n_arg
== n
)
669 expr
= pet_expr_cow(expr
);
673 if (n
< expr
->n_arg
) {
674 for (i
= n
; i
< expr
->n_arg
; ++i
)
675 pet_expr_free(expr
->args
[i
]);
680 args
= isl_realloc_array(expr
->ctx
, expr
->args
, pet_expr
*, n
);
682 return pet_expr_free(expr
);
684 for (i
= expr
->n_arg
; i
< n
; ++i
)
685 expr
->args
[i
] = NULL
;
691 /* Return the argument of "expr" at position "pos".
693 __isl_give pet_expr
*pet_expr_get_arg(__isl_keep pet_expr
*expr
, int pos
)
697 if (pos
< 0 || pos
>= expr
->n_arg
)
698 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
699 "position out of bounds", return NULL
);
701 return pet_expr_copy(expr
->args
[pos
]);
704 /* Replace "expr" by its argument at position "pos".
706 __isl_give pet_expr
*pet_expr_arg(__isl_take pet_expr
*expr
, int pos
)
710 arg
= pet_expr_get_arg(expr
, pos
);
716 /* Replace the argument of "expr" at position "pos" by "arg".
718 __isl_give pet_expr
*pet_expr_set_arg(__isl_take pet_expr
*expr
, int pos
,
719 __isl_take pet_expr
*arg
)
723 if (pos
< 0 || pos
>= expr
->n_arg
)
724 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
725 "position out of bounds", goto error
);
726 if (expr
->args
[pos
] == arg
) {
731 expr
= pet_expr_cow(expr
);
735 pet_expr_free(expr
->args
[pos
]);
736 expr
->args
[pos
] = arg
;
745 /* Does "expr" perform a comparison operation?
747 int pet_expr_is_comparison(__isl_keep pet_expr
*expr
)
751 if (expr
->type
!= pet_expr_op
)
766 /* Does "expr" perform a boolean operation?
768 int pet_expr_is_boolean(__isl_keep pet_expr
*expr
)
772 if (expr
->type
!= pet_expr_op
)
784 /* Is "expr" an address-of operation?
786 int pet_expr_is_address_of(__isl_keep pet_expr
*expr
)
790 if (expr
->type
!= pet_expr_op
)
792 return expr
->op
== pet_op_address_of
;
795 /* Is "expr" an assume statement?
797 int pet_expr_is_assume(__isl_keep pet_expr
*expr
)
801 if (expr
->type
!= pet_expr_op
)
803 return expr
->op
== pet_op_assume
;
806 /* Does "expr" perform a min operation?
808 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
812 if (expr
->type
!= pet_expr_call
)
814 if (expr
->n_arg
!= 2)
816 if (strcmp(expr
->c
.name
, "min") != 0)
821 /* Does "expr" perform a max operation?
823 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
827 if (expr
->type
!= pet_expr_call
)
829 if (expr
->n_arg
!= 2)
831 if (strcmp(expr
->c
.name
, "max") != 0)
836 /* Does "expr" represent an access to an unnamed space, i.e.,
837 * does it represent an affine expression?
839 isl_bool
pet_expr_is_affine(__isl_keep pet_expr
*expr
)
844 return isl_bool_error
;
845 if (expr
->type
!= pet_expr_access
)
846 return isl_bool_false
;
848 has_id
= isl_multi_pw_aff_has_tuple_id(expr
->acc
.index
, isl_dim_out
);
850 return isl_bool_error
;
855 /* Given that "expr" represents an affine expression, i.e., that
856 * it is an access to an unnamed (1D) space, return this affine expression.
858 __isl_give isl_pw_aff
*pet_expr_get_affine(__isl_keep pet_expr
*expr
)
862 isl_multi_pw_aff
*mpa
;
864 is_affine
= pet_expr_is_affine(expr
);
868 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
869 "not an affine expression", return NULL
);
871 mpa
= pet_expr_access_get_index(expr
);
872 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
873 isl_multi_pw_aff_free(mpa
);
877 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
878 * not part of any struct?
880 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
884 if (expr
->type
!= pet_expr_access
)
886 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
))
889 return expr
->acc
.depth
== 0;
892 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
895 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
896 __isl_keep isl_multi_pw_aff
*mpa2
)
900 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
901 if (equal
< 0 || equal
)
903 mpa2
= isl_multi_pw_aff_copy(mpa2
);
904 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
905 isl_multi_pw_aff_get_space(mpa1
));
906 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
907 isl_multi_pw_aff_free(mpa2
);
912 /* Construct an access relation from the index expression and
913 * the array depth of the access expression "expr".
915 * If the number of indices is smaller than the depth of the array,
916 * then we assume that all elements of the remaining dimensions
919 static __isl_give isl_union_map
*construct_access_relation(
920 __isl_keep pet_expr
*expr
)
929 access
= isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr
));
933 dim
= isl_map_dim(access
, isl_dim_out
);
934 if (dim
> expr
->acc
.depth
)
935 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
936 "number of indices greater than depth",
937 access
= isl_map_free(access
));
939 if (dim
!= expr
->acc
.depth
)
940 access
= extend_range(access
, expr
->acc
.depth
- dim
);
942 return isl_union_map_from_map(access
);
945 /* Ensure that all relevant access relations are explicitly
946 * available in "expr".
948 * If "expr" does not already have the relevant access relations, then create
949 * them based on the index expression and the array depth.
951 * We do not cow since adding an explicit access relation
952 * does not change the meaning of the expression.
953 * However, the explicit access relations may modify the hash value,
954 * so the cached value is reset.
956 static __isl_give pet_expr
*introduce_access_relations(
957 __isl_take pet_expr
*expr
)
959 enum pet_expr_access_type type
;
960 isl_union_map
*access
;
962 int kill
, read
, write
;
966 if (has_relevant_access_relations(expr
))
969 access
= construct_access_relation(expr
);
971 return pet_expr_free(expr
);
974 kill
= expr
->acc
.kill
;
975 read
= expr
->acc
.read
;
976 write
= expr
->acc
.write
;
977 if (kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
978 expr
->acc
.access
[pet_expr_access_fake_killed
] =
979 isl_union_map_copy(access
);
980 if (read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
981 expr
->acc
.access
[pet_expr_access_may_read
] =
982 isl_union_map_copy(access
);
983 if (write
&& !expr
->acc
.access
[pet_expr_access_may_write
])
984 expr
->acc
.access
[pet_expr_access_may_write
] =
985 isl_union_map_copy(access
);
986 if (write
&& !expr
->acc
.access
[pet_expr_access_must_write
])
987 expr
->acc
.access
[pet_expr_access_must_write
] =
988 isl_union_map_copy(access
);
990 isl_union_map_free(access
);
992 if (!has_relevant_access_relations(expr
))
993 return pet_expr_free(expr
);
998 /* Return a hash value that digests "expr".
999 * If a hash value was computed already, then return that value.
1000 * Otherwise, compute the hash value and store a copy in expr->hash.
1002 uint32_t pet_expr_get_hash(__isl_keep pet_expr
*expr
)
1005 enum pet_expr_access_type type
;
1006 uint32_t hash
, hash_f
;
1013 hash
= isl_hash_init();
1014 isl_hash_byte(hash
, expr
->type
& 0xFF);
1015 isl_hash_byte(hash
, expr
->n_arg
& 0xFF);
1016 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1018 hash_i
= pet_expr_get_hash(expr
->args
[i
]);
1019 isl_hash_hash(hash
, hash_i
);
1021 switch (expr
->type
) {
1022 case pet_expr_error
:
1024 case pet_expr_double
:
1025 hash
= isl_hash_string(hash
, expr
->d
.s
);
1028 hash_f
= isl_val_get_hash(expr
->i
);
1029 isl_hash_hash(hash
, hash_f
);
1031 case pet_expr_access
:
1032 isl_hash_byte(hash
, expr
->acc
.read
& 0xFF);
1033 isl_hash_byte(hash
, expr
->acc
.write
& 0xFF);
1034 isl_hash_byte(hash
, expr
->acc
.kill
& 0xFF);
1035 hash_f
= isl_id_get_hash(expr
->acc
.ref_id
);
1036 isl_hash_hash(hash
, hash_f
);
1037 hash_f
= isl_multi_pw_aff_get_hash(expr
->acc
.index
);
1038 isl_hash_hash(hash
, hash_f
);
1039 isl_hash_byte(hash
, expr
->acc
.depth
& 0xFF);
1040 for (type
= pet_expr_access_begin
;
1041 type
< pet_expr_access_end
; ++type
) {
1042 hash_f
= isl_union_map_get_hash(expr
->acc
.access
[type
]);
1043 isl_hash_hash(hash
, hash_f
);
1047 isl_hash_byte(hash
, expr
->op
& 0xFF);
1050 hash
= isl_hash_string(hash
, expr
->c
.name
);
1053 hash
= isl_hash_string(hash
, expr
->type_name
);
1060 /* Return 1 if the two pet_exprs are equivalent.
1062 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
1065 enum pet_expr_access_type type
;
1067 if (!expr1
|| !expr2
)
1070 if (expr1
->type
!= expr2
->type
)
1072 if (expr1
->n_arg
!= expr2
->n_arg
)
1074 for (i
= 0; i
< expr1
->n_arg
; ++i
)
1075 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
1077 switch (expr1
->type
) {
1078 case pet_expr_error
:
1080 case pet_expr_double
:
1081 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
1083 if (expr1
->d
.val
!= expr2
->d
.val
)
1087 if (!isl_val_eq(expr1
->i
, expr2
->i
))
1090 case pet_expr_access
:
1091 if (expr1
->acc
.read
!= expr2
->acc
.read
)
1093 if (expr1
->acc
.write
!= expr2
->acc
.write
)
1095 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
1097 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
1099 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
1101 if (!multi_pw_aff_is_equal(expr1
->acc
.index
, expr2
->acc
.index
))
1103 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
1105 if (has_relevant_access_relations(expr1
) !=
1106 has_relevant_access_relations(expr2
)) {
1108 expr1
= pet_expr_copy(expr1
);
1109 expr2
= pet_expr_copy(expr2
);
1110 expr1
= introduce_access_relations(expr1
);
1111 expr2
= introduce_access_relations(expr2
);
1112 equal
= pet_expr_is_equal(expr1
, expr2
);
1113 pet_expr_free(expr1
);
1114 pet_expr_free(expr2
);
1117 for (type
= pet_expr_access_begin
;
1118 type
< pet_expr_access_end
; ++type
) {
1119 if (!expr1
->acc
.access
[type
] !=
1120 !expr2
->acc
.access
[type
])
1122 if (!expr1
->acc
.access
[type
])
1124 if (!isl_union_map_is_equal(expr1
->acc
.access
[type
],
1125 expr2
->acc
.access
[type
]))
1130 if (expr1
->op
!= expr2
->op
)
1134 if (strcmp(expr1
->c
.name
, expr2
->c
.name
))
1138 if (strcmp(expr1
->type_name
, expr2
->type_name
))
1146 /* Do "expr1" and "expr2" represent two accesses to the same array
1147 * that are also of the same type? That is, can these two accesses
1148 * be replaced by a single access?
1150 isl_bool
pet_expr_is_same_access(__isl_keep pet_expr
*expr1
,
1151 __isl_keep pet_expr
*expr2
)
1153 isl_space
*space1
, *space2
;
1156 if (!expr1
|| !expr2
)
1157 return isl_bool_error
;
1158 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1159 return isl_bool_false
;
1160 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1161 return isl_bool_false
;
1162 if (expr1
->acc
.read
!= expr2
->acc
.read
)
1163 return isl_bool_false
;
1164 if (expr1
->acc
.write
!= expr2
->acc
.write
)
1165 return isl_bool_false
;
1166 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
1167 return isl_bool_false
;
1168 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
1169 return isl_bool_false
;
1171 space1
= isl_multi_pw_aff_get_space(expr1
->acc
.index
);
1172 space2
= isl_multi_pw_aff_get_space(expr2
->acc
.index
);
1173 same
= isl_space_tuple_is_equal(space1
, isl_dim_out
,
1174 space2
, isl_dim_out
);
1175 if (same
>= 0 && same
)
1176 same
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
1177 space2
, isl_dim_in
);
1178 isl_space_free(space1
);
1179 isl_space_free(space2
);
1184 /* Does the access expression "expr" read the accessed elements?
1186 isl_bool
pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
1189 return isl_bool_error
;
1190 if (expr
->type
!= pet_expr_access
)
1191 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1192 "not an access expression", return isl_bool_error
);
1194 return expr
->acc
.read
;
1197 /* Does the access expression "expr" write to the accessed elements?
1199 isl_bool
pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
1202 return isl_bool_error
;
1203 if (expr
->type
!= pet_expr_access
)
1204 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1205 "not an access expression", return isl_bool_error
);
1207 return expr
->acc
.write
;
1210 /* Does the access expression "expr" kill the accessed elements?
1212 isl_bool
pet_expr_access_is_kill(__isl_keep pet_expr
*expr
)
1215 return isl_bool_error
;
1216 if (expr
->type
!= pet_expr_access
)
1217 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1218 "not an access expression", return isl_bool_error
);
1220 return expr
->acc
.kill
;
1223 /* Return the identifier of the array accessed by "expr".
1225 * If "expr" represents a member access, then return the identifier
1226 * of the outer structure array.
1228 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
1232 if (expr
->type
!= pet_expr_access
)
1233 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1234 "not an access expression", return NULL
);
1236 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
)) {
1240 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1241 space
= isl_space_range(space
);
1242 while (space
&& isl_space_is_wrapping(space
))
1243 space
= isl_space_domain(isl_space_unwrap(space
));
1244 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
1245 isl_space_free(space
);
1250 return isl_multi_pw_aff_get_tuple_id(expr
->acc
.index
, isl_dim_out
);
1253 /* Return the parameter space of "expr".
1255 __isl_give isl_space
*pet_expr_access_get_parameter_space(
1256 __isl_keep pet_expr
*expr
)
1262 if (expr
->type
!= pet_expr_access
)
1263 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1264 "not an access expression", return NULL
);
1266 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1267 space
= isl_space_params(space
);
1272 /* Return the domain space of "expr", including the arguments (if any).
1274 __isl_give isl_space
*pet_expr_access_get_augmented_domain_space(
1275 __isl_keep pet_expr
*expr
)
1281 if (expr
->type
!= pet_expr_access
)
1282 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1283 "not an access expression", return NULL
);
1285 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1286 space
= isl_space_domain(space
);
1291 /* Return the domain space of "expr", without the arguments (if any).
1293 __isl_give isl_space
*pet_expr_access_get_domain_space(
1294 __isl_keep pet_expr
*expr
)
1298 space
= pet_expr_access_get_augmented_domain_space(expr
);
1299 if (isl_space_is_wrapping(space
))
1300 space
= isl_space_domain(isl_space_unwrap(space
));
1305 /* Return the space of the data accessed by "expr".
1307 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
1313 if (expr
->type
!= pet_expr_access
)
1314 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1315 "not an access expression", return NULL
);
1317 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1318 space
= isl_space_range(space
);
1323 /* Modify all subexpressions of "expr" by calling "fn" on them.
1324 * The subexpressions are traversed in depth first preorder.
1326 __isl_give pet_expr
*pet_expr_map_top_down(__isl_take pet_expr
*expr
,
1327 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1335 expr
= fn(expr
, user
);
1337 n
= pet_expr_get_n_arg(expr
);
1338 for (i
= 0; i
< n
; ++i
) {
1339 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1340 arg
= pet_expr_map_top_down(arg
, fn
, user
);
1341 expr
= pet_expr_set_arg(expr
, i
, arg
);
1347 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1349 static __isl_give pet_expr
*pet_expr_map_expr_of_type(__isl_take pet_expr
*expr
,
1350 enum pet_expr_type type
,
1351 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1356 n
= pet_expr_get_n_arg(expr
);
1357 for (i
= 0; i
< n
; ++i
) {
1358 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1359 arg
= pet_expr_map_expr_of_type(arg
, type
, fn
, user
);
1360 expr
= pet_expr_set_arg(expr
, i
, arg
);
1366 if (expr
->type
== type
)
1367 expr
= fn(expr
, user
);
1372 /* Modify all expressions of type pet_expr_access in "expr"
1373 * by calling "fn" on them.
1375 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
1376 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1379 return pet_expr_map_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1382 /* Modify all expressions of type pet_expr_call in "expr"
1383 * by calling "fn" on them.
1385 __isl_give pet_expr
*pet_expr_map_call(__isl_take pet_expr
*expr
,
1386 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1389 return pet_expr_map_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1392 /* Modify all expressions of type pet_expr_op in "expr"
1393 * by calling "fn" on them.
1395 __isl_give pet_expr
*pet_expr_map_op(__isl_take pet_expr
*expr
,
1396 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1399 return pet_expr_map_expr_of_type(expr
, pet_expr_op
, fn
, user
);
1402 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1404 * Return -1 on error (where fn returning a negative value is treated as
1406 * Otherwise return 0.
1408 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
1409 enum pet_expr_type type
,
1410 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1417 for (i
= 0; i
< expr
->n_arg
; ++i
)
1418 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1419 type
, fn
, user
) < 0)
1422 if (expr
->type
== type
)
1423 return fn(expr
, user
);
1428 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1430 * Return -1 on error (where fn returning a negative value is treated as
1432 * Otherwise return 0.
1434 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1435 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1437 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1440 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1442 * Return -1 on error (where fn returning a negative value is treated as
1444 * Otherwise return 0.
1446 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1447 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1449 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1452 /* Internal data structure for pet_expr_writes.
1453 * "id" is the identifier that we are looking for.
1454 * "found" is set if we have found the identifier being written to.
1456 struct pet_expr_writes_data
{
1461 /* Given an access expression, check if it writes to data->id.
1462 * If so, set data->found and abort the search.
1464 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1466 struct pet_expr_writes_data
*data
= user
;
1469 if (!expr
->acc
.write
)
1471 if (pet_expr_is_affine(expr
))
1474 write_id
= pet_expr_access_get_id(expr
);
1475 isl_id_free(write_id
);
1480 if (write_id
!= data
->id
)
1487 /* Does expression "expr" write to "id"?
1489 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1491 struct pet_expr_writes_data data
;
1495 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1502 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1503 * index expression and access relations of "expr" (if any)
1504 * to dimensions of "dst_type" at "dst_pos".
1506 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1507 enum isl_dim_type dst_type
, unsigned dst_pos
,
1508 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1510 enum pet_expr_access_type type
;
1512 expr
= pet_expr_cow(expr
);
1515 if (expr
->type
!= pet_expr_access
)
1516 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1517 "not an access pet_expr", return pet_expr_free(expr
));
1519 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1520 if (!expr
->acc
.access
[type
])
1522 expr
->acc
.access
[type
] =
1523 pet_union_map_move_dims(expr
->acc
.access
[type
],
1524 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1525 if (!expr
->acc
.access
[type
])
1528 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1529 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1530 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1531 return pet_expr_free(expr
);
1536 /* Replace the index expression and access relations (if any) of "expr"
1537 * by their preimages under the function represented by "ma".
1539 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1540 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1542 enum pet_expr_access_type type
;
1544 expr
= pet_expr_cow(expr
);
1547 if (expr
->type
!= pet_expr_access
)
1548 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1549 "not an access pet_expr", goto error
);
1551 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1552 if (!expr
->acc
.access
[type
])
1554 expr
->acc
.access
[type
] =
1555 isl_union_map_preimage_domain_multi_aff(
1556 expr
->acc
.access
[type
], isl_multi_aff_copy(ma
));
1557 if (!expr
->acc
.access
[type
])
1560 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1562 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1563 return pet_expr_free(expr
);
1567 isl_multi_aff_free(ma
);
1568 pet_expr_free(expr
);
1572 /* Replace the index expression and access relations (if any) of "expr"
1573 * by their preimages under the function represented by "mpa".
1575 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1576 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1578 enum pet_expr_access_type type
;
1580 expr
= pet_expr_cow(expr
);
1583 if (expr
->type
!= pet_expr_access
)
1584 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1585 "not an access pet_expr", goto error
);
1587 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1588 if (!expr
->acc
.access
[type
])
1590 expr
->acc
.access
[type
] =
1591 isl_union_map_preimage_domain_multi_pw_aff(
1592 expr
->acc
.access
[type
], isl_multi_pw_aff_copy(mpa
));
1593 if (!expr
->acc
.access
[type
])
1596 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1597 expr
->acc
.index
, mpa
);
1598 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1599 return pet_expr_free(expr
);
1603 isl_multi_pw_aff_free(mpa
);
1604 pet_expr_free(expr
);
1608 /* Return the index expression of access expression "expr".
1610 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1611 __isl_keep pet_expr
*expr
)
1615 if (expr
->type
!= pet_expr_access
)
1616 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1617 "not an access expression", return NULL
);
1619 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1622 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1624 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1627 enum pet_expr_access_type type
;
1629 expr
= pet_expr_cow(expr
);
1632 if (expr
->type
!= pet_expr_access
)
1633 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1634 "not an access expression", return pet_expr_free(expr
));
1636 if (!pet_expr_access_has_any_access_relation(expr
))
1639 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1640 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1641 if (!expr
->acc
.access
[type
])
1643 space
= isl_space_align_params(space
,
1644 isl_union_map_get_space(expr
->acc
.access
[type
]));
1646 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1647 isl_space_copy(space
));
1648 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1649 if (!expr
->acc
.access
[type
])
1651 expr
->acc
.access
[type
] =
1652 isl_union_map_align_params(expr
->acc
.access
[type
],
1653 isl_space_copy(space
));
1654 if (!expr
->acc
.access
[type
])
1657 isl_space_free(space
);
1658 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1659 return pet_expr_free(expr
);
1664 /* Are "expr1" and "expr2" both array accesses such that
1665 * the access relation of "expr1" is a subset of that of "expr2"?
1666 * Only take into account the first "n_arg" arguments.
1668 * This function is tailored for use by mark_self_dependences in nest.c.
1669 * In particular, the input expressions may have more than "n_arg"
1670 * elements in their arguments arrays, while only the first "n_arg"
1671 * elements are referenced from the access relations.
1673 int pet_expr_is_sub_access(__isl_keep pet_expr
*expr1
,
1674 __isl_keep pet_expr
*expr2
, int n_arg
)
1680 if (!expr1
|| !expr2
)
1682 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1684 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1686 if (pet_expr_is_affine(expr1
))
1688 if (pet_expr_is_affine(expr2
))
1690 n1
= pet_expr_get_n_arg(expr1
);
1693 n2
= pet_expr_get_n_arg(expr2
);
1698 for (i
= 0; i
< n1
; ++i
) {
1700 equal
= pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]);
1701 if (equal
< 0 || !equal
)
1704 id1
= pet_expr_access_get_id(expr1
);
1705 id2
= pet_expr_access_get_id(expr2
);
1713 expr1
= pet_expr_copy(expr1
);
1714 expr2
= pet_expr_copy(expr2
);
1715 expr1
= introduce_access_relations(expr1
);
1716 expr2
= introduce_access_relations(expr2
);
1717 if (!expr1
|| !expr2
)
1720 is_subset
= isl_union_map_is_subset(
1721 expr1
->acc
.access
[pet_expr_access_may_read
],
1722 expr2
->acc
.access
[pet_expr_access_may_read
]);
1724 pet_expr_free(expr1
);
1725 pet_expr_free(expr2
);
1729 pet_expr_free(expr1
);
1730 pet_expr_free(expr2
);
1734 /* Given a set in the iteration space "domain", extend it to live in the space
1735 * of the domain of access relations.
1737 * That, is the number of arguments "n" is 0, then simply return domain.
1738 * Otherwise, return [domain -> [a_1,...,a_n]].
1740 static __isl_give isl_set
*add_arguments(__isl_take isl_set
*domain
, int n
)
1747 map
= isl_map_from_domain(domain
);
1748 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
1749 return isl_map_wrap(map
);
1752 /* Add extra conditions to the domains of all access relations in "expr",
1753 * introducing access relations if they are not already present.
1755 * The conditions are not added to the index expression. Instead, they
1756 * are used to try and simplify the index expression.
1758 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1759 __isl_take isl_set
*cond
)
1762 isl_union_set
*uset
;
1763 enum pet_expr_access_type type
;
1765 expr
= pet_expr_cow(expr
);
1769 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1770 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1771 isl_set_copy(cond
));
1776 if (expr
->type
!= pet_expr_access
) {
1781 expr
= introduce_access_relations(expr
);
1785 cond
= add_arguments(cond
, expr
->n_arg
);
1786 uset
= isl_union_set_from_set(isl_set_copy(cond
));
1787 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1788 if (!expr
->acc
.access
[type
])
1790 expr
->acc
.access
[type
] =
1791 isl_union_map_intersect_domain(expr
->acc
.access
[type
],
1792 isl_union_set_copy(uset
));
1793 if (!expr
->acc
.access
[type
])
1796 isl_union_set_free(uset
);
1797 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, cond
);
1798 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1799 return pet_expr_free(expr
);
1804 return pet_expr_free(expr
);
1807 /* Modify the access relations (if any) and index expression
1808 * of the given access expression
1809 * based on the given iteration space transformation.
1810 * In particular, precompose the access relation and index expression
1811 * with the update function.
1813 * If the access has any arguments then the domain of the access relation
1814 * is a wrapped mapping from the iteration space to the space of
1815 * argument values. We only need to change the domain of this wrapped
1816 * mapping, so we extend the input transformation with an identity mapping
1817 * on the space of argument values.
1819 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1820 __isl_keep isl_multi_pw_aff
*update
)
1822 enum pet_expr_access_type type
;
1824 expr
= pet_expr_cow(expr
);
1827 if (expr
->type
!= pet_expr_access
)
1828 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1829 "not an access expression", return pet_expr_free(expr
));
1831 update
= isl_multi_pw_aff_copy(update
);
1833 if (expr
->n_arg
> 0) {
1835 isl_multi_pw_aff
*id
;
1837 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1838 space
= isl_space_domain(space
);
1839 space
= isl_space_unwrap(space
);
1840 space
= isl_space_range(space
);
1841 space
= isl_space_map_from_set(space
);
1842 id
= isl_multi_pw_aff_identity(space
);
1843 update
= isl_multi_pw_aff_product(update
, id
);
1846 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1847 if (!expr
->acc
.access
[type
])
1849 expr
->acc
.access
[type
] =
1850 isl_union_map_preimage_domain_multi_pw_aff(
1851 expr
->acc
.access
[type
],
1852 isl_multi_pw_aff_copy(update
));
1853 if (!expr
->acc
.access
[type
])
1856 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1857 expr
->acc
.index
, update
);
1858 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1859 return pet_expr_free(expr
);
1864 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1866 isl_multi_pw_aff
*update
= user
;
1868 return pet_expr_access_update_domain(expr
, update
);
1871 /* Modify all access relations in "expr" by precomposing them with
1872 * the given iteration space transformation.
1874 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1875 __isl_take isl_multi_pw_aff
*update
)
1877 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1878 isl_multi_pw_aff_free(update
);
1882 /* Given an expression with accesses that have a 0D anonymous domain,
1883 * replace those domains by "space".
1885 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1886 __isl_take isl_space
*space
)
1888 isl_multi_pw_aff
*mpa
;
1890 space
= isl_space_from_domain(space
);
1891 mpa
= isl_multi_pw_aff_zero(space
);
1892 return pet_expr_update_domain(expr
, mpa
);
1895 /* Add all parameters in "space" to the access relations (if any)
1896 * and index expression of "expr".
1898 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1900 isl_space
*space
= user
;
1901 enum pet_expr_access_type type
;
1903 expr
= pet_expr_cow(expr
);
1906 if (expr
->type
!= pet_expr_access
)
1907 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1908 "not an access expression", return pet_expr_free(expr
));
1910 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1911 if (!expr
->acc
.access
[type
])
1913 expr
->acc
.access
[type
] =
1914 isl_union_map_align_params(expr
->acc
.access
[type
],
1915 isl_space_copy(space
));
1916 if (!expr
->acc
.access
[type
])
1919 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1920 isl_space_copy(space
));
1921 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1922 return pet_expr_free(expr
);
1927 /* Add all parameters in "space" to all access relations and index expressions
1930 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1931 __isl_take isl_space
*space
)
1933 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1934 isl_space_free(space
);
1938 /* Insert an argument expression corresponding to "test" in front
1939 * of the list of arguments described by *n_arg and *args.
1941 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1942 __isl_keep isl_multi_pw_aff
*test
)
1945 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1948 return pet_expr_free(expr
);
1949 expr
= pet_expr_cow(expr
);
1954 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1956 return pet_expr_free(expr
);
1959 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1961 return pet_expr_free(expr
);
1962 for (i
= 0; i
< expr
->n_arg
; ++i
)
1963 ext
[1 + i
] = expr
->args
[i
];
1968 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1970 return pet_expr_free(expr
);
1975 /* Make the expression "expr" depend on the value of "test"
1976 * being equal to "satisfied".
1978 * If "test" is an affine expression, we simply add the conditions
1979 * on the expression having the value "satisfied" to all access relations
1980 * (introducing access relations if they are missing) and index expressions.
1982 * Otherwise, we add a filter to "expr" (which is then assumed to be
1983 * an access expression) corresponding to "test" being equal to "satisfied".
1985 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1986 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1991 isl_pw_multi_aff
*pma
;
1992 enum pet_expr_access_type type
;
1994 expr
= pet_expr_cow(expr
);
1998 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
2002 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
2003 isl_multi_pw_aff_free(test
);
2005 cond
= isl_pw_aff_non_zero_set(pa
);
2007 cond
= isl_pw_aff_zero_set(pa
);
2008 return pet_expr_restrict(expr
, cond
);
2011 ctx
= isl_multi_pw_aff_get_ctx(test
);
2012 if (expr
->type
!= pet_expr_access
)
2013 isl_die(ctx
, isl_error_invalid
,
2014 "can only filter access expressions", goto error
);
2016 expr
= introduce_access_relations(expr
);
2020 space
= isl_space_domain(isl_multi_pw_aff_get_space(expr
->acc
.index
));
2021 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
2022 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
2024 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2025 if (!expr
->acc
.access
[type
])
2027 expr
->acc
.access
[type
] =
2028 isl_union_map_preimage_domain_pw_multi_aff(
2029 expr
->acc
.access
[type
],
2030 isl_pw_multi_aff_copy(pma
));
2031 if (!expr
->acc
.access
[type
])
2034 pma
= isl_pw_multi_aff_gist(pma
,
2035 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
2036 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
2037 expr
->acc
.index
, pma
);
2038 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2041 expr
= insert_access_arg(expr
, test
);
2043 isl_multi_pw_aff_free(test
);
2046 isl_multi_pw_aff_free(test
);
2047 return pet_expr_free(expr
);
2050 /* Add a reference identifier to access expression "expr".
2051 * "user" points to an integer that contains the sequence number
2052 * of the next reference.
2054 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
2061 expr
= pet_expr_cow(expr
);
2064 if (expr
->type
!= pet_expr_access
)
2065 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2066 "not an access expression", return pet_expr_free(expr
));
2068 ctx
= pet_expr_get_ctx(expr
);
2069 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
2070 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
2071 if (!expr
->acc
.ref_id
)
2072 return pet_expr_free(expr
);
2077 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
2079 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
2082 /* Reset the user pointer on all parameter and tuple ids in
2083 * the access relations (if any) and the index expression
2084 * of the access expression "expr".
2086 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
2089 enum pet_expr_access_type type
;
2091 expr
= pet_expr_cow(expr
);
2094 if (expr
->type
!= pet_expr_access
)
2095 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2096 "not an access expression", return pet_expr_free(expr
));
2098 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2099 if (!expr
->acc
.access
[type
])
2101 expr
->acc
.access
[type
] =
2102 isl_union_map_reset_user(expr
->acc
.access
[type
]);
2103 if (!expr
->acc
.access
[type
])
2106 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
2107 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2108 return pet_expr_free(expr
);
2113 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
2115 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
2118 /* Data used in access_gist() callback.
2120 struct pet_access_gist_data
{
2122 isl_union_map
*value_bounds
;
2125 /* Given an expression "expr" of type pet_expr_access, compute
2126 * the gist of the associated access relations (if any) and index expression
2127 * with respect to data->domain and the bounds on the values of the arguments
2128 * of the expression.
2130 * The arguments of "expr" have been gisted right before "expr" itself
2131 * is gisted. The gisted arguments may have become equal where before
2132 * they may not have been (obviously) equal. We therefore take
2133 * the opportunity to remove duplicate arguments here.
2135 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
2137 struct pet_access_gist_data
*data
= user
;
2139 isl_union_set
*uset
;
2140 enum pet_expr_access_type type
;
2142 expr
= pet_expr_remove_duplicate_args(expr
);
2143 expr
= pet_expr_cow(expr
);
2146 if (expr
->type
!= pet_expr_access
)
2147 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2148 "not an access expression", return pet_expr_free(expr
));
2150 domain
= isl_set_copy(data
->domain
);
2151 if (expr
->n_arg
> 0)
2152 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
2153 data
->value_bounds
);
2155 uset
= isl_union_set_from_set(isl_set_copy(domain
));
2156 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
2157 if (!expr
->acc
.access
[type
])
2159 expr
->acc
.access
[type
] =
2160 isl_union_map_gist_domain(expr
->acc
.access
[type
],
2161 isl_union_set_copy(uset
));
2162 if (!expr
->acc
.access
[type
])
2165 isl_union_set_free(uset
);
2166 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
2167 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
2168 return pet_expr_free(expr
);
2173 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
2174 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2176 struct pet_access_gist_data data
= { context
, value_bounds
};
2178 return pet_expr_map_access(expr
, &access_gist
, &data
);
2181 /* Mark "expr" as a read dependening on "read".
2183 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
2187 return pet_expr_free(expr
);
2188 if (expr
->type
!= pet_expr_access
)
2189 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2190 "not an access expression", return pet_expr_free(expr
));
2191 if (expr
->acc
.read
== read
)
2193 expr
= pet_expr_cow(expr
);
2196 expr
->acc
.read
= read
;
2201 /* Mark "expr" as a write dependening on "write".
2203 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
2207 return pet_expr_free(expr
);
2208 if (expr
->type
!= pet_expr_access
)
2209 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2210 "not an access expression", return pet_expr_free(expr
));
2211 if (expr
->acc
.write
== write
)
2213 expr
= pet_expr_cow(expr
);
2216 expr
->acc
.write
= write
;
2221 /* Mark "expr" as a kill dependening on "kill".
2223 __isl_give pet_expr
*pet_expr_access_set_kill(__isl_take pet_expr
*expr
,
2227 return pet_expr_free(expr
);
2228 if (expr
->type
!= pet_expr_access
)
2229 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2230 "not an access expression", return pet_expr_free(expr
));
2231 if (expr
->acc
.kill
== kill
)
2233 expr
= pet_expr_cow(expr
);
2236 expr
->acc
.kill
= kill
;
2241 /* Map the access type "type" to the corresponding location
2242 * in the access array.
2243 * In particular, the access relation of type pet_expr_access_killed is
2244 * stored in the element at position pet_expr_access_fake_killed.
2246 static enum pet_expr_access_type
internalize_type(
2247 enum pet_expr_access_type type
)
2249 if (type
== pet_expr_access_killed
)
2250 return pet_expr_access_fake_killed
;
2254 /* Replace the access relation of the given "type" of "expr" by "access".
2255 * If the access relation is non-empty and the type is a read or a write,
2256 * then also mark the access expression itself as a read or a write.
2258 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
2259 enum pet_expr_access_type type
, __isl_take isl_union_map
*access
)
2263 expr
= pet_expr_cow(expr
);
2264 if (!expr
|| !access
)
2266 if (expr
->type
!= pet_expr_access
)
2267 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2268 "not an access expression", goto error
);
2269 type
= internalize_type(type
);
2270 isl_union_map_free(expr
->acc
.access
[type
]);
2271 expr
->acc
.access
[type
] = access
;
2276 empty
= isl_union_map_is_empty(access
);
2278 return pet_expr_free(expr
);
2282 if (type
== pet_expr_access_may_read
)
2283 expr
= pet_expr_access_set_read(expr
, 1);
2285 expr
= pet_expr_access_set_write(expr
, 1);
2289 isl_union_map_free(access
);
2290 pet_expr_free(expr
);
2294 /* Replace the index expression of "expr" by "index" and
2295 * set the array depth accordingly.
2297 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
2298 __isl_take isl_multi_pw_aff
*index
)
2300 expr
= pet_expr_cow(expr
);
2301 if (!expr
|| !index
)
2303 if (expr
->type
!= pet_expr_access
)
2304 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2305 "not an access expression", goto error
);
2306 isl_multi_pw_aff_free(expr
->acc
.index
);
2307 expr
->acc
.index
= index
;
2308 expr
->acc
.depth
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
2312 isl_multi_pw_aff_free(index
);
2313 pet_expr_free(expr
);
2317 /* Return the reference identifier of access expression "expr".
2319 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
2323 if (expr
->type
!= pet_expr_access
)
2324 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2325 "not an access expression", return NULL
);
2327 return isl_id_copy(expr
->acc
.ref_id
);
2330 /* Replace the reference identifier of access expression "expr" by "ref_id".
2332 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
2333 __isl_take isl_id
*ref_id
)
2335 expr
= pet_expr_cow(expr
);
2336 if (!expr
|| !ref_id
)
2338 if (expr
->type
!= pet_expr_access
)
2339 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2340 "not an access expression", goto error
);
2341 isl_id_free(expr
->acc
.ref_id
);
2342 expr
->acc
.ref_id
= ref_id
;
2346 isl_id_free(ref_id
);
2347 pet_expr_free(expr
);
2351 /* Tag the access relation "access" with "id".
2352 * That is, insert the id as the range of a wrapped relation
2353 * in the domain of "access".
2355 * If "access" is of the form
2359 * then the result is of the form
2361 * [D[i] -> id[]] -> A[a]
2363 __isl_give isl_union_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
2364 __isl_take isl_union_map
*access
)
2367 isl_multi_aff
*add_tag
;
2370 if (expr
->type
!= pet_expr_access
)
2371 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2372 "not an access expression",
2373 return isl_union_map_free(access
));
2375 id
= isl_id_copy(expr
->acc
.ref_id
);
2376 space
= pet_expr_access_get_domain_space(expr
);
2377 space
= isl_space_from_domain(space
);
2378 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2379 add_tag
= isl_multi_aff_domain_map(space
);
2380 access
= isl_union_map_preimage_domain_multi_aff(access
, add_tag
);
2385 /* Return the access relation of the given "type" associated to "expr"
2386 * that maps pairs of domain iterations and argument values
2387 * to the corresponding accessed data elements.
2389 * If the requested access relation is explicitly available,
2390 * then return a copy. Otherwise, check if it is irrelevant for
2391 * the access expression and return an empty relation if this is the case.
2392 * Otherwise, introduce the requested access relation in "expr" and
2395 __isl_give isl_union_map
*pet_expr_access_get_dependent_access(
2396 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2398 isl_union_map
*access
;
2403 if (expr
->type
!= pet_expr_access
)
2404 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2405 "not an access expression", return NULL
);
2407 type
= internalize_type(type
);
2408 if (expr
->acc
.access
[type
])
2409 return isl_union_map_copy(expr
->acc
.access
[type
]);
2411 if (type
== pet_expr_access_may_read
)
2412 empty
= !expr
->acc
.read
;
2414 empty
= !expr
->acc
.write
;
2417 expr
= pet_expr_copy(expr
);
2418 expr
= introduce_access_relations(expr
);
2421 access
= isl_union_map_copy(expr
->acc
.access
[type
]);
2422 pet_expr_free(expr
);
2427 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr
));
2430 /* Return the may read access relation associated to "expr"
2431 * that maps pairs of domain iterations and argument values
2432 * to the corresponding accessed data elements.
2434 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_read(
2435 __isl_keep pet_expr
*expr
)
2437 return pet_expr_access_get_dependent_access(expr
,
2438 pet_expr_access_may_read
);
2441 /* Return the may write access relation associated to "expr"
2442 * that maps pairs of domain iterations and argument values
2443 * to the corresponding accessed data elements.
2445 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_write(
2446 __isl_keep pet_expr
*expr
)
2448 return pet_expr_access_get_dependent_access(expr
,
2449 pet_expr_access_may_write
);
2452 /* Return the must write access relation associated to "expr"
2453 * that maps pairs of domain iterations and argument values
2454 * to the corresponding accessed data elements.
2456 __isl_give isl_union_map
*pet_expr_access_get_dependent_must_write(
2457 __isl_keep pet_expr
*expr
)
2459 return pet_expr_access_get_dependent_access(expr
,
2460 pet_expr_access_must_write
);
2463 /* Return the relation of the given "type" mapping domain iterations
2464 * to the accessed data elements.
2465 * In particular, take the access relation and, in case of may_read
2466 * or may_write, project out the values of the arguments, if any.
2467 * In case of must_write, return the empty relation if there are
2470 __isl_give isl_union_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
,
2471 enum pet_expr_access_type type
)
2473 isl_union_map
*access
;
2479 if (expr
->type
!= pet_expr_access
)
2480 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2481 "not an access expression", return NULL
);
2483 if (expr
->n_arg
!= 0 && type
== pet_expr_access_must_write
) {
2484 space
= pet_expr_access_get_parameter_space(expr
);
2485 return isl_union_map_empty(space
);
2488 access
= pet_expr_access_get_dependent_access(expr
, type
);
2489 if (expr
->n_arg
== 0)
2492 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
2493 space
= isl_space_domain(space
);
2494 map
= isl_map_universe(isl_space_unwrap(space
));
2495 map
= isl_map_domain_map(map
);
2496 access
= isl_union_map_apply_domain(access
,
2497 isl_union_map_from_map(map
));
2502 /* Return the relation mapping domain iterations to all possibly
2503 * read data elements.
2505 __isl_give isl_union_map
*pet_expr_access_get_may_read(
2506 __isl_keep pet_expr
*expr
)
2508 return pet_expr_access_get_access(expr
, pet_expr_access_may_read
);
2511 /* Return the relation mapping domain iterations to all possibly
2512 * written data elements.
2514 __isl_give isl_union_map
*pet_expr_access_get_may_write(
2515 __isl_keep pet_expr
*expr
)
2517 return pet_expr_access_get_access(expr
, pet_expr_access_may_write
);
2520 /* Return a relation mapping domain iterations to definitely
2521 * written data elements, assuming the statement containing
2522 * the expression is executed.
2524 __isl_give isl_union_map
*pet_expr_access_get_must_write(
2525 __isl_keep pet_expr
*expr
)
2527 return pet_expr_access_get_access(expr
, pet_expr_access_must_write
);
2530 /* Return the relation of the given "type" mapping domain iterations to
2531 * accessed data elements, with its domain tagged with the reference
2534 static __isl_give isl_union_map
*pet_expr_access_get_tagged_access(
2535 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2537 isl_union_map
*access
;
2542 access
= pet_expr_access_get_access(expr
, type
);
2543 access
= pet_expr_tag_access(expr
, access
);
2548 /* Return the relation mapping domain iterations to all possibly
2549 * read data elements, with its domain tagged with the reference
2552 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_read(
2553 __isl_keep pet_expr
*expr
)
2555 return pet_expr_access_get_tagged_access(expr
,
2556 pet_expr_access_may_read
);
2559 /* Return the relation mapping domain iterations to all possibly
2560 * written data elements, with its domain tagged with the reference
2563 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_write(
2564 __isl_keep pet_expr
*expr
)
2566 return pet_expr_access_get_tagged_access(expr
,
2567 pet_expr_access_may_write
);
2570 /* Return the operation type of operation expression "expr".
2572 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
2576 if (expr
->type
!= pet_expr_op
)
2577 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2578 "not an operation expression", return pet_op_last
);
2583 /* Replace the operation type of operation expression "expr" by "type".
2585 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
2586 enum pet_op_type type
)
2589 return pet_expr_free(expr
);
2590 if (expr
->type
!= pet_expr_op
)
2591 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2592 "not an operation expression",
2593 return pet_expr_free(expr
));
2594 if (expr
->op
== type
)
2596 expr
= pet_expr_cow(expr
);
2604 /* Return the name of the function called by "expr".
2606 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
2610 if (expr
->type
!= pet_expr_call
)
2611 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2612 "not a call expression", return NULL
);
2613 return expr
->c
.name
;
2616 /* Replace the name of the function called by "expr" by "name".
2618 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
2619 __isl_keep
const char *name
)
2621 expr
= pet_expr_cow(expr
);
2623 return pet_expr_free(expr
);
2624 if (expr
->type
!= pet_expr_call
)
2625 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2626 "not a call expression", return pet_expr_free(expr
));
2628 expr
->c
.name
= strdup(name
);
2630 return pet_expr_free(expr
);
2634 /* Does the call expression "expr" have an associated function summary?
2636 int pet_expr_call_has_summary(__isl_keep pet_expr
*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 -1);
2644 return expr
->c
.summary
!= NULL
;
2647 /* Return a copy of the function summary associated to
2648 * the call expression "expr".
2650 __isl_give pet_function_summary
*pet_expr_call_get_summary(
2651 __isl_keep pet_expr
*expr
)
2655 if (expr
->type
!= pet_expr_call
)
2656 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2657 "not a call expression", return NULL
);
2659 return pet_function_summary_copy(expr
->c
.summary
);
2662 /* Replace the function summary associated to the call expression "expr"
2665 __isl_give pet_expr
*pet_expr_call_set_summary(__isl_take pet_expr
*expr
,
2666 __isl_take pet_function_summary
*summary
)
2668 expr
= pet_expr_cow(expr
);
2669 if (!expr
|| !summary
)
2671 if (expr
->type
!= pet_expr_call
)
2672 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2673 "not a call expression", goto error
);
2674 pet_function_summary_free(expr
->c
.summary
);
2675 expr
->c
.summary
= summary
;
2678 pet_function_summary_free(summary
);
2679 return pet_expr_free(expr
);
2682 /* Replace the type of the cast performed by "expr" by "name".
2684 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
2685 __isl_keep
const char *name
)
2687 expr
= pet_expr_cow(expr
);
2689 return pet_expr_free(expr
);
2690 if (expr
->type
!= pet_expr_cast
)
2691 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2692 "not a cast expression", return pet_expr_free(expr
));
2693 free(expr
->type_name
);
2694 expr
->type_name
= strdup(name
);
2695 if (!expr
->type_name
)
2696 return pet_expr_free(expr
);
2700 /* Return the value of the integer represented by "expr".
2702 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
2706 if (expr
->type
!= pet_expr_int
)
2707 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2708 "not an int expression", return NULL
);
2710 return isl_val_copy(expr
->i
);
2713 /* Replace the value of the integer represented by "expr" by "v".
2715 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
2716 __isl_take isl_val
*v
)
2718 expr
= pet_expr_cow(expr
);
2721 if (expr
->type
!= pet_expr_int
)
2722 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2723 "not an int expression", goto error
);
2724 isl_val_free(expr
->i
);
2730 pet_expr_free(expr
);
2734 /* Replace the value and string representation of the double
2735 * represented by "expr" by "d" and "s".
2737 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2738 double d
, __isl_keep
const char *s
)
2740 expr
= pet_expr_cow(expr
);
2742 return pet_expr_free(expr
);
2743 if (expr
->type
!= pet_expr_double
)
2744 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2745 "not a double expression", return pet_expr_free(expr
));
2748 expr
->d
.s
= strdup(s
);
2750 return pet_expr_free(expr
);
2754 /* Return a string representation of the double expression "expr".
2756 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2760 if (expr
->type
!= pet_expr_double
)
2761 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2762 "not a double expression", return NULL
);
2763 return strdup(expr
->d
.s
);
2766 /* Return a piecewise affine expression defined on the specified domain
2767 * that represents NaN.
2769 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2771 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2774 /* This function is called when we come across an access that is
2775 * nested in what is supposed to be an affine expression.
2776 * "pc" is the context in which the affine expression is created.
2777 * If nesting is allowed in "pc", we return an affine expression that is
2778 * equal to a new parameter corresponding to this nested access.
2779 * Otherwise, we return NaN.
2781 * Note that we currently don't allow nested accesses themselves
2782 * to contain any nested accesses, so we check if "expr" itself
2783 * involves any nested accesses (either explicitly as arguments
2784 * or implicitly through parameters) and return NaN if it does.
2786 * The new parameter is resolved in resolve_nested.
2788 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2789 __isl_keep pet_context
*pc
)
2794 isl_local_space
*ls
;
2800 if (!pet_context_allow_nesting(pc
))
2801 return non_affine(pet_context_get_space(pc
));
2803 if (pet_expr_get_type(expr
) != pet_expr_access
)
2804 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2805 "not an access expression", return NULL
);
2807 if (expr
->n_arg
> 0)
2808 return non_affine(pet_context_get_space(pc
));
2810 space
= pet_expr_access_get_parameter_space(expr
);
2811 nested
= pet_nested_any_in_space(space
);
2812 isl_space_free(space
);
2814 return non_affine(pet_context_get_space(pc
));
2816 ctx
= pet_expr_get_ctx(expr
);
2817 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2818 space
= pet_context_get_space(pc
);
2819 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2821 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2822 ls
= isl_local_space_from_space(space
);
2823 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2825 return isl_pw_aff_from_aff(aff
);
2828 /* Extract an affine expression from the access pet_expr "expr".
2829 * "pc" is the context in which the affine expression is created.
2831 * If "expr" is actually an affine expression rather than
2832 * a real access, then we return that expression.
2833 * Otherwise, we require that "expr" is of an integral type.
2834 * If not, we return NaN.
2836 * If the variable has been assigned a known affine expression,
2837 * then we return that expression.
2839 * Otherwise, we return an expression that is equal to a parameter
2840 * representing "expr" (if "allow_nested" is set).
2842 static __isl_give isl_pw_aff
*extract_affine_from_access(
2843 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2848 if (pet_expr_is_affine(expr
))
2849 return pet_expr_get_affine(expr
);
2851 if (pet_expr_get_type_size(expr
) == 0)
2852 return non_affine(pet_context_get_space(pc
));
2854 if (!pet_expr_is_scalar_access(expr
))
2855 return nested_access(expr
, pc
);
2857 id
= pet_expr_access_get_id(expr
);
2858 if (pet_context_is_assigned(pc
, id
))
2859 return pet_context_get_value(pc
, id
);
2862 return nested_access(expr
, pc
);
2865 /* Construct an affine expression from the integer constant "expr".
2866 * "pc" is the context in which the affine expression is created.
2868 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2869 __isl_keep pet_context
*pc
)
2871 isl_local_space
*ls
;
2877 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2878 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2880 return isl_pw_aff_from_aff(aff
);
2883 /* Extract an affine expression from an addition or subtraction operation.
2884 * Return NaN if we are unable to extract an affine expression.
2886 * "pc" is the context in which the affine expression is created.
2888 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2889 __isl_keep pet_context
*pc
)
2896 if (expr
->n_arg
!= 2)
2897 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2898 "expecting two arguments", return NULL
);
2900 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2901 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2903 switch (pet_expr_op_get_type(expr
)) {
2905 return isl_pw_aff_add(lhs
, rhs
);
2907 return isl_pw_aff_sub(lhs
, rhs
);
2909 isl_pw_aff_free(lhs
);
2910 isl_pw_aff_free(rhs
);
2911 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2912 "not an addition or subtraction operation",
2918 /* Extract an affine expression from an integer division or a modulo operation.
2919 * Return NaN if we are unable to extract an affine expression.
2921 * "pc" is the context in which the affine expression is created.
2923 * In particular, if "expr" is lhs/rhs, then return
2925 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2927 * If "expr" is lhs%rhs, then return
2929 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2931 * If the second argument (rhs) is not a (positive) integer constant,
2932 * then we fail to extract an affine expression.
2934 * We simplify the result in the context of the domain of "pc" in case
2935 * this domain implies that lhs >= 0 (or < 0).
2937 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2938 __isl_keep pet_context
*pc
)
2947 if (expr
->n_arg
!= 2)
2948 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2949 "expecting two arguments", return NULL
);
2951 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2953 is_cst
= isl_pw_aff_is_cst(rhs
);
2954 if (is_cst
< 0 || !is_cst
) {
2955 isl_pw_aff_free(rhs
);
2956 return non_affine(pet_context_get_space(pc
));
2959 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2961 switch (pet_expr_op_get_type(expr
)) {
2963 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
2966 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
2969 isl_pw_aff_free(lhs
);
2970 isl_pw_aff_free(rhs
);
2971 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2972 "not a div or mod operator", return NULL
);
2975 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
2978 /* Extract an affine expression from a multiplication operation.
2979 * Return NaN if we are unable to extract an affine expression.
2980 * In particular, if neither of the arguments is a (piecewise) constant
2981 * then we return NaN.
2983 * "pc" is the context in which the affine expression is created.
2985 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2986 __isl_keep pet_context
*pc
)
2988 int lhs_cst
, rhs_cst
;
2994 if (expr
->n_arg
!= 2)
2995 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2996 "expecting two arguments", return NULL
);
2998 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2999 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
3001 lhs_cst
= isl_pw_aff_is_cst(lhs
);
3002 rhs_cst
= isl_pw_aff_is_cst(rhs
);
3003 if (lhs_cst
>= 0 && rhs_cst
>= 0 && (lhs_cst
|| rhs_cst
))
3004 return isl_pw_aff_mul(lhs
, rhs
);
3006 isl_pw_aff_free(lhs
);
3007 isl_pw_aff_free(rhs
);
3009 if (lhs_cst
< 0 || rhs_cst
< 0)
3012 return non_affine(pet_context_get_space(pc
));
3015 /* Extract an affine expression from a negation operation.
3016 * Return NaN if we are unable to extract an affine expression.
3018 * "pc" is the context in which the affine expression is created.
3020 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
3021 __isl_keep pet_context
*pc
)
3027 if (expr
->n_arg
!= 1)
3028 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3029 "expecting one argument", return NULL
);
3031 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
3032 return isl_pw_aff_neg(res
);
3035 /* Extract an affine expression from a conditional operation.
3036 * Return NaN if we are unable to extract an affine expression.
3038 * "pc" is the context in which the affine expression is created.
3040 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
3041 __isl_keep pet_context
*pc
)
3043 isl_pw_aff
*cond
, *lhs
, *rhs
;
3047 if (expr
->n_arg
!= 3)
3048 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3049 "expecting three arguments", return NULL
);
3051 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3052 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
3053 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
3055 return isl_pw_aff_cond(cond
, lhs
, rhs
);
3058 /* Limit the domain of "pwaff" to those elements where the function
3061 * 2^{width-1} <= pwaff < 2^{width-1}
3063 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
3068 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
3069 isl_local_space
*ls
= isl_local_space_from_space(space
);
3074 ctx
= isl_pw_aff_get_ctx(pwaff
);
3075 v
= isl_val_int_from_ui(ctx
, width
- 1);
3076 v
= isl_val_2exp(v
);
3078 bound
= isl_aff_zero_on_domain(ls
);
3079 bound
= isl_aff_add_constant_val(bound
, v
);
3080 b
= isl_pw_aff_from_aff(bound
);
3082 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
3083 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3085 b
= isl_pw_aff_neg(b
);
3086 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
3087 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3092 /* Handle potential overflows on signed computations.
3094 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
3095 * then we adjust the domain of "pa" to avoid overflows.
3097 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
3101 struct pet_options
*options
;
3106 ctx
= isl_pw_aff_get_ctx(pa
);
3107 options
= isl_ctx_peek_pet_options(ctx
);
3108 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
3109 pa
= avoid_overflow(pa
, width
);
3114 /* Extract an affine expression from some an operation.
3115 * Return NaN if we are unable to extract an affine expression.
3116 * If the result of a binary (non boolean) operation is unsigned,
3117 * then we wrap it based on the size of the type. If the result is signed,
3118 * then we ensure that no overflow occurs.
3120 * "pc" is the context in which the affine expression is created.
3122 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
3123 __isl_keep pet_context
*pc
)
3128 switch (pet_expr_op_get_type(expr
)) {
3131 res
= extract_affine_add_sub(expr
, pc
);
3135 res
= extract_affine_div_mod(expr
, pc
);
3138 res
= extract_affine_mul(expr
, pc
);
3141 return extract_affine_neg(expr
, pc
);
3143 return extract_affine_cond(expr
, pc
);
3153 return pet_expr_extract_affine_condition(expr
, pc
);
3155 return non_affine(pet_context_get_space(pc
));
3160 if (isl_pw_aff_involves_nan(res
)) {
3161 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
3162 isl_pw_aff_free(res
);
3163 return non_affine(space
);
3166 type_size
= pet_expr_get_type_size(expr
);
3168 res
= pet_wrap_pw_aff(res
, type_size
);
3170 res
= signed_overflow(res
, -type_size
);
3175 /* Internal data structure for affine builtin function declarations.
3177 * "pencil" is set if the builtin is pencil specific.
3178 * "n_args" is the number of arguments the function takes.
3179 * "name" is the function name.
3181 struct affine_builtin_decl
{
3187 static struct affine_builtin_decl affine_builtins
[] = {
3195 { 0, 2, "intFloor" },
3196 { 0, 2, "intCeil" },
3201 /* List of min and max builtin functions.
3203 static const char *min_max_builtins
[] = {
3204 "min", "imin", "umin",
3205 "max", "imax", "umax"
3208 /* Is a function call to "name" with "n_args" arguments a call to a
3209 * builtin function for which we can construct an affine expression?
3210 * pencil specific builtins are only recognized if "pencil" is set.
3212 static int is_affine_builtin(int pencil
, int n_args
, const char *name
)
3216 for (i
= 0; i
< ARRAY_SIZE(affine_builtins
); ++i
) {
3217 struct affine_builtin_decl
*decl
= &affine_builtins
[i
];
3219 if (decl
->pencil
&& !pencil
)
3221 if (decl
->n_args
== n_args
&& !strcmp(decl
->name
, name
))
3228 /* Is function "name" a known min or max builtin function?
3230 static int is_min_or_max_builtin(const char *name
)
3234 for (i
= 0; i
< ARRAY_SIZE(min_max_builtins
); ++i
)
3235 if (!strcmp(min_max_builtins
[i
], name
))
3241 /* Extract an affine expression from some special function calls.
3242 * Return NaN if we are unable to extract an affine expression.
3243 * In particular, we handle "min", "max", "ceild", "floord",
3244 * "intMod", "intFloor" and "intCeil".
3245 * In case of the latter five, the second argument needs to be
3246 * a (positive) integer constant.
3247 * If the pencil option is set, then we also handle "{i,u}min" and
3250 * "pc" is the context in which the affine expression is created.
3252 static __isl_give isl_pw_aff
*extract_affine_from_call(
3253 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3256 isl_pw_aff
*aff1
, *aff2
;
3259 struct pet_options
*options
;
3263 ctx
= pet_expr_get_ctx(expr
);
3264 options
= isl_ctx_peek_pet_options(ctx
);
3266 n
= pet_expr_get_n_arg(expr
);
3267 name
= pet_expr_call_get_name(expr
);
3268 if (!is_affine_builtin(options
->pencil
, n
, name
))
3269 return non_affine(pet_context_get_space(pc
));
3271 if (is_min_or_max_builtin(name
)) {
3272 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3273 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
3275 if (strstr(name
, "min"))
3276 aff1
= isl_pw_aff_min(aff1
, aff2
);
3278 aff1
= isl_pw_aff_max(aff1
, aff2
);
3279 } else if (!strcmp(name
, "intMod")) {
3282 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3283 return non_affine(pet_context_get_space(pc
));
3284 v
= pet_expr_int_get_val(expr
->args
[1]);
3285 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3286 aff1
= isl_pw_aff_mod_val(aff1
, v
);
3290 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3291 return non_affine(pet_context_get_space(pc
));
3292 v
= pet_expr_int_get_val(expr
->args
[1]);
3293 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3294 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
3295 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
3296 aff1
= isl_pw_aff_floor(aff1
);
3298 aff1
= isl_pw_aff_ceil(aff1
);
3304 /* Extract an affine expression from "expr", if possible.
3305 * Otherwise return NaN.
3307 * "pc" is the context in which the affine expression is created.
3309 * Store the result in "pc" such that it can be reused in case
3310 * pet_expr_extract_affine is called again on the same pair of
3313 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
3314 __isl_keep pet_context
*pc
)
3316 isl_maybe_isl_pw_aff m
;
3322 m
= pet_context_get_extracted_affine(pc
, expr
);
3323 if (m
.valid
< 0 || m
.valid
)
3326 switch (pet_expr_get_type(expr
)) {
3327 case pet_expr_access
:
3328 pa
= extract_affine_from_access(expr
, pc
);
3331 pa
= extract_affine_from_int(expr
, pc
);
3334 pa
= extract_affine_from_op(expr
, pc
);
3337 pa
= extract_affine_from_call(expr
, pc
);
3340 case pet_expr_double
:
3341 case pet_expr_error
:
3342 pa
= non_affine(pet_context_get_space(pc
));
3346 if (pet_context_set_extracted_affine(pc
, expr
, pa
) < 0)
3347 return isl_pw_aff_free(pa
);
3352 /* Extract an affine expressions representing the comparison "LHS op RHS"
3353 * Return NaN if we are unable to extract such an affine expression.
3355 * "pc" is the context in which the affine expression is created.
3357 * If the comparison is of the form
3361 * then the expression is constructed as the conjunction of
3366 * A similar optimization is performed for max(a,b) <= c.
3367 * We do this because that will lead to simpler representations
3368 * of the expression.
3369 * If isl is ever enhanced to explicitly deal with min and max expressions,
3370 * this optimization can be removed.
3372 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
3373 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
3374 __isl_keep pet_context
*pc
)
3376 isl_pw_aff
*lhs_pa
, *rhs_pa
;
3378 if (op
== pet_op_gt
)
3379 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
3380 if (op
== pet_op_ge
)
3381 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
3383 if (op
== pet_op_lt
|| op
== pet_op_le
) {
3384 if (pet_expr_is_min(rhs
)) {
3385 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3387 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3389 return pet_and(lhs_pa
, rhs_pa
);
3391 if (pet_expr_is_max(lhs
)) {
3392 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
3394 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
3396 return pet_and(lhs_pa
, rhs_pa
);
3400 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
3401 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
3403 return pet_comparison(op
, lhs_pa
, rhs_pa
);
3406 /* Extract an affine expressions from the comparison "expr".
3407 * Return NaN if we are unable to extract such an affine expression.
3409 * "pc" is the context in which the affine expression is created.
3411 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
3412 __isl_keep pet_context
*pc
)
3414 enum pet_op_type type
;
3418 if (expr
->n_arg
!= 2)
3419 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3420 "expecting two arguments", return NULL
);
3422 type
= pet_expr_op_get_type(expr
);
3423 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
3427 /* Extract an affine expression representing the boolean operation
3428 * expressed by "expr".
3429 * Return NaN if we are unable to extract an affine expression.
3431 * "pc" is the context in which the affine expression is created.
3433 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
3434 __isl_keep pet_context
*pc
)
3436 isl_pw_aff
*lhs
, *rhs
;
3442 n
= pet_expr_get_n_arg(expr
);
3443 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3445 return pet_not(lhs
);
3447 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
3448 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
3451 /* Extract the affine expression "expr != 0 ? 1 : 0".
3452 * Return NaN if we are unable to extract an affine expression.
3454 * "pc" is the context in which the affine expression is created.
3456 static __isl_give isl_pw_aff
*extract_implicit_condition(
3457 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3461 res
= pet_expr_extract_affine(expr
, pc
);
3462 return pet_to_bool(res
);
3465 /* Extract a boolean affine expression from "expr".
3466 * Return NaN if we are unable to extract an affine expression.
3468 * "pc" is the context in which the affine expression is created.
3470 * If "expr" is neither a comparison nor a boolean operation,
3471 * then we assume it is an affine expression and return the
3472 * boolean expression "expr != 0 ? 1 : 0".
3474 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
3475 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3480 if (pet_expr_is_comparison(expr
))
3481 return extract_comparison(expr
, pc
);
3482 if (pet_expr_is_boolean(expr
))
3483 return extract_boolean(expr
, pc
);
3485 return extract_implicit_condition(expr
, pc
);
3488 /* Check if "expr" is an assume expression and if its single argument
3489 * can be converted to an affine expression in the context of "pc".
3490 * If so, replace the argument by the affine expression.
3492 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
3493 __isl_keep pet_context
*pc
)
3496 isl_multi_pw_aff
*index
;
3500 if (!pet_expr_is_assume(expr
))
3502 if (expr
->n_arg
!= 1)
3503 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3504 "expecting one argument", return pet_expr_free(expr
));
3506 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3508 return pet_expr_free(expr
);
3509 if (isl_pw_aff_involves_nan(cond
)) {
3510 isl_pw_aff_free(cond
);
3514 index
= isl_multi_pw_aff_from_pw_aff(cond
);
3515 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
3520 /* Return the number of bits needed to represent the type of "expr".
3521 * See the description of the type_size field of pet_expr.
3523 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
3525 return expr
? expr
->type_size
: 0;
3528 /* Replace the number of bits needed to represent the type of "expr"
3530 * See the description of the type_size field of pet_expr.
3532 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
3535 expr
= pet_expr_cow(expr
);
3539 expr
->type_size
= type_size
;
3544 /* Extend an access expression "expr" with an additional index "index".
3545 * In particular, add "index" as an extra argument to "expr" and
3546 * adjust the index expression of "expr" to refer to this extra argument.
3547 * The caller is responsible for calling pet_expr_access_set_depth
3548 * to update the corresponding access relation.
3550 * Note that we only collect the individual index expressions as
3551 * arguments of "expr" here.
3552 * An attempt to integrate them into the index expression of "expr"
3553 * is performed in pet_expr_access_plug_in_args.
3555 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
3556 __isl_take pet_expr
*index
)
3560 isl_local_space
*ls
;
3563 expr
= pet_expr_cow(expr
);
3564 if (!expr
|| !index
)
3566 if (expr
->type
!= pet_expr_access
)
3567 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3568 "not an access pet_expr", goto error
);
3570 n
= pet_expr_get_n_arg(expr
);
3571 expr
= pet_expr_insert_arg(expr
, n
, index
);
3575 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3576 ls
= isl_local_space_from_space(space
);
3577 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
3578 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
3579 if (!expr
->acc
.index
)
3580 return pet_expr_free(expr
);
3584 pet_expr_free(expr
);
3585 pet_expr_free(index
);
3589 /* Extend an access expression "expr" with an additional member acces to "id".
3590 * In particular, extend the index expression of "expr" to include
3591 * the additional member access.
3592 * The caller is responsible for calling pet_expr_access_set_depth
3593 * to update the corresponding access relation.
3595 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
3596 __isl_take isl_id
*id
)
3599 isl_multi_pw_aff
*field_access
;
3601 expr
= pet_expr_cow(expr
);
3604 if (expr
->type
!= pet_expr_access
)
3605 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3606 "not an access pet_expr", goto error
);
3608 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3609 space
= isl_space_from_domain(space
);
3610 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
3611 field_access
= isl_multi_pw_aff_zero(space
);
3612 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
3613 if (!expr
->acc
.index
)
3614 return pet_expr_free(expr
);
3618 pet_expr_free(expr
);
3623 /* Prefix the access expression "expr" with "prefix".
3624 * If "add" is set, then it is not the index expression "prefix" itself
3625 * that was passed to the function, but its address.
3627 __isl_give pet_expr
*pet_expr_access_patch(__isl_take pet_expr
*expr
,
3628 __isl_take isl_multi_pw_aff
*prefix
, int add
)
3630 enum pet_expr_access_type type
;
3632 expr
= pet_expr_cow(expr
);
3633 if (!expr
|| !prefix
)
3635 if (expr
->type
!= pet_expr_access
)
3636 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3637 "not an access pet_expr", goto error
);
3639 expr
->acc
.depth
+= isl_multi_pw_aff_dim(prefix
, isl_dim_out
) - add
;
3640 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
3641 if (!expr
->acc
.access
[type
])
3643 expr
->acc
.access
[type
] = pet_patch_union_map(
3644 isl_multi_pw_aff_copy(prefix
), expr
->acc
.access
[type
],
3646 if (!expr
->acc
.access
[type
])
3649 expr
->acc
.index
= pet_patch_multi_pw_aff(prefix
, expr
->acc
.index
, add
);
3650 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
3651 return pet_expr_free(expr
);
3655 pet_expr_free(expr
);
3656 isl_multi_pw_aff_free(prefix
);
3660 /* Dump the arguments of "expr" to "p" as a YAML sequence keyed
3661 * by "args", if there are any such arguments.
3663 static __isl_give isl_printer
*dump_arguments(__isl_keep pet_expr
*expr
,
3664 __isl_take isl_printer
*p
)
3668 if (expr
->n_arg
== 0)
3671 p
= isl_printer_print_str(p
, "args");
3672 p
= isl_printer_yaml_next(p
);
3673 p
= isl_printer_yaml_start_sequence(p
);
3674 for (i
= 0; i
< expr
->n_arg
; ++i
) {
3675 p
= pet_expr_print(expr
->args
[i
], p
);
3676 p
= isl_printer_yaml_next(p
);
3678 p
= isl_printer_yaml_end_sequence(p
);
3683 /* Print "expr" to "p" in YAML format.
3685 __isl_give isl_printer
*pet_expr_print(__isl_keep pet_expr
*expr
,
3686 __isl_take isl_printer
*p
)
3689 return isl_printer_free(p
);
3691 switch (expr
->type
) {
3692 case pet_expr_double
:
3693 p
= isl_printer_print_str(p
, expr
->d
.s
);
3696 p
= isl_printer_print_val(p
, expr
->i
);
3698 case pet_expr_access
:
3699 p
= isl_printer_yaml_start_mapping(p
);
3700 if (expr
->acc
.ref_id
) {
3701 p
= isl_printer_print_str(p
, "ref_id");
3702 p
= isl_printer_yaml_next(p
);
3703 p
= isl_printer_print_id(p
, expr
->acc
.ref_id
);
3704 p
= isl_printer_yaml_next(p
);
3706 p
= isl_printer_print_str(p
, "index");
3707 p
= isl_printer_yaml_next(p
);
3708 p
= isl_printer_print_multi_pw_aff(p
, expr
->acc
.index
);
3709 p
= isl_printer_yaml_next(p
);
3710 p
= isl_printer_print_str(p
, "depth");
3711 p
= isl_printer_yaml_next(p
);
3712 p
= isl_printer_print_int(p
, expr
->acc
.depth
);
3713 p
= isl_printer_yaml_next(p
);
3714 if (expr
->acc
.kill
) {
3715 p
= isl_printer_print_str(p
, "kill");
3716 p
= isl_printer_yaml_next(p
);
3717 p
= isl_printer_print_int(p
, 1);
3718 p
= isl_printer_yaml_next(p
);
3720 p
= isl_printer_print_str(p
, "read");
3721 p
= isl_printer_yaml_next(p
);
3722 p
= isl_printer_print_int(p
, expr
->acc
.read
);
3723 p
= isl_printer_yaml_next(p
);
3724 p
= isl_printer_print_str(p
, "write");
3725 p
= isl_printer_yaml_next(p
);
3726 p
= isl_printer_print_int(p
, expr
->acc
.write
);
3727 p
= isl_printer_yaml_next(p
);
3729 if (expr
->acc
.access
[pet_expr_access_may_read
]) {
3730 p
= isl_printer_print_str(p
, "may_read");
3731 p
= isl_printer_yaml_next(p
);
3732 p
= isl_printer_print_union_map(p
,
3733 expr
->acc
.access
[pet_expr_access_may_read
]);
3734 p
= isl_printer_yaml_next(p
);
3736 if (expr
->acc
.access
[pet_expr_access_may_write
]) {
3737 p
= isl_printer_print_str(p
, "may_write");
3738 p
= isl_printer_yaml_next(p
);
3739 p
= isl_printer_print_union_map(p
,
3740 expr
->acc
.access
[pet_expr_access_may_write
]);
3741 p
= isl_printer_yaml_next(p
);
3743 if (expr
->acc
.access
[pet_expr_access_must_write
]) {
3744 p
= isl_printer_print_str(p
, "must_write");
3745 p
= isl_printer_yaml_next(p
);
3746 p
= isl_printer_print_union_map(p
,
3747 expr
->acc
.access
[pet_expr_access_must_write
]);
3748 p
= isl_printer_yaml_next(p
);
3750 p
= dump_arguments(expr
, p
);
3751 p
= isl_printer_yaml_end_mapping(p
);
3754 p
= isl_printer_yaml_start_mapping(p
);
3755 p
= isl_printer_print_str(p
, "op");
3756 p
= isl_printer_yaml_next(p
);
3757 p
= isl_printer_print_str(p
, op_str
[expr
->op
]);
3758 p
= isl_printer_yaml_next(p
);
3759 p
= dump_arguments(expr
, p
);
3760 p
= isl_printer_yaml_end_mapping(p
);
3763 p
= isl_printer_yaml_start_mapping(p
);
3764 p
= isl_printer_print_str(p
, "call");
3765 p
= isl_printer_yaml_next(p
);
3766 p
= isl_printer_print_str(p
, expr
->c
.name
);
3767 p
= isl_printer_print_str(p
, "/");
3768 p
= isl_printer_print_int(p
, expr
->n_arg
);
3769 p
= isl_printer_yaml_next(p
);
3770 p
= dump_arguments(expr
, p
);
3771 if (expr
->c
.summary
) {
3772 p
= isl_printer_print_str(p
, "summary");
3773 p
= isl_printer_yaml_next(p
);
3774 p
= pet_function_summary_print(expr
->c
.summary
, p
);
3776 p
= isl_printer_yaml_end_mapping(p
);
3779 p
= isl_printer_yaml_start_mapping(p
);
3780 p
= isl_printer_print_str(p
, "cast");
3781 p
= isl_printer_yaml_next(p
);
3782 p
= isl_printer_print_str(p
, expr
->type_name
);
3783 p
= isl_printer_yaml_next(p
);
3784 p
= dump_arguments(expr
, p
);
3785 p
= isl_printer_yaml_end_mapping(p
);
3787 case pet_expr_error
:
3788 p
= isl_printer_print_str(p
, "ERROR");
3795 /* Dump "expr" to stderr with indentation "indent".
3797 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
3804 p
= isl_printer_to_file(pet_expr_get_ctx(expr
), stderr
);
3805 p
= isl_printer_set_indent(p
, indent
);
3806 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_BLOCK
);
3807 p
= isl_printer_start_line(p
);
3808 p
= pet_expr_print(expr
, p
);
3810 isl_printer_free(p
);
3813 void pet_expr_dump(__isl_keep pet_expr
*expr
)
3815 pet_expr_dump_with_indent(expr
, 0);