break up access relations into may_read/may_write/must_write
[pet.git] / expr.c
blobf0cb3442c143dadb9d524efb112bd6eb3d18e5e7
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(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".
302 __isl_give pet_expr *pet_expr_new_unary(enum pet_op_type op,
303 __isl_take pet_expr *arg)
305 isl_ctx *ctx;
306 pet_expr *expr;
308 if (!arg)
309 return NULL;
310 ctx = pet_expr_get_ctx(arg);
311 expr = pet_expr_alloc(ctx, pet_expr_op);
312 expr = pet_expr_set_n_arg(expr, 1);
313 if (!expr)
314 goto error;
316 expr->op = op;
317 expr->args[pet_un_arg] = arg;
319 return expr;
320 error:
321 pet_expr_free(arg);
322 return NULL;
325 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
326 * where the result is represented using a type of "type_size" bits
327 * (may be zero if unknown or if the type is not an integer).
329 __isl_give pet_expr *pet_expr_new_binary(int type_size, enum pet_op_type op,
330 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
332 isl_ctx *ctx;
333 pet_expr *expr;
335 if (!lhs || !rhs)
336 goto error;
337 ctx = pet_expr_get_ctx(lhs);
338 expr = pet_expr_alloc(ctx, pet_expr_op);
339 expr = pet_expr_set_n_arg(expr, 2);
340 if (!expr)
341 goto error;
343 expr->op = op;
344 expr->type_size = type_size;
345 expr->args[pet_bin_lhs] = lhs;
346 expr->args[pet_bin_rhs] = rhs;
348 return expr;
349 error:
350 pet_expr_free(lhs);
351 pet_expr_free(rhs);
352 return NULL;
355 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
357 __isl_give pet_expr *pet_expr_new_ternary(__isl_take pet_expr *cond,
358 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
360 isl_ctx *ctx;
361 pet_expr *expr;
363 if (!cond || !lhs || !rhs)
364 goto error;
365 ctx = pet_expr_get_ctx(cond);
366 expr = pet_expr_alloc(ctx, pet_expr_op);
367 expr = pet_expr_set_n_arg(expr, 3);
368 if (!expr)
369 goto error;
371 expr->op = pet_op_cond;
372 expr->args[pet_ter_cond] = cond;
373 expr->args[pet_ter_true] = lhs;
374 expr->args[pet_ter_false] = rhs;
376 return expr;
377 error:
378 pet_expr_free(cond);
379 pet_expr_free(lhs);
380 pet_expr_free(rhs);
381 return NULL;
384 /* Construct a call pet_expr that calls function "name" with "n_arg"
385 * arguments. The caller is responsible for filling in the arguments.
387 __isl_give pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
388 unsigned n_arg)
390 pet_expr *expr;
392 expr = pet_expr_alloc(ctx, pet_expr_call);
393 expr = pet_expr_set_n_arg(expr, n_arg);
394 if (!expr)
395 return NULL;
397 expr->name = strdup(name);
398 if (!expr->name)
399 return pet_expr_free(expr);
401 return expr;
404 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
406 __isl_give pet_expr *pet_expr_new_cast(const char *type_name,
407 __isl_take pet_expr *arg)
409 isl_ctx *ctx;
410 pet_expr *expr;
412 if (!arg)
413 return NULL;
415 ctx = pet_expr_get_ctx(arg);
416 expr = pet_expr_alloc(ctx, pet_expr_cast);
417 expr = pet_expr_set_n_arg(expr, 1);
418 if (!expr)
419 goto error;
421 expr->type_name = strdup(type_name);
422 if (!expr->type_name)
423 goto error;
425 expr->args[0] = arg;
427 return expr;
428 error:
429 pet_expr_free(arg);
430 pet_expr_free(expr);
431 return NULL;
434 /* Construct a pet_expr that represents the double "d".
436 __isl_give pet_expr *pet_expr_new_double(isl_ctx *ctx,
437 double val, const char *s)
439 pet_expr *expr;
441 expr = pet_expr_alloc(ctx, pet_expr_double);
442 if (!expr)
443 return NULL;
445 expr->d.val = val;
446 expr->d.s = strdup(s);
447 if (!expr->d.s)
448 return pet_expr_free(expr);
450 return expr;
453 /* Construct a pet_expr that represents the integer value "v".
455 __isl_give pet_expr *pet_expr_new_int(__isl_take isl_val *v)
457 isl_ctx *ctx;
458 pet_expr *expr;
460 if (!v)
461 return NULL;
463 ctx = isl_val_get_ctx(v);
464 expr = pet_expr_alloc(ctx, pet_expr_int);
465 if (!expr)
466 goto error;
468 expr->i = v;
470 return expr;
471 error:
472 isl_val_free(v);
473 return NULL;
476 /* Return an independent duplicate of "expr".
478 * In case of an access expression, make sure the depth of the duplicate is set
479 * before the access relation (if any) is set and after the index expression
480 * is set.
482 static __isl_give pet_expr *pet_expr_dup(__isl_keep pet_expr *expr)
484 int i;
485 pet_expr *dup;
486 enum pet_expr_access_type type;
488 if (!expr)
489 return NULL;
491 dup = pet_expr_alloc(expr->ctx, expr->type);
492 dup = pet_expr_set_type_size(dup, expr->type_size);
493 dup = pet_expr_set_n_arg(dup, expr->n_arg);
494 for (i = 0; i < expr->n_arg; ++i)
495 dup = pet_expr_set_arg(dup, i, pet_expr_copy(expr->args[i]));
497 switch (expr->type) {
498 case pet_expr_access:
499 if (expr->acc.ref_id)
500 dup = pet_expr_access_set_ref_id(dup,
501 isl_id_copy(expr->acc.ref_id));
502 dup = pet_expr_access_set_index(dup,
503 isl_multi_pw_aff_copy(expr->acc.index));
504 dup = pet_expr_access_set_depth(dup, expr->acc.depth);
505 for (type = pet_expr_access_begin;
506 type < pet_expr_access_end; ++type) {
507 if (!expr->acc.access[type])
508 continue;
509 dup = pet_expr_access_set_access(dup, type,
510 isl_union_map_copy(expr->acc.access[type]));
512 dup = pet_expr_access_set_read(dup, expr->acc.read);
513 dup = pet_expr_access_set_write(dup, expr->acc.write);
514 dup = pet_expr_access_set_kill(dup, expr->acc.kill);
515 break;
516 case pet_expr_call:
517 dup = pet_expr_call_set_name(dup, expr->name);
518 break;
519 case pet_expr_cast:
520 dup = pet_expr_cast_set_type_name(dup, expr->type_name);
521 break;
522 case pet_expr_double:
523 dup = pet_expr_double_set(dup, expr->d.val, expr->d.s);
524 break;
525 case pet_expr_int:
526 dup = pet_expr_int_set_val(dup, isl_val_copy(expr->i));
527 break;
528 case pet_expr_op:
529 dup = pet_expr_op_set_type(dup, expr->op);
530 break;
531 case pet_expr_error:
532 dup = pet_expr_free(dup);
533 break;
536 return dup;
539 __isl_give pet_expr *pet_expr_cow(__isl_take pet_expr *expr)
541 if (!expr)
542 return NULL;
544 if (expr->ref == 1)
545 return expr;
546 expr->ref--;
547 return pet_expr_dup(expr);
550 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr)
552 enum pet_expr_access_type type;
553 int i;
555 if (!expr)
556 return NULL;
557 if (--expr->ref > 0)
558 return NULL;
560 for (i = 0; i < expr->n_arg; ++i)
561 pet_expr_free(expr->args[i]);
562 free(expr->args);
564 switch (expr->type) {
565 case pet_expr_access:
566 isl_id_free(expr->acc.ref_id);
567 for (type = pet_expr_access_begin;
568 type < pet_expr_access_end; ++type)
569 isl_union_map_free(expr->acc.access[type]);
570 isl_multi_pw_aff_free(expr->acc.index);
571 break;
572 case pet_expr_call:
573 free(expr->name);
574 break;
575 case pet_expr_cast:
576 free(expr->type_name);
577 break;
578 case pet_expr_double:
579 free(expr->d.s);
580 break;
581 case pet_expr_int:
582 isl_val_free(expr->i);
583 break;
584 case pet_expr_op:
585 case pet_expr_error:
586 break;
589 isl_ctx_deref(expr->ctx);
590 free(expr);
591 return NULL;
594 /* Return an additional reference to "expr".
596 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr)
598 if (!expr)
599 return NULL;
601 expr->ref++;
602 return expr;
605 /* Return the isl_ctx in which "expr" was created.
607 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr)
609 return expr ? expr->ctx : NULL;
612 /* Return the type of "expr".
614 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr)
616 if (!expr)
617 return pet_expr_error;
618 return expr->type;
621 /* Return the number of arguments of "expr".
623 int pet_expr_get_n_arg(__isl_keep pet_expr *expr)
625 if (!expr)
626 return -1;
628 return expr->n_arg;
631 /* Set the number of arguments of "expr" to "n".
633 * If "expr" originally had more arguments, then remove the extra arguments.
634 * If "expr" originally had fewer arguments, then create space for
635 * the extra arguments ans initialize them to NULL.
637 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n)
639 int i;
640 pet_expr **args;
642 if (!expr)
643 return NULL;
644 if (expr->n_arg == n)
645 return expr;
646 expr = pet_expr_cow(expr);
647 if (!expr)
648 return NULL;
650 if (n < expr->n_arg) {
651 for (i = n; i < expr->n_arg; ++i)
652 pet_expr_free(expr->args[i]);
653 expr->n_arg = n;
654 return expr;
657 args = isl_realloc_array(expr->ctx, expr->args, pet_expr *, n);
658 if (!args)
659 return pet_expr_free(expr);
660 expr->args = args;
661 for (i = expr->n_arg; i < n; ++i)
662 expr->args[i] = NULL;
663 expr->n_arg = n;
665 return expr;
668 /* Return the argument of "expr" at position "pos".
670 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos)
672 if (!expr)
673 return NULL;
674 if (pos < 0 || pos >= expr->n_arg)
675 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
676 "position out of bounds", return NULL);
678 return pet_expr_copy(expr->args[pos]);
681 /* Replace the argument of "expr" at position "pos" by "arg".
683 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
684 __isl_take pet_expr *arg)
686 if (!expr || !arg)
687 goto error;
688 if (pos < 0 || pos >= expr->n_arg)
689 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
690 "position out of bounds", goto error);
691 if (expr->args[pos] == arg) {
692 pet_expr_free(arg);
693 return expr;
696 expr = pet_expr_cow(expr);
697 if (!expr)
698 goto error;
700 pet_expr_free(expr->args[pos]);
701 expr->args[pos] = arg;
703 return expr;
704 error:
705 pet_expr_free(expr);
706 pet_expr_free(arg);
707 return NULL;
710 /* Does "expr" perform a comparison operation?
712 int pet_expr_is_comparison(__isl_keep pet_expr *expr)
714 if (!expr)
715 return -1;
716 if (expr->type != pet_expr_op)
717 return 0;
718 switch (expr->op) {
719 case pet_op_eq:
720 case pet_op_ne:
721 case pet_op_le:
722 case pet_op_ge:
723 case pet_op_lt:
724 case pet_op_gt:
725 return 1;
726 default:
727 return 0;
731 /* Does "expr" perform a boolean operation?
733 int pet_expr_is_boolean(__isl_keep pet_expr *expr)
735 if (!expr)
736 return -1;
737 if (expr->type != pet_expr_op)
738 return 0;
739 switch (expr->op) {
740 case pet_op_land:
741 case pet_op_lor:
742 case pet_op_lnot:
743 return 1;
744 default:
745 return 0;
749 /* Is "expr" an assume statement?
751 int pet_expr_is_assume(__isl_keep pet_expr *expr)
753 if (!expr)
754 return -1;
755 if (expr->type != pet_expr_op)
756 return 0;
757 return expr->op == pet_op_assume;
760 /* Does "expr" perform a min operation?
762 int pet_expr_is_min(__isl_keep pet_expr *expr)
764 if (!expr)
765 return -1;
766 if (expr->type != pet_expr_call)
767 return 0;
768 if (expr->n_arg != 2)
769 return 0;
770 if (strcmp(expr->name, "min") != 0)
771 return 0;
772 return 1;
775 /* Does "expr" perform a max operation?
777 int pet_expr_is_max(__isl_keep pet_expr *expr)
779 if (!expr)
780 return -1;
781 if (expr->type != pet_expr_call)
782 return 0;
783 if (expr->n_arg != 2)
784 return 0;
785 if (strcmp(expr->name, "max") != 0)
786 return 0;
787 return 1;
790 /* Does "expr" represent an access to an unnamed space, i.e.,
791 * does it represent an affine expression?
793 int pet_expr_is_affine(__isl_keep pet_expr *expr)
795 int has_id;
797 if (!expr)
798 return -1;
799 if (expr->type != pet_expr_access)
800 return 0;
802 has_id = isl_multi_pw_aff_has_tuple_id(expr->acc.index, isl_dim_out);
803 if (has_id < 0)
804 return -1;
806 return !has_id;
809 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
810 * not part of any struct?
812 int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
814 if (!expr)
815 return -1;
816 if (expr->type != pet_expr_access)
817 return 0;
818 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index))
819 return 0;
821 return expr->acc.depth == 0;
824 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
825 * of parameters.
827 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
828 __isl_keep isl_multi_pw_aff *mpa2)
830 int equal;
832 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
833 if (equal < 0 || equal)
834 return equal;
835 mpa2 = isl_multi_pw_aff_copy(mpa2);
836 mpa2 = isl_multi_pw_aff_align_params(mpa2,
837 isl_multi_pw_aff_get_space(mpa1));
838 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
839 isl_multi_pw_aff_free(mpa2);
841 return equal;
844 /* Construct an access relation from the index expression and
845 * the array depth of the access expression "expr".
847 * If the number of indices is smaller than the depth of the array,
848 * then we assume that all elements of the remaining dimensions
849 * are accessed.
851 static __isl_give isl_union_map *construct_access_relation(
852 __isl_keep pet_expr *expr)
854 isl_map *access;
855 int dim;
856 int read, write;
858 if (!expr)
859 return NULL;
861 access = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
862 if (!access)
863 return NULL;
865 dim = isl_map_dim(access, isl_dim_out);
866 if (dim > expr->acc.depth)
867 isl_die(isl_map_get_ctx(access), isl_error_internal,
868 "number of indices greater than depth",
869 access = isl_map_free(access));
871 if (dim != expr->acc.depth)
872 access = extend_range(access, expr->acc.depth - dim);
874 return isl_union_map_from_map(access);
877 /* Ensure that all relevant access relations are explicitly
878 * available in "expr".
880 * If "expr" does not already have the relevant access relations, then create
881 * them based on the index expression and the array depth.
883 * We do not cow since adding an explicit access relation
884 * does not change the meaning of the expression.
886 static __isl_give pet_expr *introduce_access_relations(
887 __isl_take pet_expr *expr)
889 enum pet_expr_access_type type;
890 isl_union_map *access;
891 int dim;
892 int kill, read, write;
894 if (!expr)
895 return NULL;
896 if (has_relevant_access_relations(expr))
897 return expr;
899 access = construct_access_relation(expr);
900 if (!access)
901 return pet_expr_free(expr);
903 kill = expr->acc.kill;
904 read = expr->acc.read;
905 write = expr->acc.write;
906 if (kill && !expr->acc.access[pet_expr_access_fake_killed])
907 expr->acc.access[pet_expr_access_fake_killed] =
908 isl_union_map_copy(access);
909 if (read && !expr->acc.access[pet_expr_access_may_read])
910 expr->acc.access[pet_expr_access_may_read] =
911 isl_union_map_copy(access);
912 if (write && !expr->acc.access[pet_expr_access_may_write])
913 expr->acc.access[pet_expr_access_may_write] =
914 isl_union_map_copy(access);
915 if (write && !expr->acc.access[pet_expr_access_must_write])
916 expr->acc.access[pet_expr_access_must_write] =
917 isl_union_map_copy(access);
919 isl_union_map_free(access);
921 if (!has_relevant_access_relations(expr))
922 return pet_expr_free(expr);
924 return expr;
927 /* Return 1 if the two pet_exprs are equivalent.
929 int pet_expr_is_equal(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2)
931 int i;
932 enum pet_expr_access_type type;
934 if (!expr1 || !expr2)
935 return 0;
937 if (expr1->type != expr2->type)
938 return 0;
939 if (expr1->n_arg != expr2->n_arg)
940 return 0;
941 for (i = 0; i < expr1->n_arg; ++i)
942 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
943 return 0;
944 switch (expr1->type) {
945 case pet_expr_error:
946 return -1;
947 case pet_expr_double:
948 if (strcmp(expr1->d.s, expr2->d.s))
949 return 0;
950 if (expr1->d.val != expr2->d.val)
951 return 0;
952 break;
953 case pet_expr_int:
954 if (!isl_val_eq(expr1->i, expr2->i))
955 return 0;
956 break;
957 case pet_expr_access:
958 if (expr1->acc.read != expr2->acc.read)
959 return 0;
960 if (expr1->acc.write != expr2->acc.write)
961 return 0;
962 if (expr1->acc.kill != expr2->acc.kill)
963 return 0;
964 if (expr1->acc.ref_id != expr2->acc.ref_id)
965 return 0;
966 if (!expr1->acc.index || !expr2->acc.index)
967 return 0;
968 if (!multi_pw_aff_is_equal(expr1->acc.index, expr2->acc.index))
969 return 0;
970 if (expr1->acc.depth != expr2->acc.depth)
971 return 0;
972 if (has_relevant_access_relations(expr1) !=
973 has_relevant_access_relations(expr2)) {
974 int equal;
975 expr1 = pet_expr_copy(expr1);
976 expr2 = pet_expr_copy(expr2);
977 expr1 = introduce_access_relations(expr1);
978 expr2 = introduce_access_relations(expr2);
979 equal = pet_expr_is_equal(expr1, expr2);
980 pet_expr_free(expr1);
981 pet_expr_free(expr2);
982 return equal;
984 for (type = pet_expr_access_begin;
985 type < pet_expr_access_end; ++type) {
986 if (!expr1->acc.access[type] !=
987 !expr2->acc.access[type])
988 return 0;
989 if (!expr1->acc.access[type])
990 continue;
991 if (!isl_union_map_is_equal(expr1->acc.access[type],
992 expr2->acc.access[type]))
993 return 0;
995 break;
996 case pet_expr_op:
997 if (expr1->op != expr2->op)
998 return 0;
999 break;
1000 case pet_expr_call:
1001 if (strcmp(expr1->name, expr2->name))
1002 return 0;
1003 break;
1004 case pet_expr_cast:
1005 if (strcmp(expr1->type_name, expr2->type_name))
1006 return 0;
1007 break;
1010 return 1;
1013 /* Does the access expression "expr" read the accessed elements?
1015 int pet_expr_access_is_read(__isl_keep pet_expr *expr)
1017 if (!expr)
1018 return -1;
1019 if (expr->type != pet_expr_access)
1020 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1021 "not an access expression", return -1);
1023 return expr->acc.read;
1026 /* Does the access expression "expr" write to the accessed elements?
1028 int pet_expr_access_is_write(__isl_keep pet_expr *expr)
1030 if (!expr)
1031 return -1;
1032 if (expr->type != pet_expr_access)
1033 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1034 "not an access expression", return -1);
1036 return expr->acc.write;
1039 /* Return the identifier of the array accessed by "expr".
1041 * If "expr" represents a member access, then return the identifier
1042 * of the outer structure array.
1044 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
1046 if (!expr)
1047 return NULL;
1048 if (expr->type != pet_expr_access)
1049 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1050 "not an access expression", return NULL);
1052 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index)) {
1053 isl_space *space;
1054 isl_id *id;
1056 space = isl_multi_pw_aff_get_space(expr->acc.index);
1057 space = isl_space_range(space);
1058 while (space && isl_space_is_wrapping(space))
1059 space = isl_space_domain(isl_space_unwrap(space));
1060 id = isl_space_get_tuple_id(space, isl_dim_set);
1061 isl_space_free(space);
1063 return id;
1066 return isl_multi_pw_aff_get_tuple_id(expr->acc.index, isl_dim_out);
1069 /* Return the parameter space of "expr".
1071 __isl_give isl_space *pet_expr_access_get_parameter_space(
1072 __isl_keep pet_expr *expr)
1074 isl_space *space;
1076 if (!expr)
1077 return NULL;
1078 if (expr->type != pet_expr_access)
1079 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1080 "not an access expression", return NULL);
1082 space = isl_multi_pw_aff_get_space(expr->acc.index);
1083 space = isl_space_params(space);
1085 return space;
1088 /* Return the domain space of "expr", without the arguments (if any).
1090 __isl_give isl_space *pet_expr_access_get_domain_space(
1091 __isl_keep pet_expr *expr)
1093 isl_space *space;
1095 if (!expr)
1096 return NULL;
1097 if (expr->type != pet_expr_access)
1098 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1099 "not an access expression", return NULL);
1101 space = isl_multi_pw_aff_get_space(expr->acc.index);
1102 space = isl_space_domain(space);
1103 if (isl_space_is_wrapping(space))
1104 space = isl_space_domain(isl_space_unwrap(space));
1106 return space;
1109 /* Return the space of the data accessed by "expr".
1111 __isl_give isl_space *pet_expr_access_get_data_space(__isl_keep pet_expr *expr)
1113 isl_space *space;
1115 if (!expr)
1116 return NULL;
1117 if (expr->type != pet_expr_access)
1118 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1119 "not an access expression", return NULL);
1121 space = isl_multi_pw_aff_get_space(expr->acc.index);
1122 space = isl_space_range(space);
1124 return space;
1127 /* Modify all expressions of type pet_expr_access in "expr"
1128 * by calling "fn" on them.
1130 __isl_give pet_expr *pet_expr_map_access(__isl_take pet_expr *expr,
1131 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1132 void *user)
1134 int i, n;
1136 n = pet_expr_get_n_arg(expr);
1137 for (i = 0; i < n; ++i) {
1138 pet_expr *arg = pet_expr_get_arg(expr, i);
1139 arg = pet_expr_map_access(arg, fn, user);
1140 expr = pet_expr_set_arg(expr, i, arg);
1143 if (!expr)
1144 return NULL;
1146 if (expr->type == pet_expr_access)
1147 expr = fn(expr, user);
1149 return expr;
1152 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1154 * Return -1 on error (where fn returning a negative value is treated as
1155 * an error).
1156 * Otherwise return 0.
1158 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
1159 enum pet_expr_type type,
1160 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1162 int i;
1164 if (!expr)
1165 return -1;
1167 for (i = 0; i < expr->n_arg; ++i)
1168 if (pet_expr_foreach_expr_of_type(expr->args[i],
1169 type, fn, user) < 0)
1170 return -1;
1172 if (expr->type == type)
1173 return fn(expr, user);
1175 return 0;
1178 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1180 * Return -1 on error (where fn returning a negative value is treated as
1181 * an error).
1182 * Otherwise return 0.
1184 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
1185 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1187 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
1190 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1192 * Return -1 on error (where fn returning a negative value is treated as
1193 * an error).
1194 * Otherwise return 0.
1196 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
1197 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1199 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1202 /* Internal data structure for pet_expr_writes.
1203 * "id" is the identifier that we are looking for.
1204 * "found" is set if we have found the identifier being written to.
1206 struct pet_expr_writes_data {
1207 isl_id *id;
1208 int found;
1211 /* Given an access expression, check if it writes to data->id.
1212 * If so, set data->found and abort the search.
1214 static int writes(__isl_keep pet_expr *expr, void *user)
1216 struct pet_expr_writes_data *data = user;
1217 isl_id *write_id;
1219 if (!expr->acc.write)
1220 return 0;
1221 if (pet_expr_is_affine(expr))
1222 return 0;
1224 write_id = pet_expr_access_get_id(expr);
1225 isl_id_free(write_id);
1227 if (!write_id)
1228 return -1;
1230 if (write_id != data->id)
1231 return 0;
1233 data->found = 1;
1234 return -1;
1237 /* Does expression "expr" write to "id"?
1239 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1241 struct pet_expr_writes_data data;
1243 data.id = id;
1244 data.found = 0;
1245 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1246 !data.found)
1247 return -1;
1249 return data.found;
1252 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1253 * index expression and access relations of "expr" (if any)
1254 * to dimensions of "dst_type" at "dst_pos".
1256 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1257 enum isl_dim_type dst_type, unsigned dst_pos,
1258 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1260 enum pet_expr_access_type type;
1262 expr = pet_expr_cow(expr);
1263 if (!expr)
1264 return NULL;
1265 if (expr->type != pet_expr_access)
1266 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1267 "not an access pet_expr", return pet_expr_free(expr));
1269 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1270 if (!expr->acc.access[type])
1271 continue;
1272 expr->acc.access[type] =
1273 pet_union_map_move_dims(expr->acc.access[type],
1274 dst_type, dst_pos, src_type, src_pos, n);
1275 if (!expr->acc.access[type])
1276 break;
1278 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1279 dst_type, dst_pos, src_type, src_pos, n);
1280 if (!expr->acc.index || type < pet_expr_access_end)
1281 return pet_expr_free(expr);
1283 return expr;
1286 /* Replace the index expression and access relations (if any) of "expr"
1287 * by their preimages under the function represented by "ma".
1289 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1290 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1292 enum pet_expr_access_type type;
1294 expr = pet_expr_cow(expr);
1295 if (!expr || !ma)
1296 goto error;
1297 if (expr->type != pet_expr_access)
1298 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1299 "not an access pet_expr", goto error);
1301 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1302 if (!expr->acc.access[type])
1303 continue;
1304 expr->acc.access[type] =
1305 isl_union_map_preimage_domain_multi_aff(
1306 expr->acc.access[type], isl_multi_aff_copy(ma));
1307 if (!expr->acc.access[type])
1308 break;
1310 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1311 ma);
1312 if (!expr->acc.index || type < pet_expr_access_end)
1313 return pet_expr_free(expr);
1315 return expr;
1316 error:
1317 isl_multi_aff_free(ma);
1318 pet_expr_free(expr);
1319 return NULL;
1322 /* Replace the index expression and access relations (if any) of "expr"
1323 * by their preimages under the function represented by "mpa".
1325 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1326 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1328 enum pet_expr_access_type type;
1330 expr = pet_expr_cow(expr);
1331 if (!expr || !mpa)
1332 goto error;
1333 if (expr->type != pet_expr_access)
1334 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1335 "not an access pet_expr", goto error);
1337 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1338 if (!expr->acc.access[type])
1339 continue;
1340 expr->acc.access[type] =
1341 isl_union_map_preimage_domain_multi_pw_aff(
1342 expr->acc.access[type], isl_multi_pw_aff_copy(mpa));
1343 if (!expr->acc.access[type])
1344 break;
1346 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1347 expr->acc.index, mpa);
1348 if (!expr->acc.index || type < pet_expr_access_end)
1349 return pet_expr_free(expr);
1351 return expr;
1352 error:
1353 isl_multi_pw_aff_free(mpa);
1354 pet_expr_free(expr);
1355 return NULL;
1358 /* Return the index expression of access expression "expr".
1360 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1361 __isl_keep pet_expr *expr)
1363 if (!expr)
1364 return NULL;
1365 if (expr->type != pet_expr_access)
1366 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1367 "not an access expression", return NULL);
1369 return isl_multi_pw_aff_copy(expr->acc.index);
1372 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1374 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1376 isl_space *space;
1377 enum pet_expr_access_type type;
1379 expr = pet_expr_cow(expr);
1380 if (!expr)
1381 return NULL;
1382 if (expr->type != pet_expr_access)
1383 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1384 "not an access expression", return pet_expr_free(expr));
1386 if (!has_any_access_relation(expr))
1387 return expr;
1389 space = isl_multi_pw_aff_get_space(expr->acc.index);
1390 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1391 if (!expr->acc.access[type])
1392 continue;
1393 space = isl_space_align_params(space,
1394 isl_union_map_get_space(expr->acc.access[type]));
1396 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1397 isl_space_copy(space));
1398 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1399 if (!expr->acc.access[type])
1400 continue;
1401 expr->acc.access[type] =
1402 isl_union_map_align_params(expr->acc.access[type],
1403 isl_space_copy(space));
1404 if (!expr->acc.access[type])
1405 break;
1407 isl_space_free(space);
1408 if (!expr->acc.index || type < pet_expr_access_end)
1409 return pet_expr_free(expr);
1411 return expr;
1414 /* Are "expr1" and "expr2" both array accesses such that
1415 * the access relation of "expr1" is a subset of that of "expr2"?
1416 * Only take into account the first "n_arg" arguments.
1418 * This function is tailored for use by mark_self_dependences in nest.c.
1419 * In particular, the input expressions may have more than "n_arg"
1420 * elements in their arguments arrays, while only the first "n_arg"
1421 * elements are referenced from the access relations.
1423 int pet_expr_is_sub_access(__isl_keep pet_expr *expr1,
1424 __isl_keep pet_expr *expr2, int n_arg)
1426 isl_id *id1, *id2;
1427 int i, n1, n2;
1428 int is_subset;
1430 if (!expr1 || !expr2)
1431 return 0;
1432 if (pet_expr_get_type(expr1) != pet_expr_access)
1433 return 0;
1434 if (pet_expr_get_type(expr2) != pet_expr_access)
1435 return 0;
1436 if (pet_expr_is_affine(expr1))
1437 return 0;
1438 if (pet_expr_is_affine(expr2))
1439 return 0;
1440 n1 = pet_expr_get_n_arg(expr1);
1441 if (n1 > n_arg)
1442 n1 = n_arg;
1443 n2 = pet_expr_get_n_arg(expr2);
1444 if (n2 > n_arg)
1445 n2 = n_arg;
1446 if (n1 != n2)
1447 return 0;
1448 for (i = 0; i < n1; ++i) {
1449 int equal;
1450 equal = pet_expr_is_equal(expr1->args[i], expr2->args[i]);
1451 if (equal < 0 || !equal)
1452 return equal;
1454 id1 = pet_expr_access_get_id(expr1);
1455 id2 = pet_expr_access_get_id(expr2);
1456 isl_id_free(id1);
1457 isl_id_free(id2);
1458 if (!id1 || !id2)
1459 return 0;
1460 if (id1 != id2)
1461 return 0;
1463 expr1 = pet_expr_copy(expr1);
1464 expr2 = pet_expr_copy(expr2);
1465 expr1 = introduce_access_relations(expr1);
1466 expr2 = introduce_access_relations(expr2);
1467 if (!expr1 || !expr2)
1468 goto error;
1470 is_subset = isl_union_map_is_subset(
1471 expr1->acc.access[pet_expr_access_may_read],
1472 expr2->acc.access[pet_expr_access_may_read]);
1474 pet_expr_free(expr1);
1475 pet_expr_free(expr2);
1477 return is_subset;
1478 error:
1479 pet_expr_free(expr1);
1480 pet_expr_free(expr2);
1481 return -1;
1484 /* Given a set in the iteration space "domain", extend it to live in the space
1485 * of the domain of access relations.
1487 * That, is the number of arguments "n" is 0, then simply return domain.
1488 * Otherwise, return [domain -> [a_1,...,a_n]].
1490 static __isl_give isl_set *add_arguments(__isl_take isl_set *domain, int n)
1492 isl_map *map;
1494 if (n == 0)
1495 return domain;
1497 map = isl_map_from_domain(domain);
1498 map = isl_map_add_dims(map, isl_dim_out, n);
1499 return isl_map_wrap(map);
1502 /* Add extra conditions to the domains of all access relations in "expr",
1503 * introducing access relations if they are not already present.
1505 * The conditions are not added to the index expression. Instead, they
1506 * are used to try and simplify the index expression.
1508 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1509 __isl_take isl_set *cond)
1511 int i;
1512 isl_union_set *uset;
1513 enum pet_expr_access_type type;
1515 expr = pet_expr_cow(expr);
1516 if (!expr)
1517 goto error;
1519 for (i = 0; i < expr->n_arg; ++i) {
1520 expr->args[i] = pet_expr_restrict(expr->args[i],
1521 isl_set_copy(cond));
1522 if (!expr->args[i])
1523 goto error;
1526 if (expr->type != pet_expr_access) {
1527 isl_set_free(cond);
1528 return expr;
1531 expr = introduce_access_relations(expr);
1532 if (!expr)
1533 goto error;
1535 cond = add_arguments(cond, expr->n_arg);
1536 uset = isl_union_set_from_set(isl_set_copy(cond));
1537 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1538 if (!expr->acc.access[type])
1539 continue;
1540 expr->acc.access[type] =
1541 isl_union_map_intersect_domain(expr->acc.access[type],
1542 isl_union_set_copy(uset));
1543 if (!expr->acc.access[type])
1544 break;
1546 isl_union_set_free(uset);
1547 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, cond);
1548 if (type < pet_expr_access_end || !expr->acc.index)
1549 return pet_expr_free(expr);
1551 return expr;
1552 error:
1553 isl_set_free(cond);
1554 return pet_expr_free(expr);
1557 /* Modify the access relations (if any) and index expression
1558 * of the given access expression
1559 * based on the given iteration space transformation.
1560 * In particular, precompose the access relation and index expression
1561 * with the update function.
1563 * If the access has any arguments then the domain of the access relation
1564 * is a wrapped mapping from the iteration space to the space of
1565 * argument values. We only need to change the domain of this wrapped
1566 * mapping, so we extend the input transformation with an identity mapping
1567 * on the space of argument values.
1569 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1570 __isl_keep isl_multi_pw_aff *update)
1572 enum pet_expr_access_type type;
1574 expr = pet_expr_cow(expr);
1575 if (!expr)
1576 return NULL;
1577 if (expr->type != pet_expr_access)
1578 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1579 "not an access expression", return pet_expr_free(expr));
1581 update = isl_multi_pw_aff_copy(update);
1583 if (expr->n_arg > 0) {
1584 isl_space *space;
1585 isl_multi_pw_aff *id;
1587 space = isl_multi_pw_aff_get_space(expr->acc.index);
1588 space = isl_space_domain(space);
1589 space = isl_space_unwrap(space);
1590 space = isl_space_range(space);
1591 space = isl_space_map_from_set(space);
1592 id = isl_multi_pw_aff_identity(space);
1593 update = isl_multi_pw_aff_product(update, id);
1596 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1597 if (!expr->acc.access[type])
1598 continue;
1599 expr->acc.access[type] =
1600 isl_union_map_preimage_domain_multi_pw_aff(
1601 expr->acc.access[type],
1602 isl_multi_pw_aff_copy(update));
1603 if (!expr->acc.access[type])
1604 break;
1606 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1607 expr->acc.index, update);
1608 if (type < pet_expr_access_end || !expr->acc.index)
1609 return pet_expr_free(expr);
1611 return expr;
1614 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1616 isl_multi_pw_aff *update = user;
1618 return pet_expr_access_update_domain(expr, update);
1621 /* Modify all access relations in "expr" by precomposing them with
1622 * the given iteration space transformation.
1624 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1625 __isl_take isl_multi_pw_aff *update)
1627 expr = pet_expr_map_access(expr, &update_domain, update);
1628 isl_multi_pw_aff_free(update);
1629 return expr;
1632 /* Given an expression with accesses that have a 0D anonymous domain,
1633 * replace those domains by "space".
1635 __isl_give pet_expr *pet_expr_insert_domain(__isl_take pet_expr *expr,
1636 __isl_take isl_space *space)
1638 isl_multi_pw_aff *mpa;
1640 space = isl_space_from_domain(space);
1641 mpa = isl_multi_pw_aff_zero(space);
1642 return pet_expr_update_domain(expr, mpa);
1645 /* Add all parameters in "space" to the access relations (if any)
1646 * and index expression of "expr".
1648 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1650 isl_space *space = user;
1651 enum pet_expr_access_type type;
1653 expr = pet_expr_cow(expr);
1654 if (!expr)
1655 return NULL;
1656 if (expr->type != pet_expr_access)
1657 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1658 "not an access expression", return pet_expr_free(expr));
1660 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1661 if (!expr->acc.access[type])
1662 continue;
1663 expr->acc.access[type] =
1664 isl_union_map_align_params(expr->acc.access[type],
1665 isl_space_copy(space));
1666 if (!expr->acc.access[type])
1667 break;
1669 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1670 isl_space_copy(space));
1671 if (type < pet_expr_access_end || !expr->acc.index)
1672 return pet_expr_free(expr);
1674 return expr;
1677 /* Add all parameters in "space" to all access relations and index expressions
1678 * in "expr".
1680 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1681 __isl_take isl_space *space)
1683 expr = pet_expr_map_access(expr, &align_params, space);
1684 isl_space_free(space);
1685 return expr;
1688 /* Insert an argument expression corresponding to "test" in front
1689 * of the list of arguments described by *n_arg and *args.
1691 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1692 __isl_keep isl_multi_pw_aff *test)
1694 int i;
1695 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1697 if (!test)
1698 return pet_expr_free(expr);
1699 expr = pet_expr_cow(expr);
1700 if (!expr)
1701 return NULL;
1703 if (!expr->args) {
1704 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1705 if (!expr->args)
1706 return pet_expr_free(expr);
1707 } else {
1708 pet_expr **ext;
1709 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1710 if (!ext)
1711 return pet_expr_free(expr);
1712 for (i = 0; i < expr->n_arg; ++i)
1713 ext[1 + i] = expr->args[i];
1714 free(expr->args);
1715 expr->args = ext;
1717 expr->n_arg++;
1718 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1719 if (!expr->args[0])
1720 return pet_expr_free(expr);
1722 return expr;
1725 /* Make the expression "expr" depend on the value of "test"
1726 * being equal to "satisfied".
1728 * If "test" is an affine expression, we simply add the conditions
1729 * on the expression having the value "satisfied" to all access relations
1730 * (introducing access relations if they are missing) and index expressions.
1732 * Otherwise, we add a filter to "expr" (which is then assumed to be
1733 * an access expression) corresponding to "test" being equal to "satisfied".
1735 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
1736 __isl_take isl_multi_pw_aff *test, int satisfied)
1738 isl_id *id;
1739 isl_ctx *ctx;
1740 isl_space *space;
1741 isl_pw_multi_aff *pma;
1742 enum pet_expr_access_type type;
1744 expr = pet_expr_cow(expr);
1745 if (!expr || !test)
1746 goto error;
1748 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
1749 isl_pw_aff *pa;
1750 isl_set *cond;
1752 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
1753 isl_multi_pw_aff_free(test);
1754 if (satisfied)
1755 cond = isl_pw_aff_non_zero_set(pa);
1756 else
1757 cond = isl_pw_aff_zero_set(pa);
1758 return pet_expr_restrict(expr, cond);
1761 ctx = isl_multi_pw_aff_get_ctx(test);
1762 if (expr->type != pet_expr_access)
1763 isl_die(ctx, isl_error_invalid,
1764 "can only filter access expressions", goto error);
1766 expr = introduce_access_relations(expr);
1767 if (!expr)
1768 goto error;
1770 space = isl_space_domain(isl_multi_pw_aff_get_space(expr->acc.index));
1771 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1772 pma = pet_filter_insert_pma(space, id, satisfied);
1774 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1775 if (!expr->acc.access[type])
1776 continue;
1777 expr->acc.access[type] =
1778 isl_union_map_preimage_domain_pw_multi_aff(
1779 expr->acc.access[type],
1780 isl_pw_multi_aff_copy(pma));
1781 if (!expr->acc.access[type])
1782 break;
1784 pma = isl_pw_multi_aff_gist(pma,
1785 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma)));
1786 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
1787 expr->acc.index, pma);
1788 if (type < pet_expr_access_end || !expr->acc.index)
1789 goto error;
1791 expr = insert_access_arg(expr, test);
1793 isl_multi_pw_aff_free(test);
1794 return expr;
1795 error:
1796 isl_multi_pw_aff_free(test);
1797 return pet_expr_free(expr);
1800 /* Add a reference identifier to access expression "expr".
1801 * "user" points to an integer that contains the sequence number
1802 * of the next reference.
1804 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
1805 void *user)
1807 isl_ctx *ctx;
1808 char name[50];
1809 int *n_ref = user;
1811 expr = pet_expr_cow(expr);
1812 if (!expr)
1813 return expr;
1814 if (expr->type != pet_expr_access)
1815 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1816 "not an access expression", return pet_expr_free(expr));
1818 ctx = pet_expr_get_ctx(expr);
1819 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
1820 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
1821 if (!expr->acc.ref_id)
1822 return pet_expr_free(expr);
1824 return expr;
1827 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
1829 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
1832 /* Reset the user pointer on all parameter and tuple ids in
1833 * the access relations (if any) and the index expression
1834 * of the access expression "expr".
1836 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
1837 void *user)
1839 enum pet_expr_access_type type;
1841 expr = pet_expr_cow(expr);
1842 if (!expr)
1843 return expr;
1844 if (expr->type != pet_expr_access)
1845 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1846 "not an access expression", return pet_expr_free(expr));
1848 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1849 if (!expr->acc.access[type])
1850 continue;
1851 expr->acc.access[type] =
1852 isl_union_map_reset_user(expr->acc.access[type]);
1853 if (!expr->acc.access[type])
1854 break;
1856 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
1857 if (type < pet_expr_access_end || !expr->acc.index)
1858 return pet_expr_free(expr);
1860 return expr;
1863 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
1865 return pet_expr_map_access(expr, &access_anonymize, NULL);
1868 /* Data used in access_gist() callback.
1870 struct pet_access_gist_data {
1871 isl_set *domain;
1872 isl_union_map *value_bounds;
1875 /* Given an expression "expr" of type pet_expr_access, compute
1876 * the gist of the associated access relations (if any) and index expression
1877 * with respect to data->domain and the bounds on the values of the arguments
1878 * of the expression.
1880 * The arguments of "expr" have been gisted right before "expr" itself
1881 * is gisted. The gisted arguments may have become equal where before
1882 * they may not have been (obviously) equal. We therefore take
1883 * the opportunity to remove duplicate arguments here.
1885 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
1887 struct pet_access_gist_data *data = user;
1888 isl_set *domain;
1889 isl_union_set *uset;
1890 enum pet_expr_access_type type;
1892 expr = pet_expr_remove_duplicate_args(expr);
1893 expr = pet_expr_cow(expr);
1894 if (!expr)
1895 return expr;
1896 if (expr->type != pet_expr_access)
1897 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1898 "not an access expression", return pet_expr_free(expr));
1900 domain = isl_set_copy(data->domain);
1901 if (expr->n_arg > 0)
1902 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
1903 data->value_bounds);
1905 uset = isl_union_set_from_set(isl_set_copy(domain));
1906 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1907 if (!expr->acc.access[type])
1908 continue;
1909 expr->acc.access[type] =
1910 isl_union_map_gist_domain(expr->acc.access[type],
1911 isl_union_set_copy(uset));
1912 if (!expr->acc.access[type])
1913 break;
1915 isl_union_set_free(uset);
1916 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
1917 if (type < pet_expr_access_end || !expr->acc.index)
1918 return pet_expr_free(expr);
1920 return expr;
1923 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
1924 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
1926 struct pet_access_gist_data data = { context, value_bounds };
1928 return pet_expr_map_access(expr, &access_gist, &data);
1931 /* Mark "expr" as a read dependening on "read".
1933 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
1934 int read)
1936 if (!expr)
1937 return pet_expr_free(expr);
1938 if (expr->type != pet_expr_access)
1939 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1940 "not an access expression", return pet_expr_free(expr));
1941 if (expr->acc.read == read)
1942 return expr;
1943 expr = pet_expr_cow(expr);
1944 if (!expr)
1945 return NULL;
1946 expr->acc.read = read;
1948 return expr;
1951 /* Mark "expr" as a write dependening on "write".
1953 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
1954 int write)
1956 if (!expr)
1957 return pet_expr_free(expr);
1958 if (expr->type != pet_expr_access)
1959 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1960 "not an access expression", return pet_expr_free(expr));
1961 if (expr->acc.write == write)
1962 return expr;
1963 expr = pet_expr_cow(expr);
1964 if (!expr)
1965 return NULL;
1966 expr->acc.write = write;
1968 return expr;
1971 /* Mark "expr" as a kill dependening on "kill".
1973 __isl_give pet_expr *pet_expr_access_set_kill(__isl_take pet_expr *expr,
1974 int kill)
1976 if (!expr)
1977 return pet_expr_free(expr);
1978 if (expr->type != pet_expr_access)
1979 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1980 "not an access expression", return pet_expr_free(expr));
1981 if (expr->acc.kill == kill)
1982 return expr;
1983 expr = pet_expr_cow(expr);
1984 if (!expr)
1985 return NULL;
1986 expr->acc.kill = kill;
1988 return expr;
1991 /* Map the access type "type" to the corresponding location
1992 * in the access array.
1993 * In particular, the access relation of type pet_expr_access_killed is
1994 * stored in the element at position pet_expr_access_fake_killed.
1996 static enum pet_expr_access_type internalize_type(
1997 enum pet_expr_access_type type)
1999 if (type == pet_expr_access_killed)
2000 return pet_expr_access_fake_killed;
2001 return type;
2004 /* Replace the access relation of the given "type" of "expr" by "access".
2006 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
2007 enum pet_expr_access_type type, __isl_take isl_union_map *access)
2009 expr = pet_expr_cow(expr);
2010 if (!expr || !access)
2011 goto error;
2012 if (expr->type != pet_expr_access)
2013 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2014 "not an access expression", goto error);
2015 type = internalize_type(type);
2016 isl_union_map_free(expr->acc.access[type]);
2017 expr->acc.access[type] = access;
2019 return expr;
2020 error:
2021 isl_union_map_free(access);
2022 pet_expr_free(expr);
2023 return NULL;
2026 /* Replace the index expression of "expr" by "index" and
2027 * set the array depth accordingly.
2029 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
2030 __isl_take isl_multi_pw_aff *index)
2032 expr = pet_expr_cow(expr);
2033 if (!expr || !index)
2034 goto error;
2035 if (expr->type != pet_expr_access)
2036 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2037 "not an access expression", goto error);
2038 isl_multi_pw_aff_free(expr->acc.index);
2039 expr->acc.index = index;
2040 expr->acc.depth = isl_multi_pw_aff_dim(index, isl_dim_out);
2042 return expr;
2043 error:
2044 isl_multi_pw_aff_free(index);
2045 pet_expr_free(expr);
2046 return NULL;
2049 /* Return the reference identifier of access expression "expr".
2051 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
2053 if (!expr)
2054 return NULL;
2055 if (expr->type != pet_expr_access)
2056 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2057 "not an access expression", return NULL);
2059 return isl_id_copy(expr->acc.ref_id);
2062 /* Replace the reference identifier of access expression "expr" by "ref_id".
2064 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
2065 __isl_take isl_id *ref_id)
2067 expr = pet_expr_cow(expr);
2068 if (!expr || !ref_id)
2069 goto error;
2070 if (expr->type != pet_expr_access)
2071 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2072 "not an access expression", goto error);
2073 isl_id_free(expr->acc.ref_id);
2074 expr->acc.ref_id = ref_id;
2076 return expr;
2077 error:
2078 isl_id_free(ref_id);
2079 pet_expr_free(expr);
2080 return NULL;
2083 /* Tag the access relation "access" with "id".
2084 * That is, insert the id as the range of a wrapped relation
2085 * in the domain of "access".
2087 * If "access" is of the form
2089 * D[i] -> A[a]
2091 * then the result is of the form
2093 * [D[i] -> id[]] -> A[a]
2095 __isl_give isl_union_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
2096 __isl_take isl_union_map *access)
2098 isl_space *space;
2099 isl_multi_aff *add_tag;
2100 isl_id *id;
2102 if (expr->type != pet_expr_access)
2103 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2104 "not an access expression",
2105 return isl_union_map_free(access));
2107 id = isl_id_copy(expr->acc.ref_id);
2108 space = pet_expr_access_get_domain_space(expr);
2109 space = isl_space_from_domain(space);
2110 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2111 add_tag = isl_multi_aff_domain_map(space);
2112 access = isl_union_map_preimage_domain_multi_aff(access, add_tag);
2114 return access;
2117 /* Return the access relation of the given "type" associated to "expr"
2118 * that maps pairs of domain iterations and argument values
2119 * to the corresponding accessed data elements.
2121 * If the requested access relation is explicitly available,
2122 * then return a copy. Otherwise, check if it is irrelevant for
2123 * the access expression and return an empty relation if this is the case.
2124 * Otherwise, introduce the requested access relation in "expr" and
2125 * return a copy.
2127 __isl_give isl_union_map *pet_expr_access_get_dependent_access(
2128 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2130 isl_union_map *access;
2131 int empty;
2133 if (!expr)
2134 return NULL;
2135 if (expr->type != pet_expr_access)
2136 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2137 "not an access expression", return NULL);
2139 type = internalize_type(type);
2140 if (expr->acc.access[type])
2141 return isl_union_map_copy(expr->acc.access[type]);
2143 if (type == pet_expr_access_may_read)
2144 empty = !expr->acc.read;
2145 else
2146 empty = !expr->acc.write;
2148 if (!empty) {
2149 expr = pet_expr_copy(expr);
2150 expr = introduce_access_relations(expr);
2151 if (!expr)
2152 return NULL;
2153 access = isl_union_map_copy(expr->acc.access[type]);
2154 pet_expr_free(expr);
2156 return access;
2159 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr));
2162 /* Return the may read access relation associated to "expr"
2163 * that maps pairs of domain iterations and argument values
2164 * to the corresponding accessed data elements.
2166 __isl_give isl_union_map *pet_expr_access_get_dependent_may_read(
2167 __isl_keep pet_expr *expr)
2169 return pet_expr_access_get_dependent_access(expr,
2170 pet_expr_access_may_read);
2173 /* Return the may write access relation associated to "expr"
2174 * that maps pairs of domain iterations and argument values
2175 * to the corresponding accessed data elements.
2177 __isl_give isl_union_map *pet_expr_access_get_dependent_may_write(
2178 __isl_keep pet_expr *expr)
2180 return pet_expr_access_get_dependent_access(expr,
2181 pet_expr_access_may_write);
2184 /* Return the must write access relation associated to "expr"
2185 * that maps pairs of domain iterations and argument values
2186 * to the corresponding accessed data elements.
2188 __isl_give isl_union_map *pet_expr_access_get_dependent_must_write(
2189 __isl_keep pet_expr *expr)
2191 return pet_expr_access_get_dependent_access(expr,
2192 pet_expr_access_must_write);
2195 /* Return the relation of the given "type" mapping domain iterations
2196 * to the accessed data elements.
2197 * In particular, take the access relation and, in case of may_read
2198 * or may_write, project out the values of the arguments, if any.
2199 * In case of must_write, return the empty relation if there are
2200 * any arguments.
2202 __isl_give isl_union_map *pet_expr_access_get_access(__isl_keep pet_expr *expr,
2203 enum pet_expr_access_type type)
2205 isl_union_map *access;
2206 isl_space *space;
2207 isl_map *map;
2209 if (!expr)
2210 return NULL;
2211 if (expr->type != pet_expr_access)
2212 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2213 "not an access expression", return NULL);
2215 if (expr->n_arg != 0 && type == pet_expr_access_must_write) {
2216 space = pet_expr_access_get_parameter_space(expr);
2217 return isl_union_map_empty(space);
2220 access = pet_expr_access_get_dependent_access(expr, type);
2221 if (expr->n_arg == 0)
2222 return access;
2224 space = isl_multi_pw_aff_get_space(expr->acc.index);
2225 space = isl_space_domain(space);
2226 map = isl_map_universe(isl_space_unwrap(space));
2227 map = isl_map_domain_map(map);
2228 access = isl_union_map_apply_domain(access,
2229 isl_union_map_from_map(map));
2231 return access;
2234 /* Return the relation mapping domain iterations to all possibly
2235 * read data elements.
2237 __isl_give isl_union_map *pet_expr_access_get_may_read(
2238 __isl_keep pet_expr *expr)
2240 return pet_expr_access_get_access(expr, pet_expr_access_may_read);
2243 /* Return the relation mapping domain iterations to all possibly
2244 * written data elements.
2246 __isl_give isl_union_map *pet_expr_access_get_may_write(
2247 __isl_keep pet_expr *expr)
2249 return pet_expr_access_get_access(expr, pet_expr_access_may_write);
2252 /* Return a relation mapping domain iterations to definitely
2253 * written data elements, assuming the statement containing
2254 * the expression is executed.
2256 __isl_give isl_union_map *pet_expr_access_get_must_write(
2257 __isl_keep pet_expr *expr)
2259 return pet_expr_access_get_access(expr, pet_expr_access_must_write);
2262 /* Return the relation of the given "type" mapping domain iterations to
2263 * accessed data elements, with its domain tagged with the reference
2264 * identifier.
2266 static __isl_give isl_union_map *pet_expr_access_get_tagged_access(
2267 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2269 isl_union_map *access;
2271 if (!expr)
2272 return NULL;
2274 access = pet_expr_access_get_access(expr, type);
2275 access = pet_expr_tag_access(expr, access);
2277 return access;
2280 /* Return the relation mapping domain iterations to all possibly
2281 * read data elements, with its domain tagged with the reference
2282 * identifier.
2284 __isl_give isl_union_map *pet_expr_access_get_tagged_may_read(
2285 __isl_keep pet_expr *expr)
2287 return pet_expr_access_get_tagged_access(expr,
2288 pet_expr_access_may_read);
2291 /* Return the relation mapping domain iterations to all possibly
2292 * written data elements, with its domain tagged with the reference
2293 * identifier.
2295 __isl_give isl_union_map *pet_expr_access_get_tagged_may_write(
2296 __isl_keep pet_expr *expr)
2298 return pet_expr_access_get_tagged_access(expr,
2299 pet_expr_access_may_write);
2302 /* Return the operation type of operation expression "expr".
2304 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
2306 if (!expr)
2307 return pet_op_last;
2308 if (expr->type != pet_expr_op)
2309 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2310 "not an operation expression", return pet_op_last);
2312 return expr->op;
2315 /* Replace the operation type of operation expression "expr" by "type".
2317 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
2318 enum pet_op_type type)
2320 if (!expr)
2321 return pet_expr_free(expr);
2322 if (expr->type != pet_expr_op)
2323 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2324 "not an operation expression",
2325 return pet_expr_free(expr));
2326 if (expr->op == type)
2327 return expr;
2328 expr = pet_expr_cow(expr);
2329 if (!expr)
2330 return NULL;
2331 expr->op = type;
2333 return expr;
2336 /* Return the name of the function called by "expr".
2338 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
2340 if (!expr)
2341 return NULL;
2342 if (expr->type != pet_expr_call)
2343 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2344 "not a call expression", return NULL);
2345 return expr->name;
2348 /* Replace the name of the function called by "expr" by "name".
2350 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
2351 __isl_keep const char *name)
2353 expr = pet_expr_cow(expr);
2354 if (!expr || !name)
2355 return pet_expr_free(expr);
2356 if (expr->type != pet_expr_call)
2357 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2358 "not a call expression", return pet_expr_free(expr));
2359 free(expr->name);
2360 expr->name = strdup(name);
2361 if (!expr->name)
2362 return pet_expr_free(expr);
2363 return expr;
2366 /* Replace the type of the cast performed by "expr" by "name".
2368 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
2369 __isl_keep const char *name)
2371 expr = pet_expr_cow(expr);
2372 if (!expr || !name)
2373 return pet_expr_free(expr);
2374 if (expr->type != pet_expr_cast)
2375 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2376 "not a cast expression", return pet_expr_free(expr));
2377 free(expr->type_name);
2378 expr->type_name = strdup(name);
2379 if (!expr->type_name)
2380 return pet_expr_free(expr);
2381 return expr;
2384 /* Return the value of the integer represented by "expr".
2386 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
2388 if (!expr)
2389 return NULL;
2390 if (expr->type != pet_expr_int)
2391 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2392 "not an int expression", return NULL);
2394 return isl_val_copy(expr->i);
2397 /* Replace the value of the integer represented by "expr" by "v".
2399 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
2400 __isl_take isl_val *v)
2402 expr = pet_expr_cow(expr);
2403 if (!expr || !v)
2404 goto error;
2405 if (expr->type != pet_expr_int)
2406 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2407 "not an int expression", goto error);
2408 isl_val_free(expr->i);
2409 expr->i = v;
2411 return expr;
2412 error:
2413 isl_val_free(v);
2414 pet_expr_free(expr);
2415 return NULL;
2418 /* Replace the value and string representation of the double
2419 * represented by "expr" by "d" and "s".
2421 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2422 double d, __isl_keep const char *s)
2424 expr = pet_expr_cow(expr);
2425 if (!expr || !s)
2426 return pet_expr_free(expr);
2427 if (expr->type != pet_expr_double)
2428 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2429 "not a double expression", return pet_expr_free(expr));
2430 expr->d.val = d;
2431 free(expr->d.s);
2432 expr->d.s = strdup(s);
2433 if (!expr->d.s)
2434 return pet_expr_free(expr);
2435 return expr;
2438 /* Return a string representation of the double expression "expr".
2440 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2442 if (!expr)
2443 return NULL;
2444 if (expr->type != pet_expr_double)
2445 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2446 "not a double expression", return NULL);
2447 return strdup(expr->d.s);
2450 /* Return a piecewise affine expression defined on the specified domain
2451 * that represents NaN.
2453 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2455 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2458 /* This function is called when we come across an access that is
2459 * nested in what is supposed to be an affine expression.
2460 * "pc" is the context in which the affine expression is created.
2461 * If nesting is allowed in "pc", we return an affine expression that is
2462 * equal to a new parameter corresponding to this nested access.
2463 * Otherwise, we return NaN.
2465 * Note that we currently don't allow nested accesses themselves
2466 * to contain any nested accesses, so we check if "expr" itself
2467 * involves any nested accesses (either explicitly as arguments
2468 * or implicitly through parameters) and return NaN if it does.
2470 * The new parameter is resolved in resolve_nested.
2472 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2473 __isl_keep pet_context *pc)
2475 isl_ctx *ctx;
2476 isl_id *id;
2477 isl_space *space;
2478 isl_local_space *ls;
2479 isl_aff *aff;
2480 int nested;
2482 if (!expr || !pc)
2483 return NULL;
2484 if (!pet_context_allow_nesting(pc))
2485 return non_affine(pet_context_get_space(pc));
2487 if (pet_expr_get_type(expr) != pet_expr_access)
2488 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2489 "not an access expression", return NULL);
2491 if (expr->n_arg > 0)
2492 return non_affine(pet_context_get_space(pc));
2494 space = pet_expr_access_get_parameter_space(expr);
2495 nested = pet_nested_any_in_space(space);
2496 isl_space_free(space);
2497 if (nested)
2498 return non_affine(pet_context_get_space(pc));
2500 ctx = pet_expr_get_ctx(expr);
2501 id = pet_nested_pet_expr(pet_expr_copy(expr));
2502 space = pet_context_get_space(pc);
2503 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2505 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2506 ls = isl_local_space_from_space(space);
2507 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2509 return isl_pw_aff_from_aff(aff);
2512 /* Extract an affine expression from the access pet_expr "expr".
2513 * "pc" is the context in which the affine expression is created.
2515 * If "expr" is actually an affine expression rather than
2516 * a real access, then we return that expression.
2517 * Otherwise, we require that "expr" is of an integral type.
2518 * If not, we return NaN.
2520 * If the variable has been assigned a known affine expression,
2521 * then we return that expression.
2523 * Otherwise, we return an expression that is equal to a parameter
2524 * representing "expr" (if "allow_nested" is set).
2526 static __isl_give isl_pw_aff *extract_affine_from_access(
2527 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2529 int pos;
2530 isl_id *id;
2532 if (pet_expr_is_affine(expr)) {
2533 isl_pw_aff *pa;
2534 isl_multi_pw_aff *mpa;
2536 mpa = pet_expr_access_get_index(expr);
2537 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
2538 isl_multi_pw_aff_free(mpa);
2539 return pa;
2542 if (pet_expr_get_type_size(expr) == 0)
2543 return non_affine(pet_context_get_space(pc));
2545 if (!pet_expr_is_scalar_access(expr))
2546 return nested_access(expr, pc);
2548 id = pet_expr_access_get_id(expr);
2549 if (pet_context_is_assigned(pc, id))
2550 return pet_context_get_value(pc, id);
2552 isl_id_free(id);
2553 return nested_access(expr, pc);
2556 /* Construct an affine expression from the integer constant "expr".
2557 * "pc" is the context in which the affine expression is created.
2559 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2560 __isl_keep pet_context *pc)
2562 isl_local_space *ls;
2563 isl_aff *aff;
2565 if (!expr)
2566 return NULL;
2568 ls = isl_local_space_from_space(pet_context_get_space(pc));
2569 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2571 return isl_pw_aff_from_aff(aff);
2574 /* Extract an affine expression from an addition or subtraction operation.
2575 * Return NaN if we are unable to extract an affine expression.
2577 * "pc" is the context in which the affine expression is created.
2579 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2580 __isl_keep pet_context *pc)
2582 isl_pw_aff *lhs;
2583 isl_pw_aff *rhs;
2585 if (!expr)
2586 return NULL;
2587 if (expr->n_arg != 2)
2588 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2589 "expecting two arguments", return NULL);
2591 lhs = pet_expr_extract_affine(expr->args[0], pc);
2592 rhs = pet_expr_extract_affine(expr->args[1], pc);
2594 switch (pet_expr_op_get_type(expr)) {
2595 case pet_op_add:
2596 return isl_pw_aff_add(lhs, rhs);
2597 case pet_op_sub:
2598 return isl_pw_aff_sub(lhs, rhs);
2599 default:
2600 isl_pw_aff_free(lhs);
2601 isl_pw_aff_free(rhs);
2602 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2603 "not an addition or subtraction operation",
2604 return NULL);
2609 /* Extract an affine expression from an integer division or a modulo operation.
2610 * Return NaN if we are unable to extract an affine expression.
2612 * "pc" is the context in which the affine expression is created.
2614 * In particular, if "expr" is lhs/rhs, then return
2616 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2618 * If "expr" is lhs%rhs, then return
2620 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2622 * If the second argument (rhs) is not a (positive) integer constant,
2623 * then we fail to extract an affine expression.
2625 * We simplify the result in the context of the domain of "pc" in case
2626 * this domain implies that lhs >= 0 (or < 0).
2628 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2629 __isl_keep pet_context *pc)
2631 int is_cst;
2632 isl_pw_aff *lhs;
2633 isl_pw_aff *rhs;
2634 isl_pw_aff *res;
2636 if (!expr)
2637 return NULL;
2638 if (expr->n_arg != 2)
2639 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2640 "expecting two arguments", return NULL);
2642 rhs = pet_expr_extract_affine(expr->args[1], pc);
2644 is_cst = isl_pw_aff_is_cst(rhs);
2645 if (is_cst < 0 || !is_cst) {
2646 isl_pw_aff_free(rhs);
2647 return non_affine(pet_context_get_space(pc));
2650 lhs = pet_expr_extract_affine(expr->args[0], pc);
2652 switch (pet_expr_op_get_type(expr)) {
2653 case pet_op_div:
2654 res = isl_pw_aff_tdiv_q(lhs, rhs);
2655 break;
2656 case pet_op_mod:
2657 res = isl_pw_aff_tdiv_r(lhs, rhs);
2658 break;
2659 default:
2660 isl_pw_aff_free(lhs);
2661 isl_pw_aff_free(rhs);
2662 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2663 "not a div or mod operator", return NULL);
2666 return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc));
2669 /* Extract an affine expression from a multiplication operation.
2670 * Return NaN if we are unable to extract an affine expression.
2671 * In particular, if neither of the arguments is a (piecewise) constant
2672 * then we return NaN.
2674 * "pc" is the context in which the affine expression is created.
2676 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2677 __isl_keep pet_context *pc)
2679 int lhs_cst, rhs_cst;
2680 isl_pw_aff *lhs;
2681 isl_pw_aff *rhs;
2683 if (!expr)
2684 return NULL;
2685 if (expr->n_arg != 2)
2686 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2687 "expecting two arguments", return NULL);
2689 lhs = pet_expr_extract_affine(expr->args[0], pc);
2690 rhs = pet_expr_extract_affine(expr->args[1], pc);
2692 lhs_cst = isl_pw_aff_is_cst(lhs);
2693 rhs_cst = isl_pw_aff_is_cst(rhs);
2694 if (lhs_cst < 0 || rhs_cst < 0 || (!lhs_cst && !rhs_cst)) {
2695 isl_pw_aff_free(lhs);
2696 isl_pw_aff_free(rhs);
2697 return non_affine(pet_context_get_space(pc));
2700 return isl_pw_aff_mul(lhs, rhs);
2703 /* Extract an affine expression from a negation operation.
2704 * Return NaN if we are unable to extract an affine expression.
2706 * "pc" is the context in which the affine expression is created.
2708 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
2709 __isl_keep pet_context *pc)
2711 isl_pw_aff *res;
2713 if (!expr)
2714 return NULL;
2715 if (expr->n_arg != 1)
2716 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2717 "expecting one argument", return NULL);
2719 res = pet_expr_extract_affine(expr->args[0], pc);
2720 return isl_pw_aff_neg(res);
2723 /* Extract an affine expression from a conditional operation.
2724 * Return NaN if we are unable to extract an affine expression.
2726 * "pc" is the context in which the affine expression is created.
2728 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
2729 __isl_keep pet_context *pc)
2731 isl_pw_aff *cond, *lhs, *rhs;
2733 if (!expr)
2734 return NULL;
2735 if (expr->n_arg != 3)
2736 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2737 "expecting three arguments", return NULL);
2739 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2740 lhs = pet_expr_extract_affine(expr->args[1], pc);
2741 rhs = pet_expr_extract_affine(expr->args[2], pc);
2743 return isl_pw_aff_cond(cond, lhs, rhs);
2746 /* Compute
2748 * pwaff mod 2^width
2750 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff, unsigned width)
2752 isl_ctx *ctx;
2753 isl_val *mod;
2755 ctx = isl_pw_aff_get_ctx(pwaff);
2756 mod = isl_val_int_from_ui(ctx, width);
2757 mod = isl_val_2exp(mod);
2759 pwaff = isl_pw_aff_mod_val(pwaff, mod);
2761 return pwaff;
2764 /* Limit the domain of "pwaff" to those elements where the function
2765 * value satisfies
2767 * 2^{width-1} <= pwaff < 2^{width-1}
2769 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
2770 unsigned width)
2772 isl_ctx *ctx;
2773 isl_val *v;
2774 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
2775 isl_local_space *ls = isl_local_space_from_space(space);
2776 isl_aff *bound;
2777 isl_set *dom;
2778 isl_pw_aff *b;
2780 ctx = isl_pw_aff_get_ctx(pwaff);
2781 v = isl_val_int_from_ui(ctx, width - 1);
2782 v = isl_val_2exp(v);
2784 bound = isl_aff_zero_on_domain(ls);
2785 bound = isl_aff_add_constant_val(bound, v);
2786 b = isl_pw_aff_from_aff(bound);
2788 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
2789 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2791 b = isl_pw_aff_neg(b);
2792 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
2793 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2795 return pwaff;
2798 /* Handle potential overflows on signed computations.
2800 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2801 * then we adjust the domain of "pa" to avoid overflows.
2803 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
2804 unsigned width)
2806 isl_ctx *ctx;
2807 struct pet_options *options;
2809 if (!pa)
2810 return NULL;
2812 ctx = isl_pw_aff_get_ctx(pa);
2813 options = isl_ctx_peek_pet_options(ctx);
2814 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
2815 pa = avoid_overflow(pa, width);
2817 return pa;
2820 /* Extract an affine expression from some an operation.
2821 * Return NaN if we are unable to extract an affine expression.
2822 * If the result of a binary (non boolean) operation is unsigned,
2823 * then we wrap it based on the size of the type. If the result is signed,
2824 * then we ensure that no overflow occurs.
2826 * "pc" is the context in which the affine expression is created.
2828 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
2829 __isl_keep pet_context *pc)
2831 isl_pw_aff *res;
2832 int type_size;
2834 switch (pet_expr_op_get_type(expr)) {
2835 case pet_op_add:
2836 case pet_op_sub:
2837 res = extract_affine_add_sub(expr, pc);
2838 break;
2839 case pet_op_div:
2840 case pet_op_mod:
2841 res = extract_affine_div_mod(expr, pc);
2842 break;
2843 case pet_op_mul:
2844 res = extract_affine_mul(expr, pc);
2845 break;
2846 case pet_op_minus:
2847 return extract_affine_neg(expr, pc);
2848 case pet_op_cond:
2849 return extract_affine_cond(expr, pc);
2850 case pet_op_eq:
2851 case pet_op_ne:
2852 case pet_op_le:
2853 case pet_op_ge:
2854 case pet_op_lt:
2855 case pet_op_gt:
2856 case pet_op_land:
2857 case pet_op_lor:
2858 case pet_op_lnot:
2859 return pet_expr_extract_affine_condition(expr, pc);
2860 default:
2861 return non_affine(pet_context_get_space(pc));
2864 if (!res)
2865 return NULL;
2866 if (isl_pw_aff_involves_nan(res)) {
2867 isl_space *space = isl_pw_aff_get_domain_space(res);
2868 isl_pw_aff_free(res);
2869 return non_affine(space);
2872 type_size = pet_expr_get_type_size(expr);
2873 if (type_size > 0)
2874 res = wrap(res, type_size);
2875 else
2876 res = signed_overflow(res, -type_size);
2878 return res;
2881 /* Extract an affine expression from some special function calls.
2882 * Return NaN if we are unable to extract an affine expression.
2883 * In particular, we handle "min", "max", "ceild", "floord",
2884 * "intMod", "intFloor" and "intCeil".
2885 * In case of the latter five, the second argument needs to be
2886 * a (positive) integer constant.
2888 * "pc" is the context in which the affine expression is created.
2890 static __isl_give isl_pw_aff *extract_affine_from_call(
2891 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2893 isl_pw_aff *aff1, *aff2;
2894 int n;
2895 const char *name;
2897 n = pet_expr_get_n_arg(expr);
2898 name = pet_expr_call_get_name(expr);
2899 if (!(n == 2 && !strcmp(name, "min")) &&
2900 !(n == 2 && !strcmp(name, "max")) &&
2901 !(n == 2 && !strcmp(name, "intMod")) &&
2902 !(n == 2 && !strcmp(name, "intFloor")) &&
2903 !(n == 2 && !strcmp(name, "intCeil")) &&
2904 !(n == 2 && !strcmp(name, "floord")) &&
2905 !(n == 2 && !strcmp(name, "ceild")))
2906 return non_affine(pet_context_get_space(pc));
2908 if (!strcmp(name, "min") || !strcmp(name, "max")) {
2909 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2910 aff2 = pet_expr_extract_affine(expr->args[1], pc);
2912 if (!strcmp(name, "min"))
2913 aff1 = isl_pw_aff_min(aff1, aff2);
2914 else
2915 aff1 = isl_pw_aff_max(aff1, aff2);
2916 } else if (!strcmp(name, "intMod")) {
2917 isl_val *v;
2919 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2920 return non_affine(pet_context_get_space(pc));
2921 v = pet_expr_int_get_val(expr->args[1]);
2922 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2923 aff1 = isl_pw_aff_mod_val(aff1, v);
2924 } else {
2925 isl_val *v;
2927 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
2928 return non_affine(pet_context_get_space(pc));
2929 v = pet_expr_int_get_val(expr->args[1]);
2930 aff1 = pet_expr_extract_affine(expr->args[0], pc);
2931 aff1 = isl_pw_aff_scale_down_val(aff1, v);
2932 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
2933 aff1 = isl_pw_aff_floor(aff1);
2934 else
2935 aff1 = isl_pw_aff_ceil(aff1);
2938 return aff1;
2941 /* Extract an affine expression from "expr", if possible.
2942 * Otherwise return NaN.
2944 * "pc" is the context in which the affine expression is created.
2946 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
2947 __isl_keep pet_context *pc)
2949 if (!expr)
2950 return NULL;
2952 switch (pet_expr_get_type(expr)) {
2953 case pet_expr_access:
2954 return extract_affine_from_access(expr, pc);
2955 case pet_expr_int:
2956 return extract_affine_from_int(expr, pc);
2957 case pet_expr_op:
2958 return extract_affine_from_op(expr, pc);
2959 case pet_expr_call:
2960 return extract_affine_from_call(expr, pc);
2961 case pet_expr_cast:
2962 case pet_expr_double:
2963 case pet_expr_error:
2964 return non_affine(pet_context_get_space(pc));
2968 /* Extract an affine expressions representing the comparison "LHS op RHS"
2969 * Return NaN if we are unable to extract such an affine expression.
2971 * "pc" is the context in which the affine expression is created.
2973 * If the comparison is of the form
2975 * a <= min(b,c)
2977 * then the expression is constructed as the conjunction of
2978 * the comparisons
2980 * a <= b and a <= c
2982 * A similar optimization is performed for max(a,b) <= c.
2983 * We do this because that will lead to simpler representations
2984 * of the expression.
2985 * If isl is ever enhanced to explicitly deal with min and max expressions,
2986 * this optimization can be removed.
2988 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
2989 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
2990 __isl_keep pet_context *pc)
2992 isl_pw_aff *lhs_pa, *rhs_pa;
2994 if (op == pet_op_gt)
2995 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
2996 if (op == pet_op_ge)
2997 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
2999 if (op == pet_op_lt || op == pet_op_le) {
3000 if (pet_expr_is_min(rhs)) {
3001 lhs_pa = pet_expr_extract_comparison(op, lhs,
3002 rhs->args[0], pc);
3003 rhs_pa = pet_expr_extract_comparison(op, lhs,
3004 rhs->args[1], pc);
3005 return pet_and(lhs_pa, rhs_pa);
3007 if (pet_expr_is_max(lhs)) {
3008 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
3009 rhs, pc);
3010 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
3011 rhs, pc);
3012 return pet_and(lhs_pa, rhs_pa);
3016 lhs_pa = pet_expr_extract_affine(lhs, pc);
3017 rhs_pa = pet_expr_extract_affine(rhs, pc);
3019 return pet_comparison(op, lhs_pa, rhs_pa);
3022 /* Extract an affine expressions from the comparison "expr".
3023 * Return NaN if we are unable to extract such an affine expression.
3025 * "pc" is the context in which the affine expression is created.
3027 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
3028 __isl_keep pet_context *pc)
3030 enum pet_op_type type;
3032 if (!expr)
3033 return NULL;
3034 if (expr->n_arg != 2)
3035 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3036 "expecting two arguments", return NULL);
3038 type = pet_expr_op_get_type(expr);
3039 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
3040 pc);
3043 /* Extract an affine expression representing the boolean operation
3044 * expressed by "expr".
3045 * Return NaN if we are unable to extract an affine expression.
3047 * "pc" is the context in which the affine expression is created.
3049 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
3050 __isl_keep pet_context *pc)
3052 isl_pw_aff *lhs, *rhs;
3053 int n;
3055 if (!expr)
3056 return NULL;
3058 n = pet_expr_get_n_arg(expr);
3059 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
3060 if (n == 1)
3061 return pet_not(lhs);
3063 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
3064 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
3067 /* Extract the affine expression "expr != 0 ? 1 : 0".
3068 * Return NaN if we are unable to extract an affine expression.
3070 * "pc" is the context in which the affine expression is created.
3072 static __isl_give isl_pw_aff *extract_implicit_condition(
3073 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3075 isl_pw_aff *res;
3077 res = pet_expr_extract_affine(expr, pc);
3078 return pet_to_bool(res);
3081 /* Extract a boolean affine expression from "expr".
3082 * Return NaN if we are unable to extract an affine expression.
3084 * "pc" is the context in which the affine expression is created.
3086 * If "expr" is neither a comparison nor a boolean operation,
3087 * then we assume it is an affine expression and return the
3088 * boolean expression "expr != 0 ? 1 : 0".
3090 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
3091 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3093 if (!expr)
3094 return NULL;
3096 if (pet_expr_is_comparison(expr))
3097 return extract_comparison(expr, pc);
3098 if (pet_expr_is_boolean(expr))
3099 return extract_boolean(expr, pc);
3101 return extract_implicit_condition(expr, pc);
3104 /* Check if "expr" is an assume expression and if its single argument
3105 * can be converted to an affine expression in the context of "pc".
3106 * If so, replace the argument by the affine expression.
3108 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
3109 __isl_keep pet_context *pc)
3111 isl_pw_aff *cond;
3112 isl_multi_pw_aff *index;
3114 if (!expr)
3115 return NULL;
3116 if (!pet_expr_is_assume(expr))
3117 return expr;
3118 if (expr->n_arg != 1)
3119 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3120 "expecting one argument", return pet_expr_free(expr));
3122 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3123 if (!cond)
3124 return pet_expr_free(expr);
3125 if (isl_pw_aff_involves_nan(cond)) {
3126 isl_pw_aff_free(cond);
3127 return expr;
3130 index = isl_multi_pw_aff_from_pw_aff(cond);
3131 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
3133 return expr;
3136 /* Return the number of bits needed to represent the type of "expr".
3137 * See the description of the type_size field of pet_expr.
3139 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
3141 return expr ? expr->type_size : 0;
3144 /* Replace the number of bits needed to represent the type of "expr"
3145 * by "type_size".
3146 * See the description of the type_size field of pet_expr.
3148 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
3149 int type_size)
3151 expr = pet_expr_cow(expr);
3152 if (!expr)
3153 return NULL;
3155 expr->type_size = type_size;
3157 return expr;
3160 /* Extend an access expression "expr" with an additional index "index".
3161 * In particular, add "index" as an extra argument to "expr" and
3162 * adjust the index expression of "expr" to refer to this extra argument.
3163 * The caller is responsible for calling pet_expr_access_set_depth
3164 * to update the corresponding access relation.
3166 * Note that we only collect the individual index expressions as
3167 * arguments of "expr" here.
3168 * An attempt to integrate them into the index expression of "expr"
3169 * is performed in pet_expr_access_plug_in_args.
3171 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
3172 __isl_take pet_expr *index)
3174 int n;
3175 isl_space *space;
3176 isl_local_space *ls;
3177 isl_pw_aff *pa;
3179 expr = pet_expr_cow(expr);
3180 if (!expr || !index)
3181 goto error;
3182 if (expr->type != pet_expr_access)
3183 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3184 "not an access pet_expr", goto error);
3186 n = pet_expr_get_n_arg(expr);
3187 expr = pet_expr_insert_arg(expr, n, index);
3188 if (!expr)
3189 return NULL;
3191 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3192 ls = isl_local_space_from_space(space);
3193 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
3194 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
3195 if (!expr->acc.index)
3196 return pet_expr_free(expr);
3198 return expr;
3199 error:
3200 pet_expr_free(expr);
3201 pet_expr_free(index);
3202 return NULL;
3205 /* Extend an access expression "expr" with an additional member acces to "id".
3206 * In particular, extend the index expression of "expr" to include
3207 * the additional member access.
3208 * The caller is responsible for calling pet_expr_access_set_depth
3209 * to update the corresponding access relation.
3211 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
3212 __isl_take isl_id *id)
3214 isl_space *space;
3215 isl_multi_pw_aff *field_access;
3217 expr = pet_expr_cow(expr);
3218 if (!expr || !id)
3219 goto error;
3220 if (expr->type != pet_expr_access)
3221 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3222 "not an access pet_expr", goto error);
3224 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3225 space = isl_space_from_domain(space);
3226 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3227 field_access = isl_multi_pw_aff_zero(space);
3228 expr->acc.index = pet_array_member(expr->acc.index, field_access);
3229 if (!expr->acc.index)
3230 return pet_expr_free(expr);
3232 return expr;
3233 error:
3234 pet_expr_free(expr);
3235 isl_id_free(id);
3236 return NULL;
3239 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
3241 int i;
3243 if (!expr)
3244 return;
3246 fprintf(stderr, "%*s", indent, "");
3248 switch (expr->type) {
3249 case pet_expr_double:
3250 fprintf(stderr, "%s\n", expr->d.s);
3251 break;
3252 case pet_expr_int:
3253 isl_val_dump(expr->i);
3254 break;
3255 case pet_expr_access:
3256 if (expr->acc.ref_id) {
3257 isl_id_dump(expr->acc.ref_id);
3258 fprintf(stderr, "%*s", indent, "");
3260 isl_multi_pw_aff_dump(expr->acc.index);
3261 fprintf(stderr, "%*sdepth: %d\n", indent + 2,
3262 "", expr->acc.depth);
3263 if (expr->acc.kill) {
3264 fprintf(stderr, "%*skill: 1\n", indent + 2, "");
3265 } else {
3266 fprintf(stderr, "%*sread: %d\n", indent + 2,
3267 "", expr->acc.read);
3268 fprintf(stderr, "%*swrite: %d\n", indent + 2,
3269 "", expr->acc.write);
3271 if (expr->acc.access[pet_expr_access_may_read]) {
3272 fprintf(stderr, "%*smay_read: ", indent + 2, "");
3273 isl_union_map_dump(
3274 expr->acc.access[pet_expr_access_may_read]);
3276 if (expr->acc.access[pet_expr_access_may_write]) {
3277 fprintf(stderr, "%*smay_write: ", indent + 2, "");
3278 isl_union_map_dump(
3279 expr->acc.access[pet_expr_access_may_write]);
3281 if (expr->acc.access[pet_expr_access_must_write]) {
3282 fprintf(stderr, "%*smust_write: ", indent + 2, "");
3283 isl_union_map_dump(
3284 expr->acc.access[pet_expr_access_must_write]);
3286 for (i = 0; i < expr->n_arg; ++i)
3287 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3288 break;
3289 case pet_expr_op:
3290 fprintf(stderr, "%s\n", op_str[expr->op]);
3291 for (i = 0; i < expr->n_arg; ++i)
3292 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3293 break;
3294 case pet_expr_call:
3295 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
3296 for (i = 0; i < expr->n_arg; ++i)
3297 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3298 break;
3299 case pet_expr_cast:
3300 fprintf(stderr, "(%s)\n", expr->type_name);
3301 for (i = 0; i < expr->n_arg; ++i)
3302 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3303 break;
3304 case pet_expr_error:
3305 fprintf(stderr, "ERROR\n");
3306 break;
3310 void pet_expr_dump(__isl_keep pet_expr *expr)
3312 pet_expr_dump_with_indent(expr, 0);