postpone introduction of access relations
[pet.git] / expr.c
blob7576409a8bef2e0f71c12d021ba57574b1bd68e7
1 /*
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
7 * are met:
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
32 * Leiden University.
35 #include <string.h>
37 #include "aff.h"
38 #include "array.h"
39 #include "expr.h"
40 #include "expr_arg.h"
41 #include "filter.h"
42 #include "nest.h"
43 #include "options.h"
44 #include "value_bounds.h"
46 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
48 static char *type_str[] = {
49 [pet_expr_access] = "access",
50 [pet_expr_call] = "call",
51 [pet_expr_cast] = "cast",
52 [pet_expr_double] = "double",
53 [pet_expr_int] = "int",
54 [pet_expr_op] = "op",
57 static char *op_str[] = {
58 [pet_op_add_assign] = "+=",
59 [pet_op_sub_assign] = "-=",
60 [pet_op_mul_assign] = "*=",
61 [pet_op_div_assign] = "/=",
62 [pet_op_assign] = "=",
63 [pet_op_add] = "+",
64 [pet_op_sub] = "-",
65 [pet_op_mul] = "*",
66 [pet_op_div] = "/",
67 [pet_op_mod] = "%",
68 [pet_op_shl] = "<<",
69 [pet_op_shr] = ">>",
70 [pet_op_eq] = "==",
71 [pet_op_ne] = "!=",
72 [pet_op_le] = "<=",
73 [pet_op_ge] = ">=",
74 [pet_op_lt] = "<",
75 [pet_op_gt] = ">",
76 [pet_op_minus] = "-",
77 [pet_op_post_inc] = "++",
78 [pet_op_post_dec] = "--",
79 [pet_op_pre_inc] = "++",
80 [pet_op_pre_dec] = "--",
81 [pet_op_address_of] = "&",
82 [pet_op_and] = "&",
83 [pet_op_xor] = "^",
84 [pet_op_or] = "|",
85 [pet_op_not] = "~",
86 [pet_op_land] = "&&",
87 [pet_op_lor] = "||",
88 [pet_op_lnot] = "!",
89 [pet_op_cond] = "?:",
90 [pet_op_assume] = "assume",
91 [pet_op_kill] = "kill"
94 const char *pet_op_str(enum pet_op_type op)
96 return op_str[op];
99 int pet_op_is_inc_dec(enum pet_op_type op)
101 return op == pet_op_post_inc || op == pet_op_post_dec ||
102 op == pet_op_pre_inc || op == pet_op_pre_dec;
105 const char *pet_type_str(enum pet_expr_type type)
107 return type_str[type];
110 enum pet_op_type pet_str_op(const char *str)
112 int i;
114 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
115 if (!strcmp(op_str[i], str))
116 return i;
118 return -1;
121 enum pet_expr_type pet_str_type(const char *str)
123 int i;
125 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
126 if (!strcmp(type_str[i], str))
127 return i;
129 return -1;
132 /* Construct a pet_expr of the given type.
134 __isl_give pet_expr *pet_expr_alloc(isl_ctx *ctx, enum pet_expr_type type)
136 pet_expr *expr;
138 expr = isl_calloc_type(ctx, struct pet_expr);
139 if (!expr)
140 return NULL;
142 expr->ctx = ctx;
143 isl_ctx_ref(ctx);
144 expr->type = type;
145 expr->ref = 1;
147 return expr;
150 /* Construct an access pet_expr from an index expression.
151 * By default, the access is considered to be a read access.
152 * The initial depth is set from the index expression and
153 * may still be updated by the caller before the access relation
154 * is created.
156 __isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
158 isl_ctx *ctx;
159 pet_expr *expr;
161 if (!index)
162 return NULL;
163 ctx = isl_multi_pw_aff_get_ctx(index);
164 expr = pet_expr_alloc(ctx, pet_expr_access);
165 if (!expr)
166 goto error;
168 expr->acc.read = 1;
169 expr->acc.write = 0;
171 expr = pet_expr_access_set_index(expr, index);
173 return expr;
174 error:
175 isl_multi_pw_aff_free(index);
176 return NULL;
179 /* Construct an access pet_expr from an access relation and an index expression.
180 * By default, it is considered to be a read access.
182 __isl_give pet_expr *pet_expr_from_access_and_index(__isl_take isl_map *access,
183 __isl_take isl_multi_pw_aff *index)
185 int depth;
186 pet_expr *expr;
188 expr = pet_expr_from_index(index);
189 depth = isl_map_dim(access, isl_dim_out);
190 expr = pet_expr_access_set_depth(expr, depth);
191 return pet_expr_access_set_access(expr, access);
194 /* Extend the range of "access" with "n" dimensions, retaining
195 * the tuple identifier on this range.
197 * If "access" represents a member access, then extend the range
198 * of the member.
200 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
202 isl_id *id;
204 id = isl_map_get_tuple_id(access, isl_dim_out);
206 if (!isl_map_range_is_wrapping(access)) {
207 access = isl_map_add_dims(access, isl_dim_out, n);
208 } else {
209 isl_map *domain;
211 domain = isl_map_copy(access);
212 domain = isl_map_range_factor_domain(domain);
213 access = isl_map_range_factor_range(access);
214 access = extend_range(access, n);
215 access = isl_map_range_product(domain, access);
218 access = isl_map_set_tuple_id(access, isl_dim_out, id);
220 return access;
223 /* Does the access expression "expr" have an explicit access relation?
225 static int has_access_relation(__isl_keep pet_expr *expr)
227 if (!expr)
228 return -1;
230 if (expr->acc.access)
231 return 1;
233 return 0;
236 /* Replace the depth of the access expr "expr" by "depth".
238 * To avoid inconsistencies between the depth and the access relation,
239 * we currently do not allow the depth to change once the access relation
240 * has been set or computed.
242 __isl_give pet_expr *pet_expr_access_set_depth(__isl_take pet_expr *expr,
243 int depth)
245 isl_map *access;
246 int dim;
248 if (!expr)
249 return NULL;
250 if (expr->acc.depth == depth)
251 return expr;
252 if (has_access_relation(expr))
253 isl_die(pet_expr_get_ctx(expr), isl_error_unsupported,
254 "depth cannot be changed after access relation "
255 "has been set or computed", return pet_expr_free(expr));
257 expr = pet_expr_cow(expr);
258 if (!expr)
259 return NULL;
260 expr->acc.depth = depth;
262 return expr;
265 /* Construct a pet_expr that kills the elements specified by
266 * the index expression "index" and the access relation "access".
268 __isl_give pet_expr *pet_expr_kill_from_access_and_index(
269 __isl_take isl_map *access, __isl_take isl_multi_pw_aff *index)
271 pet_expr *expr;
273 if (!access || !index)
274 goto error;
276 expr = pet_expr_from_access_and_index(access, index);
277 expr = pet_expr_access_set_read(expr, 0);
278 return pet_expr_new_unary(pet_op_kill, expr);
279 error:
280 isl_map_free(access);
281 isl_multi_pw_aff_free(index);
282 return NULL;
285 /* Construct a unary pet_expr that performs "op" on "arg".
287 __isl_give pet_expr *pet_expr_new_unary(enum pet_op_type op,
288 __isl_take pet_expr *arg)
290 isl_ctx *ctx;
291 pet_expr *expr;
293 if (!arg)
294 return NULL;
295 ctx = pet_expr_get_ctx(arg);
296 expr = pet_expr_alloc(ctx, pet_expr_op);
297 expr = pet_expr_set_n_arg(expr, 1);
298 if (!expr)
299 goto error;
301 expr->op = op;
302 expr->args[pet_un_arg] = arg;
304 return expr;
305 error:
306 pet_expr_free(arg);
307 return NULL;
310 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
311 * where the result is represented using a type of "type_size" bits
312 * (may be zero if unknown or if the type is not an integer).
314 __isl_give pet_expr *pet_expr_new_binary(int type_size, enum pet_op_type op,
315 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
317 isl_ctx *ctx;
318 pet_expr *expr;
320 if (!lhs || !rhs)
321 goto error;
322 ctx = pet_expr_get_ctx(lhs);
323 expr = pet_expr_alloc(ctx, pet_expr_op);
324 expr = pet_expr_set_n_arg(expr, 2);
325 if (!expr)
326 goto error;
328 expr->op = op;
329 expr->type_size = type_size;
330 expr->args[pet_bin_lhs] = lhs;
331 expr->args[pet_bin_rhs] = rhs;
333 return expr;
334 error:
335 pet_expr_free(lhs);
336 pet_expr_free(rhs);
337 return NULL;
340 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
342 __isl_give pet_expr *pet_expr_new_ternary(__isl_take pet_expr *cond,
343 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
345 isl_ctx *ctx;
346 pet_expr *expr;
348 if (!cond || !lhs || !rhs)
349 goto error;
350 ctx = pet_expr_get_ctx(cond);
351 expr = pet_expr_alloc(ctx, pet_expr_op);
352 expr = pet_expr_set_n_arg(expr, 3);
353 if (!expr)
354 goto error;
356 expr->op = pet_op_cond;
357 expr->args[pet_ter_cond] = cond;
358 expr->args[pet_ter_true] = lhs;
359 expr->args[pet_ter_false] = rhs;
361 return expr;
362 error:
363 pet_expr_free(cond);
364 pet_expr_free(lhs);
365 pet_expr_free(rhs);
366 return NULL;
369 /* Construct a call pet_expr that calls function "name" with "n_arg"
370 * arguments. The caller is responsible for filling in the arguments.
372 __isl_give pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
373 unsigned n_arg)
375 pet_expr *expr;
377 expr = pet_expr_alloc(ctx, pet_expr_call);
378 expr = pet_expr_set_n_arg(expr, n_arg);
379 if (!expr)
380 return NULL;
382 expr->name = strdup(name);
383 if (!expr->name)
384 return pet_expr_free(expr);
386 return expr;
389 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
391 __isl_give pet_expr *pet_expr_new_cast(const char *type_name,
392 __isl_take pet_expr *arg)
394 isl_ctx *ctx;
395 pet_expr *expr;
397 if (!arg)
398 return NULL;
400 ctx = pet_expr_get_ctx(arg);
401 expr = pet_expr_alloc(ctx, pet_expr_cast);
402 expr = pet_expr_set_n_arg(expr, 1);
403 if (!expr)
404 goto error;
406 expr->type_name = strdup(type_name);
407 if (!expr->type_name)
408 goto error;
410 expr->args[0] = arg;
412 return expr;
413 error:
414 pet_expr_free(arg);
415 pet_expr_free(expr);
416 return NULL;
419 /* Construct a pet_expr that represents the double "d".
421 __isl_give pet_expr *pet_expr_new_double(isl_ctx *ctx,
422 double val, const char *s)
424 pet_expr *expr;
426 expr = pet_expr_alloc(ctx, pet_expr_double);
427 if (!expr)
428 return NULL;
430 expr->d.val = val;
431 expr->d.s = strdup(s);
432 if (!expr->d.s)
433 return pet_expr_free(expr);
435 return expr;
438 /* Construct a pet_expr that represents the integer value "v".
440 __isl_give pet_expr *pet_expr_new_int(__isl_take isl_val *v)
442 isl_ctx *ctx;
443 pet_expr *expr;
445 if (!v)
446 return NULL;
448 ctx = isl_val_get_ctx(v);
449 expr = pet_expr_alloc(ctx, pet_expr_int);
450 if (!expr)
451 goto error;
453 expr->i = v;
455 return expr;
456 error:
457 isl_val_free(v);
458 return NULL;
461 /* Return an independent duplicate of "expr".
463 * In case of an access expression, make sure the depth of the duplicate is set
464 * before the access relation (if any) is set and after the index expression
465 * is set.
467 static __isl_give pet_expr *pet_expr_dup(__isl_keep pet_expr *expr)
469 int i;
470 pet_expr *dup;
472 if (!expr)
473 return NULL;
475 dup = pet_expr_alloc(expr->ctx, expr->type);
476 dup = pet_expr_set_type_size(dup, expr->type_size);
477 dup = pet_expr_set_n_arg(dup, expr->n_arg);
478 for (i = 0; i < expr->n_arg; ++i)
479 dup = pet_expr_set_arg(dup, i, pet_expr_copy(expr->args[i]));
481 switch (expr->type) {
482 case pet_expr_access:
483 if (expr->acc.ref_id)
484 dup = pet_expr_access_set_ref_id(dup,
485 isl_id_copy(expr->acc.ref_id));
486 dup = pet_expr_access_set_index(dup,
487 isl_multi_pw_aff_copy(expr->acc.index));
488 dup = pet_expr_access_set_depth(dup, expr->acc.depth);
489 if (expr->acc.access)
490 dup = pet_expr_access_set_access(dup,
491 isl_map_copy(expr->acc.access));
492 dup = pet_expr_access_set_read(dup, expr->acc.read);
493 dup = pet_expr_access_set_write(dup, expr->acc.write);
494 break;
495 case pet_expr_call:
496 dup = pet_expr_call_set_name(dup, expr->name);
497 break;
498 case pet_expr_cast:
499 dup = pet_expr_cast_set_type_name(dup, expr->type_name);
500 break;
501 case pet_expr_double:
502 dup = pet_expr_double_set(dup, expr->d.val, expr->d.s);
503 break;
504 case pet_expr_int:
505 dup = pet_expr_int_set_val(dup, isl_val_copy(expr->i));
506 break;
507 case pet_expr_op:
508 dup = pet_expr_op_set_type(dup, expr->op);
509 break;
510 case pet_expr_error:
511 dup = pet_expr_free(dup);
512 break;
515 return dup;
518 __isl_give pet_expr *pet_expr_cow(__isl_take pet_expr *expr)
520 if (!expr)
521 return NULL;
523 if (expr->ref == 1)
524 return expr;
525 expr->ref--;
526 return pet_expr_dup(expr);
529 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr)
531 int i;
533 if (!expr)
534 return NULL;
535 if (--expr->ref > 0)
536 return NULL;
538 for (i = 0; i < expr->n_arg; ++i)
539 pet_expr_free(expr->args[i]);
540 free(expr->args);
542 switch (expr->type) {
543 case pet_expr_access:
544 isl_id_free(expr->acc.ref_id);
545 isl_map_free(expr->acc.access);
546 isl_multi_pw_aff_free(expr->acc.index);
547 break;
548 case pet_expr_call:
549 free(expr->name);
550 break;
551 case pet_expr_cast:
552 free(expr->type_name);
553 break;
554 case pet_expr_double:
555 free(expr->d.s);
556 break;
557 case pet_expr_int:
558 isl_val_free(expr->i);
559 break;
560 case pet_expr_op:
561 case pet_expr_error:
562 break;
565 isl_ctx_deref(expr->ctx);
566 free(expr);
567 return NULL;
570 /* Return an additional reference to "expr".
572 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr)
574 if (!expr)
575 return NULL;
577 expr->ref++;
578 return expr;
581 /* Return the isl_ctx in which "expr" was created.
583 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr)
585 return expr ? expr->ctx : NULL;
588 /* Return the type of "expr".
590 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr)
592 if (!expr)
593 return pet_expr_error;
594 return expr->type;
597 /* Return the number of arguments of "expr".
599 int pet_expr_get_n_arg(__isl_keep pet_expr *expr)
601 if (!expr)
602 return -1;
604 return expr->n_arg;
607 /* Set the number of arguments of "expr" to "n".
609 * If "expr" originally had more arguments, then remove the extra arguments.
610 * If "expr" originally had fewer arguments, then create space for
611 * the extra arguments ans initialize them to NULL.
613 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n)
615 int i;
616 pet_expr **args;
618 if (!expr)
619 return NULL;
620 if (expr->n_arg == n)
621 return expr;
622 expr = pet_expr_cow(expr);
623 if (!expr)
624 return NULL;
626 if (n < expr->n_arg) {
627 for (i = n; i < expr->n_arg; ++i)
628 pet_expr_free(expr->args[i]);
629 expr->n_arg = n;
630 return expr;
633 args = isl_realloc_array(expr->ctx, expr->args, pet_expr *, n);
634 if (!args)
635 return pet_expr_free(expr);
636 expr->args = args;
637 for (i = expr->n_arg; i < n; ++i)
638 expr->args[i] = NULL;
639 expr->n_arg = n;
641 return expr;
644 /* Return the argument of "expr" at position "pos".
646 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos)
648 if (!expr)
649 return NULL;
650 if (pos < 0 || pos >= expr->n_arg)
651 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
652 "position out of bounds", return NULL);
654 return pet_expr_copy(expr->args[pos]);
657 /* Replace the argument of "expr" at position "pos" by "arg".
659 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
660 __isl_take pet_expr *arg)
662 if (!expr || !arg)
663 goto error;
664 if (pos < 0 || pos >= expr->n_arg)
665 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
666 "position out of bounds", goto error);
667 if (expr->args[pos] == arg) {
668 pet_expr_free(arg);
669 return expr;
672 expr = pet_expr_cow(expr);
673 if (!expr)
674 goto error;
676 pet_expr_free(expr->args[pos]);
677 expr->args[pos] = arg;
679 return expr;
680 error:
681 pet_expr_free(expr);
682 pet_expr_free(arg);
683 return NULL;
686 /* Does "expr" perform a comparison operation?
688 int pet_expr_is_comparison(__isl_keep pet_expr *expr)
690 if (!expr)
691 return -1;
692 if (expr->type != pet_expr_op)
693 return 0;
694 switch (expr->op) {
695 case pet_op_eq:
696 case pet_op_ne:
697 case pet_op_le:
698 case pet_op_ge:
699 case pet_op_lt:
700 case pet_op_gt:
701 return 1;
702 default:
703 return 0;
707 /* Does "expr" perform a boolean operation?
709 int pet_expr_is_boolean(__isl_keep pet_expr *expr)
711 if (!expr)
712 return -1;
713 if (expr->type != pet_expr_op)
714 return 0;
715 switch (expr->op) {
716 case pet_op_land:
717 case pet_op_lor:
718 case pet_op_lnot:
719 return 1;
720 default:
721 return 0;
725 /* Is "expr" an assume statement?
727 int pet_expr_is_assume(__isl_keep pet_expr *expr)
729 if (!expr)
730 return -1;
731 if (expr->type != pet_expr_op)
732 return 0;
733 return expr->op == pet_op_assume;
736 /* Does "expr" perform a min operation?
738 int pet_expr_is_min(__isl_keep pet_expr *expr)
740 if (!expr)
741 return -1;
742 if (expr->type != pet_expr_call)
743 return 0;
744 if (expr->n_arg != 2)
745 return 0;
746 if (strcmp(expr->name, "min") != 0)
747 return 0;
748 return 1;
751 /* Does "expr" perform a max operation?
753 int pet_expr_is_max(__isl_keep pet_expr *expr)
755 if (!expr)
756 return -1;
757 if (expr->type != pet_expr_call)
758 return 0;
759 if (expr->n_arg != 2)
760 return 0;
761 if (strcmp(expr->name, "max") != 0)
762 return 0;
763 return 1;
766 /* Does "expr" represent an access to an unnamed space, i.e.,
767 * does it represent an affine expression?
769 int pet_expr_is_affine(__isl_keep pet_expr *expr)
771 int has_id;
773 if (!expr)
774 return -1;
775 if (expr->type != pet_expr_access)
776 return 0;
778 has_id = isl_multi_pw_aff_has_tuple_id(expr->acc.index, isl_dim_out);
779 if (has_id < 0)
780 return -1;
782 return !has_id;
785 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
786 * not part of any struct?
788 int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
790 if (!expr)
791 return -1;
792 if (expr->type != pet_expr_access)
793 return 0;
794 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index))
795 return 0;
797 return expr->acc.depth == 0;
800 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
801 * of parameters.
803 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
804 __isl_keep isl_multi_pw_aff *mpa2)
806 int equal;
808 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
809 if (equal < 0 || equal)
810 return equal;
811 mpa2 = isl_multi_pw_aff_copy(mpa2);
812 mpa2 = isl_multi_pw_aff_align_params(mpa2,
813 isl_multi_pw_aff_get_space(mpa1));
814 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
815 isl_multi_pw_aff_free(mpa2);
817 return equal;
820 /* Construct an access relation from the index expression and
821 * the array depth of the access expression "expr".
823 * If the number of indices is smaller than the depth of the array,
824 * then we assume that all elements of the remaining dimensions
825 * are accessed.
827 static __isl_give isl_map *construct_access_relation(__isl_keep pet_expr *expr)
829 isl_map *access;
830 int dim;
831 int read, write;
833 if (!expr)
834 return NULL;
836 access = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
837 if (!access)
838 return NULL;
840 dim = isl_map_dim(access, isl_dim_out);
841 if (dim > expr->acc.depth)
842 isl_die(isl_map_get_ctx(access), isl_error_internal,
843 "number of indices greater than depth",
844 access = isl_map_free(access));
846 if (dim != expr->acc.depth)
847 access = extend_range(access, expr->acc.depth - dim);
849 return access;
852 /* Ensure that "expr" has an explicit access relation.
854 * If "expr" does not already have an access relation, then create
855 * one based on the index expression and the array depth.
857 * We do not cow since adding an explicit access relation
858 * does not change the meaning of the expression.
860 static __isl_give pet_expr *introduce_access_relation(
861 __isl_take pet_expr *expr)
863 isl_map *access;
864 int dim;
866 if (!expr)
867 return NULL;
868 if (has_access_relation(expr))
869 return expr;
871 access = construct_access_relation(expr);
872 if (!access)
873 return pet_expr_free(expr);
875 expr->acc.access = access;
877 return expr;
880 /* Return 1 if the two pet_exprs are equivalent.
882 int pet_expr_is_equal(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2)
884 int i;
886 if (!expr1 || !expr2)
887 return 0;
889 if (expr1->type != expr2->type)
890 return 0;
891 if (expr1->n_arg != expr2->n_arg)
892 return 0;
893 for (i = 0; i < expr1->n_arg; ++i)
894 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
895 return 0;
896 switch (expr1->type) {
897 case pet_expr_error:
898 return -1;
899 case pet_expr_double:
900 if (strcmp(expr1->d.s, expr2->d.s))
901 return 0;
902 if (expr1->d.val != expr2->d.val)
903 return 0;
904 break;
905 case pet_expr_int:
906 if (!isl_val_eq(expr1->i, expr2->i))
907 return 0;
908 break;
909 case pet_expr_access:
910 if (expr1->acc.read != expr2->acc.read)
911 return 0;
912 if (expr1->acc.write != expr2->acc.write)
913 return 0;
914 if (expr1->acc.ref_id != expr2->acc.ref_id)
915 return 0;
916 if (!expr1->acc.index || !expr2->acc.index)
917 return 0;
918 if (!multi_pw_aff_is_equal(expr1->acc.index, expr2->acc.index))
919 return 0;
920 if (expr1->acc.depth != expr2->acc.depth)
921 return 0;
922 if (has_access_relation(expr1) != has_access_relation(expr2)) {
923 int equal;
924 expr1 = pet_expr_copy(expr1);
925 expr2 = pet_expr_copy(expr2);
926 expr1 = introduce_access_relation(expr1);
927 expr2 = introduce_access_relation(expr2);
928 equal = pet_expr_is_equal(expr1, expr2);
929 pet_expr_free(expr1);
930 pet_expr_free(expr2);
931 return equal;
933 if (expr1->acc.access &&
934 !isl_map_is_equal(expr1->acc.access, expr2->acc.access))
935 return 0;
936 break;
937 case pet_expr_op:
938 if (expr1->op != expr2->op)
939 return 0;
940 break;
941 case pet_expr_call:
942 if (strcmp(expr1->name, expr2->name))
943 return 0;
944 break;
945 case pet_expr_cast:
946 if (strcmp(expr1->type_name, expr2->type_name))
947 return 0;
948 break;
951 return 1;
954 /* Does the access expression "expr" read the accessed elements?
956 int pet_expr_access_is_read(__isl_keep pet_expr *expr)
958 if (!expr)
959 return -1;
960 if (expr->type != pet_expr_access)
961 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
962 "not an access expression", return -1);
964 return expr->acc.read;
967 /* Does the access expression "expr" write to the accessed elements?
969 int pet_expr_access_is_write(__isl_keep pet_expr *expr)
971 if (!expr)
972 return -1;
973 if (expr->type != pet_expr_access)
974 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
975 "not an access expression", return -1);
977 return expr->acc.write;
980 /* Return the identifier of the array accessed by "expr".
982 * If "expr" represents a member access, then return the identifier
983 * of the outer structure array.
985 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
987 if (!expr)
988 return NULL;
989 if (expr->type != pet_expr_access)
990 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
991 "not an access expression", return NULL);
993 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index)) {
994 isl_space *space;
995 isl_id *id;
997 space = isl_multi_pw_aff_get_space(expr->acc.index);
998 space = isl_space_range(space);
999 while (space && isl_space_is_wrapping(space))
1000 space = isl_space_domain(isl_space_unwrap(space));
1001 id = isl_space_get_tuple_id(space, isl_dim_set);
1002 isl_space_free(space);
1004 return id;
1007 return isl_multi_pw_aff_get_tuple_id(expr->acc.index, isl_dim_out);
1010 /* Return the parameter space of "expr".
1012 __isl_give isl_space *pet_expr_access_get_parameter_space(
1013 __isl_keep pet_expr *expr)
1015 isl_space *space;
1017 if (!expr)
1018 return NULL;
1019 if (expr->type != pet_expr_access)
1020 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1021 "not an access expression", return NULL);
1023 space = isl_multi_pw_aff_get_space(expr->acc.index);
1024 space = isl_space_params(space);
1026 return space;
1029 /* Return the domain space of "expr", without the arguments (if any).
1031 __isl_give isl_space *pet_expr_access_get_domain_space(
1032 __isl_keep pet_expr *expr)
1034 isl_space *space;
1036 if (!expr)
1037 return NULL;
1038 if (expr->type != pet_expr_access)
1039 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1040 "not an access expression", return NULL);
1042 space = isl_multi_pw_aff_get_space(expr->acc.index);
1043 space = isl_space_domain(space);
1044 if (isl_space_is_wrapping(space))
1045 space = isl_space_domain(isl_space_unwrap(space));
1047 return space;
1050 /* Return the space of the data accessed by "expr".
1052 __isl_give isl_space *pet_expr_access_get_data_space(__isl_keep pet_expr *expr)
1054 isl_space *space;
1056 if (!expr)
1057 return NULL;
1058 if (expr->type != pet_expr_access)
1059 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1060 "not an access expression", return NULL);
1062 space = isl_multi_pw_aff_get_space(expr->acc.index);
1063 space = isl_space_range(space);
1065 return space;
1068 /* Modify all expressions of type pet_expr_access in "expr"
1069 * by calling "fn" on them.
1071 __isl_give pet_expr *pet_expr_map_access(__isl_take pet_expr *expr,
1072 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1073 void *user)
1075 int i, n;
1077 n = pet_expr_get_n_arg(expr);
1078 for (i = 0; i < n; ++i) {
1079 pet_expr *arg = pet_expr_get_arg(expr, i);
1080 arg = pet_expr_map_access(arg, fn, user);
1081 expr = pet_expr_set_arg(expr, i, arg);
1084 if (!expr)
1085 return NULL;
1087 if (expr->type == pet_expr_access)
1088 expr = fn(expr, user);
1090 return expr;
1093 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1095 * Return -1 on error (where fn returning a negative value is treated as
1096 * an error).
1097 * Otherwise return 0.
1099 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
1100 enum pet_expr_type type,
1101 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1103 int i;
1105 if (!expr)
1106 return -1;
1108 for (i = 0; i < expr->n_arg; ++i)
1109 if (pet_expr_foreach_expr_of_type(expr->args[i],
1110 type, fn, user) < 0)
1111 return -1;
1113 if (expr->type == type)
1114 return fn(expr, user);
1116 return 0;
1119 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1121 * Return -1 on error (where fn returning a negative value is treated as
1122 * an error).
1123 * Otherwise return 0.
1125 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
1126 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1128 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
1131 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1133 * Return -1 on error (where fn returning a negative value is treated as
1134 * an error).
1135 * Otherwise return 0.
1137 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
1138 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1140 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1143 /* Internal data structure for pet_expr_writes.
1144 * "id" is the identifier that we are looking for.
1145 * "found" is set if we have found the identifier being written to.
1147 struct pet_expr_writes_data {
1148 isl_id *id;
1149 int found;
1152 /* Given an access expression, check if it writes to data->id.
1153 * If so, set data->found and abort the search.
1155 static int writes(__isl_keep pet_expr *expr, void *user)
1157 struct pet_expr_writes_data *data = user;
1158 isl_id *write_id;
1160 if (!expr->acc.write)
1161 return 0;
1162 if (pet_expr_is_affine(expr))
1163 return 0;
1165 write_id = pet_expr_access_get_id(expr);
1166 isl_id_free(write_id);
1168 if (!write_id)
1169 return -1;
1171 if (write_id != data->id)
1172 return 0;
1174 data->found = 1;
1175 return -1;
1178 /* Does expression "expr" write to "id"?
1180 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1182 struct pet_expr_writes_data data;
1184 data.id = id;
1185 data.found = 0;
1186 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1187 !data.found)
1188 return -1;
1190 return data.found;
1193 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1194 * index expression and access relation of "expr" (if any)
1195 * to dimensions of "dst_type" at "dst_pos".
1197 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1198 enum isl_dim_type dst_type, unsigned dst_pos,
1199 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1201 expr = pet_expr_cow(expr);
1202 if (!expr)
1203 return NULL;
1204 if (expr->type != pet_expr_access)
1205 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1206 "not an access pet_expr", return pet_expr_free(expr));
1208 if (expr->acc.access) {
1209 expr->acc.access = isl_map_move_dims(expr->acc.access,
1210 dst_type, dst_pos, src_type, src_pos, n);
1211 if (!expr->acc.access)
1212 expr->acc.index =
1213 isl_multi_pw_aff_free(expr->acc.index);
1215 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1216 dst_type, dst_pos, src_type, src_pos, n);
1217 if (!expr->acc.index)
1218 return pet_expr_free(expr);
1220 return expr;
1223 /* Replace the index expression and access relation (if any) of "expr"
1224 * by their preimages under the function represented by "ma".
1226 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1227 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1229 expr = pet_expr_cow(expr);
1230 if (!expr || !ma)
1231 goto error;
1232 if (expr->type != pet_expr_access)
1233 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1234 "not an access pet_expr", goto error);
1236 if (expr->acc.access) {
1237 expr->acc.access = isl_map_preimage_domain_multi_aff(
1238 expr->acc.access, isl_multi_aff_copy(ma));
1239 if (!expr->acc.access)
1240 expr->acc.index =
1241 isl_multi_pw_aff_free(expr->acc.index);
1243 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1244 ma);
1245 if (!expr->acc.index)
1246 return pet_expr_free(expr);
1248 return expr;
1249 error:
1250 isl_multi_aff_free(ma);
1251 pet_expr_free(expr);
1252 return NULL;
1255 /* Replace the index expression and access relation (if any) of "expr"
1256 * by their preimages under the function represented by "mpa".
1258 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1259 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1261 expr = pet_expr_cow(expr);
1262 if (!expr || !mpa)
1263 goto error;
1264 if (expr->type != pet_expr_access)
1265 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1266 "not an access pet_expr", goto error);
1268 if (expr->acc.access) {
1269 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
1270 expr->acc.access, isl_multi_pw_aff_copy(mpa));
1271 if (!expr->acc.access)
1272 expr->acc.index =
1273 isl_multi_pw_aff_free(expr->acc.index);
1275 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1276 expr->acc.index, mpa);
1277 if (!expr->acc.index)
1278 return pet_expr_free(expr);
1280 return expr;
1281 error:
1282 isl_multi_pw_aff_free(mpa);
1283 pet_expr_free(expr);
1284 return NULL;
1287 /* Return the index expression of access expression "expr".
1289 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1290 __isl_keep pet_expr *expr)
1292 if (!expr)
1293 return NULL;
1294 if (expr->type != pet_expr_access)
1295 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1296 "not an access expression", return NULL);
1298 return isl_multi_pw_aff_copy(expr->acc.index);
1301 /* Align the parameters of expr->acc.index and expr->acc.access (if set).
1303 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1305 expr = pet_expr_cow(expr);
1306 if (!expr)
1307 return NULL;
1308 if (expr->type != pet_expr_access)
1309 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1310 "not an access expression", return pet_expr_free(expr));
1312 if (!has_access_relation(expr))
1313 return expr;
1315 expr->acc.access = isl_map_align_params(expr->acc.access,
1316 isl_multi_pw_aff_get_space(expr->acc.index));
1317 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1318 isl_map_get_space(expr->acc.access));
1319 if (!expr->acc.access || !expr->acc.index)
1320 return pet_expr_free(expr);
1322 return expr;
1325 /* Are "expr1" and "expr2" both array accesses such that
1326 * the access relation of "expr1" is a subset of that of "expr2"?
1327 * Only take into account the first "n_arg" arguments.
1329 * This function is tailored for use by mark_self_dependences in nest.c.
1330 * In particular, the input expressions may have more than "n_arg"
1331 * elements in their arguments arrays, while only the first "n_arg"
1332 * elements are referenced from the access relations.
1334 int pet_expr_is_sub_access(__isl_keep pet_expr *expr1,
1335 __isl_keep pet_expr *expr2, int n_arg)
1337 isl_id *id1, *id2;
1338 int i, n1, n2;
1339 int is_subset;
1341 if (!expr1 || !expr2)
1342 return 0;
1343 if (pet_expr_get_type(expr1) != pet_expr_access)
1344 return 0;
1345 if (pet_expr_get_type(expr2) != pet_expr_access)
1346 return 0;
1347 if (pet_expr_is_affine(expr1))
1348 return 0;
1349 if (pet_expr_is_affine(expr2))
1350 return 0;
1351 n1 = pet_expr_get_n_arg(expr1);
1352 if (n1 > n_arg)
1353 n1 = n_arg;
1354 n2 = pet_expr_get_n_arg(expr2);
1355 if (n2 > n_arg)
1356 n2 = n_arg;
1357 if (n1 != n2)
1358 return 0;
1359 for (i = 0; i < n1; ++i) {
1360 int equal;
1361 equal = pet_expr_is_equal(expr1->args[i], expr2->args[i]);
1362 if (equal < 0 || !equal)
1363 return equal;
1365 id1 = pet_expr_access_get_id(expr1);
1366 id2 = pet_expr_access_get_id(expr2);
1367 isl_id_free(id1);
1368 isl_id_free(id2);
1369 if (!id1 || !id2)
1370 return 0;
1371 if (id1 != id2)
1372 return 0;
1374 expr1 = pet_expr_copy(expr1);
1375 expr2 = pet_expr_copy(expr2);
1376 expr1 = introduce_access_relation(expr1);
1377 expr2 = introduce_access_relation(expr2);
1378 if (!expr1 || !expr2)
1379 goto error;
1381 is_subset = isl_map_is_subset(expr1->acc.access, expr2->acc.access);
1383 pet_expr_free(expr1);
1384 pet_expr_free(expr2);
1386 return is_subset;
1387 error:
1388 pet_expr_free(expr1);
1389 pet_expr_free(expr2);
1390 return -1;
1393 /* Given a set in the iteration space "domain", extend it to live in the space
1394 * of the domain of access relations.
1396 * That, is the number of arguments "n" is 0, then simply return domain.
1397 * Otherwise, return [domain -> [a_1,...,a_n]].
1399 static __isl_give isl_set *add_arguments(__isl_take isl_set *domain, int n)
1401 isl_map *map;
1403 if (n == 0)
1404 return domain;
1406 map = isl_map_from_domain(domain);
1407 map = isl_map_add_dims(map, isl_dim_out, n);
1408 return isl_map_wrap(map);
1411 /* Add extra conditions to the domains of all access relations in "expr",
1412 * introducing access relations if they are not already present.
1414 * The conditions are not added to the index expression. Instead, they
1415 * are used to try and simplify the index expression.
1417 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1418 __isl_take isl_set *cond)
1420 int i;
1422 expr = pet_expr_cow(expr);
1423 if (!expr)
1424 goto error;
1426 for (i = 0; i < expr->n_arg; ++i) {
1427 expr->args[i] = pet_expr_restrict(expr->args[i],
1428 isl_set_copy(cond));
1429 if (!expr->args[i])
1430 goto error;
1433 if (expr->type != pet_expr_access) {
1434 isl_set_free(cond);
1435 return expr;
1438 expr = introduce_access_relation(expr);
1439 if (!expr)
1440 goto error;
1442 cond = add_arguments(cond, expr->n_arg);
1443 expr->acc.access = isl_map_intersect_domain(expr->acc.access,
1444 isl_set_copy(cond));
1445 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, cond);
1446 if (!expr->acc.access || !expr->acc.index)
1447 return pet_expr_free(expr);
1449 return expr;
1450 error:
1451 isl_set_free(cond);
1452 return pet_expr_free(expr);
1455 /* Modify the access relation (if any) and index expression
1456 * of the given access expression
1457 * based on the given iteration space transformation.
1458 * In particular, precompose the access relation and index expression
1459 * with the update function.
1461 * If the access has any arguments then the domain of the access relation
1462 * is a wrapped mapping from the iteration space to the space of
1463 * argument values. We only need to change the domain of this wrapped
1464 * mapping, so we extend the input transformation with an identity mapping
1465 * on the space of argument values.
1467 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1468 __isl_keep isl_multi_pw_aff *update)
1470 expr = pet_expr_cow(expr);
1471 if (!expr)
1472 return NULL;
1473 if (expr->type != pet_expr_access)
1474 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1475 "not an access expression", return pet_expr_free(expr));
1477 update = isl_multi_pw_aff_copy(update);
1479 if (expr->n_arg > 0) {
1480 isl_space *space;
1481 isl_multi_pw_aff *id;
1483 space = isl_multi_pw_aff_get_space(expr->acc.index);
1484 space = isl_space_domain(space);
1485 space = isl_space_unwrap(space);
1486 space = isl_space_range(space);
1487 space = isl_space_map_from_set(space);
1488 id = isl_multi_pw_aff_identity(space);
1489 update = isl_multi_pw_aff_product(update, id);
1492 if (expr->acc.access) {
1493 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
1494 expr->acc.access,
1495 isl_multi_pw_aff_copy(update));
1496 if (!expr->acc.access)
1497 expr->acc.index =
1498 isl_multi_pw_aff_free(expr->acc.index);
1500 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1501 expr->acc.index, update);
1502 if (!expr->acc.index)
1503 return pet_expr_free(expr);
1505 return expr;
1508 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1510 isl_multi_pw_aff *update = user;
1512 return pet_expr_access_update_domain(expr, update);
1515 /* Modify all access relations in "expr" by precomposing them with
1516 * the given iteration space transformation.
1518 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1519 __isl_take isl_multi_pw_aff *update)
1521 expr = pet_expr_map_access(expr, &update_domain, update);
1522 isl_multi_pw_aff_free(update);
1523 return expr;
1526 /* Given an expression with accesses that have a 0D anonymous domain,
1527 * replace those domains by "space".
1529 __isl_give pet_expr *pet_expr_insert_domain(__isl_take pet_expr *expr,
1530 __isl_take isl_space *space)
1532 isl_multi_pw_aff *mpa;
1534 space = isl_space_from_domain(space);
1535 mpa = isl_multi_pw_aff_zero(space);
1536 return pet_expr_update_domain(expr, mpa);
1539 /* Add all parameters in "space" to the access relation (if any)
1540 * and index expression of "expr".
1542 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1544 isl_space *space = user;
1546 expr = pet_expr_cow(expr);
1547 if (!expr)
1548 return NULL;
1549 if (expr->type != pet_expr_access)
1550 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1551 "not an access expression", return pet_expr_free(expr));
1553 if (expr->acc.access) {
1554 expr->acc.access = isl_map_align_params(expr->acc.access,
1555 isl_space_copy(space));
1556 if (!expr->acc.access)
1557 expr->acc.index =
1558 isl_multi_pw_aff_free(expr->acc.index);
1560 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1561 isl_space_copy(space));
1562 if (!expr->acc.index)
1563 return pet_expr_free(expr);
1565 return expr;
1568 /* Add all parameters in "space" to all access relations and index expressions
1569 * in "expr".
1571 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1572 __isl_take isl_space *space)
1574 expr = pet_expr_map_access(expr, &align_params, space);
1575 isl_space_free(space);
1576 return expr;
1579 /* Insert an argument expression corresponding to "test" in front
1580 * of the list of arguments described by *n_arg and *args.
1582 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1583 __isl_keep isl_multi_pw_aff *test)
1585 int i;
1586 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1588 if (!test)
1589 return pet_expr_free(expr);
1590 expr = pet_expr_cow(expr);
1591 if (!expr)
1592 return NULL;
1594 if (!expr->args) {
1595 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1596 if (!expr->args)
1597 return pet_expr_free(expr);
1598 } else {
1599 pet_expr **ext;
1600 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1601 if (!ext)
1602 return pet_expr_free(expr);
1603 for (i = 0; i < expr->n_arg; ++i)
1604 ext[1 + i] = expr->args[i];
1605 free(expr->args);
1606 expr->args = ext;
1608 expr->n_arg++;
1609 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1610 if (!expr->args[0])
1611 return pet_expr_free(expr);
1613 return expr;
1616 /* Make the expression "expr" depend on the value of "test"
1617 * being equal to "satisfied".
1619 * If "test" is an affine expression, we simply add the conditions
1620 * on the expression having the value "satisfied" to all access relations
1621 * (introducing access relations if they are missing) and index expressions.
1623 * Otherwise, we add a filter to "expr" (which is then assumed to be
1624 * an access expression) corresponding to "test" being equal to "satisfied".
1626 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
1627 __isl_take isl_multi_pw_aff *test, int satisfied)
1629 isl_id *id;
1630 isl_ctx *ctx;
1631 isl_space *space;
1632 isl_pw_multi_aff *pma;
1634 expr = pet_expr_cow(expr);
1635 if (!expr || !test)
1636 goto error;
1638 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
1639 isl_pw_aff *pa;
1640 isl_set *cond;
1642 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
1643 isl_multi_pw_aff_free(test);
1644 if (satisfied)
1645 cond = isl_pw_aff_non_zero_set(pa);
1646 else
1647 cond = isl_pw_aff_zero_set(pa);
1648 return pet_expr_restrict(expr, cond);
1651 ctx = isl_multi_pw_aff_get_ctx(test);
1652 if (expr->type != pet_expr_access)
1653 isl_die(ctx, isl_error_invalid,
1654 "can only filter access expressions", goto error);
1656 expr = introduce_access_relation(expr);
1657 if (!expr)
1658 goto error;
1660 space = isl_space_domain(isl_multi_pw_aff_get_space(expr->acc.index));
1661 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1662 pma = pet_filter_insert_pma(space, id, satisfied);
1664 expr->acc.access = isl_map_preimage_domain_pw_multi_aff(
1665 expr->acc.access,
1666 isl_pw_multi_aff_copy(pma));
1667 pma = isl_pw_multi_aff_gist(pma,
1668 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma)));
1669 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
1670 expr->acc.index, pma);
1671 if (!expr->acc.access || !expr->acc.index)
1672 goto error;
1674 expr = insert_access_arg(expr, test);
1676 isl_multi_pw_aff_free(test);
1677 return expr;
1678 error:
1679 isl_multi_pw_aff_free(test);
1680 return pet_expr_free(expr);
1683 /* Add a reference identifier to access expression "expr".
1684 * "user" points to an integer that contains the sequence number
1685 * of the next reference.
1687 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
1688 void *user)
1690 isl_ctx *ctx;
1691 char name[50];
1692 int *n_ref = user;
1694 expr = pet_expr_cow(expr);
1695 if (!expr)
1696 return expr;
1697 if (expr->type != pet_expr_access)
1698 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1699 "not an access expression", return pet_expr_free(expr));
1701 ctx = pet_expr_get_ctx(expr);
1702 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
1703 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
1704 if (!expr->acc.ref_id)
1705 return pet_expr_free(expr);
1707 return expr;
1710 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
1712 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
1715 /* Reset the user pointer on all parameter and tuple ids in
1716 * the access relation (if any) and the index expression
1717 * of the access expression "expr".
1719 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
1720 void *user)
1722 expr = pet_expr_cow(expr);
1723 if (!expr)
1724 return expr;
1725 if (expr->type != pet_expr_access)
1726 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1727 "not an access expression", return pet_expr_free(expr));
1729 if (expr->acc.access) {
1730 expr->acc.access = isl_map_reset_user(expr->acc.access);
1731 if (!expr->acc.access)
1732 expr->acc.index =
1733 isl_multi_pw_aff_free(expr->acc.index);
1735 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
1736 if (!expr->acc.index)
1737 return pet_expr_free(expr);
1739 return expr;
1742 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
1744 return pet_expr_map_access(expr, &access_anonymize, NULL);
1747 /* Data used in access_gist() callback.
1749 struct pet_access_gist_data {
1750 isl_set *domain;
1751 isl_union_map *value_bounds;
1754 /* Given an expression "expr" of type pet_expr_access, compute
1755 * the gist of the associated access relation (if any) and index expression
1756 * with respect to data->domain and the bounds on the values of the arguments
1757 * of the expression.
1759 * The arguments of "expr" have been gisted right before "expr" itself
1760 * is gisted. The gisted arguments may have become equal where before
1761 * they may not have been (obviously) equal. We therefore take
1762 * the opportunity to remove duplicate arguments here.
1764 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
1766 struct pet_access_gist_data *data = user;
1767 isl_set *domain;
1769 expr = pet_expr_remove_duplicate_args(expr);
1770 expr = pet_expr_cow(expr);
1771 if (!expr)
1772 return expr;
1773 if (expr->type != pet_expr_access)
1774 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1775 "not an access expression", return pet_expr_free(expr));
1777 domain = isl_set_copy(data->domain);
1778 if (expr->n_arg > 0)
1779 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
1780 data->value_bounds);
1782 if (expr->acc.access) {
1783 expr->acc.access = isl_map_gist_domain(expr->acc.access,
1784 isl_set_copy(domain));
1785 if (!expr->acc.access)
1786 expr->acc.index =
1787 isl_multi_pw_aff_free(expr->acc.index);
1789 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
1790 if (!expr->acc.index)
1791 return pet_expr_free(expr);
1793 return expr;
1796 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
1797 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
1799 struct pet_access_gist_data data = { context, value_bounds };
1801 return pet_expr_map_access(expr, &access_gist, &data);
1804 /* Mark "expr" as a read dependening on "read".
1806 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
1807 int read)
1809 if (!expr)
1810 return pet_expr_free(expr);
1811 if (expr->type != pet_expr_access)
1812 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1813 "not an access expression", return pet_expr_free(expr));
1814 if (expr->acc.read == read)
1815 return expr;
1816 expr = pet_expr_cow(expr);
1817 if (!expr)
1818 return NULL;
1819 expr->acc.read = read;
1821 return expr;
1824 /* Mark "expr" as a write dependening on "write".
1826 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
1827 int write)
1829 if (!expr)
1830 return pet_expr_free(expr);
1831 if (expr->type != pet_expr_access)
1832 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1833 "not an access expression", return pet_expr_free(expr));
1834 if (expr->acc.write == write)
1835 return expr;
1836 expr = pet_expr_cow(expr);
1837 if (!expr)
1838 return NULL;
1839 expr->acc.write = write;
1841 return expr;
1844 /* Replace the access relation of "expr" by "access".
1846 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
1847 __isl_take isl_map *access)
1849 expr = pet_expr_cow(expr);
1850 if (!expr || !access)
1851 goto error;
1852 if (expr->type != pet_expr_access)
1853 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1854 "not an access expression", goto error);
1855 isl_map_free(expr->acc.access);
1856 expr->acc.access = access;
1858 return expr;
1859 error:
1860 isl_map_free(access);
1861 pet_expr_free(expr);
1862 return NULL;
1865 /* Replace the index expression of "expr" by "index" and
1866 * set the array depth accordingly.
1868 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
1869 __isl_take isl_multi_pw_aff *index)
1871 expr = pet_expr_cow(expr);
1872 if (!expr || !index)
1873 goto error;
1874 if (expr->type != pet_expr_access)
1875 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1876 "not an access expression", goto error);
1877 isl_multi_pw_aff_free(expr->acc.index);
1878 expr->acc.index = index;
1879 expr->acc.depth = isl_multi_pw_aff_dim(index, isl_dim_out);
1881 return expr;
1882 error:
1883 isl_multi_pw_aff_free(index);
1884 pet_expr_free(expr);
1885 return NULL;
1888 /* Return the reference identifier of access expression "expr".
1890 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
1892 if (!expr)
1893 return NULL;
1894 if (expr->type != pet_expr_access)
1895 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1896 "not an access expression", return NULL);
1898 return isl_id_copy(expr->acc.ref_id);
1901 /* Replace the reference identifier of access expression "expr" by "ref_id".
1903 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
1904 __isl_take isl_id *ref_id)
1906 expr = pet_expr_cow(expr);
1907 if (!expr || !ref_id)
1908 goto error;
1909 if (expr->type != pet_expr_access)
1910 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1911 "not an access expression", goto error);
1912 isl_id_free(expr->acc.ref_id);
1913 expr->acc.ref_id = ref_id;
1915 return expr;
1916 error:
1917 isl_id_free(ref_id);
1918 pet_expr_free(expr);
1919 return NULL;
1922 /* Tag the access relation "access" with "id".
1923 * That is, insert the id as the range of a wrapped relation
1924 * in the domain of "access".
1926 * If "access" is of the form
1928 * D[i] -> A[a]
1930 * then the result is of the form
1932 * [D[i] -> id[]] -> A[a]
1934 __isl_give isl_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
1935 __isl_take isl_map *access)
1937 isl_space *space;
1938 isl_map *add_tag;
1939 isl_id *id;
1941 if (expr->type != pet_expr_access)
1942 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1943 "not an access expression",
1944 return isl_map_free(access));
1946 id = isl_id_copy(expr->acc.ref_id);
1947 space = isl_space_range(isl_map_get_space(access));
1948 space = isl_space_from_range(space);
1949 space = isl_space_set_tuple_id(space, isl_dim_in, id);
1950 add_tag = isl_map_universe(space);
1951 access = isl_map_domain_product(access, add_tag);
1953 return access;
1956 /* Return the relation mapping pairs of domain iterations and argument
1957 * values to the corresponding accessed data elements.
1959 __isl_give isl_map *pet_expr_access_get_dependent_access(
1960 __isl_keep pet_expr *expr)
1962 isl_map *access;
1964 if (!expr)
1965 return NULL;
1966 if (expr->type != pet_expr_access)
1967 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1968 "not an access expression", return NULL);
1970 if (expr->acc.access)
1971 return isl_map_copy(expr->acc.access);
1973 expr = pet_expr_copy(expr);
1974 expr = introduce_access_relation(expr);
1975 if (!expr)
1976 return NULL;
1977 access = isl_map_copy(expr->acc.access);
1978 pet_expr_free(expr);
1980 return access;
1983 /* Return the relation mapping domain iterations to all possibly
1984 * accessed data elements.
1985 * In particular, take the access relation and project out the values
1986 * of the arguments, if any.
1988 __isl_give isl_map *pet_expr_access_get_may_access(__isl_keep pet_expr *expr)
1990 isl_map *access;
1991 isl_space *space;
1992 isl_map *map;
1994 if (!expr)
1995 return NULL;
1996 if (expr->type != pet_expr_access)
1997 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1998 "not an access expression", return NULL);
2000 access = pet_expr_access_get_dependent_access(expr);
2001 if (expr->n_arg == 0)
2002 return access;
2004 space = isl_space_domain(isl_map_get_space(access));
2005 map = isl_map_universe(isl_space_unwrap(space));
2006 map = isl_map_domain_map(map);
2007 access = isl_map_apply_domain(access, map);
2009 return access;
2012 /* Return a relation mapping domain iterations to definitely
2013 * accessed data elements, assuming the statement containing
2014 * the expression is executed.
2016 * If there are no arguments, then all elements are accessed.
2017 * Otherwise, we conservatively return an empty relation.
2019 __isl_give isl_map *pet_expr_access_get_must_access(__isl_keep pet_expr *expr)
2021 isl_space *space;
2023 if (!expr)
2024 return NULL;
2025 if (expr->type != pet_expr_access)
2026 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2027 "not an access expression", return NULL);
2029 if (expr->n_arg == 0)
2030 return pet_expr_access_get_dependent_access(expr);
2032 space = isl_multi_pw_aff_get_space(expr->acc.index);
2033 space = isl_space_domain_factor_domain(space);
2035 return isl_map_empty(space);
2038 /* Return the relation mapping domain iterations to all possibly
2039 * accessed data elements, with its domain tagged with the reference
2040 * identifier.
2042 __isl_give isl_map *pet_expr_access_get_tagged_may_access(
2043 __isl_keep pet_expr *expr)
2045 isl_map *access;
2047 if (!expr)
2048 return NULL;
2050 access = pet_expr_access_get_may_access(expr);
2051 access = pet_expr_tag_access(expr, access);
2053 return access;
2056 /* Return the operation type of operation expression "expr".
2058 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
2060 if (!expr)
2061 return pet_op_last;
2062 if (expr->type != pet_expr_op)
2063 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2064 "not an operation expression", return pet_op_last);
2066 return expr->op;
2069 /* Replace the operation type of operation expression "expr" by "type".
2071 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
2072 enum pet_op_type type)
2074 if (!expr)
2075 return pet_expr_free(expr);
2076 if (expr->type != pet_expr_op)
2077 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2078 "not an operation expression",
2079 return pet_expr_free(expr));
2080 if (expr->op == type)
2081 return expr;
2082 expr = pet_expr_cow(expr);
2083 if (!expr)
2084 return NULL;
2085 expr->op = type;
2087 return expr;
2090 /* Return the name of the function called by "expr".
2092 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
2094 if (!expr)
2095 return NULL;
2096 if (expr->type != pet_expr_call)
2097 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2098 "not a call expression", return NULL);
2099 return expr->name;
2102 /* Replace the name of the function called by "expr" by "name".
2104 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
2105 __isl_keep const char *name)
2107 expr = pet_expr_cow(expr);
2108 if (!expr || !name)
2109 return pet_expr_free(expr);
2110 if (expr->type != pet_expr_call)
2111 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2112 "not a call expression", return pet_expr_free(expr));
2113 free(expr->name);
2114 expr->name = strdup(name);
2115 if (!expr->name)
2116 return pet_expr_free(expr);
2117 return expr;
2120 /* Replace the type of the cast performed by "expr" by "name".
2122 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
2123 __isl_keep const char *name)
2125 expr = pet_expr_cow(expr);
2126 if (!expr || !name)
2127 return pet_expr_free(expr);
2128 if (expr->type != pet_expr_cast)
2129 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2130 "not a cast expression", return pet_expr_free(expr));
2131 free(expr->type_name);
2132 expr->type_name = strdup(name);
2133 if (!expr->type_name)
2134 return pet_expr_free(expr);
2135 return expr;
2138 /* Return the value of the integer represented by "expr".
2140 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
2142 if (!expr)
2143 return NULL;
2144 if (expr->type != pet_expr_int)
2145 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2146 "not an int expression", return NULL);
2148 return isl_val_copy(expr->i);
2151 /* Replace the value of the integer represented by "expr" by "v".
2153 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
2154 __isl_take isl_val *v)
2156 expr = pet_expr_cow(expr);
2157 if (!expr || !v)
2158 goto error;
2159 if (expr->type != pet_expr_int)
2160 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2161 "not an int expression", goto error);
2162 isl_val_free(expr->i);
2163 expr->i = v;
2165 return expr;
2166 error:
2167 isl_val_free(v);
2168 pet_expr_free(expr);
2169 return NULL;
2172 /* Replace the value and string representation of the double
2173 * represented by "expr" by "d" and "s".
2175 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2176 double d, __isl_keep const char *s)
2178 expr = pet_expr_cow(expr);
2179 if (!expr || !s)
2180 return pet_expr_free(expr);
2181 if (expr->type != pet_expr_double)
2182 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2183 "not a double expression", return pet_expr_free(expr));
2184 expr->d.val = d;
2185 free(expr->d.s);
2186 expr->d.s = strdup(s);
2187 if (!expr->d.s)
2188 return pet_expr_free(expr);
2189 return expr;
2192 /* Return a string representation of the double expression "expr".
2194 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2196 if (!expr)
2197 return NULL;
2198 if (expr->type != pet_expr_double)
2199 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2200 "not a double expression", return NULL);
2201 return strdup(expr->d.s);
2204 /* Return a piecewise affine expression defined on the specified domain
2205 * that represents NaN.
2207 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2209 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2212 /* This function is called when we come across an access that is
2213 * nested in what is supposed to be an affine expression.
2214 * "pc" is the context in which the affine expression is created.
2215 * If nesting is allowed in "pc", we return an affine expression that is
2216 * equal to a new parameter corresponding to this nested access.
2217 * Otherwise, we return NaN.
2219 * Note that we currently don't allow nested accesses themselves
2220 * to contain any nested accesses, so we check if "expr" itself
2221 * involves any nested accesses (either explicitly as arguments
2222 * or implicitly through parameters) and return NaN if it does.
2224 * The new parameter is resolved in resolve_nested.
2226 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2227 __isl_keep pet_context *pc)
2229 isl_ctx *ctx;
2230 isl_id *id;
2231 isl_space *space;
2232 isl_local_space *ls;
2233 isl_aff *aff;
2234 int nested;
2236 if (!expr || !pc)
2237 return NULL;
2238 if (!pet_context_allow_nesting(pc))
2239 return non_affine(pet_context_get_space(pc));
2241 if (pet_expr_get_type(expr) != pet_expr_access)
2242 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2243 "not an access expression", return NULL);
2245 if (expr->n_arg > 0)
2246 return non_affine(pet_context_get_space(pc));
2248 space = pet_expr_access_get_parameter_space(expr);
2249 nested = pet_nested_any_in_space(space);
2250 isl_space_free(space);
2251 if (nested)
2252 return non_affine(pet_context_get_space(pc));
2254 ctx = pet_expr_get_ctx(expr);
2255 id = pet_nested_pet_expr(pet_expr_copy(expr));
2256 space = pet_context_get_space(pc);
2257 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2259 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2260 ls = isl_local_space_from_space(space);
2261 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2263 return isl_pw_aff_from_aff(aff);
2266 /* Extract an affine expression from the access pet_expr "expr".
2267 * "pc" is the context in which the affine expression is created.
2269 * If "expr" is actually an affine expression rather than
2270 * a real access, then we return that expression.
2271 * Otherwise, we require that "expr" is of an integral type.
2272 * If not, we return NaN.
2274 * If the variable has been assigned a known affine expression,
2275 * then we return that expression.
2277 * Otherwise, we return an expression that is equal to a parameter
2278 * representing "expr" (if "allow_nested" is set).
2280 static __isl_give isl_pw_aff *extract_affine_from_access(
2281 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2283 int pos;
2284 isl_id *id;
2286 if (pet_expr_is_affine(expr)) {
2287 isl_pw_aff *pa;
2288 isl_multi_pw_aff *mpa;
2290 mpa = pet_expr_access_get_index(expr);
2291 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
2292 isl_multi_pw_aff_free(mpa);
2293 return pa;
2296 if (pet_expr_get_type_size(expr) == 0)
2297 return non_affine(pet_context_get_space(pc));
2299 if (!pet_expr_is_scalar_access(expr))
2300 return nested_access(expr, pc);
2302 id = pet_expr_access_get_id(expr);
2303 if (pet_context_is_assigned(pc, id))
2304 return pet_context_get_value(pc, id);
2306 isl_id_free(id);
2307 return nested_access(expr, pc);
2310 /* Construct an affine expression from the integer constant "expr".
2311 * "pc" is the context in which the affine expression is created.
2313 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2314 __isl_keep pet_context *pc)
2316 isl_local_space *ls;
2317 isl_aff *aff;
2319 if (!expr)
2320 return NULL;
2322 ls = isl_local_space_from_space(pet_context_get_space(pc));
2323 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2325 return isl_pw_aff_from_aff(aff);
2328 /* Extract an affine expression from an addition or subtraction operation.
2329 * Return NaN if we are unable to extract an affine expression.
2331 * "pc" is the context in which the affine expression is created.
2333 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2334 __isl_keep pet_context *pc)
2336 isl_pw_aff *lhs;
2337 isl_pw_aff *rhs;
2339 if (!expr)
2340 return NULL;
2341 if (expr->n_arg != 2)
2342 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2343 "expecting two arguments", return NULL);
2345 lhs = pet_expr_extract_affine(expr->args[0], pc);
2346 rhs = pet_expr_extract_affine(expr->args[1], pc);
2348 switch (pet_expr_op_get_type(expr)) {
2349 case pet_op_add:
2350 return isl_pw_aff_add(lhs, rhs);
2351 case pet_op_sub:
2352 return isl_pw_aff_sub(lhs, rhs);
2353 default:
2354 isl_pw_aff_free(lhs);
2355 isl_pw_aff_free(rhs);
2356 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2357 "not an addition or subtraction operation",
2358 return NULL);
2363 /* Extract an affine expression from an integer division or a modulo operation.
2364 * Return NaN if we are unable to extract an affine expression.
2366 * "pc" is the context in which the affine expression is created.
2368 * In particular, if "expr" is lhs/rhs, then return
2370 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2372 * If "expr" is lhs%rhs, then return
2374 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2376 * If the second argument (rhs) is not a (positive) integer constant,
2377 * then we fail to extract an affine expression.
2379 * We simplify the result in the context of the domain of "pc" in case
2380 * this domain implies that lhs >= 0 (or < 0).
2382 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2383 __isl_keep pet_context *pc)
2385 int is_cst;
2386 isl_pw_aff *lhs;
2387 isl_pw_aff *rhs;
2388 isl_pw_aff *res;
2390 if (!expr)
2391 return NULL;
2392 if (expr->n_arg != 2)
2393 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2394 "expecting two arguments", return NULL);
2396 rhs = pet_expr_extract_affine(expr->args[1], pc);
2398 is_cst = isl_pw_aff_is_cst(rhs);
2399 if (is_cst < 0 || !is_cst) {
2400 isl_pw_aff_free(rhs);
2401 return non_affine(pet_context_get_space(pc));
2404 lhs = pet_expr_extract_affine(expr->args[0], pc);
2406 switch (pet_expr_op_get_type(expr)) {
2407 case pet_op_div:
2408 res = isl_pw_aff_tdiv_q(lhs, rhs);
2409 break;
2410 case pet_op_mod:
2411 res = isl_pw_aff_tdiv_r(lhs, rhs);
2412 break;
2413 default:
2414 isl_pw_aff_free(lhs);
2415 isl_pw_aff_free(rhs);
2416 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2417 "not a div or mod operator", return NULL);
2420 return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc));
2423 /* Extract an affine expression from a multiplication operation.
2424 * Return NaN if we are unable to extract an affine expression.
2425 * In particular, if neither of the arguments is a (piecewise) constant
2426 * then we return NaN.
2428 * "pc" is the context in which the affine expression is created.
2430 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2431 __isl_keep pet_context *pc)
2433 int lhs_cst, rhs_cst;
2434 isl_pw_aff *lhs;
2435 isl_pw_aff *rhs;
2437 if (!expr)
2438 return NULL;
2439 if (expr->n_arg != 2)
2440 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2441 "expecting two arguments", return NULL);
2443 lhs = pet_expr_extract_affine(expr->args[0], pc);
2444 rhs = pet_expr_extract_affine(expr->args[1], pc);
2446 lhs_cst = isl_pw_aff_is_cst(lhs);
2447 rhs_cst = isl_pw_aff_is_cst(rhs);
2448 if (lhs_cst < 0 || rhs_cst < 0 || (!lhs_cst && !rhs_cst)) {
2449 isl_pw_aff_free(lhs);
2450 isl_pw_aff_free(rhs);
2451 return non_affine(pet_context_get_space(pc));
2454 return isl_pw_aff_mul(lhs, rhs);
2457 /* Extract an affine expression from a negation operation.
2458 * Return NaN if we are unable to extract an affine expression.
2460 * "pc" is the context in which the affine expression is created.
2462 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
2463 __isl_keep pet_context *pc)
2465 isl_pw_aff *res;
2467 if (!expr)
2468 return NULL;
2469 if (expr->n_arg != 1)
2470 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2471 "expecting one argument", return NULL);
2473 res = pet_expr_extract_affine(expr->args[0], pc);
2474 return isl_pw_aff_neg(res);
2477 /* Extract an affine expression from a conditional operation.
2478 * Return NaN if we are unable to extract an affine expression.
2480 * "pc" is the context in which the affine expression is created.
2482 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
2483 __isl_keep pet_context *pc)
2485 isl_pw_aff *cond, *lhs, *rhs;
2487 if (!expr)
2488 return NULL;
2489 if (expr->n_arg != 3)
2490 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2491 "expecting three arguments", return NULL);
2493 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2494 lhs = pet_expr_extract_affine(expr->args[1], pc);
2495 rhs = pet_expr_extract_affine(expr->args[2], pc);
2497 return isl_pw_aff_cond(cond, lhs, rhs);
2500 /* Compute
2502 * pwaff mod 2^width
2504 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff, unsigned width)
2506 isl_ctx *ctx;
2507 isl_val *mod;
2509 ctx = isl_pw_aff_get_ctx(pwaff);
2510 mod = isl_val_int_from_ui(ctx, width);
2511 mod = isl_val_2exp(mod);
2513 pwaff = isl_pw_aff_mod_val(pwaff, mod);
2515 return pwaff;
2518 /* Limit the domain of "pwaff" to those elements where the function
2519 * value satisfies
2521 * 2^{width-1} <= pwaff < 2^{width-1}
2523 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
2524 unsigned width)
2526 isl_ctx *ctx;
2527 isl_val *v;
2528 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
2529 isl_local_space *ls = isl_local_space_from_space(space);
2530 isl_aff *bound;
2531 isl_set *dom;
2532 isl_pw_aff *b;
2534 ctx = isl_pw_aff_get_ctx(pwaff);
2535 v = isl_val_int_from_ui(ctx, width - 1);
2536 v = isl_val_2exp(v);
2538 bound = isl_aff_zero_on_domain(ls);
2539 bound = isl_aff_add_constant_val(bound, v);
2540 b = isl_pw_aff_from_aff(bound);
2542 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
2543 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2545 b = isl_pw_aff_neg(b);
2546 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
2547 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2549 return pwaff;
2552 /* Handle potential overflows on signed computations.
2554 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2555 * then we adjust the domain of "pa" to avoid overflows.
2557 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
2558 unsigned width)
2560 isl_ctx *ctx;
2561 struct pet_options *options;
2563 if (!pa)
2564 return NULL;
2566 ctx = isl_pw_aff_get_ctx(pa);
2567 options = isl_ctx_peek_pet_options(ctx);
2568 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
2569 pa = avoid_overflow(pa, width);
2571 return pa;
2574 /* Extract an affine expression from some an operation.
2575 * Return NaN if we are unable to extract an affine expression.
2576 * If the result of a binary (non boolean) operation is unsigned,
2577 * then we wrap it based on the size of the type. If the result is signed,
2578 * then we ensure that no overflow occurs.
2580 * "pc" is the context in which the affine expression is created.
2582 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
2583 __isl_keep pet_context *pc)
2585 isl_pw_aff *res;
2586 int type_size;
2588 switch (pet_expr_op_get_type(expr)) {
2589 case pet_op_add:
2590 case pet_op_sub:
2591 res = extract_affine_add_sub(expr, pc);
2592 break;
2593 case pet_op_div:
2594 case pet_op_mod:
2595 res = extract_affine_div_mod(expr, pc);
2596 break;
2597 case pet_op_mul:
2598 res = extract_affine_mul(expr, pc);
2599 break;
2600 case pet_op_minus:
2601 return extract_affine_neg(expr, pc);
2602 case pet_op_cond:
2603 return extract_affine_cond(expr, pc);
2604 case pet_op_eq:
2605 case pet_op_ne:
2606 case pet_op_le:
2607 case pet_op_ge:
2608 case pet_op_lt:
2609 case pet_op_gt:
2610 case pet_op_land:
2611 case pet_op_lor:
2612 case pet_op_lnot:
2613 return pet_expr_extract_affine_condition(expr, pc);
2614 default:
2615 return non_affine(pet_context_get_space(pc));
2618 if (!res)
2619 return NULL;
2620 if (isl_pw_aff_involves_nan(res)) {
2621 isl_space *space = isl_pw_aff_get_domain_space(res);
2622 isl_pw_aff_free(res);
2623 return non_affine(space);
2626 type_size = pet_expr_get_type_size(expr);
2627 if (type_size > 0)
2628 res = wrap(res, type_size);
2629 else
2630 res = signed_overflow(res, -type_size);
2632 return res;
2635 /* Extract an affine expression from some special function calls.
2636 * Return NaN if we are unable to extract an affine expression.
2637 * In particular, we handle "min", "max", "ceild", "floord",
2638 * "intMod", "intFloor" and "intCeil".
2639 * In case of the latter five, the second argument needs to be
2640 * a (positive) integer constant.
2642 * "pc" is the context in which the affine expression is created.
2644 static __isl_give isl_pw_aff *extract_affine_from_call(
2645 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2647 isl_pw_aff *aff1, *aff2;
2648 int n;
2649 const char *name;
2651 n = pet_expr_get_n_arg(expr);
2652 name = pet_expr_call_get_name(expr);
2653 if (!(n == 2 && !strcmp(name, "min")) &&
2654 !(n == 2 && !strcmp(name, "max")) &&
2655 !(n == 2 && !strcmp(name, "intMod")) &&
2656 !(n == 2 && !strcmp(name, "intFloor")) &&
2657 !(n == 2 && !strcmp(name, "intCeil")) &&
2658 !(n == 2 && !strcmp(name, "floord")) &&
2659 !(n == 2 && !strcmp(name, "ceild")))
2660 return non_affine(pet_context_get_space(pc));
2662 if (!strcmp(name, "min") || !strcmp(name, "max")) {
2663 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2664 aff2 = pet_expr_extract_affine(expr->args[1], pc);
2666 if (!strcmp(name, "min"))
2667 aff1 = isl_pw_aff_min(aff1, aff2);
2668 else
2669 aff1 = isl_pw_aff_max(aff1, aff2);
2670 } else if (!strcmp(name, "intMod")) {
2671 isl_val *v;
2673 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2674 return non_affine(pet_context_get_space(pc));
2675 v = pet_expr_int_get_val(expr->args[1]);
2676 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2677 aff1 = isl_pw_aff_mod_val(aff1, v);
2678 } else {
2679 isl_val *v;
2681 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2682 return non_affine(pet_context_get_space(pc));
2683 v = pet_expr_int_get_val(expr->args[1]);
2684 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2685 aff1 = isl_pw_aff_scale_down_val(aff1, v);
2686 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
2687 aff1 = isl_pw_aff_floor(aff1);
2688 else
2689 aff1 = isl_pw_aff_ceil(aff1);
2692 return aff1;
2695 /* Extract an affine expression from "expr", if possible.
2696 * Otherwise return NaN.
2698 * "pc" is the context in which the affine expression is created.
2700 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
2701 __isl_keep pet_context *pc)
2703 if (!expr)
2704 return NULL;
2706 switch (pet_expr_get_type(expr)) {
2707 case pet_expr_access:
2708 return extract_affine_from_access(expr, pc);
2709 case pet_expr_int:
2710 return extract_affine_from_int(expr, pc);
2711 case pet_expr_op:
2712 return extract_affine_from_op(expr, pc);
2713 case pet_expr_call:
2714 return extract_affine_from_call(expr, pc);
2715 case pet_expr_cast:
2716 case pet_expr_double:
2717 case pet_expr_error:
2718 return non_affine(pet_context_get_space(pc));
2722 /* Extract an affine expressions representing the comparison "LHS op RHS"
2723 * Return NaN if we are unable to extract such an affine expression.
2725 * "pc" is the context in which the affine expression is created.
2727 * If the comparison is of the form
2729 * a <= min(b,c)
2731 * then the expression is constructed as the conjunction of
2732 * the comparisons
2734 * a <= b and a <= c
2736 * A similar optimization is performed for max(a,b) <= c.
2737 * We do this because that will lead to simpler representations
2738 * of the expression.
2739 * If isl is ever enhanced to explicitly deal with min and max expressions,
2740 * this optimization can be removed.
2742 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
2743 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
2744 __isl_keep pet_context *pc)
2746 isl_pw_aff *lhs_pa, *rhs_pa;
2748 if (op == pet_op_gt)
2749 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
2750 if (op == pet_op_ge)
2751 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
2753 if (op == pet_op_lt || op == pet_op_le) {
2754 if (pet_expr_is_min(rhs)) {
2755 lhs_pa = pet_expr_extract_comparison(op, lhs,
2756 rhs->args[0], pc);
2757 rhs_pa = pet_expr_extract_comparison(op, lhs,
2758 rhs->args[1], pc);
2759 return pet_and(lhs_pa, rhs_pa);
2761 if (pet_expr_is_max(lhs)) {
2762 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
2763 rhs, pc);
2764 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
2765 rhs, pc);
2766 return pet_and(lhs_pa, rhs_pa);
2770 lhs_pa = pet_expr_extract_affine(lhs, pc);
2771 rhs_pa = pet_expr_extract_affine(rhs, pc);
2773 return pet_comparison(op, lhs_pa, rhs_pa);
2776 /* Extract an affine expressions from the comparison "expr".
2777 * Return NaN if we are unable to extract such an affine expression.
2779 * "pc" is the context in which the affine expression is created.
2781 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
2782 __isl_keep pet_context *pc)
2784 enum pet_op_type type;
2786 if (!expr)
2787 return NULL;
2788 if (expr->n_arg != 2)
2789 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2790 "expecting two arguments", return NULL);
2792 type = pet_expr_op_get_type(expr);
2793 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
2794 pc);
2797 /* Extract an affine expression representing the boolean operation
2798 * expressed by "expr".
2799 * Return NaN if we are unable to extract an affine expression.
2801 * "pc" is the context in which the affine expression is created.
2803 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
2804 __isl_keep pet_context *pc)
2806 isl_pw_aff *lhs, *rhs;
2807 int n;
2809 if (!expr)
2810 return NULL;
2812 n = pet_expr_get_n_arg(expr);
2813 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
2814 if (n == 1)
2815 return pet_not(lhs);
2817 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
2818 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
2821 /* Extract the affine expression "expr != 0 ? 1 : 0".
2822 * Return NaN if we are unable to extract an affine expression.
2824 * "pc" is the context in which the affine expression is created.
2826 static __isl_give isl_pw_aff *extract_implicit_condition(
2827 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2829 isl_pw_aff *res;
2831 res = pet_expr_extract_affine(expr, pc);
2832 return pet_to_bool(res);
2835 /* Extract a boolean affine expression from "expr".
2836 * Return NaN if we are unable to extract an affine expression.
2838 * "pc" is the context in which the affine expression is created.
2840 * If "expr" is neither a comparison nor a boolean operation,
2841 * then we assume it is an affine expression and return the
2842 * boolean expression "expr != 0 ? 1 : 0".
2844 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
2845 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2847 if (!expr)
2848 return NULL;
2850 if (pet_expr_is_comparison(expr))
2851 return extract_comparison(expr, pc);
2852 if (pet_expr_is_boolean(expr))
2853 return extract_boolean(expr, pc);
2855 return extract_implicit_condition(expr, pc);
2858 /* Check if "expr" is an assume expression and if its single argument
2859 * can be converted to an affine expression in the context of "pc".
2860 * If so, replace the argument by the affine expression.
2862 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
2863 __isl_keep pet_context *pc)
2865 isl_pw_aff *cond;
2866 isl_multi_pw_aff *index;
2868 if (!expr)
2869 return NULL;
2870 if (!pet_expr_is_assume(expr))
2871 return expr;
2872 if (expr->n_arg != 1)
2873 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2874 "expecting one argument", return pet_expr_free(expr));
2876 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2877 if (!cond)
2878 return pet_expr_free(expr);
2879 if (isl_pw_aff_involves_nan(cond)) {
2880 isl_pw_aff_free(cond);
2881 return expr;
2884 index = isl_multi_pw_aff_from_pw_aff(cond);
2885 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
2887 return expr;
2890 /* Return the number of bits needed to represent the type of "expr".
2891 * See the description of the type_size field of pet_expr.
2893 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
2895 return expr ? expr->type_size : 0;
2898 /* Replace the number of bits needed to represent the type of "expr"
2899 * by "type_size".
2900 * See the description of the type_size field of pet_expr.
2902 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
2903 int type_size)
2905 expr = pet_expr_cow(expr);
2906 if (!expr)
2907 return NULL;
2909 expr->type_size = type_size;
2911 return expr;
2914 /* Extend an access expression "expr" with an additional index "index".
2915 * In particular, add "index" as an extra argument to "expr" and
2916 * adjust the index expression of "expr" to refer to this extra argument.
2917 * The caller is responsible for calling pet_expr_access_set_depth
2918 * to update the corresponding access relation.
2920 * Note that we only collect the individual index expressions as
2921 * arguments of "expr" here.
2922 * An attempt to integrate them into the index expression of "expr"
2923 * is performed in pet_expr_access_plug_in_args.
2925 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
2926 __isl_take pet_expr *index)
2928 int n;
2929 isl_space *space;
2930 isl_local_space *ls;
2931 isl_pw_aff *pa;
2933 expr = pet_expr_cow(expr);
2934 if (!expr || !index)
2935 goto error;
2936 if (expr->type != pet_expr_access)
2937 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2938 "not an access pet_expr", goto error);
2940 n = pet_expr_get_n_arg(expr);
2941 expr = pet_expr_insert_arg(expr, n, index);
2942 if (!expr)
2943 return NULL;
2945 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
2946 ls = isl_local_space_from_space(space);
2947 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
2948 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
2949 if (!expr->acc.index)
2950 return pet_expr_free(expr);
2952 return expr;
2953 error:
2954 pet_expr_free(expr);
2955 pet_expr_free(index);
2956 return NULL;
2959 /* Extend an access expression "expr" with an additional member acces to "id".
2960 * In particular, extend the index expression of "expr" to include
2961 * the additional member access.
2962 * The caller is responsible for calling pet_expr_access_set_depth
2963 * to update the corresponding access relation.
2965 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
2966 __isl_take isl_id *id)
2968 isl_space *space;
2969 isl_multi_pw_aff *field_access;
2971 expr = pet_expr_cow(expr);
2972 if (!expr || !id)
2973 goto error;
2974 if (expr->type != pet_expr_access)
2975 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2976 "not an access pet_expr", goto error);
2978 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
2979 space = isl_space_from_domain(space);
2980 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2981 field_access = isl_multi_pw_aff_zero(space);
2982 expr->acc.index = pet_array_member(expr->acc.index, field_access);
2983 if (!expr->acc.index)
2984 return pet_expr_free(expr);
2986 return expr;
2987 error:
2988 pet_expr_free(expr);
2989 isl_id_free(id);
2990 return NULL;
2993 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
2995 int i;
2997 if (!expr)
2998 return;
3000 fprintf(stderr, "%*s", indent, "");
3002 switch (expr->type) {
3003 case pet_expr_double:
3004 fprintf(stderr, "%s\n", expr->d.s);
3005 break;
3006 case pet_expr_int:
3007 isl_val_dump(expr->i);
3008 break;
3009 case pet_expr_access:
3010 if (expr->acc.ref_id) {
3011 isl_id_dump(expr->acc.ref_id);
3012 fprintf(stderr, "%*s", indent, "");
3014 isl_multi_pw_aff_dump(expr->acc.index);
3015 fprintf(stderr, "%*sdepth: %d\n", indent + 2,
3016 "", expr->acc.depth);
3017 fprintf(stderr, "%*sread: %d\n", indent + 2,
3018 "", expr->acc.read);
3019 fprintf(stderr, "%*swrite: %d\n", indent + 2,
3020 "", expr->acc.write);
3021 if (expr->acc.access) {
3022 fprintf(stderr, "%*saccess: ", indent + 2, "");
3023 isl_map_dump(expr->acc.access);
3025 for (i = 0; i < expr->n_arg; ++i)
3026 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3027 break;
3028 case pet_expr_op:
3029 fprintf(stderr, "%s\n", op_str[expr->op]);
3030 for (i = 0; i < expr->n_arg; ++i)
3031 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3032 break;
3033 case pet_expr_call:
3034 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
3035 for (i = 0; i < expr->n_arg; ++i)
3036 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3037 break;
3038 case pet_expr_cast:
3039 fprintf(stderr, "(%s)\n", expr->type_name);
3040 for (i = 0; i < expr->n_arg; ++i)
3041 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3042 break;
3043 case pet_expr_error:
3044 fprintf(stderr, "ERROR\n");
3045 break;
3049 void pet_expr_dump(__isl_keep pet_expr *expr)
3051 pet_expr_dump_with_indent(expr, 0);