break up access relations into may_read/may_write/must_write in interface
[pet.git] / expr.c
blobb7b04b0659aac2151199a102a0b469cb429fcdf8
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_union_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
1935 __isl_take isl_union_map *access)
1937 isl_space *space;
1938 isl_multi_aff *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_union_map_free(access));
1946 id = isl_id_copy(expr->acc.ref_id);
1947 space = pet_expr_access_get_domain_space(expr);
1948 space = isl_space_from_domain(space);
1949 space = isl_space_set_tuple_id(space, isl_dim_out, id);
1950 add_tag = isl_multi_aff_domain_map(space);
1951 access = isl_union_map_preimage_domain_multi_aff(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 static __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 an empty access relation for access expression "expr".
1985 static __isl_give isl_union_map *empty_access_relation(
1986 __isl_keep pet_expr *expr)
1988 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr));
1991 /* Return the may read access relation associated to "expr"
1992 * that maps pairs of domain iterations and argument values
1993 * to the corresponding accessed data elements.
1995 * Since the accesses are currently represented by a single access relation,
1996 * we return the entire access relation if "expr" is a read and
1997 * an empty relation if it is not.
1999 __isl_give isl_union_map *pet_expr_access_get_dependent_may_read(
2000 __isl_keep pet_expr *expr)
2002 isl_map *access;
2004 if (!expr)
2005 return NULL;
2006 if (!pet_expr_access_is_read(expr))
2007 return empty_access_relation(expr);
2008 access = pet_expr_access_get_dependent_access(expr);
2009 return isl_union_map_from_map(access);
2012 /* Return the may write access relation associated to "expr"
2013 * that maps pairs of domain iterations and argument values
2014 * to the corresponding accessed data elements.
2016 * Since the accesses are currently represented by a single access relation,
2017 * we return the entire access relation if "expr" is a write and
2018 * an empty relation if it is not.
2020 __isl_give isl_union_map *pet_expr_access_get_dependent_may_write(
2021 __isl_keep pet_expr *expr)
2023 isl_map *access;
2025 if (!expr)
2026 return NULL;
2027 if (!pet_expr_access_is_write(expr))
2028 return empty_access_relation(expr);
2029 access = pet_expr_access_get_dependent_access(expr);
2030 return isl_union_map_from_map(access);
2033 /* Return the must write access relation associated to "expr"
2034 * that maps pairs of domain iterations and argument values
2035 * to the corresponding accessed data elements.
2037 * Since the accesses are currently represented by a single access relation,
2038 * we return the entire access relation when "expr" is a write.
2040 __isl_give isl_union_map *pet_expr_access_get_dependent_must_write(
2041 __isl_keep pet_expr *expr)
2043 isl_map *access;
2045 if (!expr)
2046 return NULL;
2047 if (!pet_expr_access_is_write(expr))
2048 return empty_access_relation(expr);
2049 access = pet_expr_access_get_dependent_access(expr);
2050 return isl_union_map_from_map(access);
2053 /* Return the relation mapping domain iterations to all possibly
2054 * accessed data elements.
2055 * In particular, take the access relation and project out the values
2056 * of the arguments, if any.
2058 __isl_give isl_map *pet_expr_access_get_may_access(__isl_keep pet_expr *expr)
2060 isl_map *access;
2061 isl_space *space;
2062 isl_map *map;
2064 if (!expr)
2065 return NULL;
2066 if (expr->type != pet_expr_access)
2067 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2068 "not an access expression", return NULL);
2070 access = pet_expr_access_get_dependent_access(expr);
2071 if (expr->n_arg == 0)
2072 return access;
2074 space = isl_space_domain(isl_map_get_space(access));
2075 map = isl_map_universe(isl_space_unwrap(space));
2076 map = isl_map_domain_map(map);
2077 access = isl_map_apply_domain(access, map);
2079 return access;
2082 /* Return the relation mapping domain iterations to all possibly
2083 * read data elements.
2085 * Since the accesses are currently represented by a single access relation,
2086 * we return the may access relation if "expr" is a read and
2087 * an empty relation if it is not.
2089 __isl_give isl_union_map *pet_expr_access_get_may_read(
2090 __isl_keep pet_expr *expr)
2092 if (!expr)
2093 return NULL;
2094 if (!pet_expr_access_is_read(expr))
2095 return empty_access_relation(expr);
2096 return isl_union_map_from_map(pet_expr_access_get_may_access(expr));
2099 /* Return the relation mapping domain iterations to all possibly
2100 * written data elements.
2102 * Since the accesses are currently represented by a single access relation,
2103 * we return the may access relation if "expr" is a write and
2104 * an empty relation if it is not.
2106 __isl_give isl_union_map *pet_expr_access_get_may_write(
2107 __isl_keep pet_expr *expr)
2109 if (!expr)
2110 return NULL;
2111 if (!pet_expr_access_is_write(expr))
2112 return empty_access_relation(expr);
2113 return isl_union_map_from_map(pet_expr_access_get_may_access(expr));
2116 /* Return a relation mapping domain iterations to definitely
2117 * accessed data elements, assuming the statement containing
2118 * the expression is executed.
2120 * If there are no arguments, then all elements are accessed.
2121 * Otherwise, we conservatively return an empty relation.
2123 static __isl_give isl_map *pet_expr_access_get_must_access(
2124 __isl_keep pet_expr *expr)
2126 isl_space *space;
2128 if (!expr)
2129 return NULL;
2130 if (expr->type != pet_expr_access)
2131 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2132 "not an access expression", return NULL);
2134 if (expr->n_arg == 0)
2135 return pet_expr_access_get_dependent_access(expr);
2137 space = isl_multi_pw_aff_get_space(expr->acc.index);
2138 space = isl_space_domain_factor_domain(space);
2140 return isl_map_empty(space);
2143 /* Return a relation mapping domain iterations to definitely
2144 * written data elements, assuming the statement containing
2145 * the expression is executed.
2147 * Since the accesses are currently represented by a single access relation,
2148 * we return the must access relation if "expr" is a write and
2149 * an empty relation if it is not.
2151 __isl_give isl_union_map *pet_expr_access_get_must_write(
2152 __isl_keep pet_expr *expr)
2154 if (!expr)
2155 return NULL;
2156 if (!pet_expr_access_is_write(expr))
2157 return empty_access_relation(expr);
2158 return isl_union_map_from_map(pet_expr_access_get_must_access(expr));
2161 /* Return the relation mapping domain iterations to all possibly
2162 * read data elements, with its domain tagged with the reference
2163 * identifier.
2165 __isl_give isl_union_map *pet_expr_access_get_tagged_may_read(
2166 __isl_keep pet_expr *expr)
2168 isl_union_map *access;
2170 if (!expr)
2171 return NULL;
2173 access = pet_expr_access_get_may_read(expr);
2174 access = pet_expr_tag_access(expr, access);
2176 return access;
2179 /* Return the relation mapping domain iterations to all possibly
2180 * written data elements, with its domain tagged with the reference
2181 * identifier.
2183 __isl_give isl_union_map *pet_expr_access_get_tagged_may_write(
2184 __isl_keep pet_expr *expr)
2186 isl_union_map *access;
2188 if (!expr)
2189 return NULL;
2191 access = pet_expr_access_get_may_write(expr);
2192 access = pet_expr_tag_access(expr, access);
2194 return access;
2197 /* Return the operation type of operation expression "expr".
2199 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
2201 if (!expr)
2202 return pet_op_last;
2203 if (expr->type != pet_expr_op)
2204 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2205 "not an operation expression", return pet_op_last);
2207 return expr->op;
2210 /* Replace the operation type of operation expression "expr" by "type".
2212 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
2213 enum pet_op_type type)
2215 if (!expr)
2216 return pet_expr_free(expr);
2217 if (expr->type != pet_expr_op)
2218 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2219 "not an operation expression",
2220 return pet_expr_free(expr));
2221 if (expr->op == type)
2222 return expr;
2223 expr = pet_expr_cow(expr);
2224 if (!expr)
2225 return NULL;
2226 expr->op = type;
2228 return expr;
2231 /* Return the name of the function called by "expr".
2233 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
2235 if (!expr)
2236 return NULL;
2237 if (expr->type != pet_expr_call)
2238 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2239 "not a call expression", return NULL);
2240 return expr->name;
2243 /* Replace the name of the function called by "expr" by "name".
2245 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
2246 __isl_keep const char *name)
2248 expr = pet_expr_cow(expr);
2249 if (!expr || !name)
2250 return pet_expr_free(expr);
2251 if (expr->type != pet_expr_call)
2252 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2253 "not a call expression", return pet_expr_free(expr));
2254 free(expr->name);
2255 expr->name = strdup(name);
2256 if (!expr->name)
2257 return pet_expr_free(expr);
2258 return expr;
2261 /* Replace the type of the cast performed by "expr" by "name".
2263 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
2264 __isl_keep const char *name)
2266 expr = pet_expr_cow(expr);
2267 if (!expr || !name)
2268 return pet_expr_free(expr);
2269 if (expr->type != pet_expr_cast)
2270 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2271 "not a cast expression", return pet_expr_free(expr));
2272 free(expr->type_name);
2273 expr->type_name = strdup(name);
2274 if (!expr->type_name)
2275 return pet_expr_free(expr);
2276 return expr;
2279 /* Return the value of the integer represented by "expr".
2281 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
2283 if (!expr)
2284 return NULL;
2285 if (expr->type != pet_expr_int)
2286 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2287 "not an int expression", return NULL);
2289 return isl_val_copy(expr->i);
2292 /* Replace the value of the integer represented by "expr" by "v".
2294 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
2295 __isl_take isl_val *v)
2297 expr = pet_expr_cow(expr);
2298 if (!expr || !v)
2299 goto error;
2300 if (expr->type != pet_expr_int)
2301 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2302 "not an int expression", goto error);
2303 isl_val_free(expr->i);
2304 expr->i = v;
2306 return expr;
2307 error:
2308 isl_val_free(v);
2309 pet_expr_free(expr);
2310 return NULL;
2313 /* Replace the value and string representation of the double
2314 * represented by "expr" by "d" and "s".
2316 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2317 double d, __isl_keep const char *s)
2319 expr = pet_expr_cow(expr);
2320 if (!expr || !s)
2321 return pet_expr_free(expr);
2322 if (expr->type != pet_expr_double)
2323 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2324 "not a double expression", return pet_expr_free(expr));
2325 expr->d.val = d;
2326 free(expr->d.s);
2327 expr->d.s = strdup(s);
2328 if (!expr->d.s)
2329 return pet_expr_free(expr);
2330 return expr;
2333 /* Return a string representation of the double expression "expr".
2335 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2337 if (!expr)
2338 return NULL;
2339 if (expr->type != pet_expr_double)
2340 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2341 "not a double expression", return NULL);
2342 return strdup(expr->d.s);
2345 /* Return a piecewise affine expression defined on the specified domain
2346 * that represents NaN.
2348 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2350 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2353 /* This function is called when we come across an access that is
2354 * nested in what is supposed to be an affine expression.
2355 * "pc" is the context in which the affine expression is created.
2356 * If nesting is allowed in "pc", we return an affine expression that is
2357 * equal to a new parameter corresponding to this nested access.
2358 * Otherwise, we return NaN.
2360 * Note that we currently don't allow nested accesses themselves
2361 * to contain any nested accesses, so we check if "expr" itself
2362 * involves any nested accesses (either explicitly as arguments
2363 * or implicitly through parameters) and return NaN if it does.
2365 * The new parameter is resolved in resolve_nested.
2367 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2368 __isl_keep pet_context *pc)
2370 isl_ctx *ctx;
2371 isl_id *id;
2372 isl_space *space;
2373 isl_local_space *ls;
2374 isl_aff *aff;
2375 int nested;
2377 if (!expr || !pc)
2378 return NULL;
2379 if (!pet_context_allow_nesting(pc))
2380 return non_affine(pet_context_get_space(pc));
2382 if (pet_expr_get_type(expr) != pet_expr_access)
2383 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2384 "not an access expression", return NULL);
2386 if (expr->n_arg > 0)
2387 return non_affine(pet_context_get_space(pc));
2389 space = pet_expr_access_get_parameter_space(expr);
2390 nested = pet_nested_any_in_space(space);
2391 isl_space_free(space);
2392 if (nested)
2393 return non_affine(pet_context_get_space(pc));
2395 ctx = pet_expr_get_ctx(expr);
2396 id = pet_nested_pet_expr(pet_expr_copy(expr));
2397 space = pet_context_get_space(pc);
2398 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2400 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2401 ls = isl_local_space_from_space(space);
2402 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2404 return isl_pw_aff_from_aff(aff);
2407 /* Extract an affine expression from the access pet_expr "expr".
2408 * "pc" is the context in which the affine expression is created.
2410 * If "expr" is actually an affine expression rather than
2411 * a real access, then we return that expression.
2412 * Otherwise, we require that "expr" is of an integral type.
2413 * If not, we return NaN.
2415 * If the variable has been assigned a known affine expression,
2416 * then we return that expression.
2418 * Otherwise, we return an expression that is equal to a parameter
2419 * representing "expr" (if "allow_nested" is set).
2421 static __isl_give isl_pw_aff *extract_affine_from_access(
2422 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2424 int pos;
2425 isl_id *id;
2427 if (pet_expr_is_affine(expr)) {
2428 isl_pw_aff *pa;
2429 isl_multi_pw_aff *mpa;
2431 mpa = pet_expr_access_get_index(expr);
2432 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
2433 isl_multi_pw_aff_free(mpa);
2434 return pa;
2437 if (pet_expr_get_type_size(expr) == 0)
2438 return non_affine(pet_context_get_space(pc));
2440 if (!pet_expr_is_scalar_access(expr))
2441 return nested_access(expr, pc);
2443 id = pet_expr_access_get_id(expr);
2444 if (pet_context_is_assigned(pc, id))
2445 return pet_context_get_value(pc, id);
2447 isl_id_free(id);
2448 return nested_access(expr, pc);
2451 /* Construct an affine expression from the integer constant "expr".
2452 * "pc" is the context in which the affine expression is created.
2454 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2455 __isl_keep pet_context *pc)
2457 isl_local_space *ls;
2458 isl_aff *aff;
2460 if (!expr)
2461 return NULL;
2463 ls = isl_local_space_from_space(pet_context_get_space(pc));
2464 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2466 return isl_pw_aff_from_aff(aff);
2469 /* Extract an affine expression from an addition or subtraction operation.
2470 * Return NaN if we are unable to extract an affine expression.
2472 * "pc" is the context in which the affine expression is created.
2474 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2475 __isl_keep pet_context *pc)
2477 isl_pw_aff *lhs;
2478 isl_pw_aff *rhs;
2480 if (!expr)
2481 return NULL;
2482 if (expr->n_arg != 2)
2483 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2484 "expecting two arguments", return NULL);
2486 lhs = pet_expr_extract_affine(expr->args[0], pc);
2487 rhs = pet_expr_extract_affine(expr->args[1], pc);
2489 switch (pet_expr_op_get_type(expr)) {
2490 case pet_op_add:
2491 return isl_pw_aff_add(lhs, rhs);
2492 case pet_op_sub:
2493 return isl_pw_aff_sub(lhs, rhs);
2494 default:
2495 isl_pw_aff_free(lhs);
2496 isl_pw_aff_free(rhs);
2497 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2498 "not an addition or subtraction operation",
2499 return NULL);
2504 /* Extract an affine expression from an integer division or a modulo operation.
2505 * Return NaN if we are unable to extract an affine expression.
2507 * "pc" is the context in which the affine expression is created.
2509 * In particular, if "expr" is lhs/rhs, then return
2511 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2513 * If "expr" is lhs%rhs, then return
2515 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2517 * If the second argument (rhs) is not a (positive) integer constant,
2518 * then we fail to extract an affine expression.
2520 * We simplify the result in the context of the domain of "pc" in case
2521 * this domain implies that lhs >= 0 (or < 0).
2523 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2524 __isl_keep pet_context *pc)
2526 int is_cst;
2527 isl_pw_aff *lhs;
2528 isl_pw_aff *rhs;
2529 isl_pw_aff *res;
2531 if (!expr)
2532 return NULL;
2533 if (expr->n_arg != 2)
2534 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2535 "expecting two arguments", return NULL);
2537 rhs = pet_expr_extract_affine(expr->args[1], pc);
2539 is_cst = isl_pw_aff_is_cst(rhs);
2540 if (is_cst < 0 || !is_cst) {
2541 isl_pw_aff_free(rhs);
2542 return non_affine(pet_context_get_space(pc));
2545 lhs = pet_expr_extract_affine(expr->args[0], pc);
2547 switch (pet_expr_op_get_type(expr)) {
2548 case pet_op_div:
2549 res = isl_pw_aff_tdiv_q(lhs, rhs);
2550 break;
2551 case pet_op_mod:
2552 res = isl_pw_aff_tdiv_r(lhs, rhs);
2553 break;
2554 default:
2555 isl_pw_aff_free(lhs);
2556 isl_pw_aff_free(rhs);
2557 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2558 "not a div or mod operator", return NULL);
2561 return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc));
2564 /* Extract an affine expression from a multiplication operation.
2565 * Return NaN if we are unable to extract an affine expression.
2566 * In particular, if neither of the arguments is a (piecewise) constant
2567 * then we return NaN.
2569 * "pc" is the context in which the affine expression is created.
2571 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2572 __isl_keep pet_context *pc)
2574 int lhs_cst, rhs_cst;
2575 isl_pw_aff *lhs;
2576 isl_pw_aff *rhs;
2578 if (!expr)
2579 return NULL;
2580 if (expr->n_arg != 2)
2581 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2582 "expecting two arguments", return NULL);
2584 lhs = pet_expr_extract_affine(expr->args[0], pc);
2585 rhs = pet_expr_extract_affine(expr->args[1], pc);
2587 lhs_cst = isl_pw_aff_is_cst(lhs);
2588 rhs_cst = isl_pw_aff_is_cst(rhs);
2589 if (lhs_cst < 0 || rhs_cst < 0 || (!lhs_cst && !rhs_cst)) {
2590 isl_pw_aff_free(lhs);
2591 isl_pw_aff_free(rhs);
2592 return non_affine(pet_context_get_space(pc));
2595 return isl_pw_aff_mul(lhs, rhs);
2598 /* Extract an affine expression from a negation operation.
2599 * Return NaN if we are unable to extract an affine expression.
2601 * "pc" is the context in which the affine expression is created.
2603 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
2604 __isl_keep pet_context *pc)
2606 isl_pw_aff *res;
2608 if (!expr)
2609 return NULL;
2610 if (expr->n_arg != 1)
2611 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2612 "expecting one argument", return NULL);
2614 res = pet_expr_extract_affine(expr->args[0], pc);
2615 return isl_pw_aff_neg(res);
2618 /* Extract an affine expression from a conditional operation.
2619 * Return NaN if we are unable to extract an affine expression.
2621 * "pc" is the context in which the affine expression is created.
2623 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
2624 __isl_keep pet_context *pc)
2626 isl_pw_aff *cond, *lhs, *rhs;
2628 if (!expr)
2629 return NULL;
2630 if (expr->n_arg != 3)
2631 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2632 "expecting three arguments", return NULL);
2634 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2635 lhs = pet_expr_extract_affine(expr->args[1], pc);
2636 rhs = pet_expr_extract_affine(expr->args[2], pc);
2638 return isl_pw_aff_cond(cond, lhs, rhs);
2641 /* Compute
2643 * pwaff mod 2^width
2645 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff, unsigned width)
2647 isl_ctx *ctx;
2648 isl_val *mod;
2650 ctx = isl_pw_aff_get_ctx(pwaff);
2651 mod = isl_val_int_from_ui(ctx, width);
2652 mod = isl_val_2exp(mod);
2654 pwaff = isl_pw_aff_mod_val(pwaff, mod);
2656 return pwaff;
2659 /* Limit the domain of "pwaff" to those elements where the function
2660 * value satisfies
2662 * 2^{width-1} <= pwaff < 2^{width-1}
2664 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
2665 unsigned width)
2667 isl_ctx *ctx;
2668 isl_val *v;
2669 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
2670 isl_local_space *ls = isl_local_space_from_space(space);
2671 isl_aff *bound;
2672 isl_set *dom;
2673 isl_pw_aff *b;
2675 ctx = isl_pw_aff_get_ctx(pwaff);
2676 v = isl_val_int_from_ui(ctx, width - 1);
2677 v = isl_val_2exp(v);
2679 bound = isl_aff_zero_on_domain(ls);
2680 bound = isl_aff_add_constant_val(bound, v);
2681 b = isl_pw_aff_from_aff(bound);
2683 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
2684 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2686 b = isl_pw_aff_neg(b);
2687 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
2688 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2690 return pwaff;
2693 /* Handle potential overflows on signed computations.
2695 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2696 * then we adjust the domain of "pa" to avoid overflows.
2698 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
2699 unsigned width)
2701 isl_ctx *ctx;
2702 struct pet_options *options;
2704 if (!pa)
2705 return NULL;
2707 ctx = isl_pw_aff_get_ctx(pa);
2708 options = isl_ctx_peek_pet_options(ctx);
2709 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
2710 pa = avoid_overflow(pa, width);
2712 return pa;
2715 /* Extract an affine expression from some an operation.
2716 * Return NaN if we are unable to extract an affine expression.
2717 * If the result of a binary (non boolean) operation is unsigned,
2718 * then we wrap it based on the size of the type. If the result is signed,
2719 * then we ensure that no overflow occurs.
2721 * "pc" is the context in which the affine expression is created.
2723 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
2724 __isl_keep pet_context *pc)
2726 isl_pw_aff *res;
2727 int type_size;
2729 switch (pet_expr_op_get_type(expr)) {
2730 case pet_op_add:
2731 case pet_op_sub:
2732 res = extract_affine_add_sub(expr, pc);
2733 break;
2734 case pet_op_div:
2735 case pet_op_mod:
2736 res = extract_affine_div_mod(expr, pc);
2737 break;
2738 case pet_op_mul:
2739 res = extract_affine_mul(expr, pc);
2740 break;
2741 case pet_op_minus:
2742 return extract_affine_neg(expr, pc);
2743 case pet_op_cond:
2744 return extract_affine_cond(expr, pc);
2745 case pet_op_eq:
2746 case pet_op_ne:
2747 case pet_op_le:
2748 case pet_op_ge:
2749 case pet_op_lt:
2750 case pet_op_gt:
2751 case pet_op_land:
2752 case pet_op_lor:
2753 case pet_op_lnot:
2754 return pet_expr_extract_affine_condition(expr, pc);
2755 default:
2756 return non_affine(pet_context_get_space(pc));
2759 if (!res)
2760 return NULL;
2761 if (isl_pw_aff_involves_nan(res)) {
2762 isl_space *space = isl_pw_aff_get_domain_space(res);
2763 isl_pw_aff_free(res);
2764 return non_affine(space);
2767 type_size = pet_expr_get_type_size(expr);
2768 if (type_size > 0)
2769 res = wrap(res, type_size);
2770 else
2771 res = signed_overflow(res, -type_size);
2773 return res;
2776 /* Extract an affine expression from some special function calls.
2777 * Return NaN if we are unable to extract an affine expression.
2778 * In particular, we handle "min", "max", "ceild", "floord",
2779 * "intMod", "intFloor" and "intCeil".
2780 * In case of the latter five, the second argument needs to be
2781 * a (positive) integer constant.
2783 * "pc" is the context in which the affine expression is created.
2785 static __isl_give isl_pw_aff *extract_affine_from_call(
2786 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2788 isl_pw_aff *aff1, *aff2;
2789 int n;
2790 const char *name;
2792 n = pet_expr_get_n_arg(expr);
2793 name = pet_expr_call_get_name(expr);
2794 if (!(n == 2 && !strcmp(name, "min")) &&
2795 !(n == 2 && !strcmp(name, "max")) &&
2796 !(n == 2 && !strcmp(name, "intMod")) &&
2797 !(n == 2 && !strcmp(name, "intFloor")) &&
2798 !(n == 2 && !strcmp(name, "intCeil")) &&
2799 !(n == 2 && !strcmp(name, "floord")) &&
2800 !(n == 2 && !strcmp(name, "ceild")))
2801 return non_affine(pet_context_get_space(pc));
2803 if (!strcmp(name, "min") || !strcmp(name, "max")) {
2804 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2805 aff2 = pet_expr_extract_affine(expr->args[1], pc);
2807 if (!strcmp(name, "min"))
2808 aff1 = isl_pw_aff_min(aff1, aff2);
2809 else
2810 aff1 = isl_pw_aff_max(aff1, aff2);
2811 } else if (!strcmp(name, "intMod")) {
2812 isl_val *v;
2814 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2815 return non_affine(pet_context_get_space(pc));
2816 v = pet_expr_int_get_val(expr->args[1]);
2817 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2818 aff1 = isl_pw_aff_mod_val(aff1, v);
2819 } else {
2820 isl_val *v;
2822 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2823 return non_affine(pet_context_get_space(pc));
2824 v = pet_expr_int_get_val(expr->args[1]);
2825 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2826 aff1 = isl_pw_aff_scale_down_val(aff1, v);
2827 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
2828 aff1 = isl_pw_aff_floor(aff1);
2829 else
2830 aff1 = isl_pw_aff_ceil(aff1);
2833 return aff1;
2836 /* Extract an affine expression from "expr", if possible.
2837 * Otherwise return NaN.
2839 * "pc" is the context in which the affine expression is created.
2841 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
2842 __isl_keep pet_context *pc)
2844 if (!expr)
2845 return NULL;
2847 switch (pet_expr_get_type(expr)) {
2848 case pet_expr_access:
2849 return extract_affine_from_access(expr, pc);
2850 case pet_expr_int:
2851 return extract_affine_from_int(expr, pc);
2852 case pet_expr_op:
2853 return extract_affine_from_op(expr, pc);
2854 case pet_expr_call:
2855 return extract_affine_from_call(expr, pc);
2856 case pet_expr_cast:
2857 case pet_expr_double:
2858 case pet_expr_error:
2859 return non_affine(pet_context_get_space(pc));
2863 /* Extract an affine expressions representing the comparison "LHS op RHS"
2864 * Return NaN if we are unable to extract such an affine expression.
2866 * "pc" is the context in which the affine expression is created.
2868 * If the comparison is of the form
2870 * a <= min(b,c)
2872 * then the expression is constructed as the conjunction of
2873 * the comparisons
2875 * a <= b and a <= c
2877 * A similar optimization is performed for max(a,b) <= c.
2878 * We do this because that will lead to simpler representations
2879 * of the expression.
2880 * If isl is ever enhanced to explicitly deal with min and max expressions,
2881 * this optimization can be removed.
2883 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
2884 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
2885 __isl_keep pet_context *pc)
2887 isl_pw_aff *lhs_pa, *rhs_pa;
2889 if (op == pet_op_gt)
2890 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
2891 if (op == pet_op_ge)
2892 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
2894 if (op == pet_op_lt || op == pet_op_le) {
2895 if (pet_expr_is_min(rhs)) {
2896 lhs_pa = pet_expr_extract_comparison(op, lhs,
2897 rhs->args[0], pc);
2898 rhs_pa = pet_expr_extract_comparison(op, lhs,
2899 rhs->args[1], pc);
2900 return pet_and(lhs_pa, rhs_pa);
2902 if (pet_expr_is_max(lhs)) {
2903 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
2904 rhs, pc);
2905 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
2906 rhs, pc);
2907 return pet_and(lhs_pa, rhs_pa);
2911 lhs_pa = pet_expr_extract_affine(lhs, pc);
2912 rhs_pa = pet_expr_extract_affine(rhs, pc);
2914 return pet_comparison(op, lhs_pa, rhs_pa);
2917 /* Extract an affine expressions from the comparison "expr".
2918 * Return NaN if we are unable to extract such an affine expression.
2920 * "pc" is the context in which the affine expression is created.
2922 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
2923 __isl_keep pet_context *pc)
2925 enum pet_op_type type;
2927 if (!expr)
2928 return NULL;
2929 if (expr->n_arg != 2)
2930 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2931 "expecting two arguments", return NULL);
2933 type = pet_expr_op_get_type(expr);
2934 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
2935 pc);
2938 /* Extract an affine expression representing the boolean operation
2939 * expressed by "expr".
2940 * Return NaN if we are unable to extract an affine expression.
2942 * "pc" is the context in which the affine expression is created.
2944 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
2945 __isl_keep pet_context *pc)
2947 isl_pw_aff *lhs, *rhs;
2948 int n;
2950 if (!expr)
2951 return NULL;
2953 n = pet_expr_get_n_arg(expr);
2954 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
2955 if (n == 1)
2956 return pet_not(lhs);
2958 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
2959 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
2962 /* Extract the affine expression "expr != 0 ? 1 : 0".
2963 * Return NaN if we are unable to extract an affine expression.
2965 * "pc" is the context in which the affine expression is created.
2967 static __isl_give isl_pw_aff *extract_implicit_condition(
2968 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2970 isl_pw_aff *res;
2972 res = pet_expr_extract_affine(expr, pc);
2973 return pet_to_bool(res);
2976 /* Extract a boolean affine expression from "expr".
2977 * Return NaN if we are unable to extract an affine expression.
2979 * "pc" is the context in which the affine expression is created.
2981 * If "expr" is neither a comparison nor a boolean operation,
2982 * then we assume it is an affine expression and return the
2983 * boolean expression "expr != 0 ? 1 : 0".
2985 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
2986 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2988 if (!expr)
2989 return NULL;
2991 if (pet_expr_is_comparison(expr))
2992 return extract_comparison(expr, pc);
2993 if (pet_expr_is_boolean(expr))
2994 return extract_boolean(expr, pc);
2996 return extract_implicit_condition(expr, pc);
2999 /* Check if "expr" is an assume expression and if its single argument
3000 * can be converted to an affine expression in the context of "pc".
3001 * If so, replace the argument by the affine expression.
3003 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
3004 __isl_keep pet_context *pc)
3006 isl_pw_aff *cond;
3007 isl_multi_pw_aff *index;
3009 if (!expr)
3010 return NULL;
3011 if (!pet_expr_is_assume(expr))
3012 return expr;
3013 if (expr->n_arg != 1)
3014 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3015 "expecting one argument", return pet_expr_free(expr));
3017 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3018 if (!cond)
3019 return pet_expr_free(expr);
3020 if (isl_pw_aff_involves_nan(cond)) {
3021 isl_pw_aff_free(cond);
3022 return expr;
3025 index = isl_multi_pw_aff_from_pw_aff(cond);
3026 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
3028 return expr;
3031 /* Return the number of bits needed to represent the type of "expr".
3032 * See the description of the type_size field of pet_expr.
3034 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
3036 return expr ? expr->type_size : 0;
3039 /* Replace the number of bits needed to represent the type of "expr"
3040 * by "type_size".
3041 * See the description of the type_size field of pet_expr.
3043 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
3044 int type_size)
3046 expr = pet_expr_cow(expr);
3047 if (!expr)
3048 return NULL;
3050 expr->type_size = type_size;
3052 return expr;
3055 /* Extend an access expression "expr" with an additional index "index".
3056 * In particular, add "index" as an extra argument to "expr" and
3057 * adjust the index expression of "expr" to refer to this extra argument.
3058 * The caller is responsible for calling pet_expr_access_set_depth
3059 * to update the corresponding access relation.
3061 * Note that we only collect the individual index expressions as
3062 * arguments of "expr" here.
3063 * An attempt to integrate them into the index expression of "expr"
3064 * is performed in pet_expr_access_plug_in_args.
3066 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
3067 __isl_take pet_expr *index)
3069 int n;
3070 isl_space *space;
3071 isl_local_space *ls;
3072 isl_pw_aff *pa;
3074 expr = pet_expr_cow(expr);
3075 if (!expr || !index)
3076 goto error;
3077 if (expr->type != pet_expr_access)
3078 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3079 "not an access pet_expr", goto error);
3081 n = pet_expr_get_n_arg(expr);
3082 expr = pet_expr_insert_arg(expr, n, index);
3083 if (!expr)
3084 return NULL;
3086 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3087 ls = isl_local_space_from_space(space);
3088 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
3089 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
3090 if (!expr->acc.index)
3091 return pet_expr_free(expr);
3093 return expr;
3094 error:
3095 pet_expr_free(expr);
3096 pet_expr_free(index);
3097 return NULL;
3100 /* Extend an access expression "expr" with an additional member acces to "id".
3101 * In particular, extend the index expression of "expr" to include
3102 * the additional member access.
3103 * The caller is responsible for calling pet_expr_access_set_depth
3104 * to update the corresponding access relation.
3106 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
3107 __isl_take isl_id *id)
3109 isl_space *space;
3110 isl_multi_pw_aff *field_access;
3112 expr = pet_expr_cow(expr);
3113 if (!expr || !id)
3114 goto error;
3115 if (expr->type != pet_expr_access)
3116 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3117 "not an access pet_expr", goto error);
3119 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3120 space = isl_space_from_domain(space);
3121 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3122 field_access = isl_multi_pw_aff_zero(space);
3123 expr->acc.index = pet_array_member(expr->acc.index, field_access);
3124 if (!expr->acc.index)
3125 return pet_expr_free(expr);
3127 return expr;
3128 error:
3129 pet_expr_free(expr);
3130 isl_id_free(id);
3131 return NULL;
3134 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
3136 int i;
3138 if (!expr)
3139 return;
3141 fprintf(stderr, "%*s", indent, "");
3143 switch (expr->type) {
3144 case pet_expr_double:
3145 fprintf(stderr, "%s\n", expr->d.s);
3146 break;
3147 case pet_expr_int:
3148 isl_val_dump(expr->i);
3149 break;
3150 case pet_expr_access:
3151 if (expr->acc.ref_id) {
3152 isl_id_dump(expr->acc.ref_id);
3153 fprintf(stderr, "%*s", indent, "");
3155 isl_multi_pw_aff_dump(expr->acc.index);
3156 fprintf(stderr, "%*sdepth: %d\n", indent + 2,
3157 "", expr->acc.depth);
3158 fprintf(stderr, "%*sread: %d\n", indent + 2,
3159 "", expr->acc.read);
3160 fprintf(stderr, "%*swrite: %d\n", indent + 2,
3161 "", expr->acc.write);
3162 if (expr->acc.access) {
3163 fprintf(stderr, "%*saccess: ", indent + 2, "");
3164 isl_map_dump(expr->acc.access);
3166 for (i = 0; i < expr->n_arg; ++i)
3167 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3168 break;
3169 case pet_expr_op:
3170 fprintf(stderr, "%s\n", op_str[expr->op]);
3171 for (i = 0; i < expr->n_arg; ++i)
3172 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3173 break;
3174 case pet_expr_call:
3175 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
3176 for (i = 0; i < expr->n_arg; ++i)
3177 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3178 break;
3179 case pet_expr_cast:
3180 fprintf(stderr, "(%s)\n", expr->type_name);
3181 for (i = 0; i < expr->n_arg; ++i)
3182 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3183 break;
3184 case pet_expr_error:
3185 fprintf(stderr, "ERROR\n");
3186 break;
3190 void pet_expr_dump(__isl_keep pet_expr *expr)
3192 pet_expr_dump_with_indent(expr, 0);