PetScan::extract_access_expr: construct access expression directly from pet_expr
[pet.git] / expr.c
blobef3a88e5d82e8f4082861faf9ccbc884a7e56f29
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 access relation and an index expression.
151 * By default, it is considered to be a read access.
153 __isl_give pet_expr *pet_expr_from_access_and_index( __isl_take isl_map *access,
154 __isl_take isl_multi_pw_aff *index)
156 isl_ctx *ctx = isl_map_get_ctx(access);
157 pet_expr *expr;
159 if (!index || !access)
160 goto error;
161 expr = pet_expr_alloc(ctx, pet_expr_access);
162 if (!expr)
163 goto error;
165 expr->acc.access = access;
166 expr->acc.index = index;
167 expr->acc.read = 1;
168 expr->acc.write = 0;
170 return expr;
171 error:
172 isl_map_free(access);
173 isl_multi_pw_aff_free(index);
174 return NULL;
177 /* Construct an access pet_expr from an index expression.
178 * By default, the access is considered to be a read access.
180 __isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
182 isl_map *access;
184 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
185 return pet_expr_from_access_and_index(access, index);
188 /* Extend the range of "access" with "n" dimensions, retaining
189 * the tuple identifier on this range.
191 * If "access" represents a member access, then extend the range
192 * of the member.
194 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
196 isl_id *id;
198 id = isl_map_get_tuple_id(access, isl_dim_out);
200 if (!isl_map_range_is_wrapping(access)) {
201 access = isl_map_add_dims(access, isl_dim_out, n);
202 } else {
203 isl_map *domain;
205 domain = isl_map_copy(access);
206 domain = isl_map_range_factor_domain(domain);
207 access = isl_map_range_factor_range(access);
208 access = extend_range(access, n);
209 access = isl_map_range_product(domain, access);
212 access = isl_map_set_tuple_id(access, isl_dim_out, id);
214 return access;
217 /* Finalize the construction of an access expression by setting
218 * the depth of the accessed array.
220 * The index expression may have been updated by
221 * pet_expr_access_subscript and/or pet_expr_access_member
222 * without the access relation having been updated accordingly.
223 * We perform this update here, taking into account the depth
224 * of the accessed array.
226 * If the number of indices is smaller than the depth of the array,
227 * then we assume that all elements of the remaining dimensions
228 * are accessed.
230 __isl_give pet_expr *pet_expr_access_set_depth(__isl_take pet_expr *expr,
231 int depth)
233 isl_map *access;
234 int dim;
236 expr = pet_expr_cow(expr);
237 if (!expr)
238 return NULL;
240 access = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
241 if (!access)
242 return pet_expr_free(expr);
244 dim = isl_map_dim(access, isl_dim_out);
245 if (dim > depth)
246 isl_die(isl_map_get_ctx(access), isl_error_internal,
247 "number of indices greater than depth",
248 access = isl_map_free(access));
250 if (dim != depth)
251 access = extend_range(access, depth - dim);
253 return pet_expr_access_set_access(expr, access);
256 /* Construct a pet_expr that kills the elements specified by
257 * the index expression "index" and the access relation "access".
259 __isl_give pet_expr *pet_expr_kill_from_access_and_index(
260 __isl_take isl_map *access, __isl_take isl_multi_pw_aff *index)
262 pet_expr *expr;
264 if (!access || !index)
265 goto error;
267 expr = pet_expr_from_access_and_index(access, index);
268 expr = pet_expr_access_set_read(expr, 0);
269 return pet_expr_new_unary(pet_op_kill, expr);
270 error:
271 isl_map_free(access);
272 isl_multi_pw_aff_free(index);
273 return NULL;
276 /* Construct a unary pet_expr that performs "op" on "arg".
278 __isl_give pet_expr *pet_expr_new_unary(enum pet_op_type op,
279 __isl_take pet_expr *arg)
281 isl_ctx *ctx;
282 pet_expr *expr;
284 if (!arg)
285 return NULL;
286 ctx = pet_expr_get_ctx(arg);
287 expr = pet_expr_alloc(ctx, pet_expr_op);
288 expr = pet_expr_set_n_arg(expr, 1);
289 if (!expr)
290 goto error;
292 expr->op = op;
293 expr->args[pet_un_arg] = arg;
295 return expr;
296 error:
297 pet_expr_free(arg);
298 return NULL;
301 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
302 * where the result is represented using a type of "type_size" bits
303 * (may be zero if unknown or if the type is not an integer).
305 __isl_give pet_expr *pet_expr_new_binary(int type_size, enum pet_op_type op,
306 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
308 isl_ctx *ctx;
309 pet_expr *expr;
311 if (!lhs || !rhs)
312 goto error;
313 ctx = pet_expr_get_ctx(lhs);
314 expr = pet_expr_alloc(ctx, pet_expr_op);
315 expr = pet_expr_set_n_arg(expr, 2);
316 if (!expr)
317 goto error;
319 expr->op = op;
320 expr->type_size = type_size;
321 expr->args[pet_bin_lhs] = lhs;
322 expr->args[pet_bin_rhs] = rhs;
324 return expr;
325 error:
326 pet_expr_free(lhs);
327 pet_expr_free(rhs);
328 return NULL;
331 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
333 __isl_give pet_expr *pet_expr_new_ternary(__isl_take pet_expr *cond,
334 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
336 isl_ctx *ctx;
337 pet_expr *expr;
339 if (!cond || !lhs || !rhs)
340 goto error;
341 ctx = pet_expr_get_ctx(cond);
342 expr = pet_expr_alloc(ctx, pet_expr_op);
343 expr = pet_expr_set_n_arg(expr, 3);
344 if (!expr)
345 goto error;
347 expr->op = pet_op_cond;
348 expr->args[pet_ter_cond] = cond;
349 expr->args[pet_ter_true] = lhs;
350 expr->args[pet_ter_false] = rhs;
352 return expr;
353 error:
354 pet_expr_free(cond);
355 pet_expr_free(lhs);
356 pet_expr_free(rhs);
357 return NULL;
360 /* Construct a call pet_expr that calls function "name" with "n_arg"
361 * arguments. The caller is responsible for filling in the arguments.
363 __isl_give pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
364 unsigned n_arg)
366 pet_expr *expr;
368 expr = pet_expr_alloc(ctx, pet_expr_call);
369 expr = pet_expr_set_n_arg(expr, n_arg);
370 if (!expr)
371 return NULL;
373 expr->name = strdup(name);
374 if (!expr->name)
375 return pet_expr_free(expr);
377 return expr;
380 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
382 __isl_give pet_expr *pet_expr_new_cast(const char *type_name,
383 __isl_take pet_expr *arg)
385 isl_ctx *ctx;
386 pet_expr *expr;
388 if (!arg)
389 return NULL;
391 ctx = pet_expr_get_ctx(arg);
392 expr = pet_expr_alloc(ctx, pet_expr_cast);
393 expr = pet_expr_set_n_arg(expr, 1);
394 if (!expr)
395 goto error;
397 expr->type_name = strdup(type_name);
398 if (!expr->type_name)
399 goto error;
401 expr->args[0] = arg;
403 return expr;
404 error:
405 pet_expr_free(arg);
406 pet_expr_free(expr);
407 return NULL;
410 /* Construct a pet_expr that represents the double "d".
412 __isl_give pet_expr *pet_expr_new_double(isl_ctx *ctx,
413 double val, const char *s)
415 pet_expr *expr;
417 expr = pet_expr_alloc(ctx, pet_expr_double);
418 if (!expr)
419 return NULL;
421 expr->d.val = val;
422 expr->d.s = strdup(s);
423 if (!expr->d.s)
424 return pet_expr_free(expr);
426 return expr;
429 /* Construct a pet_expr that represents the integer value "v".
431 __isl_give pet_expr *pet_expr_new_int(__isl_take isl_val *v)
433 isl_ctx *ctx;
434 pet_expr *expr;
436 if (!v)
437 return NULL;
439 ctx = isl_val_get_ctx(v);
440 expr = pet_expr_alloc(ctx, pet_expr_int);
441 if (!expr)
442 goto error;
444 expr->i = v;
446 return expr;
447 error:
448 isl_val_free(v);
449 return NULL;
452 static __isl_give pet_expr *pet_expr_dup(__isl_keep pet_expr *expr)
454 int i;
455 pet_expr *dup;
457 if (!expr)
458 return NULL;
460 dup = pet_expr_alloc(expr->ctx, expr->type);
461 dup = pet_expr_set_type_size(dup, expr->type_size);
462 dup = pet_expr_set_n_arg(dup, expr->n_arg);
463 for (i = 0; i < expr->n_arg; ++i)
464 dup = pet_expr_set_arg(dup, i, pet_expr_copy(expr->args[i]));
466 switch (expr->type) {
467 case pet_expr_access:
468 if (expr->acc.ref_id)
469 dup = pet_expr_access_set_ref_id(dup,
470 isl_id_copy(expr->acc.ref_id));
471 dup = pet_expr_access_set_access(dup,
472 isl_map_copy(expr->acc.access));
473 dup = pet_expr_access_set_index(dup,
474 isl_multi_pw_aff_copy(expr->acc.index));
475 dup = pet_expr_access_set_read(dup, expr->acc.read);
476 dup = pet_expr_access_set_write(dup, expr->acc.write);
477 break;
478 case pet_expr_call:
479 dup = pet_expr_call_set_name(dup, expr->name);
480 break;
481 case pet_expr_cast:
482 dup = pet_expr_cast_set_type_name(dup, expr->type_name);
483 break;
484 case pet_expr_double:
485 dup = pet_expr_double_set(dup, expr->d.val, expr->d.s);
486 break;
487 case pet_expr_int:
488 dup = pet_expr_int_set_val(dup, isl_val_copy(expr->i));
489 break;
490 case pet_expr_op:
491 dup = pet_expr_op_set_type(dup, expr->op);
492 break;
493 case pet_expr_error:
494 dup = pet_expr_free(dup);
495 break;
498 return dup;
501 __isl_give pet_expr *pet_expr_cow(__isl_take pet_expr *expr)
503 if (!expr)
504 return NULL;
506 if (expr->ref == 1)
507 return expr;
508 expr->ref--;
509 return pet_expr_dup(expr);
512 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr)
514 int i;
516 if (!expr)
517 return NULL;
518 if (--expr->ref > 0)
519 return NULL;
521 for (i = 0; i < expr->n_arg; ++i)
522 pet_expr_free(expr->args[i]);
523 free(expr->args);
525 switch (expr->type) {
526 case pet_expr_access:
527 isl_id_free(expr->acc.ref_id);
528 isl_map_free(expr->acc.access);
529 isl_multi_pw_aff_free(expr->acc.index);
530 break;
531 case pet_expr_call:
532 free(expr->name);
533 break;
534 case pet_expr_cast:
535 free(expr->type_name);
536 break;
537 case pet_expr_double:
538 free(expr->d.s);
539 break;
540 case pet_expr_int:
541 isl_val_free(expr->i);
542 break;
543 case pet_expr_op:
544 case pet_expr_error:
545 break;
548 isl_ctx_deref(expr->ctx);
549 free(expr);
550 return NULL;
553 /* Return an additional reference to "expr".
555 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr)
557 if (!expr)
558 return NULL;
560 expr->ref++;
561 return expr;
564 /* Return the isl_ctx in which "expr" was created.
566 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr)
568 return expr ? expr->ctx : NULL;
571 /* Return the type of "expr".
573 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr)
575 if (!expr)
576 return pet_expr_error;
577 return expr->type;
580 /* Return the number of arguments of "expr".
582 int pet_expr_get_n_arg(__isl_keep pet_expr *expr)
584 if (!expr)
585 return -1;
587 return expr->n_arg;
590 /* Set the number of arguments of "expr" to "n".
592 * If "expr" originally had more arguments, then remove the extra arguments.
593 * If "expr" originally had fewer arguments, then create space for
594 * the extra arguments ans initialize them to NULL.
596 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n)
598 int i;
599 pet_expr **args;
601 if (!expr)
602 return NULL;
603 if (expr->n_arg == n)
604 return expr;
605 expr = pet_expr_cow(expr);
606 if (!expr)
607 return NULL;
609 if (n < expr->n_arg) {
610 for (i = n; i < expr->n_arg; ++i)
611 pet_expr_free(expr->args[i]);
612 expr->n_arg = n;
613 return expr;
616 args = isl_realloc_array(expr->ctx, expr->args, pet_expr *, n);
617 if (!args)
618 return pet_expr_free(expr);
619 expr->args = args;
620 for (i = expr->n_arg; i < n; ++i)
621 expr->args[i] = NULL;
622 expr->n_arg = n;
624 return expr;
627 /* Return the argument of "expr" at position "pos".
629 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos)
631 if (!expr)
632 return NULL;
633 if (pos < 0 || pos >= expr->n_arg)
634 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
635 "position out of bounds", return NULL);
637 return pet_expr_copy(expr->args[pos]);
640 /* Replace the argument of "expr" at position "pos" by "arg".
642 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
643 __isl_take pet_expr *arg)
645 if (!expr || !arg)
646 goto error;
647 if (pos < 0 || pos >= expr->n_arg)
648 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
649 "position out of bounds", goto error);
650 if (expr->args[pos] == arg) {
651 pet_expr_free(arg);
652 return expr;
655 expr = pet_expr_cow(expr);
656 if (!expr)
657 goto error;
659 pet_expr_free(expr->args[pos]);
660 expr->args[pos] = arg;
662 return expr;
663 error:
664 pet_expr_free(expr);
665 pet_expr_free(arg);
666 return NULL;
669 /* Does "expr" perform a comparison operation?
671 int pet_expr_is_comparison(__isl_keep pet_expr *expr)
673 if (!expr)
674 return -1;
675 if (expr->type != pet_expr_op)
676 return 0;
677 switch (expr->op) {
678 case pet_op_eq:
679 case pet_op_ne:
680 case pet_op_le:
681 case pet_op_ge:
682 case pet_op_lt:
683 case pet_op_gt:
684 return 1;
685 default:
686 return 0;
690 /* Does "expr" perform a boolean operation?
692 int pet_expr_is_boolean(__isl_keep pet_expr *expr)
694 if (!expr)
695 return -1;
696 if (expr->type != pet_expr_op)
697 return 0;
698 switch (expr->op) {
699 case pet_op_land:
700 case pet_op_lor:
701 case pet_op_lnot:
702 return 1;
703 default:
704 return 0;
708 /* Does "expr" perform a min operation?
710 int pet_expr_is_min(__isl_keep pet_expr *expr)
712 if (!expr)
713 return -1;
714 if (expr->type != pet_expr_call)
715 return 0;
716 if (expr->n_arg != 2)
717 return 0;
718 if (strcmp(expr->name, "min") != 0)
719 return 0;
720 return 1;
723 /* Does "expr" perform a max operation?
725 int pet_expr_is_max(__isl_keep pet_expr *expr)
727 if (!expr)
728 return -1;
729 if (expr->type != pet_expr_call)
730 return 0;
731 if (expr->n_arg != 2)
732 return 0;
733 if (strcmp(expr->name, "max") != 0)
734 return 0;
735 return 1;
738 /* Does "expr" represent an access to an unnamed space, i.e.,
739 * does it represent an affine expression?
741 int pet_expr_is_affine(__isl_keep pet_expr *expr)
743 int has_id;
745 if (!expr)
746 return -1;
747 if (expr->type != pet_expr_access)
748 return 0;
750 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
751 if (has_id < 0)
752 return -1;
754 return !has_id;
757 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
758 * not part of any struct?
760 int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
762 if (!expr)
763 return -1;
764 if (expr->type != pet_expr_access)
765 return 0;
766 if (isl_map_range_is_wrapping(expr->acc.access))
767 return 0;
769 return isl_map_dim(expr->acc.access, isl_dim_out) == 0;
772 /* Return 1 if the two pet_exprs are equivalent.
774 int pet_expr_is_equal(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2)
776 int i;
778 if (!expr1 || !expr2)
779 return 0;
781 if (expr1->type != expr2->type)
782 return 0;
783 if (expr1->n_arg != expr2->n_arg)
784 return 0;
785 for (i = 0; i < expr1->n_arg; ++i)
786 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
787 return 0;
788 switch (expr1->type) {
789 case pet_expr_error:
790 return -1;
791 case pet_expr_double:
792 if (strcmp(expr1->d.s, expr2->d.s))
793 return 0;
794 if (expr1->d.val != expr2->d.val)
795 return 0;
796 break;
797 case pet_expr_int:
798 if (!isl_val_eq(expr1->i, expr2->i))
799 return 0;
800 break;
801 case pet_expr_access:
802 if (expr1->acc.read != expr2->acc.read)
803 return 0;
804 if (expr1->acc.write != expr2->acc.write)
805 return 0;
806 if (expr1->acc.ref_id != expr2->acc.ref_id)
807 return 0;
808 if (!expr1->acc.access || !expr2->acc.access)
809 return 0;
810 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
811 return 0;
812 if (!expr1->acc.index || !expr2->acc.index)
813 return 0;
814 if (!isl_multi_pw_aff_plain_is_equal(expr1->acc.index,
815 expr2->acc.index))
816 return 0;
817 break;
818 case pet_expr_op:
819 if (expr1->op != expr2->op)
820 return 0;
821 break;
822 case pet_expr_call:
823 if (strcmp(expr1->name, expr2->name))
824 return 0;
825 break;
826 case pet_expr_cast:
827 if (strcmp(expr1->type_name, expr2->type_name))
828 return 0;
829 break;
832 return 1;
835 /* Does the access expression "expr" read the accessed elements?
837 int pet_expr_access_is_read(__isl_keep pet_expr *expr)
839 if (!expr)
840 return -1;
841 if (expr->type != pet_expr_access)
842 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
843 "not an access expression", return -1);
845 return expr->acc.read;
848 /* Does the access expression "expr" write to the accessed elements?
850 int pet_expr_access_is_write(__isl_keep pet_expr *expr)
852 if (!expr)
853 return -1;
854 if (expr->type != pet_expr_access)
855 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
856 "not an access expression", return -1);
858 return expr->acc.write;
861 /* Return the identifier of the array accessed by "expr".
863 * If "expr" represents a member access, then return the identifier
864 * of the outer structure array.
866 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
868 if (!expr)
869 return NULL;
870 if (expr->type != pet_expr_access)
871 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
872 "not an access expression", return NULL);
874 if (isl_map_range_is_wrapping(expr->acc.access)) {
875 isl_space *space;
876 isl_id *id;
878 space = isl_map_get_space(expr->acc.access);
879 space = isl_space_range(space);
880 while (space && isl_space_is_wrapping(space))
881 space = isl_space_domain(isl_space_unwrap(space));
882 id = isl_space_get_tuple_id(space, isl_dim_set);
883 isl_space_free(space);
885 return id;
888 return isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
891 /* Return the parameter space of "expr".
893 __isl_give isl_space *pet_expr_access_get_parameter_space(
894 __isl_keep pet_expr *expr)
896 isl_space *space;
898 if (!expr)
899 return NULL;
900 if (expr->type != pet_expr_access)
901 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
902 "not an access expression", return NULL);
904 space = isl_multi_pw_aff_get_space(expr->acc.index);
905 space = isl_space_params(space);
907 return space;
910 /* Return the space of the data accessed by "expr".
912 __isl_give isl_space *pet_expr_access_get_data_space(__isl_keep pet_expr *expr)
914 isl_space *space;
916 if (!expr)
917 return NULL;
918 if (expr->type != pet_expr_access)
919 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
920 "not an access expression", return NULL);
922 space = isl_multi_pw_aff_get_space(expr->acc.index);
923 space = isl_space_range(space);
925 return space;
928 /* Modify all expressions of type pet_expr_access in "expr"
929 * by calling "fn" on them.
931 __isl_give pet_expr *pet_expr_map_access(__isl_take pet_expr *expr,
932 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
933 void *user)
935 int i, n;
937 n = pet_expr_get_n_arg(expr);
938 for (i = 0; i < n; ++i) {
939 pet_expr *arg = pet_expr_get_arg(expr, i);
940 arg = pet_expr_map_access(arg, fn, user);
941 expr = pet_expr_set_arg(expr, i, arg);
944 if (!expr)
945 return NULL;
947 if (expr->type == pet_expr_access)
948 expr = fn(expr, user);
950 return expr;
953 /* Call "fn" on each of the subexpressions of "expr" of type "type".
955 * Return -1 on error (where fn returning a negative value is treated as
956 * an error).
957 * Otherwise return 0.
959 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
960 enum pet_expr_type type,
961 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
963 int i;
965 if (!expr)
966 return -1;
968 for (i = 0; i < expr->n_arg; ++i)
969 if (pet_expr_foreach_expr_of_type(expr->args[i],
970 type, fn, user) < 0)
971 return -1;
973 if (expr->type == type)
974 return fn(expr, user);
976 return 0;
979 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
981 * Return -1 on error (where fn returning a negative value is treated as
982 * an error).
983 * Otherwise return 0.
985 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
986 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
988 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
991 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
993 * Return -1 on error (where fn returning a negative value is treated as
994 * an error).
995 * Otherwise return 0.
997 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
998 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1000 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1003 /* Internal data structure for pet_expr_writes.
1004 * "id" is the identifier that we are looking for.
1005 * "found" is set if we have found the identifier being written to.
1007 struct pet_expr_writes_data {
1008 isl_id *id;
1009 int found;
1012 /* Given an access expression, check if it writes to data->id.
1013 * If so, set data->found and abort the search.
1015 static int writes(__isl_keep pet_expr *expr, void *user)
1017 struct pet_expr_writes_data *data = user;
1018 isl_id *write_id;
1020 if (!expr->acc.write)
1021 return 0;
1022 if (pet_expr_is_affine(expr))
1023 return 0;
1025 write_id = pet_expr_access_get_id(expr);
1026 isl_id_free(write_id);
1028 if (!write_id)
1029 return -1;
1031 if (write_id != data->id)
1032 return 0;
1034 data->found = 1;
1035 return -1;
1038 /* Does expression "expr" write to "id"?
1040 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1042 struct pet_expr_writes_data data;
1044 data.id = id;
1045 data.found = 0;
1046 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1047 !data.found)
1048 return -1;
1050 return data.found;
1053 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1054 * index expression and access relation of "expr"
1055 * to dimensions of "dst_type" at "dst_pos".
1057 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1058 enum isl_dim_type dst_type, unsigned dst_pos,
1059 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1061 expr = pet_expr_cow(expr);
1062 if (!expr)
1063 return NULL;
1064 if (expr->type != pet_expr_access)
1065 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1066 "not an access pet_expr", return pet_expr_free(expr));
1068 expr->acc.access = isl_map_move_dims(expr->acc.access,
1069 dst_type, dst_pos, src_type, src_pos, n);
1070 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1071 dst_type, dst_pos, src_type, src_pos, n);
1072 if (!expr->acc.access || !expr->acc.index)
1073 return pet_expr_free(expr);
1075 return expr;
1078 /* Replace the index expression and access relation of "expr"
1079 * by their preimages under the function represented by "ma".
1081 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1082 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1084 expr = pet_expr_cow(expr);
1085 if (!expr || !ma)
1086 goto error;
1087 if (expr->type != pet_expr_access)
1088 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1089 "not an access pet_expr", goto error);
1091 expr->acc.access = isl_map_preimage_domain_multi_aff(expr->acc.access,
1092 isl_multi_aff_copy(ma));
1093 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1094 ma);
1095 if (!expr->acc.access || !expr->acc.index)
1096 return pet_expr_free(expr);
1098 return expr;
1099 error:
1100 isl_multi_aff_free(ma);
1101 pet_expr_free(expr);
1102 return NULL;
1105 /* Replace the index expression and access relation of "expr"
1106 * by their preimages under the function represented by "mpa".
1108 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1109 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1111 expr = pet_expr_cow(expr);
1112 if (!expr || !mpa)
1113 goto error;
1114 if (expr->type != pet_expr_access)
1115 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1116 "not an access pet_expr", goto error);
1118 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
1119 expr->acc.access, isl_multi_pw_aff_copy(mpa));
1120 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1121 expr->acc.index, mpa);
1122 if (!expr->acc.access || !expr->acc.index)
1123 return pet_expr_free(expr);
1125 return expr;
1126 error:
1127 isl_multi_pw_aff_free(mpa);
1128 pet_expr_free(expr);
1129 return NULL;
1132 /* Return the access relation of access expression "expr".
1134 __isl_give isl_map *pet_expr_access_get_access(__isl_keep pet_expr *expr)
1136 if (!expr)
1137 return NULL;
1138 if (expr->type != pet_expr_access)
1139 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1140 "not an access expression", return NULL);
1142 return isl_map_copy(expr->acc.access);
1145 /* Return the index expression of access expression "expr".
1147 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1148 __isl_keep pet_expr *expr)
1150 if (!expr)
1151 return NULL;
1152 if (expr->type != pet_expr_access)
1153 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1154 "not an access expression", return NULL);
1156 return isl_multi_pw_aff_copy(expr->acc.index);
1159 /* Align the parameters of expr->acc.index and expr->acc.access.
1161 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1163 expr = pet_expr_cow(expr);
1164 if (!expr)
1165 return NULL;
1166 if (expr->type != pet_expr_access)
1167 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1168 "not an access expression", return pet_expr_free(expr));
1170 expr->acc.access = isl_map_align_params(expr->acc.access,
1171 isl_multi_pw_aff_get_space(expr->acc.index));
1172 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1173 isl_map_get_space(expr->acc.access));
1174 if (!expr->acc.access || !expr->acc.index)
1175 return pet_expr_free(expr);
1177 return expr;
1180 /* Add extra conditions on the parameters to all access relations in "expr".
1182 * The conditions are not added to the index expression. Instead, they
1183 * are used to try and simplify the index expression.
1185 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1186 __isl_take isl_set *cond)
1188 int i;
1190 expr = pet_expr_cow(expr);
1191 if (!expr)
1192 goto error;
1194 for (i = 0; i < expr->n_arg; ++i) {
1195 expr->args[i] = pet_expr_restrict(expr->args[i],
1196 isl_set_copy(cond));
1197 if (!expr->args[i])
1198 goto error;
1201 if (expr->type == pet_expr_access) {
1202 expr->acc.access = isl_map_intersect_params(expr->acc.access,
1203 isl_set_copy(cond));
1204 expr->acc.index = isl_multi_pw_aff_gist_params(
1205 expr->acc.index, isl_set_copy(cond));
1206 if (!expr->acc.access || !expr->acc.index)
1207 goto error;
1210 isl_set_free(cond);
1211 return expr;
1212 error:
1213 isl_set_free(cond);
1214 return pet_expr_free(expr);
1217 /* Modify the access relation and index expression
1218 * of the given access expression
1219 * based on the given iteration space transformation.
1220 * In particular, precompose the access relation and index expression
1221 * with the update function.
1223 * If the access has any arguments then the domain of the access relation
1224 * is a wrapped mapping from the iteration space to the space of
1225 * argument values. We only need to change the domain of this wrapped
1226 * mapping, so we extend the input transformation with an identity mapping
1227 * on the space of argument values.
1229 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1230 __isl_keep isl_multi_pw_aff *update)
1232 isl_space *space;
1234 expr = pet_expr_cow(expr);
1235 if (!expr)
1236 return NULL;
1237 if (expr->type != pet_expr_access)
1238 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1239 "not an access expression", return pet_expr_free(expr));
1241 update = isl_multi_pw_aff_copy(update);
1243 space = isl_map_get_space(expr->acc.access);
1244 space = isl_space_domain(space);
1245 if (!isl_space_is_wrapping(space))
1246 isl_space_free(space);
1247 else {
1248 isl_multi_pw_aff *id;
1249 space = isl_space_unwrap(space);
1250 space = isl_space_range(space);
1251 space = isl_space_map_from_set(space);
1252 id = isl_multi_pw_aff_identity(space);
1253 update = isl_multi_pw_aff_product(update, id);
1256 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
1257 expr->acc.access,
1258 isl_multi_pw_aff_copy(update));
1259 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1260 expr->acc.index, update);
1261 if (!expr->acc.access || !expr->acc.index)
1262 return pet_expr_free(expr);
1264 return expr;
1267 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1269 isl_multi_pw_aff *update = user;
1271 return pet_expr_access_update_domain(expr, update);
1274 /* Modify all access relations in "expr" by precomposing them with
1275 * the given iteration space transformation.
1277 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1278 __isl_take isl_multi_pw_aff *update)
1280 expr = pet_expr_map_access(expr, &update_domain, update);
1281 isl_multi_pw_aff_free(update);
1282 return expr;
1285 /* Add all parameters in "space" to the access relation and index expression
1286 * of "expr".
1288 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1290 isl_space *space = user;
1292 expr = pet_expr_cow(expr);
1293 if (!expr)
1294 return NULL;
1295 if (expr->type != pet_expr_access)
1296 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1297 "not an access expression", return pet_expr_free(expr));
1299 expr->acc.access = isl_map_align_params(expr->acc.access,
1300 isl_space_copy(space));
1301 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1302 isl_space_copy(space));
1303 if (!expr->acc.access || !expr->acc.index)
1304 return pet_expr_free(expr);
1306 return expr;
1309 /* Add all parameters in "space" to all access relations and index expressions
1310 * in "expr".
1312 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1313 __isl_take isl_space *space)
1315 expr = pet_expr_map_access(expr, &align_params, space);
1316 isl_space_free(space);
1317 return expr;
1320 /* Insert an argument expression corresponding to "test" in front
1321 * of the list of arguments described by *n_arg and *args.
1323 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1324 __isl_keep isl_multi_pw_aff *test)
1326 int i;
1327 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1329 if (!test)
1330 return pet_expr_free(expr);
1331 expr = pet_expr_cow(expr);
1332 if (!expr)
1333 return NULL;
1335 if (!expr->args) {
1336 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1337 if (!expr->args)
1338 return pet_expr_free(expr);
1339 } else {
1340 pet_expr **ext;
1341 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1342 if (!ext)
1343 return pet_expr_free(expr);
1344 for (i = 0; i < expr->n_arg; ++i)
1345 ext[1 + i] = expr->args[i];
1346 free(expr->args);
1347 expr->args = ext;
1349 expr->n_arg++;
1350 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1351 if (!expr->args[0])
1352 return pet_expr_free(expr);
1354 return expr;
1357 /* Make the expression "expr" depend on the value of "test"
1358 * being equal to "satisfied".
1360 * If "test" is an affine expression, we simply add the conditions
1361 * on the expression having the value "satisfied" to all access relations
1362 * and index expressions.
1364 * Otherwise, we add a filter to "expr" (which is then assumed to be
1365 * an access expression) corresponding to "test" being equal to "satisfied".
1367 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
1368 __isl_take isl_multi_pw_aff *test, int satisfied)
1370 isl_id *id;
1371 isl_ctx *ctx;
1372 isl_space *space;
1373 isl_pw_multi_aff *pma;
1375 expr = pet_expr_cow(expr);
1376 if (!expr || !test)
1377 goto error;
1379 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
1380 isl_pw_aff *pa;
1381 isl_set *cond;
1383 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
1384 isl_multi_pw_aff_free(test);
1385 if (satisfied)
1386 cond = isl_pw_aff_non_zero_set(pa);
1387 else
1388 cond = isl_pw_aff_zero_set(pa);
1389 return pet_expr_restrict(expr, isl_set_params(cond));
1392 ctx = isl_multi_pw_aff_get_ctx(test);
1393 if (expr->type != pet_expr_access)
1394 isl_die(ctx, isl_error_invalid,
1395 "can only filter access expressions", goto error);
1397 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1398 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1399 pma = pet_filter_insert_pma(space, id, satisfied);
1401 expr->acc.access = isl_map_preimage_domain_pw_multi_aff(
1402 expr->acc.access,
1403 isl_pw_multi_aff_copy(pma));
1404 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
1405 expr->acc.index, pma);
1406 if (!expr->acc.access || !expr->acc.index)
1407 goto error;
1409 expr = insert_access_arg(expr, test);
1411 isl_multi_pw_aff_free(test);
1412 return expr;
1413 error:
1414 isl_multi_pw_aff_free(test);
1415 return pet_expr_free(expr);
1418 /* Check if the given index expression accesses a (0D) array that corresponds
1419 * to one of the parameters in "space". If so, replace the array access
1420 * by an access to the set of integers with as index (and value)
1421 * that parameter.
1423 static __isl_give isl_multi_pw_aff *index_detect_parameter(
1424 __isl_take isl_multi_pw_aff *index, __isl_take isl_space *space)
1426 isl_local_space *ls;
1427 isl_id *array_id = NULL;
1428 isl_aff *aff;
1429 int pos = -1;
1431 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out)) {
1432 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1433 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
1435 isl_space_free(space);
1437 if (pos < 0) {
1438 isl_id_free(array_id);
1439 return index;
1442 space = isl_multi_pw_aff_get_domain_space(index);
1443 isl_multi_pw_aff_free(index);
1445 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
1446 if (pos < 0) {
1447 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
1448 space = isl_space_set_dim_id(space, isl_dim_param, 0, array_id);
1449 pos = 0;
1450 } else
1451 isl_id_free(array_id);
1453 ls = isl_local_space_from_space(space);
1454 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
1455 index = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1457 return index;
1460 /* Check if the given access relation accesses a (0D) array that corresponds
1461 * to one of the parameters in "space". If so, replace the array access
1462 * by an access to the set of integers with as index (and value)
1463 * that parameter.
1465 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
1466 __isl_take isl_space *space)
1468 isl_id *array_id = NULL;
1469 int pos = -1;
1471 if (isl_map_has_tuple_id(access, isl_dim_out)) {
1472 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1473 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
1475 isl_space_free(space);
1477 if (pos < 0) {
1478 isl_id_free(array_id);
1479 return access;
1482 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
1483 if (pos < 0) {
1484 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
1485 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
1486 pos = 0;
1487 } else
1488 isl_id_free(array_id);
1490 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1491 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
1493 return access;
1496 /* If "expr" accesses a (0D) array that corresponds to one of the parameters
1497 * in "space" then replace it by a value equal to the corresponding parameter.
1499 static __isl_give pet_expr *detect_parameter_accesses(__isl_take pet_expr *expr,
1500 void *user)
1502 isl_space *space = user;
1504 expr = pet_expr_cow(expr);
1505 if (!expr)
1506 return NULL;
1507 if (expr->type != pet_expr_access)
1508 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1509 "not an access expression", return pet_expr_free(expr));
1511 expr->acc.access = access_detect_parameter(expr->acc.access,
1512 isl_space_copy(space));
1513 expr->acc.index = index_detect_parameter(expr->acc.index,
1514 isl_space_copy(space));
1515 if (!expr->acc.access || !expr->acc.index)
1516 return pet_expr_free(expr);
1518 return expr;
1521 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1522 * in "space" by a value equal to the corresponding parameter.
1524 __isl_give pet_expr *pet_expr_detect_parameter_accesses(
1525 __isl_take pet_expr *expr, __isl_take isl_space *space)
1527 expr = pet_expr_map_access(expr, &detect_parameter_accesses, space);
1528 isl_space_free(space);
1529 return expr;
1532 /* Add a reference identifier to access expression "expr".
1533 * "user" points to an integer that contains the sequence number
1534 * of the next reference.
1536 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
1537 void *user)
1539 isl_ctx *ctx;
1540 char name[50];
1541 int *n_ref = user;
1543 expr = pet_expr_cow(expr);
1544 if (!expr)
1545 return expr;
1546 if (expr->type != pet_expr_access)
1547 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1548 "not an access expression", return pet_expr_free(expr));
1550 ctx = isl_map_get_ctx(expr->acc.access);
1551 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
1552 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
1553 if (!expr->acc.ref_id)
1554 return pet_expr_free(expr);
1556 return expr;
1559 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
1561 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
1564 /* Reset the user pointer on all parameter and tuple ids in
1565 * the access relation and the index expressions
1566 * of the access expression "expr".
1568 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
1569 void *user)
1571 expr = pet_expr_cow(expr);
1572 if (!expr)
1573 return expr;
1574 if (expr->type != pet_expr_access)
1575 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1576 "not an access expression", return pet_expr_free(expr));
1578 expr->acc.access = isl_map_reset_user(expr->acc.access);
1579 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
1580 if (!expr->acc.access || !expr->acc.index)
1581 return pet_expr_free(expr);
1583 return expr;
1586 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
1588 return pet_expr_map_access(expr, &access_anonymize, NULL);
1591 /* Data used in access_gist() callback.
1593 struct pet_access_gist_data {
1594 isl_set *domain;
1595 isl_union_map *value_bounds;
1598 /* Given an expression "expr" of type pet_expr_access, compute
1599 * the gist of the associated access relation and index expression
1600 * with respect to data->domain and the bounds on the values of the arguments
1601 * of the expression.
1603 * The arguments of "expr" have been gisted right before "expr" itself
1604 * is gisted. The gisted arguments may have become equal where before
1605 * they may not have been (obviously) equal. We therefore take
1606 * the opportunity to remove duplicate arguments here.
1608 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
1610 struct pet_access_gist_data *data = user;
1611 isl_set *domain;
1613 expr = pet_expr_remove_duplicate_args(expr);
1614 expr = pet_expr_cow(expr);
1615 if (!expr)
1616 return expr;
1617 if (expr->type != pet_expr_access)
1618 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1619 "not an access expression", return pet_expr_free(expr));
1621 domain = isl_set_copy(data->domain);
1622 if (expr->n_arg > 0)
1623 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
1624 data->value_bounds);
1626 expr->acc.access = isl_map_gist_domain(expr->acc.access,
1627 isl_set_copy(domain));
1628 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
1629 if (!expr->acc.access || !expr->acc.index)
1630 return pet_expr_free(expr);
1632 return expr;
1635 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
1636 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
1638 struct pet_access_gist_data data = { context, value_bounds };
1640 return pet_expr_map_access(expr, &access_gist, &data);
1643 /* Mark "expr" as a read dependening on "read".
1645 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
1646 int read)
1648 if (!expr)
1649 return pet_expr_free(expr);
1650 if (expr->type != pet_expr_access)
1651 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1652 "not an access expression", return pet_expr_free(expr));
1653 if (expr->acc.read == read)
1654 return expr;
1655 expr = pet_expr_cow(expr);
1656 if (!expr)
1657 return NULL;
1658 expr->acc.read = read;
1660 return expr;
1663 /* Mark "expr" as a write dependening on "write".
1665 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
1666 int write)
1668 if (!expr)
1669 return pet_expr_free(expr);
1670 if (expr->type != pet_expr_access)
1671 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1672 "not an access expression", return pet_expr_free(expr));
1673 if (expr->acc.write == write)
1674 return expr;
1675 expr = pet_expr_cow(expr);
1676 if (!expr)
1677 return NULL;
1678 expr->acc.write = write;
1680 return expr;
1683 /* Replace the access relation of "expr" by "access".
1685 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
1686 __isl_take isl_map *access)
1688 expr = pet_expr_cow(expr);
1689 if (!expr || !access)
1690 goto error;
1691 if (expr->type != pet_expr_access)
1692 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1693 "not an access expression", goto error);
1694 isl_map_free(expr->acc.access);
1695 expr->acc.access = access;
1697 return expr;
1698 error:
1699 isl_map_free(access);
1700 pet_expr_free(expr);
1701 return NULL;
1704 /* Replace the index expression of "expr" by "index".
1706 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
1707 __isl_take isl_multi_pw_aff *index)
1709 expr = pet_expr_cow(expr);
1710 if (!expr || !index)
1711 goto error;
1712 if (expr->type != pet_expr_access)
1713 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1714 "not an access expression", goto error);
1715 isl_multi_pw_aff_free(expr->acc.index);
1716 expr->acc.index = index;
1718 return expr;
1719 error:
1720 isl_multi_pw_aff_free(index);
1721 pet_expr_free(expr);
1722 return NULL;
1725 /* Return the reference identifier of access expression "expr".
1727 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
1729 if (!expr)
1730 return NULL;
1731 if (expr->type != pet_expr_access)
1732 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1733 "not an access expression", return NULL);
1735 return isl_id_copy(expr->acc.ref_id);
1738 /* Replace the reference identifier of access expression "expr" by "ref_id".
1740 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
1741 __isl_take isl_id *ref_id)
1743 expr = pet_expr_cow(expr);
1744 if (!expr || !ref_id)
1745 goto error;
1746 if (expr->type != pet_expr_access)
1747 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1748 "not an access expression", goto error);
1749 isl_id_free(expr->acc.ref_id);
1750 expr->acc.ref_id = ref_id;
1752 return expr;
1753 error:
1754 isl_id_free(ref_id);
1755 pet_expr_free(expr);
1756 return NULL;
1759 /* Tag the access relation "access" with "id".
1760 * That is, insert the id as the range of a wrapped relation
1761 * in the domain of "access".
1763 * If "access" is of the form
1765 * D[i] -> A[a]
1767 * then the result is of the form
1769 * [D[i] -> id[]] -> A[a]
1771 __isl_give isl_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
1772 __isl_take isl_map *access)
1774 isl_space *space;
1775 isl_map *add_tag;
1776 isl_id *id;
1778 if (expr->type != pet_expr_access)
1779 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1780 "not an access expression",
1781 return isl_map_free(access));
1783 id = isl_id_copy(expr->acc.ref_id);
1784 space = isl_space_range(isl_map_get_space(access));
1785 space = isl_space_from_range(space);
1786 space = isl_space_set_tuple_id(space, isl_dim_in, id);
1787 add_tag = isl_map_universe(space);
1788 access = isl_map_domain_product(access, add_tag);
1790 return access;
1793 /* Return the relation mapping pairs of domain iterations and argument
1794 * values to the corresponding accessed data elements.
1796 __isl_give isl_map *pet_expr_access_get_dependent_access(
1797 __isl_keep pet_expr *expr)
1799 if (!expr)
1800 return NULL;
1801 if (expr->type != pet_expr_access)
1802 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1803 "not an access expression", return NULL);
1805 return isl_map_copy(expr->acc.access);
1808 /* Return the relation mapping domain iterations to all possibly
1809 * accessed data elements.
1810 * In particular, take the access relation and project out the values
1811 * of the arguments, if any.
1813 __isl_give isl_map *pet_expr_access_get_may_access(__isl_keep pet_expr *expr)
1815 isl_map *access;
1816 isl_space *space;
1817 isl_map *map;
1819 if (!expr)
1820 return NULL;
1821 if (expr->type != pet_expr_access)
1822 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1823 "not an access expression", return NULL);
1825 access = pet_expr_access_get_dependent_access(expr);
1826 if (expr->n_arg == 0)
1827 return access;
1829 space = isl_space_domain(isl_map_get_space(access));
1830 map = isl_map_universe(isl_space_unwrap(space));
1831 map = isl_map_domain_map(map);
1832 access = isl_map_apply_domain(access, map);
1834 return access;
1837 /* Return a relation mapping domain iterations to definitely
1838 * accessed data elements, assuming the statement containing
1839 * the expression is executed.
1841 * If there are no arguments, then all elements are accessed.
1842 * Otherwise, we conservatively return an empty relation.
1844 __isl_give isl_map *pet_expr_access_get_must_access(__isl_keep pet_expr *expr)
1846 isl_space *space;
1848 if (!expr)
1849 return NULL;
1850 if (expr->type != pet_expr_access)
1851 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1852 "not an access expression", return NULL);
1854 if (expr->n_arg == 0)
1855 return pet_expr_access_get_dependent_access(expr);
1857 space = isl_map_get_space(expr->acc.access);
1858 space = isl_space_domain_factor_domain(space);
1860 return isl_map_empty(space);
1863 /* Return the relation mapping domain iterations to all possibly
1864 * accessed data elements, with its domain tagged with the reference
1865 * identifier.
1867 __isl_give isl_map *pet_expr_access_get_tagged_may_access(
1868 __isl_keep pet_expr *expr)
1870 isl_map *access;
1872 if (!expr)
1873 return NULL;
1875 access = pet_expr_access_get_may_access(expr);
1876 access = pet_expr_tag_access(expr, access);
1878 return access;
1881 /* Return the operation type of operation expression "expr".
1883 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
1885 if (!expr)
1886 return pet_op_last;
1887 if (expr->type != pet_expr_op)
1888 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1889 "not an operation expression", return pet_op_last);
1891 return expr->op;
1894 /* Replace the operation type of operation expression "expr" by "type".
1896 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
1897 enum pet_op_type type)
1899 if (!expr)
1900 return pet_expr_free(expr);
1901 if (expr->type != pet_expr_op)
1902 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1903 "not an operation expression",
1904 return pet_expr_free(expr));
1905 if (expr->op == type)
1906 return expr;
1907 expr = pet_expr_cow(expr);
1908 if (!expr)
1909 return NULL;
1910 expr->op = type;
1912 return expr;
1915 /* Return the name of the function called by "expr".
1917 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
1919 if (!expr)
1920 return NULL;
1921 if (expr->type != pet_expr_call)
1922 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1923 "not a call expression", return NULL);
1924 return expr->name;
1927 /* Replace the name of the function called by "expr" by "name".
1929 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
1930 __isl_keep const char *name)
1932 expr = pet_expr_cow(expr);
1933 if (!expr || !name)
1934 return pet_expr_free(expr);
1935 if (expr->type != pet_expr_call)
1936 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1937 "not a call expression", return pet_expr_free(expr));
1938 free(expr->name);
1939 expr->name = strdup(name);
1940 if (!expr->name)
1941 return pet_expr_free(expr);
1942 return expr;
1945 /* Replace the type of the cast performed by "expr" by "name".
1947 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
1948 __isl_keep const char *name)
1950 expr = pet_expr_cow(expr);
1951 if (!expr || !name)
1952 return pet_expr_free(expr);
1953 if (expr->type != pet_expr_cast)
1954 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1955 "not a cast expression", return pet_expr_free(expr));
1956 free(expr->type_name);
1957 expr->type_name = strdup(name);
1958 if (!expr->type_name)
1959 return pet_expr_free(expr);
1960 return expr;
1963 /* Return the value of the integer represented by "expr".
1965 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
1967 if (!expr)
1968 return NULL;
1969 if (expr->type != pet_expr_int)
1970 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1971 "not an int expression", return NULL);
1973 return isl_val_copy(expr->i);
1976 /* Replace the value of the integer represented by "expr" by "v".
1978 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
1979 __isl_take isl_val *v)
1981 expr = pet_expr_cow(expr);
1982 if (!expr || !v)
1983 goto error;
1984 if (expr->type != pet_expr_int)
1985 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1986 "not an int expression", goto error);
1987 isl_val_free(expr->i);
1988 expr->i = v;
1990 return expr;
1991 error:
1992 isl_val_free(v);
1993 pet_expr_free(expr);
1994 return NULL;
1997 /* Replace the value and string representation of the double
1998 * represented by "expr" by "d" and "s".
2000 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2001 double d, __isl_keep const char *s)
2003 expr = pet_expr_cow(expr);
2004 if (!expr || !s)
2005 return pet_expr_free(expr);
2006 if (expr->type != pet_expr_double)
2007 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2008 "not a double expression", return pet_expr_free(expr));
2009 expr->d.val = d;
2010 free(expr->d.s);
2011 expr->d.s = strdup(s);
2012 if (!expr->d.s)
2013 return pet_expr_free(expr);
2014 return expr;
2017 /* Return a string representation of the double expression "expr".
2019 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2021 if (!expr)
2022 return NULL;
2023 if (expr->type != pet_expr_double)
2024 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2025 "not a double expression", return NULL);
2026 return strdup(expr->d.s);
2029 /* Return a piecewise affine expression defined on the specified domain
2030 * that represents NaN.
2032 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2034 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2037 /* This function is called when we come across an access that is
2038 * nested in what is supposed to be an affine expression.
2039 * "pc" is the context in which the affine expression is created.
2040 * If nesting is allowed in "pc", we return an affine expression that is
2041 * equal to a new parameter corresponding to this nested access.
2042 * Otherwise, we return NaN.
2044 * Note that we currently don't allow nested accesses themselves
2045 * to contain any nested accesses, so we check if "expr" itself
2046 * involves any nested accesses (either explicitly as arguments
2047 * or implicitly through parameters) and return NaN if it does.
2049 * The new parameter is resolved in resolve_nested.
2051 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2052 __isl_keep pet_context *pc)
2054 isl_ctx *ctx;
2055 isl_id *id;
2056 isl_space *space;
2057 isl_local_space *ls;
2058 isl_aff *aff;
2059 int nested;
2061 if (!expr || !pc)
2062 return NULL;
2063 if (!pet_context_allow_nesting(pc))
2064 return non_affine(pet_context_get_space(pc));
2066 if (pet_expr_get_type(expr) != pet_expr_access)
2067 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2068 "not an access expression", return NULL);
2070 if (expr->n_arg > 0)
2071 return non_affine(pet_context_get_space(pc));
2073 space = pet_expr_access_get_parameter_space(expr);
2074 nested = pet_nested_any_in_space(space);
2075 isl_space_free(space);
2076 if (nested)
2077 return non_affine(pet_context_get_space(pc));
2079 ctx = pet_expr_get_ctx(expr);
2080 id = pet_nested_pet_expr(pet_expr_copy(expr));
2081 space = pet_context_get_space(pc);
2082 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2084 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2085 ls = isl_local_space_from_space(space);
2086 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2088 return isl_pw_aff_from_aff(aff);
2091 /* Extract an affine expression from the access pet_expr "expr".
2092 * "pc" is the context in which the affine expression is created.
2094 * If "expr" is actually an affine expression rather than
2095 * a real access, then we return that expression.
2096 * Otherwise, we require that "expr" is of an integral type.
2097 * If not, we return NaN.
2099 * If we are accessing a scalar (i.e., not an array and not a member)
2100 * and if that scalar can be treated as a parameter (because it is
2101 * not assigned a known or unknown value in the relevant part of the AST),
2102 * then we return an affine expression equal to that parameter.
2104 * If the variable has been assigned a known affine expression,
2105 * then we return that expression.
2107 * Otherwise, we return an expression that is equal to a parameter
2108 * representing "expr" (if "allow_nested" is set).
2110 static __isl_give isl_pw_aff *extract_affine_from_access(
2111 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2113 int pos;
2114 isl_id *id;
2115 isl_space *space;
2116 isl_local_space *ls;
2117 isl_aff *aff;
2119 if (pet_expr_is_affine(expr)) {
2120 isl_pw_aff *pa;
2121 isl_multi_pw_aff *mpa;
2123 mpa = pet_expr_access_get_index(expr);
2124 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
2125 isl_multi_pw_aff_free(mpa);
2126 return pa;
2129 if (pet_expr_get_type_size(expr) == 0)
2130 return non_affine(pet_context_get_space(pc));
2132 if (!pet_expr_is_scalar_access(expr))
2133 return nested_access(expr, pc);
2135 id = pet_expr_access_get_id(expr);
2136 if (pet_context_is_assigned(pc, id)) {
2137 isl_pw_aff *pa;
2139 pa = pet_context_get_value(pc, id);
2140 if (!pa)
2141 return NULL;
2142 if (!isl_pw_aff_involves_nan(pa))
2143 return pa;
2144 isl_pw_aff_free(pa);
2145 return nested_access(expr, pc);
2148 space = pet_context_get_space(pc);
2150 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
2151 if (pos >= 0) {
2152 isl_id_free(id);
2153 } else {
2154 pos = isl_space_dim(space, isl_dim_param);
2155 space = isl_space_add_dims(space, isl_dim_param, 1);
2156 space = isl_space_set_dim_id(space, isl_dim_param, pos, id);
2159 ls = isl_local_space_from_space(space);
2160 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
2162 return isl_pw_aff_from_aff(aff);
2165 /* Construct an affine expression from the integer constant "expr".
2166 * "pc" is the context in which the affine expression is created.
2168 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2169 __isl_keep pet_context *pc)
2171 isl_local_space *ls;
2172 isl_aff *aff;
2174 if (!expr)
2175 return NULL;
2177 ls = isl_local_space_from_space(pet_context_get_space(pc));
2178 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2180 return isl_pw_aff_from_aff(aff);
2183 /* Extract an affine expression from an addition or subtraction operation.
2184 * Return NaN if we are unable to extract an affine expression.
2186 * "pc" is the context in which the affine expression is created.
2188 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2189 __isl_keep pet_context *pc)
2191 isl_pw_aff *lhs;
2192 isl_pw_aff *rhs;
2194 if (!expr)
2195 return NULL;
2196 if (expr->n_arg != 2)
2197 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2198 "expecting two arguments", return NULL);
2200 lhs = pet_expr_extract_affine(expr->args[0], pc);
2201 rhs = pet_expr_extract_affine(expr->args[1], pc);
2203 switch (pet_expr_op_get_type(expr)) {
2204 case pet_op_add:
2205 return isl_pw_aff_add(lhs, rhs);
2206 case pet_op_sub:
2207 return isl_pw_aff_sub(lhs, rhs);
2208 default:
2209 isl_pw_aff_free(lhs);
2210 isl_pw_aff_free(rhs);
2211 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2212 "not an addition or subtraction operation",
2213 return NULL);
2218 /* Extract an affine expression from an integer division or a modulo operation.
2219 * Return NaN if we are unable to extract an affine expression.
2221 * "pc" is the context in which the affine expression is created.
2223 * In particular, if "expr" is lhs/rhs, then return
2225 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2227 * If "expr" is lhs%rhs, then return
2229 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2231 * If the second argument (rhs) is not a (positive) integer constant,
2232 * then we fail to extract an affine expression.
2234 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2235 __isl_keep pet_context *pc)
2237 int is_cst;
2238 isl_pw_aff *lhs;
2239 isl_pw_aff *rhs;
2241 if (!expr)
2242 return NULL;
2243 if (expr->n_arg != 2)
2244 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2245 "expecting two arguments", return NULL);
2247 rhs = pet_expr_extract_affine(expr->args[1], pc);
2249 is_cst = isl_pw_aff_is_cst(rhs);
2250 if (is_cst < 0 || !is_cst) {
2251 isl_pw_aff_free(rhs);
2252 return non_affine(pet_context_get_space(pc));
2255 lhs = pet_expr_extract_affine(expr->args[0], pc);
2257 switch (pet_expr_op_get_type(expr)) {
2258 case pet_op_div:
2259 return isl_pw_aff_tdiv_q(lhs, rhs);
2260 case pet_op_mod:
2261 return isl_pw_aff_tdiv_r(lhs, rhs);
2262 default:
2263 isl_pw_aff_free(lhs);
2264 isl_pw_aff_free(rhs);
2265 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2266 "not a div or mod operator", return NULL);
2271 /* Extract an affine expression from a multiplication operation.
2272 * Return NaN if we are unable to extract an affine expression.
2273 * In particular, if neither of the arguments is a (piecewise) constant
2274 * then we return NaN.
2276 * "pc" is the context in which the affine expression is created.
2278 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2279 __isl_keep pet_context *pc)
2281 int lhs_cst, rhs_cst;
2282 isl_pw_aff *lhs;
2283 isl_pw_aff *rhs;
2285 if (!expr)
2286 return NULL;
2287 if (expr->n_arg != 2)
2288 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2289 "expecting two arguments", return NULL);
2291 lhs = pet_expr_extract_affine(expr->args[0], pc);
2292 rhs = pet_expr_extract_affine(expr->args[1], pc);
2294 lhs_cst = isl_pw_aff_is_cst(lhs);
2295 rhs_cst = isl_pw_aff_is_cst(rhs);
2296 if (lhs_cst < 0 || rhs_cst < 0 || (!lhs_cst && !rhs_cst)) {
2297 isl_pw_aff_free(lhs);
2298 isl_pw_aff_free(rhs);
2299 return non_affine(pet_context_get_space(pc));
2302 return isl_pw_aff_mul(lhs, rhs);
2305 /* Extract an affine expression from a negation operation.
2306 * Return NaN if we are unable to extract an affine expression.
2308 * "pc" is the context in which the affine expression is created.
2310 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
2311 __isl_keep pet_context *pc)
2313 isl_pw_aff *res;
2315 if (!expr)
2316 return NULL;
2317 if (expr->n_arg != 1)
2318 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2319 "expecting one argument", return NULL);
2321 res = pet_expr_extract_affine(expr->args[0], pc);
2322 return isl_pw_aff_neg(res);
2325 /* Extract an affine expression from a conditional operation.
2326 * Return NaN if we are unable to extract an affine expression.
2328 * "pc" is the context in which the affine expression is created.
2330 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
2331 __isl_keep pet_context *pc)
2333 isl_pw_aff *cond, *lhs, *rhs;
2335 if (!expr)
2336 return NULL;
2337 if (expr->n_arg != 3)
2338 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2339 "expecting three arguments", return NULL);
2341 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2342 lhs = pet_expr_extract_affine(expr->args[1], pc);
2343 rhs = pet_expr_extract_affine(expr->args[2], pc);
2345 return isl_pw_aff_cond(cond, lhs, rhs);
2348 /* Compute
2350 * pwaff mod 2^width
2352 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff, unsigned width)
2354 isl_ctx *ctx;
2355 isl_val *mod;
2357 ctx = isl_pw_aff_get_ctx(pwaff);
2358 mod = isl_val_int_from_ui(ctx, width);
2359 mod = isl_val_2exp(mod);
2361 pwaff = isl_pw_aff_mod_val(pwaff, mod);
2363 return pwaff;
2366 /* Limit the domain of "pwaff" to those elements where the function
2367 * value satisfies
2369 * 2^{width-1} <= pwaff < 2^{width-1}
2371 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
2372 unsigned width)
2374 isl_ctx *ctx;
2375 isl_val *v;
2376 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
2377 isl_local_space *ls = isl_local_space_from_space(space);
2378 isl_aff *bound;
2379 isl_set *dom;
2380 isl_pw_aff *b;
2382 ctx = isl_pw_aff_get_ctx(pwaff);
2383 v = isl_val_int_from_ui(ctx, width - 1);
2384 v = isl_val_2exp(v);
2386 bound = isl_aff_zero_on_domain(ls);
2387 bound = isl_aff_add_constant_val(bound, v);
2388 b = isl_pw_aff_from_aff(bound);
2390 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
2391 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2393 b = isl_pw_aff_neg(b);
2394 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
2395 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2397 return pwaff;
2400 /* Handle potential overflows on signed computations.
2402 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2403 * then we adjust the domain of "pa" to avoid overflows.
2405 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
2406 unsigned width)
2408 isl_ctx *ctx;
2409 struct pet_options *options;
2411 if (!pa)
2412 return NULL;
2414 ctx = isl_pw_aff_get_ctx(pa);
2415 options = isl_ctx_peek_pet_options(ctx);
2416 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
2417 pa = avoid_overflow(pa, width);
2419 return pa;
2422 /* Extract an affine expression from some an operation.
2423 * Return NaN if we are unable to extract an affine expression.
2424 * If the result of a binary (non boolean) operation is unsigned,
2425 * then we wrap it based on the size of the type. If the result is signed,
2426 * then we ensure that no overflow occurs.
2428 * "pc" is the context in which the affine expression is created.
2430 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
2431 __isl_keep pet_context *pc)
2433 isl_pw_aff *res;
2434 int type_size;
2436 switch (pet_expr_op_get_type(expr)) {
2437 case pet_op_add:
2438 case pet_op_sub:
2439 res = extract_affine_add_sub(expr, pc);
2440 break;
2441 case pet_op_div:
2442 case pet_op_mod:
2443 res = extract_affine_div_mod(expr, pc);
2444 break;
2445 case pet_op_mul:
2446 res = extract_affine_mul(expr, pc);
2447 break;
2448 case pet_op_minus:
2449 return extract_affine_neg(expr, pc);
2450 case pet_op_cond:
2451 return extract_affine_cond(expr, pc);
2452 case pet_op_eq:
2453 case pet_op_ne:
2454 case pet_op_le:
2455 case pet_op_ge:
2456 case pet_op_lt:
2457 case pet_op_gt:
2458 case pet_op_land:
2459 case pet_op_lor:
2460 case pet_op_lnot:
2461 return pet_expr_extract_affine_condition(expr, pc);
2462 default:
2463 return non_affine(pet_context_get_space(pc));
2466 if (!res)
2467 return NULL;
2468 if (isl_pw_aff_involves_nan(res)) {
2469 isl_space *space = isl_pw_aff_get_domain_space(res);
2470 isl_pw_aff_free(res);
2471 return non_affine(space);
2474 type_size = pet_expr_get_type_size(expr);
2475 if (type_size > 0)
2476 res = wrap(res, type_size);
2477 else
2478 res = signed_overflow(res, -type_size);
2480 return res;
2483 /* Extract an affine expression from some special function calls.
2484 * Return NaN if we are unable to extract an affine expression.
2485 * In particular, we handle "min", "max", "ceild", "floord",
2486 * "intMod", "intFloor" and "intCeil".
2487 * In case of the latter five, the second argument needs to be
2488 * a (positive) integer constant.
2490 * "pc" is the context in which the affine expression is created.
2492 static __isl_give isl_pw_aff *extract_affine_from_call(
2493 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2495 isl_pw_aff *aff1, *aff2;
2496 int n;
2497 const char *name;
2499 n = pet_expr_get_n_arg(expr);
2500 name = pet_expr_call_get_name(expr);
2501 if (!(n == 2 && !strcmp(name, "min")) &&
2502 !(n == 2 && !strcmp(name, "max")) &&
2503 !(n == 2 && !strcmp(name, "intMod")) &&
2504 !(n == 2 && !strcmp(name, "intFloor")) &&
2505 !(n == 2 && !strcmp(name, "intCeil")) &&
2506 !(n == 2 && !strcmp(name, "floord")) &&
2507 !(n == 2 && !strcmp(name, "ceild")))
2508 return non_affine(pet_context_get_space(pc));
2510 if (!strcmp(name, "min") || !strcmp(name, "max")) {
2511 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2512 aff2 = pet_expr_extract_affine(expr->args[1], pc);
2514 if (!strcmp(name, "min"))
2515 aff1 = isl_pw_aff_min(aff1, aff2);
2516 else
2517 aff1 = isl_pw_aff_max(aff1, aff2);
2518 } else if (!strcmp(name, "intMod")) {
2519 isl_val *v;
2521 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2522 return non_affine(pet_context_get_space(pc));
2523 v = pet_expr_int_get_val(expr->args[1]);
2524 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2525 aff1 = isl_pw_aff_mod_val(aff1, v);
2526 } else {
2527 isl_val *v;
2529 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2530 return non_affine(pet_context_get_space(pc));
2531 v = pet_expr_int_get_val(expr->args[1]);
2532 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2533 aff1 = isl_pw_aff_scale_down_val(aff1, v);
2534 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
2535 aff1 = isl_pw_aff_floor(aff1);
2536 else
2537 aff1 = isl_pw_aff_ceil(aff1);
2540 return aff1;
2543 /* Extract an affine expression from "expr", if possible.
2544 * Otherwise return NaN.
2546 * "pc" is the context in which the affine expression is created.
2548 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
2549 __isl_keep pet_context *pc)
2551 if (!expr)
2552 return NULL;
2554 switch (pet_expr_get_type(expr)) {
2555 case pet_expr_access:
2556 return extract_affine_from_access(expr, pc);
2557 case pet_expr_int:
2558 return extract_affine_from_int(expr, pc);
2559 case pet_expr_op:
2560 return extract_affine_from_op(expr, pc);
2561 case pet_expr_call:
2562 return extract_affine_from_call(expr, pc);
2563 case pet_expr_cast:
2564 case pet_expr_double:
2565 case pet_expr_error:
2566 return non_affine(pet_context_get_space(pc));
2570 /* Extract an affine expressions representing the comparison "LHS op RHS"
2571 * Return NaN if we are unable to extract such an affine expression.
2573 * "pc" is the context in which the affine expression is created.
2575 * If the comparison is of the form
2577 * a <= min(b,c)
2579 * then the expression is constructed as the conjunction of
2580 * the comparisons
2582 * a <= b and a <= c
2584 * A similar optimization is performed for max(a,b) <= c.
2585 * We do this because that will lead to simpler representations
2586 * of the expression.
2587 * If isl is ever enhanced to explicitly deal with min and max expressions,
2588 * this optimization can be removed.
2590 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
2591 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
2592 __isl_keep pet_context *pc)
2594 isl_pw_aff *lhs_pa, *rhs_pa;
2596 if (op == pet_op_gt)
2597 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
2598 if (op == pet_op_ge)
2599 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
2601 if (op == pet_op_lt || op == pet_op_le) {
2602 if (pet_expr_is_min(rhs)) {
2603 lhs_pa = pet_expr_extract_comparison(op, lhs,
2604 rhs->args[0], pc);
2605 rhs_pa = pet_expr_extract_comparison(op, lhs,
2606 rhs->args[1], pc);
2607 return pet_and(lhs_pa, rhs_pa);
2609 if (pet_expr_is_max(lhs)) {
2610 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
2611 rhs, pc);
2612 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
2613 rhs, pc);
2614 return pet_and(lhs_pa, rhs_pa);
2618 lhs_pa = pet_expr_extract_affine(lhs, pc);
2619 rhs_pa = pet_expr_extract_affine(rhs, pc);
2621 return pet_comparison(op, lhs_pa, rhs_pa);
2624 /* Extract an affine expressions from the comparison "expr".
2625 * Return NaN if we are unable to extract such an affine expression.
2627 * "pc" is the context in which the affine expression is created.
2629 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
2630 __isl_keep pet_context *pc)
2632 enum pet_op_type type;
2634 if (!expr)
2635 return NULL;
2636 if (expr->n_arg != 2)
2637 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2638 "expecting two arguments", return NULL);
2640 type = pet_expr_op_get_type(expr);
2641 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
2642 pc);
2645 /* Extract an affine expression representing the boolean operation
2646 * expressed by "expr".
2647 * Return NaN if we are unable to extract an affine expression.
2649 * "pc" is the context in which the affine expression is created.
2651 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
2652 __isl_keep pet_context *pc)
2654 isl_pw_aff *lhs, *rhs;
2655 int n;
2657 if (!expr)
2658 return NULL;
2660 n = pet_expr_get_n_arg(expr);
2661 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
2662 if (n == 1)
2663 return pet_not(lhs);
2665 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
2666 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
2669 /* Extract the affine expression "expr != 0 ? 1 : 0".
2670 * Return NaN if we are unable to extract an affine expression.
2672 * "pc" is the context in which the affine expression is created.
2674 static __isl_give isl_pw_aff *extract_implicit_condition(
2675 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2677 isl_pw_aff *res;
2679 res = pet_expr_extract_affine(expr, pc);
2680 return pet_to_bool(res);
2683 /* Extract a boolean affine expression from "expr".
2684 * Return NaN if we are unable to extract an affine expression.
2686 * "pc" is the context in which the affine expression is created.
2688 * If "expr" is neither a comparison nor a boolean operation,
2689 * then we assume it is an affine expression and return the
2690 * boolean expression "expr != 0 ? 1 : 0".
2692 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
2693 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2695 if (!expr)
2696 return NULL;
2698 if (pet_expr_is_comparison(expr))
2699 return extract_comparison(expr, pc);
2700 if (pet_expr_is_boolean(expr))
2701 return extract_boolean(expr, pc);
2703 return extract_implicit_condition(expr, pc);
2706 /* Return the number of bits needed to represent the type of "expr".
2707 * See the description of the type_size field of pet_expr.
2709 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
2711 return expr ? expr->type_size : 0;
2714 /* Replace the number of bits needed to represent the type of "expr"
2715 * by "type_size".
2716 * See the description of the type_size field of pet_expr.
2718 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
2719 int type_size)
2721 expr = pet_expr_cow(expr);
2722 if (!expr)
2723 return NULL;
2725 expr->type_size = type_size;
2727 return expr;
2730 /* Extend an access expression "expr" with an additional index "index".
2731 * In particular, add "index" as an extra argument to "expr" and
2732 * adjust the index expression of "expr" to refer to this extra argument.
2733 * The caller is responsible for calling pet_expr_access_set_depth
2734 * to update the corresponding access relation.
2736 * Note that we only collect the individual index expressions as
2737 * arguments of "expr" here.
2738 * An attempt to integrate them into the index expression of "expr"
2739 * is performed in pet_expr_access_plug_in_args.
2741 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
2742 __isl_take pet_expr *index)
2744 int n;
2745 isl_space *space;
2746 isl_local_space *ls;
2747 isl_pw_aff *pa;
2749 expr = pet_expr_cow(expr);
2750 if (!expr || !index)
2751 goto error;
2752 if (expr->type != pet_expr_access)
2753 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2754 "not an access pet_expr", goto error);
2756 n = pet_expr_get_n_arg(expr);
2757 expr = pet_expr_insert_arg(expr, n, index);
2758 if (!expr)
2759 return NULL;
2761 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
2762 ls = isl_local_space_from_space(space);
2763 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
2764 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
2765 if (!expr->acc.index)
2766 return pet_expr_free(expr);
2768 return expr;
2769 error:
2770 pet_expr_free(expr);
2771 pet_expr_free(index);
2772 return NULL;
2775 /* Extend an access expression "expr" with an additional member acces to "id".
2776 * In particular, extend the index expression of "expr" to include
2777 * the additional member access.
2778 * The caller is responsible for calling pet_expr_access_set_depth
2779 * to update the corresponding access relation.
2781 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
2782 __isl_take isl_id *id)
2784 isl_space *space;
2785 isl_multi_pw_aff *field_access;
2787 expr = pet_expr_cow(expr);
2788 if (!expr || !id)
2789 goto error;
2790 if (expr->type != pet_expr_access)
2791 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2792 "not an access pet_expr", goto error);
2794 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
2795 space = isl_space_from_domain(space);
2796 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2797 field_access = isl_multi_pw_aff_zero(space);
2798 expr->acc.index = pet_array_member(expr->acc.index, field_access);
2799 if (!expr->acc.index)
2800 return pet_expr_free(expr);
2802 return expr;
2803 error:
2804 pet_expr_free(expr);
2805 isl_id_free(id);
2806 return NULL;
2809 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
2811 int i;
2813 if (!expr)
2814 return;
2816 fprintf(stderr, "%*s", indent, "");
2818 switch (expr->type) {
2819 case pet_expr_double:
2820 fprintf(stderr, "%s\n", expr->d.s);
2821 break;
2822 case pet_expr_int:
2823 isl_val_dump(expr->i);
2824 break;
2825 case pet_expr_access:
2826 if (expr->acc.ref_id) {
2827 isl_id_dump(expr->acc.ref_id);
2828 fprintf(stderr, "%*s", indent, "");
2830 isl_map_dump(expr->acc.access);
2831 fprintf(stderr, "%*s", indent, "");
2832 isl_multi_pw_aff_dump(expr->acc.index);
2833 fprintf(stderr, "%*sread: %d\n", indent + 2,
2834 "", expr->acc.read);
2835 fprintf(stderr, "%*swrite: %d\n", indent + 2,
2836 "", expr->acc.write);
2837 for (i = 0; i < expr->n_arg; ++i)
2838 pet_expr_dump_with_indent(expr->args[i], indent + 2);
2839 break;
2840 case pet_expr_op:
2841 fprintf(stderr, "%s\n", op_str[expr->op]);
2842 for (i = 0; i < expr->n_arg; ++i)
2843 pet_expr_dump_with_indent(expr->args[i], indent + 2);
2844 break;
2845 case pet_expr_call:
2846 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
2847 for (i = 0; i < expr->n_arg; ++i)
2848 pet_expr_dump_with_indent(expr->args[i], indent + 2);
2849 break;
2850 case pet_expr_cast:
2851 fprintf(stderr, "(%s)\n", expr->type_name);
2852 for (i = 0; i < expr->n_arg; ++i)
2853 pet_expr_dump_with_indent(expr->args[i], indent + 2);
2854 break;
2855 case pet_expr_error:
2856 fprintf(stderr, "ERROR\n");
2857 break;
2861 void pet_expr_dump(__isl_keep pet_expr *expr)
2863 pet_expr_dump_with_indent(expr, 0);