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 isl_bool
pet_expr_is_affine(__isl_keep pet_expr
*expr
)
831 return isl_bool_error
;
832 if (expr
->type
!= pet_expr_access
)
833 return isl_bool_false
;
835 has_id
= isl_multi_pw_aff_has_tuple_id(expr
->acc
.index
, isl_dim_out
);
837 return isl_bool_error
;
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 isl_bool
pet_expr_access_is_read(__isl_keep pet_expr
*expr
)
1051 return isl_bool_error
;
1052 if (expr
->type
!= pet_expr_access
)
1053 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1054 "not an access expression", return isl_bool_error
);
1056 return expr
->acc
.read
;
1059 /* Does the access expression "expr" write to the accessed elements?
1061 isl_bool
pet_expr_access_is_write(__isl_keep pet_expr
*expr
)
1064 return isl_bool_error
;
1065 if (expr
->type
!= pet_expr_access
)
1066 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1067 "not an access expression", return isl_bool_error
);
1069 return expr
->acc
.write
;
1072 /* Does the access expression "expr" kill the accessed elements?
1074 isl_bool
pet_expr_access_is_kill(__isl_keep pet_expr
*expr
)
1077 return isl_bool_error
;
1078 if (expr
->type
!= pet_expr_access
)
1079 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1080 "not an access expression", return isl_bool_error
);
1082 return expr
->acc
.kill
;
1085 /* Return the identifier of the array accessed by "expr".
1087 * If "expr" represents a member access, then return the identifier
1088 * of the outer structure array.
1090 __isl_give isl_id
*pet_expr_access_get_id(__isl_keep pet_expr
*expr
)
1094 if (expr
->type
!= pet_expr_access
)
1095 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1096 "not an access expression", return NULL
);
1098 if (isl_multi_pw_aff_range_is_wrapping(expr
->acc
.index
)) {
1102 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1103 space
= isl_space_range(space
);
1104 while (space
&& isl_space_is_wrapping(space
))
1105 space
= isl_space_domain(isl_space_unwrap(space
));
1106 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
1107 isl_space_free(space
);
1112 return isl_multi_pw_aff_get_tuple_id(expr
->acc
.index
, isl_dim_out
);
1115 /* Return the parameter space of "expr".
1117 __isl_give isl_space
*pet_expr_access_get_parameter_space(
1118 __isl_keep pet_expr
*expr
)
1124 if (expr
->type
!= pet_expr_access
)
1125 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1126 "not an access expression", return NULL
);
1128 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1129 space
= isl_space_params(space
);
1134 /* Return the domain space of "expr", including the arguments (if any).
1136 __isl_give isl_space
*pet_expr_access_get_augmented_domain_space(
1137 __isl_keep pet_expr
*expr
)
1143 if (expr
->type
!= pet_expr_access
)
1144 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1145 "not an access expression", return NULL
);
1147 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1148 space
= isl_space_domain(space
);
1153 /* Return the domain space of "expr", without the arguments (if any).
1155 __isl_give isl_space
*pet_expr_access_get_domain_space(
1156 __isl_keep pet_expr
*expr
)
1160 space
= pet_expr_access_get_augmented_domain_space(expr
);
1161 if (isl_space_is_wrapping(space
))
1162 space
= isl_space_domain(isl_space_unwrap(space
));
1167 /* Return the space of the data accessed by "expr".
1169 __isl_give isl_space
*pet_expr_access_get_data_space(__isl_keep pet_expr
*expr
)
1175 if (expr
->type
!= pet_expr_access
)
1176 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1177 "not an access expression", return NULL
);
1179 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1180 space
= isl_space_range(space
);
1185 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1187 static __isl_give pet_expr
*pet_expr_map_expr_of_type(__isl_take pet_expr
*expr
,
1188 enum pet_expr_type type
,
1189 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1194 n
= pet_expr_get_n_arg(expr
);
1195 for (i
= 0; i
< n
; ++i
) {
1196 pet_expr
*arg
= pet_expr_get_arg(expr
, i
);
1197 arg
= pet_expr_map_expr_of_type(arg
, type
, fn
, user
);
1198 expr
= pet_expr_set_arg(expr
, i
, arg
);
1204 if (expr
->type
== type
)
1205 expr
= fn(expr
, user
);
1210 /* Modify all expressions of type pet_expr_access in "expr"
1211 * by calling "fn" on them.
1213 __isl_give pet_expr
*pet_expr_map_access(__isl_take pet_expr
*expr
,
1214 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1217 return pet_expr_map_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1220 /* Modify all expressions of type pet_expr_call in "expr"
1221 * by calling "fn" on them.
1223 __isl_give pet_expr
*pet_expr_map_call(__isl_take pet_expr
*expr
,
1224 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1227 return pet_expr_map_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1230 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1232 * Return -1 on error (where fn returning a negative value is treated as
1234 * Otherwise return 0.
1236 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr
*expr
,
1237 enum pet_expr_type type
,
1238 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1245 for (i
= 0; i
< expr
->n_arg
; ++i
)
1246 if (pet_expr_foreach_expr_of_type(expr
->args
[i
],
1247 type
, fn
, user
) < 0)
1250 if (expr
->type
== type
)
1251 return fn(expr
, user
);
1256 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1258 * Return -1 on error (where fn returning a negative value is treated as
1260 * Otherwise return 0.
1262 int pet_expr_foreach_access_expr(__isl_keep pet_expr
*expr
,
1263 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1265 return pet_expr_foreach_expr_of_type(expr
, pet_expr_access
, fn
, user
);
1268 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1270 * Return -1 on error (where fn returning a negative value is treated as
1272 * Otherwise return 0.
1274 int pet_expr_foreach_call_expr(__isl_keep pet_expr
*expr
,
1275 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1277 return pet_expr_foreach_expr_of_type(expr
, pet_expr_call
, fn
, user
);
1280 /* Internal data structure for pet_expr_writes.
1281 * "id" is the identifier that we are looking for.
1282 * "found" is set if we have found the identifier being written to.
1284 struct pet_expr_writes_data
{
1289 /* Given an access expression, check if it writes to data->id.
1290 * If so, set data->found and abort the search.
1292 static int writes(__isl_keep pet_expr
*expr
, void *user
)
1294 struct pet_expr_writes_data
*data
= user
;
1297 if (!expr
->acc
.write
)
1299 if (pet_expr_is_affine(expr
))
1302 write_id
= pet_expr_access_get_id(expr
);
1303 isl_id_free(write_id
);
1308 if (write_id
!= data
->id
)
1315 /* Does expression "expr" write to "id"?
1317 int pet_expr_writes(__isl_keep pet_expr
*expr
, __isl_keep isl_id
*id
)
1319 struct pet_expr_writes_data data
;
1323 if (pet_expr_foreach_access_expr(expr
, &writes
, &data
) < 0 &&
1330 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1331 * index expression and access relations of "expr" (if any)
1332 * to dimensions of "dst_type" at "dst_pos".
1334 __isl_give pet_expr
*pet_expr_access_move_dims(__isl_take pet_expr
*expr
,
1335 enum isl_dim_type dst_type
, unsigned dst_pos
,
1336 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1338 enum pet_expr_access_type type
;
1340 expr
= pet_expr_cow(expr
);
1343 if (expr
->type
!= pet_expr_access
)
1344 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1345 "not an access pet_expr", return pet_expr_free(expr
));
1347 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1348 if (!expr
->acc
.access
[type
])
1350 expr
->acc
.access
[type
] =
1351 pet_union_map_move_dims(expr
->acc
.access
[type
],
1352 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1353 if (!expr
->acc
.access
[type
])
1356 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
1357 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1358 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1359 return pet_expr_free(expr
);
1364 /* Replace the index expression and access relations (if any) of "expr"
1365 * by their preimages under the function represented by "ma".
1367 __isl_give pet_expr
*pet_expr_access_pullback_multi_aff(
1368 __isl_take pet_expr
*expr
, __isl_take isl_multi_aff
*ma
)
1370 enum pet_expr_access_type type
;
1372 expr
= pet_expr_cow(expr
);
1375 if (expr
->type
!= pet_expr_access
)
1376 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1377 "not an access pet_expr", goto error
);
1379 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1380 if (!expr
->acc
.access
[type
])
1382 expr
->acc
.access
[type
] =
1383 isl_union_map_preimage_domain_multi_aff(
1384 expr
->acc
.access
[type
], isl_multi_aff_copy(ma
));
1385 if (!expr
->acc
.access
[type
])
1388 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
1390 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1391 return pet_expr_free(expr
);
1395 isl_multi_aff_free(ma
);
1396 pet_expr_free(expr
);
1400 /* Replace the index expression and access relations (if any) of "expr"
1401 * by their preimages under the function represented by "mpa".
1403 __isl_give pet_expr
*pet_expr_access_pullback_multi_pw_aff(
1404 __isl_take pet_expr
*expr
, __isl_take isl_multi_pw_aff
*mpa
)
1406 enum pet_expr_access_type type
;
1408 expr
= pet_expr_cow(expr
);
1411 if (expr
->type
!= pet_expr_access
)
1412 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1413 "not an access pet_expr", goto error
);
1415 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1416 if (!expr
->acc
.access
[type
])
1418 expr
->acc
.access
[type
] =
1419 isl_union_map_preimage_domain_multi_pw_aff(
1420 expr
->acc
.access
[type
], isl_multi_pw_aff_copy(mpa
));
1421 if (!expr
->acc
.access
[type
])
1424 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1425 expr
->acc
.index
, mpa
);
1426 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1427 return pet_expr_free(expr
);
1431 isl_multi_pw_aff_free(mpa
);
1432 pet_expr_free(expr
);
1436 /* Return the index expression of access expression "expr".
1438 __isl_give isl_multi_pw_aff
*pet_expr_access_get_index(
1439 __isl_keep pet_expr
*expr
)
1443 if (expr
->type
!= pet_expr_access
)
1444 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1445 "not an access expression", return NULL
);
1447 return isl_multi_pw_aff_copy(expr
->acc
.index
);
1450 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1452 __isl_give pet_expr
*pet_expr_access_align_params(__isl_take pet_expr
*expr
)
1455 enum pet_expr_access_type type
;
1457 expr
= pet_expr_cow(expr
);
1460 if (expr
->type
!= pet_expr_access
)
1461 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1462 "not an access expression", return pet_expr_free(expr
));
1464 if (!has_any_access_relation(expr
))
1467 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1468 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1469 if (!expr
->acc
.access
[type
])
1471 space
= isl_space_align_params(space
,
1472 isl_union_map_get_space(expr
->acc
.access
[type
]));
1474 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1475 isl_space_copy(space
));
1476 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1477 if (!expr
->acc
.access
[type
])
1479 expr
->acc
.access
[type
] =
1480 isl_union_map_align_params(expr
->acc
.access
[type
],
1481 isl_space_copy(space
));
1482 if (!expr
->acc
.access
[type
])
1485 isl_space_free(space
);
1486 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
1487 return pet_expr_free(expr
);
1492 /* Are "expr1" and "expr2" both array accesses such that
1493 * the access relation of "expr1" is a subset of that of "expr2"?
1494 * Only take into account the first "n_arg" arguments.
1496 * This function is tailored for use by mark_self_dependences in nest.c.
1497 * In particular, the input expressions may have more than "n_arg"
1498 * elements in their arguments arrays, while only the first "n_arg"
1499 * elements are referenced from the access relations.
1501 int pet_expr_is_sub_access(__isl_keep pet_expr
*expr1
,
1502 __isl_keep pet_expr
*expr2
, int n_arg
)
1508 if (!expr1
|| !expr2
)
1510 if (pet_expr_get_type(expr1
) != pet_expr_access
)
1512 if (pet_expr_get_type(expr2
) != pet_expr_access
)
1514 if (pet_expr_is_affine(expr1
))
1516 if (pet_expr_is_affine(expr2
))
1518 n1
= pet_expr_get_n_arg(expr1
);
1521 n2
= pet_expr_get_n_arg(expr2
);
1526 for (i
= 0; i
< n1
; ++i
) {
1528 equal
= pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]);
1529 if (equal
< 0 || !equal
)
1532 id1
= pet_expr_access_get_id(expr1
);
1533 id2
= pet_expr_access_get_id(expr2
);
1541 expr1
= pet_expr_copy(expr1
);
1542 expr2
= pet_expr_copy(expr2
);
1543 expr1
= introduce_access_relations(expr1
);
1544 expr2
= introduce_access_relations(expr2
);
1545 if (!expr1
|| !expr2
)
1548 is_subset
= isl_union_map_is_subset(
1549 expr1
->acc
.access
[pet_expr_access_may_read
],
1550 expr2
->acc
.access
[pet_expr_access_may_read
]);
1552 pet_expr_free(expr1
);
1553 pet_expr_free(expr2
);
1557 pet_expr_free(expr1
);
1558 pet_expr_free(expr2
);
1562 /* Given a set in the iteration space "domain", extend it to live in the space
1563 * of the domain of access relations.
1565 * That, is the number of arguments "n" is 0, then simply return domain.
1566 * Otherwise, return [domain -> [a_1,...,a_n]].
1568 static __isl_give isl_set
*add_arguments(__isl_take isl_set
*domain
, int n
)
1575 map
= isl_map_from_domain(domain
);
1576 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
1577 return isl_map_wrap(map
);
1580 /* Add extra conditions to the domains of all access relations in "expr",
1581 * introducing access relations if they are not already present.
1583 * The conditions are not added to the index expression. Instead, they
1584 * are used to try and simplify the index expression.
1586 __isl_give pet_expr
*pet_expr_restrict(__isl_take pet_expr
*expr
,
1587 __isl_take isl_set
*cond
)
1590 isl_union_set
*uset
;
1591 enum pet_expr_access_type type
;
1593 expr
= pet_expr_cow(expr
);
1597 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1598 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
1599 isl_set_copy(cond
));
1604 if (expr
->type
!= pet_expr_access
) {
1609 expr
= introduce_access_relations(expr
);
1613 cond
= add_arguments(cond
, expr
->n_arg
);
1614 uset
= isl_union_set_from_set(isl_set_copy(cond
));
1615 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1616 if (!expr
->acc
.access
[type
])
1618 expr
->acc
.access
[type
] =
1619 isl_union_map_intersect_domain(expr
->acc
.access
[type
],
1620 isl_union_set_copy(uset
));
1621 if (!expr
->acc
.access
[type
])
1624 isl_union_set_free(uset
);
1625 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, cond
);
1626 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1627 return pet_expr_free(expr
);
1632 return pet_expr_free(expr
);
1635 /* Modify the access relations (if any) and index expression
1636 * of the given access expression
1637 * based on the given iteration space transformation.
1638 * In particular, precompose the access relation and index expression
1639 * with the update function.
1641 * If the access has any arguments then the domain of the access relation
1642 * is a wrapped mapping from the iteration space to the space of
1643 * argument values. We only need to change the domain of this wrapped
1644 * mapping, so we extend the input transformation with an identity mapping
1645 * on the space of argument values.
1647 __isl_give pet_expr
*pet_expr_access_update_domain(__isl_take pet_expr
*expr
,
1648 __isl_keep isl_multi_pw_aff
*update
)
1650 enum pet_expr_access_type type
;
1652 expr
= pet_expr_cow(expr
);
1655 if (expr
->type
!= pet_expr_access
)
1656 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1657 "not an access expression", return pet_expr_free(expr
));
1659 update
= isl_multi_pw_aff_copy(update
);
1661 if (expr
->n_arg
> 0) {
1663 isl_multi_pw_aff
*id
;
1665 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
1666 space
= isl_space_domain(space
);
1667 space
= isl_space_unwrap(space
);
1668 space
= isl_space_range(space
);
1669 space
= isl_space_map_from_set(space
);
1670 id
= isl_multi_pw_aff_identity(space
);
1671 update
= isl_multi_pw_aff_product(update
, id
);
1674 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1675 if (!expr
->acc
.access
[type
])
1677 expr
->acc
.access
[type
] =
1678 isl_union_map_preimage_domain_multi_pw_aff(
1679 expr
->acc
.access
[type
],
1680 isl_multi_pw_aff_copy(update
));
1681 if (!expr
->acc
.access
[type
])
1684 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_pw_aff(
1685 expr
->acc
.index
, update
);
1686 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1687 return pet_expr_free(expr
);
1692 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1694 isl_multi_pw_aff
*update
= user
;
1696 return pet_expr_access_update_domain(expr
, update
);
1699 /* Modify all access relations in "expr" by precomposing them with
1700 * the given iteration space transformation.
1702 __isl_give pet_expr
*pet_expr_update_domain(__isl_take pet_expr
*expr
,
1703 __isl_take isl_multi_pw_aff
*update
)
1705 expr
= pet_expr_map_access(expr
, &update_domain
, update
);
1706 isl_multi_pw_aff_free(update
);
1710 /* Given an expression with accesses that have a 0D anonymous domain,
1711 * replace those domains by "space".
1713 __isl_give pet_expr
*pet_expr_insert_domain(__isl_take pet_expr
*expr
,
1714 __isl_take isl_space
*space
)
1716 isl_multi_pw_aff
*mpa
;
1718 space
= isl_space_from_domain(space
);
1719 mpa
= isl_multi_pw_aff_zero(space
);
1720 return pet_expr_update_domain(expr
, mpa
);
1723 /* Add all parameters in "space" to the access relations (if any)
1724 * and index expression of "expr".
1726 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
, void *user
)
1728 isl_space
*space
= user
;
1729 enum pet_expr_access_type type
;
1731 expr
= pet_expr_cow(expr
);
1734 if (expr
->type
!= pet_expr_access
)
1735 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1736 "not an access expression", return pet_expr_free(expr
));
1738 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1739 if (!expr
->acc
.access
[type
])
1741 expr
->acc
.access
[type
] =
1742 isl_union_map_align_params(expr
->acc
.access
[type
],
1743 isl_space_copy(space
));
1744 if (!expr
->acc
.access
[type
])
1747 expr
->acc
.index
= isl_multi_pw_aff_align_params(expr
->acc
.index
,
1748 isl_space_copy(space
));
1749 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1750 return pet_expr_free(expr
);
1755 /* Add all parameters in "space" to all access relations and index expressions
1758 __isl_give pet_expr
*pet_expr_align_params(__isl_take pet_expr
*expr
,
1759 __isl_take isl_space
*space
)
1761 expr
= pet_expr_map_access(expr
, &align_params
, space
);
1762 isl_space_free(space
);
1766 /* Insert an argument expression corresponding to "test" in front
1767 * of the list of arguments described by *n_arg and *args.
1769 static __isl_give pet_expr
*insert_access_arg(__isl_take pet_expr
*expr
,
1770 __isl_keep isl_multi_pw_aff
*test
)
1773 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1776 return pet_expr_free(expr
);
1777 expr
= pet_expr_cow(expr
);
1782 expr
->args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1784 return pet_expr_free(expr
);
1787 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + expr
->n_arg
);
1789 return pet_expr_free(expr
);
1790 for (i
= 0; i
< expr
->n_arg
; ++i
)
1791 ext
[1 + i
] = expr
->args
[i
];
1796 expr
->args
[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1798 return pet_expr_free(expr
);
1803 /* Make the expression "expr" depend on the value of "test"
1804 * being equal to "satisfied".
1806 * If "test" is an affine expression, we simply add the conditions
1807 * on the expression having the value "satisfied" to all access relations
1808 * (introducing access relations if they are missing) and index expressions.
1810 * Otherwise, we add a filter to "expr" (which is then assumed to be
1811 * an access expression) corresponding to "test" being equal to "satisfied".
1813 __isl_give pet_expr
*pet_expr_filter(__isl_take pet_expr
*expr
,
1814 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1819 isl_pw_multi_aff
*pma
;
1820 enum pet_expr_access_type type
;
1822 expr
= pet_expr_cow(expr
);
1826 if (!isl_multi_pw_aff_has_tuple_id(test
, isl_dim_out
)) {
1830 pa
= isl_multi_pw_aff_get_pw_aff(test
, 0);
1831 isl_multi_pw_aff_free(test
);
1833 cond
= isl_pw_aff_non_zero_set(pa
);
1835 cond
= isl_pw_aff_zero_set(pa
);
1836 return pet_expr_restrict(expr
, cond
);
1839 ctx
= isl_multi_pw_aff_get_ctx(test
);
1840 if (expr
->type
!= pet_expr_access
)
1841 isl_die(ctx
, isl_error_invalid
,
1842 "can only filter access expressions", goto error
);
1844 expr
= introduce_access_relations(expr
);
1848 space
= isl_space_domain(isl_multi_pw_aff_get_space(expr
->acc
.index
));
1849 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1850 pma
= pet_filter_insert_pma(space
, id
, satisfied
);
1852 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1853 if (!expr
->acc
.access
[type
])
1855 expr
->acc
.access
[type
] =
1856 isl_union_map_preimage_domain_pw_multi_aff(
1857 expr
->acc
.access
[type
],
1858 isl_pw_multi_aff_copy(pma
));
1859 if (!expr
->acc
.access
[type
])
1862 pma
= isl_pw_multi_aff_gist(pma
,
1863 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
)));
1864 expr
->acc
.index
= isl_multi_pw_aff_pullback_pw_multi_aff(
1865 expr
->acc
.index
, pma
);
1866 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1869 expr
= insert_access_arg(expr
, test
);
1871 isl_multi_pw_aff_free(test
);
1874 isl_multi_pw_aff_free(test
);
1875 return pet_expr_free(expr
);
1878 /* Add a reference identifier to access expression "expr".
1879 * "user" points to an integer that contains the sequence number
1880 * of the next reference.
1882 static __isl_give pet_expr
*access_add_ref_id(__isl_take pet_expr
*expr
,
1889 expr
= pet_expr_cow(expr
);
1892 if (expr
->type
!= pet_expr_access
)
1893 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1894 "not an access expression", return pet_expr_free(expr
));
1896 ctx
= pet_expr_get_ctx(expr
);
1897 snprintf(name
, sizeof(name
), "__pet_ref_%d", (*n_ref
)++);
1898 expr
->acc
.ref_id
= isl_id_alloc(ctx
, name
, NULL
);
1899 if (!expr
->acc
.ref_id
)
1900 return pet_expr_free(expr
);
1905 __isl_give pet_expr
*pet_expr_add_ref_ids(__isl_take pet_expr
*expr
, int *n_ref
)
1907 return pet_expr_map_access(expr
, &access_add_ref_id
, n_ref
);
1910 /* Reset the user pointer on all parameter and tuple ids in
1911 * the access relations (if any) and the index expression
1912 * of the access expression "expr".
1914 static __isl_give pet_expr
*access_anonymize(__isl_take pet_expr
*expr
,
1917 enum pet_expr_access_type type
;
1919 expr
= pet_expr_cow(expr
);
1922 if (expr
->type
!= pet_expr_access
)
1923 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1924 "not an access expression", return pet_expr_free(expr
));
1926 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1927 if (!expr
->acc
.access
[type
])
1929 expr
->acc
.access
[type
] =
1930 isl_union_map_reset_user(expr
->acc
.access
[type
]);
1931 if (!expr
->acc
.access
[type
])
1934 expr
->acc
.index
= isl_multi_pw_aff_reset_user(expr
->acc
.index
);
1935 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1936 return pet_expr_free(expr
);
1941 __isl_give pet_expr
*pet_expr_anonymize(__isl_take pet_expr
*expr
)
1943 return pet_expr_map_access(expr
, &access_anonymize
, NULL
);
1946 /* Data used in access_gist() callback.
1948 struct pet_access_gist_data
{
1950 isl_union_map
*value_bounds
;
1953 /* Given an expression "expr" of type pet_expr_access, compute
1954 * the gist of the associated access relations (if any) and index expression
1955 * with respect to data->domain and the bounds on the values of the arguments
1956 * of the expression.
1958 * The arguments of "expr" have been gisted right before "expr" itself
1959 * is gisted. The gisted arguments may have become equal where before
1960 * they may not have been (obviously) equal. We therefore take
1961 * the opportunity to remove duplicate arguments here.
1963 static __isl_give pet_expr
*access_gist(__isl_take pet_expr
*expr
, void *user
)
1965 struct pet_access_gist_data
*data
= user
;
1967 isl_union_set
*uset
;
1968 enum pet_expr_access_type type
;
1970 expr
= pet_expr_remove_duplicate_args(expr
);
1971 expr
= pet_expr_cow(expr
);
1974 if (expr
->type
!= pet_expr_access
)
1975 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
1976 "not an access expression", return pet_expr_free(expr
));
1978 domain
= isl_set_copy(data
->domain
);
1979 if (expr
->n_arg
> 0)
1980 domain
= pet_value_bounds_apply(domain
, expr
->n_arg
, expr
->args
,
1981 data
->value_bounds
);
1983 uset
= isl_union_set_from_set(isl_set_copy(domain
));
1984 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
1985 if (!expr
->acc
.access
[type
])
1987 expr
->acc
.access
[type
] =
1988 isl_union_map_gist_domain(expr
->acc
.access
[type
],
1989 isl_union_set_copy(uset
));
1990 if (!expr
->acc
.access
[type
])
1993 isl_union_set_free(uset
);
1994 expr
->acc
.index
= isl_multi_pw_aff_gist(expr
->acc
.index
, domain
);
1995 if (type
< pet_expr_access_end
|| !expr
->acc
.index
)
1996 return pet_expr_free(expr
);
2001 __isl_give pet_expr
*pet_expr_gist(__isl_take pet_expr
*expr
,
2002 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2004 struct pet_access_gist_data data
= { context
, value_bounds
};
2006 return pet_expr_map_access(expr
, &access_gist
, &data
);
2009 /* Mark "expr" as a read dependening on "read".
2011 __isl_give pet_expr
*pet_expr_access_set_read(__isl_take pet_expr
*expr
,
2015 return pet_expr_free(expr
);
2016 if (expr
->type
!= pet_expr_access
)
2017 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2018 "not an access expression", return pet_expr_free(expr
));
2019 if (expr
->acc
.read
== read
)
2021 expr
= pet_expr_cow(expr
);
2024 expr
->acc
.read
= read
;
2029 /* Mark "expr" as a write dependening on "write".
2031 __isl_give pet_expr
*pet_expr_access_set_write(__isl_take pet_expr
*expr
,
2035 return pet_expr_free(expr
);
2036 if (expr
->type
!= pet_expr_access
)
2037 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2038 "not an access expression", return pet_expr_free(expr
));
2039 if (expr
->acc
.write
== write
)
2041 expr
= pet_expr_cow(expr
);
2044 expr
->acc
.write
= write
;
2049 /* Mark "expr" as a kill dependening on "kill".
2051 __isl_give pet_expr
*pet_expr_access_set_kill(__isl_take pet_expr
*expr
,
2055 return pet_expr_free(expr
);
2056 if (expr
->type
!= pet_expr_access
)
2057 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2058 "not an access expression", return pet_expr_free(expr
));
2059 if (expr
->acc
.kill
== kill
)
2061 expr
= pet_expr_cow(expr
);
2064 expr
->acc
.kill
= kill
;
2069 /* Map the access type "type" to the corresponding location
2070 * in the access array.
2071 * In particular, the access relation of type pet_expr_access_killed is
2072 * stored in the element at position pet_expr_access_fake_killed.
2074 static enum pet_expr_access_type
internalize_type(
2075 enum pet_expr_access_type type
)
2077 if (type
== pet_expr_access_killed
)
2078 return pet_expr_access_fake_killed
;
2082 /* Replace the access relation of the given "type" of "expr" by "access".
2083 * If the access relation is non-empty and the type is a read or a write,
2084 * then also mark the access expression itself as a read or a write.
2086 __isl_give pet_expr
*pet_expr_access_set_access(__isl_take pet_expr
*expr
,
2087 enum pet_expr_access_type type
, __isl_take isl_union_map
*access
)
2091 expr
= pet_expr_cow(expr
);
2092 if (!expr
|| !access
)
2094 if (expr
->type
!= pet_expr_access
)
2095 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2096 "not an access expression", goto error
);
2097 type
= internalize_type(type
);
2098 isl_union_map_free(expr
->acc
.access
[type
]);
2099 expr
->acc
.access
[type
] = access
;
2104 empty
= isl_union_map_is_empty(access
);
2106 return pet_expr_free(expr
);
2110 if (type
== pet_expr_access_may_read
)
2111 expr
= pet_expr_access_set_read(expr
, 1);
2113 expr
= pet_expr_access_set_write(expr
, 1);
2117 isl_union_map_free(access
);
2118 pet_expr_free(expr
);
2122 /* Replace the index expression of "expr" by "index" and
2123 * set the array depth accordingly.
2125 __isl_give pet_expr
*pet_expr_access_set_index(__isl_take pet_expr
*expr
,
2126 __isl_take isl_multi_pw_aff
*index
)
2128 expr
= pet_expr_cow(expr
);
2129 if (!expr
|| !index
)
2131 if (expr
->type
!= pet_expr_access
)
2132 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2133 "not an access expression", goto error
);
2134 isl_multi_pw_aff_free(expr
->acc
.index
);
2135 expr
->acc
.index
= index
;
2136 expr
->acc
.depth
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
2140 isl_multi_pw_aff_free(index
);
2141 pet_expr_free(expr
);
2145 /* Return the reference identifier of access expression "expr".
2147 __isl_give isl_id
*pet_expr_access_get_ref_id(__isl_keep pet_expr
*expr
)
2151 if (expr
->type
!= pet_expr_access
)
2152 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2153 "not an access expression", return NULL
);
2155 return isl_id_copy(expr
->acc
.ref_id
);
2158 /* Replace the reference identifier of access expression "expr" by "ref_id".
2160 __isl_give pet_expr
*pet_expr_access_set_ref_id(__isl_take pet_expr
*expr
,
2161 __isl_take isl_id
*ref_id
)
2163 expr
= pet_expr_cow(expr
);
2164 if (!expr
|| !ref_id
)
2166 if (expr
->type
!= pet_expr_access
)
2167 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2168 "not an access expression", goto error
);
2169 isl_id_free(expr
->acc
.ref_id
);
2170 expr
->acc
.ref_id
= ref_id
;
2174 isl_id_free(ref_id
);
2175 pet_expr_free(expr
);
2179 /* Tag the access relation "access" with "id".
2180 * That is, insert the id as the range of a wrapped relation
2181 * in the domain of "access".
2183 * If "access" is of the form
2187 * then the result is of the form
2189 * [D[i] -> id[]] -> A[a]
2191 __isl_give isl_union_map
*pet_expr_tag_access(__isl_keep pet_expr
*expr
,
2192 __isl_take isl_union_map
*access
)
2195 isl_multi_aff
*add_tag
;
2198 if (expr
->type
!= pet_expr_access
)
2199 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2200 "not an access expression",
2201 return isl_union_map_free(access
));
2203 id
= isl_id_copy(expr
->acc
.ref_id
);
2204 space
= pet_expr_access_get_domain_space(expr
);
2205 space
= isl_space_from_domain(space
);
2206 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2207 add_tag
= isl_multi_aff_domain_map(space
);
2208 access
= isl_union_map_preimage_domain_multi_aff(access
, add_tag
);
2213 /* Return the access relation of the given "type" associated to "expr"
2214 * that maps pairs of domain iterations and argument values
2215 * to the corresponding accessed data elements.
2217 * If the requested access relation is explicitly available,
2218 * then return a copy. Otherwise, check if it is irrelevant for
2219 * the access expression and return an empty relation if this is the case.
2220 * Otherwise, introduce the requested access relation in "expr" and
2223 __isl_give isl_union_map
*pet_expr_access_get_dependent_access(
2224 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2226 isl_union_map
*access
;
2231 if (expr
->type
!= pet_expr_access
)
2232 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2233 "not an access expression", return NULL
);
2235 type
= internalize_type(type
);
2236 if (expr
->acc
.access
[type
])
2237 return isl_union_map_copy(expr
->acc
.access
[type
]);
2239 if (type
== pet_expr_access_may_read
)
2240 empty
= !expr
->acc
.read
;
2242 empty
= !expr
->acc
.write
;
2245 expr
= pet_expr_copy(expr
);
2246 expr
= introduce_access_relations(expr
);
2249 access
= isl_union_map_copy(expr
->acc
.access
[type
]);
2250 pet_expr_free(expr
);
2255 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr
));
2258 /* Return the may read access relation associated to "expr"
2259 * that maps pairs of domain iterations and argument values
2260 * to the corresponding accessed data elements.
2262 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_read(
2263 __isl_keep pet_expr
*expr
)
2265 return pet_expr_access_get_dependent_access(expr
,
2266 pet_expr_access_may_read
);
2269 /* Return the may write access relation associated to "expr"
2270 * that maps pairs of domain iterations and argument values
2271 * to the corresponding accessed data elements.
2273 __isl_give isl_union_map
*pet_expr_access_get_dependent_may_write(
2274 __isl_keep pet_expr
*expr
)
2276 return pet_expr_access_get_dependent_access(expr
,
2277 pet_expr_access_may_write
);
2280 /* Return the must write access relation associated to "expr"
2281 * that maps pairs of domain iterations and argument values
2282 * to the corresponding accessed data elements.
2284 __isl_give isl_union_map
*pet_expr_access_get_dependent_must_write(
2285 __isl_keep pet_expr
*expr
)
2287 return pet_expr_access_get_dependent_access(expr
,
2288 pet_expr_access_must_write
);
2291 /* Return the relation of the given "type" mapping domain iterations
2292 * to the accessed data elements.
2293 * In particular, take the access relation and, in case of may_read
2294 * or may_write, project out the values of the arguments, if any.
2295 * In case of must_write, return the empty relation if there are
2298 __isl_give isl_union_map
*pet_expr_access_get_access(__isl_keep pet_expr
*expr
,
2299 enum pet_expr_access_type type
)
2301 isl_union_map
*access
;
2307 if (expr
->type
!= pet_expr_access
)
2308 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2309 "not an access expression", return NULL
);
2311 if (expr
->n_arg
!= 0 && type
== pet_expr_access_must_write
) {
2312 space
= pet_expr_access_get_parameter_space(expr
);
2313 return isl_union_map_empty(space
);
2316 access
= pet_expr_access_get_dependent_access(expr
, type
);
2317 if (expr
->n_arg
== 0)
2320 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
2321 space
= isl_space_domain(space
);
2322 map
= isl_map_universe(isl_space_unwrap(space
));
2323 map
= isl_map_domain_map(map
);
2324 access
= isl_union_map_apply_domain(access
,
2325 isl_union_map_from_map(map
));
2330 /* Return the relation mapping domain iterations to all possibly
2331 * read data elements.
2333 __isl_give isl_union_map
*pet_expr_access_get_may_read(
2334 __isl_keep pet_expr
*expr
)
2336 return pet_expr_access_get_access(expr
, pet_expr_access_may_read
);
2339 /* Return the relation mapping domain iterations to all possibly
2340 * written data elements.
2342 __isl_give isl_union_map
*pet_expr_access_get_may_write(
2343 __isl_keep pet_expr
*expr
)
2345 return pet_expr_access_get_access(expr
, pet_expr_access_may_write
);
2348 /* Return a relation mapping domain iterations to definitely
2349 * written data elements, assuming the statement containing
2350 * the expression is executed.
2352 __isl_give isl_union_map
*pet_expr_access_get_must_write(
2353 __isl_keep pet_expr
*expr
)
2355 return pet_expr_access_get_access(expr
, pet_expr_access_must_write
);
2358 /* Return the relation of the given "type" mapping domain iterations to
2359 * accessed data elements, with its domain tagged with the reference
2362 static __isl_give isl_union_map
*pet_expr_access_get_tagged_access(
2363 __isl_keep pet_expr
*expr
, enum pet_expr_access_type type
)
2365 isl_union_map
*access
;
2370 access
= pet_expr_access_get_access(expr
, type
);
2371 access
= pet_expr_tag_access(expr
, access
);
2376 /* Return the relation mapping domain iterations to all possibly
2377 * read data elements, with its domain tagged with the reference
2380 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_read(
2381 __isl_keep pet_expr
*expr
)
2383 return pet_expr_access_get_tagged_access(expr
,
2384 pet_expr_access_may_read
);
2387 /* Return the relation mapping domain iterations to all possibly
2388 * written data elements, with its domain tagged with the reference
2391 __isl_give isl_union_map
*pet_expr_access_get_tagged_may_write(
2392 __isl_keep pet_expr
*expr
)
2394 return pet_expr_access_get_tagged_access(expr
,
2395 pet_expr_access_may_write
);
2398 /* Return the operation type of operation expression "expr".
2400 enum pet_op_type
pet_expr_op_get_type(__isl_keep pet_expr
*expr
)
2404 if (expr
->type
!= pet_expr_op
)
2405 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2406 "not an operation expression", return pet_op_last
);
2411 /* Replace the operation type of operation expression "expr" by "type".
2413 __isl_give pet_expr
*pet_expr_op_set_type(__isl_take pet_expr
*expr
,
2414 enum pet_op_type type
)
2417 return pet_expr_free(expr
);
2418 if (expr
->type
!= pet_expr_op
)
2419 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2420 "not an operation expression",
2421 return pet_expr_free(expr
));
2422 if (expr
->op
== type
)
2424 expr
= pet_expr_cow(expr
);
2432 /* Return the name of the function called by "expr".
2434 __isl_keep
const char *pet_expr_call_get_name(__isl_keep pet_expr
*expr
)
2438 if (expr
->type
!= pet_expr_call
)
2439 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2440 "not a call expression", return NULL
);
2441 return expr
->c
.name
;
2444 /* Replace the name of the function called by "expr" by "name".
2446 __isl_give pet_expr
*pet_expr_call_set_name(__isl_take pet_expr
*expr
,
2447 __isl_keep
const char *name
)
2449 expr
= pet_expr_cow(expr
);
2451 return pet_expr_free(expr
);
2452 if (expr
->type
!= pet_expr_call
)
2453 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2454 "not a call expression", return pet_expr_free(expr
));
2456 expr
->c
.name
= strdup(name
);
2458 return pet_expr_free(expr
);
2462 /* Does the call expression "expr" have an associated function summary?
2464 int pet_expr_call_has_summary(__isl_keep pet_expr
*expr
)
2468 if (expr
->type
!= pet_expr_call
)
2469 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2470 "not a call expression", return -1);
2472 return expr
->c
.summary
!= NULL
;
2475 /* Return a copy of the function summary associated to
2476 * the call expression "expr".
2478 __isl_give pet_function_summary
*pet_expr_call_get_summary(
2479 __isl_keep pet_expr
*expr
)
2483 if (expr
->type
!= pet_expr_call
)
2484 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2485 "not a call expression", return NULL
);
2487 return pet_function_summary_copy(expr
->c
.summary
);
2490 /* Replace the function summary associated to the call expression "expr"
2493 __isl_give pet_expr
*pet_expr_call_set_summary(__isl_take pet_expr
*expr
,
2494 __isl_take pet_function_summary
*summary
)
2496 expr
= pet_expr_cow(expr
);
2497 if (!expr
|| !summary
)
2499 if (expr
->type
!= pet_expr_call
)
2500 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2501 "not a call expression", goto error
);
2502 pet_function_summary_free(expr
->c
.summary
);
2503 expr
->c
.summary
= summary
;
2506 pet_function_summary_free(summary
);
2507 return pet_expr_free(expr
);
2510 /* Replace the type of the cast performed by "expr" by "name".
2512 __isl_give pet_expr
*pet_expr_cast_set_type_name(__isl_take pet_expr
*expr
,
2513 __isl_keep
const char *name
)
2515 expr
= pet_expr_cow(expr
);
2517 return pet_expr_free(expr
);
2518 if (expr
->type
!= pet_expr_cast
)
2519 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2520 "not a cast expression", return pet_expr_free(expr
));
2521 free(expr
->type_name
);
2522 expr
->type_name
= strdup(name
);
2523 if (!expr
->type_name
)
2524 return pet_expr_free(expr
);
2528 /* Return the value of the integer represented by "expr".
2530 __isl_give isl_val
*pet_expr_int_get_val(__isl_keep pet_expr
*expr
)
2534 if (expr
->type
!= pet_expr_int
)
2535 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2536 "not an int expression", return NULL
);
2538 return isl_val_copy(expr
->i
);
2541 /* Replace the value of the integer represented by "expr" by "v".
2543 __isl_give pet_expr
*pet_expr_int_set_val(__isl_take pet_expr
*expr
,
2544 __isl_take isl_val
*v
)
2546 expr
= pet_expr_cow(expr
);
2549 if (expr
->type
!= pet_expr_int
)
2550 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2551 "not an int expression", goto error
);
2552 isl_val_free(expr
->i
);
2558 pet_expr_free(expr
);
2562 /* Replace the value and string representation of the double
2563 * represented by "expr" by "d" and "s".
2565 __isl_give pet_expr
*pet_expr_double_set(__isl_take pet_expr
*expr
,
2566 double d
, __isl_keep
const char *s
)
2568 expr
= pet_expr_cow(expr
);
2570 return pet_expr_free(expr
);
2571 if (expr
->type
!= pet_expr_double
)
2572 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2573 "not a double expression", return pet_expr_free(expr
));
2576 expr
->d
.s
= strdup(s
);
2578 return pet_expr_free(expr
);
2582 /* Return a string representation of the double expression "expr".
2584 __isl_give
char *pet_expr_double_get_str(__isl_keep pet_expr
*expr
)
2588 if (expr
->type
!= pet_expr_double
)
2589 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2590 "not a double expression", return NULL
);
2591 return strdup(expr
->d
.s
);
2594 /* Return a piecewise affine expression defined on the specified domain
2595 * that represents NaN.
2597 static __isl_give isl_pw_aff
*non_affine(__isl_take isl_space
*space
)
2599 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
2602 /* This function is called when we come across an access that is
2603 * nested in what is supposed to be an affine expression.
2604 * "pc" is the context in which the affine expression is created.
2605 * If nesting is allowed in "pc", we return an affine expression that is
2606 * equal to a new parameter corresponding to this nested access.
2607 * Otherwise, we return NaN.
2609 * Note that we currently don't allow nested accesses themselves
2610 * to contain any nested accesses, so we check if "expr" itself
2611 * involves any nested accesses (either explicitly as arguments
2612 * or implicitly through parameters) and return NaN if it does.
2614 * The new parameter is resolved in resolve_nested.
2616 static __isl_give isl_pw_aff
*nested_access(__isl_keep pet_expr
*expr
,
2617 __isl_keep pet_context
*pc
)
2622 isl_local_space
*ls
;
2628 if (!pet_context_allow_nesting(pc
))
2629 return non_affine(pet_context_get_space(pc
));
2631 if (pet_expr_get_type(expr
) != pet_expr_access
)
2632 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2633 "not an access expression", return NULL
);
2635 if (expr
->n_arg
> 0)
2636 return non_affine(pet_context_get_space(pc
));
2638 space
= pet_expr_access_get_parameter_space(expr
);
2639 nested
= pet_nested_any_in_space(space
);
2640 isl_space_free(space
);
2642 return non_affine(pet_context_get_space(pc
));
2644 ctx
= pet_expr_get_ctx(expr
);
2645 id
= pet_nested_pet_expr(pet_expr_copy(expr
));
2646 space
= pet_context_get_space(pc
);
2647 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2649 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
2650 ls
= isl_local_space_from_space(space
);
2651 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
2653 return isl_pw_aff_from_aff(aff
);
2656 /* Extract an affine expression from the access pet_expr "expr".
2657 * "pc" is the context in which the affine expression is created.
2659 * If "expr" is actually an affine expression rather than
2660 * a real access, then we return that expression.
2661 * Otherwise, we require that "expr" is of an integral type.
2662 * If not, we return NaN.
2664 * If the variable has been assigned a known affine expression,
2665 * then we return that expression.
2667 * Otherwise, we return an expression that is equal to a parameter
2668 * representing "expr" (if "allow_nested" is set).
2670 static __isl_give isl_pw_aff
*extract_affine_from_access(
2671 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
2676 if (pet_expr_is_affine(expr
)) {
2678 isl_multi_pw_aff
*mpa
;
2680 mpa
= pet_expr_access_get_index(expr
);
2681 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
2682 isl_multi_pw_aff_free(mpa
);
2686 if (pet_expr_get_type_size(expr
) == 0)
2687 return non_affine(pet_context_get_space(pc
));
2689 if (!pet_expr_is_scalar_access(expr
))
2690 return nested_access(expr
, pc
);
2692 id
= pet_expr_access_get_id(expr
);
2693 if (pet_context_is_assigned(pc
, id
))
2694 return pet_context_get_value(pc
, id
);
2697 return nested_access(expr
, pc
);
2700 /* Construct an affine expression from the integer constant "expr".
2701 * "pc" is the context in which the affine expression is created.
2703 static __isl_give isl_pw_aff
*extract_affine_from_int(__isl_keep pet_expr
*expr
,
2704 __isl_keep pet_context
*pc
)
2706 isl_local_space
*ls
;
2712 ls
= isl_local_space_from_space(pet_context_get_space(pc
));
2713 aff
= isl_aff_val_on_domain(ls
, pet_expr_int_get_val(expr
));
2715 return isl_pw_aff_from_aff(aff
);
2718 /* Extract an affine expression from an addition or subtraction operation.
2719 * Return NaN if we are unable to extract an affine expression.
2721 * "pc" is the context in which the affine expression is created.
2723 static __isl_give isl_pw_aff
*extract_affine_add_sub(__isl_keep pet_expr
*expr
,
2724 __isl_keep pet_context
*pc
)
2731 if (expr
->n_arg
!= 2)
2732 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2733 "expecting two arguments", return NULL
);
2735 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2736 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2738 switch (pet_expr_op_get_type(expr
)) {
2740 return isl_pw_aff_add(lhs
, rhs
);
2742 return isl_pw_aff_sub(lhs
, rhs
);
2744 isl_pw_aff_free(lhs
);
2745 isl_pw_aff_free(rhs
);
2746 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2747 "not an addition or subtraction operation",
2753 /* Extract an affine expression from an integer division or a modulo operation.
2754 * Return NaN if we are unable to extract an affine expression.
2756 * "pc" is the context in which the affine expression is created.
2758 * In particular, if "expr" is lhs/rhs, then return
2760 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2762 * If "expr" is lhs%rhs, then return
2764 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2766 * If the second argument (rhs) is not a (positive) integer constant,
2767 * then we fail to extract an affine expression.
2769 * We simplify the result in the context of the domain of "pc" in case
2770 * this domain implies that lhs >= 0 (or < 0).
2772 static __isl_give isl_pw_aff
*extract_affine_div_mod(__isl_keep pet_expr
*expr
,
2773 __isl_keep pet_context
*pc
)
2782 if (expr
->n_arg
!= 2)
2783 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2784 "expecting two arguments", return NULL
);
2786 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2788 is_cst
= isl_pw_aff_is_cst(rhs
);
2789 if (is_cst
< 0 || !is_cst
) {
2790 isl_pw_aff_free(rhs
);
2791 return non_affine(pet_context_get_space(pc
));
2794 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2796 switch (pet_expr_op_get_type(expr
)) {
2798 res
= isl_pw_aff_tdiv_q(lhs
, rhs
);
2801 res
= isl_pw_aff_tdiv_r(lhs
, rhs
);
2804 isl_pw_aff_free(lhs
);
2805 isl_pw_aff_free(rhs
);
2806 isl_die(pet_expr_get_ctx(expr
), isl_error_internal
,
2807 "not a div or mod operator", return NULL
);
2810 return isl_pw_aff_gist(res
, pet_context_get_gist_domain(pc
));
2813 /* Extract an affine expression from a multiplication operation.
2814 * Return NaN if we are unable to extract an affine expression.
2815 * In particular, if neither of the arguments is a (piecewise) constant
2816 * then we return NaN.
2818 * "pc" is the context in which the affine expression is created.
2820 static __isl_give isl_pw_aff
*extract_affine_mul(__isl_keep pet_expr
*expr
,
2821 __isl_keep pet_context
*pc
)
2823 int lhs_cst
, rhs_cst
;
2829 if (expr
->n_arg
!= 2)
2830 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2831 "expecting two arguments", return NULL
);
2833 lhs
= pet_expr_extract_affine(expr
->args
[0], pc
);
2834 rhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2836 lhs_cst
= isl_pw_aff_is_cst(lhs
);
2837 rhs_cst
= isl_pw_aff_is_cst(rhs
);
2838 if (lhs_cst
>= 0 && rhs_cst
>= 0 && (lhs_cst
|| rhs_cst
))
2839 return isl_pw_aff_mul(lhs
, rhs
);
2841 isl_pw_aff_free(lhs
);
2842 isl_pw_aff_free(rhs
);
2844 if (lhs_cst
< 0 || rhs_cst
< 0)
2847 return non_affine(pet_context_get_space(pc
));
2850 /* Extract an affine expression from a negation operation.
2851 * Return NaN if we are unable to extract an affine expression.
2853 * "pc" is the context in which the affine expression is created.
2855 static __isl_give isl_pw_aff
*extract_affine_neg(__isl_keep pet_expr
*expr
,
2856 __isl_keep pet_context
*pc
)
2862 if (expr
->n_arg
!= 1)
2863 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2864 "expecting one argument", return NULL
);
2866 res
= pet_expr_extract_affine(expr
->args
[0], pc
);
2867 return isl_pw_aff_neg(res
);
2870 /* Extract an affine expression from a conditional operation.
2871 * Return NaN if we are unable to extract an affine expression.
2873 * "pc" is the context in which the affine expression is created.
2875 static __isl_give isl_pw_aff
*extract_affine_cond(__isl_keep pet_expr
*expr
,
2876 __isl_keep pet_context
*pc
)
2878 isl_pw_aff
*cond
, *lhs
, *rhs
;
2882 if (expr
->n_arg
!= 3)
2883 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
2884 "expecting three arguments", return NULL
);
2886 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
2887 lhs
= pet_expr_extract_affine(expr
->args
[1], pc
);
2888 rhs
= pet_expr_extract_affine(expr
->args
[2], pc
);
2890 return isl_pw_aff_cond(cond
, lhs
, rhs
);
2897 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
, unsigned width
)
2902 ctx
= isl_pw_aff_get_ctx(pwaff
);
2903 mod
= isl_val_int_from_ui(ctx
, width
);
2904 mod
= isl_val_2exp(mod
);
2906 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
2911 /* Limit the domain of "pwaff" to those elements where the function
2914 * 2^{width-1} <= pwaff < 2^{width-1}
2916 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
2921 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
2922 isl_local_space
*ls
= isl_local_space_from_space(space
);
2927 ctx
= isl_pw_aff_get_ctx(pwaff
);
2928 v
= isl_val_int_from_ui(ctx
, width
- 1);
2929 v
= isl_val_2exp(v
);
2931 bound
= isl_aff_zero_on_domain(ls
);
2932 bound
= isl_aff_add_constant_val(bound
, v
);
2933 b
= isl_pw_aff_from_aff(bound
);
2935 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
2936 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2938 b
= isl_pw_aff_neg(b
);
2939 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
2940 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2945 /* Handle potential overflows on signed computations.
2947 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2948 * then we adjust the domain of "pa" to avoid overflows.
2950 static __isl_give isl_pw_aff
*signed_overflow(__isl_take isl_pw_aff
*pa
,
2954 struct pet_options
*options
;
2959 ctx
= isl_pw_aff_get_ctx(pa
);
2960 options
= isl_ctx_peek_pet_options(ctx
);
2961 if (!options
|| options
->signed_overflow
== PET_OVERFLOW_AVOID
)
2962 pa
= avoid_overflow(pa
, width
);
2967 /* Extract an affine expression from some an operation.
2968 * Return NaN if we are unable to extract an affine expression.
2969 * If the result of a binary (non boolean) operation is unsigned,
2970 * then we wrap it based on the size of the type. If the result is signed,
2971 * then we ensure that no overflow occurs.
2973 * "pc" is the context in which the affine expression is created.
2975 static __isl_give isl_pw_aff
*extract_affine_from_op(__isl_keep pet_expr
*expr
,
2976 __isl_keep pet_context
*pc
)
2981 switch (pet_expr_op_get_type(expr
)) {
2984 res
= extract_affine_add_sub(expr
, pc
);
2988 res
= extract_affine_div_mod(expr
, pc
);
2991 res
= extract_affine_mul(expr
, pc
);
2994 return extract_affine_neg(expr
, pc
);
2996 return extract_affine_cond(expr
, pc
);
3006 return pet_expr_extract_affine_condition(expr
, pc
);
3008 return non_affine(pet_context_get_space(pc
));
3013 if (isl_pw_aff_involves_nan(res
)) {
3014 isl_space
*space
= isl_pw_aff_get_domain_space(res
);
3015 isl_pw_aff_free(res
);
3016 return non_affine(space
);
3019 type_size
= pet_expr_get_type_size(expr
);
3021 res
= wrap(res
, type_size
);
3023 res
= signed_overflow(res
, -type_size
);
3028 /* Internal data structure for affine builtin function declarations.
3030 * "pencil" is set if the builtin is pencil specific.
3031 * "n_args" is the number of arguments the function takes.
3032 * "name" is the function name.
3034 struct affine_builtin_decl
{
3040 static struct affine_builtin_decl affine_builtins
[] = {
3048 { 0, 2, "intFloor" },
3049 { 0, 2, "intCeil" },
3054 /* List of min and max builtin functions.
3056 static const char *min_max_builtins
[] = {
3057 "min", "imin", "umin",
3058 "max", "imax", "umax"
3061 /* Is a function call to "name" with "n_args" arguments a call to a
3062 * builtin function for which we can construct an affine expression?
3063 * pencil specific builtins are only recognized if "pencil" is set.
3065 static int is_affine_builtin(int pencil
, int n_args
, const char *name
)
3069 for (i
= 0; i
< ARRAY_SIZE(affine_builtins
); ++i
) {
3070 struct affine_builtin_decl
*decl
= &affine_builtins
[i
];
3072 if (decl
->pencil
&& !pencil
)
3074 if (decl
->n_args
== n_args
&& !strcmp(decl
->name
, name
))
3081 /* Is function "name" a known min or max builtin function?
3083 static int is_min_or_max_builtin(const char *name
)
3087 for (i
= 0; i
< ARRAY_SIZE(min_max_builtins
); ++i
)
3088 if (!strcmp(min_max_builtins
[i
], name
))
3094 /* Extract an affine expression from some special function calls.
3095 * Return NaN if we are unable to extract an affine expression.
3096 * In particular, we handle "min", "max", "ceild", "floord",
3097 * "intMod", "intFloor" and "intCeil".
3098 * In case of the latter five, the second argument needs to be
3099 * a (positive) integer constant.
3100 * If the pencil option is set, then we also handle "{i,u}min" and
3103 * "pc" is the context in which the affine expression is created.
3105 static __isl_give isl_pw_aff
*extract_affine_from_call(
3106 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3109 isl_pw_aff
*aff1
, *aff2
;
3112 struct pet_options
*options
;
3116 ctx
= pet_expr_get_ctx(expr
);
3117 options
= isl_ctx_peek_pet_options(ctx
);
3119 n
= pet_expr_get_n_arg(expr
);
3120 name
= pet_expr_call_get_name(expr
);
3121 if (!is_affine_builtin(options
->pencil
, n
, name
))
3122 return non_affine(pet_context_get_space(pc
));
3124 if (is_min_or_max_builtin(name
)) {
3125 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3126 aff2
= pet_expr_extract_affine(expr
->args
[1], pc
);
3128 if (strstr(name
, "min"))
3129 aff1
= isl_pw_aff_min(aff1
, aff2
);
3131 aff1
= isl_pw_aff_max(aff1
, aff2
);
3132 } else if (!strcmp(name
, "intMod")) {
3135 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3136 return non_affine(pet_context_get_space(pc
));
3137 v
= pet_expr_int_get_val(expr
->args
[1]);
3138 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3139 aff1
= isl_pw_aff_mod_val(aff1
, v
);
3143 if (pet_expr_get_type(expr
->args
[1]) != pet_expr_int
)
3144 return non_affine(pet_context_get_space(pc
));
3145 v
= pet_expr_int_get_val(expr
->args
[1]);
3146 aff1
= pet_expr_extract_affine(expr
->args
[0], pc
);
3147 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
3148 if (!strcmp(name
, "floord") || !strcmp(name
, "intFloor"))
3149 aff1
= isl_pw_aff_floor(aff1
);
3151 aff1
= isl_pw_aff_ceil(aff1
);
3157 /* Extract an affine expression from "expr", if possible.
3158 * Otherwise return NaN.
3160 * "pc" is the context in which the affine expression is created.
3162 __isl_give isl_pw_aff
*pet_expr_extract_affine(__isl_keep pet_expr
*expr
,
3163 __isl_keep pet_context
*pc
)
3168 switch (pet_expr_get_type(expr
)) {
3169 case pet_expr_access
:
3170 return extract_affine_from_access(expr
, pc
);
3172 return extract_affine_from_int(expr
, pc
);
3174 return extract_affine_from_op(expr
, pc
);
3176 return extract_affine_from_call(expr
, pc
);
3178 case pet_expr_double
:
3179 case pet_expr_error
:
3180 return non_affine(pet_context_get_space(pc
));
3184 /* Extract an affine expressions representing the comparison "LHS op RHS"
3185 * Return NaN if we are unable to extract such an affine expression.
3187 * "pc" is the context in which the affine expression is created.
3189 * If the comparison is of the form
3193 * then the expression is constructed as the conjunction of
3198 * A similar optimization is performed for max(a,b) <= c.
3199 * We do this because that will lead to simpler representations
3200 * of the expression.
3201 * If isl is ever enhanced to explicitly deal with min and max expressions,
3202 * this optimization can be removed.
3204 __isl_give isl_pw_aff
*pet_expr_extract_comparison(enum pet_op_type op
,
3205 __isl_keep pet_expr
*lhs
, __isl_keep pet_expr
*rhs
,
3206 __isl_keep pet_context
*pc
)
3208 isl_pw_aff
*lhs_pa
, *rhs_pa
;
3210 if (op
== pet_op_gt
)
3211 return pet_expr_extract_comparison(pet_op_lt
, rhs
, lhs
, pc
);
3212 if (op
== pet_op_ge
)
3213 return pet_expr_extract_comparison(pet_op_le
, rhs
, lhs
, pc
);
3215 if (op
== pet_op_lt
|| op
== pet_op_le
) {
3216 if (pet_expr_is_min(rhs
)) {
3217 lhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3219 rhs_pa
= pet_expr_extract_comparison(op
, lhs
,
3221 return pet_and(lhs_pa
, rhs_pa
);
3223 if (pet_expr_is_max(lhs
)) {
3224 lhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[0],
3226 rhs_pa
= pet_expr_extract_comparison(op
, lhs
->args
[1],
3228 return pet_and(lhs_pa
, rhs_pa
);
3232 lhs_pa
= pet_expr_extract_affine(lhs
, pc
);
3233 rhs_pa
= pet_expr_extract_affine(rhs
, pc
);
3235 return pet_comparison(op
, lhs_pa
, rhs_pa
);
3238 /* Extract an affine expressions from the comparison "expr".
3239 * Return NaN if we are unable to extract such an affine expression.
3241 * "pc" is the context in which the affine expression is created.
3243 static __isl_give isl_pw_aff
*extract_comparison(__isl_keep pet_expr
*expr
,
3244 __isl_keep pet_context
*pc
)
3246 enum pet_op_type type
;
3250 if (expr
->n_arg
!= 2)
3251 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3252 "expecting two arguments", return NULL
);
3254 type
= pet_expr_op_get_type(expr
);
3255 return pet_expr_extract_comparison(type
, expr
->args
[0], expr
->args
[1],
3259 /* Extract an affine expression representing the boolean operation
3260 * expressed by "expr".
3261 * Return NaN if we are unable to extract an affine expression.
3263 * "pc" is the context in which the affine expression is created.
3265 static __isl_give isl_pw_aff
*extract_boolean(__isl_keep pet_expr
*expr
,
3266 __isl_keep pet_context
*pc
)
3268 isl_pw_aff
*lhs
, *rhs
;
3274 n
= pet_expr_get_n_arg(expr
);
3275 lhs
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3277 return pet_not(lhs
);
3279 rhs
= pet_expr_extract_affine_condition(expr
->args
[1], pc
);
3280 return pet_boolean(pet_expr_op_get_type(expr
), lhs
, rhs
);
3283 /* Extract the affine expression "expr != 0 ? 1 : 0".
3284 * Return NaN if we are unable to extract an affine expression.
3286 * "pc" is the context in which the affine expression is created.
3288 static __isl_give isl_pw_aff
*extract_implicit_condition(
3289 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3293 res
= pet_expr_extract_affine(expr
, pc
);
3294 return pet_to_bool(res
);
3297 /* Extract a boolean affine expression from "expr".
3298 * Return NaN if we are unable to extract an affine expression.
3300 * "pc" is the context in which the affine expression is created.
3302 * If "expr" is neither a comparison nor a boolean operation,
3303 * then we assume it is an affine expression and return the
3304 * boolean expression "expr != 0 ? 1 : 0".
3306 __isl_give isl_pw_aff
*pet_expr_extract_affine_condition(
3307 __isl_keep pet_expr
*expr
, __isl_keep pet_context
*pc
)
3312 if (pet_expr_is_comparison(expr
))
3313 return extract_comparison(expr
, pc
);
3314 if (pet_expr_is_boolean(expr
))
3315 return extract_boolean(expr
, pc
);
3317 return extract_implicit_condition(expr
, pc
);
3320 /* Check if "expr" is an assume expression and if its single argument
3321 * can be converted to an affine expression in the context of "pc".
3322 * If so, replace the argument by the affine expression.
3324 __isl_give pet_expr
*pet_expr_resolve_assume(__isl_take pet_expr
*expr
,
3325 __isl_keep pet_context
*pc
)
3328 isl_multi_pw_aff
*index
;
3332 if (!pet_expr_is_assume(expr
))
3334 if (expr
->n_arg
!= 1)
3335 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3336 "expecting one argument", return pet_expr_free(expr
));
3338 cond
= pet_expr_extract_affine_condition(expr
->args
[0], pc
);
3340 return pet_expr_free(expr
);
3341 if (isl_pw_aff_involves_nan(cond
)) {
3342 isl_pw_aff_free(cond
);
3346 index
= isl_multi_pw_aff_from_pw_aff(cond
);
3347 expr
= pet_expr_set_arg(expr
, 0, pet_expr_from_index(index
));
3352 /* Return the number of bits needed to represent the type of "expr".
3353 * See the description of the type_size field of pet_expr.
3355 int pet_expr_get_type_size(__isl_keep pet_expr
*expr
)
3357 return expr
? expr
->type_size
: 0;
3360 /* Replace the number of bits needed to represent the type of "expr"
3362 * See the description of the type_size field of pet_expr.
3364 __isl_give pet_expr
*pet_expr_set_type_size(__isl_take pet_expr
*expr
,
3367 expr
= pet_expr_cow(expr
);
3371 expr
->type_size
= type_size
;
3376 /* Extend an access expression "expr" with an additional index "index".
3377 * In particular, add "index" as an extra argument to "expr" and
3378 * adjust the index expression of "expr" to refer to this extra argument.
3379 * The caller is responsible for calling pet_expr_access_set_depth
3380 * to update the corresponding access relation.
3382 * Note that we only collect the individual index expressions as
3383 * arguments of "expr" here.
3384 * An attempt to integrate them into the index expression of "expr"
3385 * is performed in pet_expr_access_plug_in_args.
3387 __isl_give pet_expr
*pet_expr_access_subscript(__isl_take pet_expr
*expr
,
3388 __isl_take pet_expr
*index
)
3392 isl_local_space
*ls
;
3395 expr
= pet_expr_cow(expr
);
3396 if (!expr
|| !index
)
3398 if (expr
->type
!= pet_expr_access
)
3399 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3400 "not an access pet_expr", goto error
);
3402 n
= pet_expr_get_n_arg(expr
);
3403 expr
= pet_expr_insert_arg(expr
, n
, index
);
3407 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3408 ls
= isl_local_space_from_space(space
);
3409 pa
= isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, isl_dim_set
, n
));
3410 expr
->acc
.index
= pet_array_subscript(expr
->acc
.index
, pa
);
3411 if (!expr
->acc
.index
)
3412 return pet_expr_free(expr
);
3416 pet_expr_free(expr
);
3417 pet_expr_free(index
);
3421 /* Extend an access expression "expr" with an additional member acces to "id".
3422 * In particular, extend the index expression of "expr" to include
3423 * the additional member access.
3424 * The caller is responsible for calling pet_expr_access_set_depth
3425 * to update the corresponding access relation.
3427 __isl_give pet_expr
*pet_expr_access_member(__isl_take pet_expr
*expr
,
3428 __isl_take isl_id
*id
)
3431 isl_multi_pw_aff
*field_access
;
3433 expr
= pet_expr_cow(expr
);
3436 if (expr
->type
!= pet_expr_access
)
3437 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3438 "not an access pet_expr", goto error
);
3440 space
= isl_multi_pw_aff_get_domain_space(expr
->acc
.index
);
3441 space
= isl_space_from_domain(space
);
3442 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
3443 field_access
= isl_multi_pw_aff_zero(space
);
3444 expr
->acc
.index
= pet_array_member(expr
->acc
.index
, field_access
);
3445 if (!expr
->acc
.index
)
3446 return pet_expr_free(expr
);
3450 pet_expr_free(expr
);
3455 /* Prefix the access expression "expr" with "prefix".
3456 * If "add" is set, then it is not the index expression "prefix" itself
3457 * that was passed to the function, but its address.
3459 __isl_give pet_expr
*pet_expr_access_patch(__isl_take pet_expr
*expr
,
3460 __isl_take isl_multi_pw_aff
*prefix
, int add
)
3462 enum pet_expr_access_type type
;
3464 expr
= pet_expr_cow(expr
);
3465 if (!expr
|| !prefix
)
3467 if (expr
->type
!= pet_expr_access
)
3468 isl_die(pet_expr_get_ctx(expr
), isl_error_invalid
,
3469 "not an access pet_expr", goto error
);
3471 expr
->acc
.depth
+= isl_multi_pw_aff_dim(prefix
, isl_dim_out
) - add
;
3472 for (type
= pet_expr_access_begin
; type
< pet_expr_access_end
; ++type
) {
3473 if (!expr
->acc
.access
[type
])
3475 expr
->acc
.access
[type
] = pet_patch_union_map(
3476 isl_multi_pw_aff_copy(prefix
), expr
->acc
.access
[type
],
3478 if (!expr
->acc
.access
[type
])
3481 expr
->acc
.index
= pet_patch_multi_pw_aff(prefix
, expr
->acc
.index
, add
);
3482 if (!expr
->acc
.index
|| type
< pet_expr_access_end
)
3483 return pet_expr_free(expr
);
3487 pet_expr_free(expr
);
3488 isl_multi_pw_aff_free(prefix
);
3492 void pet_expr_dump_with_indent(__isl_keep pet_expr
*expr
, int indent
)
3499 fprintf(stderr
, "%*s", indent
, "");
3501 switch (expr
->type
) {
3502 case pet_expr_double
:
3503 fprintf(stderr
, "%s\n", expr
->d
.s
);
3506 isl_val_dump(expr
->i
);
3508 case pet_expr_access
:
3509 if (expr
->acc
.ref_id
) {
3510 isl_id_dump(expr
->acc
.ref_id
);
3511 fprintf(stderr
, "%*s", indent
, "");
3513 isl_multi_pw_aff_dump(expr
->acc
.index
);
3514 fprintf(stderr
, "%*sdepth: %d\n", indent
+ 2,
3515 "", expr
->acc
.depth
);
3516 if (expr
->acc
.kill
) {
3517 fprintf(stderr
, "%*skill: 1\n", indent
+ 2, "");
3519 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
3520 "", expr
->acc
.read
);
3521 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
3522 "", expr
->acc
.write
);
3524 if (expr
->acc
.access
[pet_expr_access_may_read
]) {
3525 fprintf(stderr
, "%*smay_read: ", indent
+ 2, "");
3527 expr
->acc
.access
[pet_expr_access_may_read
]);
3529 if (expr
->acc
.access
[pet_expr_access_may_write
]) {
3530 fprintf(stderr
, "%*smay_write: ", indent
+ 2, "");
3532 expr
->acc
.access
[pet_expr_access_may_write
]);
3534 if (expr
->acc
.access
[pet_expr_access_must_write
]) {
3535 fprintf(stderr
, "%*smust_write: ", indent
+ 2, "");
3537 expr
->acc
.access
[pet_expr_access_must_write
]);
3539 for (i
= 0; i
< expr
->n_arg
; ++i
)
3540 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3543 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
3544 for (i
= 0; i
< expr
->n_arg
; ++i
)
3545 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3548 fprintf(stderr
, "%s/%d\n", expr
->c
.name
, expr
->n_arg
);
3549 for (i
= 0; i
< expr
->n_arg
; ++i
)
3550 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3551 if (expr
->c
.summary
) {
3552 fprintf(stderr
, "%*s", indent
, "");
3553 fprintf(stderr
, "summary:\n");
3554 pet_function_summary_dump_with_indent(expr
->c
.summary
,
3559 fprintf(stderr
, "(%s)\n", expr
->type_name
);
3560 for (i
= 0; i
< expr
->n_arg
; ++i
)
3561 pet_expr_dump_with_indent(expr
->args
[i
], indent
+ 2);
3563 case pet_expr_error
:
3564 fprintf(stderr
, "ERROR\n");
3569 void pet_expr_dump(__isl_keep pet_expr
*expr
)
3571 pet_expr_dump_with_indent(expr
, 0);