2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
44 #include "value_bounds.h"
46 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
48 static char *type_str
[] = {
49 [pet_expr_access
] = "access",
50 [pet_expr_call
] = "call",
51 [pet_expr_cast
] = "cast",
52 [pet_expr_double
] = "double",
53 [pet_expr_int
] = "int",
57 static char *op_str
[] = {
58 [pet_op_add_assign
] = "+=",
59 [pet_op_sub_assign
] = "-=",
60 [pet_op_mul_assign
] = "*=",
61 [pet_op_div_assign
] = "/=",
62 [pet_op_assign
] = "=",
77 [pet_op_post_inc
] = "++",
78 [pet_op_post_dec
] = "--",
79 [pet_op_pre_inc
] = "++",
80 [pet_op_pre_dec
] = "--",
81 [pet_op_address_of
] = "&",
90 [pet_op_assume
] = "assume",
91 [pet_op_kill
] = "kill"
94 const char *pet_op_str(enum pet_op_type op
)
99 int pet_op_is_inc_dec(enum pet_op_type op
)
101 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
102 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
105 const char *pet_type_str(enum pet_expr_type type
)
107 return type_str
[type
];
110 enum pet_op_type
pet_str_op(const char *str
)
114 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
115 if (!strcmp(op_str
[i
], str
))
121 enum pet_expr_type
pet_str_type(const char *str
)
125 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
126 if (!strcmp(type_str
[i
], str
))
132 /* Construct a pet_expr of the given type.
134 __isl_give pet_expr
*pet_expr_alloc(isl_ctx
*ctx
, enum pet_expr_type type
)
138 expr
= isl_calloc_type(ctx
, struct pet_expr
);
150 /* Construct an access pet_expr from an index expression.
151 * By default, the access is considered to be a read access.
152 * The initial depth is set from the index expression and
153 * may still be updated by the caller before the access relation
156 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
163 ctx
= isl_multi_pw_aff_get_ctx(index
);
164 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
171 expr
= pet_expr_access_set_index(expr
, index
);
175 isl_multi_pw_aff_free(index
);
179 /* Extend the range of "access" with "n" dimensions, retaining
180 * the tuple identifier on this range.
182 * If "access" represents a member access, then extend the range
185 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
189 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
191 if (!isl_map_range_is_wrapping(access
)) {
192 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
196 domain
= isl_map_copy(access
);
197 domain
= isl_map_range_factor_domain(domain
);
198 access
= isl_map_range_factor_range(access
);
199 access
= extend_range(access
, n
);
200 access
= isl_map_range_product(domain
, access
);
203 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
208 /* Does the access expression "expr" have any explicit access relation?
210 static int has_any_access_relation(__isl_keep pet_expr
*expr
)
212 enum pet_expr_access_type type
;
217 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
)
218 if (expr
->acc
.access
[type
])
224 /* Are all relevant access relations explicitly available in "expr"?
226 static int has_relevant_access_relations(__isl_keep pet_expr
*expr
)
228 enum pet_expr_access_type type
;
233 if (expr
->acc
.kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
235 if (expr
->acc
.read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
237 if (expr
->acc
.write
&&
238 (!expr
->acc
.access
[pet_expr_access_may_write
] ||
239 !expr
->acc
.access
[pet_expr_access_must_write
]))
245 /* Replace the depth of the access expr "expr" by "depth".
247 * To avoid inconsistencies between the depth and the access relation,
248 * we currently do not allow the depth to change once the access relation
249 * has been set or computed.
251 __isl_give pet_expr
*pet_expr_access_set_depth(__isl_take pet_expr
*expr
,
259 if (expr
->acc
.depth
== depth
)
261 if (has_any_access_relation(expr
))
262 isl_die(pet_expr_get_ctx(expr
), isl_error_unsupported
,
263 "depth cannot be changed after access relation "
264 "has been set or computed", return pet_expr_free(expr
));
266 expr
= pet_expr_cow(expr
);
269 expr
->acc
.depth
= depth
;
274 /* Construct a pet_expr that kills the elements specified by
275 * the index expression "index" and the access relation "access".
277 __isl_give pet_expr
*pet_expr_kill_from_access_and_index(
278 __isl_take isl_map
*access
, __isl_take isl_multi_pw_aff
*index
)
283 if (!access
|| !index
)
286 expr
= pet_expr_from_index(index
);
287 expr
= pet_expr_access_set_read(expr
, 0);
288 expr
= pet_expr_access_set_kill(expr
, 1);
289 depth
= isl_map_dim(access
, isl_dim_out
);
290 expr
= pet_expr_access_set_depth(expr
, depth
);
291 expr
= pet_expr_access_set_access(expr
, pet_expr_access_killed
,
292 isl_union_map_from_map(access
));
293 return pet_expr_new_unary(0, pet_op_kill
, expr
);
295 isl_map_free(access
);
296 isl_multi_pw_aff_free(index
);
300 /* Construct a unary pet_expr that performs "op" on "arg",
301 * where the result is represented using a type of "type_size" bits
302 * (may be zero if unknown or if the type is not an integer).
304 __isl_give pet_expr
*pet_expr_new_unary(int type_size
, enum pet_op_type op
,
305 __isl_take pet_expr
*arg
)
312 ctx
= pet_expr_get_ctx(arg
);
313 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
314 expr
= pet_expr_set_n_arg(expr
, 1);
319 expr
->type_size
= type_size
;
320 expr
->args
[pet_un_arg
] = arg
;
328 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
329 * where the result is represented using a type of "type_size" bits
330 * (may be zero if unknown or if the type is not an integer).
332 __isl_give pet_expr
*pet_expr_new_binary(int type_size
, enum pet_op_type op
,
333 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
340 ctx
= pet_expr_get_ctx(lhs
);
341 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
342 expr
= pet_expr_set_n_arg(expr
, 2);
347 expr
->type_size
= type_size
;
348 expr
->args
[pet_bin_lhs
] = lhs
;
349 expr
->args
[pet_bin_rhs
] = rhs
;
358 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
360 __isl_give pet_expr
*pet_expr_new_ternary(__isl_take pet_expr
*cond
,
361 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
366 if (!cond
|| !lhs
|| !rhs
)
368 ctx
= pet_expr_get_ctx(cond
);
369 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
370 expr
= pet_expr_set_n_arg(expr
, 3);
374 expr
->op
= pet_op_cond
;
375 expr
->args
[pet_ter_cond
] = cond
;
376 expr
->args
[pet_ter_true
] = lhs
;
377 expr
->args
[pet_ter_false
] = rhs
;
387 /* Construct a call pet_expr that calls function "name" with "n_arg"
388 * arguments. The caller is responsible for filling in the arguments.
390 __isl_give pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
395 expr
= pet_expr_alloc(ctx
, pet_expr_call
);
396 expr
= pet_expr_set_n_arg(expr
, n_arg
);
400 expr
->c
.name
= strdup(name
);
402 return pet_expr_free(expr
);
407 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
409 __isl_give pet_expr
*pet_expr_new_cast(const char *type_name
,
410 __isl_take pet_expr
*arg
)
418 ctx
= pet_expr_get_ctx(arg
);
419 expr
= pet_expr_alloc(ctx
, pet_expr_cast
);
420 expr
= pet_expr_set_n_arg(expr
, 1);
424 expr
->type_name
= strdup(type_name
);
425 if (!expr
->type_name
)
437 /* Construct a pet_expr that represents the double "d".
439 __isl_give pet_expr
*pet_expr_new_double(isl_ctx
*ctx
,
440 double val
, const char *s
)
444 expr
= pet_expr_alloc(ctx
, pet_expr_double
);
449 expr
->d
.s
= strdup(s
);
451 return pet_expr_free(expr
);
456 /* Construct a pet_expr that represents the integer value "v".
458 __isl_give pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
466 ctx
= isl_val_get_ctx(v
);
467 expr
= pet_expr_alloc(ctx
, pet_expr_int
);
479 /* Return an independent duplicate of "expr".
481 * In case of an access expression, make sure the depth of the duplicate is set
482 * before the access relation (if any) is set and after the index expression
485 static __isl_give pet_expr
*pet_expr_dup(__isl_keep pet_expr
*expr
)
489 enum pet_expr_access_type type
;
494 dup
= pet_expr_alloc(expr
->ctx
, expr
->type
);
495 dup
= pet_expr_set_type_size(dup
, expr
->type_size
);
496 dup
= pet_expr_set_n_arg(dup
, expr
->n_arg
);
497 for (i
= 0; i
< expr
->n_arg
; ++i
)
498 dup
= pet_expr_set_arg(dup
, i
, pet_expr_copy(expr
->args
[i
]));
500 switch (expr
->type
) {
501 case pet_expr_access
:
502 if (expr
->acc
.ref_id
)
503 dup
= pet_expr_access_set_ref_id(dup
,
504 isl_id_copy(expr
->acc
.ref_id
));
505 dup
= pet_expr_access_set_index(dup
,
506 isl_multi_pw_aff_copy(expr
->acc
.index
));
507 dup
= pet_expr_access_set_depth(dup
, expr
->acc
.depth
);
508 for (type
= pet_expr_access_begin
;
509 type
< pet_expr_access_end
; ++type
) {
510 if (!expr
->acc
.access
[type
])
512 dup
= pet_expr_access_set_access(dup
, type
,
513 isl_union_map_copy(expr
->acc
.access
[type
]));
515 dup
= pet_expr_access_set_read(dup
, expr
->acc
.read
);
516 dup
= pet_expr_access_set_write(dup
, expr
->acc
.write
);
517 dup
= pet_expr_access_set_kill(dup
, expr
->acc
.kill
);
520 dup
= pet_expr_call_set_name(dup
, expr
->c
.name
);
522 dup
= pet_expr_call_set_summary(dup
,
523 pet_function_summary_copy(expr
->c
.summary
));
526 dup
= pet_expr_cast_set_type_name(dup
, expr
->type_name
);
528 case pet_expr_double
:
529 dup
= pet_expr_double_set(dup
, expr
->d
.val
, expr
->d
.s
);
532 dup
= pet_expr_int_set_val(dup
, isl_val_copy(expr
->i
));
535 dup
= pet_expr_op_set_type(dup
, expr
->op
);
538 dup
= pet_expr_free(dup
);
545 __isl_give pet_expr
*pet_expr_cow(__isl_take pet_expr
*expr
)
553 return pet_expr_dup(expr
);
556 __isl_null pet_expr
*pet_expr_free(__isl_take pet_expr
*expr
)
558 enum pet_expr_access_type type
;
566 for (i
= 0; i
< expr
->n_arg
; ++i
)
567 pet_expr_free(expr
->args
[i
]);
570 switch (expr
->type
) {
571 case pet_expr_access
:
572 isl_id_free(expr
->acc
.ref_id
);
573 for (type
= pet_expr_access_begin
;
574 type
< pet_expr_access_end
; ++type
)
575 isl_union_map_free(expr
->acc
.access
[type
]);
576 isl_multi_pw_aff_free(expr
->acc
.index
);
580 pet_function_summary_free(expr
->c
.summary
);
583 free(expr
->type_name
);
585 case pet_expr_double
:
589 isl_val_free(expr
->i
);
596 isl_ctx_deref(expr
->ctx
);
601 /* Return an additional reference to "expr".
603 __isl_give pet_expr
*pet_expr_copy(__isl_keep pet_expr
*expr
)
612 /* Return the isl_ctx in which "expr" was created.
614 isl_ctx
*pet_expr_get_ctx(__isl_keep pet_expr
*expr
)
616 return expr
? expr
->ctx
: NULL
;
619 /* Return the type of "expr".
621 enum pet_expr_type
pet_expr_get_type(__isl_keep pet_expr
*expr
)
624 return pet_expr_error
;
628 /* Return the number of arguments of "expr".
630 int pet_expr_get_n_arg(__isl_keep pet_expr
*expr
)
638 /* Set the number of arguments of "expr" to "n".
640 * If "expr" originally had more arguments, then remove the extra arguments.
641 * If "expr" originally had fewer arguments, then create space for
642 * the extra arguments ans initialize them to NULL.
644 __isl_give pet_expr
*pet_expr_set_n_arg(__isl_take pet_expr
*expr
, int n
)
651 if (expr
->n_arg
== n
)
653 expr
= pet_expr_cow(expr
);
657 if (n
< expr
->n_arg
) {
658 for (i
= n
; i
< expr
->n_arg
; ++i
)
659 pet_expr_free(expr
->args
[i
]);
664 args
= isl_realloc_array(expr
->ctx
, expr
->args
, pet_expr
*, n
);
666 return pet_expr_free(expr
);
668 for (i
= expr
->n_arg
; i
< n
; ++i
)
669 expr
->args
[i
] = NULL
;
675 /* Return the argument of "expr" at position "pos".
677 __isl_give pet_expr
*pet_expr_get_arg(__isl_keep pet_expr
*expr
, int pos
)
681 if (pos
< 0 || pos
>= expr
->n_arg
)
682 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
683 "position out of bounds", return NULL
);
685 return pet_expr_copy(expr
->args
[pos
]);
688 /* Replace the argument of "expr" at position "pos" by "arg".
690 __isl_give pet_expr
*pet_expr_set_arg(__isl_take pet_expr
*expr
, int pos
,
691 __isl_take pet_expr
*arg
)
695 if (pos
< 0 || pos
>= expr
->n_arg
)
696 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
697 "position out of bounds", goto error
);
698 if (expr
->args
[pos
] == arg
) {
703 expr
= pet_expr_cow(expr
);
707 pet_expr_free(expr
->args
[pos
]);
708 expr
->args
[pos
] = arg
;
717 /* Does "expr" perform a comparison operation?
719 int pet_expr_is_comparison(__isl_keep pet_expr
*expr
)
723 if (expr
->type
!= pet_expr_op
)
738 /* Does "expr" perform a boolean operation?
740 int pet_expr_is_boolean(__isl_keep pet_expr
*expr
)
744 if (expr
->type
!= pet_expr_op
)
756 /* Is "expr" an assume statement?
758 int pet_expr_is_assume(__isl_keep pet_expr
*expr
)
762 if (expr
->type
!= pet_expr_op
)
764 return expr
->op
== pet_op_assume
;
767 /* Does "expr" perform a min operation?
769 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
773 if (expr
->type
!= pet_expr_call
)
775 if (expr
->n_arg
!= 2)
777 if (strcmp(expr
->c
.name
, "min") != 0)
782 /* Does "expr" perform a max operation?
784 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
788 if (expr
->type
!= pet_expr_call
)
790 if (expr
->n_arg
!= 2)
792 if (strcmp(expr
->c
.name
, "max") != 0)
797 /* Does "expr" represent an access to an unnamed space, i.e.,
798 * does it represent an affine expression?
800 int pet_expr_is_affine(__isl_keep pet_expr
*expr
)
806 if (expr
->type
!= pet_expr_access
)
809 has_id
= isl_multi_pw_aff_has_tuple_id(expr
->acc
.index
, isl_dim_out
);
816 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
817 * not part of any struct?
819 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
823 if (expr
->type
!= pet_expr_access
)
825 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
))
828 return expr
->acc
.depth
== 0;
831 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
834 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
835 __isl_keep isl_multi_pw_aff
*mpa2
)
839 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
840 if (equal
< 0 || equal
)
842 mpa2
= isl_multi_pw_aff_copy(mpa2
);
843 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
844 isl_multi_pw_aff_get_space(mpa1
));
845 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
846 isl_multi_pw_aff_free(mpa2
);
851 /* Construct an access relation from the index expression and
852 * the array depth of the access expression "expr".
854 * If the number of indices is smaller than the depth of the array,
855 * then we assume that all elements of the remaining dimensions
858 static __isl_give isl_union_map
*construct_access_relation(
859 __isl_keep pet_expr
*expr
)
868 access
= isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr
));
872 dim
= isl_map_dim(access
, isl_dim_out
);
873 if (dim
> expr
->acc
.depth
)
874 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
875 "number of indices greater than depth",
876 access
= isl_map_free(access
));
878 if (dim
!= expr
->acc
.depth
)
879 access
= extend_range(access
, expr
->acc
.depth
- dim
);
881 return isl_union_map_from_map(access
);
884 /* Ensure that all relevant access relations are explicitly
885 * available in "expr".
887 * If "expr" does not already have the relevant access relations, then create
888 * them based on the index expression and the array depth.
890 * We do not cow since adding an explicit access relation
891 * does not change the meaning of the expression.
893 static __isl_give pet_expr
*introduce_access_relations(
894 __isl_take pet_expr
*expr
)
896 enum pet_expr_access_type type
;
897 isl_union_map
*access
;
899 int kill
, read
, write
;
903 if (has_relevant_access_relations(expr
))
906 access
= construct_access_relation(expr
);
908 return pet_expr_free(expr
);
910 kill
= expr
->acc
.kill
;
911 read
= expr
->acc
.read
;
912 write
= expr
->acc
.write
;
913 if (kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
914 expr
->acc
.access
[pet_expr_access_fake_killed
] =
915 isl_union_map_copy(access
);
916 if (read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
917 expr
->acc
.access
[pet_expr_access_may_read
] =
918 isl_union_map_copy(access
);
919 if (write
&& !expr
->acc
.access
[pet_expr_access_may_write
])
920 expr
->acc
.access
[pet_expr_access_may_write
] =
921 isl_union_map_copy(access
);
922 if (write
&& !expr
->acc
.access
[pet_expr_access_must_write
])
923 expr
->acc
.access
[pet_expr_access_must_write
] =
924 isl_union_map_copy(access
);
926 isl_union_map_free(access
);
928 if (!has_relevant_access_relations(expr
))
929 return pet_expr_free(expr
);
934 /* Return 1 if the two pet_exprs are equivalent.
936 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
939 enum pet_expr_access_type type
;
941 if (!expr1
|| !expr2
)
944 if (expr1
->type
!= expr2
->type
)
946 if (expr1
->n_arg
!= expr2
->n_arg
)
948 for (i
= 0; i
< expr1
->n_arg
; ++i
)
949 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
951 switch (expr1
->type
) {
954 case pet_expr_double
:
955 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
957 if (expr1
->d
.val
!= expr2
->d
.val
)
961 if (!isl_val_eq(expr1
->i
, expr2
->i
))
964 case pet_expr_access
:
965 if (expr1
->acc
.read
!= expr2
->acc
.read
)
967 if (expr1
->acc
.write
!= expr2
->acc
.write
)
969 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
971 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
973 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
975 if (!multi_pw_aff_is_equal(expr1
->acc
.index
, expr2
->acc
.index
))
977 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
979 if (has_relevant_access_relations(expr1
) !=
980 has_relevant_access_relations(expr2
)) {
982 expr1
= pet_expr_copy(expr1
);
983 expr2
= pet_expr_copy(expr2
);
984 expr1
= introduce_access_relations(expr1
);
985 expr2
= introduce_access_relations(expr2
);
986 equal
= pet_expr_is_equal(expr1
, expr2
);
987 pet_expr_free(expr1
);
988 pet_expr_free(expr2
);
991 for (type
= pet_expr_access_begin
;
992 type
< pet_expr_access_end
; ++type
) {
993 if (!expr1
->acc
.access
[type
] !=
994 !expr2
->acc
.access
[type
])
996 if (!expr1
->acc
.access
[type
])
998 if (!isl_union_map_is_equal(expr1
->acc
.access
[type
],
999 expr2
->acc
.access
[type
]))
1004 if (expr1
->op
!= expr2
->op
)
1008 if (strcmp(expr1
->c
.name
, expr2
->c
.name
))
1012 if (strcmp(expr1
->type_name
, expr2
->type_name
))
1020 /* Does the access expression "expr" read the accessed elements?
1022 int pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
1026 if (expr
->type
!= pet_expr_access
)
1027 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1028 "not an access expression", return -1);
1030 return expr
->acc
.read
;
1033 /* Does the access expression "expr" write to the accessed elements?
1035 int pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
1039 if (expr
->type
!= pet_expr_access
)
1040 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1041 "not an access expression", return -1);
1043 return expr
->acc
.write
;
1046 /* Return the identifier of the array accessed by "expr".
1048 * If "expr" represents a member access, then return the identifier
1049 * of the outer structure array.
1051 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
1055 if (expr
->type
!= pet_expr_access
)
1056 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1057 "not an access expression", return NULL
);
1059 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
)) {
1063 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1064 space
= isl_space_range(space
);
1065 while (space
&& isl_space_is_wrapping(space
))
1066 space
= isl_space_domain(isl_space_unwrap(space
));
1067 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
1068 isl_space_free(space
);
1073 return isl_multi_pw_aff_get_tuple_id(expr
->acc
.index
, isl_dim_out
);
1076 /* Return the parameter space of "expr".
1078 __isl_give isl_space
*pet_expr_access_get_parameter_space(
1079 __isl_keep pet_expr
*expr
)
1085 if (expr
->type
!= pet_expr_access
)
1086 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1087 "not an access expression", return NULL
);
1089 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1090 space
= isl_space_params(space
);
1095 /* Return the domain space of "expr", including the arguments (if any).
1097 __isl_give isl_space
*pet_expr_access_get_augmented_domain_space(
1098 __isl_keep pet_expr
*expr
)
1104 if (expr
->type
!= pet_expr_access
)
1105 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1106 "not an access expression", return NULL
);
1108 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1109 space
= isl_space_domain(space
);
1114 /* Return the domain space of "expr", without the arguments (if any).
1116 __isl_give isl_space
*pet_expr_access_get_domain_space(
1117 __isl_keep pet_expr
*expr
)
1121 space
= pet_expr_access_get_augmented_domain_space(expr
);
1122 if (isl_space_is_wrapping(space
))
1123 space
= isl_space_domain(isl_space_unwrap(space
));
1128 /* Return the space of the data accessed by "expr".
1130 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
1136 if (expr
->type
!= pet_expr_access
)
1137 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1138 "not an access expression", return NULL
);
1140 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1141 space
= isl_space_range(space
);
1146 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1148 static __isl_give pet_expr
*pet_expr_map_expr_of_type(__isl_take pet_expr
*expr
,
1149 enum pet_expr_type type
,
1150 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1155 n
= pet_expr_get_n_arg(expr
);
1156 for (i
= 0; i
< n
; ++i
) {
1157 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1158 arg
= pet_expr_map_expr_of_type(arg
, type
, fn
, user
);
1159 expr
= pet_expr_set_arg(expr
, i
, arg
);
1165 if (expr
->type
== type
)
1166 expr
= fn(expr
, user
);
1171 /* Modify all expressions of type pet_expr_access in "expr"
1172 * by calling "fn" on them.
1174 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
1175 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1178 return pet_expr_map_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1181 /* Modify all expressions of type pet_expr_call in "expr"
1182 * by calling "fn" on them.
1184 __isl_give pet_expr
*pet_expr_map_call(__isl_take pet_expr
*expr
,
1185 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1188 return pet_expr_map_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1191 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1193 * Return -1 on error (where fn returning a negative value is treated as
1195 * Otherwise return 0.
1197 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
1198 enum pet_expr_type type
,
1199 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1206 for (i
= 0; i
< expr
->n_arg
; ++i
)
1207 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1208 type
, fn
, user
) < 0)
1211 if (expr
->type
== type
)
1212 return fn(expr
, user
);
1217 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1219 * Return -1 on error (where fn returning a negative value is treated as
1221 * Otherwise return 0.
1223 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1224 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1226 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1229 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1231 * Return -1 on error (where fn returning a negative value is treated as
1233 * Otherwise return 0.
1235 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1236 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1238 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1241 /* Internal data structure for pet_expr_writes.
1242 * "id" is the identifier that we are looking for.
1243 * "found" is set if we have found the identifier being written to.
1245 struct pet_expr_writes_data
{
1250 /* Given an access expression, check if it writes to data->id.
1251 * If so, set data->found and abort the search.
1253 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1255 struct pet_expr_writes_data
*data
= user
;
1258 if (!expr
->acc
.write
)
1260 if (pet_expr_is_affine(expr
))
1263 write_id
= pet_expr_access_get_id(expr
);
1264 isl_id_free(write_id
);
1269 if (write_id
!= data
->id
)
1276 /* Does expression "expr" write to "id"?
1278 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1280 struct pet_expr_writes_data data
;
1284 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1291 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1292 * index expression and access relations of "expr" (if any)
1293 * to dimensions of "dst_type" at "dst_pos".
1295 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1296 enum isl_dim_type dst_type
, unsigned dst_pos
,
1297 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1299 enum pet_expr_access_type type
;
1301 expr
= pet_expr_cow(expr
);
1304 if (expr
->type
!= pet_expr_access
)
1305 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1306 "not an access pet_expr", return pet_expr_free(expr
));
1308 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1309 if (!expr
->acc
.access
[type
])
1311 expr
->acc
.access
[type
] =
1312 pet_union_map_move_dims(expr
->acc
.access
[type
],
1313 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1314 if (!expr
->acc
.access
[type
])
1317 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1318 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1319 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1320 return pet_expr_free(expr
);
1325 /* Replace the index expression and access relations (if any) of "expr"
1326 * by their preimages under the function represented by "ma".
1328 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1329 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1331 enum pet_expr_access_type type
;
1333 expr
= pet_expr_cow(expr
);
1336 if (expr
->type
!= pet_expr_access
)
1337 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1338 "not an access pet_expr", goto error
);
1340 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1341 if (!expr
->acc
.access
[type
])
1343 expr
->acc
.access
[type
] =
1344 isl_union_map_preimage_domain_multi_aff(
1345 expr
->acc
.access
[type
], isl_multi_aff_copy(ma
));
1346 if (!expr
->acc
.access
[type
])
1349 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1351 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1352 return pet_expr_free(expr
);
1356 isl_multi_aff_free(ma
);
1357 pet_expr_free(expr
);
1361 /* Replace the index expression and access relations (if any) of "expr"
1362 * by their preimages under the function represented by "mpa".
1364 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1365 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1367 enum pet_expr_access_type type
;
1369 expr
= pet_expr_cow(expr
);
1372 if (expr
->type
!= pet_expr_access
)
1373 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1374 "not an access pet_expr", goto error
);
1376 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1377 if (!expr
->acc
.access
[type
])
1379 expr
->acc
.access
[type
] =
1380 isl_union_map_preimage_domain_multi_pw_aff(
1381 expr
->acc
.access
[type
], isl_multi_pw_aff_copy(mpa
));
1382 if (!expr
->acc
.access
[type
])
1385 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1386 expr
->acc
.index
, mpa
);
1387 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1388 return pet_expr_free(expr
);
1392 isl_multi_pw_aff_free(mpa
);
1393 pet_expr_free(expr
);
1397 /* Return the index expression of access expression "expr".
1399 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1400 __isl_keep pet_expr
*expr
)
1404 if (expr
->type
!= pet_expr_access
)
1405 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1406 "not an access expression", return NULL
);
1408 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1411 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1413 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1416 enum pet_expr_access_type type
;
1418 expr
= pet_expr_cow(expr
);
1421 if (expr
->type
!= pet_expr_access
)
1422 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1423 "not an access expression", return pet_expr_free(expr
));
1425 if (!has_any_access_relation(expr
))
1428 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1429 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1430 if (!expr
->acc
.access
[type
])
1432 space
= isl_space_align_params(space
,
1433 isl_union_map_get_space(expr
->acc
.access
[type
]));
1435 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1436 isl_space_copy(space
));
1437 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1438 if (!expr
->acc
.access
[type
])
1440 expr
->acc
.access
[type
] =
1441 isl_union_map_align_params(expr
->acc
.access
[type
],
1442 isl_space_copy(space
));
1443 if (!expr
->acc
.access
[type
])
1446 isl_space_free(space
);
1447 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1448 return pet_expr_free(expr
);
1453 /* Are "expr1" and "expr2" both array accesses such that
1454 * the access relation of "expr1" is a subset of that of "expr2"?
1455 * Only take into account the first "n_arg" arguments.
1457 * This function is tailored for use by mark_self_dependences in nest.c.
1458 * In particular, the input expressions may have more than "n_arg"
1459 * elements in their arguments arrays, while only the first "n_arg"
1460 * elements are referenced from the access relations.
1462 int pet_expr_is_sub_access(__isl_keep pet_expr
*expr1
,
1463 __isl_keep pet_expr
*expr2
, int n_arg
)
1469 if (!expr1
|| !expr2
)
1471 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1473 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1475 if (pet_expr_is_affine(expr1
))
1477 if (pet_expr_is_affine(expr2
))
1479 n1
= pet_expr_get_n_arg(expr1
);
1482 n2
= pet_expr_get_n_arg(expr2
);
1487 for (i
= 0; i
< n1
; ++i
) {
1489 equal
= pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]);
1490 if (equal
< 0 || !equal
)
1493 id1
= pet_expr_access_get_id(expr1
);
1494 id2
= pet_expr_access_get_id(expr2
);
1502 expr1
= pet_expr_copy(expr1
);
1503 expr2
= pet_expr_copy(expr2
);
1504 expr1
= introduce_access_relations(expr1
);
1505 expr2
= introduce_access_relations(expr2
);
1506 if (!expr1
|| !expr2
)
1509 is_subset
= isl_union_map_is_subset(
1510 expr1
->acc
.access
[pet_expr_access_may_read
],
1511 expr2
->acc
.access
[pet_expr_access_may_read
]);
1513 pet_expr_free(expr1
);
1514 pet_expr_free(expr2
);
1518 pet_expr_free(expr1
);
1519 pet_expr_free(expr2
);
1523 /* Given a set in the iteration space "domain", extend it to live in the space
1524 * of the domain of access relations.
1526 * That, is the number of arguments "n" is 0, then simply return domain.
1527 * Otherwise, return [domain -> [a_1,...,a_n]].
1529 static __isl_give isl_set
*add_arguments(__isl_take isl_set
*domain
, int n
)
1536 map
= isl_map_from_domain(domain
);
1537 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
1538 return isl_map_wrap(map
);
1541 /* Add extra conditions to the domains of all access relations in "expr",
1542 * introducing access relations if they are not already present.
1544 * The conditions are not added to the index expression. Instead, they
1545 * are used to try and simplify the index expression.
1547 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1548 __isl_take isl_set
*cond
)
1551 isl_union_set
*uset
;
1552 enum pet_expr_access_type type
;
1554 expr
= pet_expr_cow(expr
);
1558 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1559 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1560 isl_set_copy(cond
));
1565 if (expr
->type
!= pet_expr_access
) {
1570 expr
= introduce_access_relations(expr
);
1574 cond
= add_arguments(cond
, expr
->n_arg
);
1575 uset
= isl_union_set_from_set(isl_set_copy(cond
));
1576 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1577 if (!expr
->acc
.access
[type
])
1579 expr
->acc
.access
[type
] =
1580 isl_union_map_intersect_domain(expr
->acc
.access
[type
],
1581 isl_union_set_copy(uset
));
1582 if (!expr
->acc
.access
[type
])
1585 isl_union_set_free(uset
);
1586 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, cond
);
1587 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1588 return pet_expr_free(expr
);
1593 return pet_expr_free(expr
);
1596 /* Modify the access relations (if any) and index expression
1597 * of the given access expression
1598 * based on the given iteration space transformation.
1599 * In particular, precompose the access relation and index expression
1600 * with the update function.
1602 * If the access has any arguments then the domain of the access relation
1603 * is a wrapped mapping from the iteration space to the space of
1604 * argument values. We only need to change the domain of this wrapped
1605 * mapping, so we extend the input transformation with an identity mapping
1606 * on the space of argument values.
1608 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1609 __isl_keep isl_multi_pw_aff
*update
)
1611 enum pet_expr_access_type type
;
1613 expr
= pet_expr_cow(expr
);
1616 if (expr
->type
!= pet_expr_access
)
1617 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1618 "not an access expression", return pet_expr_free(expr
));
1620 update
= isl_multi_pw_aff_copy(update
);
1622 if (expr
->n_arg
> 0) {
1624 isl_multi_pw_aff
*id
;
1626 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1627 space
= isl_space_domain(space
);
1628 space
= isl_space_unwrap(space
);
1629 space
= isl_space_range(space
);
1630 space
= isl_space_map_from_set(space
);
1631 id
= isl_multi_pw_aff_identity(space
);
1632 update
= isl_multi_pw_aff_product(update
, id
);
1635 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1636 if (!expr
->acc
.access
[type
])
1638 expr
->acc
.access
[type
] =
1639 isl_union_map_preimage_domain_multi_pw_aff(
1640 expr
->acc
.access
[type
],
1641 isl_multi_pw_aff_copy(update
));
1642 if (!expr
->acc
.access
[type
])
1645 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1646 expr
->acc
.index
, update
);
1647 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1648 return pet_expr_free(expr
);
1653 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1655 isl_multi_pw_aff
*update
= user
;
1657 return pet_expr_access_update_domain(expr
, update
);
1660 /* Modify all access relations in "expr" by precomposing them with
1661 * the given iteration space transformation.
1663 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1664 __isl_take isl_multi_pw_aff
*update
)
1666 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1667 isl_multi_pw_aff_free(update
);
1671 /* Given an expression with accesses that have a 0D anonymous domain,
1672 * replace those domains by "space".
1674 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1675 __isl_take isl_space
*space
)
1677 isl_multi_pw_aff
*mpa
;
1679 space
= isl_space_from_domain(space
);
1680 mpa
= isl_multi_pw_aff_zero(space
);
1681 return pet_expr_update_domain(expr
, mpa
);
1684 /* Add all parameters in "space" to the access relations (if any)
1685 * and index expression of "expr".
1687 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1689 isl_space
*space
= user
;
1690 enum pet_expr_access_type type
;
1692 expr
= pet_expr_cow(expr
);
1695 if (expr
->type
!= pet_expr_access
)
1696 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1697 "not an access expression", return pet_expr_free(expr
));
1699 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1700 if (!expr
->acc
.access
[type
])
1702 expr
->acc
.access
[type
] =
1703 isl_union_map_align_params(expr
->acc
.access
[type
],
1704 isl_space_copy(space
));
1705 if (!expr
->acc
.access
[type
])
1708 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1709 isl_space_copy(space
));
1710 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1711 return pet_expr_free(expr
);
1716 /* Add all parameters in "space" to all access relations and index expressions
1719 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1720 __isl_take isl_space
*space
)
1722 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1723 isl_space_free(space
);
1727 /* Insert an argument expression corresponding to "test" in front
1728 * of the list of arguments described by *n_arg and *args.
1730 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1731 __isl_keep isl_multi_pw_aff
*test
)
1734 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1737 return pet_expr_free(expr
);
1738 expr
= pet_expr_cow(expr
);
1743 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1745 return pet_expr_free(expr
);
1748 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1750 return pet_expr_free(expr
);
1751 for (i
= 0; i
< expr
->n_arg
; ++i
)
1752 ext
[1 + i
] = expr
->args
[i
];
1757 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1759 return pet_expr_free(expr
);
1764 /* Make the expression "expr" depend on the value of "test"
1765 * being equal to "satisfied".
1767 * If "test" is an affine expression, we simply add the conditions
1768 * on the expression having the value "satisfied" to all access relations
1769 * (introducing access relations if they are missing) and index expressions.
1771 * Otherwise, we add a filter to "expr" (which is then assumed to be
1772 * an access expression) corresponding to "test" being equal to "satisfied".
1774 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1775 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1780 isl_pw_multi_aff
*pma
;
1781 enum pet_expr_access_type type
;
1783 expr
= pet_expr_cow(expr
);
1787 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
1791 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
1792 isl_multi_pw_aff_free(test
);
1794 cond
= isl_pw_aff_non_zero_set(pa
);
1796 cond
= isl_pw_aff_zero_set(pa
);
1797 return pet_expr_restrict(expr
, cond
);
1800 ctx
= isl_multi_pw_aff_get_ctx(test
);
1801 if (expr
->type
!= pet_expr_access
)
1802 isl_die(ctx
, isl_error_invalid
,
1803 "can only filter access expressions", goto error
);
1805 expr
= introduce_access_relations(expr
);
1809 space
= isl_space_domain(isl_multi_pw_aff_get_space(expr
->acc
.index
));
1810 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1811 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
1813 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1814 if (!expr
->acc
.access
[type
])
1816 expr
->acc
.access
[type
] =
1817 isl_union_map_preimage_domain_pw_multi_aff(
1818 expr
->acc
.access
[type
],
1819 isl_pw_multi_aff_copy(pma
));
1820 if (!expr
->acc
.access
[type
])
1823 pma
= isl_pw_multi_aff_gist(pma
,
1824 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
1825 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
1826 expr
->acc
.index
, pma
);
1827 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1830 expr
= insert_access_arg(expr
, test
);
1832 isl_multi_pw_aff_free(test
);
1835 isl_multi_pw_aff_free(test
);
1836 return pet_expr_free(expr
);
1839 /* Add a reference identifier to access expression "expr".
1840 * "user" points to an integer that contains the sequence number
1841 * of the next reference.
1843 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
1850 expr
= pet_expr_cow(expr
);
1853 if (expr
->type
!= pet_expr_access
)
1854 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1855 "not an access expression", return pet_expr_free(expr
));
1857 ctx
= pet_expr_get_ctx(expr
);
1858 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
1859 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
1860 if (!expr
->acc
.ref_id
)
1861 return pet_expr_free(expr
);
1866 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
1868 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
1871 /* Reset the user pointer on all parameter and tuple ids in
1872 * the access relations (if any) and the index expression
1873 * of the access expression "expr".
1875 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
1878 enum pet_expr_access_type type
;
1880 expr
= pet_expr_cow(expr
);
1883 if (expr
->type
!= pet_expr_access
)
1884 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1885 "not an access expression", return pet_expr_free(expr
));
1887 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1888 if (!expr
->acc
.access
[type
])
1890 expr
->acc
.access
[type
] =
1891 isl_union_map_reset_user(expr
->acc
.access
[type
]);
1892 if (!expr
->acc
.access
[type
])
1895 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
1896 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1897 return pet_expr_free(expr
);
1902 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
1904 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
1907 /* Data used in access_gist() callback.
1909 struct pet_access_gist_data
{
1911 isl_union_map
*value_bounds
;
1914 /* Given an expression "expr" of type pet_expr_access, compute
1915 * the gist of the associated access relations (if any) and index expression
1916 * with respect to data->domain and the bounds on the values of the arguments
1917 * of the expression.
1919 * The arguments of "expr" have been gisted right before "expr" itself
1920 * is gisted. The gisted arguments may have become equal where before
1921 * they may not have been (obviously) equal. We therefore take
1922 * the opportunity to remove duplicate arguments here.
1924 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
1926 struct pet_access_gist_data
*data
= user
;
1928 isl_union_set
*uset
;
1929 enum pet_expr_access_type type
;
1931 expr
= pet_expr_remove_duplicate_args(expr
);
1932 expr
= pet_expr_cow(expr
);
1935 if (expr
->type
!= pet_expr_access
)
1936 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1937 "not an access expression", return pet_expr_free(expr
));
1939 domain
= isl_set_copy(data
->domain
);
1940 if (expr
->n_arg
> 0)
1941 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
1942 data
->value_bounds
);
1944 uset
= isl_union_set_from_set(isl_set_copy(domain
));
1945 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1946 if (!expr
->acc
.access
[type
])
1948 expr
->acc
.access
[type
] =
1949 isl_union_map_gist_domain(expr
->acc
.access
[type
],
1950 isl_union_set_copy(uset
));
1951 if (!expr
->acc
.access
[type
])
1954 isl_union_set_free(uset
);
1955 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
1956 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1957 return pet_expr_free(expr
);
1962 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
1963 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
1965 struct pet_access_gist_data data
= { context
, value_bounds
};
1967 return pet_expr_map_access(expr
, &access_gist
, &data
);
1970 /* Mark "expr" as a read dependening on "read".
1972 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
1976 return pet_expr_free(expr
);
1977 if (expr
->type
!= pet_expr_access
)
1978 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1979 "not an access expression", return pet_expr_free(expr
));
1980 if (expr
->acc
.read
== read
)
1982 expr
= pet_expr_cow(expr
);
1985 expr
->acc
.read
= read
;
1990 /* Mark "expr" as a write dependening on "write".
1992 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
1996 return pet_expr_free(expr
);
1997 if (expr
->type
!= pet_expr_access
)
1998 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1999 "not an access expression", return pet_expr_free(expr
));
2000 if (expr
->acc
.write
== write
)
2002 expr
= pet_expr_cow(expr
);
2005 expr
->acc
.write
= write
;
2010 /* Mark "expr" as a kill dependening on "kill".
2012 __isl_give pet_expr
*pet_expr_access_set_kill(__isl_take pet_expr
*expr
,
2016 return pet_expr_free(expr
);
2017 if (expr
->type
!= pet_expr_access
)
2018 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2019 "not an access expression", return pet_expr_free(expr
));
2020 if (expr
->acc
.kill
== kill
)
2022 expr
= pet_expr_cow(expr
);
2025 expr
->acc
.kill
= kill
;
2030 /* Map the access type "type" to the corresponding location
2031 * in the access array.
2032 * In particular, the access relation of type pet_expr_access_killed is
2033 * stored in the element at position pet_expr_access_fake_killed.
2035 static enum pet_expr_access_type
internalize_type(
2036 enum pet_expr_access_type type
)
2038 if (type
== pet_expr_access_killed
)
2039 return pet_expr_access_fake_killed
;
2043 /* Replace the access relation of the given "type" of "expr" by "access".
2044 * If the access relation is non-empty and the type is a read or a write,
2045 * then also mark the access expression itself as a read or a write.
2047 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
2048 enum pet_expr_access_type type
, __isl_take isl_union_map
*access
)
2052 expr
= pet_expr_cow(expr
);
2053 if (!expr
|| !access
)
2055 if (expr
->type
!= pet_expr_access
)
2056 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2057 "not an access expression", goto error
);
2058 type
= internalize_type(type
);
2059 isl_union_map_free(expr
->acc
.access
[type
]);
2060 expr
->acc
.access
[type
] = access
;
2065 empty
= isl_union_map_is_empty(access
);
2067 return pet_expr_free(expr
);
2071 if (type
== pet_expr_access_may_read
)
2072 expr
= pet_expr_access_set_read(expr
, 1);
2074 expr
= pet_expr_access_set_write(expr
, 1);
2078 isl_union_map_free(access
);
2079 pet_expr_free(expr
);
2083 /* Replace the index expression of "expr" by "index" and
2084 * set the array depth accordingly.
2086 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
2087 __isl_take isl_multi_pw_aff
*index
)
2089 expr
= pet_expr_cow(expr
);
2090 if (!expr
|| !index
)
2092 if (expr
->type
!= pet_expr_access
)
2093 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2094 "not an access expression", goto error
);
2095 isl_multi_pw_aff_free(expr
->acc
.index
);
2096 expr
->acc
.index
= index
;
2097 expr
->acc
.depth
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
2101 isl_multi_pw_aff_free(index
);
2102 pet_expr_free(expr
);
2106 /* Return the reference identifier of access expression "expr".
2108 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
2112 if (expr
->type
!= pet_expr_access
)
2113 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2114 "not an access expression", return NULL
);
2116 return isl_id_copy(expr
->acc
.ref_id
);
2119 /* Replace the reference identifier of access expression "expr" by "ref_id".
2121 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
2122 __isl_take isl_id
*ref_id
)
2124 expr
= pet_expr_cow(expr
);
2125 if (!expr
|| !ref_id
)
2127 if (expr
->type
!= pet_expr_access
)
2128 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2129 "not an access expression", goto error
);
2130 isl_id_free(expr
->acc
.ref_id
);
2131 expr
->acc
.ref_id
= ref_id
;
2135 isl_id_free(ref_id
);
2136 pet_expr_free(expr
);
2140 /* Tag the access relation "access" with "id".
2141 * That is, insert the id as the range of a wrapped relation
2142 * in the domain of "access".
2144 * If "access" is of the form
2148 * then the result is of the form
2150 * [D[i] -> id[]] -> A[a]
2152 __isl_give isl_union_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
2153 __isl_take isl_union_map
*access
)
2156 isl_multi_aff
*add_tag
;
2159 if (expr
->type
!= pet_expr_access
)
2160 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2161 "not an access expression",
2162 return isl_union_map_free(access
));
2164 id
= isl_id_copy(expr
->acc
.ref_id
);
2165 space
= pet_expr_access_get_domain_space(expr
);
2166 space
= isl_space_from_domain(space
);
2167 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2168 add_tag
= isl_multi_aff_domain_map(space
);
2169 access
= isl_union_map_preimage_domain_multi_aff(access
, add_tag
);
2174 /* Return the access relation of the given "type" associated to "expr"
2175 * that maps pairs of domain iterations and argument values
2176 * to the corresponding accessed data elements.
2178 * If the requested access relation is explicitly available,
2179 * then return a copy. Otherwise, check if it is irrelevant for
2180 * the access expression and return an empty relation if this is the case.
2181 * Otherwise, introduce the requested access relation in "expr" and
2184 __isl_give isl_union_map
*pet_expr_access_get_dependent_access(
2185 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2187 isl_union_map
*access
;
2192 if (expr
->type
!= pet_expr_access
)
2193 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2194 "not an access expression", return NULL
);
2196 type
= internalize_type(type
);
2197 if (expr
->acc
.access
[type
])
2198 return isl_union_map_copy(expr
->acc
.access
[type
]);
2200 if (type
== pet_expr_access_may_read
)
2201 empty
= !expr
->acc
.read
;
2203 empty
= !expr
->acc
.write
;
2206 expr
= pet_expr_copy(expr
);
2207 expr
= introduce_access_relations(expr
);
2210 access
= isl_union_map_copy(expr
->acc
.access
[type
]);
2211 pet_expr_free(expr
);
2216 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr
));
2219 /* Return the may read access relation associated to "expr"
2220 * that maps pairs of domain iterations and argument values
2221 * to the corresponding accessed data elements.
2223 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_read(
2224 __isl_keep pet_expr
*expr
)
2226 return pet_expr_access_get_dependent_access(expr
,
2227 pet_expr_access_may_read
);
2230 /* Return the may write access relation associated to "expr"
2231 * that maps pairs of domain iterations and argument values
2232 * to the corresponding accessed data elements.
2234 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_write(
2235 __isl_keep pet_expr
*expr
)
2237 return pet_expr_access_get_dependent_access(expr
,
2238 pet_expr_access_may_write
);
2241 /* Return the must write access relation associated to "expr"
2242 * that maps pairs of domain iterations and argument values
2243 * to the corresponding accessed data elements.
2245 __isl_give isl_union_map
*pet_expr_access_get_dependent_must_write(
2246 __isl_keep pet_expr
*expr
)
2248 return pet_expr_access_get_dependent_access(expr
,
2249 pet_expr_access_must_write
);
2252 /* Return the relation of the given "type" mapping domain iterations
2253 * to the accessed data elements.
2254 * In particular, take the access relation and, in case of may_read
2255 * or may_write, project out the values of the arguments, if any.
2256 * In case of must_write, return the empty relation if there are
2259 __isl_give isl_union_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
,
2260 enum pet_expr_access_type type
)
2262 isl_union_map
*access
;
2268 if (expr
->type
!= pet_expr_access
)
2269 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2270 "not an access expression", return NULL
);
2272 if (expr
->n_arg
!= 0 && type
== pet_expr_access_must_write
) {
2273 space
= pet_expr_access_get_parameter_space(expr
);
2274 return isl_union_map_empty(space
);
2277 access
= pet_expr_access_get_dependent_access(expr
, type
);
2278 if (expr
->n_arg
== 0)
2281 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
2282 space
= isl_space_domain(space
);
2283 map
= isl_map_universe(isl_space_unwrap(space
));
2284 map
= isl_map_domain_map(map
);
2285 access
= isl_union_map_apply_domain(access
,
2286 isl_union_map_from_map(map
));
2291 /* Return the relation mapping domain iterations to all possibly
2292 * read data elements.
2294 __isl_give isl_union_map
*pet_expr_access_get_may_read(
2295 __isl_keep pet_expr
*expr
)
2297 return pet_expr_access_get_access(expr
, pet_expr_access_may_read
);
2300 /* Return the relation mapping domain iterations to all possibly
2301 * written data elements.
2303 __isl_give isl_union_map
*pet_expr_access_get_may_write(
2304 __isl_keep pet_expr
*expr
)
2306 return pet_expr_access_get_access(expr
, pet_expr_access_may_write
);
2309 /* Return a relation mapping domain iterations to definitely
2310 * written data elements, assuming the statement containing
2311 * the expression is executed.
2313 __isl_give isl_union_map
*pet_expr_access_get_must_write(
2314 __isl_keep pet_expr
*expr
)
2316 return pet_expr_access_get_access(expr
, pet_expr_access_must_write
);
2319 /* Return the relation of the given "type" mapping domain iterations to
2320 * accessed data elements, with its domain tagged with the reference
2323 static __isl_give isl_union_map
*pet_expr_access_get_tagged_access(
2324 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2326 isl_union_map
*access
;
2331 access
= pet_expr_access_get_access(expr
, type
);
2332 access
= pet_expr_tag_access(expr
, access
);
2337 /* Return the relation mapping domain iterations to all possibly
2338 * read data elements, with its domain tagged with the reference
2341 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_read(
2342 __isl_keep pet_expr
*expr
)
2344 return pet_expr_access_get_tagged_access(expr
,
2345 pet_expr_access_may_read
);
2348 /* Return the relation mapping domain iterations to all possibly
2349 * written data elements, with its domain tagged with the reference
2352 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_write(
2353 __isl_keep pet_expr
*expr
)
2355 return pet_expr_access_get_tagged_access(expr
,
2356 pet_expr_access_may_write
);
2359 /* Return the operation type of operation expression "expr".
2361 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
2365 if (expr
->type
!= pet_expr_op
)
2366 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2367 "not an operation expression", return pet_op_last
);
2372 /* Replace the operation type of operation expression "expr" by "type".
2374 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
2375 enum pet_op_type type
)
2378 return pet_expr_free(expr
);
2379 if (expr
->type
!= pet_expr_op
)
2380 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2381 "not an operation expression",
2382 return pet_expr_free(expr
));
2383 if (expr
->op
== type
)
2385 expr
= pet_expr_cow(expr
);
2393 /* Return the name of the function called by "expr".
2395 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
2399 if (expr
->type
!= pet_expr_call
)
2400 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2401 "not a call expression", return NULL
);
2402 return expr
->c
.name
;
2405 /* Replace the name of the function called by "expr" by "name".
2407 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
2408 __isl_keep
const char *name
)
2410 expr
= pet_expr_cow(expr
);
2412 return pet_expr_free(expr
);
2413 if (expr
->type
!= pet_expr_call
)
2414 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2415 "not a call expression", return pet_expr_free(expr
));
2417 expr
->c
.name
= strdup(name
);
2419 return pet_expr_free(expr
);
2423 /* Does the call expression "expr" have an associated function summary?
2425 int pet_expr_call_has_summary(__isl_keep pet_expr
*expr
)
2429 if (expr
->type
!= pet_expr_call
)
2430 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2431 "not a call expression", return -1);
2433 return expr
->c
.summary
!= NULL
;
2436 /* Return a copy of the function summary associated to
2437 * the call expression "expr".
2439 __isl_give pet_function_summary
*pet_expr_call_get_summary(
2440 __isl_keep pet_expr
*expr
)
2444 if (expr
->type
!= pet_expr_call
)
2445 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2446 "not a call expression", return NULL
);
2448 return pet_function_summary_copy(expr
->c
.summary
);
2451 /* Replace the function summary associated to the call expression "expr"
2454 __isl_give pet_expr
*pet_expr_call_set_summary(__isl_take pet_expr
*expr
,
2455 __isl_take pet_function_summary
*summary
)
2457 expr
= pet_expr_cow(expr
);
2458 if (!expr
|| !summary
)
2460 if (expr
->type
!= pet_expr_call
)
2461 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2462 "not a call expression", goto error
);
2463 pet_function_summary_free(expr
->c
.summary
);
2464 expr
->c
.summary
= summary
;
2467 pet_function_summary_free(summary
);
2468 return pet_expr_free(expr
);
2471 /* Replace the type of the cast performed by "expr" by "name".
2473 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
2474 __isl_keep
const char *name
)
2476 expr
= pet_expr_cow(expr
);
2478 return pet_expr_free(expr
);
2479 if (expr
->type
!= pet_expr_cast
)
2480 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2481 "not a cast expression", return pet_expr_free(expr
));
2482 free(expr
->type_name
);
2483 expr
->type_name
= strdup(name
);
2484 if (!expr
->type_name
)
2485 return pet_expr_free(expr
);
2489 /* Return the value of the integer represented by "expr".
2491 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
2495 if (expr
->type
!= pet_expr_int
)
2496 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2497 "not an int expression", return NULL
);
2499 return isl_val_copy(expr
->i
);
2502 /* Replace the value of the integer represented by "expr" by "v".
2504 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
2505 __isl_take isl_val
*v
)
2507 expr
= pet_expr_cow(expr
);
2510 if (expr
->type
!= pet_expr_int
)
2511 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2512 "not an int expression", goto error
);
2513 isl_val_free(expr
->i
);
2519 pet_expr_free(expr
);
2523 /* Replace the value and string representation of the double
2524 * represented by "expr" by "d" and "s".
2526 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2527 double d
, __isl_keep
const char *s
)
2529 expr
= pet_expr_cow(expr
);
2531 return pet_expr_free(expr
);
2532 if (expr
->type
!= pet_expr_double
)
2533 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2534 "not a double expression", return pet_expr_free(expr
));
2537 expr
->d
.s
= strdup(s
);
2539 return pet_expr_free(expr
);
2543 /* Return a string representation of the double expression "expr".
2545 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2549 if (expr
->type
!= pet_expr_double
)
2550 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2551 "not a double expression", return NULL
);
2552 return strdup(expr
->d
.s
);
2555 /* Return a piecewise affine expression defined on the specified domain
2556 * that represents NaN.
2558 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2560 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2563 /* This function is called when we come across an access that is
2564 * nested in what is supposed to be an affine expression.
2565 * "pc" is the context in which the affine expression is created.
2566 * If nesting is allowed in "pc", we return an affine expression that is
2567 * equal to a new parameter corresponding to this nested access.
2568 * Otherwise, we return NaN.
2570 * Note that we currently don't allow nested accesses themselves
2571 * to contain any nested accesses, so we check if "expr" itself
2572 * involves any nested accesses (either explicitly as arguments
2573 * or implicitly through parameters) and return NaN if it does.
2575 * The new parameter is resolved in resolve_nested.
2577 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2578 __isl_keep pet_context
*pc
)
2583 isl_local_space
*ls
;
2589 if (!pet_context_allow_nesting(pc
))
2590 return non_affine(pet_context_get_space(pc
));
2592 if (pet_expr_get_type(expr
) != pet_expr_access
)
2593 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2594 "not an access expression", return NULL
);
2596 if (expr
->n_arg
> 0)
2597 return non_affine(pet_context_get_space(pc
));
2599 space
= pet_expr_access_get_parameter_space(expr
);
2600 nested
= pet_nested_any_in_space(space
);
2601 isl_space_free(space
);
2603 return non_affine(pet_context_get_space(pc
));
2605 ctx
= pet_expr_get_ctx(expr
);
2606 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2607 space
= pet_context_get_space(pc
);
2608 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2610 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2611 ls
= isl_local_space_from_space(space
);
2612 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2614 return isl_pw_aff_from_aff(aff
);
2617 /* Extract an affine expression from the access pet_expr "expr".
2618 * "pc" is the context in which the affine expression is created.
2620 * If "expr" is actually an affine expression rather than
2621 * a real access, then we return that expression.
2622 * Otherwise, we require that "expr" is of an integral type.
2623 * If not, we return NaN.
2625 * If the variable has been assigned a known affine expression,
2626 * then we return that expression.
2628 * Otherwise, we return an expression that is equal to a parameter
2629 * representing "expr" (if "allow_nested" is set).
2631 static __isl_give isl_pw_aff
*extract_affine_from_access(
2632 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2637 if (pet_expr_is_affine(expr
)) {
2639 isl_multi_pw_aff
*mpa
;
2641 mpa
= pet_expr_access_get_index(expr
);
2642 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
2643 isl_multi_pw_aff_free(mpa
);
2647 if (pet_expr_get_type_size(expr
) == 0)
2648 return non_affine(pet_context_get_space(pc
));
2650 if (!pet_expr_is_scalar_access(expr
))
2651 return nested_access(expr
, pc
);
2653 id
= pet_expr_access_get_id(expr
);
2654 if (pet_context_is_assigned(pc
, id
))
2655 return pet_context_get_value(pc
, id
);
2658 return nested_access(expr
, pc
);
2661 /* Construct an affine expression from the integer constant "expr".
2662 * "pc" is the context in which the affine expression is created.
2664 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2665 __isl_keep pet_context
*pc
)
2667 isl_local_space
*ls
;
2673 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2674 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2676 return isl_pw_aff_from_aff(aff
);
2679 /* Extract an affine expression from an addition or subtraction operation.
2680 * Return NaN if we are unable to extract an affine expression.
2682 * "pc" is the context in which the affine expression is created.
2684 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2685 __isl_keep pet_context
*pc
)
2692 if (expr
->n_arg
!= 2)
2693 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2694 "expecting two arguments", return NULL
);
2696 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2697 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2699 switch (pet_expr_op_get_type(expr
)) {
2701 return isl_pw_aff_add(lhs
, rhs
);
2703 return isl_pw_aff_sub(lhs
, rhs
);
2705 isl_pw_aff_free(lhs
);
2706 isl_pw_aff_free(rhs
);
2707 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2708 "not an addition or subtraction operation",
2714 /* Extract an affine expression from an integer division or a modulo operation.
2715 * Return NaN if we are unable to extract an affine expression.
2717 * "pc" is the context in which the affine expression is created.
2719 * In particular, if "expr" is lhs/rhs, then return
2721 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2723 * If "expr" is lhs%rhs, then return
2725 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2727 * If the second argument (rhs) is not a (positive) integer constant,
2728 * then we fail to extract an affine expression.
2730 * We simplify the result in the context of the domain of "pc" in case
2731 * this domain implies that lhs >= 0 (or < 0).
2733 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2734 __isl_keep pet_context
*pc
)
2743 if (expr
->n_arg
!= 2)
2744 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2745 "expecting two arguments", return NULL
);
2747 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2749 is_cst
= isl_pw_aff_is_cst(rhs
);
2750 if (is_cst
< 0 || !is_cst
) {
2751 isl_pw_aff_free(rhs
);
2752 return non_affine(pet_context_get_space(pc
));
2755 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2757 switch (pet_expr_op_get_type(expr
)) {
2759 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
2762 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
2765 isl_pw_aff_free(lhs
);
2766 isl_pw_aff_free(rhs
);
2767 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2768 "not a div or mod operator", return NULL
);
2771 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
2774 /* Extract an affine expression from a multiplication operation.
2775 * Return NaN if we are unable to extract an affine expression.
2776 * In particular, if neither of the arguments is a (piecewise) constant
2777 * then we return NaN.
2779 * "pc" is the context in which the affine expression is created.
2781 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2782 __isl_keep pet_context
*pc
)
2784 int lhs_cst
, rhs_cst
;
2790 if (expr
->n_arg
!= 2)
2791 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2792 "expecting two arguments", return NULL
);
2794 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2795 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2797 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2798 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2799 if (lhs_cst
>= 0 && rhs_cst
>= 0 && (lhs_cst
|| rhs_cst
))
2800 return isl_pw_aff_mul(lhs
, rhs
);
2802 isl_pw_aff_free(lhs
);
2803 isl_pw_aff_free(rhs
);
2805 if (lhs_cst
< 0 || rhs_cst
< 0)
2808 return non_affine(pet_context_get_space(pc
));
2811 /* Extract an affine expression from a negation operation.
2812 * Return NaN if we are unable to extract an affine expression.
2814 * "pc" is the context in which the affine expression is created.
2816 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
2817 __isl_keep pet_context
*pc
)
2823 if (expr
->n_arg
!= 1)
2824 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2825 "expecting one argument", return NULL
);
2827 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
2828 return isl_pw_aff_neg(res
);
2831 /* Extract an affine expression from a conditional operation.
2832 * Return NaN if we are unable to extract an affine expression.
2834 * "pc" is the context in which the affine expression is created.
2836 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
2837 __isl_keep pet_context
*pc
)
2839 isl_pw_aff
*cond
, *lhs
, *rhs
;
2843 if (expr
->n_arg
!= 3)
2844 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2845 "expecting three arguments", return NULL
);
2847 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2848 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2849 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
2851 return isl_pw_aff_cond(cond
, lhs
, rhs
);
2858 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
, unsigned width
)
2863 ctx
= isl_pw_aff_get_ctx(pwaff
);
2864 mod
= isl_val_int_from_ui(ctx
, width
);
2865 mod
= isl_val_2exp(mod
);
2867 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
2872 /* Limit the domain of "pwaff" to those elements where the function
2875 * 2^{width-1} <= pwaff < 2^{width-1}
2877 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
2882 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
2883 isl_local_space
*ls
= isl_local_space_from_space(space
);
2888 ctx
= isl_pw_aff_get_ctx(pwaff
);
2889 v
= isl_val_int_from_ui(ctx
, width
- 1);
2890 v
= isl_val_2exp(v
);
2892 bound
= isl_aff_zero_on_domain(ls
);
2893 bound
= isl_aff_add_constant_val(bound
, v
);
2894 b
= isl_pw_aff_from_aff(bound
);
2896 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
2897 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2899 b
= isl_pw_aff_neg(b
);
2900 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
2901 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2906 /* Handle potential overflows on signed computations.
2908 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2909 * then we adjust the domain of "pa" to avoid overflows.
2911 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
2915 struct pet_options
*options
;
2920 ctx
= isl_pw_aff_get_ctx(pa
);
2921 options
= isl_ctx_peek_pet_options(ctx
);
2922 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
2923 pa
= avoid_overflow(pa
, width
);
2928 /* Extract an affine expression from some an operation.
2929 * Return NaN if we are unable to extract an affine expression.
2930 * If the result of a binary (non boolean) operation is unsigned,
2931 * then we wrap it based on the size of the type. If the result is signed,
2932 * then we ensure that no overflow occurs.
2934 * "pc" is the context in which the affine expression is created.
2936 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
2937 __isl_keep pet_context
*pc
)
2942 switch (pet_expr_op_get_type(expr
)) {
2945 res
= extract_affine_add_sub(expr
, pc
);
2949 res
= extract_affine_div_mod(expr
, pc
);
2952 res
= extract_affine_mul(expr
, pc
);
2955 return extract_affine_neg(expr
, pc
);
2957 return extract_affine_cond(expr
, pc
);
2967 return pet_expr_extract_affine_condition(expr
, pc
);
2969 return non_affine(pet_context_get_space(pc
));
2974 if (isl_pw_aff_involves_nan(res
)) {
2975 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
2976 isl_pw_aff_free(res
);
2977 return non_affine(space
);
2980 type_size
= pet_expr_get_type_size(expr
);
2982 res
= wrap(res
, type_size
);
2984 res
= signed_overflow(res
, -type_size
);
2989 /* Extract an affine expression from some special function calls.
2990 * Return NaN if we are unable to extract an affine expression.
2991 * In particular, we handle "min", "max", "ceild", "floord",
2992 * "intMod", "intFloor" and "intCeil".
2993 * In case of the latter five, the second argument needs to be
2994 * a (positive) integer constant.
2996 * "pc" is the context in which the affine expression is created.
2998 static __isl_give isl_pw_aff
*extract_affine_from_call(
2999 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3001 isl_pw_aff
*aff1
, *aff2
;
3005 n
= pet_expr_get_n_arg(expr
);
3006 name
= pet_expr_call_get_name(expr
);
3007 if (!(n
== 2 && !strcmp(name
, "min")) &&
3008 !(n
== 2 && !strcmp(name
, "max")) &&
3009 !(n
== 2 && !strcmp(name
, "intMod")) &&
3010 !(n
== 2 && !strcmp(name
, "intFloor")) &&
3011 !(n
== 2 && !strcmp(name
, "intCeil")) &&
3012 !(n
== 2 && !strcmp(name
, "floord")) &&
3013 !(n
== 2 && !strcmp(name
, "ceild")))
3014 return non_affine(pet_context_get_space(pc
));
3016 if (!strcmp(name
, "min") || !strcmp(name
, "max")) {
3017 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3018 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
3020 if (!strcmp(name
, "min"))
3021 aff1
= isl_pw_aff_min(aff1
, aff2
);
3023 aff1
= isl_pw_aff_max(aff1
, aff2
);
3024 } else if (!strcmp(name
, "intMod")) {
3027 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3028 return non_affine(pet_context_get_space(pc
));
3029 v
= pet_expr_int_get_val(expr
->args
[1]);
3030 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3031 aff1
= isl_pw_aff_mod_val(aff1
, v
);
3035 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3036 return non_affine(pet_context_get_space(pc
));
3037 v
= pet_expr_int_get_val(expr
->args
[1]);
3038 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3039 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
3040 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
3041 aff1
= isl_pw_aff_floor(aff1
);
3043 aff1
= isl_pw_aff_ceil(aff1
);
3049 /* Extract an affine expression from "expr", if possible.
3050 * Otherwise return NaN.
3052 * "pc" is the context in which the affine expression is created.
3054 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
3055 __isl_keep pet_context
*pc
)
3060 switch (pet_expr_get_type(expr
)) {
3061 case pet_expr_access
:
3062 return extract_affine_from_access(expr
, pc
);
3064 return extract_affine_from_int(expr
, pc
);
3066 return extract_affine_from_op(expr
, pc
);
3068 return extract_affine_from_call(expr
, pc
);
3070 case pet_expr_double
:
3071 case pet_expr_error
:
3072 return non_affine(pet_context_get_space(pc
));
3076 /* Extract an affine expressions representing the comparison "LHS op RHS"
3077 * Return NaN if we are unable to extract such an affine expression.
3079 * "pc" is the context in which the affine expression is created.
3081 * If the comparison is of the form
3085 * then the expression is constructed as the conjunction of
3090 * A similar optimization is performed for max(a,b) <= c.
3091 * We do this because that will lead to simpler representations
3092 * of the expression.
3093 * If isl is ever enhanced to explicitly deal with min and max expressions,
3094 * this optimization can be removed.
3096 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
3097 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
3098 __isl_keep pet_context
*pc
)
3100 isl_pw_aff
*lhs_pa
, *rhs_pa
;
3102 if (op
== pet_op_gt
)
3103 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
3104 if (op
== pet_op_ge
)
3105 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
3107 if (op
== pet_op_lt
|| op
== pet_op_le
) {
3108 if (pet_expr_is_min(rhs
)) {
3109 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3111 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3113 return pet_and(lhs_pa
, rhs_pa
);
3115 if (pet_expr_is_max(lhs
)) {
3116 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
3118 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
3120 return pet_and(lhs_pa
, rhs_pa
);
3124 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
3125 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
3127 return pet_comparison(op
, lhs_pa
, rhs_pa
);
3130 /* Extract an affine expressions from the comparison "expr".
3131 * Return NaN if we are unable to extract such an affine expression.
3133 * "pc" is the context in which the affine expression is created.
3135 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
3136 __isl_keep pet_context
*pc
)
3138 enum pet_op_type type
;
3142 if (expr
->n_arg
!= 2)
3143 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3144 "expecting two arguments", return NULL
);
3146 type
= pet_expr_op_get_type(expr
);
3147 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
3151 /* Extract an affine expression representing the boolean operation
3152 * expressed by "expr".
3153 * Return NaN if we are unable to extract an affine expression.
3155 * "pc" is the context in which the affine expression is created.
3157 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
3158 __isl_keep pet_context
*pc
)
3160 isl_pw_aff
*lhs
, *rhs
;
3166 n
= pet_expr_get_n_arg(expr
);
3167 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3169 return pet_not(lhs
);
3171 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
3172 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
3175 /* Extract the affine expression "expr != 0 ? 1 : 0".
3176 * Return NaN if we are unable to extract an affine expression.
3178 * "pc" is the context in which the affine expression is created.
3180 static __isl_give isl_pw_aff
*extract_implicit_condition(
3181 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3185 res
= pet_expr_extract_affine(expr
, pc
);
3186 return pet_to_bool(res
);
3189 /* Extract a boolean affine expression from "expr".
3190 * Return NaN if we are unable to extract an affine expression.
3192 * "pc" is the context in which the affine expression is created.
3194 * If "expr" is neither a comparison nor a boolean operation,
3195 * then we assume it is an affine expression and return the
3196 * boolean expression "expr != 0 ? 1 : 0".
3198 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
3199 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3204 if (pet_expr_is_comparison(expr
))
3205 return extract_comparison(expr
, pc
);
3206 if (pet_expr_is_boolean(expr
))
3207 return extract_boolean(expr
, pc
);
3209 return extract_implicit_condition(expr
, pc
);
3212 /* Check if "expr" is an assume expression and if its single argument
3213 * can be converted to an affine expression in the context of "pc".
3214 * If so, replace the argument by the affine expression.
3216 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
3217 __isl_keep pet_context
*pc
)
3220 isl_multi_pw_aff
*index
;
3224 if (!pet_expr_is_assume(expr
))
3226 if (expr
->n_arg
!= 1)
3227 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3228 "expecting one argument", return pet_expr_free(expr
));
3230 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3232 return pet_expr_free(expr
);
3233 if (isl_pw_aff_involves_nan(cond
)) {
3234 isl_pw_aff_free(cond
);
3238 index
= isl_multi_pw_aff_from_pw_aff(cond
);
3239 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
3244 /* Return the number of bits needed to represent the type of "expr".
3245 * See the description of the type_size field of pet_expr.
3247 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
3249 return expr
? expr
->type_size
: 0;
3252 /* Replace the number of bits needed to represent the type of "expr"
3254 * See the description of the type_size field of pet_expr.
3256 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
3259 expr
= pet_expr_cow(expr
);
3263 expr
->type_size
= type_size
;
3268 /* Extend an access expression "expr" with an additional index "index".
3269 * In particular, add "index" as an extra argument to "expr" and
3270 * adjust the index expression of "expr" to refer to this extra argument.
3271 * The caller is responsible for calling pet_expr_access_set_depth
3272 * to update the corresponding access relation.
3274 * Note that we only collect the individual index expressions as
3275 * arguments of "expr" here.
3276 * An attempt to integrate them into the index expression of "expr"
3277 * is performed in pet_expr_access_plug_in_args.
3279 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
3280 __isl_take pet_expr
*index
)
3284 isl_local_space
*ls
;
3287 expr
= pet_expr_cow(expr
);
3288 if (!expr
|| !index
)
3290 if (expr
->type
!= pet_expr_access
)
3291 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3292 "not an access pet_expr", goto error
);
3294 n
= pet_expr_get_n_arg(expr
);
3295 expr
= pet_expr_insert_arg(expr
, n
, index
);
3299 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3300 ls
= isl_local_space_from_space(space
);
3301 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
3302 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
3303 if (!expr
->acc
.index
)
3304 return pet_expr_free(expr
);
3308 pet_expr_free(expr
);
3309 pet_expr_free(index
);
3313 /* Extend an access expression "expr" with an additional member acces to "id".
3314 * In particular, extend the index expression of "expr" to include
3315 * the additional member access.
3316 * The caller is responsible for calling pet_expr_access_set_depth
3317 * to update the corresponding access relation.
3319 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
3320 __isl_take isl_id
*id
)
3323 isl_multi_pw_aff
*field_access
;
3325 expr
= pet_expr_cow(expr
);
3328 if (expr
->type
!= pet_expr_access
)
3329 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3330 "not an access pet_expr", goto error
);
3332 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3333 space
= isl_space_from_domain(space
);
3334 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
3335 field_access
= isl_multi_pw_aff_zero(space
);
3336 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
3337 if (!expr
->acc
.index
)
3338 return pet_expr_free(expr
);
3342 pet_expr_free(expr
);
3347 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
3354 fprintf(stderr
, "%*s", indent
, "");
3356 switch (expr
->type
) {
3357 case pet_expr_double
:
3358 fprintf(stderr
, "%s\n", expr
->d
.s
);
3361 isl_val_dump(expr
->i
);
3363 case pet_expr_access
:
3364 if (expr
->acc
.ref_id
) {
3365 isl_id_dump(expr
->acc
.ref_id
);
3366 fprintf(stderr
, "%*s", indent
, "");
3368 isl_multi_pw_aff_dump(expr
->acc
.index
);
3369 fprintf(stderr
, "%*sdepth: %d\n", indent
+ 2,
3370 "", expr
->acc
.depth
);
3371 if (expr
->acc
.kill
) {
3372 fprintf(stderr
, "%*skill: 1\n", indent
+ 2, "");
3374 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
3375 "", expr
->acc
.read
);
3376 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
3377 "", expr
->acc
.write
);
3379 if (expr
->acc
.access
[pet_expr_access_may_read
]) {
3380 fprintf(stderr
, "%*smay_read: ", indent
+ 2, "");
3382 expr
->acc
.access
[pet_expr_access_may_read
]);
3384 if (expr
->acc
.access
[pet_expr_access_may_write
]) {
3385 fprintf(stderr
, "%*smay_write: ", indent
+ 2, "");
3387 expr
->acc
.access
[pet_expr_access_may_write
]);
3389 if (expr
->acc
.access
[pet_expr_access_must_write
]) {
3390 fprintf(stderr
, "%*smust_write: ", indent
+ 2, "");
3392 expr
->acc
.access
[pet_expr_access_must_write
]);
3394 for (i
= 0; i
< expr
->n_arg
; ++i
)
3395 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3398 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
3399 for (i
= 0; i
< expr
->n_arg
; ++i
)
3400 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3403 fprintf(stderr
, "%s/%d\n", expr
->c
.name
, expr
->n_arg
);
3404 for (i
= 0; i
< expr
->n_arg
; ++i
)
3405 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3406 if (expr
->c
.summary
) {
3407 fprintf(stderr
, "%*s", indent
, "");
3408 fprintf(stderr
, "summary:\n");
3409 pet_function_summary_dump_with_indent(expr
->c
.summary
,
3414 fprintf(stderr
, "(%s)\n", expr
->type_name
);
3415 for (i
= 0; i
< expr
->n_arg
; ++i
)
3416 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3418 case pet_expr_error
:
3419 fprintf(stderr
, "ERROR\n");
3424 void pet_expr_dump(__isl_keep pet_expr
*expr
)
3426 pet_expr_dump_with_indent(expr
, 0);