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
37 #include <isl/union_set.h>
46 #include "value_bounds.h"
49 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
51 static char *type_str
[] = {
52 [pet_expr_access
] = "access",
53 [pet_expr_call
] = "call",
54 [pet_expr_cast
] = "cast",
55 [pet_expr_double
] = "double",
56 [pet_expr_int
] = "int",
60 static char *op_str
[] = {
61 [pet_op_add_assign
] = "+=",
62 [pet_op_sub_assign
] = "-=",
63 [pet_op_mul_assign
] = "*=",
64 [pet_op_div_assign
] = "/=",
65 [pet_op_assign
] = "=",
80 [pet_op_post_inc
] = "++",
81 [pet_op_post_dec
] = "--",
82 [pet_op_pre_inc
] = "++",
83 [pet_op_pre_dec
] = "--",
84 [pet_op_address_of
] = "&",
93 [pet_op_assume
] = "assume",
94 [pet_op_kill
] = "kill"
97 const char *pet_op_str(enum pet_op_type op
)
102 int pet_op_is_inc_dec(enum pet_op_type op
)
104 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
105 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
108 const char *pet_type_str(enum pet_expr_type type
)
110 return type_str
[type
];
113 enum pet_op_type
pet_str_op(const char *str
)
117 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
118 if (!strcmp(op_str
[i
], str
))
124 enum pet_expr_type
pet_str_type(const char *str
)
128 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
129 if (!strcmp(type_str
[i
], str
))
135 /* Construct a pet_expr of the given type.
137 __isl_give pet_expr
*pet_expr_alloc(isl_ctx
*ctx
, enum pet_expr_type type
)
141 expr
= isl_calloc_type(ctx
, struct pet_expr
);
153 /* Construct an access pet_expr from an index expression.
154 * By default, the access is considered to be a read access.
155 * The initial depth is set from the index expression and
156 * may still be updated by the caller before the access relation
159 __isl_give pet_expr
*pet_expr_from_index(__isl_take isl_multi_pw_aff
*index
)
166 ctx
= isl_multi_pw_aff_get_ctx(index
);
167 expr
= pet_expr_alloc(ctx
, pet_expr_access
);
174 expr
= pet_expr_access_set_index(expr
, index
);
178 isl_multi_pw_aff_free(index
);
182 /* Extend the range of "access" with "n" dimensions, retaining
183 * the tuple identifier on this range.
185 * If "access" represents a member access, then extend the range
188 static __isl_give isl_map
*extend_range(__isl_take isl_map
*access
, int n
)
192 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
194 if (!isl_map_range_is_wrapping(access
)) {
195 access
= isl_map_add_dims(access
, isl_dim_out
, n
);
199 domain
= isl_map_copy(access
);
200 domain
= isl_map_range_factor_domain(domain
);
201 access
= isl_map_range_factor_range(access
);
202 access
= extend_range(access
, n
);
203 access
= isl_map_range_product(domain
, access
);
206 access
= isl_map_set_tuple_id(access
, isl_dim_out
, id
);
211 /* Does the access expression "expr" have any explicit access relation?
213 static int has_any_access_relation(__isl_keep pet_expr
*expr
)
215 enum pet_expr_access_type type
;
220 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
)
221 if (expr
->acc
.access
[type
])
227 /* Are all relevant access relations explicitly available in "expr"?
229 static int has_relevant_access_relations(__isl_keep pet_expr
*expr
)
231 enum pet_expr_access_type type
;
236 if (expr
->acc
.kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
238 if (expr
->acc
.read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
240 if (expr
->acc
.write
&&
241 (!expr
->acc
.access
[pet_expr_access_may_write
] ||
242 !expr
->acc
.access
[pet_expr_access_must_write
]))
248 /* Replace the depth of the access expr "expr" by "depth".
250 * To avoid inconsistencies between the depth and the access relation,
251 * we currently do not allow the depth to change once the access relation
252 * has been set or computed.
254 __isl_give pet_expr
*pet_expr_access_set_depth(__isl_take pet_expr
*expr
,
262 if (expr
->acc
.depth
== depth
)
264 if (has_any_access_relation(expr
))
265 isl_die(pet_expr_get_ctx(expr
), isl_error_unsupported
,
266 "depth cannot be changed after access relation "
267 "has been set or computed", return pet_expr_free(expr
));
269 expr
= pet_expr_cow(expr
);
272 expr
->acc
.depth
= depth
;
277 /* Construct a pet_expr that kills the elements specified by
278 * the index expression "index" and the access relation "access".
280 __isl_give pet_expr
*pet_expr_kill_from_access_and_index(
281 __isl_take isl_map
*access
, __isl_take isl_multi_pw_aff
*index
)
286 if (!access
|| !index
)
289 expr
= pet_expr_from_index(index
);
290 expr
= pet_expr_access_set_read(expr
, 0);
291 expr
= pet_expr_access_set_kill(expr
, 1);
292 depth
= isl_map_dim(access
, isl_dim_out
);
293 expr
= pet_expr_access_set_depth(expr
, depth
);
294 expr
= pet_expr_access_set_access(expr
, pet_expr_access_killed
,
295 isl_union_map_from_map(access
));
296 return pet_expr_new_unary(0, pet_op_kill
, expr
);
298 isl_map_free(access
);
299 isl_multi_pw_aff_free(index
);
303 /* Construct a unary pet_expr that performs "op" on "arg",
304 * where the result is represented using a type of "type_size" bits
305 * (may be zero if unknown or if the type is not an integer).
307 __isl_give pet_expr
*pet_expr_new_unary(int type_size
, enum pet_op_type op
,
308 __isl_take pet_expr
*arg
)
315 ctx
= pet_expr_get_ctx(arg
);
316 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
317 expr
= pet_expr_set_n_arg(expr
, 1);
322 expr
->type_size
= type_size
;
323 expr
->args
[pet_un_arg
] = arg
;
331 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
332 * where the result is represented using a type of "type_size" bits
333 * (may be zero if unknown or if the type is not an integer).
335 __isl_give pet_expr
*pet_expr_new_binary(int type_size
, enum pet_op_type op
,
336 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
343 ctx
= pet_expr_get_ctx(lhs
);
344 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
345 expr
= pet_expr_set_n_arg(expr
, 2);
350 expr
->type_size
= type_size
;
351 expr
->args
[pet_bin_lhs
] = lhs
;
352 expr
->args
[pet_bin_rhs
] = rhs
;
361 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
363 __isl_give pet_expr
*pet_expr_new_ternary(__isl_take pet_expr
*cond
,
364 __isl_take pet_expr
*lhs
, __isl_take pet_expr
*rhs
)
369 if (!cond
|| !lhs
|| !rhs
)
371 ctx
= pet_expr_get_ctx(cond
);
372 expr
= pet_expr_alloc(ctx
, pet_expr_op
);
373 expr
= pet_expr_set_n_arg(expr
, 3);
377 expr
->op
= pet_op_cond
;
378 expr
->args
[pet_ter_cond
] = cond
;
379 expr
->args
[pet_ter_true
] = lhs
;
380 expr
->args
[pet_ter_false
] = rhs
;
390 /* Construct a call pet_expr that calls function "name" with "n_arg"
391 * arguments. The caller is responsible for filling in the arguments.
393 __isl_give pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
398 expr
= pet_expr_alloc(ctx
, pet_expr_call
);
399 expr
= pet_expr_set_n_arg(expr
, n_arg
);
403 expr
->c
.name
= strdup(name
);
405 return pet_expr_free(expr
);
410 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
412 __isl_give pet_expr
*pet_expr_new_cast(const char *type_name
,
413 __isl_take pet_expr
*arg
)
421 ctx
= pet_expr_get_ctx(arg
);
422 expr
= pet_expr_alloc(ctx
, pet_expr_cast
);
423 expr
= pet_expr_set_n_arg(expr
, 1);
427 expr
->type_name
= strdup(type_name
);
428 if (!expr
->type_name
)
440 /* Construct a pet_expr that represents the double "d".
442 __isl_give pet_expr
*pet_expr_new_double(isl_ctx
*ctx
,
443 double val
, const char *s
)
447 expr
= pet_expr_alloc(ctx
, pet_expr_double
);
452 expr
->d
.s
= strdup(s
);
454 return pet_expr_free(expr
);
459 /* Construct a pet_expr that represents the integer value "v".
461 __isl_give pet_expr
*pet_expr_new_int(__isl_take isl_val
*v
)
469 ctx
= isl_val_get_ctx(v
);
470 expr
= pet_expr_alloc(ctx
, pet_expr_int
);
482 /* Return an independent duplicate of "expr".
484 * In case of an access expression, make sure the depth of the duplicate is set
485 * before the access relation (if any) is set and after the index expression
488 static __isl_give pet_expr
*pet_expr_dup(__isl_keep pet_expr
*expr
)
492 enum pet_expr_access_type type
;
497 dup
= pet_expr_alloc(expr
->ctx
, expr
->type
);
498 dup
= pet_expr_set_type_size(dup
, expr
->type_size
);
499 dup
= pet_expr_set_n_arg(dup
, expr
->n_arg
);
500 for (i
= 0; i
< expr
->n_arg
; ++i
)
501 dup
= pet_expr_set_arg(dup
, i
, pet_expr_copy(expr
->args
[i
]));
503 switch (expr
->type
) {
504 case pet_expr_access
:
505 if (expr
->acc
.ref_id
)
506 dup
= pet_expr_access_set_ref_id(dup
,
507 isl_id_copy(expr
->acc
.ref_id
));
508 dup
= pet_expr_access_set_index(dup
,
509 isl_multi_pw_aff_copy(expr
->acc
.index
));
510 dup
= pet_expr_access_set_depth(dup
, expr
->acc
.depth
);
511 for (type
= pet_expr_access_begin
;
512 type
< pet_expr_access_end
; ++type
) {
513 if (!expr
->acc
.access
[type
])
515 dup
= pet_expr_access_set_access(dup
, type
,
516 isl_union_map_copy(expr
->acc
.access
[type
]));
518 dup
= pet_expr_access_set_read(dup
, expr
->acc
.read
);
519 dup
= pet_expr_access_set_write(dup
, expr
->acc
.write
);
520 dup
= pet_expr_access_set_kill(dup
, expr
->acc
.kill
);
523 dup
= pet_expr_call_set_name(dup
, expr
->c
.name
);
525 dup
= pet_expr_call_set_summary(dup
,
526 pet_function_summary_copy(expr
->c
.summary
));
529 dup
= pet_expr_cast_set_type_name(dup
, expr
->type_name
);
531 case pet_expr_double
:
532 dup
= pet_expr_double_set(dup
, expr
->d
.val
, expr
->d
.s
);
535 dup
= pet_expr_int_set_val(dup
, isl_val_copy(expr
->i
));
538 dup
= pet_expr_op_set_type(dup
, expr
->op
);
541 dup
= pet_expr_free(dup
);
548 __isl_give pet_expr
*pet_expr_cow(__isl_take pet_expr
*expr
)
556 return pet_expr_dup(expr
);
559 __isl_null pet_expr
*pet_expr_free(__isl_take pet_expr
*expr
)
561 enum pet_expr_access_type type
;
569 for (i
= 0; i
< expr
->n_arg
; ++i
)
570 pet_expr_free(expr
->args
[i
]);
573 switch (expr
->type
) {
574 case pet_expr_access
:
575 isl_id_free(expr
->acc
.ref_id
);
576 for (type
= pet_expr_access_begin
;
577 type
< pet_expr_access_end
; ++type
)
578 isl_union_map_free(expr
->acc
.access
[type
]);
579 isl_multi_pw_aff_free(expr
->acc
.index
);
583 pet_function_summary_free(expr
->c
.summary
);
586 free(expr
->type_name
);
588 case pet_expr_double
:
592 isl_val_free(expr
->i
);
599 isl_ctx_deref(expr
->ctx
);
604 /* Return an additional reference to "expr".
606 __isl_give pet_expr
*pet_expr_copy(__isl_keep pet_expr
*expr
)
615 /* Return the isl_ctx in which "expr" was created.
617 isl_ctx
*pet_expr_get_ctx(__isl_keep pet_expr
*expr
)
619 return expr
? expr
->ctx
: NULL
;
622 /* Return the type of "expr".
624 enum pet_expr_type
pet_expr_get_type(__isl_keep pet_expr
*expr
)
627 return pet_expr_error
;
631 /* Return the number of arguments of "expr".
633 int pet_expr_get_n_arg(__isl_keep pet_expr
*expr
)
641 /* Set the number of arguments of "expr" to "n".
643 * If "expr" originally had more arguments, then remove the extra arguments.
644 * If "expr" originally had fewer arguments, then create space for
645 * the extra arguments ans initialize them to NULL.
647 __isl_give pet_expr
*pet_expr_set_n_arg(__isl_take pet_expr
*expr
, int n
)
654 if (expr
->n_arg
== n
)
656 expr
= pet_expr_cow(expr
);
660 if (n
< expr
->n_arg
) {
661 for (i
= n
; i
< expr
->n_arg
; ++i
)
662 pet_expr_free(expr
->args
[i
]);
667 args
= isl_realloc_array(expr
->ctx
, expr
->args
, pet_expr
*, n
);
669 return pet_expr_free(expr
);
671 for (i
= expr
->n_arg
; i
< n
; ++i
)
672 expr
->args
[i
] = NULL
;
678 /* Return the argument of "expr" at position "pos".
680 __isl_give pet_expr
*pet_expr_get_arg(__isl_keep pet_expr
*expr
, int pos
)
684 if (pos
< 0 || pos
>= expr
->n_arg
)
685 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
686 "position out of bounds", return NULL
);
688 return pet_expr_copy(expr
->args
[pos
]);
691 /* Replace "expr" by its argument at position "pos".
693 __isl_give pet_expr
*pet_expr_arg(__isl_take pet_expr
*expr
, int pos
)
697 arg
= pet_expr_get_arg(expr
, pos
);
703 /* Replace the argument of "expr" at position "pos" by "arg".
705 __isl_give pet_expr
*pet_expr_set_arg(__isl_take pet_expr
*expr
, int pos
,
706 __isl_take pet_expr
*arg
)
710 if (pos
< 0 || pos
>= expr
->n_arg
)
711 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
712 "position out of bounds", goto error
);
713 if (expr
->args
[pos
] == arg
) {
718 expr
= pet_expr_cow(expr
);
722 pet_expr_free(expr
->args
[pos
]);
723 expr
->args
[pos
] = arg
;
732 /* Does "expr" perform a comparison operation?
734 int pet_expr_is_comparison(__isl_keep pet_expr
*expr
)
738 if (expr
->type
!= pet_expr_op
)
753 /* Does "expr" perform a boolean operation?
755 int pet_expr_is_boolean(__isl_keep pet_expr
*expr
)
759 if (expr
->type
!= pet_expr_op
)
771 /* Is "expr" an address-of operation?
773 int pet_expr_is_address_of(__isl_keep pet_expr
*expr
)
777 if (expr
->type
!= pet_expr_op
)
779 return expr
->op
== pet_op_address_of
;
782 /* Is "expr" an assume statement?
784 int pet_expr_is_assume(__isl_keep pet_expr
*expr
)
788 if (expr
->type
!= pet_expr_op
)
790 return expr
->op
== pet_op_assume
;
793 /* Does "expr" perform a min operation?
795 int pet_expr_is_min(__isl_keep pet_expr
*expr
)
799 if (expr
->type
!= pet_expr_call
)
801 if (expr
->n_arg
!= 2)
803 if (strcmp(expr
->c
.name
, "min") != 0)
808 /* Does "expr" perform a max operation?
810 int pet_expr_is_max(__isl_keep pet_expr
*expr
)
814 if (expr
->type
!= pet_expr_call
)
816 if (expr
->n_arg
!= 2)
818 if (strcmp(expr
->c
.name
, "max") != 0)
823 /* Does "expr" represent an access to an unnamed space, i.e.,
824 * does it represent an affine expression?
826 int pet_expr_is_affine(__isl_keep pet_expr
*expr
)
832 if (expr
->type
!= pet_expr_access
)
835 has_id
= isl_multi_pw_aff_has_tuple_id(expr
->acc
.index
, isl_dim_out
);
842 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
843 * not part of any struct?
845 int pet_expr_is_scalar_access(__isl_keep pet_expr
*expr
)
849 if (expr
->type
!= pet_expr_access
)
851 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
))
854 return expr
->acc
.depth
== 0;
857 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
860 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
861 __isl_keep isl_multi_pw_aff
*mpa2
)
865 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
866 if (equal
< 0 || equal
)
868 mpa2
= isl_multi_pw_aff_copy(mpa2
);
869 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
870 isl_multi_pw_aff_get_space(mpa1
));
871 equal
= isl_multi_pw_aff_plain_is_equal(mpa1
, mpa2
);
872 isl_multi_pw_aff_free(mpa2
);
877 /* Construct an access relation from the index expression and
878 * the array depth of the access expression "expr".
880 * If the number of indices is smaller than the depth of the array,
881 * then we assume that all elements of the remaining dimensions
884 static __isl_give isl_union_map
*construct_access_relation(
885 __isl_keep pet_expr
*expr
)
894 access
= isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr
));
898 dim
= isl_map_dim(access
, isl_dim_out
);
899 if (dim
> expr
->acc
.depth
)
900 isl_die(isl_map_get_ctx(access
), isl_error_internal
,
901 "number of indices greater than depth",
902 access
= isl_map_free(access
));
904 if (dim
!= expr
->acc
.depth
)
905 access
= extend_range(access
, expr
->acc
.depth
- dim
);
907 return isl_union_map_from_map(access
);
910 /* Ensure that all relevant access relations are explicitly
911 * available in "expr".
913 * If "expr" does not already have the relevant access relations, then create
914 * them based on the index expression and the array depth.
916 * We do not cow since adding an explicit access relation
917 * does not change the meaning of the expression.
919 static __isl_give pet_expr
*introduce_access_relations(
920 __isl_take pet_expr
*expr
)
922 enum pet_expr_access_type type
;
923 isl_union_map
*access
;
925 int kill
, read
, write
;
929 if (has_relevant_access_relations(expr
))
932 access
= construct_access_relation(expr
);
934 return pet_expr_free(expr
);
936 kill
= expr
->acc
.kill
;
937 read
= expr
->acc
.read
;
938 write
= expr
->acc
.write
;
939 if (kill
&& !expr
->acc
.access
[pet_expr_access_fake_killed
])
940 expr
->acc
.access
[pet_expr_access_fake_killed
] =
941 isl_union_map_copy(access
);
942 if (read
&& !expr
->acc
.access
[pet_expr_access_may_read
])
943 expr
->acc
.access
[pet_expr_access_may_read
] =
944 isl_union_map_copy(access
);
945 if (write
&& !expr
->acc
.access
[pet_expr_access_may_write
])
946 expr
->acc
.access
[pet_expr_access_may_write
] =
947 isl_union_map_copy(access
);
948 if (write
&& !expr
->acc
.access
[pet_expr_access_must_write
])
949 expr
->acc
.access
[pet_expr_access_must_write
] =
950 isl_union_map_copy(access
);
952 isl_union_map_free(access
);
954 if (!has_relevant_access_relations(expr
))
955 return pet_expr_free(expr
);
960 /* Return 1 if the two pet_exprs are equivalent.
962 int pet_expr_is_equal(__isl_keep pet_expr
*expr1
, __isl_keep pet_expr
*expr2
)
965 enum pet_expr_access_type type
;
967 if (!expr1
|| !expr2
)
970 if (expr1
->type
!= expr2
->type
)
972 if (expr1
->n_arg
!= expr2
->n_arg
)
974 for (i
= 0; i
< expr1
->n_arg
; ++i
)
975 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
977 switch (expr1
->type
) {
980 case pet_expr_double
:
981 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
983 if (expr1
->d
.val
!= expr2
->d
.val
)
987 if (!isl_val_eq(expr1
->i
, expr2
->i
))
990 case pet_expr_access
:
991 if (expr1
->acc
.read
!= expr2
->acc
.read
)
993 if (expr1
->acc
.write
!= expr2
->acc
.write
)
995 if (expr1
->acc
.kill
!= expr2
->acc
.kill
)
997 if (expr1
->acc
.ref_id
!= expr2
->acc
.ref_id
)
999 if (!expr1
->acc
.index
|| !expr2
->acc
.index
)
1001 if (!multi_pw_aff_is_equal(expr1
->acc
.index
, expr2
->acc
.index
))
1003 if (expr1
->acc
.depth
!= expr2
->acc
.depth
)
1005 if (has_relevant_access_relations(expr1
) !=
1006 has_relevant_access_relations(expr2
)) {
1008 expr1
= pet_expr_copy(expr1
);
1009 expr2
= pet_expr_copy(expr2
);
1010 expr1
= introduce_access_relations(expr1
);
1011 expr2
= introduce_access_relations(expr2
);
1012 equal
= pet_expr_is_equal(expr1
, expr2
);
1013 pet_expr_free(expr1
);
1014 pet_expr_free(expr2
);
1017 for (type
= pet_expr_access_begin
;
1018 type
< pet_expr_access_end
; ++type
) {
1019 if (!expr1
->acc
.access
[type
] !=
1020 !expr2
->acc
.access
[type
])
1022 if (!expr1
->acc
.access
[type
])
1024 if (!isl_union_map_is_equal(expr1
->acc
.access
[type
],
1025 expr2
->acc
.access
[type
]))
1030 if (expr1
->op
!= expr2
->op
)
1034 if (strcmp(expr1
->c
.name
, expr2
->c
.name
))
1038 if (strcmp(expr1
->type_name
, expr2
->type_name
))
1046 /* Does the access expression "expr" read the accessed elements?
1048 int pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
1052 if (expr
->type
!= pet_expr_access
)
1053 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1054 "not an access expression", return -1);
1056 return expr
->acc
.read
;
1059 /* Does the access expression "expr" write to the accessed elements?
1061 int pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
1065 if (expr
->type
!= pet_expr_access
)
1066 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1067 "not an access expression", return -1);
1069 return expr
->acc
.write
;
1072 /* Return the identifier of the array accessed by "expr".
1074 * If "expr" represents a member access, then return the identifier
1075 * of the outer structure array.
1077 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
1081 if (expr
->type
!= pet_expr_access
)
1082 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1083 "not an access expression", return NULL
);
1085 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
)) {
1089 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1090 space
= isl_space_range(space
);
1091 while (space
&& isl_space_is_wrapping(space
))
1092 space
= isl_space_domain(isl_space_unwrap(space
));
1093 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
1094 isl_space_free(space
);
1099 return isl_multi_pw_aff_get_tuple_id(expr
->acc
.index
, isl_dim_out
);
1102 /* Return the parameter space of "expr".
1104 __isl_give isl_space
*pet_expr_access_get_parameter_space(
1105 __isl_keep pet_expr
*expr
)
1111 if (expr
->type
!= pet_expr_access
)
1112 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1113 "not an access expression", return NULL
);
1115 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1116 space
= isl_space_params(space
);
1121 /* Return the domain space of "expr", including the arguments (if any).
1123 __isl_give isl_space
*pet_expr_access_get_augmented_domain_space(
1124 __isl_keep pet_expr
*expr
)
1130 if (expr
->type
!= pet_expr_access
)
1131 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1132 "not an access expression", return NULL
);
1134 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1135 space
= isl_space_domain(space
);
1140 /* Return the domain space of "expr", without the arguments (if any).
1142 __isl_give isl_space
*pet_expr_access_get_domain_space(
1143 __isl_keep pet_expr
*expr
)
1147 space
= pet_expr_access_get_augmented_domain_space(expr
);
1148 if (isl_space_is_wrapping(space
))
1149 space
= isl_space_domain(isl_space_unwrap(space
));
1154 /* Return the space of the data accessed by "expr".
1156 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
1162 if (expr
->type
!= pet_expr_access
)
1163 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1164 "not an access expression", return NULL
);
1166 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1167 space
= isl_space_range(space
);
1172 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1174 static __isl_give pet_expr
*pet_expr_map_expr_of_type(__isl_take pet_expr
*expr
,
1175 enum pet_expr_type type
,
1176 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1181 n
= pet_expr_get_n_arg(expr
);
1182 for (i
= 0; i
< n
; ++i
) {
1183 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1184 arg
= pet_expr_map_expr_of_type(arg
, type
, fn
, user
);
1185 expr
= pet_expr_set_arg(expr
, i
, arg
);
1191 if (expr
->type
== type
)
1192 expr
= fn(expr
, user
);
1197 /* Modify all expressions of type pet_expr_access in "expr"
1198 * by calling "fn" on them.
1200 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
1201 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1204 return pet_expr_map_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1207 /* Modify all expressions of type pet_expr_call in "expr"
1208 * by calling "fn" on them.
1210 __isl_give pet_expr
*pet_expr_map_call(__isl_take pet_expr
*expr
,
1211 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1214 return pet_expr_map_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1217 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1219 * Return -1 on error (where fn returning a negative value is treated as
1221 * Otherwise return 0.
1223 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
1224 enum pet_expr_type type
,
1225 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1232 for (i
= 0; i
< expr
->n_arg
; ++i
)
1233 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1234 type
, fn
, user
) < 0)
1237 if (expr
->type
== type
)
1238 return fn(expr
, user
);
1243 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1245 * Return -1 on error (where fn returning a negative value is treated as
1247 * Otherwise return 0.
1249 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1250 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1252 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1255 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1257 * Return -1 on error (where fn returning a negative value is treated as
1259 * Otherwise return 0.
1261 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1262 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1264 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1267 /* Internal data structure for pet_expr_writes.
1268 * "id" is the identifier that we are looking for.
1269 * "found" is set if we have found the identifier being written to.
1271 struct pet_expr_writes_data
{
1276 /* Given an access expression, check if it writes to data->id.
1277 * If so, set data->found and abort the search.
1279 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1281 struct pet_expr_writes_data
*data
= user
;
1284 if (!expr
->acc
.write
)
1286 if (pet_expr_is_affine(expr
))
1289 write_id
= pet_expr_access_get_id(expr
);
1290 isl_id_free(write_id
);
1295 if (write_id
!= data
->id
)
1302 /* Does expression "expr" write to "id"?
1304 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1306 struct pet_expr_writes_data data
;
1310 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1317 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1318 * index expression and access relations of "expr" (if any)
1319 * to dimensions of "dst_type" at "dst_pos".
1321 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1322 enum isl_dim_type dst_type
, unsigned dst_pos
,
1323 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1325 enum pet_expr_access_type type
;
1327 expr
= pet_expr_cow(expr
);
1330 if (expr
->type
!= pet_expr_access
)
1331 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1332 "not an access pet_expr", return pet_expr_free(expr
));
1334 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1335 if (!expr
->acc
.access
[type
])
1337 expr
->acc
.access
[type
] =
1338 pet_union_map_move_dims(expr
->acc
.access
[type
],
1339 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1340 if (!expr
->acc
.access
[type
])
1343 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1344 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1345 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1346 return pet_expr_free(expr
);
1351 /* Replace the index expression and access relations (if any) of "expr"
1352 * by their preimages under the function represented by "ma".
1354 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1355 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1357 enum pet_expr_access_type type
;
1359 expr
= pet_expr_cow(expr
);
1362 if (expr
->type
!= pet_expr_access
)
1363 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1364 "not an access pet_expr", goto error
);
1366 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1367 if (!expr
->acc
.access
[type
])
1369 expr
->acc
.access
[type
] =
1370 isl_union_map_preimage_domain_multi_aff(
1371 expr
->acc
.access
[type
], isl_multi_aff_copy(ma
));
1372 if (!expr
->acc
.access
[type
])
1375 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1377 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1378 return pet_expr_free(expr
);
1382 isl_multi_aff_free(ma
);
1383 pet_expr_free(expr
);
1387 /* Replace the index expression and access relations (if any) of "expr"
1388 * by their preimages under the function represented by "mpa".
1390 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1391 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1393 enum pet_expr_access_type type
;
1395 expr
= pet_expr_cow(expr
);
1398 if (expr
->type
!= pet_expr_access
)
1399 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1400 "not an access pet_expr", goto error
);
1402 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1403 if (!expr
->acc
.access
[type
])
1405 expr
->acc
.access
[type
] =
1406 isl_union_map_preimage_domain_multi_pw_aff(
1407 expr
->acc
.access
[type
], isl_multi_pw_aff_copy(mpa
));
1408 if (!expr
->acc
.access
[type
])
1411 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1412 expr
->acc
.index
, mpa
);
1413 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1414 return pet_expr_free(expr
);
1418 isl_multi_pw_aff_free(mpa
);
1419 pet_expr_free(expr
);
1423 /* Return the index expression of access expression "expr".
1425 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1426 __isl_keep pet_expr
*expr
)
1430 if (expr
->type
!= pet_expr_access
)
1431 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1432 "not an access expression", return NULL
);
1434 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1437 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1439 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1442 enum pet_expr_access_type type
;
1444 expr
= pet_expr_cow(expr
);
1447 if (expr
->type
!= pet_expr_access
)
1448 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1449 "not an access expression", return pet_expr_free(expr
));
1451 if (!has_any_access_relation(expr
))
1454 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1455 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1456 if (!expr
->acc
.access
[type
])
1458 space
= isl_space_align_params(space
,
1459 isl_union_map_get_space(expr
->acc
.access
[type
]));
1461 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1462 isl_space_copy(space
));
1463 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1464 if (!expr
->acc
.access
[type
])
1466 expr
->acc
.access
[type
] =
1467 isl_union_map_align_params(expr
->acc
.access
[type
],
1468 isl_space_copy(space
));
1469 if (!expr
->acc
.access
[type
])
1472 isl_space_free(space
);
1473 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1474 return pet_expr_free(expr
);
1479 /* Are "expr1" and "expr2" both array accesses such that
1480 * the access relation of "expr1" is a subset of that of "expr2"?
1481 * Only take into account the first "n_arg" arguments.
1483 * This function is tailored for use by mark_self_dependences in nest.c.
1484 * In particular, the input expressions may have more than "n_arg"
1485 * elements in their arguments arrays, while only the first "n_arg"
1486 * elements are referenced from the access relations.
1488 int pet_expr_is_sub_access(__isl_keep pet_expr
*expr1
,
1489 __isl_keep pet_expr
*expr2
, int n_arg
)
1495 if (!expr1
|| !expr2
)
1497 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1499 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1501 if (pet_expr_is_affine(expr1
))
1503 if (pet_expr_is_affine(expr2
))
1505 n1
= pet_expr_get_n_arg(expr1
);
1508 n2
= pet_expr_get_n_arg(expr2
);
1513 for (i
= 0; i
< n1
; ++i
) {
1515 equal
= pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]);
1516 if (equal
< 0 || !equal
)
1519 id1
= pet_expr_access_get_id(expr1
);
1520 id2
= pet_expr_access_get_id(expr2
);
1528 expr1
= pet_expr_copy(expr1
);
1529 expr2
= pet_expr_copy(expr2
);
1530 expr1
= introduce_access_relations(expr1
);
1531 expr2
= introduce_access_relations(expr2
);
1532 if (!expr1
|| !expr2
)
1535 is_subset
= isl_union_map_is_subset(
1536 expr1
->acc
.access
[pet_expr_access_may_read
],
1537 expr2
->acc
.access
[pet_expr_access_may_read
]);
1539 pet_expr_free(expr1
);
1540 pet_expr_free(expr2
);
1544 pet_expr_free(expr1
);
1545 pet_expr_free(expr2
);
1549 /* Given a set in the iteration space "domain", extend it to live in the space
1550 * of the domain of access relations.
1552 * That, is the number of arguments "n" is 0, then simply return domain.
1553 * Otherwise, return [domain -> [a_1,...,a_n]].
1555 static __isl_give isl_set
*add_arguments(__isl_take isl_set
*domain
, int n
)
1562 map
= isl_map_from_domain(domain
);
1563 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
1564 return isl_map_wrap(map
);
1567 /* Add extra conditions to the domains of all access relations in "expr",
1568 * introducing access relations if they are not already present.
1570 * The conditions are not added to the index expression. Instead, they
1571 * are used to try and simplify the index expression.
1573 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1574 __isl_take isl_set
*cond
)
1577 isl_union_set
*uset
;
1578 enum pet_expr_access_type type
;
1580 expr
= pet_expr_cow(expr
);
1584 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1585 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1586 isl_set_copy(cond
));
1591 if (expr
->type
!= pet_expr_access
) {
1596 expr
= introduce_access_relations(expr
);
1600 cond
= add_arguments(cond
, expr
->n_arg
);
1601 uset
= isl_union_set_from_set(isl_set_copy(cond
));
1602 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1603 if (!expr
->acc
.access
[type
])
1605 expr
->acc
.access
[type
] =
1606 isl_union_map_intersect_domain(expr
->acc
.access
[type
],
1607 isl_union_set_copy(uset
));
1608 if (!expr
->acc
.access
[type
])
1611 isl_union_set_free(uset
);
1612 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, cond
);
1613 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1614 return pet_expr_free(expr
);
1619 return pet_expr_free(expr
);
1622 /* Modify the access relations (if any) and index expression
1623 * of the given access expression
1624 * based on the given iteration space transformation.
1625 * In particular, precompose the access relation and index expression
1626 * with the update function.
1628 * If the access has any arguments then the domain of the access relation
1629 * is a wrapped mapping from the iteration space to the space of
1630 * argument values. We only need to change the domain of this wrapped
1631 * mapping, so we extend the input transformation with an identity mapping
1632 * on the space of argument values.
1634 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1635 __isl_keep isl_multi_pw_aff
*update
)
1637 enum pet_expr_access_type type
;
1639 expr
= pet_expr_cow(expr
);
1642 if (expr
->type
!= pet_expr_access
)
1643 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1644 "not an access expression", return pet_expr_free(expr
));
1646 update
= isl_multi_pw_aff_copy(update
);
1648 if (expr
->n_arg
> 0) {
1650 isl_multi_pw_aff
*id
;
1652 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1653 space
= isl_space_domain(space
);
1654 space
= isl_space_unwrap(space
);
1655 space
= isl_space_range(space
);
1656 space
= isl_space_map_from_set(space
);
1657 id
= isl_multi_pw_aff_identity(space
);
1658 update
= isl_multi_pw_aff_product(update
, id
);
1661 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1662 if (!expr
->acc
.access
[type
])
1664 expr
->acc
.access
[type
] =
1665 isl_union_map_preimage_domain_multi_pw_aff(
1666 expr
->acc
.access
[type
],
1667 isl_multi_pw_aff_copy(update
));
1668 if (!expr
->acc
.access
[type
])
1671 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1672 expr
->acc
.index
, update
);
1673 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1674 return pet_expr_free(expr
);
1679 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1681 isl_multi_pw_aff
*update
= user
;
1683 return pet_expr_access_update_domain(expr
, update
);
1686 /* Modify all access relations in "expr" by precomposing them with
1687 * the given iteration space transformation.
1689 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1690 __isl_take isl_multi_pw_aff
*update
)
1692 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1693 isl_multi_pw_aff_free(update
);
1697 /* Given an expression with accesses that have a 0D anonymous domain,
1698 * replace those domains by "space".
1700 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1701 __isl_take isl_space
*space
)
1703 isl_multi_pw_aff
*mpa
;
1705 space
= isl_space_from_domain(space
);
1706 mpa
= isl_multi_pw_aff_zero(space
);
1707 return pet_expr_update_domain(expr
, mpa
);
1710 /* Add all parameters in "space" to the access relations (if any)
1711 * and index expression of "expr".
1713 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1715 isl_space
*space
= user
;
1716 enum pet_expr_access_type type
;
1718 expr
= pet_expr_cow(expr
);
1721 if (expr
->type
!= pet_expr_access
)
1722 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1723 "not an access expression", return pet_expr_free(expr
));
1725 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1726 if (!expr
->acc
.access
[type
])
1728 expr
->acc
.access
[type
] =
1729 isl_union_map_align_params(expr
->acc
.access
[type
],
1730 isl_space_copy(space
));
1731 if (!expr
->acc
.access
[type
])
1734 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1735 isl_space_copy(space
));
1736 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1737 return pet_expr_free(expr
);
1742 /* Add all parameters in "space" to all access relations and index expressions
1745 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1746 __isl_take isl_space
*space
)
1748 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1749 isl_space_free(space
);
1753 /* Insert an argument expression corresponding to "test" in front
1754 * of the list of arguments described by *n_arg and *args.
1756 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1757 __isl_keep isl_multi_pw_aff
*test
)
1760 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1763 return pet_expr_free(expr
);
1764 expr
= pet_expr_cow(expr
);
1769 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1771 return pet_expr_free(expr
);
1774 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1776 return pet_expr_free(expr
);
1777 for (i
= 0; i
< expr
->n_arg
; ++i
)
1778 ext
[1 + i
] = expr
->args
[i
];
1783 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1785 return pet_expr_free(expr
);
1790 /* Make the expression "expr" depend on the value of "test"
1791 * being equal to "satisfied".
1793 * If "test" is an affine expression, we simply add the conditions
1794 * on the expression having the value "satisfied" to all access relations
1795 * (introducing access relations if they are missing) and index expressions.
1797 * Otherwise, we add a filter to "expr" (which is then assumed to be
1798 * an access expression) corresponding to "test" being equal to "satisfied".
1800 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1801 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1806 isl_pw_multi_aff
*pma
;
1807 enum pet_expr_access_type type
;
1809 expr
= pet_expr_cow(expr
);
1813 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
1817 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
1818 isl_multi_pw_aff_free(test
);
1820 cond
= isl_pw_aff_non_zero_set(pa
);
1822 cond
= isl_pw_aff_zero_set(pa
);
1823 return pet_expr_restrict(expr
, cond
);
1826 ctx
= isl_multi_pw_aff_get_ctx(test
);
1827 if (expr
->type
!= pet_expr_access
)
1828 isl_die(ctx
, isl_error_invalid
,
1829 "can only filter access expressions", goto error
);
1831 expr
= introduce_access_relations(expr
);
1835 space
= isl_space_domain(isl_multi_pw_aff_get_space(expr
->acc
.index
));
1836 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1837 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
1839 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1840 if (!expr
->acc
.access
[type
])
1842 expr
->acc
.access
[type
] =
1843 isl_union_map_preimage_domain_pw_multi_aff(
1844 expr
->acc
.access
[type
],
1845 isl_pw_multi_aff_copy(pma
));
1846 if (!expr
->acc
.access
[type
])
1849 pma
= isl_pw_multi_aff_gist(pma
,
1850 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
1851 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
1852 expr
->acc
.index
, pma
);
1853 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1856 expr
= insert_access_arg(expr
, test
);
1858 isl_multi_pw_aff_free(test
);
1861 isl_multi_pw_aff_free(test
);
1862 return pet_expr_free(expr
);
1865 /* Add a reference identifier to access expression "expr".
1866 * "user" points to an integer that contains the sequence number
1867 * of the next reference.
1869 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
1876 expr
= pet_expr_cow(expr
);
1879 if (expr
->type
!= pet_expr_access
)
1880 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1881 "not an access expression", return pet_expr_free(expr
));
1883 ctx
= pet_expr_get_ctx(expr
);
1884 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
1885 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
1886 if (!expr
->acc
.ref_id
)
1887 return pet_expr_free(expr
);
1892 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
1894 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
1897 /* Reset the user pointer on all parameter and tuple ids in
1898 * the access relations (if any) and the index expression
1899 * of the access expression "expr".
1901 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
1904 enum pet_expr_access_type type
;
1906 expr
= pet_expr_cow(expr
);
1909 if (expr
->type
!= pet_expr_access
)
1910 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1911 "not an access expression", return pet_expr_free(expr
));
1913 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1914 if (!expr
->acc
.access
[type
])
1916 expr
->acc
.access
[type
] =
1917 isl_union_map_reset_user(expr
->acc
.access
[type
]);
1918 if (!expr
->acc
.access
[type
])
1921 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
1922 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1923 return pet_expr_free(expr
);
1928 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
1930 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
1933 /* Data used in access_gist() callback.
1935 struct pet_access_gist_data
{
1937 isl_union_map
*value_bounds
;
1940 /* Given an expression "expr" of type pet_expr_access, compute
1941 * the gist of the associated access relations (if any) and index expression
1942 * with respect to data->domain and the bounds on the values of the arguments
1943 * of the expression.
1945 * The arguments of "expr" have been gisted right before "expr" itself
1946 * is gisted. The gisted arguments may have become equal where before
1947 * they may not have been (obviously) equal. We therefore take
1948 * the opportunity to remove duplicate arguments here.
1950 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
1952 struct pet_access_gist_data
*data
= user
;
1954 isl_union_set
*uset
;
1955 enum pet_expr_access_type type
;
1957 expr
= pet_expr_remove_duplicate_args(expr
);
1958 expr
= pet_expr_cow(expr
);
1961 if (expr
->type
!= pet_expr_access
)
1962 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1963 "not an access expression", return pet_expr_free(expr
));
1965 domain
= isl_set_copy(data
->domain
);
1966 if (expr
->n_arg
> 0)
1967 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
1968 data
->value_bounds
);
1970 uset
= isl_union_set_from_set(isl_set_copy(domain
));
1971 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1972 if (!expr
->acc
.access
[type
])
1974 expr
->acc
.access
[type
] =
1975 isl_union_map_gist_domain(expr
->acc
.access
[type
],
1976 isl_union_set_copy(uset
));
1977 if (!expr
->acc
.access
[type
])
1980 isl_union_set_free(uset
);
1981 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
1982 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1983 return pet_expr_free(expr
);
1988 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
1989 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
1991 struct pet_access_gist_data data
= { context
, value_bounds
};
1993 return pet_expr_map_access(expr
, &access_gist
, &data
);
1996 /* Mark "expr" as a read dependening on "read".
1998 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
2002 return pet_expr_free(expr
);
2003 if (expr
->type
!= pet_expr_access
)
2004 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2005 "not an access expression", return pet_expr_free(expr
));
2006 if (expr
->acc
.read
== read
)
2008 expr
= pet_expr_cow(expr
);
2011 expr
->acc
.read
= read
;
2016 /* Mark "expr" as a write dependening on "write".
2018 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
2022 return pet_expr_free(expr
);
2023 if (expr
->type
!= pet_expr_access
)
2024 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2025 "not an access expression", return pet_expr_free(expr
));
2026 if (expr
->acc
.write
== write
)
2028 expr
= pet_expr_cow(expr
);
2031 expr
->acc
.write
= write
;
2036 /* Mark "expr" as a kill dependening on "kill".
2038 __isl_give pet_expr
*pet_expr_access_set_kill(__isl_take pet_expr
*expr
,
2042 return pet_expr_free(expr
);
2043 if (expr
->type
!= pet_expr_access
)
2044 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2045 "not an access expression", return pet_expr_free(expr
));
2046 if (expr
->acc
.kill
== kill
)
2048 expr
= pet_expr_cow(expr
);
2051 expr
->acc
.kill
= kill
;
2056 /* Map the access type "type" to the corresponding location
2057 * in the access array.
2058 * In particular, the access relation of type pet_expr_access_killed is
2059 * stored in the element at position pet_expr_access_fake_killed.
2061 static enum pet_expr_access_type
internalize_type(
2062 enum pet_expr_access_type type
)
2064 if (type
== pet_expr_access_killed
)
2065 return pet_expr_access_fake_killed
;
2069 /* Replace the access relation of the given "type" of "expr" by "access".
2070 * If the access relation is non-empty and the type is a read or a write,
2071 * then also mark the access expression itself as a read or a write.
2073 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
2074 enum pet_expr_access_type type
, __isl_take isl_union_map
*access
)
2078 expr
= pet_expr_cow(expr
);
2079 if (!expr
|| !access
)
2081 if (expr
->type
!= pet_expr_access
)
2082 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2083 "not an access expression", goto error
);
2084 type
= internalize_type(type
);
2085 isl_union_map_free(expr
->acc
.access
[type
]);
2086 expr
->acc
.access
[type
] = access
;
2091 empty
= isl_union_map_is_empty(access
);
2093 return pet_expr_free(expr
);
2097 if (type
== pet_expr_access_may_read
)
2098 expr
= pet_expr_access_set_read(expr
, 1);
2100 expr
= pet_expr_access_set_write(expr
, 1);
2104 isl_union_map_free(access
);
2105 pet_expr_free(expr
);
2109 /* Replace the index expression of "expr" by "index" and
2110 * set the array depth accordingly.
2112 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
2113 __isl_take isl_multi_pw_aff
*index
)
2115 expr
= pet_expr_cow(expr
);
2116 if (!expr
|| !index
)
2118 if (expr
->type
!= pet_expr_access
)
2119 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2120 "not an access expression", goto error
);
2121 isl_multi_pw_aff_free(expr
->acc
.index
);
2122 expr
->acc
.index
= index
;
2123 expr
->acc
.depth
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
2127 isl_multi_pw_aff_free(index
);
2128 pet_expr_free(expr
);
2132 /* Return the reference identifier of access expression "expr".
2134 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
2138 if (expr
->type
!= pet_expr_access
)
2139 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2140 "not an access expression", return NULL
);
2142 return isl_id_copy(expr
->acc
.ref_id
);
2145 /* Replace the reference identifier of access expression "expr" by "ref_id".
2147 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
2148 __isl_take isl_id
*ref_id
)
2150 expr
= pet_expr_cow(expr
);
2151 if (!expr
|| !ref_id
)
2153 if (expr
->type
!= pet_expr_access
)
2154 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2155 "not an access expression", goto error
);
2156 isl_id_free(expr
->acc
.ref_id
);
2157 expr
->acc
.ref_id
= ref_id
;
2161 isl_id_free(ref_id
);
2162 pet_expr_free(expr
);
2166 /* Tag the access relation "access" with "id".
2167 * That is, insert the id as the range of a wrapped relation
2168 * in the domain of "access".
2170 * If "access" is of the form
2174 * then the result is of the form
2176 * [D[i] -> id[]] -> A[a]
2178 __isl_give isl_union_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
2179 __isl_take isl_union_map
*access
)
2182 isl_multi_aff
*add_tag
;
2185 if (expr
->type
!= pet_expr_access
)
2186 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2187 "not an access expression",
2188 return isl_union_map_free(access
));
2190 id
= isl_id_copy(expr
->acc
.ref_id
);
2191 space
= pet_expr_access_get_domain_space(expr
);
2192 space
= isl_space_from_domain(space
);
2193 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2194 add_tag
= isl_multi_aff_domain_map(space
);
2195 access
= isl_union_map_preimage_domain_multi_aff(access
, add_tag
);
2200 /* Return the access relation of the given "type" associated to "expr"
2201 * that maps pairs of domain iterations and argument values
2202 * to the corresponding accessed data elements.
2204 * If the requested access relation is explicitly available,
2205 * then return a copy. Otherwise, check if it is irrelevant for
2206 * the access expression and return an empty relation if this is the case.
2207 * Otherwise, introduce the requested access relation in "expr" and
2210 __isl_give isl_union_map
*pet_expr_access_get_dependent_access(
2211 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2213 isl_union_map
*access
;
2218 if (expr
->type
!= pet_expr_access
)
2219 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2220 "not an access expression", return NULL
);
2222 type
= internalize_type(type
);
2223 if (expr
->acc
.access
[type
])
2224 return isl_union_map_copy(expr
->acc
.access
[type
]);
2226 if (type
== pet_expr_access_may_read
)
2227 empty
= !expr
->acc
.read
;
2229 empty
= !expr
->acc
.write
;
2232 expr
= pet_expr_copy(expr
);
2233 expr
= introduce_access_relations(expr
);
2236 access
= isl_union_map_copy(expr
->acc
.access
[type
]);
2237 pet_expr_free(expr
);
2242 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr
));
2245 /* Return the may read access relation associated to "expr"
2246 * that maps pairs of domain iterations and argument values
2247 * to the corresponding accessed data elements.
2249 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_read(
2250 __isl_keep pet_expr
*expr
)
2252 return pet_expr_access_get_dependent_access(expr
,
2253 pet_expr_access_may_read
);
2256 /* Return the may write access relation associated to "expr"
2257 * that maps pairs of domain iterations and argument values
2258 * to the corresponding accessed data elements.
2260 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_write(
2261 __isl_keep pet_expr
*expr
)
2263 return pet_expr_access_get_dependent_access(expr
,
2264 pet_expr_access_may_write
);
2267 /* Return the must write access relation associated to "expr"
2268 * that maps pairs of domain iterations and argument values
2269 * to the corresponding accessed data elements.
2271 __isl_give isl_union_map
*pet_expr_access_get_dependent_must_write(
2272 __isl_keep pet_expr
*expr
)
2274 return pet_expr_access_get_dependent_access(expr
,
2275 pet_expr_access_must_write
);
2278 /* Return the relation of the given "type" mapping domain iterations
2279 * to the accessed data elements.
2280 * In particular, take the access relation and, in case of may_read
2281 * or may_write, project out the values of the arguments, if any.
2282 * In case of must_write, return the empty relation if there are
2285 __isl_give isl_union_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
,
2286 enum pet_expr_access_type type
)
2288 isl_union_map
*access
;
2294 if (expr
->type
!= pet_expr_access
)
2295 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2296 "not an access expression", return NULL
);
2298 if (expr
->n_arg
!= 0 && type
== pet_expr_access_must_write
) {
2299 space
= pet_expr_access_get_parameter_space(expr
);
2300 return isl_union_map_empty(space
);
2303 access
= pet_expr_access_get_dependent_access(expr
, type
);
2304 if (expr
->n_arg
== 0)
2307 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
2308 space
= isl_space_domain(space
);
2309 map
= isl_map_universe(isl_space_unwrap(space
));
2310 map
= isl_map_domain_map(map
);
2311 access
= isl_union_map_apply_domain(access
,
2312 isl_union_map_from_map(map
));
2317 /* Return the relation mapping domain iterations to all possibly
2318 * read data elements.
2320 __isl_give isl_union_map
*pet_expr_access_get_may_read(
2321 __isl_keep pet_expr
*expr
)
2323 return pet_expr_access_get_access(expr
, pet_expr_access_may_read
);
2326 /* Return the relation mapping domain iterations to all possibly
2327 * written data elements.
2329 __isl_give isl_union_map
*pet_expr_access_get_may_write(
2330 __isl_keep pet_expr
*expr
)
2332 return pet_expr_access_get_access(expr
, pet_expr_access_may_write
);
2335 /* Return a relation mapping domain iterations to definitely
2336 * written data elements, assuming the statement containing
2337 * the expression is executed.
2339 __isl_give isl_union_map
*pet_expr_access_get_must_write(
2340 __isl_keep pet_expr
*expr
)
2342 return pet_expr_access_get_access(expr
, pet_expr_access_must_write
);
2345 /* Return the relation of the given "type" mapping domain iterations to
2346 * accessed data elements, with its domain tagged with the reference
2349 static __isl_give isl_union_map
*pet_expr_access_get_tagged_access(
2350 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2352 isl_union_map
*access
;
2357 access
= pet_expr_access_get_access(expr
, type
);
2358 access
= pet_expr_tag_access(expr
, access
);
2363 /* Return the relation mapping domain iterations to all possibly
2364 * read data elements, with its domain tagged with the reference
2367 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_read(
2368 __isl_keep pet_expr
*expr
)
2370 return pet_expr_access_get_tagged_access(expr
,
2371 pet_expr_access_may_read
);
2374 /* Return the relation mapping domain iterations to all possibly
2375 * written data elements, with its domain tagged with the reference
2378 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_write(
2379 __isl_keep pet_expr
*expr
)
2381 return pet_expr_access_get_tagged_access(expr
,
2382 pet_expr_access_may_write
);
2385 /* Return the operation type of operation expression "expr".
2387 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
2391 if (expr
->type
!= pet_expr_op
)
2392 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2393 "not an operation expression", return pet_op_last
);
2398 /* Replace the operation type of operation expression "expr" by "type".
2400 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
2401 enum pet_op_type type
)
2404 return pet_expr_free(expr
);
2405 if (expr
->type
!= pet_expr_op
)
2406 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2407 "not an operation expression",
2408 return pet_expr_free(expr
));
2409 if (expr
->op
== type
)
2411 expr
= pet_expr_cow(expr
);
2419 /* Return the name of the function called by "expr".
2421 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
2425 if (expr
->type
!= pet_expr_call
)
2426 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2427 "not a call expression", return NULL
);
2428 return expr
->c
.name
;
2431 /* Replace the name of the function called by "expr" by "name".
2433 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
2434 __isl_keep
const char *name
)
2436 expr
= pet_expr_cow(expr
);
2438 return pet_expr_free(expr
);
2439 if (expr
->type
!= pet_expr_call
)
2440 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2441 "not a call expression", return pet_expr_free(expr
));
2443 expr
->c
.name
= strdup(name
);
2445 return pet_expr_free(expr
);
2449 /* Does the call expression "expr" have an associated function summary?
2451 int pet_expr_call_has_summary(__isl_keep pet_expr
*expr
)
2455 if (expr
->type
!= pet_expr_call
)
2456 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2457 "not a call expression", return -1);
2459 return expr
->c
.summary
!= NULL
;
2462 /* Return a copy of the function summary associated to
2463 * the call expression "expr".
2465 __isl_give pet_function_summary
*pet_expr_call_get_summary(
2466 __isl_keep pet_expr
*expr
)
2470 if (expr
->type
!= pet_expr_call
)
2471 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2472 "not a call expression", return NULL
);
2474 return pet_function_summary_copy(expr
->c
.summary
);
2477 /* Replace the function summary associated to the call expression "expr"
2480 __isl_give pet_expr
*pet_expr_call_set_summary(__isl_take pet_expr
*expr
,
2481 __isl_take pet_function_summary
*summary
)
2483 expr
= pet_expr_cow(expr
);
2484 if (!expr
|| !summary
)
2486 if (expr
->type
!= pet_expr_call
)
2487 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2488 "not a call expression", goto error
);
2489 pet_function_summary_free(expr
->c
.summary
);
2490 expr
->c
.summary
= summary
;
2493 pet_function_summary_free(summary
);
2494 return pet_expr_free(expr
);
2497 /* Replace the type of the cast performed by "expr" by "name".
2499 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
2500 __isl_keep
const char *name
)
2502 expr
= pet_expr_cow(expr
);
2504 return pet_expr_free(expr
);
2505 if (expr
->type
!= pet_expr_cast
)
2506 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2507 "not a cast expression", return pet_expr_free(expr
));
2508 free(expr
->type_name
);
2509 expr
->type_name
= strdup(name
);
2510 if (!expr
->type_name
)
2511 return pet_expr_free(expr
);
2515 /* Return the value of the integer represented by "expr".
2517 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
2521 if (expr
->type
!= pet_expr_int
)
2522 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2523 "not an int expression", return NULL
);
2525 return isl_val_copy(expr
->i
);
2528 /* Replace the value of the integer represented by "expr" by "v".
2530 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
2531 __isl_take isl_val
*v
)
2533 expr
= pet_expr_cow(expr
);
2536 if (expr
->type
!= pet_expr_int
)
2537 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2538 "not an int expression", goto error
);
2539 isl_val_free(expr
->i
);
2545 pet_expr_free(expr
);
2549 /* Replace the value and string representation of the double
2550 * represented by "expr" by "d" and "s".
2552 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2553 double d
, __isl_keep
const char *s
)
2555 expr
= pet_expr_cow(expr
);
2557 return pet_expr_free(expr
);
2558 if (expr
->type
!= pet_expr_double
)
2559 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2560 "not a double expression", return pet_expr_free(expr
));
2563 expr
->d
.s
= strdup(s
);
2565 return pet_expr_free(expr
);
2569 /* Return a string representation of the double expression "expr".
2571 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2575 if (expr
->type
!= pet_expr_double
)
2576 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2577 "not a double expression", return NULL
);
2578 return strdup(expr
->d
.s
);
2581 /* Return a piecewise affine expression defined on the specified domain
2582 * that represents NaN.
2584 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2586 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2589 /* This function is called when we come across an access that is
2590 * nested in what is supposed to be an affine expression.
2591 * "pc" is the context in which the affine expression is created.
2592 * If nesting is allowed in "pc", we return an affine expression that is
2593 * equal to a new parameter corresponding to this nested access.
2594 * Otherwise, we return NaN.
2596 * Note that we currently don't allow nested accesses themselves
2597 * to contain any nested accesses, so we check if "expr" itself
2598 * involves any nested accesses (either explicitly as arguments
2599 * or implicitly through parameters) and return NaN if it does.
2601 * The new parameter is resolved in resolve_nested.
2603 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2604 __isl_keep pet_context
*pc
)
2609 isl_local_space
*ls
;
2615 if (!pet_context_allow_nesting(pc
))
2616 return non_affine(pet_context_get_space(pc
));
2618 if (pet_expr_get_type(expr
) != pet_expr_access
)
2619 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2620 "not an access expression", return NULL
);
2622 if (expr
->n_arg
> 0)
2623 return non_affine(pet_context_get_space(pc
));
2625 space
= pet_expr_access_get_parameter_space(expr
);
2626 nested
= pet_nested_any_in_space(space
);
2627 isl_space_free(space
);
2629 return non_affine(pet_context_get_space(pc
));
2631 ctx
= pet_expr_get_ctx(expr
);
2632 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2633 space
= pet_context_get_space(pc
);
2634 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2636 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2637 ls
= isl_local_space_from_space(space
);
2638 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2640 return isl_pw_aff_from_aff(aff
);
2643 /* Extract an affine expression from the access pet_expr "expr".
2644 * "pc" is the context in which the affine expression is created.
2646 * If "expr" is actually an affine expression rather than
2647 * a real access, then we return that expression.
2648 * Otherwise, we require that "expr" is of an integral type.
2649 * If not, we return NaN.
2651 * If the variable has been assigned a known affine expression,
2652 * then we return that expression.
2654 * Otherwise, we return an expression that is equal to a parameter
2655 * representing "expr" (if "allow_nested" is set).
2657 static __isl_give isl_pw_aff
*extract_affine_from_access(
2658 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2663 if (pet_expr_is_affine(expr
)) {
2665 isl_multi_pw_aff
*mpa
;
2667 mpa
= pet_expr_access_get_index(expr
);
2668 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
2669 isl_multi_pw_aff_free(mpa
);
2673 if (pet_expr_get_type_size(expr
) == 0)
2674 return non_affine(pet_context_get_space(pc
));
2676 if (!pet_expr_is_scalar_access(expr
))
2677 return nested_access(expr
, pc
);
2679 id
= pet_expr_access_get_id(expr
);
2680 if (pet_context_is_assigned(pc
, id
))
2681 return pet_context_get_value(pc
, id
);
2684 return nested_access(expr
, pc
);
2687 /* Construct an affine expression from the integer constant "expr".
2688 * "pc" is the context in which the affine expression is created.
2690 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2691 __isl_keep pet_context
*pc
)
2693 isl_local_space
*ls
;
2699 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2700 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2702 return isl_pw_aff_from_aff(aff
);
2705 /* Extract an affine expression from an addition or subtraction operation.
2706 * Return NaN if we are unable to extract an affine expression.
2708 * "pc" is the context in which the affine expression is created.
2710 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2711 __isl_keep pet_context
*pc
)
2718 if (expr
->n_arg
!= 2)
2719 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2720 "expecting two arguments", return NULL
);
2722 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2723 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2725 switch (pet_expr_op_get_type(expr
)) {
2727 return isl_pw_aff_add(lhs
, rhs
);
2729 return isl_pw_aff_sub(lhs
, rhs
);
2731 isl_pw_aff_free(lhs
);
2732 isl_pw_aff_free(rhs
);
2733 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2734 "not an addition or subtraction operation",
2740 /* Extract an affine expression from an integer division or a modulo operation.
2741 * Return NaN if we are unable to extract an affine expression.
2743 * "pc" is the context in which the affine expression is created.
2745 * In particular, if "expr" is lhs/rhs, then return
2747 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2749 * If "expr" is lhs%rhs, then return
2751 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2753 * If the second argument (rhs) is not a (positive) integer constant,
2754 * then we fail to extract an affine expression.
2756 * We simplify the result in the context of the domain of "pc" in case
2757 * this domain implies that lhs >= 0 (or < 0).
2759 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2760 __isl_keep pet_context
*pc
)
2769 if (expr
->n_arg
!= 2)
2770 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2771 "expecting two arguments", return NULL
);
2773 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2775 is_cst
= isl_pw_aff_is_cst(rhs
);
2776 if (is_cst
< 0 || !is_cst
) {
2777 isl_pw_aff_free(rhs
);
2778 return non_affine(pet_context_get_space(pc
));
2781 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2783 switch (pet_expr_op_get_type(expr
)) {
2785 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
2788 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
2791 isl_pw_aff_free(lhs
);
2792 isl_pw_aff_free(rhs
);
2793 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2794 "not a div or mod operator", return NULL
);
2797 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
2800 /* Extract an affine expression from a multiplication operation.
2801 * Return NaN if we are unable to extract an affine expression.
2802 * In particular, if neither of the arguments is a (piecewise) constant
2803 * then we return NaN.
2805 * "pc" is the context in which the affine expression is created.
2807 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2808 __isl_keep pet_context
*pc
)
2810 int lhs_cst
, rhs_cst
;
2816 if (expr
->n_arg
!= 2)
2817 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2818 "expecting two arguments", return NULL
);
2820 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2821 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2823 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2824 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2825 if (lhs_cst
>= 0 && rhs_cst
>= 0 && (lhs_cst
|| rhs_cst
))
2826 return isl_pw_aff_mul(lhs
, rhs
);
2828 isl_pw_aff_free(lhs
);
2829 isl_pw_aff_free(rhs
);
2831 if (lhs_cst
< 0 || rhs_cst
< 0)
2834 return non_affine(pet_context_get_space(pc
));
2837 /* Extract an affine expression from a negation operation.
2838 * Return NaN if we are unable to extract an affine expression.
2840 * "pc" is the context in which the affine expression is created.
2842 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
2843 __isl_keep pet_context
*pc
)
2849 if (expr
->n_arg
!= 1)
2850 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2851 "expecting one argument", return NULL
);
2853 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
2854 return isl_pw_aff_neg(res
);
2857 /* Extract an affine expression from a conditional operation.
2858 * Return NaN if we are unable to extract an affine expression.
2860 * "pc" is the context in which the affine expression is created.
2862 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
2863 __isl_keep pet_context
*pc
)
2865 isl_pw_aff
*cond
, *lhs
, *rhs
;
2869 if (expr
->n_arg
!= 3)
2870 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2871 "expecting three arguments", return NULL
);
2873 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2874 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2875 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
2877 return isl_pw_aff_cond(cond
, lhs
, rhs
);
2884 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
, unsigned width
)
2889 ctx
= isl_pw_aff_get_ctx(pwaff
);
2890 mod
= isl_val_int_from_ui(ctx
, width
);
2891 mod
= isl_val_2exp(mod
);
2893 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
2898 /* Limit the domain of "pwaff" to those elements where the function
2901 * 2^{width-1} <= pwaff < 2^{width-1}
2903 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
2908 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
2909 isl_local_space
*ls
= isl_local_space_from_space(space
);
2914 ctx
= isl_pw_aff_get_ctx(pwaff
);
2915 v
= isl_val_int_from_ui(ctx
, width
- 1);
2916 v
= isl_val_2exp(v
);
2918 bound
= isl_aff_zero_on_domain(ls
);
2919 bound
= isl_aff_add_constant_val(bound
, v
);
2920 b
= isl_pw_aff_from_aff(bound
);
2922 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
2923 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2925 b
= isl_pw_aff_neg(b
);
2926 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
2927 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2932 /* Handle potential overflows on signed computations.
2934 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2935 * then we adjust the domain of "pa" to avoid overflows.
2937 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
2941 struct pet_options
*options
;
2946 ctx
= isl_pw_aff_get_ctx(pa
);
2947 options
= isl_ctx_peek_pet_options(ctx
);
2948 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
2949 pa
= avoid_overflow(pa
, width
);
2954 /* Extract an affine expression from some an operation.
2955 * Return NaN if we are unable to extract an affine expression.
2956 * If the result of a binary (non boolean) operation is unsigned,
2957 * then we wrap it based on the size of the type. If the result is signed,
2958 * then we ensure that no overflow occurs.
2960 * "pc" is the context in which the affine expression is created.
2962 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
2963 __isl_keep pet_context
*pc
)
2968 switch (pet_expr_op_get_type(expr
)) {
2971 res
= extract_affine_add_sub(expr
, pc
);
2975 res
= extract_affine_div_mod(expr
, pc
);
2978 res
= extract_affine_mul(expr
, pc
);
2981 return extract_affine_neg(expr
, pc
);
2983 return extract_affine_cond(expr
, pc
);
2993 return pet_expr_extract_affine_condition(expr
, pc
);
2995 return non_affine(pet_context_get_space(pc
));
3000 if (isl_pw_aff_involves_nan(res
)) {
3001 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
3002 isl_pw_aff_free(res
);
3003 return non_affine(space
);
3006 type_size
= pet_expr_get_type_size(expr
);
3008 res
= wrap(res
, type_size
);
3010 res
= signed_overflow(res
, -type_size
);
3015 /* Internal data structure for affine builtin function declarations.
3017 * "pencil" is set if the builtin is pencil specific.
3018 * "n_args" is the number of arguments the function takes.
3019 * "name" is the function name.
3021 struct affine_builtin_decl
{
3027 static struct affine_builtin_decl affine_builtins
[] = {
3035 { 0, 2, "intFloor" },
3036 { 0, 2, "intCeil" },
3041 /* List of min and max builtin functions.
3043 static const char *min_max_builtins
[] = {
3044 "min", "imin", "umin",
3045 "max", "imax", "umax"
3048 /* Is a function call to "name" with "n_args" arguments a call to a
3049 * builtin function for which we can construct an affine expression?
3050 * pencil specific builtins are only recognized if "pencil" is set.
3052 static int is_affine_builtin(int pencil
, int n_args
, const char *name
)
3056 for (i
= 0; i
< ARRAY_SIZE(affine_builtins
); ++i
) {
3057 struct affine_builtin_decl
*decl
= &affine_builtins
[i
];
3059 if (decl
->pencil
&& !pencil
)
3061 if (decl
->n_args
== n_args
&& !strcmp(decl
->name
, name
))
3068 /* Is function "name" a known min or max builtin function?
3070 static int is_min_or_max_builtin(const char *name
)
3074 for (i
= 0; i
< ARRAY_SIZE(min_max_builtins
); ++i
)
3075 if (!strcmp(min_max_builtins
[i
], name
))
3081 /* Extract an affine expression from some special function calls.
3082 * Return NaN if we are unable to extract an affine expression.
3083 * In particular, we handle "min", "max", "ceild", "floord",
3084 * "intMod", "intFloor" and "intCeil".
3085 * In case of the latter five, the second argument needs to be
3086 * a (positive) integer constant.
3087 * If the pencil option is set, then we also handle "{i,u}min" and
3090 * "pc" is the context in which the affine expression is created.
3092 static __isl_give isl_pw_aff
*extract_affine_from_call(
3093 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3096 isl_pw_aff
*aff1
, *aff2
;
3099 struct pet_options
*options
;
3103 ctx
= pet_expr_get_ctx(expr
);
3104 options
= isl_ctx_peek_pet_options(ctx
);
3106 n
= pet_expr_get_n_arg(expr
);
3107 name
= pet_expr_call_get_name(expr
);
3108 if (!is_affine_builtin(options
->pencil
, n
, name
))
3109 return non_affine(pet_context_get_space(pc
));
3111 if (is_min_or_max_builtin(name
)) {
3112 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3113 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
3115 if (strstr(name
, "min"))
3116 aff1
= isl_pw_aff_min(aff1
, aff2
);
3118 aff1
= isl_pw_aff_max(aff1
, aff2
);
3119 } else if (!strcmp(name
, "intMod")) {
3122 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3123 return non_affine(pet_context_get_space(pc
));
3124 v
= pet_expr_int_get_val(expr
->args
[1]);
3125 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3126 aff1
= isl_pw_aff_mod_val(aff1
, v
);
3130 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3131 return non_affine(pet_context_get_space(pc
));
3132 v
= pet_expr_int_get_val(expr
->args
[1]);
3133 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3134 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
3135 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
3136 aff1
= isl_pw_aff_floor(aff1
);
3138 aff1
= isl_pw_aff_ceil(aff1
);
3144 /* Extract an affine expression from "expr", if possible.
3145 * Otherwise return NaN.
3147 * "pc" is the context in which the affine expression is created.
3149 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
3150 __isl_keep pet_context
*pc
)
3155 switch (pet_expr_get_type(expr
)) {
3156 case pet_expr_access
:
3157 return extract_affine_from_access(expr
, pc
);
3159 return extract_affine_from_int(expr
, pc
);
3161 return extract_affine_from_op(expr
, pc
);
3163 return extract_affine_from_call(expr
, pc
);
3165 case pet_expr_double
:
3166 case pet_expr_error
:
3167 return non_affine(pet_context_get_space(pc
));
3171 /* Extract an affine expressions representing the comparison "LHS op RHS"
3172 * Return NaN if we are unable to extract such an affine expression.
3174 * "pc" is the context in which the affine expression is created.
3176 * If the comparison is of the form
3180 * then the expression is constructed as the conjunction of
3185 * A similar optimization is performed for max(a,b) <= c.
3186 * We do this because that will lead to simpler representations
3187 * of the expression.
3188 * If isl is ever enhanced to explicitly deal with min and max expressions,
3189 * this optimization can be removed.
3191 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
3192 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
3193 __isl_keep pet_context
*pc
)
3195 isl_pw_aff
*lhs_pa
, *rhs_pa
;
3197 if (op
== pet_op_gt
)
3198 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
3199 if (op
== pet_op_ge
)
3200 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
3202 if (op
== pet_op_lt
|| op
== pet_op_le
) {
3203 if (pet_expr_is_min(rhs
)) {
3204 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3206 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3208 return pet_and(lhs_pa
, rhs_pa
);
3210 if (pet_expr_is_max(lhs
)) {
3211 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
3213 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
3215 return pet_and(lhs_pa
, rhs_pa
);
3219 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
3220 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
3222 return pet_comparison(op
, lhs_pa
, rhs_pa
);
3225 /* Extract an affine expressions from the comparison "expr".
3226 * Return NaN if we are unable to extract such an affine expression.
3228 * "pc" is the context in which the affine expression is created.
3230 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
3231 __isl_keep pet_context
*pc
)
3233 enum pet_op_type type
;
3237 if (expr
->n_arg
!= 2)
3238 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3239 "expecting two arguments", return NULL
);
3241 type
= pet_expr_op_get_type(expr
);
3242 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
3246 /* Extract an affine expression representing the boolean operation
3247 * expressed by "expr".
3248 * Return NaN if we are unable to extract an affine expression.
3250 * "pc" is the context in which the affine expression is created.
3252 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
3253 __isl_keep pet_context
*pc
)
3255 isl_pw_aff
*lhs
, *rhs
;
3261 n
= pet_expr_get_n_arg(expr
);
3262 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3264 return pet_not(lhs
);
3266 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
3267 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
3270 /* Extract the affine expression "expr != 0 ? 1 : 0".
3271 * Return NaN if we are unable to extract an affine expression.
3273 * "pc" is the context in which the affine expression is created.
3275 static __isl_give isl_pw_aff
*extract_implicit_condition(
3276 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3280 res
= pet_expr_extract_affine(expr
, pc
);
3281 return pet_to_bool(res
);
3284 /* Extract a boolean affine expression from "expr".
3285 * Return NaN if we are unable to extract an affine expression.
3287 * "pc" is the context in which the affine expression is created.
3289 * If "expr" is neither a comparison nor a boolean operation,
3290 * then we assume it is an affine expression and return the
3291 * boolean expression "expr != 0 ? 1 : 0".
3293 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
3294 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3299 if (pet_expr_is_comparison(expr
))
3300 return extract_comparison(expr
, pc
);
3301 if (pet_expr_is_boolean(expr
))
3302 return extract_boolean(expr
, pc
);
3304 return extract_implicit_condition(expr
, pc
);
3307 /* Check if "expr" is an assume expression and if its single argument
3308 * can be converted to an affine expression in the context of "pc".
3309 * If so, replace the argument by the affine expression.
3311 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
3312 __isl_keep pet_context
*pc
)
3315 isl_multi_pw_aff
*index
;
3319 if (!pet_expr_is_assume(expr
))
3321 if (expr
->n_arg
!= 1)
3322 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3323 "expecting one argument", return pet_expr_free(expr
));
3325 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3327 return pet_expr_free(expr
);
3328 if (isl_pw_aff_involves_nan(cond
)) {
3329 isl_pw_aff_free(cond
);
3333 index
= isl_multi_pw_aff_from_pw_aff(cond
);
3334 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
3339 /* Return the number of bits needed to represent the type of "expr".
3340 * See the description of the type_size field of pet_expr.
3342 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
3344 return expr
? expr
->type_size
: 0;
3347 /* Replace the number of bits needed to represent the type of "expr"
3349 * See the description of the type_size field of pet_expr.
3351 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
3354 expr
= pet_expr_cow(expr
);
3358 expr
->type_size
= type_size
;
3363 /* Extend an access expression "expr" with an additional index "index".
3364 * In particular, add "index" as an extra argument to "expr" and
3365 * adjust the index expression of "expr" to refer to this extra argument.
3366 * The caller is responsible for calling pet_expr_access_set_depth
3367 * to update the corresponding access relation.
3369 * Note that we only collect the individual index expressions as
3370 * arguments of "expr" here.
3371 * An attempt to integrate them into the index expression of "expr"
3372 * is performed in pet_expr_access_plug_in_args.
3374 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
3375 __isl_take pet_expr
*index
)
3379 isl_local_space
*ls
;
3382 expr
= pet_expr_cow(expr
);
3383 if (!expr
|| !index
)
3385 if (expr
->type
!= pet_expr_access
)
3386 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3387 "not an access pet_expr", goto error
);
3389 n
= pet_expr_get_n_arg(expr
);
3390 expr
= pet_expr_insert_arg(expr
, n
, index
);
3394 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3395 ls
= isl_local_space_from_space(space
);
3396 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
3397 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
3398 if (!expr
->acc
.index
)
3399 return pet_expr_free(expr
);
3403 pet_expr_free(expr
);
3404 pet_expr_free(index
);
3408 /* Extend an access expression "expr" with an additional member acces to "id".
3409 * In particular, extend the index expression of "expr" to include
3410 * the additional member access.
3411 * The caller is responsible for calling pet_expr_access_set_depth
3412 * to update the corresponding access relation.
3414 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
3415 __isl_take isl_id
*id
)
3418 isl_multi_pw_aff
*field_access
;
3420 expr
= pet_expr_cow(expr
);
3423 if (expr
->type
!= pet_expr_access
)
3424 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3425 "not an access pet_expr", goto error
);
3427 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3428 space
= isl_space_from_domain(space
);
3429 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
3430 field_access
= isl_multi_pw_aff_zero(space
);
3431 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
3432 if (!expr
->acc
.index
)
3433 return pet_expr_free(expr
);
3437 pet_expr_free(expr
);
3442 /* Prefix the access expression "expr" with "prefix".
3443 * If "add" is set, then it is not the index expression "prefix" itself
3444 * that was passed to the function, but its address.
3446 __isl_give pet_expr
*pet_expr_access_patch(__isl_take pet_expr
*expr
,
3447 __isl_take isl_multi_pw_aff
*prefix
, int add
)
3449 enum pet_expr_access_type type
;
3451 expr
= pet_expr_cow(expr
);
3452 if (!expr
|| !prefix
)
3454 if (expr
->type
!= pet_expr_access
)
3455 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3456 "not an access pet_expr", goto error
);
3458 expr
->acc
.depth
+= isl_multi_pw_aff_dim(prefix
, isl_dim_out
) - add
;
3459 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
3460 if (!expr
->acc
.access
[type
])
3462 expr
->acc
.access
[type
] = pet_patch_union_map(
3463 isl_multi_pw_aff_copy(prefix
), expr
->acc
.access
[type
],
3465 if (!expr
->acc
.access
[type
])
3468 expr
->acc
.index
= pet_patch_multi_pw_aff(prefix
, expr
->acc
.index
, add
);
3469 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
3470 return pet_expr_free(expr
);
3474 pet_expr_free(expr
);
3475 isl_multi_pw_aff_free(prefix
);
3479 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
3486 fprintf(stderr
, "%*s", indent
, "");
3488 switch (expr
->type
) {
3489 case pet_expr_double
:
3490 fprintf(stderr
, "%s\n", expr
->d
.s
);
3493 isl_val_dump(expr
->i
);
3495 case pet_expr_access
:
3496 if (expr
->acc
.ref_id
) {
3497 isl_id_dump(expr
->acc
.ref_id
);
3498 fprintf(stderr
, "%*s", indent
, "");
3500 isl_multi_pw_aff_dump(expr
->acc
.index
);
3501 fprintf(stderr
, "%*sdepth: %d\n", indent
+ 2,
3502 "", expr
->acc
.depth
);
3503 if (expr
->acc
.kill
) {
3504 fprintf(stderr
, "%*skill: 1\n", indent
+ 2, "");
3506 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
3507 "", expr
->acc
.read
);
3508 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
3509 "", expr
->acc
.write
);
3511 if (expr
->acc
.access
[pet_expr_access_may_read
]) {
3512 fprintf(stderr
, "%*smay_read: ", indent
+ 2, "");
3514 expr
->acc
.access
[pet_expr_access_may_read
]);
3516 if (expr
->acc
.access
[pet_expr_access_may_write
]) {
3517 fprintf(stderr
, "%*smay_write: ", indent
+ 2, "");
3519 expr
->acc
.access
[pet_expr_access_may_write
]);
3521 if (expr
->acc
.access
[pet_expr_access_must_write
]) {
3522 fprintf(stderr
, "%*smust_write: ", indent
+ 2, "");
3524 expr
->acc
.access
[pet_expr_access_must_write
]);
3526 for (i
= 0; i
< expr
->n_arg
; ++i
)
3527 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3530 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
3531 for (i
= 0; i
< expr
->n_arg
; ++i
)
3532 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3535 fprintf(stderr
, "%s/%d\n", expr
->c
.name
, expr
->n_arg
);
3536 for (i
= 0; i
< expr
->n_arg
; ++i
)
3537 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3538 if (expr
->c
.summary
) {
3539 fprintf(stderr
, "%*s", indent
, "");
3540 fprintf(stderr
, "summary:\n");
3541 pet_function_summary_dump_with_indent(expr
->c
.summary
,
3546 fprintf(stderr
, "(%s)\n", expr
->type_name
);
3547 for (i
= 0; i
< expr
->n_arg
; ++i
)
3548 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3550 case pet_expr_error
:
3551 fprintf(stderr
, "ERROR\n");
3556 void pet_expr_dump(__isl_keep pet_expr
*expr
)
3558 pet_expr_dump_with_indent(expr
, 0);