add functions for manipulating the domain of a pet_context
[pet.git] / expr.c
blob96bfc4cfe73368bfd3e426dfac76dba4ed0c8507
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 /* Is "expr" an assume statement?
710 int pet_expr_is_assume(__isl_keep pet_expr *expr)
712 if (!expr)
713 return -1;
714 if (expr->type != pet_expr_op)
715 return 0;
716 return expr->op == pet_op_assume;
719 /* Does "expr" perform a min operation?
721 int pet_expr_is_min(__isl_keep pet_expr *expr)
723 if (!expr)
724 return -1;
725 if (expr->type != pet_expr_call)
726 return 0;
727 if (expr->n_arg != 2)
728 return 0;
729 if (strcmp(expr->name, "min") != 0)
730 return 0;
731 return 1;
734 /* Does "expr" perform a max operation?
736 int pet_expr_is_max(__isl_keep pet_expr *expr)
738 if (!expr)
739 return -1;
740 if (expr->type != pet_expr_call)
741 return 0;
742 if (expr->n_arg != 2)
743 return 0;
744 if (strcmp(expr->name, "max") != 0)
745 return 0;
746 return 1;
749 /* Does "expr" represent an access to an unnamed space, i.e.,
750 * does it represent an affine expression?
752 int pet_expr_is_affine(__isl_keep pet_expr *expr)
754 int has_id;
756 if (!expr)
757 return -1;
758 if (expr->type != pet_expr_access)
759 return 0;
761 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
762 if (has_id < 0)
763 return -1;
765 return !has_id;
768 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
769 * not part of any struct?
771 int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
773 if (!expr)
774 return -1;
775 if (expr->type != pet_expr_access)
776 return 0;
777 if (isl_map_range_is_wrapping(expr->acc.access))
778 return 0;
780 return isl_map_dim(expr->acc.access, isl_dim_out) == 0;
783 /* Return 1 if the two pet_exprs are equivalent.
785 int pet_expr_is_equal(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2)
787 int i;
789 if (!expr1 || !expr2)
790 return 0;
792 if (expr1->type != expr2->type)
793 return 0;
794 if (expr1->n_arg != expr2->n_arg)
795 return 0;
796 for (i = 0; i < expr1->n_arg; ++i)
797 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
798 return 0;
799 switch (expr1->type) {
800 case pet_expr_error:
801 return -1;
802 case pet_expr_double:
803 if (strcmp(expr1->d.s, expr2->d.s))
804 return 0;
805 if (expr1->d.val != expr2->d.val)
806 return 0;
807 break;
808 case pet_expr_int:
809 if (!isl_val_eq(expr1->i, expr2->i))
810 return 0;
811 break;
812 case pet_expr_access:
813 if (expr1->acc.read != expr2->acc.read)
814 return 0;
815 if (expr1->acc.write != expr2->acc.write)
816 return 0;
817 if (expr1->acc.ref_id != expr2->acc.ref_id)
818 return 0;
819 if (!expr1->acc.access || !expr2->acc.access)
820 return 0;
821 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
822 return 0;
823 if (!expr1->acc.index || !expr2->acc.index)
824 return 0;
825 if (!isl_multi_pw_aff_plain_is_equal(expr1->acc.index,
826 expr2->acc.index))
827 return 0;
828 break;
829 case pet_expr_op:
830 if (expr1->op != expr2->op)
831 return 0;
832 break;
833 case pet_expr_call:
834 if (strcmp(expr1->name, expr2->name))
835 return 0;
836 break;
837 case pet_expr_cast:
838 if (strcmp(expr1->type_name, expr2->type_name))
839 return 0;
840 break;
843 return 1;
846 /* Does the access expression "expr" read the accessed elements?
848 int pet_expr_access_is_read(__isl_keep pet_expr *expr)
850 if (!expr)
851 return -1;
852 if (expr->type != pet_expr_access)
853 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
854 "not an access expression", return -1);
856 return expr->acc.read;
859 /* Does the access expression "expr" write to the accessed elements?
861 int pet_expr_access_is_write(__isl_keep pet_expr *expr)
863 if (!expr)
864 return -1;
865 if (expr->type != pet_expr_access)
866 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
867 "not an access expression", return -1);
869 return expr->acc.write;
872 /* Return the identifier of the array accessed by "expr".
874 * If "expr" represents a member access, then return the identifier
875 * of the outer structure array.
877 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
879 if (!expr)
880 return NULL;
881 if (expr->type != pet_expr_access)
882 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
883 "not an access expression", return NULL);
885 if (isl_map_range_is_wrapping(expr->acc.access)) {
886 isl_space *space;
887 isl_id *id;
889 space = isl_map_get_space(expr->acc.access);
890 space = isl_space_range(space);
891 while (space && isl_space_is_wrapping(space))
892 space = isl_space_domain(isl_space_unwrap(space));
893 id = isl_space_get_tuple_id(space, isl_dim_set);
894 isl_space_free(space);
896 return id;
899 return isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
902 /* Return the parameter space of "expr".
904 __isl_give isl_space *pet_expr_access_get_parameter_space(
905 __isl_keep pet_expr *expr)
907 isl_space *space;
909 if (!expr)
910 return NULL;
911 if (expr->type != pet_expr_access)
912 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
913 "not an access expression", return NULL);
915 space = isl_multi_pw_aff_get_space(expr->acc.index);
916 space = isl_space_params(space);
918 return space;
921 /* Return the domain space of "expr", without the arguments (if any).
923 __isl_give isl_space *pet_expr_access_get_domain_space(
924 __isl_keep pet_expr *expr)
926 isl_space *space;
928 if (!expr)
929 return NULL;
930 if (expr->type != pet_expr_access)
931 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
932 "not an access expression", return NULL);
934 space = isl_multi_pw_aff_get_space(expr->acc.index);
935 space = isl_space_domain(space);
936 if (isl_space_is_wrapping(space))
937 space = isl_space_domain(isl_space_unwrap(space));
939 return space;
942 /* Return the space of the data accessed by "expr".
944 __isl_give isl_space *pet_expr_access_get_data_space(__isl_keep pet_expr *expr)
946 isl_space *space;
948 if (!expr)
949 return NULL;
950 if (expr->type != pet_expr_access)
951 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
952 "not an access expression", return NULL);
954 space = isl_multi_pw_aff_get_space(expr->acc.index);
955 space = isl_space_range(space);
957 return space;
960 /* Modify all expressions of type pet_expr_access in "expr"
961 * by calling "fn" on them.
963 __isl_give pet_expr *pet_expr_map_access(__isl_take pet_expr *expr,
964 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
965 void *user)
967 int i, n;
969 n = pet_expr_get_n_arg(expr);
970 for (i = 0; i < n; ++i) {
971 pet_expr *arg = pet_expr_get_arg(expr, i);
972 arg = pet_expr_map_access(arg, fn, user);
973 expr = pet_expr_set_arg(expr, i, arg);
976 if (!expr)
977 return NULL;
979 if (expr->type == pet_expr_access)
980 expr = fn(expr, user);
982 return expr;
985 /* Call "fn" on each of the subexpressions of "expr" of type "type".
987 * Return -1 on error (where fn returning a negative value is treated as
988 * an error).
989 * Otherwise return 0.
991 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
992 enum pet_expr_type type,
993 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
995 int i;
997 if (!expr)
998 return -1;
1000 for (i = 0; i < expr->n_arg; ++i)
1001 if (pet_expr_foreach_expr_of_type(expr->args[i],
1002 type, fn, user) < 0)
1003 return -1;
1005 if (expr->type == type)
1006 return fn(expr, user);
1008 return 0;
1011 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1013 * Return -1 on error (where fn returning a negative value is treated as
1014 * an error).
1015 * Otherwise return 0.
1017 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
1018 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1020 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
1023 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1025 * Return -1 on error (where fn returning a negative value is treated as
1026 * an error).
1027 * Otherwise return 0.
1029 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
1030 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1032 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1035 /* Internal data structure for pet_expr_writes.
1036 * "id" is the identifier that we are looking for.
1037 * "found" is set if we have found the identifier being written to.
1039 struct pet_expr_writes_data {
1040 isl_id *id;
1041 int found;
1044 /* Given an access expression, check if it writes to data->id.
1045 * If so, set data->found and abort the search.
1047 static int writes(__isl_keep pet_expr *expr, void *user)
1049 struct pet_expr_writes_data *data = user;
1050 isl_id *write_id;
1052 if (!expr->acc.write)
1053 return 0;
1054 if (pet_expr_is_affine(expr))
1055 return 0;
1057 write_id = pet_expr_access_get_id(expr);
1058 isl_id_free(write_id);
1060 if (!write_id)
1061 return -1;
1063 if (write_id != data->id)
1064 return 0;
1066 data->found = 1;
1067 return -1;
1070 /* Does expression "expr" write to "id"?
1072 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1074 struct pet_expr_writes_data data;
1076 data.id = id;
1077 data.found = 0;
1078 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1079 !data.found)
1080 return -1;
1082 return data.found;
1085 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1086 * index expression and access relation of "expr"
1087 * to dimensions of "dst_type" at "dst_pos".
1089 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1090 enum isl_dim_type dst_type, unsigned dst_pos,
1091 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1093 expr = pet_expr_cow(expr);
1094 if (!expr)
1095 return NULL;
1096 if (expr->type != pet_expr_access)
1097 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1098 "not an access pet_expr", return pet_expr_free(expr));
1100 expr->acc.access = isl_map_move_dims(expr->acc.access,
1101 dst_type, dst_pos, src_type, src_pos, n);
1102 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1103 dst_type, dst_pos, src_type, src_pos, n);
1104 if (!expr->acc.access || !expr->acc.index)
1105 return pet_expr_free(expr);
1107 return expr;
1110 /* Replace the index expression and access relation of "expr"
1111 * by their preimages under the function represented by "ma".
1113 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1114 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1116 expr = pet_expr_cow(expr);
1117 if (!expr || !ma)
1118 goto error;
1119 if (expr->type != pet_expr_access)
1120 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1121 "not an access pet_expr", goto error);
1123 expr->acc.access = isl_map_preimage_domain_multi_aff(expr->acc.access,
1124 isl_multi_aff_copy(ma));
1125 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1126 ma);
1127 if (!expr->acc.access || !expr->acc.index)
1128 return pet_expr_free(expr);
1130 return expr;
1131 error:
1132 isl_multi_aff_free(ma);
1133 pet_expr_free(expr);
1134 return NULL;
1137 /* Replace the index expression and access relation of "expr"
1138 * by their preimages under the function represented by "mpa".
1140 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1141 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1143 expr = pet_expr_cow(expr);
1144 if (!expr || !mpa)
1145 goto error;
1146 if (expr->type != pet_expr_access)
1147 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1148 "not an access pet_expr", goto error);
1150 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
1151 expr->acc.access, isl_multi_pw_aff_copy(mpa));
1152 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1153 expr->acc.index, mpa);
1154 if (!expr->acc.access || !expr->acc.index)
1155 return pet_expr_free(expr);
1157 return expr;
1158 error:
1159 isl_multi_pw_aff_free(mpa);
1160 pet_expr_free(expr);
1161 return NULL;
1164 /* Return the access relation of access expression "expr".
1166 __isl_give isl_map *pet_expr_access_get_access(__isl_keep pet_expr *expr)
1168 if (!expr)
1169 return NULL;
1170 if (expr->type != pet_expr_access)
1171 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1172 "not an access expression", return NULL);
1174 return isl_map_copy(expr->acc.access);
1177 /* Return the index expression of access expression "expr".
1179 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1180 __isl_keep pet_expr *expr)
1182 if (!expr)
1183 return NULL;
1184 if (expr->type != pet_expr_access)
1185 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1186 "not an access expression", return NULL);
1188 return isl_multi_pw_aff_copy(expr->acc.index);
1191 /* Align the parameters of expr->acc.index and expr->acc.access.
1193 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1195 expr = pet_expr_cow(expr);
1196 if (!expr)
1197 return NULL;
1198 if (expr->type != pet_expr_access)
1199 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1200 "not an access expression", return pet_expr_free(expr));
1202 expr->acc.access = isl_map_align_params(expr->acc.access,
1203 isl_multi_pw_aff_get_space(expr->acc.index));
1204 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1205 isl_map_get_space(expr->acc.access));
1206 if (!expr->acc.access || !expr->acc.index)
1207 return pet_expr_free(expr);
1209 return expr;
1212 /* Add extra conditions on the parameters to all access relations in "expr".
1214 * The conditions are not added to the index expression. Instead, they
1215 * are used to try and simplify the index expression.
1217 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1218 __isl_take isl_set *cond)
1220 int i;
1222 expr = pet_expr_cow(expr);
1223 if (!expr)
1224 goto error;
1226 for (i = 0; i < expr->n_arg; ++i) {
1227 expr->args[i] = pet_expr_restrict(expr->args[i],
1228 isl_set_copy(cond));
1229 if (!expr->args[i])
1230 goto error;
1233 if (expr->type == pet_expr_access) {
1234 expr->acc.access = isl_map_intersect_params(expr->acc.access,
1235 isl_set_copy(cond));
1236 expr->acc.index = isl_multi_pw_aff_gist_params(
1237 expr->acc.index, isl_set_copy(cond));
1238 if (!expr->acc.access || !expr->acc.index)
1239 goto error;
1242 isl_set_free(cond);
1243 return expr;
1244 error:
1245 isl_set_free(cond);
1246 return pet_expr_free(expr);
1249 /* Modify the access relation and index expression
1250 * of the given access expression
1251 * based on the given iteration space transformation.
1252 * In particular, precompose the access relation and index expression
1253 * with the update function.
1255 * If the access has any arguments then the domain of the access relation
1256 * is a wrapped mapping from the iteration space to the space of
1257 * argument values. We only need to change the domain of this wrapped
1258 * mapping, so we extend the input transformation with an identity mapping
1259 * on the space of argument values.
1261 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1262 __isl_keep isl_multi_pw_aff *update)
1264 isl_space *space;
1266 expr = pet_expr_cow(expr);
1267 if (!expr)
1268 return NULL;
1269 if (expr->type != pet_expr_access)
1270 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1271 "not an access expression", return pet_expr_free(expr));
1273 update = isl_multi_pw_aff_copy(update);
1275 space = isl_map_get_space(expr->acc.access);
1276 space = isl_space_domain(space);
1277 if (!isl_space_is_wrapping(space))
1278 isl_space_free(space);
1279 else {
1280 isl_multi_pw_aff *id;
1281 space = isl_space_unwrap(space);
1282 space = isl_space_range(space);
1283 space = isl_space_map_from_set(space);
1284 id = isl_multi_pw_aff_identity(space);
1285 update = isl_multi_pw_aff_product(update, id);
1288 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
1289 expr->acc.access,
1290 isl_multi_pw_aff_copy(update));
1291 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1292 expr->acc.index, update);
1293 if (!expr->acc.access || !expr->acc.index)
1294 return pet_expr_free(expr);
1296 return expr;
1299 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1301 isl_multi_pw_aff *update = user;
1303 return pet_expr_access_update_domain(expr, update);
1306 /* Modify all access relations in "expr" by precomposing them with
1307 * the given iteration space transformation.
1309 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1310 __isl_take isl_multi_pw_aff *update)
1312 expr = pet_expr_map_access(expr, &update_domain, update);
1313 isl_multi_pw_aff_free(update);
1314 return expr;
1317 /* Given an expression with accesses that have a 0D anonymous domain,
1318 * replace those domains by "space".
1320 __isl_give pet_expr *pet_expr_insert_domain(__isl_take pet_expr *expr,
1321 __isl_take isl_space *space)
1323 isl_multi_pw_aff *mpa;
1325 space = isl_space_from_domain(space);
1326 mpa = isl_multi_pw_aff_zero(space);
1327 return pet_expr_update_domain(expr, mpa);
1330 /* Add all parameters in "space" to the access relation and index expression
1331 * of "expr".
1333 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1335 isl_space *space = user;
1337 expr = pet_expr_cow(expr);
1338 if (!expr)
1339 return NULL;
1340 if (expr->type != pet_expr_access)
1341 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1342 "not an access expression", return pet_expr_free(expr));
1344 expr->acc.access = isl_map_align_params(expr->acc.access,
1345 isl_space_copy(space));
1346 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1347 isl_space_copy(space));
1348 if (!expr->acc.access || !expr->acc.index)
1349 return pet_expr_free(expr);
1351 return expr;
1354 /* Add all parameters in "space" to all access relations and index expressions
1355 * in "expr".
1357 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1358 __isl_take isl_space *space)
1360 expr = pet_expr_map_access(expr, &align_params, space);
1361 isl_space_free(space);
1362 return expr;
1365 /* Insert an argument expression corresponding to "test" in front
1366 * of the list of arguments described by *n_arg and *args.
1368 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1369 __isl_keep isl_multi_pw_aff *test)
1371 int i;
1372 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1374 if (!test)
1375 return pet_expr_free(expr);
1376 expr = pet_expr_cow(expr);
1377 if (!expr)
1378 return NULL;
1380 if (!expr->args) {
1381 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1382 if (!expr->args)
1383 return pet_expr_free(expr);
1384 } else {
1385 pet_expr **ext;
1386 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1387 if (!ext)
1388 return pet_expr_free(expr);
1389 for (i = 0; i < expr->n_arg; ++i)
1390 ext[1 + i] = expr->args[i];
1391 free(expr->args);
1392 expr->args = ext;
1394 expr->n_arg++;
1395 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1396 if (!expr->args[0])
1397 return pet_expr_free(expr);
1399 return expr;
1402 /* Make the expression "expr" depend on the value of "test"
1403 * being equal to "satisfied".
1405 * If "test" is an affine expression, we simply add the conditions
1406 * on the expression having the value "satisfied" to all access relations
1407 * and index expressions.
1409 * Otherwise, we add a filter to "expr" (which is then assumed to be
1410 * an access expression) corresponding to "test" being equal to "satisfied".
1412 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
1413 __isl_take isl_multi_pw_aff *test, int satisfied)
1415 isl_id *id;
1416 isl_ctx *ctx;
1417 isl_space *space;
1418 isl_pw_multi_aff *pma;
1420 expr = pet_expr_cow(expr);
1421 if (!expr || !test)
1422 goto error;
1424 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
1425 isl_pw_aff *pa;
1426 isl_set *cond;
1428 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
1429 isl_multi_pw_aff_free(test);
1430 if (satisfied)
1431 cond = isl_pw_aff_non_zero_set(pa);
1432 else
1433 cond = isl_pw_aff_zero_set(pa);
1434 return pet_expr_restrict(expr, isl_set_params(cond));
1437 ctx = isl_multi_pw_aff_get_ctx(test);
1438 if (expr->type != pet_expr_access)
1439 isl_die(ctx, isl_error_invalid,
1440 "can only filter access expressions", goto error);
1442 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1443 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1444 pma = pet_filter_insert_pma(space, id, satisfied);
1446 expr->acc.access = isl_map_preimage_domain_pw_multi_aff(
1447 expr->acc.access,
1448 isl_pw_multi_aff_copy(pma));
1449 pma = isl_pw_multi_aff_gist(pma,
1450 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma)));
1451 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
1452 expr->acc.index, pma);
1453 if (!expr->acc.access || !expr->acc.index)
1454 goto error;
1456 expr = insert_access_arg(expr, test);
1458 isl_multi_pw_aff_free(test);
1459 return expr;
1460 error:
1461 isl_multi_pw_aff_free(test);
1462 return pet_expr_free(expr);
1465 /* Add a reference identifier to access expression "expr".
1466 * "user" points to an integer that contains the sequence number
1467 * of the next reference.
1469 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
1470 void *user)
1472 isl_ctx *ctx;
1473 char name[50];
1474 int *n_ref = user;
1476 expr = pet_expr_cow(expr);
1477 if (!expr)
1478 return expr;
1479 if (expr->type != pet_expr_access)
1480 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1481 "not an access expression", return pet_expr_free(expr));
1483 ctx = isl_map_get_ctx(expr->acc.access);
1484 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
1485 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
1486 if (!expr->acc.ref_id)
1487 return pet_expr_free(expr);
1489 return expr;
1492 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
1494 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
1497 /* Reset the user pointer on all parameter and tuple ids in
1498 * the access relation and the index expressions
1499 * of the access expression "expr".
1501 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
1502 void *user)
1504 expr = pet_expr_cow(expr);
1505 if (!expr)
1506 return expr;
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 = isl_map_reset_user(expr->acc.access);
1512 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
1513 if (!expr->acc.access || !expr->acc.index)
1514 return pet_expr_free(expr);
1516 return expr;
1519 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
1521 return pet_expr_map_access(expr, &access_anonymize, NULL);
1524 /* Data used in access_gist() callback.
1526 struct pet_access_gist_data {
1527 isl_set *domain;
1528 isl_union_map *value_bounds;
1531 /* Given an expression "expr" of type pet_expr_access, compute
1532 * the gist of the associated access relation and index expression
1533 * with respect to data->domain and the bounds on the values of the arguments
1534 * of the expression.
1536 * The arguments of "expr" have been gisted right before "expr" itself
1537 * is gisted. The gisted arguments may have become equal where before
1538 * they may not have been (obviously) equal. We therefore take
1539 * the opportunity to remove duplicate arguments here.
1541 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
1543 struct pet_access_gist_data *data = user;
1544 isl_set *domain;
1546 expr = pet_expr_remove_duplicate_args(expr);
1547 expr = pet_expr_cow(expr);
1548 if (!expr)
1549 return expr;
1550 if (expr->type != pet_expr_access)
1551 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1552 "not an access expression", return pet_expr_free(expr));
1554 domain = isl_set_copy(data->domain);
1555 if (expr->n_arg > 0)
1556 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
1557 data->value_bounds);
1559 expr->acc.access = isl_map_gist_domain(expr->acc.access,
1560 isl_set_copy(domain));
1561 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
1562 if (!expr->acc.access || !expr->acc.index)
1563 return pet_expr_free(expr);
1565 return expr;
1568 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
1569 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
1571 struct pet_access_gist_data data = { context, value_bounds };
1573 return pet_expr_map_access(expr, &access_gist, &data);
1576 /* Mark "expr" as a read dependening on "read".
1578 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
1579 int read)
1581 if (!expr)
1582 return pet_expr_free(expr);
1583 if (expr->type != pet_expr_access)
1584 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1585 "not an access expression", return pet_expr_free(expr));
1586 if (expr->acc.read == read)
1587 return expr;
1588 expr = pet_expr_cow(expr);
1589 if (!expr)
1590 return NULL;
1591 expr->acc.read = read;
1593 return expr;
1596 /* Mark "expr" as a write dependening on "write".
1598 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
1599 int write)
1601 if (!expr)
1602 return pet_expr_free(expr);
1603 if (expr->type != pet_expr_access)
1604 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1605 "not an access expression", return pet_expr_free(expr));
1606 if (expr->acc.write == write)
1607 return expr;
1608 expr = pet_expr_cow(expr);
1609 if (!expr)
1610 return NULL;
1611 expr->acc.write = write;
1613 return expr;
1616 /* Replace the access relation of "expr" by "access".
1618 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
1619 __isl_take isl_map *access)
1621 expr = pet_expr_cow(expr);
1622 if (!expr || !access)
1623 goto error;
1624 if (expr->type != pet_expr_access)
1625 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1626 "not an access expression", goto error);
1627 isl_map_free(expr->acc.access);
1628 expr->acc.access = access;
1630 return expr;
1631 error:
1632 isl_map_free(access);
1633 pet_expr_free(expr);
1634 return NULL;
1637 /* Replace the index expression of "expr" by "index".
1639 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
1640 __isl_take isl_multi_pw_aff *index)
1642 expr = pet_expr_cow(expr);
1643 if (!expr || !index)
1644 goto error;
1645 if (expr->type != pet_expr_access)
1646 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1647 "not an access expression", goto error);
1648 isl_multi_pw_aff_free(expr->acc.index);
1649 expr->acc.index = index;
1651 return expr;
1652 error:
1653 isl_multi_pw_aff_free(index);
1654 pet_expr_free(expr);
1655 return NULL;
1658 /* Return the reference identifier of access expression "expr".
1660 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
1662 if (!expr)
1663 return NULL;
1664 if (expr->type != pet_expr_access)
1665 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1666 "not an access expression", return NULL);
1668 return isl_id_copy(expr->acc.ref_id);
1671 /* Replace the reference identifier of access expression "expr" by "ref_id".
1673 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
1674 __isl_take isl_id *ref_id)
1676 expr = pet_expr_cow(expr);
1677 if (!expr || !ref_id)
1678 goto error;
1679 if (expr->type != pet_expr_access)
1680 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1681 "not an access expression", goto error);
1682 isl_id_free(expr->acc.ref_id);
1683 expr->acc.ref_id = ref_id;
1685 return expr;
1686 error:
1687 isl_id_free(ref_id);
1688 pet_expr_free(expr);
1689 return NULL;
1692 /* Tag the access relation "access" with "id".
1693 * That is, insert the id as the range of a wrapped relation
1694 * in the domain of "access".
1696 * If "access" is of the form
1698 * D[i] -> A[a]
1700 * then the result is of the form
1702 * [D[i] -> id[]] -> A[a]
1704 __isl_give isl_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
1705 __isl_take isl_map *access)
1707 isl_space *space;
1708 isl_map *add_tag;
1709 isl_id *id;
1711 if (expr->type != pet_expr_access)
1712 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1713 "not an access expression",
1714 return isl_map_free(access));
1716 id = isl_id_copy(expr->acc.ref_id);
1717 space = isl_space_range(isl_map_get_space(access));
1718 space = isl_space_from_range(space);
1719 space = isl_space_set_tuple_id(space, isl_dim_in, id);
1720 add_tag = isl_map_universe(space);
1721 access = isl_map_domain_product(access, add_tag);
1723 return access;
1726 /* Return the relation mapping pairs of domain iterations and argument
1727 * values to the corresponding accessed data elements.
1729 __isl_give isl_map *pet_expr_access_get_dependent_access(
1730 __isl_keep pet_expr *expr)
1732 if (!expr)
1733 return NULL;
1734 if (expr->type != pet_expr_access)
1735 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1736 "not an access expression", return NULL);
1738 return isl_map_copy(expr->acc.access);
1741 /* Return the relation mapping domain iterations to all possibly
1742 * accessed data elements.
1743 * In particular, take the access relation and project out the values
1744 * of the arguments, if any.
1746 __isl_give isl_map *pet_expr_access_get_may_access(__isl_keep pet_expr *expr)
1748 isl_map *access;
1749 isl_space *space;
1750 isl_map *map;
1752 if (!expr)
1753 return NULL;
1754 if (expr->type != pet_expr_access)
1755 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1756 "not an access expression", return NULL);
1758 access = pet_expr_access_get_dependent_access(expr);
1759 if (expr->n_arg == 0)
1760 return access;
1762 space = isl_space_domain(isl_map_get_space(access));
1763 map = isl_map_universe(isl_space_unwrap(space));
1764 map = isl_map_domain_map(map);
1765 access = isl_map_apply_domain(access, map);
1767 return access;
1770 /* Return a relation mapping domain iterations to definitely
1771 * accessed data elements, assuming the statement containing
1772 * the expression is executed.
1774 * If there are no arguments, then all elements are accessed.
1775 * Otherwise, we conservatively return an empty relation.
1777 __isl_give isl_map *pet_expr_access_get_must_access(__isl_keep pet_expr *expr)
1779 isl_space *space;
1781 if (!expr)
1782 return NULL;
1783 if (expr->type != pet_expr_access)
1784 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1785 "not an access expression", return NULL);
1787 if (expr->n_arg == 0)
1788 return pet_expr_access_get_dependent_access(expr);
1790 space = isl_map_get_space(expr->acc.access);
1791 space = isl_space_domain_factor_domain(space);
1793 return isl_map_empty(space);
1796 /* Return the relation mapping domain iterations to all possibly
1797 * accessed data elements, with its domain tagged with the reference
1798 * identifier.
1800 __isl_give isl_map *pet_expr_access_get_tagged_may_access(
1801 __isl_keep pet_expr *expr)
1803 isl_map *access;
1805 if (!expr)
1806 return NULL;
1808 access = pet_expr_access_get_may_access(expr);
1809 access = pet_expr_tag_access(expr, access);
1811 return access;
1814 /* Return the operation type of operation expression "expr".
1816 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
1818 if (!expr)
1819 return pet_op_last;
1820 if (expr->type != pet_expr_op)
1821 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1822 "not an operation expression", return pet_op_last);
1824 return expr->op;
1827 /* Replace the operation type of operation expression "expr" by "type".
1829 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
1830 enum pet_op_type type)
1832 if (!expr)
1833 return pet_expr_free(expr);
1834 if (expr->type != pet_expr_op)
1835 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1836 "not an operation expression",
1837 return pet_expr_free(expr));
1838 if (expr->op == type)
1839 return expr;
1840 expr = pet_expr_cow(expr);
1841 if (!expr)
1842 return NULL;
1843 expr->op = type;
1845 return expr;
1848 /* Return the name of the function called by "expr".
1850 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
1852 if (!expr)
1853 return NULL;
1854 if (expr->type != pet_expr_call)
1855 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1856 "not a call expression", return NULL);
1857 return expr->name;
1860 /* Replace the name of the function called by "expr" by "name".
1862 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
1863 __isl_keep const char *name)
1865 expr = pet_expr_cow(expr);
1866 if (!expr || !name)
1867 return pet_expr_free(expr);
1868 if (expr->type != pet_expr_call)
1869 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1870 "not a call expression", return pet_expr_free(expr));
1871 free(expr->name);
1872 expr->name = strdup(name);
1873 if (!expr->name)
1874 return pet_expr_free(expr);
1875 return expr;
1878 /* Replace the type of the cast performed by "expr" by "name".
1880 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
1881 __isl_keep const char *name)
1883 expr = pet_expr_cow(expr);
1884 if (!expr || !name)
1885 return pet_expr_free(expr);
1886 if (expr->type != pet_expr_cast)
1887 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1888 "not a cast expression", return pet_expr_free(expr));
1889 free(expr->type_name);
1890 expr->type_name = strdup(name);
1891 if (!expr->type_name)
1892 return pet_expr_free(expr);
1893 return expr;
1896 /* Return the value of the integer represented by "expr".
1898 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
1900 if (!expr)
1901 return NULL;
1902 if (expr->type != pet_expr_int)
1903 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1904 "not an int expression", return NULL);
1906 return isl_val_copy(expr->i);
1909 /* Replace the value of the integer represented by "expr" by "v".
1911 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
1912 __isl_take isl_val *v)
1914 expr = pet_expr_cow(expr);
1915 if (!expr || !v)
1916 goto error;
1917 if (expr->type != pet_expr_int)
1918 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1919 "not an int expression", goto error);
1920 isl_val_free(expr->i);
1921 expr->i = v;
1923 return expr;
1924 error:
1925 isl_val_free(v);
1926 pet_expr_free(expr);
1927 return NULL;
1930 /* Replace the value and string representation of the double
1931 * represented by "expr" by "d" and "s".
1933 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
1934 double d, __isl_keep const char *s)
1936 expr = pet_expr_cow(expr);
1937 if (!expr || !s)
1938 return pet_expr_free(expr);
1939 if (expr->type != pet_expr_double)
1940 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1941 "not a double expression", return pet_expr_free(expr));
1942 expr->d.val = d;
1943 free(expr->d.s);
1944 expr->d.s = strdup(s);
1945 if (!expr->d.s)
1946 return pet_expr_free(expr);
1947 return expr;
1950 /* Return a string representation of the double expression "expr".
1952 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
1954 if (!expr)
1955 return NULL;
1956 if (expr->type != pet_expr_double)
1957 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1958 "not a double expression", return NULL);
1959 return strdup(expr->d.s);
1962 /* Return a piecewise affine expression defined on the specified domain
1963 * that represents NaN.
1965 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
1967 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
1970 /* This function is called when we come across an access that is
1971 * nested in what is supposed to be an affine expression.
1972 * "pc" is the context in which the affine expression is created.
1973 * If nesting is allowed in "pc", we return an affine expression that is
1974 * equal to a new parameter corresponding to this nested access.
1975 * Otherwise, we return NaN.
1977 * Note that we currently don't allow nested accesses themselves
1978 * to contain any nested accesses, so we check if "expr" itself
1979 * involves any nested accesses (either explicitly as arguments
1980 * or implicitly through parameters) and return NaN if it does.
1982 * The new parameter is resolved in resolve_nested.
1984 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
1985 __isl_keep pet_context *pc)
1987 isl_ctx *ctx;
1988 isl_id *id;
1989 isl_space *space;
1990 isl_local_space *ls;
1991 isl_aff *aff;
1992 int nested;
1994 if (!expr || !pc)
1995 return NULL;
1996 if (!pet_context_allow_nesting(pc))
1997 return non_affine(pet_context_get_space(pc));
1999 if (pet_expr_get_type(expr) != pet_expr_access)
2000 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2001 "not an access expression", return NULL);
2003 if (expr->n_arg > 0)
2004 return non_affine(pet_context_get_space(pc));
2006 space = pet_expr_access_get_parameter_space(expr);
2007 nested = pet_nested_any_in_space(space);
2008 isl_space_free(space);
2009 if (nested)
2010 return non_affine(pet_context_get_space(pc));
2012 ctx = pet_expr_get_ctx(expr);
2013 id = pet_nested_pet_expr(pet_expr_copy(expr));
2014 space = pet_context_get_space(pc);
2015 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2017 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2018 ls = isl_local_space_from_space(space);
2019 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2021 return isl_pw_aff_from_aff(aff);
2024 /* Extract an affine expression from the access pet_expr "expr".
2025 * "pc" is the context in which the affine expression is created.
2027 * If "expr" is actually an affine expression rather than
2028 * a real access, then we return that expression.
2029 * Otherwise, we require that "expr" is of an integral type.
2030 * If not, we return NaN.
2032 * If we are accessing a scalar (i.e., not an array and not a member)
2033 * and if that scalar can be treated as a parameter (because it is
2034 * not assigned a known or unknown value in the relevant part of the AST),
2035 * then we return an affine expression equal to that parameter.
2037 * If the variable has been assigned a known affine expression,
2038 * then we return that expression.
2040 * Otherwise, we return an expression that is equal to a parameter
2041 * representing "expr" (if "allow_nested" is set).
2043 static __isl_give isl_pw_aff *extract_affine_from_access(
2044 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2046 int pos;
2047 isl_id *id;
2048 isl_space *space;
2049 isl_local_space *ls;
2050 isl_aff *aff;
2052 if (pet_expr_is_affine(expr)) {
2053 isl_pw_aff *pa;
2054 isl_multi_pw_aff *mpa;
2056 mpa = pet_expr_access_get_index(expr);
2057 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
2058 isl_multi_pw_aff_free(mpa);
2059 return pa;
2062 if (pet_expr_get_type_size(expr) == 0)
2063 return non_affine(pet_context_get_space(pc));
2065 if (!pet_expr_is_scalar_access(expr))
2066 return nested_access(expr, pc);
2068 id = pet_expr_access_get_id(expr);
2069 if (pet_context_is_assigned(pc, id)) {
2070 isl_pw_aff *pa;
2072 pa = pet_context_get_value(pc, id);
2073 if (!pa)
2074 return NULL;
2075 if (!isl_pw_aff_involves_nan(pa))
2076 return pa;
2077 isl_pw_aff_free(pa);
2078 return nested_access(expr, pc);
2081 space = pet_context_get_space(pc);
2083 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
2084 if (pos >= 0) {
2085 isl_id_free(id);
2086 } else {
2087 pos = isl_space_dim(space, isl_dim_param);
2088 space = isl_space_add_dims(space, isl_dim_param, 1);
2089 space = isl_space_set_dim_id(space, isl_dim_param, pos, id);
2092 ls = isl_local_space_from_space(space);
2093 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
2095 return isl_pw_aff_from_aff(aff);
2098 /* Construct an affine expression from the integer constant "expr".
2099 * "pc" is the context in which the affine expression is created.
2101 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2102 __isl_keep pet_context *pc)
2104 isl_local_space *ls;
2105 isl_aff *aff;
2107 if (!expr)
2108 return NULL;
2110 ls = isl_local_space_from_space(pet_context_get_space(pc));
2111 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2113 return isl_pw_aff_from_aff(aff);
2116 /* Extract an affine expression from an addition or subtraction operation.
2117 * Return NaN if we are unable to extract an affine expression.
2119 * "pc" is the context in which the affine expression is created.
2121 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2122 __isl_keep pet_context *pc)
2124 isl_pw_aff *lhs;
2125 isl_pw_aff *rhs;
2127 if (!expr)
2128 return NULL;
2129 if (expr->n_arg != 2)
2130 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2131 "expecting two arguments", return NULL);
2133 lhs = pet_expr_extract_affine(expr->args[0], pc);
2134 rhs = pet_expr_extract_affine(expr->args[1], pc);
2136 switch (pet_expr_op_get_type(expr)) {
2137 case pet_op_add:
2138 return isl_pw_aff_add(lhs, rhs);
2139 case pet_op_sub:
2140 return isl_pw_aff_sub(lhs, rhs);
2141 default:
2142 isl_pw_aff_free(lhs);
2143 isl_pw_aff_free(rhs);
2144 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2145 "not an addition or subtraction operation",
2146 return NULL);
2151 /* Extract an affine expression from an integer division or a modulo operation.
2152 * Return NaN if we are unable to extract an affine expression.
2154 * "pc" is the context in which the affine expression is created.
2156 * In particular, if "expr" is lhs/rhs, then return
2158 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2160 * If "expr" is lhs%rhs, then return
2162 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2164 * If the second argument (rhs) is not a (positive) integer constant,
2165 * then we fail to extract an affine expression.
2167 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2168 __isl_keep pet_context *pc)
2170 int is_cst;
2171 isl_pw_aff *lhs;
2172 isl_pw_aff *rhs;
2174 if (!expr)
2175 return NULL;
2176 if (expr->n_arg != 2)
2177 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2178 "expecting two arguments", return NULL);
2180 rhs = pet_expr_extract_affine(expr->args[1], pc);
2182 is_cst = isl_pw_aff_is_cst(rhs);
2183 if (is_cst < 0 || !is_cst) {
2184 isl_pw_aff_free(rhs);
2185 return non_affine(pet_context_get_space(pc));
2188 lhs = pet_expr_extract_affine(expr->args[0], pc);
2190 switch (pet_expr_op_get_type(expr)) {
2191 case pet_op_div:
2192 return isl_pw_aff_tdiv_q(lhs, rhs);
2193 case pet_op_mod:
2194 return isl_pw_aff_tdiv_r(lhs, rhs);
2195 default:
2196 isl_pw_aff_free(lhs);
2197 isl_pw_aff_free(rhs);
2198 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2199 "not a div or mod operator", return NULL);
2204 /* Extract an affine expression from a multiplication operation.
2205 * Return NaN if we are unable to extract an affine expression.
2206 * In particular, if neither of the arguments is a (piecewise) constant
2207 * then we return NaN.
2209 * "pc" is the context in which the affine expression is created.
2211 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2212 __isl_keep pet_context *pc)
2214 int lhs_cst, rhs_cst;
2215 isl_pw_aff *lhs;
2216 isl_pw_aff *rhs;
2218 if (!expr)
2219 return NULL;
2220 if (expr->n_arg != 2)
2221 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2222 "expecting two arguments", return NULL);
2224 lhs = pet_expr_extract_affine(expr->args[0], pc);
2225 rhs = pet_expr_extract_affine(expr->args[1], pc);
2227 lhs_cst = isl_pw_aff_is_cst(lhs);
2228 rhs_cst = isl_pw_aff_is_cst(rhs);
2229 if (lhs_cst < 0 || rhs_cst < 0 || (!lhs_cst && !rhs_cst)) {
2230 isl_pw_aff_free(lhs);
2231 isl_pw_aff_free(rhs);
2232 return non_affine(pet_context_get_space(pc));
2235 return isl_pw_aff_mul(lhs, rhs);
2238 /* Extract an affine expression from a negation operation.
2239 * Return NaN if we are unable to extract an affine expression.
2241 * "pc" is the context in which the affine expression is created.
2243 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
2244 __isl_keep pet_context *pc)
2246 isl_pw_aff *res;
2248 if (!expr)
2249 return NULL;
2250 if (expr->n_arg != 1)
2251 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2252 "expecting one argument", return NULL);
2254 res = pet_expr_extract_affine(expr->args[0], pc);
2255 return isl_pw_aff_neg(res);
2258 /* Extract an affine expression from a conditional operation.
2259 * Return NaN if we are unable to extract an affine expression.
2261 * "pc" is the context in which the affine expression is created.
2263 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
2264 __isl_keep pet_context *pc)
2266 isl_pw_aff *cond, *lhs, *rhs;
2268 if (!expr)
2269 return NULL;
2270 if (expr->n_arg != 3)
2271 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2272 "expecting three arguments", return NULL);
2274 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2275 lhs = pet_expr_extract_affine(expr->args[1], pc);
2276 rhs = pet_expr_extract_affine(expr->args[2], pc);
2278 return isl_pw_aff_cond(cond, lhs, rhs);
2281 /* Compute
2283 * pwaff mod 2^width
2285 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff, unsigned width)
2287 isl_ctx *ctx;
2288 isl_val *mod;
2290 ctx = isl_pw_aff_get_ctx(pwaff);
2291 mod = isl_val_int_from_ui(ctx, width);
2292 mod = isl_val_2exp(mod);
2294 pwaff = isl_pw_aff_mod_val(pwaff, mod);
2296 return pwaff;
2299 /* Limit the domain of "pwaff" to those elements where the function
2300 * value satisfies
2302 * 2^{width-1} <= pwaff < 2^{width-1}
2304 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
2305 unsigned width)
2307 isl_ctx *ctx;
2308 isl_val *v;
2309 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
2310 isl_local_space *ls = isl_local_space_from_space(space);
2311 isl_aff *bound;
2312 isl_set *dom;
2313 isl_pw_aff *b;
2315 ctx = isl_pw_aff_get_ctx(pwaff);
2316 v = isl_val_int_from_ui(ctx, width - 1);
2317 v = isl_val_2exp(v);
2319 bound = isl_aff_zero_on_domain(ls);
2320 bound = isl_aff_add_constant_val(bound, v);
2321 b = isl_pw_aff_from_aff(bound);
2323 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
2324 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2326 b = isl_pw_aff_neg(b);
2327 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
2328 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2330 return pwaff;
2333 /* Handle potential overflows on signed computations.
2335 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2336 * then we adjust the domain of "pa" to avoid overflows.
2338 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
2339 unsigned width)
2341 isl_ctx *ctx;
2342 struct pet_options *options;
2344 if (!pa)
2345 return NULL;
2347 ctx = isl_pw_aff_get_ctx(pa);
2348 options = isl_ctx_peek_pet_options(ctx);
2349 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
2350 pa = avoid_overflow(pa, width);
2352 return pa;
2355 /* Extract an affine expression from some an operation.
2356 * Return NaN if we are unable to extract an affine expression.
2357 * If the result of a binary (non boolean) operation is unsigned,
2358 * then we wrap it based on the size of the type. If the result is signed,
2359 * then we ensure that no overflow occurs.
2361 * "pc" is the context in which the affine expression is created.
2363 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
2364 __isl_keep pet_context *pc)
2366 isl_pw_aff *res;
2367 int type_size;
2369 switch (pet_expr_op_get_type(expr)) {
2370 case pet_op_add:
2371 case pet_op_sub:
2372 res = extract_affine_add_sub(expr, pc);
2373 break;
2374 case pet_op_div:
2375 case pet_op_mod:
2376 res = extract_affine_div_mod(expr, pc);
2377 break;
2378 case pet_op_mul:
2379 res = extract_affine_mul(expr, pc);
2380 break;
2381 case pet_op_minus:
2382 return extract_affine_neg(expr, pc);
2383 case pet_op_cond:
2384 return extract_affine_cond(expr, pc);
2385 case pet_op_eq:
2386 case pet_op_ne:
2387 case pet_op_le:
2388 case pet_op_ge:
2389 case pet_op_lt:
2390 case pet_op_gt:
2391 case pet_op_land:
2392 case pet_op_lor:
2393 case pet_op_lnot:
2394 return pet_expr_extract_affine_condition(expr, pc);
2395 default:
2396 return non_affine(pet_context_get_space(pc));
2399 if (!res)
2400 return NULL;
2401 if (isl_pw_aff_involves_nan(res)) {
2402 isl_space *space = isl_pw_aff_get_domain_space(res);
2403 isl_pw_aff_free(res);
2404 return non_affine(space);
2407 type_size = pet_expr_get_type_size(expr);
2408 if (type_size > 0)
2409 res = wrap(res, type_size);
2410 else
2411 res = signed_overflow(res, -type_size);
2413 return res;
2416 /* Extract an affine expression from some special function calls.
2417 * Return NaN if we are unable to extract an affine expression.
2418 * In particular, we handle "min", "max", "ceild", "floord",
2419 * "intMod", "intFloor" and "intCeil".
2420 * In case of the latter five, the second argument needs to be
2421 * a (positive) integer constant.
2423 * "pc" is the context in which the affine expression is created.
2425 static __isl_give isl_pw_aff *extract_affine_from_call(
2426 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2428 isl_pw_aff *aff1, *aff2;
2429 int n;
2430 const char *name;
2432 n = pet_expr_get_n_arg(expr);
2433 name = pet_expr_call_get_name(expr);
2434 if (!(n == 2 && !strcmp(name, "min")) &&
2435 !(n == 2 && !strcmp(name, "max")) &&
2436 !(n == 2 && !strcmp(name, "intMod")) &&
2437 !(n == 2 && !strcmp(name, "intFloor")) &&
2438 !(n == 2 && !strcmp(name, "intCeil")) &&
2439 !(n == 2 && !strcmp(name, "floord")) &&
2440 !(n == 2 && !strcmp(name, "ceild")))
2441 return non_affine(pet_context_get_space(pc));
2443 if (!strcmp(name, "min") || !strcmp(name, "max")) {
2444 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2445 aff2 = pet_expr_extract_affine(expr->args[1], pc);
2447 if (!strcmp(name, "min"))
2448 aff1 = isl_pw_aff_min(aff1, aff2);
2449 else
2450 aff1 = isl_pw_aff_max(aff1, aff2);
2451 } else if (!strcmp(name, "intMod")) {
2452 isl_val *v;
2454 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2455 return non_affine(pet_context_get_space(pc));
2456 v = pet_expr_int_get_val(expr->args[1]);
2457 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2458 aff1 = isl_pw_aff_mod_val(aff1, v);
2459 } else {
2460 isl_val *v;
2462 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2463 return non_affine(pet_context_get_space(pc));
2464 v = pet_expr_int_get_val(expr->args[1]);
2465 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2466 aff1 = isl_pw_aff_scale_down_val(aff1, v);
2467 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
2468 aff1 = isl_pw_aff_floor(aff1);
2469 else
2470 aff1 = isl_pw_aff_ceil(aff1);
2473 return aff1;
2476 /* Extract an affine expression from "expr", if possible.
2477 * Otherwise return NaN.
2479 * "pc" is the context in which the affine expression is created.
2481 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
2482 __isl_keep pet_context *pc)
2484 if (!expr)
2485 return NULL;
2487 switch (pet_expr_get_type(expr)) {
2488 case pet_expr_access:
2489 return extract_affine_from_access(expr, pc);
2490 case pet_expr_int:
2491 return extract_affine_from_int(expr, pc);
2492 case pet_expr_op:
2493 return extract_affine_from_op(expr, pc);
2494 case pet_expr_call:
2495 return extract_affine_from_call(expr, pc);
2496 case pet_expr_cast:
2497 case pet_expr_double:
2498 case pet_expr_error:
2499 return non_affine(pet_context_get_space(pc));
2503 /* Extract an affine expressions representing the comparison "LHS op RHS"
2504 * Return NaN if we are unable to extract such an affine expression.
2506 * "pc" is the context in which the affine expression is created.
2508 * If the comparison is of the form
2510 * a <= min(b,c)
2512 * then the expression is constructed as the conjunction of
2513 * the comparisons
2515 * a <= b and a <= c
2517 * A similar optimization is performed for max(a,b) <= c.
2518 * We do this because that will lead to simpler representations
2519 * of the expression.
2520 * If isl is ever enhanced to explicitly deal with min and max expressions,
2521 * this optimization can be removed.
2523 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
2524 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
2525 __isl_keep pet_context *pc)
2527 isl_pw_aff *lhs_pa, *rhs_pa;
2529 if (op == pet_op_gt)
2530 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
2531 if (op == pet_op_ge)
2532 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
2534 if (op == pet_op_lt || op == pet_op_le) {
2535 if (pet_expr_is_min(rhs)) {
2536 lhs_pa = pet_expr_extract_comparison(op, lhs,
2537 rhs->args[0], pc);
2538 rhs_pa = pet_expr_extract_comparison(op, lhs,
2539 rhs->args[1], pc);
2540 return pet_and(lhs_pa, rhs_pa);
2542 if (pet_expr_is_max(lhs)) {
2543 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
2544 rhs, pc);
2545 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
2546 rhs, pc);
2547 return pet_and(lhs_pa, rhs_pa);
2551 lhs_pa = pet_expr_extract_affine(lhs, pc);
2552 rhs_pa = pet_expr_extract_affine(rhs, pc);
2554 return pet_comparison(op, lhs_pa, rhs_pa);
2557 /* Extract an affine expressions from the comparison "expr".
2558 * Return NaN if we are unable to extract such an affine expression.
2560 * "pc" is the context in which the affine expression is created.
2562 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
2563 __isl_keep pet_context *pc)
2565 enum pet_op_type type;
2567 if (!expr)
2568 return NULL;
2569 if (expr->n_arg != 2)
2570 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2571 "expecting two arguments", return NULL);
2573 type = pet_expr_op_get_type(expr);
2574 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
2575 pc);
2578 /* Extract an affine expression representing the boolean operation
2579 * expressed by "expr".
2580 * Return NaN if we are unable to extract an affine expression.
2582 * "pc" is the context in which the affine expression is created.
2584 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
2585 __isl_keep pet_context *pc)
2587 isl_pw_aff *lhs, *rhs;
2588 int n;
2590 if (!expr)
2591 return NULL;
2593 n = pet_expr_get_n_arg(expr);
2594 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
2595 if (n == 1)
2596 return pet_not(lhs);
2598 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
2599 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
2602 /* Extract the affine expression "expr != 0 ? 1 : 0".
2603 * Return NaN if we are unable to extract an affine expression.
2605 * "pc" is the context in which the affine expression is created.
2607 static __isl_give isl_pw_aff *extract_implicit_condition(
2608 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2610 isl_pw_aff *res;
2612 res = pet_expr_extract_affine(expr, pc);
2613 return pet_to_bool(res);
2616 /* Extract a boolean affine expression from "expr".
2617 * Return NaN if we are unable to extract an affine expression.
2619 * "pc" is the context in which the affine expression is created.
2621 * If "expr" is neither a comparison nor a boolean operation,
2622 * then we assume it is an affine expression and return the
2623 * boolean expression "expr != 0 ? 1 : 0".
2625 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
2626 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2628 if (!expr)
2629 return NULL;
2631 if (pet_expr_is_comparison(expr))
2632 return extract_comparison(expr, pc);
2633 if (pet_expr_is_boolean(expr))
2634 return extract_boolean(expr, pc);
2636 return extract_implicit_condition(expr, pc);
2639 /* Check if "expr" is an assume expression and if its single argument
2640 * can be converted to an affine expression in the context of "pc".
2641 * If so, replace the argument by the affine expression.
2643 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
2644 __isl_keep pet_context *pc)
2646 isl_pw_aff *cond;
2647 isl_multi_pw_aff *index;
2649 if (!expr)
2650 return NULL;
2651 if (!pet_expr_is_assume(expr))
2652 return expr;
2653 if (expr->n_arg != 1)
2654 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2655 "expecting one argument", return pet_expr_free(expr));
2657 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2658 if (!cond)
2659 return pet_expr_free(expr);
2660 if (isl_pw_aff_involves_nan(cond)) {
2661 isl_pw_aff_free(cond);
2662 return expr;
2665 index = isl_multi_pw_aff_from_pw_aff(cond);
2666 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
2668 return expr;
2671 /* Return the number of bits needed to represent the type of "expr".
2672 * See the description of the type_size field of pet_expr.
2674 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
2676 return expr ? expr->type_size : 0;
2679 /* Replace the number of bits needed to represent the type of "expr"
2680 * by "type_size".
2681 * See the description of the type_size field of pet_expr.
2683 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
2684 int type_size)
2686 expr = pet_expr_cow(expr);
2687 if (!expr)
2688 return NULL;
2690 expr->type_size = type_size;
2692 return expr;
2695 /* Extend an access expression "expr" with an additional index "index".
2696 * In particular, add "index" as an extra argument to "expr" and
2697 * adjust the index expression of "expr" to refer to this extra argument.
2698 * The caller is responsible for calling pet_expr_access_set_depth
2699 * to update the corresponding access relation.
2701 * Note that we only collect the individual index expressions as
2702 * arguments of "expr" here.
2703 * An attempt to integrate them into the index expression of "expr"
2704 * is performed in pet_expr_access_plug_in_args.
2706 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
2707 __isl_take pet_expr *index)
2709 int n;
2710 isl_space *space;
2711 isl_local_space *ls;
2712 isl_pw_aff *pa;
2714 expr = pet_expr_cow(expr);
2715 if (!expr || !index)
2716 goto error;
2717 if (expr->type != pet_expr_access)
2718 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2719 "not an access pet_expr", goto error);
2721 n = pet_expr_get_n_arg(expr);
2722 expr = pet_expr_insert_arg(expr, n, index);
2723 if (!expr)
2724 return NULL;
2726 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
2727 ls = isl_local_space_from_space(space);
2728 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
2729 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
2730 if (!expr->acc.index)
2731 return pet_expr_free(expr);
2733 return expr;
2734 error:
2735 pet_expr_free(expr);
2736 pet_expr_free(index);
2737 return NULL;
2740 /* Extend an access expression "expr" with an additional member acces to "id".
2741 * In particular, extend the index expression of "expr" to include
2742 * the additional member access.
2743 * The caller is responsible for calling pet_expr_access_set_depth
2744 * to update the corresponding access relation.
2746 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
2747 __isl_take isl_id *id)
2749 isl_space *space;
2750 isl_multi_pw_aff *field_access;
2752 expr = pet_expr_cow(expr);
2753 if (!expr || !id)
2754 goto error;
2755 if (expr->type != pet_expr_access)
2756 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2757 "not an access pet_expr", goto error);
2759 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
2760 space = isl_space_from_domain(space);
2761 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2762 field_access = isl_multi_pw_aff_zero(space);
2763 expr->acc.index = pet_array_member(expr->acc.index, field_access);
2764 if (!expr->acc.index)
2765 return pet_expr_free(expr);
2767 return expr;
2768 error:
2769 pet_expr_free(expr);
2770 isl_id_free(id);
2771 return NULL;
2774 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
2776 int i;
2778 if (!expr)
2779 return;
2781 fprintf(stderr, "%*s", indent, "");
2783 switch (expr->type) {
2784 case pet_expr_double:
2785 fprintf(stderr, "%s\n", expr->d.s);
2786 break;
2787 case pet_expr_int:
2788 isl_val_dump(expr->i);
2789 break;
2790 case pet_expr_access:
2791 if (expr->acc.ref_id) {
2792 isl_id_dump(expr->acc.ref_id);
2793 fprintf(stderr, "%*s", indent, "");
2795 isl_map_dump(expr->acc.access);
2796 fprintf(stderr, "%*s", indent, "");
2797 isl_multi_pw_aff_dump(expr->acc.index);
2798 fprintf(stderr, "%*sread: %d\n", indent + 2,
2799 "", expr->acc.read);
2800 fprintf(stderr, "%*swrite: %d\n", indent + 2,
2801 "", expr->acc.write);
2802 for (i = 0; i < expr->n_arg; ++i)
2803 pet_expr_dump_with_indent(expr->args[i], indent + 2);
2804 break;
2805 case pet_expr_op:
2806 fprintf(stderr, "%s\n", op_str[expr->op]);
2807 for (i = 0; i < expr->n_arg; ++i)
2808 pet_expr_dump_with_indent(expr->args[i], indent + 2);
2809 break;
2810 case pet_expr_call:
2811 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
2812 for (i = 0; i < expr->n_arg; ++i)
2813 pet_expr_dump_with_indent(expr->args[i], indent + 2);
2814 break;
2815 case pet_expr_cast:
2816 fprintf(stderr, "(%s)\n", expr->type_name);
2817 for (i = 0; i < expr->n_arg; ++i)
2818 pet_expr_dump_with_indent(expr->args[i], indent + 2);
2819 break;
2820 case pet_expr_error:
2821 fprintf(stderr, "ERROR\n");
2822 break;
2826 void pet_expr_dump(__isl_keep pet_expr *expr)
2828 pet_expr_dump_with_indent(expr, 0);