export pet_expr_new_cast
[pet.git] / expr.c
blob62f6b0f69bd33cbb77f10e0c74ed88fca8cf3513
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
35 #include <string.h>
37 #include "aff.h"
38 #include "array.h"
39 #include "expr.h"
40 #include "expr_arg.h"
41 #include "filter.h"
42 #include "nest.h"
43 #include "options.h"
44 #include "value_bounds.h"
46 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
48 static char *type_str[] = {
49 [pet_expr_access] = "access",
50 [pet_expr_call] = "call",
51 [pet_expr_cast] = "cast",
52 [pet_expr_double] = "double",
53 [pet_expr_int] = "int",
54 [pet_expr_op] = "op",
57 static char *op_str[] = {
58 [pet_op_add_assign] = "+=",
59 [pet_op_sub_assign] = "-=",
60 [pet_op_mul_assign] = "*=",
61 [pet_op_div_assign] = "/=",
62 [pet_op_assign] = "=",
63 [pet_op_add] = "+",
64 [pet_op_sub] = "-",
65 [pet_op_mul] = "*",
66 [pet_op_div] = "/",
67 [pet_op_mod] = "%",
68 [pet_op_shl] = "<<",
69 [pet_op_shr] = ">>",
70 [pet_op_eq] = "==",
71 [pet_op_ne] = "!=",
72 [pet_op_le] = "<=",
73 [pet_op_ge] = ">=",
74 [pet_op_lt] = "<",
75 [pet_op_gt] = ">",
76 [pet_op_minus] = "-",
77 [pet_op_post_inc] = "++",
78 [pet_op_post_dec] = "--",
79 [pet_op_pre_inc] = "++",
80 [pet_op_pre_dec] = "--",
81 [pet_op_address_of] = "&",
82 [pet_op_and] = "&",
83 [pet_op_xor] = "^",
84 [pet_op_or] = "|",
85 [pet_op_not] = "~",
86 [pet_op_land] = "&&",
87 [pet_op_lor] = "||",
88 [pet_op_lnot] = "!",
89 [pet_op_cond] = "?:",
90 [pet_op_assume] = "assume",
91 [pet_op_kill] = "kill"
94 const char *pet_op_str(enum pet_op_type op)
96 return op_str[op];
99 int pet_op_is_inc_dec(enum pet_op_type op)
101 return op == pet_op_post_inc || op == pet_op_post_dec ||
102 op == pet_op_pre_inc || op == pet_op_pre_dec;
105 const char *pet_type_str(enum pet_expr_type type)
107 return type_str[type];
110 enum pet_op_type pet_str_op(const char *str)
112 int i;
114 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
115 if (!strcmp(op_str[i], str))
116 return i;
118 return -1;
121 enum pet_expr_type pet_str_type(const char *str)
123 int i;
125 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
126 if (!strcmp(type_str[i], str))
127 return i;
129 return -1;
132 /* Construct a pet_expr of the given type.
134 __isl_give pet_expr *pet_expr_alloc(isl_ctx *ctx, enum pet_expr_type type)
136 pet_expr *expr;
138 expr = isl_calloc_type(ctx, struct pet_expr);
139 if (!expr)
140 return NULL;
142 expr->ctx = ctx;
143 isl_ctx_ref(ctx);
144 expr->type = type;
145 expr->ref = 1;
147 return expr;
150 /* Construct an access pet_expr from an index expression.
151 * By default, the access is considered to be a read access.
152 * The initial depth is set from the index expression and
153 * may still be updated by the caller before the access relation
154 * is created.
156 __isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
158 isl_ctx *ctx;
159 pet_expr *expr;
161 if (!index)
162 return NULL;
163 ctx = isl_multi_pw_aff_get_ctx(index);
164 expr = pet_expr_alloc(ctx, pet_expr_access);
165 if (!expr)
166 goto error;
168 expr->acc.read = 1;
169 expr->acc.write = 0;
171 expr = pet_expr_access_set_index(expr, index);
173 return expr;
174 error:
175 isl_multi_pw_aff_free(index);
176 return NULL;
179 /* Extend the range of "access" with "n" dimensions, retaining
180 * the tuple identifier on this range.
182 * If "access" represents a member access, then extend the range
183 * of the member.
185 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
187 isl_id *id;
189 id = isl_map_get_tuple_id(access, isl_dim_out);
191 if (!isl_map_range_is_wrapping(access)) {
192 access = isl_map_add_dims(access, isl_dim_out, n);
193 } else {
194 isl_map *domain;
196 domain = isl_map_copy(access);
197 domain = isl_map_range_factor_domain(domain);
198 access = isl_map_range_factor_range(access);
199 access = extend_range(access, n);
200 access = isl_map_range_product(domain, access);
203 access = isl_map_set_tuple_id(access, isl_dim_out, id);
205 return access;
208 /* Does the access expression "expr" have any explicit access relation?
210 static int has_any_access_relation(__isl_keep pet_expr *expr)
212 enum pet_expr_access_type type;
214 if (!expr)
215 return -1;
217 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type)
218 if (expr->acc.access[type])
219 return 1;
221 return 0;
224 /* Are all relevant access relations explicitly available in "expr"?
226 static int has_relevant_access_relations(__isl_keep pet_expr *expr)
228 enum pet_expr_access_type type;
230 if (!expr)
231 return -1;
233 if (expr->acc.kill && !expr->acc.access[pet_expr_access_fake_killed])
234 return 0;
235 if (expr->acc.read && !expr->acc.access[pet_expr_access_may_read])
236 return 0;
237 if (expr->acc.write &&
238 (!expr->acc.access[pet_expr_access_may_write] ||
239 !expr->acc.access[pet_expr_access_must_write]))
240 return 0;
242 return 1;
245 /* Replace the depth of the access expr "expr" by "depth".
247 * To avoid inconsistencies between the depth and the access relation,
248 * we currently do not allow the depth to change once the access relation
249 * has been set or computed.
251 __isl_give pet_expr *pet_expr_access_set_depth(__isl_take pet_expr *expr,
252 int depth)
254 isl_map *access;
255 int dim;
257 if (!expr)
258 return NULL;
259 if (expr->acc.depth == depth)
260 return expr;
261 if (has_any_access_relation(expr))
262 isl_die(pet_expr_get_ctx(expr), isl_error_unsupported,
263 "depth cannot be changed after access relation "
264 "has been set or computed", return pet_expr_free(expr));
266 expr = pet_expr_cow(expr);
267 if (!expr)
268 return NULL;
269 expr->acc.depth = depth;
271 return expr;
274 /* Construct a pet_expr that kills the elements specified by
275 * the index expression "index" and the access relation "access".
277 __isl_give pet_expr *pet_expr_kill_from_access_and_index(
278 __isl_take isl_map *access, __isl_take isl_multi_pw_aff *index)
280 int depth;
281 pet_expr *expr;
283 if (!access || !index)
284 goto error;
286 expr = pet_expr_from_index(index);
287 expr = pet_expr_access_set_read(expr, 0);
288 expr = pet_expr_access_set_kill(expr, 1);
289 depth = isl_map_dim(access, isl_dim_out);
290 expr = pet_expr_access_set_depth(expr, depth);
291 expr = pet_expr_access_set_access(expr, pet_expr_access_killed,
292 isl_union_map_from_map(access));
293 return pet_expr_new_unary(0, pet_op_kill, expr);
294 error:
295 isl_map_free(access);
296 isl_multi_pw_aff_free(index);
297 return NULL;
300 /* Construct a unary pet_expr that performs "op" on "arg",
301 * where the result is represented using a type of "type_size" bits
302 * (may be zero if unknown or if the type is not an integer).
304 __isl_give pet_expr *pet_expr_new_unary(int type_size, enum pet_op_type op,
305 __isl_take pet_expr *arg)
307 isl_ctx *ctx;
308 pet_expr *expr;
310 if (!arg)
311 return NULL;
312 ctx = pet_expr_get_ctx(arg);
313 expr = pet_expr_alloc(ctx, pet_expr_op);
314 expr = pet_expr_set_n_arg(expr, 1);
315 if (!expr)
316 goto error;
318 expr->op = op;
319 expr->type_size = type_size;
320 expr->args[pet_un_arg] = arg;
322 return expr;
323 error:
324 pet_expr_free(arg);
325 return NULL;
328 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
329 * where the result is represented using a type of "type_size" bits
330 * (may be zero if unknown or if the type is not an integer).
332 __isl_give pet_expr *pet_expr_new_binary(int type_size, enum pet_op_type op,
333 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
335 isl_ctx *ctx;
336 pet_expr *expr;
338 if (!lhs || !rhs)
339 goto error;
340 ctx = pet_expr_get_ctx(lhs);
341 expr = pet_expr_alloc(ctx, pet_expr_op);
342 expr = pet_expr_set_n_arg(expr, 2);
343 if (!expr)
344 goto error;
346 expr->op = op;
347 expr->type_size = type_size;
348 expr->args[pet_bin_lhs] = lhs;
349 expr->args[pet_bin_rhs] = rhs;
351 return expr;
352 error:
353 pet_expr_free(lhs);
354 pet_expr_free(rhs);
355 return NULL;
358 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
360 __isl_give pet_expr *pet_expr_new_ternary(__isl_take pet_expr *cond,
361 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
363 isl_ctx *ctx;
364 pet_expr *expr;
366 if (!cond || !lhs || !rhs)
367 goto error;
368 ctx = pet_expr_get_ctx(cond);
369 expr = pet_expr_alloc(ctx, pet_expr_op);
370 expr = pet_expr_set_n_arg(expr, 3);
371 if (!expr)
372 goto error;
374 expr->op = pet_op_cond;
375 expr->args[pet_ter_cond] = cond;
376 expr->args[pet_ter_true] = lhs;
377 expr->args[pet_ter_false] = rhs;
379 return expr;
380 error:
381 pet_expr_free(cond);
382 pet_expr_free(lhs);
383 pet_expr_free(rhs);
384 return NULL;
387 /* Construct a call pet_expr that calls function "name" with "n_arg"
388 * arguments. The caller is responsible for filling in the arguments.
390 __isl_give pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
391 unsigned n_arg)
393 pet_expr *expr;
395 expr = pet_expr_alloc(ctx, pet_expr_call);
396 expr = pet_expr_set_n_arg(expr, n_arg);
397 if (!expr)
398 return NULL;
400 expr->c.name = strdup(name);
401 if (!expr->c.name)
402 return pet_expr_free(expr);
404 return expr;
407 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
409 __isl_give pet_expr *pet_expr_new_cast(const char *type_name,
410 __isl_take pet_expr *arg)
412 isl_ctx *ctx;
413 pet_expr *expr;
415 if (!arg)
416 return NULL;
418 ctx = pet_expr_get_ctx(arg);
419 expr = pet_expr_alloc(ctx, pet_expr_cast);
420 expr = pet_expr_set_n_arg(expr, 1);
421 if (!expr)
422 goto error;
424 expr->type_name = strdup(type_name);
425 if (!expr->type_name)
426 goto error;
428 expr->args[0] = arg;
430 return expr;
431 error:
432 pet_expr_free(arg);
433 pet_expr_free(expr);
434 return NULL;
437 /* Construct a pet_expr that represents the double "d".
439 __isl_give pet_expr *pet_expr_new_double(isl_ctx *ctx,
440 double val, const char *s)
442 pet_expr *expr;
444 expr = pet_expr_alloc(ctx, pet_expr_double);
445 if (!expr)
446 return NULL;
448 expr->d.val = val;
449 expr->d.s = strdup(s);
450 if (!expr->d.s)
451 return pet_expr_free(expr);
453 return expr;
456 /* Construct a pet_expr that represents the integer value "v".
458 __isl_give pet_expr *pet_expr_new_int(__isl_take isl_val *v)
460 isl_ctx *ctx;
461 pet_expr *expr;
463 if (!v)
464 return NULL;
466 ctx = isl_val_get_ctx(v);
467 expr = pet_expr_alloc(ctx, pet_expr_int);
468 if (!expr)
469 goto error;
471 expr->i = v;
473 return expr;
474 error:
475 isl_val_free(v);
476 return NULL;
479 /* Return an independent duplicate of "expr".
481 * In case of an access expression, make sure the depth of the duplicate is set
482 * before the access relation (if any) is set and after the index expression
483 * is set.
485 static __isl_give pet_expr *pet_expr_dup(__isl_keep pet_expr *expr)
487 int i;
488 pet_expr *dup;
489 enum pet_expr_access_type type;
491 if (!expr)
492 return NULL;
494 dup = pet_expr_alloc(expr->ctx, expr->type);
495 dup = pet_expr_set_type_size(dup, expr->type_size);
496 dup = pet_expr_set_n_arg(dup, expr->n_arg);
497 for (i = 0; i < expr->n_arg; ++i)
498 dup = pet_expr_set_arg(dup, i, pet_expr_copy(expr->args[i]));
500 switch (expr->type) {
501 case pet_expr_access:
502 if (expr->acc.ref_id)
503 dup = pet_expr_access_set_ref_id(dup,
504 isl_id_copy(expr->acc.ref_id));
505 dup = pet_expr_access_set_index(dup,
506 isl_multi_pw_aff_copy(expr->acc.index));
507 dup = pet_expr_access_set_depth(dup, expr->acc.depth);
508 for (type = pet_expr_access_begin;
509 type < pet_expr_access_end; ++type) {
510 if (!expr->acc.access[type])
511 continue;
512 dup = pet_expr_access_set_access(dup, type,
513 isl_union_map_copy(expr->acc.access[type]));
515 dup = pet_expr_access_set_read(dup, expr->acc.read);
516 dup = pet_expr_access_set_write(dup, expr->acc.write);
517 dup = pet_expr_access_set_kill(dup, expr->acc.kill);
518 break;
519 case pet_expr_call:
520 dup = pet_expr_call_set_name(dup, expr->c.name);
521 if (expr->c.summary)
522 dup = pet_expr_call_set_summary(dup,
523 pet_function_summary_copy(expr->c.summary));
524 break;
525 case pet_expr_cast:
526 dup = pet_expr_cast_set_type_name(dup, expr->type_name);
527 break;
528 case pet_expr_double:
529 dup = pet_expr_double_set(dup, expr->d.val, expr->d.s);
530 break;
531 case pet_expr_int:
532 dup = pet_expr_int_set_val(dup, isl_val_copy(expr->i));
533 break;
534 case pet_expr_op:
535 dup = pet_expr_op_set_type(dup, expr->op);
536 break;
537 case pet_expr_error:
538 dup = pet_expr_free(dup);
539 break;
542 return dup;
545 __isl_give pet_expr *pet_expr_cow(__isl_take pet_expr *expr)
547 if (!expr)
548 return NULL;
550 if (expr->ref == 1)
551 return expr;
552 expr->ref--;
553 return pet_expr_dup(expr);
556 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr)
558 enum pet_expr_access_type type;
559 int i;
561 if (!expr)
562 return NULL;
563 if (--expr->ref > 0)
564 return NULL;
566 for (i = 0; i < expr->n_arg; ++i)
567 pet_expr_free(expr->args[i]);
568 free(expr->args);
570 switch (expr->type) {
571 case pet_expr_access:
572 isl_id_free(expr->acc.ref_id);
573 for (type = pet_expr_access_begin;
574 type < pet_expr_access_end; ++type)
575 isl_union_map_free(expr->acc.access[type]);
576 isl_multi_pw_aff_free(expr->acc.index);
577 break;
578 case pet_expr_call:
579 free(expr->c.name);
580 pet_function_summary_free(expr->c.summary);
581 break;
582 case pet_expr_cast:
583 free(expr->type_name);
584 break;
585 case pet_expr_double:
586 free(expr->d.s);
587 break;
588 case pet_expr_int:
589 isl_val_free(expr->i);
590 break;
591 case pet_expr_op:
592 case pet_expr_error:
593 break;
596 isl_ctx_deref(expr->ctx);
597 free(expr);
598 return NULL;
601 /* Return an additional reference to "expr".
603 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr)
605 if (!expr)
606 return NULL;
608 expr->ref++;
609 return expr;
612 /* Return the isl_ctx in which "expr" was created.
614 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr)
616 return expr ? expr->ctx : NULL;
619 /* Return the type of "expr".
621 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr)
623 if (!expr)
624 return pet_expr_error;
625 return expr->type;
628 /* Return the number of arguments of "expr".
630 int pet_expr_get_n_arg(__isl_keep pet_expr *expr)
632 if (!expr)
633 return -1;
635 return expr->n_arg;
638 /* Set the number of arguments of "expr" to "n".
640 * If "expr" originally had more arguments, then remove the extra arguments.
641 * If "expr" originally had fewer arguments, then create space for
642 * the extra arguments ans initialize them to NULL.
644 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n)
646 int i;
647 pet_expr **args;
649 if (!expr)
650 return NULL;
651 if (expr->n_arg == n)
652 return expr;
653 expr = pet_expr_cow(expr);
654 if (!expr)
655 return NULL;
657 if (n < expr->n_arg) {
658 for (i = n; i < expr->n_arg; ++i)
659 pet_expr_free(expr->args[i]);
660 expr->n_arg = n;
661 return expr;
664 args = isl_realloc_array(expr->ctx, expr->args, pet_expr *, n);
665 if (!args)
666 return pet_expr_free(expr);
667 expr->args = args;
668 for (i = expr->n_arg; i < n; ++i)
669 expr->args[i] = NULL;
670 expr->n_arg = n;
672 return expr;
675 /* Return the argument of "expr" at position "pos".
677 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos)
679 if (!expr)
680 return NULL;
681 if (pos < 0 || pos >= expr->n_arg)
682 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
683 "position out of bounds", return NULL);
685 return pet_expr_copy(expr->args[pos]);
688 /* Replace the argument of "expr" at position "pos" by "arg".
690 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
691 __isl_take pet_expr *arg)
693 if (!expr || !arg)
694 goto error;
695 if (pos < 0 || pos >= expr->n_arg)
696 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
697 "position out of bounds", goto error);
698 if (expr->args[pos] == arg) {
699 pet_expr_free(arg);
700 return expr;
703 expr = pet_expr_cow(expr);
704 if (!expr)
705 goto error;
707 pet_expr_free(expr->args[pos]);
708 expr->args[pos] = arg;
710 return expr;
711 error:
712 pet_expr_free(expr);
713 pet_expr_free(arg);
714 return NULL;
717 /* Does "expr" perform a comparison operation?
719 int pet_expr_is_comparison(__isl_keep pet_expr *expr)
721 if (!expr)
722 return -1;
723 if (expr->type != pet_expr_op)
724 return 0;
725 switch (expr->op) {
726 case pet_op_eq:
727 case pet_op_ne:
728 case pet_op_le:
729 case pet_op_ge:
730 case pet_op_lt:
731 case pet_op_gt:
732 return 1;
733 default:
734 return 0;
738 /* Does "expr" perform a boolean operation?
740 int pet_expr_is_boolean(__isl_keep pet_expr *expr)
742 if (!expr)
743 return -1;
744 if (expr->type != pet_expr_op)
745 return 0;
746 switch (expr->op) {
747 case pet_op_land:
748 case pet_op_lor:
749 case pet_op_lnot:
750 return 1;
751 default:
752 return 0;
756 /* Is "expr" an assume statement?
758 int pet_expr_is_assume(__isl_keep pet_expr *expr)
760 if (!expr)
761 return -1;
762 if (expr->type != pet_expr_op)
763 return 0;
764 return expr->op == pet_op_assume;
767 /* Does "expr" perform a min operation?
769 int pet_expr_is_min(__isl_keep pet_expr *expr)
771 if (!expr)
772 return -1;
773 if (expr->type != pet_expr_call)
774 return 0;
775 if (expr->n_arg != 2)
776 return 0;
777 if (strcmp(expr->c.name, "min") != 0)
778 return 0;
779 return 1;
782 /* Does "expr" perform a max operation?
784 int pet_expr_is_max(__isl_keep pet_expr *expr)
786 if (!expr)
787 return -1;
788 if (expr->type != pet_expr_call)
789 return 0;
790 if (expr->n_arg != 2)
791 return 0;
792 if (strcmp(expr->c.name, "max") != 0)
793 return 0;
794 return 1;
797 /* Does "expr" represent an access to an unnamed space, i.e.,
798 * does it represent an affine expression?
800 int pet_expr_is_affine(__isl_keep pet_expr *expr)
802 int has_id;
804 if (!expr)
805 return -1;
806 if (expr->type != pet_expr_access)
807 return 0;
809 has_id = isl_multi_pw_aff_has_tuple_id(expr->acc.index, isl_dim_out);
810 if (has_id < 0)
811 return -1;
813 return !has_id;
816 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
817 * not part of any struct?
819 int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
821 if (!expr)
822 return -1;
823 if (expr->type != pet_expr_access)
824 return 0;
825 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index))
826 return 0;
828 return expr->acc.depth == 0;
831 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
832 * of parameters.
834 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
835 __isl_keep isl_multi_pw_aff *mpa2)
837 int equal;
839 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
840 if (equal < 0 || equal)
841 return equal;
842 mpa2 = isl_multi_pw_aff_copy(mpa2);
843 mpa2 = isl_multi_pw_aff_align_params(mpa2,
844 isl_multi_pw_aff_get_space(mpa1));
845 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
846 isl_multi_pw_aff_free(mpa2);
848 return equal;
851 /* Construct an access relation from the index expression and
852 * the array depth of the access expression "expr".
854 * If the number of indices is smaller than the depth of the array,
855 * then we assume that all elements of the remaining dimensions
856 * are accessed.
858 static __isl_give isl_union_map *construct_access_relation(
859 __isl_keep pet_expr *expr)
861 isl_map *access;
862 int dim;
863 int read, write;
865 if (!expr)
866 return NULL;
868 access = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
869 if (!access)
870 return NULL;
872 dim = isl_map_dim(access, isl_dim_out);
873 if (dim > expr->acc.depth)
874 isl_die(isl_map_get_ctx(access), isl_error_internal,
875 "number of indices greater than depth",
876 access = isl_map_free(access));
878 if (dim != expr->acc.depth)
879 access = extend_range(access, expr->acc.depth - dim);
881 return isl_union_map_from_map(access);
884 /* Ensure that all relevant access relations are explicitly
885 * available in "expr".
887 * If "expr" does not already have the relevant access relations, then create
888 * them based on the index expression and the array depth.
890 * We do not cow since adding an explicit access relation
891 * does not change the meaning of the expression.
893 static __isl_give pet_expr *introduce_access_relations(
894 __isl_take pet_expr *expr)
896 enum pet_expr_access_type type;
897 isl_union_map *access;
898 int dim;
899 int kill, read, write;
901 if (!expr)
902 return NULL;
903 if (has_relevant_access_relations(expr))
904 return expr;
906 access = construct_access_relation(expr);
907 if (!access)
908 return pet_expr_free(expr);
910 kill = expr->acc.kill;
911 read = expr->acc.read;
912 write = expr->acc.write;
913 if (kill && !expr->acc.access[pet_expr_access_fake_killed])
914 expr->acc.access[pet_expr_access_fake_killed] =
915 isl_union_map_copy(access);
916 if (read && !expr->acc.access[pet_expr_access_may_read])
917 expr->acc.access[pet_expr_access_may_read] =
918 isl_union_map_copy(access);
919 if (write && !expr->acc.access[pet_expr_access_may_write])
920 expr->acc.access[pet_expr_access_may_write] =
921 isl_union_map_copy(access);
922 if (write && !expr->acc.access[pet_expr_access_must_write])
923 expr->acc.access[pet_expr_access_must_write] =
924 isl_union_map_copy(access);
926 isl_union_map_free(access);
928 if (!has_relevant_access_relations(expr))
929 return pet_expr_free(expr);
931 return expr;
934 /* Return 1 if the two pet_exprs are equivalent.
936 int pet_expr_is_equal(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2)
938 int i;
939 enum pet_expr_access_type type;
941 if (!expr1 || !expr2)
942 return 0;
944 if (expr1->type != expr2->type)
945 return 0;
946 if (expr1->n_arg != expr2->n_arg)
947 return 0;
948 for (i = 0; i < expr1->n_arg; ++i)
949 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
950 return 0;
951 switch (expr1->type) {
952 case pet_expr_error:
953 return -1;
954 case pet_expr_double:
955 if (strcmp(expr1->d.s, expr2->d.s))
956 return 0;
957 if (expr1->d.val != expr2->d.val)
958 return 0;
959 break;
960 case pet_expr_int:
961 if (!isl_val_eq(expr1->i, expr2->i))
962 return 0;
963 break;
964 case pet_expr_access:
965 if (expr1->acc.read != expr2->acc.read)
966 return 0;
967 if (expr1->acc.write != expr2->acc.write)
968 return 0;
969 if (expr1->acc.kill != expr2->acc.kill)
970 return 0;
971 if (expr1->acc.ref_id != expr2->acc.ref_id)
972 return 0;
973 if (!expr1->acc.index || !expr2->acc.index)
974 return 0;
975 if (!multi_pw_aff_is_equal(expr1->acc.index, expr2->acc.index))
976 return 0;
977 if (expr1->acc.depth != expr2->acc.depth)
978 return 0;
979 if (has_relevant_access_relations(expr1) !=
980 has_relevant_access_relations(expr2)) {
981 int equal;
982 expr1 = pet_expr_copy(expr1);
983 expr2 = pet_expr_copy(expr2);
984 expr1 = introduce_access_relations(expr1);
985 expr2 = introduce_access_relations(expr2);
986 equal = pet_expr_is_equal(expr1, expr2);
987 pet_expr_free(expr1);
988 pet_expr_free(expr2);
989 return equal;
991 for (type = pet_expr_access_begin;
992 type < pet_expr_access_end; ++type) {
993 if (!expr1->acc.access[type] !=
994 !expr2->acc.access[type])
995 return 0;
996 if (!expr1->acc.access[type])
997 continue;
998 if (!isl_union_map_is_equal(expr1->acc.access[type],
999 expr2->acc.access[type]))
1000 return 0;
1002 break;
1003 case pet_expr_op:
1004 if (expr1->op != expr2->op)
1005 return 0;
1006 break;
1007 case pet_expr_call:
1008 if (strcmp(expr1->c.name, expr2->c.name))
1009 return 0;
1010 break;
1011 case pet_expr_cast:
1012 if (strcmp(expr1->type_name, expr2->type_name))
1013 return 0;
1014 break;
1017 return 1;
1020 /* Does the access expression "expr" read the accessed elements?
1022 int pet_expr_access_is_read(__isl_keep pet_expr *expr)
1024 if (!expr)
1025 return -1;
1026 if (expr->type != pet_expr_access)
1027 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1028 "not an access expression", return -1);
1030 return expr->acc.read;
1033 /* Does the access expression "expr" write to the accessed elements?
1035 int pet_expr_access_is_write(__isl_keep pet_expr *expr)
1037 if (!expr)
1038 return -1;
1039 if (expr->type != pet_expr_access)
1040 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1041 "not an access expression", return -1);
1043 return expr->acc.write;
1046 /* Return the identifier of the array accessed by "expr".
1048 * If "expr" represents a member access, then return the identifier
1049 * of the outer structure array.
1051 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
1053 if (!expr)
1054 return NULL;
1055 if (expr->type != pet_expr_access)
1056 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1057 "not an access expression", return NULL);
1059 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index)) {
1060 isl_space *space;
1061 isl_id *id;
1063 space = isl_multi_pw_aff_get_space(expr->acc.index);
1064 space = isl_space_range(space);
1065 while (space && isl_space_is_wrapping(space))
1066 space = isl_space_domain(isl_space_unwrap(space));
1067 id = isl_space_get_tuple_id(space, isl_dim_set);
1068 isl_space_free(space);
1070 return id;
1073 return isl_multi_pw_aff_get_tuple_id(expr->acc.index, isl_dim_out);
1076 /* Return the parameter space of "expr".
1078 __isl_give isl_space *pet_expr_access_get_parameter_space(
1079 __isl_keep pet_expr *expr)
1081 isl_space *space;
1083 if (!expr)
1084 return NULL;
1085 if (expr->type != pet_expr_access)
1086 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1087 "not an access expression", return NULL);
1089 space = isl_multi_pw_aff_get_space(expr->acc.index);
1090 space = isl_space_params(space);
1092 return space;
1095 /* Return the domain space of "expr", including the arguments (if any).
1097 __isl_give isl_space *pet_expr_access_get_augmented_domain_space(
1098 __isl_keep pet_expr *expr)
1100 isl_space *space;
1102 if (!expr)
1103 return NULL;
1104 if (expr->type != pet_expr_access)
1105 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1106 "not an access expression", return NULL);
1108 space = isl_multi_pw_aff_get_space(expr->acc.index);
1109 space = isl_space_domain(space);
1111 return space;
1114 /* Return the domain space of "expr", without the arguments (if any).
1116 __isl_give isl_space *pet_expr_access_get_domain_space(
1117 __isl_keep pet_expr *expr)
1119 isl_space *space;
1121 space = pet_expr_access_get_augmented_domain_space(expr);
1122 if (isl_space_is_wrapping(space))
1123 space = isl_space_domain(isl_space_unwrap(space));
1125 return space;
1128 /* Return the space of the data accessed by "expr".
1130 __isl_give isl_space *pet_expr_access_get_data_space(__isl_keep pet_expr *expr)
1132 isl_space *space;
1134 if (!expr)
1135 return NULL;
1136 if (expr->type != pet_expr_access)
1137 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1138 "not an access expression", return NULL);
1140 space = isl_multi_pw_aff_get_space(expr->acc.index);
1141 space = isl_space_range(space);
1143 return space;
1146 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1148 static __isl_give pet_expr *pet_expr_map_expr_of_type(__isl_take pet_expr *expr,
1149 enum pet_expr_type type,
1150 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1151 void *user)
1153 int i, n;
1155 n = pet_expr_get_n_arg(expr);
1156 for (i = 0; i < n; ++i) {
1157 pet_expr *arg = pet_expr_get_arg(expr, i);
1158 arg = pet_expr_map_expr_of_type(arg, type, fn, user);
1159 expr = pet_expr_set_arg(expr, i, arg);
1162 if (!expr)
1163 return NULL;
1165 if (expr->type == type)
1166 expr = fn(expr, user);
1168 return expr;
1171 /* Modify all expressions of type pet_expr_access in "expr"
1172 * by calling "fn" on them.
1174 __isl_give pet_expr *pet_expr_map_access(__isl_take pet_expr *expr,
1175 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1176 void *user)
1178 return pet_expr_map_expr_of_type(expr, pet_expr_access, fn, user);
1181 /* Modify all expressions of type pet_expr_call in "expr"
1182 * by calling "fn" on them.
1184 __isl_give pet_expr *pet_expr_map_call(__isl_take pet_expr *expr,
1185 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1186 void *user)
1188 return pet_expr_map_expr_of_type(expr, pet_expr_call, fn, user);
1191 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1193 * Return -1 on error (where fn returning a negative value is treated as
1194 * an error).
1195 * Otherwise return 0.
1197 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
1198 enum pet_expr_type type,
1199 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1201 int i;
1203 if (!expr)
1204 return -1;
1206 for (i = 0; i < expr->n_arg; ++i)
1207 if (pet_expr_foreach_expr_of_type(expr->args[i],
1208 type, fn, user) < 0)
1209 return -1;
1211 if (expr->type == type)
1212 return fn(expr, user);
1214 return 0;
1217 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1219 * Return -1 on error (where fn returning a negative value is treated as
1220 * an error).
1221 * Otherwise return 0.
1223 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
1224 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1226 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
1229 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1231 * Return -1 on error (where fn returning a negative value is treated as
1232 * an error).
1233 * Otherwise return 0.
1235 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
1236 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1238 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1241 /* Internal data structure for pet_expr_writes.
1242 * "id" is the identifier that we are looking for.
1243 * "found" is set if we have found the identifier being written to.
1245 struct pet_expr_writes_data {
1246 isl_id *id;
1247 int found;
1250 /* Given an access expression, check if it writes to data->id.
1251 * If so, set data->found and abort the search.
1253 static int writes(__isl_keep pet_expr *expr, void *user)
1255 struct pet_expr_writes_data *data = user;
1256 isl_id *write_id;
1258 if (!expr->acc.write)
1259 return 0;
1260 if (pet_expr_is_affine(expr))
1261 return 0;
1263 write_id = pet_expr_access_get_id(expr);
1264 isl_id_free(write_id);
1266 if (!write_id)
1267 return -1;
1269 if (write_id != data->id)
1270 return 0;
1272 data->found = 1;
1273 return -1;
1276 /* Does expression "expr" write to "id"?
1278 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1280 struct pet_expr_writes_data data;
1282 data.id = id;
1283 data.found = 0;
1284 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1285 !data.found)
1286 return -1;
1288 return data.found;
1291 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1292 * index expression and access relations of "expr" (if any)
1293 * to dimensions of "dst_type" at "dst_pos".
1295 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1296 enum isl_dim_type dst_type, unsigned dst_pos,
1297 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1299 enum pet_expr_access_type type;
1301 expr = pet_expr_cow(expr);
1302 if (!expr)
1303 return NULL;
1304 if (expr->type != pet_expr_access)
1305 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1306 "not an access pet_expr", return pet_expr_free(expr));
1308 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1309 if (!expr->acc.access[type])
1310 continue;
1311 expr->acc.access[type] =
1312 pet_union_map_move_dims(expr->acc.access[type],
1313 dst_type, dst_pos, src_type, src_pos, n);
1314 if (!expr->acc.access[type])
1315 break;
1317 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1318 dst_type, dst_pos, src_type, src_pos, n);
1319 if (!expr->acc.index || type < pet_expr_access_end)
1320 return pet_expr_free(expr);
1322 return expr;
1325 /* Replace the index expression and access relations (if any) of "expr"
1326 * by their preimages under the function represented by "ma".
1328 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1329 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1331 enum pet_expr_access_type type;
1333 expr = pet_expr_cow(expr);
1334 if (!expr || !ma)
1335 goto error;
1336 if (expr->type != pet_expr_access)
1337 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1338 "not an access pet_expr", goto error);
1340 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1341 if (!expr->acc.access[type])
1342 continue;
1343 expr->acc.access[type] =
1344 isl_union_map_preimage_domain_multi_aff(
1345 expr->acc.access[type], isl_multi_aff_copy(ma));
1346 if (!expr->acc.access[type])
1347 break;
1349 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1350 ma);
1351 if (!expr->acc.index || type < pet_expr_access_end)
1352 return pet_expr_free(expr);
1354 return expr;
1355 error:
1356 isl_multi_aff_free(ma);
1357 pet_expr_free(expr);
1358 return NULL;
1361 /* Replace the index expression and access relations (if any) of "expr"
1362 * by their preimages under the function represented by "mpa".
1364 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1365 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1367 enum pet_expr_access_type type;
1369 expr = pet_expr_cow(expr);
1370 if (!expr || !mpa)
1371 goto error;
1372 if (expr->type != pet_expr_access)
1373 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1374 "not an access pet_expr", goto error);
1376 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1377 if (!expr->acc.access[type])
1378 continue;
1379 expr->acc.access[type] =
1380 isl_union_map_preimage_domain_multi_pw_aff(
1381 expr->acc.access[type], isl_multi_pw_aff_copy(mpa));
1382 if (!expr->acc.access[type])
1383 break;
1385 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1386 expr->acc.index, mpa);
1387 if (!expr->acc.index || type < pet_expr_access_end)
1388 return pet_expr_free(expr);
1390 return expr;
1391 error:
1392 isl_multi_pw_aff_free(mpa);
1393 pet_expr_free(expr);
1394 return NULL;
1397 /* Return the index expression of access expression "expr".
1399 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1400 __isl_keep pet_expr *expr)
1402 if (!expr)
1403 return NULL;
1404 if (expr->type != pet_expr_access)
1405 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1406 "not an access expression", return NULL);
1408 return isl_multi_pw_aff_copy(expr->acc.index);
1411 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1413 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1415 isl_space *space;
1416 enum pet_expr_access_type type;
1418 expr = pet_expr_cow(expr);
1419 if (!expr)
1420 return NULL;
1421 if (expr->type != pet_expr_access)
1422 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1423 "not an access expression", return pet_expr_free(expr));
1425 if (!has_any_access_relation(expr))
1426 return expr;
1428 space = isl_multi_pw_aff_get_space(expr->acc.index);
1429 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1430 if (!expr->acc.access[type])
1431 continue;
1432 space = isl_space_align_params(space,
1433 isl_union_map_get_space(expr->acc.access[type]));
1435 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1436 isl_space_copy(space));
1437 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1438 if (!expr->acc.access[type])
1439 continue;
1440 expr->acc.access[type] =
1441 isl_union_map_align_params(expr->acc.access[type],
1442 isl_space_copy(space));
1443 if (!expr->acc.access[type])
1444 break;
1446 isl_space_free(space);
1447 if (!expr->acc.index || type < pet_expr_access_end)
1448 return pet_expr_free(expr);
1450 return expr;
1453 /* Are "expr1" and "expr2" both array accesses such that
1454 * the access relation of "expr1" is a subset of that of "expr2"?
1455 * Only take into account the first "n_arg" arguments.
1457 * This function is tailored for use by mark_self_dependences in nest.c.
1458 * In particular, the input expressions may have more than "n_arg"
1459 * elements in their arguments arrays, while only the first "n_arg"
1460 * elements are referenced from the access relations.
1462 int pet_expr_is_sub_access(__isl_keep pet_expr *expr1,
1463 __isl_keep pet_expr *expr2, int n_arg)
1465 isl_id *id1, *id2;
1466 int i, n1, n2;
1467 int is_subset;
1469 if (!expr1 || !expr2)
1470 return 0;
1471 if (pet_expr_get_type(expr1) != pet_expr_access)
1472 return 0;
1473 if (pet_expr_get_type(expr2) != pet_expr_access)
1474 return 0;
1475 if (pet_expr_is_affine(expr1))
1476 return 0;
1477 if (pet_expr_is_affine(expr2))
1478 return 0;
1479 n1 = pet_expr_get_n_arg(expr1);
1480 if (n1 > n_arg)
1481 n1 = n_arg;
1482 n2 = pet_expr_get_n_arg(expr2);
1483 if (n2 > n_arg)
1484 n2 = n_arg;
1485 if (n1 != n2)
1486 return 0;
1487 for (i = 0; i < n1; ++i) {
1488 int equal;
1489 equal = pet_expr_is_equal(expr1->args[i], expr2->args[i]);
1490 if (equal < 0 || !equal)
1491 return equal;
1493 id1 = pet_expr_access_get_id(expr1);
1494 id2 = pet_expr_access_get_id(expr2);
1495 isl_id_free(id1);
1496 isl_id_free(id2);
1497 if (!id1 || !id2)
1498 return 0;
1499 if (id1 != id2)
1500 return 0;
1502 expr1 = pet_expr_copy(expr1);
1503 expr2 = pet_expr_copy(expr2);
1504 expr1 = introduce_access_relations(expr1);
1505 expr2 = introduce_access_relations(expr2);
1506 if (!expr1 || !expr2)
1507 goto error;
1509 is_subset = isl_union_map_is_subset(
1510 expr1->acc.access[pet_expr_access_may_read],
1511 expr2->acc.access[pet_expr_access_may_read]);
1513 pet_expr_free(expr1);
1514 pet_expr_free(expr2);
1516 return is_subset;
1517 error:
1518 pet_expr_free(expr1);
1519 pet_expr_free(expr2);
1520 return -1;
1523 /* Given a set in the iteration space "domain", extend it to live in the space
1524 * of the domain of access relations.
1526 * That, is the number of arguments "n" is 0, then simply return domain.
1527 * Otherwise, return [domain -> [a_1,...,a_n]].
1529 static __isl_give isl_set *add_arguments(__isl_take isl_set *domain, int n)
1531 isl_map *map;
1533 if (n == 0)
1534 return domain;
1536 map = isl_map_from_domain(domain);
1537 map = isl_map_add_dims(map, isl_dim_out, n);
1538 return isl_map_wrap(map);
1541 /* Add extra conditions to the domains of all access relations in "expr",
1542 * introducing access relations if they are not already present.
1544 * The conditions are not added to the index expression. Instead, they
1545 * are used to try and simplify the index expression.
1547 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1548 __isl_take isl_set *cond)
1550 int i;
1551 isl_union_set *uset;
1552 enum pet_expr_access_type type;
1554 expr = pet_expr_cow(expr);
1555 if (!expr)
1556 goto error;
1558 for (i = 0; i < expr->n_arg; ++i) {
1559 expr->args[i] = pet_expr_restrict(expr->args[i],
1560 isl_set_copy(cond));
1561 if (!expr->args[i])
1562 goto error;
1565 if (expr->type != pet_expr_access) {
1566 isl_set_free(cond);
1567 return expr;
1570 expr = introduce_access_relations(expr);
1571 if (!expr)
1572 goto error;
1574 cond = add_arguments(cond, expr->n_arg);
1575 uset = isl_union_set_from_set(isl_set_copy(cond));
1576 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1577 if (!expr->acc.access[type])
1578 continue;
1579 expr->acc.access[type] =
1580 isl_union_map_intersect_domain(expr->acc.access[type],
1581 isl_union_set_copy(uset));
1582 if (!expr->acc.access[type])
1583 break;
1585 isl_union_set_free(uset);
1586 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, cond);
1587 if (type < pet_expr_access_end || !expr->acc.index)
1588 return pet_expr_free(expr);
1590 return expr;
1591 error:
1592 isl_set_free(cond);
1593 return pet_expr_free(expr);
1596 /* Modify the access relations (if any) and index expression
1597 * of the given access expression
1598 * based on the given iteration space transformation.
1599 * In particular, precompose the access relation and index expression
1600 * with the update function.
1602 * If the access has any arguments then the domain of the access relation
1603 * is a wrapped mapping from the iteration space to the space of
1604 * argument values. We only need to change the domain of this wrapped
1605 * mapping, so we extend the input transformation with an identity mapping
1606 * on the space of argument values.
1608 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1609 __isl_keep isl_multi_pw_aff *update)
1611 enum pet_expr_access_type type;
1613 expr = pet_expr_cow(expr);
1614 if (!expr)
1615 return NULL;
1616 if (expr->type != pet_expr_access)
1617 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1618 "not an access expression", return pet_expr_free(expr));
1620 update = isl_multi_pw_aff_copy(update);
1622 if (expr->n_arg > 0) {
1623 isl_space *space;
1624 isl_multi_pw_aff *id;
1626 space = isl_multi_pw_aff_get_space(expr->acc.index);
1627 space = isl_space_domain(space);
1628 space = isl_space_unwrap(space);
1629 space = isl_space_range(space);
1630 space = isl_space_map_from_set(space);
1631 id = isl_multi_pw_aff_identity(space);
1632 update = isl_multi_pw_aff_product(update, id);
1635 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1636 if (!expr->acc.access[type])
1637 continue;
1638 expr->acc.access[type] =
1639 isl_union_map_preimage_domain_multi_pw_aff(
1640 expr->acc.access[type],
1641 isl_multi_pw_aff_copy(update));
1642 if (!expr->acc.access[type])
1643 break;
1645 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1646 expr->acc.index, update);
1647 if (type < pet_expr_access_end || !expr->acc.index)
1648 return pet_expr_free(expr);
1650 return expr;
1653 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1655 isl_multi_pw_aff *update = user;
1657 return pet_expr_access_update_domain(expr, update);
1660 /* Modify all access relations in "expr" by precomposing them with
1661 * the given iteration space transformation.
1663 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1664 __isl_take isl_multi_pw_aff *update)
1666 expr = pet_expr_map_access(expr, &update_domain, update);
1667 isl_multi_pw_aff_free(update);
1668 return expr;
1671 /* Given an expression with accesses that have a 0D anonymous domain,
1672 * replace those domains by "space".
1674 __isl_give pet_expr *pet_expr_insert_domain(__isl_take pet_expr *expr,
1675 __isl_take isl_space *space)
1677 isl_multi_pw_aff *mpa;
1679 space = isl_space_from_domain(space);
1680 mpa = isl_multi_pw_aff_zero(space);
1681 return pet_expr_update_domain(expr, mpa);
1684 /* Add all parameters in "space" to the access relations (if any)
1685 * and index expression of "expr".
1687 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1689 isl_space *space = user;
1690 enum pet_expr_access_type type;
1692 expr = pet_expr_cow(expr);
1693 if (!expr)
1694 return NULL;
1695 if (expr->type != pet_expr_access)
1696 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1697 "not an access expression", return pet_expr_free(expr));
1699 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1700 if (!expr->acc.access[type])
1701 continue;
1702 expr->acc.access[type] =
1703 isl_union_map_align_params(expr->acc.access[type],
1704 isl_space_copy(space));
1705 if (!expr->acc.access[type])
1706 break;
1708 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1709 isl_space_copy(space));
1710 if (type < pet_expr_access_end || !expr->acc.index)
1711 return pet_expr_free(expr);
1713 return expr;
1716 /* Add all parameters in "space" to all access relations and index expressions
1717 * in "expr".
1719 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1720 __isl_take isl_space *space)
1722 expr = pet_expr_map_access(expr, &align_params, space);
1723 isl_space_free(space);
1724 return expr;
1727 /* Insert an argument expression corresponding to "test" in front
1728 * of the list of arguments described by *n_arg and *args.
1730 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1731 __isl_keep isl_multi_pw_aff *test)
1733 int i;
1734 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1736 if (!test)
1737 return pet_expr_free(expr);
1738 expr = pet_expr_cow(expr);
1739 if (!expr)
1740 return NULL;
1742 if (!expr->args) {
1743 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1744 if (!expr->args)
1745 return pet_expr_free(expr);
1746 } else {
1747 pet_expr **ext;
1748 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1749 if (!ext)
1750 return pet_expr_free(expr);
1751 for (i = 0; i < expr->n_arg; ++i)
1752 ext[1 + i] = expr->args[i];
1753 free(expr->args);
1754 expr->args = ext;
1756 expr->n_arg++;
1757 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1758 if (!expr->args[0])
1759 return pet_expr_free(expr);
1761 return expr;
1764 /* Make the expression "expr" depend on the value of "test"
1765 * being equal to "satisfied".
1767 * If "test" is an affine expression, we simply add the conditions
1768 * on the expression having the value "satisfied" to all access relations
1769 * (introducing access relations if they are missing) and index expressions.
1771 * Otherwise, we add a filter to "expr" (which is then assumed to be
1772 * an access expression) corresponding to "test" being equal to "satisfied".
1774 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
1775 __isl_take isl_multi_pw_aff *test, int satisfied)
1777 isl_id *id;
1778 isl_ctx *ctx;
1779 isl_space *space;
1780 isl_pw_multi_aff *pma;
1781 enum pet_expr_access_type type;
1783 expr = pet_expr_cow(expr);
1784 if (!expr || !test)
1785 goto error;
1787 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
1788 isl_pw_aff *pa;
1789 isl_set *cond;
1791 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
1792 isl_multi_pw_aff_free(test);
1793 if (satisfied)
1794 cond = isl_pw_aff_non_zero_set(pa);
1795 else
1796 cond = isl_pw_aff_zero_set(pa);
1797 return pet_expr_restrict(expr, cond);
1800 ctx = isl_multi_pw_aff_get_ctx(test);
1801 if (expr->type != pet_expr_access)
1802 isl_die(ctx, isl_error_invalid,
1803 "can only filter access expressions", goto error);
1805 expr = introduce_access_relations(expr);
1806 if (!expr)
1807 goto error;
1809 space = isl_space_domain(isl_multi_pw_aff_get_space(expr->acc.index));
1810 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1811 pma = pet_filter_insert_pma(space, id, satisfied);
1813 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1814 if (!expr->acc.access[type])
1815 continue;
1816 expr->acc.access[type] =
1817 isl_union_map_preimage_domain_pw_multi_aff(
1818 expr->acc.access[type],
1819 isl_pw_multi_aff_copy(pma));
1820 if (!expr->acc.access[type])
1821 break;
1823 pma = isl_pw_multi_aff_gist(pma,
1824 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma)));
1825 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
1826 expr->acc.index, pma);
1827 if (type < pet_expr_access_end || !expr->acc.index)
1828 goto error;
1830 expr = insert_access_arg(expr, test);
1832 isl_multi_pw_aff_free(test);
1833 return expr;
1834 error:
1835 isl_multi_pw_aff_free(test);
1836 return pet_expr_free(expr);
1839 /* Add a reference identifier to access expression "expr".
1840 * "user" points to an integer that contains the sequence number
1841 * of the next reference.
1843 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
1844 void *user)
1846 isl_ctx *ctx;
1847 char name[50];
1848 int *n_ref = user;
1850 expr = pet_expr_cow(expr);
1851 if (!expr)
1852 return expr;
1853 if (expr->type != pet_expr_access)
1854 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1855 "not an access expression", return pet_expr_free(expr));
1857 ctx = pet_expr_get_ctx(expr);
1858 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
1859 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
1860 if (!expr->acc.ref_id)
1861 return pet_expr_free(expr);
1863 return expr;
1866 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
1868 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
1871 /* Reset the user pointer on all parameter and tuple ids in
1872 * the access relations (if any) and the index expression
1873 * of the access expression "expr".
1875 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
1876 void *user)
1878 enum pet_expr_access_type type;
1880 expr = pet_expr_cow(expr);
1881 if (!expr)
1882 return expr;
1883 if (expr->type != pet_expr_access)
1884 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1885 "not an access expression", return pet_expr_free(expr));
1887 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1888 if (!expr->acc.access[type])
1889 continue;
1890 expr->acc.access[type] =
1891 isl_union_map_reset_user(expr->acc.access[type]);
1892 if (!expr->acc.access[type])
1893 break;
1895 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
1896 if (type < pet_expr_access_end || !expr->acc.index)
1897 return pet_expr_free(expr);
1899 return expr;
1902 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
1904 return pet_expr_map_access(expr, &access_anonymize, NULL);
1907 /* Data used in access_gist() callback.
1909 struct pet_access_gist_data {
1910 isl_set *domain;
1911 isl_union_map *value_bounds;
1914 /* Given an expression "expr" of type pet_expr_access, compute
1915 * the gist of the associated access relations (if any) and index expression
1916 * with respect to data->domain and the bounds on the values of the arguments
1917 * of the expression.
1919 * The arguments of "expr" have been gisted right before "expr" itself
1920 * is gisted. The gisted arguments may have become equal where before
1921 * they may not have been (obviously) equal. We therefore take
1922 * the opportunity to remove duplicate arguments here.
1924 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
1926 struct pet_access_gist_data *data = user;
1927 isl_set *domain;
1928 isl_union_set *uset;
1929 enum pet_expr_access_type type;
1931 expr = pet_expr_remove_duplicate_args(expr);
1932 expr = pet_expr_cow(expr);
1933 if (!expr)
1934 return expr;
1935 if (expr->type != pet_expr_access)
1936 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1937 "not an access expression", return pet_expr_free(expr));
1939 domain = isl_set_copy(data->domain);
1940 if (expr->n_arg > 0)
1941 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
1942 data->value_bounds);
1944 uset = isl_union_set_from_set(isl_set_copy(domain));
1945 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1946 if (!expr->acc.access[type])
1947 continue;
1948 expr->acc.access[type] =
1949 isl_union_map_gist_domain(expr->acc.access[type],
1950 isl_union_set_copy(uset));
1951 if (!expr->acc.access[type])
1952 break;
1954 isl_union_set_free(uset);
1955 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
1956 if (type < pet_expr_access_end || !expr->acc.index)
1957 return pet_expr_free(expr);
1959 return expr;
1962 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
1963 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
1965 struct pet_access_gist_data data = { context, value_bounds };
1967 return pet_expr_map_access(expr, &access_gist, &data);
1970 /* Mark "expr" as a read dependening on "read".
1972 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
1973 int read)
1975 if (!expr)
1976 return pet_expr_free(expr);
1977 if (expr->type != pet_expr_access)
1978 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1979 "not an access expression", return pet_expr_free(expr));
1980 if (expr->acc.read == read)
1981 return expr;
1982 expr = pet_expr_cow(expr);
1983 if (!expr)
1984 return NULL;
1985 expr->acc.read = read;
1987 return expr;
1990 /* Mark "expr" as a write dependening on "write".
1992 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
1993 int write)
1995 if (!expr)
1996 return pet_expr_free(expr);
1997 if (expr->type != pet_expr_access)
1998 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1999 "not an access expression", return pet_expr_free(expr));
2000 if (expr->acc.write == write)
2001 return expr;
2002 expr = pet_expr_cow(expr);
2003 if (!expr)
2004 return NULL;
2005 expr->acc.write = write;
2007 return expr;
2010 /* Mark "expr" as a kill dependening on "kill".
2012 __isl_give pet_expr *pet_expr_access_set_kill(__isl_take pet_expr *expr,
2013 int kill)
2015 if (!expr)
2016 return pet_expr_free(expr);
2017 if (expr->type != pet_expr_access)
2018 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2019 "not an access expression", return pet_expr_free(expr));
2020 if (expr->acc.kill == kill)
2021 return expr;
2022 expr = pet_expr_cow(expr);
2023 if (!expr)
2024 return NULL;
2025 expr->acc.kill = kill;
2027 return expr;
2030 /* Map the access type "type" to the corresponding location
2031 * in the access array.
2032 * In particular, the access relation of type pet_expr_access_killed is
2033 * stored in the element at position pet_expr_access_fake_killed.
2035 static enum pet_expr_access_type internalize_type(
2036 enum pet_expr_access_type type)
2038 if (type == pet_expr_access_killed)
2039 return pet_expr_access_fake_killed;
2040 return type;
2043 /* Replace the access relation of the given "type" of "expr" by "access".
2044 * If the access relation is non-empty and the type is a read or a write,
2045 * then also mark the access expression itself as a read or a write.
2047 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
2048 enum pet_expr_access_type type, __isl_take isl_union_map *access)
2050 int empty;
2052 expr = pet_expr_cow(expr);
2053 if (!expr || !access)
2054 goto error;
2055 if (expr->type != pet_expr_access)
2056 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2057 "not an access expression", goto error);
2058 type = internalize_type(type);
2059 isl_union_map_free(expr->acc.access[type]);
2060 expr->acc.access[type] = access;
2062 if (expr->acc.kill)
2063 return expr;
2065 empty = isl_union_map_is_empty(access);
2066 if (empty < 0)
2067 return pet_expr_free(expr);
2068 if (empty)
2069 return expr;
2071 if (type == pet_expr_access_may_read)
2072 expr = pet_expr_access_set_read(expr, 1);
2073 else
2074 expr = pet_expr_access_set_write(expr, 1);
2076 return expr;
2077 error:
2078 isl_union_map_free(access);
2079 pet_expr_free(expr);
2080 return NULL;
2083 /* Replace the index expression of "expr" by "index" and
2084 * set the array depth accordingly.
2086 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
2087 __isl_take isl_multi_pw_aff *index)
2089 expr = pet_expr_cow(expr);
2090 if (!expr || !index)
2091 goto error;
2092 if (expr->type != pet_expr_access)
2093 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2094 "not an access expression", goto error);
2095 isl_multi_pw_aff_free(expr->acc.index);
2096 expr->acc.index = index;
2097 expr->acc.depth = isl_multi_pw_aff_dim(index, isl_dim_out);
2099 return expr;
2100 error:
2101 isl_multi_pw_aff_free(index);
2102 pet_expr_free(expr);
2103 return NULL;
2106 /* Return the reference identifier of access expression "expr".
2108 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
2110 if (!expr)
2111 return NULL;
2112 if (expr->type != pet_expr_access)
2113 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2114 "not an access expression", return NULL);
2116 return isl_id_copy(expr->acc.ref_id);
2119 /* Replace the reference identifier of access expression "expr" by "ref_id".
2121 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
2122 __isl_take isl_id *ref_id)
2124 expr = pet_expr_cow(expr);
2125 if (!expr || !ref_id)
2126 goto error;
2127 if (expr->type != pet_expr_access)
2128 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2129 "not an access expression", goto error);
2130 isl_id_free(expr->acc.ref_id);
2131 expr->acc.ref_id = ref_id;
2133 return expr;
2134 error:
2135 isl_id_free(ref_id);
2136 pet_expr_free(expr);
2137 return NULL;
2140 /* Tag the access relation "access" with "id".
2141 * That is, insert the id as the range of a wrapped relation
2142 * in the domain of "access".
2144 * If "access" is of the form
2146 * D[i] -> A[a]
2148 * then the result is of the form
2150 * [D[i] -> id[]] -> A[a]
2152 __isl_give isl_union_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
2153 __isl_take isl_union_map *access)
2155 isl_space *space;
2156 isl_multi_aff *add_tag;
2157 isl_id *id;
2159 if (expr->type != pet_expr_access)
2160 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2161 "not an access expression",
2162 return isl_union_map_free(access));
2164 id = isl_id_copy(expr->acc.ref_id);
2165 space = pet_expr_access_get_domain_space(expr);
2166 space = isl_space_from_domain(space);
2167 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2168 add_tag = isl_multi_aff_domain_map(space);
2169 access = isl_union_map_preimage_domain_multi_aff(access, add_tag);
2171 return access;
2174 /* Return the access relation of the given "type" associated to "expr"
2175 * that maps pairs of domain iterations and argument values
2176 * to the corresponding accessed data elements.
2178 * If the requested access relation is explicitly available,
2179 * then return a copy. Otherwise, check if it is irrelevant for
2180 * the access expression and return an empty relation if this is the case.
2181 * Otherwise, introduce the requested access relation in "expr" and
2182 * return a copy.
2184 __isl_give isl_union_map *pet_expr_access_get_dependent_access(
2185 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2187 isl_union_map *access;
2188 int empty;
2190 if (!expr)
2191 return NULL;
2192 if (expr->type != pet_expr_access)
2193 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2194 "not an access expression", return NULL);
2196 type = internalize_type(type);
2197 if (expr->acc.access[type])
2198 return isl_union_map_copy(expr->acc.access[type]);
2200 if (type == pet_expr_access_may_read)
2201 empty = !expr->acc.read;
2202 else
2203 empty = !expr->acc.write;
2205 if (!empty) {
2206 expr = pet_expr_copy(expr);
2207 expr = introduce_access_relations(expr);
2208 if (!expr)
2209 return NULL;
2210 access = isl_union_map_copy(expr->acc.access[type]);
2211 pet_expr_free(expr);
2213 return access;
2216 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr));
2219 /* Return the may read access relation associated to "expr"
2220 * that maps pairs of domain iterations and argument values
2221 * to the corresponding accessed data elements.
2223 __isl_give isl_union_map *pet_expr_access_get_dependent_may_read(
2224 __isl_keep pet_expr *expr)
2226 return pet_expr_access_get_dependent_access(expr,
2227 pet_expr_access_may_read);
2230 /* Return the may write access relation associated to "expr"
2231 * that maps pairs of domain iterations and argument values
2232 * to the corresponding accessed data elements.
2234 __isl_give isl_union_map *pet_expr_access_get_dependent_may_write(
2235 __isl_keep pet_expr *expr)
2237 return pet_expr_access_get_dependent_access(expr,
2238 pet_expr_access_may_write);
2241 /* Return the must write access relation associated to "expr"
2242 * that maps pairs of domain iterations and argument values
2243 * to the corresponding accessed data elements.
2245 __isl_give isl_union_map *pet_expr_access_get_dependent_must_write(
2246 __isl_keep pet_expr *expr)
2248 return pet_expr_access_get_dependent_access(expr,
2249 pet_expr_access_must_write);
2252 /* Return the relation of the given "type" mapping domain iterations
2253 * to the accessed data elements.
2254 * In particular, take the access relation and, in case of may_read
2255 * or may_write, project out the values of the arguments, if any.
2256 * In case of must_write, return the empty relation if there are
2257 * any arguments.
2259 __isl_give isl_union_map *pet_expr_access_get_access(__isl_keep pet_expr *expr,
2260 enum pet_expr_access_type type)
2262 isl_union_map *access;
2263 isl_space *space;
2264 isl_map *map;
2266 if (!expr)
2267 return NULL;
2268 if (expr->type != pet_expr_access)
2269 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2270 "not an access expression", return NULL);
2272 if (expr->n_arg != 0 && type == pet_expr_access_must_write) {
2273 space = pet_expr_access_get_parameter_space(expr);
2274 return isl_union_map_empty(space);
2277 access = pet_expr_access_get_dependent_access(expr, type);
2278 if (expr->n_arg == 0)
2279 return access;
2281 space = isl_multi_pw_aff_get_space(expr->acc.index);
2282 space = isl_space_domain(space);
2283 map = isl_map_universe(isl_space_unwrap(space));
2284 map = isl_map_domain_map(map);
2285 access = isl_union_map_apply_domain(access,
2286 isl_union_map_from_map(map));
2288 return access;
2291 /* Return the relation mapping domain iterations to all possibly
2292 * read data elements.
2294 __isl_give isl_union_map *pet_expr_access_get_may_read(
2295 __isl_keep pet_expr *expr)
2297 return pet_expr_access_get_access(expr, pet_expr_access_may_read);
2300 /* Return the relation mapping domain iterations to all possibly
2301 * written data elements.
2303 __isl_give isl_union_map *pet_expr_access_get_may_write(
2304 __isl_keep pet_expr *expr)
2306 return pet_expr_access_get_access(expr, pet_expr_access_may_write);
2309 /* Return a relation mapping domain iterations to definitely
2310 * written data elements, assuming the statement containing
2311 * the expression is executed.
2313 __isl_give isl_union_map *pet_expr_access_get_must_write(
2314 __isl_keep pet_expr *expr)
2316 return pet_expr_access_get_access(expr, pet_expr_access_must_write);
2319 /* Return the relation of the given "type" mapping domain iterations to
2320 * accessed data elements, with its domain tagged with the reference
2321 * identifier.
2323 static __isl_give isl_union_map *pet_expr_access_get_tagged_access(
2324 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2326 isl_union_map *access;
2328 if (!expr)
2329 return NULL;
2331 access = pet_expr_access_get_access(expr, type);
2332 access = pet_expr_tag_access(expr, access);
2334 return access;
2337 /* Return the relation mapping domain iterations to all possibly
2338 * read data elements, with its domain tagged with the reference
2339 * identifier.
2341 __isl_give isl_union_map *pet_expr_access_get_tagged_may_read(
2342 __isl_keep pet_expr *expr)
2344 return pet_expr_access_get_tagged_access(expr,
2345 pet_expr_access_may_read);
2348 /* Return the relation mapping domain iterations to all possibly
2349 * written data elements, with its domain tagged with the reference
2350 * identifier.
2352 __isl_give isl_union_map *pet_expr_access_get_tagged_may_write(
2353 __isl_keep pet_expr *expr)
2355 return pet_expr_access_get_tagged_access(expr,
2356 pet_expr_access_may_write);
2359 /* Return the operation type of operation expression "expr".
2361 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
2363 if (!expr)
2364 return pet_op_last;
2365 if (expr->type != pet_expr_op)
2366 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2367 "not an operation expression", return pet_op_last);
2369 return expr->op;
2372 /* Replace the operation type of operation expression "expr" by "type".
2374 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
2375 enum pet_op_type type)
2377 if (!expr)
2378 return pet_expr_free(expr);
2379 if (expr->type != pet_expr_op)
2380 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2381 "not an operation expression",
2382 return pet_expr_free(expr));
2383 if (expr->op == type)
2384 return expr;
2385 expr = pet_expr_cow(expr);
2386 if (!expr)
2387 return NULL;
2388 expr->op = type;
2390 return expr;
2393 /* Return the name of the function called by "expr".
2395 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
2397 if (!expr)
2398 return NULL;
2399 if (expr->type != pet_expr_call)
2400 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2401 "not a call expression", return NULL);
2402 return expr->c.name;
2405 /* Replace the name of the function called by "expr" by "name".
2407 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
2408 __isl_keep const char *name)
2410 expr = pet_expr_cow(expr);
2411 if (!expr || !name)
2412 return pet_expr_free(expr);
2413 if (expr->type != pet_expr_call)
2414 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2415 "not a call expression", return pet_expr_free(expr));
2416 free(expr->c.name);
2417 expr->c.name = strdup(name);
2418 if (!expr->c.name)
2419 return pet_expr_free(expr);
2420 return expr;
2423 /* Does the call expression "expr" have an associated function summary?
2425 int pet_expr_call_has_summary(__isl_keep pet_expr *expr)
2427 if (!expr)
2428 return -1;
2429 if (expr->type != pet_expr_call)
2430 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2431 "not a call expression", return -1);
2433 return expr->c.summary != NULL;
2436 /* Return a copy of the function summary associated to
2437 * the call expression "expr".
2439 __isl_give pet_function_summary *pet_expr_call_get_summary(
2440 __isl_keep pet_expr *expr)
2442 if (!expr)
2443 return NULL;
2444 if (expr->type != pet_expr_call)
2445 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2446 "not a call expression", return NULL);
2448 return pet_function_summary_copy(expr->c.summary);
2451 /* Replace the function summary associated to the call expression "expr"
2452 * by "summary".
2454 __isl_give pet_expr *pet_expr_call_set_summary(__isl_take pet_expr *expr,
2455 __isl_take pet_function_summary *summary)
2457 expr = pet_expr_cow(expr);
2458 if (!expr || !summary)
2459 goto error;
2460 if (expr->type != pet_expr_call)
2461 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2462 "not a call expression", goto error);
2463 pet_function_summary_free(expr->c.summary);
2464 expr->c.summary = summary;
2465 return expr;
2466 error:
2467 pet_function_summary_free(summary);
2468 return pet_expr_free(expr);
2471 /* Replace the type of the cast performed by "expr" by "name".
2473 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
2474 __isl_keep const char *name)
2476 expr = pet_expr_cow(expr);
2477 if (!expr || !name)
2478 return pet_expr_free(expr);
2479 if (expr->type != pet_expr_cast)
2480 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2481 "not a cast expression", return pet_expr_free(expr));
2482 free(expr->type_name);
2483 expr->type_name = strdup(name);
2484 if (!expr->type_name)
2485 return pet_expr_free(expr);
2486 return expr;
2489 /* Return the value of the integer represented by "expr".
2491 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
2493 if (!expr)
2494 return NULL;
2495 if (expr->type != pet_expr_int)
2496 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2497 "not an int expression", return NULL);
2499 return isl_val_copy(expr->i);
2502 /* Replace the value of the integer represented by "expr" by "v".
2504 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
2505 __isl_take isl_val *v)
2507 expr = pet_expr_cow(expr);
2508 if (!expr || !v)
2509 goto error;
2510 if (expr->type != pet_expr_int)
2511 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2512 "not an int expression", goto error);
2513 isl_val_free(expr->i);
2514 expr->i = v;
2516 return expr;
2517 error:
2518 isl_val_free(v);
2519 pet_expr_free(expr);
2520 return NULL;
2523 /* Replace the value and string representation of the double
2524 * represented by "expr" by "d" and "s".
2526 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2527 double d, __isl_keep const char *s)
2529 expr = pet_expr_cow(expr);
2530 if (!expr || !s)
2531 return pet_expr_free(expr);
2532 if (expr->type != pet_expr_double)
2533 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2534 "not a double expression", return pet_expr_free(expr));
2535 expr->d.val = d;
2536 free(expr->d.s);
2537 expr->d.s = strdup(s);
2538 if (!expr->d.s)
2539 return pet_expr_free(expr);
2540 return expr;
2543 /* Return a string representation of the double expression "expr".
2545 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2547 if (!expr)
2548 return NULL;
2549 if (expr->type != pet_expr_double)
2550 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2551 "not a double expression", return NULL);
2552 return strdup(expr->d.s);
2555 /* Return a piecewise affine expression defined on the specified domain
2556 * that represents NaN.
2558 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2560 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2563 /* This function is called when we come across an access that is
2564 * nested in what is supposed to be an affine expression.
2565 * "pc" is the context in which the affine expression is created.
2566 * If nesting is allowed in "pc", we return an affine expression that is
2567 * equal to a new parameter corresponding to this nested access.
2568 * Otherwise, we return NaN.
2570 * Note that we currently don't allow nested accesses themselves
2571 * to contain any nested accesses, so we check if "expr" itself
2572 * involves any nested accesses (either explicitly as arguments
2573 * or implicitly through parameters) and return NaN if it does.
2575 * The new parameter is resolved in resolve_nested.
2577 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2578 __isl_keep pet_context *pc)
2580 isl_ctx *ctx;
2581 isl_id *id;
2582 isl_space *space;
2583 isl_local_space *ls;
2584 isl_aff *aff;
2585 int nested;
2587 if (!expr || !pc)
2588 return NULL;
2589 if (!pet_context_allow_nesting(pc))
2590 return non_affine(pet_context_get_space(pc));
2592 if (pet_expr_get_type(expr) != pet_expr_access)
2593 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2594 "not an access expression", return NULL);
2596 if (expr->n_arg > 0)
2597 return non_affine(pet_context_get_space(pc));
2599 space = pet_expr_access_get_parameter_space(expr);
2600 nested = pet_nested_any_in_space(space);
2601 isl_space_free(space);
2602 if (nested)
2603 return non_affine(pet_context_get_space(pc));
2605 ctx = pet_expr_get_ctx(expr);
2606 id = pet_nested_pet_expr(pet_expr_copy(expr));
2607 space = pet_context_get_space(pc);
2608 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2610 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2611 ls = isl_local_space_from_space(space);
2612 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2614 return isl_pw_aff_from_aff(aff);
2617 /* Extract an affine expression from the access pet_expr "expr".
2618 * "pc" is the context in which the affine expression is created.
2620 * If "expr" is actually an affine expression rather than
2621 * a real access, then we return that expression.
2622 * Otherwise, we require that "expr" is of an integral type.
2623 * If not, we return NaN.
2625 * If the variable has been assigned a known affine expression,
2626 * then we return that expression.
2628 * Otherwise, we return an expression that is equal to a parameter
2629 * representing "expr" (if "allow_nested" is set).
2631 static __isl_give isl_pw_aff *extract_affine_from_access(
2632 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2634 int pos;
2635 isl_id *id;
2637 if (pet_expr_is_affine(expr)) {
2638 isl_pw_aff *pa;
2639 isl_multi_pw_aff *mpa;
2641 mpa = pet_expr_access_get_index(expr);
2642 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
2643 isl_multi_pw_aff_free(mpa);
2644 return pa;
2647 if (pet_expr_get_type_size(expr) == 0)
2648 return non_affine(pet_context_get_space(pc));
2650 if (!pet_expr_is_scalar_access(expr))
2651 return nested_access(expr, pc);
2653 id = pet_expr_access_get_id(expr);
2654 if (pet_context_is_assigned(pc, id))
2655 return pet_context_get_value(pc, id);
2657 isl_id_free(id);
2658 return nested_access(expr, pc);
2661 /* Construct an affine expression from the integer constant "expr".
2662 * "pc" is the context in which the affine expression is created.
2664 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2665 __isl_keep pet_context *pc)
2667 isl_local_space *ls;
2668 isl_aff *aff;
2670 if (!expr)
2671 return NULL;
2673 ls = isl_local_space_from_space(pet_context_get_space(pc));
2674 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2676 return isl_pw_aff_from_aff(aff);
2679 /* Extract an affine expression from an addition or subtraction operation.
2680 * Return NaN if we are unable to extract an affine expression.
2682 * "pc" is the context in which the affine expression is created.
2684 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2685 __isl_keep pet_context *pc)
2687 isl_pw_aff *lhs;
2688 isl_pw_aff *rhs;
2690 if (!expr)
2691 return NULL;
2692 if (expr->n_arg != 2)
2693 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2694 "expecting two arguments", return NULL);
2696 lhs = pet_expr_extract_affine(expr->args[0], pc);
2697 rhs = pet_expr_extract_affine(expr->args[1], pc);
2699 switch (pet_expr_op_get_type(expr)) {
2700 case pet_op_add:
2701 return isl_pw_aff_add(lhs, rhs);
2702 case pet_op_sub:
2703 return isl_pw_aff_sub(lhs, rhs);
2704 default:
2705 isl_pw_aff_free(lhs);
2706 isl_pw_aff_free(rhs);
2707 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2708 "not an addition or subtraction operation",
2709 return NULL);
2714 /* Extract an affine expression from an integer division or a modulo operation.
2715 * Return NaN if we are unable to extract an affine expression.
2717 * "pc" is the context in which the affine expression is created.
2719 * In particular, if "expr" is lhs/rhs, then return
2721 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2723 * If "expr" is lhs%rhs, then return
2725 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2727 * If the second argument (rhs) is not a (positive) integer constant,
2728 * then we fail to extract an affine expression.
2730 * We simplify the result in the context of the domain of "pc" in case
2731 * this domain implies that lhs >= 0 (or < 0).
2733 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2734 __isl_keep pet_context *pc)
2736 int is_cst;
2737 isl_pw_aff *lhs;
2738 isl_pw_aff *rhs;
2739 isl_pw_aff *res;
2741 if (!expr)
2742 return NULL;
2743 if (expr->n_arg != 2)
2744 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2745 "expecting two arguments", return NULL);
2747 rhs = pet_expr_extract_affine(expr->args[1], pc);
2749 is_cst = isl_pw_aff_is_cst(rhs);
2750 if (is_cst < 0 || !is_cst) {
2751 isl_pw_aff_free(rhs);
2752 return non_affine(pet_context_get_space(pc));
2755 lhs = pet_expr_extract_affine(expr->args[0], pc);
2757 switch (pet_expr_op_get_type(expr)) {
2758 case pet_op_div:
2759 res = isl_pw_aff_tdiv_q(lhs, rhs);
2760 break;
2761 case pet_op_mod:
2762 res = isl_pw_aff_tdiv_r(lhs, rhs);
2763 break;
2764 default:
2765 isl_pw_aff_free(lhs);
2766 isl_pw_aff_free(rhs);
2767 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2768 "not a div or mod operator", return NULL);
2771 return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc));
2774 /* Extract an affine expression from a multiplication operation.
2775 * Return NaN if we are unable to extract an affine expression.
2776 * In particular, if neither of the arguments is a (piecewise) constant
2777 * then we return NaN.
2779 * "pc" is the context in which the affine expression is created.
2781 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2782 __isl_keep pet_context *pc)
2784 int lhs_cst, rhs_cst;
2785 isl_pw_aff *lhs;
2786 isl_pw_aff *rhs;
2788 if (!expr)
2789 return NULL;
2790 if (expr->n_arg != 2)
2791 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2792 "expecting two arguments", return NULL);
2794 lhs = pet_expr_extract_affine(expr->args[0], pc);
2795 rhs = pet_expr_extract_affine(expr->args[1], pc);
2797 lhs_cst = isl_pw_aff_is_cst(lhs);
2798 rhs_cst = isl_pw_aff_is_cst(rhs);
2799 if (lhs_cst >= 0 && rhs_cst >= 0 && (lhs_cst || rhs_cst))
2800 return isl_pw_aff_mul(lhs, rhs);
2802 isl_pw_aff_free(lhs);
2803 isl_pw_aff_free(rhs);
2805 if (lhs_cst < 0 || rhs_cst < 0)
2806 return NULL;
2808 return non_affine(pet_context_get_space(pc));
2811 /* Extract an affine expression from a negation operation.
2812 * Return NaN if we are unable to extract an affine expression.
2814 * "pc" is the context in which the affine expression is created.
2816 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
2817 __isl_keep pet_context *pc)
2819 isl_pw_aff *res;
2821 if (!expr)
2822 return NULL;
2823 if (expr->n_arg != 1)
2824 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2825 "expecting one argument", return NULL);
2827 res = pet_expr_extract_affine(expr->args[0], pc);
2828 return isl_pw_aff_neg(res);
2831 /* Extract an affine expression from a conditional operation.
2832 * Return NaN if we are unable to extract an affine expression.
2834 * "pc" is the context in which the affine expression is created.
2836 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
2837 __isl_keep pet_context *pc)
2839 isl_pw_aff *cond, *lhs, *rhs;
2841 if (!expr)
2842 return NULL;
2843 if (expr->n_arg != 3)
2844 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2845 "expecting three arguments", return NULL);
2847 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2848 lhs = pet_expr_extract_affine(expr->args[1], pc);
2849 rhs = pet_expr_extract_affine(expr->args[2], pc);
2851 return isl_pw_aff_cond(cond, lhs, rhs);
2854 /* Compute
2856 * pwaff mod 2^width
2858 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff, unsigned width)
2860 isl_ctx *ctx;
2861 isl_val *mod;
2863 ctx = isl_pw_aff_get_ctx(pwaff);
2864 mod = isl_val_int_from_ui(ctx, width);
2865 mod = isl_val_2exp(mod);
2867 pwaff = isl_pw_aff_mod_val(pwaff, mod);
2869 return pwaff;
2872 /* Limit the domain of "pwaff" to those elements where the function
2873 * value satisfies
2875 * 2^{width-1} <= pwaff < 2^{width-1}
2877 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
2878 unsigned width)
2880 isl_ctx *ctx;
2881 isl_val *v;
2882 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
2883 isl_local_space *ls = isl_local_space_from_space(space);
2884 isl_aff *bound;
2885 isl_set *dom;
2886 isl_pw_aff *b;
2888 ctx = isl_pw_aff_get_ctx(pwaff);
2889 v = isl_val_int_from_ui(ctx, width - 1);
2890 v = isl_val_2exp(v);
2892 bound = isl_aff_zero_on_domain(ls);
2893 bound = isl_aff_add_constant_val(bound, v);
2894 b = isl_pw_aff_from_aff(bound);
2896 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
2897 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2899 b = isl_pw_aff_neg(b);
2900 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
2901 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2903 return pwaff;
2906 /* Handle potential overflows on signed computations.
2908 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2909 * then we adjust the domain of "pa" to avoid overflows.
2911 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
2912 unsigned width)
2914 isl_ctx *ctx;
2915 struct pet_options *options;
2917 if (!pa)
2918 return NULL;
2920 ctx = isl_pw_aff_get_ctx(pa);
2921 options = isl_ctx_peek_pet_options(ctx);
2922 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
2923 pa = avoid_overflow(pa, width);
2925 return pa;
2928 /* Extract an affine expression from some an operation.
2929 * Return NaN if we are unable to extract an affine expression.
2930 * If the result of a binary (non boolean) operation is unsigned,
2931 * then we wrap it based on the size of the type. If the result is signed,
2932 * then we ensure that no overflow occurs.
2934 * "pc" is the context in which the affine expression is created.
2936 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
2937 __isl_keep pet_context *pc)
2939 isl_pw_aff *res;
2940 int type_size;
2942 switch (pet_expr_op_get_type(expr)) {
2943 case pet_op_add:
2944 case pet_op_sub:
2945 res = extract_affine_add_sub(expr, pc);
2946 break;
2947 case pet_op_div:
2948 case pet_op_mod:
2949 res = extract_affine_div_mod(expr, pc);
2950 break;
2951 case pet_op_mul:
2952 res = extract_affine_mul(expr, pc);
2953 break;
2954 case pet_op_minus:
2955 return extract_affine_neg(expr, pc);
2956 case pet_op_cond:
2957 return extract_affine_cond(expr, pc);
2958 case pet_op_eq:
2959 case pet_op_ne:
2960 case pet_op_le:
2961 case pet_op_ge:
2962 case pet_op_lt:
2963 case pet_op_gt:
2964 case pet_op_land:
2965 case pet_op_lor:
2966 case pet_op_lnot:
2967 return pet_expr_extract_affine_condition(expr, pc);
2968 default:
2969 return non_affine(pet_context_get_space(pc));
2972 if (!res)
2973 return NULL;
2974 if (isl_pw_aff_involves_nan(res)) {
2975 isl_space *space = isl_pw_aff_get_domain_space(res);
2976 isl_pw_aff_free(res);
2977 return non_affine(space);
2980 type_size = pet_expr_get_type_size(expr);
2981 if (type_size > 0)
2982 res = wrap(res, type_size);
2983 else
2984 res = signed_overflow(res, -type_size);
2986 return res;
2989 /* Extract an affine expression from some special function calls.
2990 * Return NaN if we are unable to extract an affine expression.
2991 * In particular, we handle "min", "max", "ceild", "floord",
2992 * "intMod", "intFloor" and "intCeil".
2993 * In case of the latter five, the second argument needs to be
2994 * a (positive) integer constant.
2996 * "pc" is the context in which the affine expression is created.
2998 static __isl_give isl_pw_aff *extract_affine_from_call(
2999 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3001 isl_pw_aff *aff1, *aff2;
3002 int n;
3003 const char *name;
3005 n = pet_expr_get_n_arg(expr);
3006 name = pet_expr_call_get_name(expr);
3007 if (!(n == 2 && !strcmp(name, "min")) &&
3008 !(n == 2 && !strcmp(name, "max")) &&
3009 !(n == 2 && !strcmp(name, "intMod")) &&
3010 !(n == 2 && !strcmp(name, "intFloor")) &&
3011 !(n == 2 && !strcmp(name, "intCeil")) &&
3012 !(n == 2 && !strcmp(name, "floord")) &&
3013 !(n == 2 && !strcmp(name, "ceild")))
3014 return non_affine(pet_context_get_space(pc));
3016 if (!strcmp(name, "min") || !strcmp(name, "max")) {
3017 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3018 aff2 = pet_expr_extract_affine(expr->args[1], pc);
3020 if (!strcmp(name, "min"))
3021 aff1 = isl_pw_aff_min(aff1, aff2);
3022 else
3023 aff1 = isl_pw_aff_max(aff1, aff2);
3024 } else if (!strcmp(name, "intMod")) {
3025 isl_val *v;
3027 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3028 return non_affine(pet_context_get_space(pc));
3029 v = pet_expr_int_get_val(expr->args[1]);
3030 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3031 aff1 = isl_pw_aff_mod_val(aff1, v);
3032 } else {
3033 isl_val *v;
3035 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3036 return non_affine(pet_context_get_space(pc));
3037 v = pet_expr_int_get_val(expr->args[1]);
3038 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3039 aff1 = isl_pw_aff_scale_down_val(aff1, v);
3040 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
3041 aff1 = isl_pw_aff_floor(aff1);
3042 else
3043 aff1 = isl_pw_aff_ceil(aff1);
3046 return aff1;
3049 /* Extract an affine expression from "expr", if possible.
3050 * Otherwise return NaN.
3052 * "pc" is the context in which the affine expression is created.
3054 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
3055 __isl_keep pet_context *pc)
3057 if (!expr)
3058 return NULL;
3060 switch (pet_expr_get_type(expr)) {
3061 case pet_expr_access:
3062 return extract_affine_from_access(expr, pc);
3063 case pet_expr_int:
3064 return extract_affine_from_int(expr, pc);
3065 case pet_expr_op:
3066 return extract_affine_from_op(expr, pc);
3067 case pet_expr_call:
3068 return extract_affine_from_call(expr, pc);
3069 case pet_expr_cast:
3070 case pet_expr_double:
3071 case pet_expr_error:
3072 return non_affine(pet_context_get_space(pc));
3076 /* Extract an affine expressions representing the comparison "LHS op RHS"
3077 * Return NaN if we are unable to extract such an affine expression.
3079 * "pc" is the context in which the affine expression is created.
3081 * If the comparison is of the form
3083 * a <= min(b,c)
3085 * then the expression is constructed as the conjunction of
3086 * the comparisons
3088 * a <= b and a <= c
3090 * A similar optimization is performed for max(a,b) <= c.
3091 * We do this because that will lead to simpler representations
3092 * of the expression.
3093 * If isl is ever enhanced to explicitly deal with min and max expressions,
3094 * this optimization can be removed.
3096 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
3097 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
3098 __isl_keep pet_context *pc)
3100 isl_pw_aff *lhs_pa, *rhs_pa;
3102 if (op == pet_op_gt)
3103 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
3104 if (op == pet_op_ge)
3105 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
3107 if (op == pet_op_lt || op == pet_op_le) {
3108 if (pet_expr_is_min(rhs)) {
3109 lhs_pa = pet_expr_extract_comparison(op, lhs,
3110 rhs->args[0], pc);
3111 rhs_pa = pet_expr_extract_comparison(op, lhs,
3112 rhs->args[1], pc);
3113 return pet_and(lhs_pa, rhs_pa);
3115 if (pet_expr_is_max(lhs)) {
3116 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
3117 rhs, pc);
3118 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
3119 rhs, pc);
3120 return pet_and(lhs_pa, rhs_pa);
3124 lhs_pa = pet_expr_extract_affine(lhs, pc);
3125 rhs_pa = pet_expr_extract_affine(rhs, pc);
3127 return pet_comparison(op, lhs_pa, rhs_pa);
3130 /* Extract an affine expressions from the comparison "expr".
3131 * Return NaN if we are unable to extract such an affine expression.
3133 * "pc" is the context in which the affine expression is created.
3135 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
3136 __isl_keep pet_context *pc)
3138 enum pet_op_type type;
3140 if (!expr)
3141 return NULL;
3142 if (expr->n_arg != 2)
3143 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3144 "expecting two arguments", return NULL);
3146 type = pet_expr_op_get_type(expr);
3147 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
3148 pc);
3151 /* Extract an affine expression representing the boolean operation
3152 * expressed by "expr".
3153 * Return NaN if we are unable to extract an affine expression.
3155 * "pc" is the context in which the affine expression is created.
3157 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
3158 __isl_keep pet_context *pc)
3160 isl_pw_aff *lhs, *rhs;
3161 int n;
3163 if (!expr)
3164 return NULL;
3166 n = pet_expr_get_n_arg(expr);
3167 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
3168 if (n == 1)
3169 return pet_not(lhs);
3171 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
3172 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
3175 /* Extract the affine expression "expr != 0 ? 1 : 0".
3176 * Return NaN if we are unable to extract an affine expression.
3178 * "pc" is the context in which the affine expression is created.
3180 static __isl_give isl_pw_aff *extract_implicit_condition(
3181 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3183 isl_pw_aff *res;
3185 res = pet_expr_extract_affine(expr, pc);
3186 return pet_to_bool(res);
3189 /* Extract a boolean affine expression from "expr".
3190 * Return NaN if we are unable to extract an affine expression.
3192 * "pc" is the context in which the affine expression is created.
3194 * If "expr" is neither a comparison nor a boolean operation,
3195 * then we assume it is an affine expression and return the
3196 * boolean expression "expr != 0 ? 1 : 0".
3198 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
3199 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3201 if (!expr)
3202 return NULL;
3204 if (pet_expr_is_comparison(expr))
3205 return extract_comparison(expr, pc);
3206 if (pet_expr_is_boolean(expr))
3207 return extract_boolean(expr, pc);
3209 return extract_implicit_condition(expr, pc);
3212 /* Check if "expr" is an assume expression and if its single argument
3213 * can be converted to an affine expression in the context of "pc".
3214 * If so, replace the argument by the affine expression.
3216 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
3217 __isl_keep pet_context *pc)
3219 isl_pw_aff *cond;
3220 isl_multi_pw_aff *index;
3222 if (!expr)
3223 return NULL;
3224 if (!pet_expr_is_assume(expr))
3225 return expr;
3226 if (expr->n_arg != 1)
3227 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3228 "expecting one argument", return pet_expr_free(expr));
3230 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3231 if (!cond)
3232 return pet_expr_free(expr);
3233 if (isl_pw_aff_involves_nan(cond)) {
3234 isl_pw_aff_free(cond);
3235 return expr;
3238 index = isl_multi_pw_aff_from_pw_aff(cond);
3239 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
3241 return expr;
3244 /* Return the number of bits needed to represent the type of "expr".
3245 * See the description of the type_size field of pet_expr.
3247 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
3249 return expr ? expr->type_size : 0;
3252 /* Replace the number of bits needed to represent the type of "expr"
3253 * by "type_size".
3254 * See the description of the type_size field of pet_expr.
3256 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
3257 int type_size)
3259 expr = pet_expr_cow(expr);
3260 if (!expr)
3261 return NULL;
3263 expr->type_size = type_size;
3265 return expr;
3268 /* Extend an access expression "expr" with an additional index "index".
3269 * In particular, add "index" as an extra argument to "expr" and
3270 * adjust the index expression of "expr" to refer to this extra argument.
3271 * The caller is responsible for calling pet_expr_access_set_depth
3272 * to update the corresponding access relation.
3274 * Note that we only collect the individual index expressions as
3275 * arguments of "expr" here.
3276 * An attempt to integrate them into the index expression of "expr"
3277 * is performed in pet_expr_access_plug_in_args.
3279 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
3280 __isl_take pet_expr *index)
3282 int n;
3283 isl_space *space;
3284 isl_local_space *ls;
3285 isl_pw_aff *pa;
3287 expr = pet_expr_cow(expr);
3288 if (!expr || !index)
3289 goto error;
3290 if (expr->type != pet_expr_access)
3291 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3292 "not an access pet_expr", goto error);
3294 n = pet_expr_get_n_arg(expr);
3295 expr = pet_expr_insert_arg(expr, n, index);
3296 if (!expr)
3297 return NULL;
3299 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3300 ls = isl_local_space_from_space(space);
3301 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
3302 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
3303 if (!expr->acc.index)
3304 return pet_expr_free(expr);
3306 return expr;
3307 error:
3308 pet_expr_free(expr);
3309 pet_expr_free(index);
3310 return NULL;
3313 /* Extend an access expression "expr" with an additional member acces to "id".
3314 * In particular, extend the index expression of "expr" to include
3315 * the additional member access.
3316 * The caller is responsible for calling pet_expr_access_set_depth
3317 * to update the corresponding access relation.
3319 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
3320 __isl_take isl_id *id)
3322 isl_space *space;
3323 isl_multi_pw_aff *field_access;
3325 expr = pet_expr_cow(expr);
3326 if (!expr || !id)
3327 goto error;
3328 if (expr->type != pet_expr_access)
3329 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3330 "not an access pet_expr", goto error);
3332 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3333 space = isl_space_from_domain(space);
3334 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3335 field_access = isl_multi_pw_aff_zero(space);
3336 expr->acc.index = pet_array_member(expr->acc.index, field_access);
3337 if (!expr->acc.index)
3338 return pet_expr_free(expr);
3340 return expr;
3341 error:
3342 pet_expr_free(expr);
3343 isl_id_free(id);
3344 return NULL;
3347 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
3349 int i;
3351 if (!expr)
3352 return;
3354 fprintf(stderr, "%*s", indent, "");
3356 switch (expr->type) {
3357 case pet_expr_double:
3358 fprintf(stderr, "%s\n", expr->d.s);
3359 break;
3360 case pet_expr_int:
3361 isl_val_dump(expr->i);
3362 break;
3363 case pet_expr_access:
3364 if (expr->acc.ref_id) {
3365 isl_id_dump(expr->acc.ref_id);
3366 fprintf(stderr, "%*s", indent, "");
3368 isl_multi_pw_aff_dump(expr->acc.index);
3369 fprintf(stderr, "%*sdepth: %d\n", indent + 2,
3370 "", expr->acc.depth);
3371 if (expr->acc.kill) {
3372 fprintf(stderr, "%*skill: 1\n", indent + 2, "");
3373 } else {
3374 fprintf(stderr, "%*sread: %d\n", indent + 2,
3375 "", expr->acc.read);
3376 fprintf(stderr, "%*swrite: %d\n", indent + 2,
3377 "", expr->acc.write);
3379 if (expr->acc.access[pet_expr_access_may_read]) {
3380 fprintf(stderr, "%*smay_read: ", indent + 2, "");
3381 isl_union_map_dump(
3382 expr->acc.access[pet_expr_access_may_read]);
3384 if (expr->acc.access[pet_expr_access_may_write]) {
3385 fprintf(stderr, "%*smay_write: ", indent + 2, "");
3386 isl_union_map_dump(
3387 expr->acc.access[pet_expr_access_may_write]);
3389 if (expr->acc.access[pet_expr_access_must_write]) {
3390 fprintf(stderr, "%*smust_write: ", indent + 2, "");
3391 isl_union_map_dump(
3392 expr->acc.access[pet_expr_access_must_write]);
3394 for (i = 0; i < expr->n_arg; ++i)
3395 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3396 break;
3397 case pet_expr_op:
3398 fprintf(stderr, "%s\n", op_str[expr->op]);
3399 for (i = 0; i < expr->n_arg; ++i)
3400 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3401 break;
3402 case pet_expr_call:
3403 fprintf(stderr, "%s/%d\n", expr->c.name, expr->n_arg);
3404 for (i = 0; i < expr->n_arg; ++i)
3405 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3406 if (expr->c.summary) {
3407 fprintf(stderr, "%*s", indent, "");
3408 fprintf(stderr, "summary:\n");
3409 pet_function_summary_dump_with_indent(expr->c.summary,
3410 indent + 2);
3412 break;
3413 case pet_expr_cast:
3414 fprintf(stderr, "(%s)\n", expr->type_name);
3415 for (i = 0; i < expr->n_arg; ++i)
3416 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3417 break;
3418 case pet_expr_error:
3419 fprintf(stderr, "ERROR\n");
3420 break;
3424 void pet_expr_dump(__isl_keep pet_expr *expr)
3426 pet_expr_dump_with_indent(expr, 0);