extract out pet_killed_locals
[pet.git] / expr.c
blobbc8b1747df33659a84925a42f2891cc0efa6a4bd
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 <isl/union_set.h>
39 #include "aff.h"
40 #include "array.h"
41 #include "expr.h"
42 #include "expr_arg.h"
43 #include "filter.h"
44 #include "nest.h"
45 #include "options.h"
46 #include "value_bounds.h"
47 #include "patch.h"
49 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
51 static char *type_str[] = {
52 [pet_expr_access] = "access",
53 [pet_expr_call] = "call",
54 [pet_expr_cast] = "cast",
55 [pet_expr_double] = "double",
56 [pet_expr_int] = "int",
57 [pet_expr_op] = "op",
60 static char *op_str[] = {
61 [pet_op_add_assign] = "+=",
62 [pet_op_sub_assign] = "-=",
63 [pet_op_mul_assign] = "*=",
64 [pet_op_div_assign] = "/=",
65 [pet_op_assign] = "=",
66 [pet_op_add] = "+",
67 [pet_op_sub] = "-",
68 [pet_op_mul] = "*",
69 [pet_op_div] = "/",
70 [pet_op_mod] = "%",
71 [pet_op_shl] = "<<",
72 [pet_op_shr] = ">>",
73 [pet_op_eq] = "==",
74 [pet_op_ne] = "!=",
75 [pet_op_le] = "<=",
76 [pet_op_ge] = ">=",
77 [pet_op_lt] = "<",
78 [pet_op_gt] = ">",
79 [pet_op_minus] = "-",
80 [pet_op_post_inc] = "++",
81 [pet_op_post_dec] = "--",
82 [pet_op_pre_inc] = "++",
83 [pet_op_pre_dec] = "--",
84 [pet_op_address_of] = "&",
85 [pet_op_and] = "&",
86 [pet_op_xor] = "^",
87 [pet_op_or] = "|",
88 [pet_op_not] = "~",
89 [pet_op_land] = "&&",
90 [pet_op_lor] = "||",
91 [pet_op_lnot] = "!",
92 [pet_op_cond] = "?:",
93 [pet_op_assume] = "assume",
94 [pet_op_kill] = "kill"
97 const char *pet_op_str(enum pet_op_type op)
99 return op_str[op];
102 int pet_op_is_inc_dec(enum pet_op_type op)
104 return op == pet_op_post_inc || op == pet_op_post_dec ||
105 op == pet_op_pre_inc || op == pet_op_pre_dec;
108 const char *pet_type_str(enum pet_expr_type type)
110 return type_str[type];
113 enum pet_op_type pet_str_op(const char *str)
115 int i;
117 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
118 if (!strcmp(op_str[i], str))
119 return i;
121 return -1;
124 enum pet_expr_type pet_str_type(const char *str)
126 int i;
128 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
129 if (!strcmp(type_str[i], str))
130 return i;
132 return -1;
135 /* Construct a pet_expr of the given type.
137 __isl_give pet_expr *pet_expr_alloc(isl_ctx *ctx, enum pet_expr_type type)
139 pet_expr *expr;
141 expr = isl_calloc_type(ctx, struct pet_expr);
142 if (!expr)
143 return NULL;
145 expr->ctx = ctx;
146 isl_ctx_ref(ctx);
147 expr->type = type;
148 expr->ref = 1;
150 return expr;
153 /* Construct an access pet_expr from an index expression.
154 * By default, the access is considered to be a read access.
155 * The initial depth is set from the index expression and
156 * may still be updated by the caller before the access relation
157 * is created.
159 __isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
161 isl_ctx *ctx;
162 pet_expr *expr;
164 if (!index)
165 return NULL;
166 ctx = isl_multi_pw_aff_get_ctx(index);
167 expr = pet_expr_alloc(ctx, pet_expr_access);
168 if (!expr)
169 goto error;
171 expr->acc.read = 1;
172 expr->acc.write = 0;
174 expr = pet_expr_access_set_index(expr, index);
176 return expr;
177 error:
178 isl_multi_pw_aff_free(index);
179 return NULL;
182 /* Extend the range of "access" with "n" dimensions, retaining
183 * the tuple identifier on this range.
185 * If "access" represents a member access, then extend the range
186 * of the member.
188 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
190 isl_id *id;
192 id = isl_map_get_tuple_id(access, isl_dim_out);
194 if (!isl_map_range_is_wrapping(access)) {
195 access = isl_map_add_dims(access, isl_dim_out, n);
196 } else {
197 isl_map *domain;
199 domain = isl_map_copy(access);
200 domain = isl_map_range_factor_domain(domain);
201 access = isl_map_range_factor_range(access);
202 access = extend_range(access, n);
203 access = isl_map_range_product(domain, access);
206 access = isl_map_set_tuple_id(access, isl_dim_out, id);
208 return access;
211 /* Does the access expression "expr" have any explicit access relation?
213 static int has_any_access_relation(__isl_keep pet_expr *expr)
215 enum pet_expr_access_type type;
217 if (!expr)
218 return -1;
220 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type)
221 if (expr->acc.access[type])
222 return 1;
224 return 0;
227 /* Are all relevant access relations explicitly available in "expr"?
229 static int has_relevant_access_relations(__isl_keep pet_expr *expr)
231 enum pet_expr_access_type type;
233 if (!expr)
234 return -1;
236 if (expr->acc.kill && !expr->acc.access[pet_expr_access_fake_killed])
237 return 0;
238 if (expr->acc.read && !expr->acc.access[pet_expr_access_may_read])
239 return 0;
240 if (expr->acc.write &&
241 (!expr->acc.access[pet_expr_access_may_write] ||
242 !expr->acc.access[pet_expr_access_must_write]))
243 return 0;
245 return 1;
248 /* Replace the depth of the access expr "expr" by "depth".
250 * To avoid inconsistencies between the depth and the access relation,
251 * we currently do not allow the depth to change once the access relation
252 * has been set or computed.
254 __isl_give pet_expr *pet_expr_access_set_depth(__isl_take pet_expr *expr,
255 int depth)
257 isl_map *access;
258 int dim;
260 if (!expr)
261 return NULL;
262 if (expr->acc.depth == depth)
263 return expr;
264 if (has_any_access_relation(expr))
265 isl_die(pet_expr_get_ctx(expr), isl_error_unsupported,
266 "depth cannot be changed after access relation "
267 "has been set or computed", return pet_expr_free(expr));
269 expr = pet_expr_cow(expr);
270 if (!expr)
271 return NULL;
272 expr->acc.depth = depth;
274 return expr;
277 /* Construct a pet_expr that kills the elements specified by
278 * the index expression "index" and the access relation "access".
280 __isl_give pet_expr *pet_expr_kill_from_access_and_index(
281 __isl_take isl_map *access, __isl_take isl_multi_pw_aff *index)
283 int depth;
284 pet_expr *expr;
286 if (!access || !index)
287 goto error;
289 expr = pet_expr_from_index(index);
290 expr = pet_expr_access_set_read(expr, 0);
291 expr = pet_expr_access_set_kill(expr, 1);
292 depth = isl_map_dim(access, isl_dim_out);
293 expr = pet_expr_access_set_depth(expr, depth);
294 expr = pet_expr_access_set_access(expr, pet_expr_access_killed,
295 isl_union_map_from_map(access));
296 return pet_expr_new_unary(0, pet_op_kill, expr);
297 error:
298 isl_map_free(access);
299 isl_multi_pw_aff_free(index);
300 return NULL;
303 /* Construct a unary pet_expr that performs "op" on "arg",
304 * where the result is represented using a type of "type_size" bits
305 * (may be zero if unknown or if the type is not an integer).
307 __isl_give pet_expr *pet_expr_new_unary(int type_size, enum pet_op_type op,
308 __isl_take pet_expr *arg)
310 isl_ctx *ctx;
311 pet_expr *expr;
313 if (!arg)
314 return NULL;
315 ctx = pet_expr_get_ctx(arg);
316 expr = pet_expr_alloc(ctx, pet_expr_op);
317 expr = pet_expr_set_n_arg(expr, 1);
318 if (!expr)
319 goto error;
321 expr->op = op;
322 expr->type_size = type_size;
323 expr->args[pet_un_arg] = arg;
325 return expr;
326 error:
327 pet_expr_free(arg);
328 return NULL;
331 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
332 * where the result is represented using a type of "type_size" bits
333 * (may be zero if unknown or if the type is not an integer).
335 __isl_give pet_expr *pet_expr_new_binary(int type_size, enum pet_op_type op,
336 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
338 isl_ctx *ctx;
339 pet_expr *expr;
341 if (!lhs || !rhs)
342 goto error;
343 ctx = pet_expr_get_ctx(lhs);
344 expr = pet_expr_alloc(ctx, pet_expr_op);
345 expr = pet_expr_set_n_arg(expr, 2);
346 if (!expr)
347 goto error;
349 expr->op = op;
350 expr->type_size = type_size;
351 expr->args[pet_bin_lhs] = lhs;
352 expr->args[pet_bin_rhs] = rhs;
354 return expr;
355 error:
356 pet_expr_free(lhs);
357 pet_expr_free(rhs);
358 return NULL;
361 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
363 __isl_give pet_expr *pet_expr_new_ternary(__isl_take pet_expr *cond,
364 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
366 isl_ctx *ctx;
367 pet_expr *expr;
369 if (!cond || !lhs || !rhs)
370 goto error;
371 ctx = pet_expr_get_ctx(cond);
372 expr = pet_expr_alloc(ctx, pet_expr_op);
373 expr = pet_expr_set_n_arg(expr, 3);
374 if (!expr)
375 goto error;
377 expr->op = pet_op_cond;
378 expr->args[pet_ter_cond] = cond;
379 expr->args[pet_ter_true] = lhs;
380 expr->args[pet_ter_false] = rhs;
382 return expr;
383 error:
384 pet_expr_free(cond);
385 pet_expr_free(lhs);
386 pet_expr_free(rhs);
387 return NULL;
390 /* Construct a call pet_expr that calls function "name" with "n_arg"
391 * arguments. The caller is responsible for filling in the arguments.
393 __isl_give pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
394 unsigned n_arg)
396 pet_expr *expr;
398 expr = pet_expr_alloc(ctx, pet_expr_call);
399 expr = pet_expr_set_n_arg(expr, n_arg);
400 if (!expr)
401 return NULL;
403 expr->c.name = strdup(name);
404 if (!expr->c.name)
405 return pet_expr_free(expr);
407 return expr;
410 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
412 __isl_give pet_expr *pet_expr_new_cast(const char *type_name,
413 __isl_take pet_expr *arg)
415 isl_ctx *ctx;
416 pet_expr *expr;
418 if (!arg)
419 return NULL;
421 ctx = pet_expr_get_ctx(arg);
422 expr = pet_expr_alloc(ctx, pet_expr_cast);
423 expr = pet_expr_set_n_arg(expr, 1);
424 if (!expr)
425 goto error;
427 expr->type_name = strdup(type_name);
428 if (!expr->type_name)
429 goto error;
431 expr->args[0] = arg;
433 return expr;
434 error:
435 pet_expr_free(arg);
436 pet_expr_free(expr);
437 return NULL;
440 /* Construct a pet_expr that represents the double "d".
442 __isl_give pet_expr *pet_expr_new_double(isl_ctx *ctx,
443 double val, const char *s)
445 pet_expr *expr;
447 expr = pet_expr_alloc(ctx, pet_expr_double);
448 if (!expr)
449 return NULL;
451 expr->d.val = val;
452 expr->d.s = strdup(s);
453 if (!expr->d.s)
454 return pet_expr_free(expr);
456 return expr;
459 /* Construct a pet_expr that represents the integer value "v".
461 __isl_give pet_expr *pet_expr_new_int(__isl_take isl_val *v)
463 isl_ctx *ctx;
464 pet_expr *expr;
466 if (!v)
467 return NULL;
469 ctx = isl_val_get_ctx(v);
470 expr = pet_expr_alloc(ctx, pet_expr_int);
471 if (!expr)
472 goto error;
474 expr->i = v;
476 return expr;
477 error:
478 isl_val_free(v);
479 return NULL;
482 /* Return an independent duplicate of "expr".
484 * In case of an access expression, make sure the depth of the duplicate is set
485 * before the access relation (if any) is set and after the index expression
486 * is set.
488 static __isl_give pet_expr *pet_expr_dup(__isl_keep pet_expr *expr)
490 int i;
491 pet_expr *dup;
492 enum pet_expr_access_type type;
494 if (!expr)
495 return NULL;
497 dup = pet_expr_alloc(expr->ctx, expr->type);
498 dup = pet_expr_set_type_size(dup, expr->type_size);
499 dup = pet_expr_set_n_arg(dup, expr->n_arg);
500 for (i = 0; i < expr->n_arg; ++i)
501 dup = pet_expr_set_arg(dup, i, pet_expr_copy(expr->args[i]));
503 switch (expr->type) {
504 case pet_expr_access:
505 if (expr->acc.ref_id)
506 dup = pet_expr_access_set_ref_id(dup,
507 isl_id_copy(expr->acc.ref_id));
508 dup = pet_expr_access_set_index(dup,
509 isl_multi_pw_aff_copy(expr->acc.index));
510 dup = pet_expr_access_set_depth(dup, expr->acc.depth);
511 for (type = pet_expr_access_begin;
512 type < pet_expr_access_end; ++type) {
513 if (!expr->acc.access[type])
514 continue;
515 dup = pet_expr_access_set_access(dup, type,
516 isl_union_map_copy(expr->acc.access[type]));
518 dup = pet_expr_access_set_read(dup, expr->acc.read);
519 dup = pet_expr_access_set_write(dup, expr->acc.write);
520 dup = pet_expr_access_set_kill(dup, expr->acc.kill);
521 break;
522 case pet_expr_call:
523 dup = pet_expr_call_set_name(dup, expr->c.name);
524 if (expr->c.summary)
525 dup = pet_expr_call_set_summary(dup,
526 pet_function_summary_copy(expr->c.summary));
527 break;
528 case pet_expr_cast:
529 dup = pet_expr_cast_set_type_name(dup, expr->type_name);
530 break;
531 case pet_expr_double:
532 dup = pet_expr_double_set(dup, expr->d.val, expr->d.s);
533 break;
534 case pet_expr_int:
535 dup = pet_expr_int_set_val(dup, isl_val_copy(expr->i));
536 break;
537 case pet_expr_op:
538 dup = pet_expr_op_set_type(dup, expr->op);
539 break;
540 case pet_expr_error:
541 dup = pet_expr_free(dup);
542 break;
545 return dup;
548 __isl_give pet_expr *pet_expr_cow(__isl_take pet_expr *expr)
550 if (!expr)
551 return NULL;
553 if (expr->ref == 1)
554 return expr;
555 expr->ref--;
556 return pet_expr_dup(expr);
559 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr)
561 enum pet_expr_access_type type;
562 int i;
564 if (!expr)
565 return NULL;
566 if (--expr->ref > 0)
567 return NULL;
569 for (i = 0; i < expr->n_arg; ++i)
570 pet_expr_free(expr->args[i]);
571 free(expr->args);
573 switch (expr->type) {
574 case pet_expr_access:
575 isl_id_free(expr->acc.ref_id);
576 for (type = pet_expr_access_begin;
577 type < pet_expr_access_end; ++type)
578 isl_union_map_free(expr->acc.access[type]);
579 isl_multi_pw_aff_free(expr->acc.index);
580 break;
581 case pet_expr_call:
582 free(expr->c.name);
583 pet_function_summary_free(expr->c.summary);
584 break;
585 case pet_expr_cast:
586 free(expr->type_name);
587 break;
588 case pet_expr_double:
589 free(expr->d.s);
590 break;
591 case pet_expr_int:
592 isl_val_free(expr->i);
593 break;
594 case pet_expr_op:
595 case pet_expr_error:
596 break;
599 isl_ctx_deref(expr->ctx);
600 free(expr);
601 return NULL;
604 /* Return an additional reference to "expr".
606 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr)
608 if (!expr)
609 return NULL;
611 expr->ref++;
612 return expr;
615 /* Return the isl_ctx in which "expr" was created.
617 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr)
619 return expr ? expr->ctx : NULL;
622 /* Return the type of "expr".
624 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr)
626 if (!expr)
627 return pet_expr_error;
628 return expr->type;
631 /* Return the number of arguments of "expr".
633 int pet_expr_get_n_arg(__isl_keep pet_expr *expr)
635 if (!expr)
636 return -1;
638 return expr->n_arg;
641 /* Set the number of arguments of "expr" to "n".
643 * If "expr" originally had more arguments, then remove the extra arguments.
644 * If "expr" originally had fewer arguments, then create space for
645 * the extra arguments ans initialize them to NULL.
647 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n)
649 int i;
650 pet_expr **args;
652 if (!expr)
653 return NULL;
654 if (expr->n_arg == n)
655 return expr;
656 expr = pet_expr_cow(expr);
657 if (!expr)
658 return NULL;
660 if (n < expr->n_arg) {
661 for (i = n; i < expr->n_arg; ++i)
662 pet_expr_free(expr->args[i]);
663 expr->n_arg = n;
664 return expr;
667 args = isl_realloc_array(expr->ctx, expr->args, pet_expr *, n);
668 if (!args)
669 return pet_expr_free(expr);
670 expr->args = args;
671 for (i = expr->n_arg; i < n; ++i)
672 expr->args[i] = NULL;
673 expr->n_arg = n;
675 return expr;
678 /* Return the argument of "expr" at position "pos".
680 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos)
682 if (!expr)
683 return NULL;
684 if (pos < 0 || pos >= expr->n_arg)
685 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
686 "position out of bounds", return NULL);
688 return pet_expr_copy(expr->args[pos]);
691 /* Replace "expr" by its argument at position "pos".
693 __isl_give pet_expr *pet_expr_arg(__isl_take pet_expr *expr, int pos)
695 pet_expr *arg;
697 arg = pet_expr_get_arg(expr, pos);
698 pet_expr_free(expr);
700 return arg;
703 /* Replace the argument of "expr" at position "pos" by "arg".
705 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
706 __isl_take pet_expr *arg)
708 if (!expr || !arg)
709 goto error;
710 if (pos < 0 || pos >= expr->n_arg)
711 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
712 "position out of bounds", goto error);
713 if (expr->args[pos] == arg) {
714 pet_expr_free(arg);
715 return expr;
718 expr = pet_expr_cow(expr);
719 if (!expr)
720 goto error;
722 pet_expr_free(expr->args[pos]);
723 expr->args[pos] = arg;
725 return expr;
726 error:
727 pet_expr_free(expr);
728 pet_expr_free(arg);
729 return NULL;
732 /* Does "expr" perform a comparison operation?
734 int pet_expr_is_comparison(__isl_keep pet_expr *expr)
736 if (!expr)
737 return -1;
738 if (expr->type != pet_expr_op)
739 return 0;
740 switch (expr->op) {
741 case pet_op_eq:
742 case pet_op_ne:
743 case pet_op_le:
744 case pet_op_ge:
745 case pet_op_lt:
746 case pet_op_gt:
747 return 1;
748 default:
749 return 0;
753 /* Does "expr" perform a boolean operation?
755 int pet_expr_is_boolean(__isl_keep pet_expr *expr)
757 if (!expr)
758 return -1;
759 if (expr->type != pet_expr_op)
760 return 0;
761 switch (expr->op) {
762 case pet_op_land:
763 case pet_op_lor:
764 case pet_op_lnot:
765 return 1;
766 default:
767 return 0;
771 /* Is "expr" an address-of operation?
773 int pet_expr_is_address_of(__isl_keep pet_expr *expr)
775 if (!expr)
776 return -1;
777 if (expr->type != pet_expr_op)
778 return 0;
779 return expr->op == pet_op_address_of;
782 /* Is "expr" an assume statement?
784 int pet_expr_is_assume(__isl_keep pet_expr *expr)
786 if (!expr)
787 return -1;
788 if (expr->type != pet_expr_op)
789 return 0;
790 return expr->op == pet_op_assume;
793 /* Does "expr" perform a min operation?
795 int pet_expr_is_min(__isl_keep pet_expr *expr)
797 if (!expr)
798 return -1;
799 if (expr->type != pet_expr_call)
800 return 0;
801 if (expr->n_arg != 2)
802 return 0;
803 if (strcmp(expr->c.name, "min") != 0)
804 return 0;
805 return 1;
808 /* Does "expr" perform a max operation?
810 int pet_expr_is_max(__isl_keep pet_expr *expr)
812 if (!expr)
813 return -1;
814 if (expr->type != pet_expr_call)
815 return 0;
816 if (expr->n_arg != 2)
817 return 0;
818 if (strcmp(expr->c.name, "max") != 0)
819 return 0;
820 return 1;
823 /* Does "expr" represent an access to an unnamed space, i.e.,
824 * does it represent an affine expression?
826 isl_bool pet_expr_is_affine(__isl_keep pet_expr *expr)
828 int has_id;
830 if (!expr)
831 return isl_bool_error;
832 if (expr->type != pet_expr_access)
833 return isl_bool_false;
835 has_id = isl_multi_pw_aff_has_tuple_id(expr->acc.index, isl_dim_out);
836 if (has_id < 0)
837 return isl_bool_error;
839 return !has_id;
842 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
843 * not part of any struct?
845 int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
847 if (!expr)
848 return -1;
849 if (expr->type != pet_expr_access)
850 return 0;
851 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index))
852 return 0;
854 return expr->acc.depth == 0;
857 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
858 * of parameters.
860 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
861 __isl_keep isl_multi_pw_aff *mpa2)
863 int equal;
865 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
866 if (equal < 0 || equal)
867 return equal;
868 mpa2 = isl_multi_pw_aff_copy(mpa2);
869 mpa2 = isl_multi_pw_aff_align_params(mpa2,
870 isl_multi_pw_aff_get_space(mpa1));
871 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
872 isl_multi_pw_aff_free(mpa2);
874 return equal;
877 /* Construct an access relation from the index expression and
878 * the array depth of the access expression "expr".
880 * If the number of indices is smaller than the depth of the array,
881 * then we assume that all elements of the remaining dimensions
882 * are accessed.
884 static __isl_give isl_union_map *construct_access_relation(
885 __isl_keep pet_expr *expr)
887 isl_map *access;
888 int dim;
889 int read, write;
891 if (!expr)
892 return NULL;
894 access = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
895 if (!access)
896 return NULL;
898 dim = isl_map_dim(access, isl_dim_out);
899 if (dim > expr->acc.depth)
900 isl_die(isl_map_get_ctx(access), isl_error_internal,
901 "number of indices greater than depth",
902 access = isl_map_free(access));
904 if (dim != expr->acc.depth)
905 access = extend_range(access, expr->acc.depth - dim);
907 return isl_union_map_from_map(access);
910 /* Ensure that all relevant access relations are explicitly
911 * available in "expr".
913 * If "expr" does not already have the relevant access relations, then create
914 * them based on the index expression and the array depth.
916 * We do not cow since adding an explicit access relation
917 * does not change the meaning of the expression.
919 static __isl_give pet_expr *introduce_access_relations(
920 __isl_take pet_expr *expr)
922 enum pet_expr_access_type type;
923 isl_union_map *access;
924 int dim;
925 int kill, read, write;
927 if (!expr)
928 return NULL;
929 if (has_relevant_access_relations(expr))
930 return expr;
932 access = construct_access_relation(expr);
933 if (!access)
934 return pet_expr_free(expr);
936 kill = expr->acc.kill;
937 read = expr->acc.read;
938 write = expr->acc.write;
939 if (kill && !expr->acc.access[pet_expr_access_fake_killed])
940 expr->acc.access[pet_expr_access_fake_killed] =
941 isl_union_map_copy(access);
942 if (read && !expr->acc.access[pet_expr_access_may_read])
943 expr->acc.access[pet_expr_access_may_read] =
944 isl_union_map_copy(access);
945 if (write && !expr->acc.access[pet_expr_access_may_write])
946 expr->acc.access[pet_expr_access_may_write] =
947 isl_union_map_copy(access);
948 if (write && !expr->acc.access[pet_expr_access_must_write])
949 expr->acc.access[pet_expr_access_must_write] =
950 isl_union_map_copy(access);
952 isl_union_map_free(access);
954 if (!has_relevant_access_relations(expr))
955 return pet_expr_free(expr);
957 return expr;
960 /* Return 1 if the two pet_exprs are equivalent.
962 int pet_expr_is_equal(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2)
964 int i;
965 enum pet_expr_access_type type;
967 if (!expr1 || !expr2)
968 return 0;
970 if (expr1->type != expr2->type)
971 return 0;
972 if (expr1->n_arg != expr2->n_arg)
973 return 0;
974 for (i = 0; i < expr1->n_arg; ++i)
975 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
976 return 0;
977 switch (expr1->type) {
978 case pet_expr_error:
979 return -1;
980 case pet_expr_double:
981 if (strcmp(expr1->d.s, expr2->d.s))
982 return 0;
983 if (expr1->d.val != expr2->d.val)
984 return 0;
985 break;
986 case pet_expr_int:
987 if (!isl_val_eq(expr1->i, expr2->i))
988 return 0;
989 break;
990 case pet_expr_access:
991 if (expr1->acc.read != expr2->acc.read)
992 return 0;
993 if (expr1->acc.write != expr2->acc.write)
994 return 0;
995 if (expr1->acc.kill != expr2->acc.kill)
996 return 0;
997 if (expr1->acc.ref_id != expr2->acc.ref_id)
998 return 0;
999 if (!expr1->acc.index || !expr2->acc.index)
1000 return 0;
1001 if (!multi_pw_aff_is_equal(expr1->acc.index, expr2->acc.index))
1002 return 0;
1003 if (expr1->acc.depth != expr2->acc.depth)
1004 return 0;
1005 if (has_relevant_access_relations(expr1) !=
1006 has_relevant_access_relations(expr2)) {
1007 int equal;
1008 expr1 = pet_expr_copy(expr1);
1009 expr2 = pet_expr_copy(expr2);
1010 expr1 = introduce_access_relations(expr1);
1011 expr2 = introduce_access_relations(expr2);
1012 equal = pet_expr_is_equal(expr1, expr2);
1013 pet_expr_free(expr1);
1014 pet_expr_free(expr2);
1015 return equal;
1017 for (type = pet_expr_access_begin;
1018 type < pet_expr_access_end; ++type) {
1019 if (!expr1->acc.access[type] !=
1020 !expr2->acc.access[type])
1021 return 0;
1022 if (!expr1->acc.access[type])
1023 continue;
1024 if (!isl_union_map_is_equal(expr1->acc.access[type],
1025 expr2->acc.access[type]))
1026 return 0;
1028 break;
1029 case pet_expr_op:
1030 if (expr1->op != expr2->op)
1031 return 0;
1032 break;
1033 case pet_expr_call:
1034 if (strcmp(expr1->c.name, expr2->c.name))
1035 return 0;
1036 break;
1037 case pet_expr_cast:
1038 if (strcmp(expr1->type_name, expr2->type_name))
1039 return 0;
1040 break;
1043 return 1;
1046 /* Does the access expression "expr" read the accessed elements?
1048 isl_bool pet_expr_access_is_read(__isl_keep pet_expr *expr)
1050 if (!expr)
1051 return isl_bool_error;
1052 if (expr->type != pet_expr_access)
1053 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1054 "not an access expression", return isl_bool_error);
1056 return expr->acc.read;
1059 /* Does the access expression "expr" write to the accessed elements?
1061 isl_bool pet_expr_access_is_write(__isl_keep pet_expr *expr)
1063 if (!expr)
1064 return isl_bool_error;
1065 if (expr->type != pet_expr_access)
1066 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1067 "not an access expression", return isl_bool_error);
1069 return expr->acc.write;
1072 /* Does the access expression "expr" kill the accessed elements?
1074 isl_bool pet_expr_access_is_kill(__isl_keep pet_expr *expr)
1076 if (!expr)
1077 return isl_bool_error;
1078 if (expr->type != pet_expr_access)
1079 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1080 "not an access expression", return isl_bool_error);
1082 return expr->acc.kill;
1085 /* Return the identifier of the array accessed by "expr".
1087 * If "expr" represents a member access, then return the identifier
1088 * of the outer structure array.
1090 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
1092 if (!expr)
1093 return NULL;
1094 if (expr->type != pet_expr_access)
1095 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1096 "not an access expression", return NULL);
1098 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index)) {
1099 isl_space *space;
1100 isl_id *id;
1102 space = isl_multi_pw_aff_get_space(expr->acc.index);
1103 space = isl_space_range(space);
1104 while (space && isl_space_is_wrapping(space))
1105 space = isl_space_domain(isl_space_unwrap(space));
1106 id = isl_space_get_tuple_id(space, isl_dim_set);
1107 isl_space_free(space);
1109 return id;
1112 return isl_multi_pw_aff_get_tuple_id(expr->acc.index, isl_dim_out);
1115 /* Return the parameter space of "expr".
1117 __isl_give isl_space *pet_expr_access_get_parameter_space(
1118 __isl_keep pet_expr *expr)
1120 isl_space *space;
1122 if (!expr)
1123 return NULL;
1124 if (expr->type != pet_expr_access)
1125 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1126 "not an access expression", return NULL);
1128 space = isl_multi_pw_aff_get_space(expr->acc.index);
1129 space = isl_space_params(space);
1131 return space;
1134 /* Return the domain space of "expr", including the arguments (if any).
1136 __isl_give isl_space *pet_expr_access_get_augmented_domain_space(
1137 __isl_keep pet_expr *expr)
1139 isl_space *space;
1141 if (!expr)
1142 return NULL;
1143 if (expr->type != pet_expr_access)
1144 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1145 "not an access expression", return NULL);
1147 space = isl_multi_pw_aff_get_space(expr->acc.index);
1148 space = isl_space_domain(space);
1150 return space;
1153 /* Return the domain space of "expr", without the arguments (if any).
1155 __isl_give isl_space *pet_expr_access_get_domain_space(
1156 __isl_keep pet_expr *expr)
1158 isl_space *space;
1160 space = pet_expr_access_get_augmented_domain_space(expr);
1161 if (isl_space_is_wrapping(space))
1162 space = isl_space_domain(isl_space_unwrap(space));
1164 return space;
1167 /* Return the space of the data accessed by "expr".
1169 __isl_give isl_space *pet_expr_access_get_data_space(__isl_keep pet_expr *expr)
1171 isl_space *space;
1173 if (!expr)
1174 return NULL;
1175 if (expr->type != pet_expr_access)
1176 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1177 "not an access expression", return NULL);
1179 space = isl_multi_pw_aff_get_space(expr->acc.index);
1180 space = isl_space_range(space);
1182 return space;
1185 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1187 static __isl_give pet_expr *pet_expr_map_expr_of_type(__isl_take pet_expr *expr,
1188 enum pet_expr_type type,
1189 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1190 void *user)
1192 int i, n;
1194 n = pet_expr_get_n_arg(expr);
1195 for (i = 0; i < n; ++i) {
1196 pet_expr *arg = pet_expr_get_arg(expr, i);
1197 arg = pet_expr_map_expr_of_type(arg, type, fn, user);
1198 expr = pet_expr_set_arg(expr, i, arg);
1201 if (!expr)
1202 return NULL;
1204 if (expr->type == type)
1205 expr = fn(expr, user);
1207 return expr;
1210 /* Modify all expressions of type pet_expr_access in "expr"
1211 * by calling "fn" on them.
1213 __isl_give pet_expr *pet_expr_map_access(__isl_take pet_expr *expr,
1214 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1215 void *user)
1217 return pet_expr_map_expr_of_type(expr, pet_expr_access, fn, user);
1220 /* Modify all expressions of type pet_expr_call in "expr"
1221 * by calling "fn" on them.
1223 __isl_give pet_expr *pet_expr_map_call(__isl_take pet_expr *expr,
1224 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1225 void *user)
1227 return pet_expr_map_expr_of_type(expr, pet_expr_call, fn, user);
1230 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1232 * Return -1 on error (where fn returning a negative value is treated as
1233 * an error).
1234 * Otherwise return 0.
1236 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
1237 enum pet_expr_type type,
1238 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1240 int i;
1242 if (!expr)
1243 return -1;
1245 for (i = 0; i < expr->n_arg; ++i)
1246 if (pet_expr_foreach_expr_of_type(expr->args[i],
1247 type, fn, user) < 0)
1248 return -1;
1250 if (expr->type == type)
1251 return fn(expr, user);
1253 return 0;
1256 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1258 * Return -1 on error (where fn returning a negative value is treated as
1259 * an error).
1260 * Otherwise return 0.
1262 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
1263 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1265 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
1268 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1270 * Return -1 on error (where fn returning a negative value is treated as
1271 * an error).
1272 * Otherwise return 0.
1274 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
1275 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1277 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1280 /* Internal data structure for pet_expr_writes.
1281 * "id" is the identifier that we are looking for.
1282 * "found" is set if we have found the identifier being written to.
1284 struct pet_expr_writes_data {
1285 isl_id *id;
1286 int found;
1289 /* Given an access expression, check if it writes to data->id.
1290 * If so, set data->found and abort the search.
1292 static int writes(__isl_keep pet_expr *expr, void *user)
1294 struct pet_expr_writes_data *data = user;
1295 isl_id *write_id;
1297 if (!expr->acc.write)
1298 return 0;
1299 if (pet_expr_is_affine(expr))
1300 return 0;
1302 write_id = pet_expr_access_get_id(expr);
1303 isl_id_free(write_id);
1305 if (!write_id)
1306 return -1;
1308 if (write_id != data->id)
1309 return 0;
1311 data->found = 1;
1312 return -1;
1315 /* Does expression "expr" write to "id"?
1317 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1319 struct pet_expr_writes_data data;
1321 data.id = id;
1322 data.found = 0;
1323 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1324 !data.found)
1325 return -1;
1327 return data.found;
1330 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1331 * index expression and access relations of "expr" (if any)
1332 * to dimensions of "dst_type" at "dst_pos".
1334 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1335 enum isl_dim_type dst_type, unsigned dst_pos,
1336 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1338 enum pet_expr_access_type type;
1340 expr = pet_expr_cow(expr);
1341 if (!expr)
1342 return NULL;
1343 if (expr->type != pet_expr_access)
1344 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1345 "not an access pet_expr", return pet_expr_free(expr));
1347 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1348 if (!expr->acc.access[type])
1349 continue;
1350 expr->acc.access[type] =
1351 pet_union_map_move_dims(expr->acc.access[type],
1352 dst_type, dst_pos, src_type, src_pos, n);
1353 if (!expr->acc.access[type])
1354 break;
1356 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1357 dst_type, dst_pos, src_type, src_pos, n);
1358 if (!expr->acc.index || type < pet_expr_access_end)
1359 return pet_expr_free(expr);
1361 return expr;
1364 /* Replace the index expression and access relations (if any) of "expr"
1365 * by their preimages under the function represented by "ma".
1367 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1368 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1370 enum pet_expr_access_type type;
1372 expr = pet_expr_cow(expr);
1373 if (!expr || !ma)
1374 goto error;
1375 if (expr->type != pet_expr_access)
1376 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1377 "not an access pet_expr", goto error);
1379 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1380 if (!expr->acc.access[type])
1381 continue;
1382 expr->acc.access[type] =
1383 isl_union_map_preimage_domain_multi_aff(
1384 expr->acc.access[type], isl_multi_aff_copy(ma));
1385 if (!expr->acc.access[type])
1386 break;
1388 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1389 ma);
1390 if (!expr->acc.index || type < pet_expr_access_end)
1391 return pet_expr_free(expr);
1393 return expr;
1394 error:
1395 isl_multi_aff_free(ma);
1396 pet_expr_free(expr);
1397 return NULL;
1400 /* Replace the index expression and access relations (if any) of "expr"
1401 * by their preimages under the function represented by "mpa".
1403 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1404 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1406 enum pet_expr_access_type type;
1408 expr = pet_expr_cow(expr);
1409 if (!expr || !mpa)
1410 goto error;
1411 if (expr->type != pet_expr_access)
1412 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1413 "not an access pet_expr", goto error);
1415 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1416 if (!expr->acc.access[type])
1417 continue;
1418 expr->acc.access[type] =
1419 isl_union_map_preimage_domain_multi_pw_aff(
1420 expr->acc.access[type], isl_multi_pw_aff_copy(mpa));
1421 if (!expr->acc.access[type])
1422 break;
1424 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1425 expr->acc.index, mpa);
1426 if (!expr->acc.index || type < pet_expr_access_end)
1427 return pet_expr_free(expr);
1429 return expr;
1430 error:
1431 isl_multi_pw_aff_free(mpa);
1432 pet_expr_free(expr);
1433 return NULL;
1436 /* Return the index expression of access expression "expr".
1438 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1439 __isl_keep pet_expr *expr)
1441 if (!expr)
1442 return NULL;
1443 if (expr->type != pet_expr_access)
1444 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1445 "not an access expression", return NULL);
1447 return isl_multi_pw_aff_copy(expr->acc.index);
1450 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1452 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1454 isl_space *space;
1455 enum pet_expr_access_type type;
1457 expr = pet_expr_cow(expr);
1458 if (!expr)
1459 return NULL;
1460 if (expr->type != pet_expr_access)
1461 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1462 "not an access expression", return pet_expr_free(expr));
1464 if (!has_any_access_relation(expr))
1465 return expr;
1467 space = isl_multi_pw_aff_get_space(expr->acc.index);
1468 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1469 if (!expr->acc.access[type])
1470 continue;
1471 space = isl_space_align_params(space,
1472 isl_union_map_get_space(expr->acc.access[type]));
1474 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1475 isl_space_copy(space));
1476 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1477 if (!expr->acc.access[type])
1478 continue;
1479 expr->acc.access[type] =
1480 isl_union_map_align_params(expr->acc.access[type],
1481 isl_space_copy(space));
1482 if (!expr->acc.access[type])
1483 break;
1485 isl_space_free(space);
1486 if (!expr->acc.index || type < pet_expr_access_end)
1487 return pet_expr_free(expr);
1489 return expr;
1492 /* Are "expr1" and "expr2" both array accesses such that
1493 * the access relation of "expr1" is a subset of that of "expr2"?
1494 * Only take into account the first "n_arg" arguments.
1496 * This function is tailored for use by mark_self_dependences in nest.c.
1497 * In particular, the input expressions may have more than "n_arg"
1498 * elements in their arguments arrays, while only the first "n_arg"
1499 * elements are referenced from the access relations.
1501 int pet_expr_is_sub_access(__isl_keep pet_expr *expr1,
1502 __isl_keep pet_expr *expr2, int n_arg)
1504 isl_id *id1, *id2;
1505 int i, n1, n2;
1506 int is_subset;
1508 if (!expr1 || !expr2)
1509 return 0;
1510 if (pet_expr_get_type(expr1) != pet_expr_access)
1511 return 0;
1512 if (pet_expr_get_type(expr2) != pet_expr_access)
1513 return 0;
1514 if (pet_expr_is_affine(expr1))
1515 return 0;
1516 if (pet_expr_is_affine(expr2))
1517 return 0;
1518 n1 = pet_expr_get_n_arg(expr1);
1519 if (n1 > n_arg)
1520 n1 = n_arg;
1521 n2 = pet_expr_get_n_arg(expr2);
1522 if (n2 > n_arg)
1523 n2 = n_arg;
1524 if (n1 != n2)
1525 return 0;
1526 for (i = 0; i < n1; ++i) {
1527 int equal;
1528 equal = pet_expr_is_equal(expr1->args[i], expr2->args[i]);
1529 if (equal < 0 || !equal)
1530 return equal;
1532 id1 = pet_expr_access_get_id(expr1);
1533 id2 = pet_expr_access_get_id(expr2);
1534 isl_id_free(id1);
1535 isl_id_free(id2);
1536 if (!id1 || !id2)
1537 return 0;
1538 if (id1 != id2)
1539 return 0;
1541 expr1 = pet_expr_copy(expr1);
1542 expr2 = pet_expr_copy(expr2);
1543 expr1 = introduce_access_relations(expr1);
1544 expr2 = introduce_access_relations(expr2);
1545 if (!expr1 || !expr2)
1546 goto error;
1548 is_subset = isl_union_map_is_subset(
1549 expr1->acc.access[pet_expr_access_may_read],
1550 expr2->acc.access[pet_expr_access_may_read]);
1552 pet_expr_free(expr1);
1553 pet_expr_free(expr2);
1555 return is_subset;
1556 error:
1557 pet_expr_free(expr1);
1558 pet_expr_free(expr2);
1559 return -1;
1562 /* Given a set in the iteration space "domain", extend it to live in the space
1563 * of the domain of access relations.
1565 * That, is the number of arguments "n" is 0, then simply return domain.
1566 * Otherwise, return [domain -> [a_1,...,a_n]].
1568 static __isl_give isl_set *add_arguments(__isl_take isl_set *domain, int n)
1570 isl_map *map;
1572 if (n == 0)
1573 return domain;
1575 map = isl_map_from_domain(domain);
1576 map = isl_map_add_dims(map, isl_dim_out, n);
1577 return isl_map_wrap(map);
1580 /* Add extra conditions to the domains of all access relations in "expr",
1581 * introducing access relations if they are not already present.
1583 * The conditions are not added to the index expression. Instead, they
1584 * are used to try and simplify the index expression.
1586 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1587 __isl_take isl_set *cond)
1589 int i;
1590 isl_union_set *uset;
1591 enum pet_expr_access_type type;
1593 expr = pet_expr_cow(expr);
1594 if (!expr)
1595 goto error;
1597 for (i = 0; i < expr->n_arg; ++i) {
1598 expr->args[i] = pet_expr_restrict(expr->args[i],
1599 isl_set_copy(cond));
1600 if (!expr->args[i])
1601 goto error;
1604 if (expr->type != pet_expr_access) {
1605 isl_set_free(cond);
1606 return expr;
1609 expr = introduce_access_relations(expr);
1610 if (!expr)
1611 goto error;
1613 cond = add_arguments(cond, expr->n_arg);
1614 uset = isl_union_set_from_set(isl_set_copy(cond));
1615 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1616 if (!expr->acc.access[type])
1617 continue;
1618 expr->acc.access[type] =
1619 isl_union_map_intersect_domain(expr->acc.access[type],
1620 isl_union_set_copy(uset));
1621 if (!expr->acc.access[type])
1622 break;
1624 isl_union_set_free(uset);
1625 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, cond);
1626 if (type < pet_expr_access_end || !expr->acc.index)
1627 return pet_expr_free(expr);
1629 return expr;
1630 error:
1631 isl_set_free(cond);
1632 return pet_expr_free(expr);
1635 /* Modify the access relations (if any) and index expression
1636 * of the given access expression
1637 * based on the given iteration space transformation.
1638 * In particular, precompose the access relation and index expression
1639 * with the update function.
1641 * If the access has any arguments then the domain of the access relation
1642 * is a wrapped mapping from the iteration space to the space of
1643 * argument values. We only need to change the domain of this wrapped
1644 * mapping, so we extend the input transformation with an identity mapping
1645 * on the space of argument values.
1647 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1648 __isl_keep isl_multi_pw_aff *update)
1650 enum pet_expr_access_type type;
1652 expr = pet_expr_cow(expr);
1653 if (!expr)
1654 return NULL;
1655 if (expr->type != pet_expr_access)
1656 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1657 "not an access expression", return pet_expr_free(expr));
1659 update = isl_multi_pw_aff_copy(update);
1661 if (expr->n_arg > 0) {
1662 isl_space *space;
1663 isl_multi_pw_aff *id;
1665 space = isl_multi_pw_aff_get_space(expr->acc.index);
1666 space = isl_space_domain(space);
1667 space = isl_space_unwrap(space);
1668 space = isl_space_range(space);
1669 space = isl_space_map_from_set(space);
1670 id = isl_multi_pw_aff_identity(space);
1671 update = isl_multi_pw_aff_product(update, id);
1674 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1675 if (!expr->acc.access[type])
1676 continue;
1677 expr->acc.access[type] =
1678 isl_union_map_preimage_domain_multi_pw_aff(
1679 expr->acc.access[type],
1680 isl_multi_pw_aff_copy(update));
1681 if (!expr->acc.access[type])
1682 break;
1684 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1685 expr->acc.index, update);
1686 if (type < pet_expr_access_end || !expr->acc.index)
1687 return pet_expr_free(expr);
1689 return expr;
1692 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1694 isl_multi_pw_aff *update = user;
1696 return pet_expr_access_update_domain(expr, update);
1699 /* Modify all access relations in "expr" by precomposing them with
1700 * the given iteration space transformation.
1702 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1703 __isl_take isl_multi_pw_aff *update)
1705 expr = pet_expr_map_access(expr, &update_domain, update);
1706 isl_multi_pw_aff_free(update);
1707 return expr;
1710 /* Given an expression with accesses that have a 0D anonymous domain,
1711 * replace those domains by "space".
1713 __isl_give pet_expr *pet_expr_insert_domain(__isl_take pet_expr *expr,
1714 __isl_take isl_space *space)
1716 isl_multi_pw_aff *mpa;
1718 space = isl_space_from_domain(space);
1719 mpa = isl_multi_pw_aff_zero(space);
1720 return pet_expr_update_domain(expr, mpa);
1723 /* Add all parameters in "space" to the access relations (if any)
1724 * and index expression of "expr".
1726 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1728 isl_space *space = user;
1729 enum pet_expr_access_type type;
1731 expr = pet_expr_cow(expr);
1732 if (!expr)
1733 return NULL;
1734 if (expr->type != pet_expr_access)
1735 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1736 "not an access expression", return pet_expr_free(expr));
1738 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1739 if (!expr->acc.access[type])
1740 continue;
1741 expr->acc.access[type] =
1742 isl_union_map_align_params(expr->acc.access[type],
1743 isl_space_copy(space));
1744 if (!expr->acc.access[type])
1745 break;
1747 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1748 isl_space_copy(space));
1749 if (type < pet_expr_access_end || !expr->acc.index)
1750 return pet_expr_free(expr);
1752 return expr;
1755 /* Add all parameters in "space" to all access relations and index expressions
1756 * in "expr".
1758 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1759 __isl_take isl_space *space)
1761 expr = pet_expr_map_access(expr, &align_params, space);
1762 isl_space_free(space);
1763 return expr;
1766 /* Insert an argument expression corresponding to "test" in front
1767 * of the list of arguments described by *n_arg and *args.
1769 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1770 __isl_keep isl_multi_pw_aff *test)
1772 int i;
1773 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1775 if (!test)
1776 return pet_expr_free(expr);
1777 expr = pet_expr_cow(expr);
1778 if (!expr)
1779 return NULL;
1781 if (!expr->args) {
1782 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1783 if (!expr->args)
1784 return pet_expr_free(expr);
1785 } else {
1786 pet_expr **ext;
1787 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1788 if (!ext)
1789 return pet_expr_free(expr);
1790 for (i = 0; i < expr->n_arg; ++i)
1791 ext[1 + i] = expr->args[i];
1792 free(expr->args);
1793 expr->args = ext;
1795 expr->n_arg++;
1796 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1797 if (!expr->args[0])
1798 return pet_expr_free(expr);
1800 return expr;
1803 /* Make the expression "expr" depend on the value of "test"
1804 * being equal to "satisfied".
1806 * If "test" is an affine expression, we simply add the conditions
1807 * on the expression having the value "satisfied" to all access relations
1808 * (introducing access relations if they are missing) and index expressions.
1810 * Otherwise, we add a filter to "expr" (which is then assumed to be
1811 * an access expression) corresponding to "test" being equal to "satisfied".
1813 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
1814 __isl_take isl_multi_pw_aff *test, int satisfied)
1816 isl_id *id;
1817 isl_ctx *ctx;
1818 isl_space *space;
1819 isl_pw_multi_aff *pma;
1820 enum pet_expr_access_type type;
1822 expr = pet_expr_cow(expr);
1823 if (!expr || !test)
1824 goto error;
1826 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
1827 isl_pw_aff *pa;
1828 isl_set *cond;
1830 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
1831 isl_multi_pw_aff_free(test);
1832 if (satisfied)
1833 cond = isl_pw_aff_non_zero_set(pa);
1834 else
1835 cond = isl_pw_aff_zero_set(pa);
1836 return pet_expr_restrict(expr, cond);
1839 ctx = isl_multi_pw_aff_get_ctx(test);
1840 if (expr->type != pet_expr_access)
1841 isl_die(ctx, isl_error_invalid,
1842 "can only filter access expressions", goto error);
1844 expr = introduce_access_relations(expr);
1845 if (!expr)
1846 goto error;
1848 space = isl_space_domain(isl_multi_pw_aff_get_space(expr->acc.index));
1849 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1850 pma = pet_filter_insert_pma(space, id, satisfied);
1852 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1853 if (!expr->acc.access[type])
1854 continue;
1855 expr->acc.access[type] =
1856 isl_union_map_preimage_domain_pw_multi_aff(
1857 expr->acc.access[type],
1858 isl_pw_multi_aff_copy(pma));
1859 if (!expr->acc.access[type])
1860 break;
1862 pma = isl_pw_multi_aff_gist(pma,
1863 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma)));
1864 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
1865 expr->acc.index, pma);
1866 if (type < pet_expr_access_end || !expr->acc.index)
1867 goto error;
1869 expr = insert_access_arg(expr, test);
1871 isl_multi_pw_aff_free(test);
1872 return expr;
1873 error:
1874 isl_multi_pw_aff_free(test);
1875 return pet_expr_free(expr);
1878 /* Add a reference identifier to access expression "expr".
1879 * "user" points to an integer that contains the sequence number
1880 * of the next reference.
1882 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
1883 void *user)
1885 isl_ctx *ctx;
1886 char name[50];
1887 int *n_ref = user;
1889 expr = pet_expr_cow(expr);
1890 if (!expr)
1891 return expr;
1892 if (expr->type != pet_expr_access)
1893 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1894 "not an access expression", return pet_expr_free(expr));
1896 ctx = pet_expr_get_ctx(expr);
1897 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
1898 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
1899 if (!expr->acc.ref_id)
1900 return pet_expr_free(expr);
1902 return expr;
1905 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
1907 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
1910 /* Reset the user pointer on all parameter and tuple ids in
1911 * the access relations (if any) and the index expression
1912 * of the access expression "expr".
1914 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
1915 void *user)
1917 enum pet_expr_access_type type;
1919 expr = pet_expr_cow(expr);
1920 if (!expr)
1921 return expr;
1922 if (expr->type != pet_expr_access)
1923 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1924 "not an access expression", return pet_expr_free(expr));
1926 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1927 if (!expr->acc.access[type])
1928 continue;
1929 expr->acc.access[type] =
1930 isl_union_map_reset_user(expr->acc.access[type]);
1931 if (!expr->acc.access[type])
1932 break;
1934 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
1935 if (type < pet_expr_access_end || !expr->acc.index)
1936 return pet_expr_free(expr);
1938 return expr;
1941 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
1943 return pet_expr_map_access(expr, &access_anonymize, NULL);
1946 /* Data used in access_gist() callback.
1948 struct pet_access_gist_data {
1949 isl_set *domain;
1950 isl_union_map *value_bounds;
1953 /* Given an expression "expr" of type pet_expr_access, compute
1954 * the gist of the associated access relations (if any) and index expression
1955 * with respect to data->domain and the bounds on the values of the arguments
1956 * of the expression.
1958 * The arguments of "expr" have been gisted right before "expr" itself
1959 * is gisted. The gisted arguments may have become equal where before
1960 * they may not have been (obviously) equal. We therefore take
1961 * the opportunity to remove duplicate arguments here.
1963 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
1965 struct pet_access_gist_data *data = user;
1966 isl_set *domain;
1967 isl_union_set *uset;
1968 enum pet_expr_access_type type;
1970 expr = pet_expr_remove_duplicate_args(expr);
1971 expr = pet_expr_cow(expr);
1972 if (!expr)
1973 return expr;
1974 if (expr->type != pet_expr_access)
1975 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1976 "not an access expression", return pet_expr_free(expr));
1978 domain = isl_set_copy(data->domain);
1979 if (expr->n_arg > 0)
1980 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
1981 data->value_bounds);
1983 uset = isl_union_set_from_set(isl_set_copy(domain));
1984 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1985 if (!expr->acc.access[type])
1986 continue;
1987 expr->acc.access[type] =
1988 isl_union_map_gist_domain(expr->acc.access[type],
1989 isl_union_set_copy(uset));
1990 if (!expr->acc.access[type])
1991 break;
1993 isl_union_set_free(uset);
1994 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
1995 if (type < pet_expr_access_end || !expr->acc.index)
1996 return pet_expr_free(expr);
1998 return expr;
2001 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
2002 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2004 struct pet_access_gist_data data = { context, value_bounds };
2006 return pet_expr_map_access(expr, &access_gist, &data);
2009 /* Mark "expr" as a read dependening on "read".
2011 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
2012 int read)
2014 if (!expr)
2015 return pet_expr_free(expr);
2016 if (expr->type != pet_expr_access)
2017 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2018 "not an access expression", return pet_expr_free(expr));
2019 if (expr->acc.read == read)
2020 return expr;
2021 expr = pet_expr_cow(expr);
2022 if (!expr)
2023 return NULL;
2024 expr->acc.read = read;
2026 return expr;
2029 /* Mark "expr" as a write dependening on "write".
2031 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
2032 int write)
2034 if (!expr)
2035 return pet_expr_free(expr);
2036 if (expr->type != pet_expr_access)
2037 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2038 "not an access expression", return pet_expr_free(expr));
2039 if (expr->acc.write == write)
2040 return expr;
2041 expr = pet_expr_cow(expr);
2042 if (!expr)
2043 return NULL;
2044 expr->acc.write = write;
2046 return expr;
2049 /* Mark "expr" as a kill dependening on "kill".
2051 __isl_give pet_expr *pet_expr_access_set_kill(__isl_take pet_expr *expr,
2052 int kill)
2054 if (!expr)
2055 return pet_expr_free(expr);
2056 if (expr->type != pet_expr_access)
2057 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2058 "not an access expression", return pet_expr_free(expr));
2059 if (expr->acc.kill == kill)
2060 return expr;
2061 expr = pet_expr_cow(expr);
2062 if (!expr)
2063 return NULL;
2064 expr->acc.kill = kill;
2066 return expr;
2069 /* Map the access type "type" to the corresponding location
2070 * in the access array.
2071 * In particular, the access relation of type pet_expr_access_killed is
2072 * stored in the element at position pet_expr_access_fake_killed.
2074 static enum pet_expr_access_type internalize_type(
2075 enum pet_expr_access_type type)
2077 if (type == pet_expr_access_killed)
2078 return pet_expr_access_fake_killed;
2079 return type;
2082 /* Replace the access relation of the given "type" of "expr" by "access".
2083 * If the access relation is non-empty and the type is a read or a write,
2084 * then also mark the access expression itself as a read or a write.
2086 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
2087 enum pet_expr_access_type type, __isl_take isl_union_map *access)
2089 int empty;
2091 expr = pet_expr_cow(expr);
2092 if (!expr || !access)
2093 goto error;
2094 if (expr->type != pet_expr_access)
2095 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2096 "not an access expression", goto error);
2097 type = internalize_type(type);
2098 isl_union_map_free(expr->acc.access[type]);
2099 expr->acc.access[type] = access;
2101 if (expr->acc.kill)
2102 return expr;
2104 empty = isl_union_map_is_empty(access);
2105 if (empty < 0)
2106 return pet_expr_free(expr);
2107 if (empty)
2108 return expr;
2110 if (type == pet_expr_access_may_read)
2111 expr = pet_expr_access_set_read(expr, 1);
2112 else
2113 expr = pet_expr_access_set_write(expr, 1);
2115 return expr;
2116 error:
2117 isl_union_map_free(access);
2118 pet_expr_free(expr);
2119 return NULL;
2122 /* Replace the index expression of "expr" by "index" and
2123 * set the array depth accordingly.
2125 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
2126 __isl_take isl_multi_pw_aff *index)
2128 expr = pet_expr_cow(expr);
2129 if (!expr || !index)
2130 goto error;
2131 if (expr->type != pet_expr_access)
2132 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2133 "not an access expression", goto error);
2134 isl_multi_pw_aff_free(expr->acc.index);
2135 expr->acc.index = index;
2136 expr->acc.depth = isl_multi_pw_aff_dim(index, isl_dim_out);
2138 return expr;
2139 error:
2140 isl_multi_pw_aff_free(index);
2141 pet_expr_free(expr);
2142 return NULL;
2145 /* Return the reference identifier of access expression "expr".
2147 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
2149 if (!expr)
2150 return NULL;
2151 if (expr->type != pet_expr_access)
2152 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2153 "not an access expression", return NULL);
2155 return isl_id_copy(expr->acc.ref_id);
2158 /* Replace the reference identifier of access expression "expr" by "ref_id".
2160 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
2161 __isl_take isl_id *ref_id)
2163 expr = pet_expr_cow(expr);
2164 if (!expr || !ref_id)
2165 goto error;
2166 if (expr->type != pet_expr_access)
2167 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2168 "not an access expression", goto error);
2169 isl_id_free(expr->acc.ref_id);
2170 expr->acc.ref_id = ref_id;
2172 return expr;
2173 error:
2174 isl_id_free(ref_id);
2175 pet_expr_free(expr);
2176 return NULL;
2179 /* Tag the access relation "access" with "id".
2180 * That is, insert the id as the range of a wrapped relation
2181 * in the domain of "access".
2183 * If "access" is of the form
2185 * D[i] -> A[a]
2187 * then the result is of the form
2189 * [D[i] -> id[]] -> A[a]
2191 __isl_give isl_union_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
2192 __isl_take isl_union_map *access)
2194 isl_space *space;
2195 isl_multi_aff *add_tag;
2196 isl_id *id;
2198 if (expr->type != pet_expr_access)
2199 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2200 "not an access expression",
2201 return isl_union_map_free(access));
2203 id = isl_id_copy(expr->acc.ref_id);
2204 space = pet_expr_access_get_domain_space(expr);
2205 space = isl_space_from_domain(space);
2206 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2207 add_tag = isl_multi_aff_domain_map(space);
2208 access = isl_union_map_preimage_domain_multi_aff(access, add_tag);
2210 return access;
2213 /* Return the access relation of the given "type" associated to "expr"
2214 * that maps pairs of domain iterations and argument values
2215 * to the corresponding accessed data elements.
2217 * If the requested access relation is explicitly available,
2218 * then return a copy. Otherwise, check if it is irrelevant for
2219 * the access expression and return an empty relation if this is the case.
2220 * Otherwise, introduce the requested access relation in "expr" and
2221 * return a copy.
2223 __isl_give isl_union_map *pet_expr_access_get_dependent_access(
2224 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2226 isl_union_map *access;
2227 int empty;
2229 if (!expr)
2230 return NULL;
2231 if (expr->type != pet_expr_access)
2232 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2233 "not an access expression", return NULL);
2235 type = internalize_type(type);
2236 if (expr->acc.access[type])
2237 return isl_union_map_copy(expr->acc.access[type]);
2239 if (type == pet_expr_access_may_read)
2240 empty = !expr->acc.read;
2241 else
2242 empty = !expr->acc.write;
2244 if (!empty) {
2245 expr = pet_expr_copy(expr);
2246 expr = introduce_access_relations(expr);
2247 if (!expr)
2248 return NULL;
2249 access = isl_union_map_copy(expr->acc.access[type]);
2250 pet_expr_free(expr);
2252 return access;
2255 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr));
2258 /* Return the may read access relation associated to "expr"
2259 * that maps pairs of domain iterations and argument values
2260 * to the corresponding accessed data elements.
2262 __isl_give isl_union_map *pet_expr_access_get_dependent_may_read(
2263 __isl_keep pet_expr *expr)
2265 return pet_expr_access_get_dependent_access(expr,
2266 pet_expr_access_may_read);
2269 /* Return the may write access relation associated to "expr"
2270 * that maps pairs of domain iterations and argument values
2271 * to the corresponding accessed data elements.
2273 __isl_give isl_union_map *pet_expr_access_get_dependent_may_write(
2274 __isl_keep pet_expr *expr)
2276 return pet_expr_access_get_dependent_access(expr,
2277 pet_expr_access_may_write);
2280 /* Return the must write access relation associated to "expr"
2281 * that maps pairs of domain iterations and argument values
2282 * to the corresponding accessed data elements.
2284 __isl_give isl_union_map *pet_expr_access_get_dependent_must_write(
2285 __isl_keep pet_expr *expr)
2287 return pet_expr_access_get_dependent_access(expr,
2288 pet_expr_access_must_write);
2291 /* Return the relation of the given "type" mapping domain iterations
2292 * to the accessed data elements.
2293 * In particular, take the access relation and, in case of may_read
2294 * or may_write, project out the values of the arguments, if any.
2295 * In case of must_write, return the empty relation if there are
2296 * any arguments.
2298 __isl_give isl_union_map *pet_expr_access_get_access(__isl_keep pet_expr *expr,
2299 enum pet_expr_access_type type)
2301 isl_union_map *access;
2302 isl_space *space;
2303 isl_map *map;
2305 if (!expr)
2306 return NULL;
2307 if (expr->type != pet_expr_access)
2308 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2309 "not an access expression", return NULL);
2311 if (expr->n_arg != 0 && type == pet_expr_access_must_write) {
2312 space = pet_expr_access_get_parameter_space(expr);
2313 return isl_union_map_empty(space);
2316 access = pet_expr_access_get_dependent_access(expr, type);
2317 if (expr->n_arg == 0)
2318 return access;
2320 space = isl_multi_pw_aff_get_space(expr->acc.index);
2321 space = isl_space_domain(space);
2322 map = isl_map_universe(isl_space_unwrap(space));
2323 map = isl_map_domain_map(map);
2324 access = isl_union_map_apply_domain(access,
2325 isl_union_map_from_map(map));
2327 return access;
2330 /* Return the relation mapping domain iterations to all possibly
2331 * read data elements.
2333 __isl_give isl_union_map *pet_expr_access_get_may_read(
2334 __isl_keep pet_expr *expr)
2336 return pet_expr_access_get_access(expr, pet_expr_access_may_read);
2339 /* Return the relation mapping domain iterations to all possibly
2340 * written data elements.
2342 __isl_give isl_union_map *pet_expr_access_get_may_write(
2343 __isl_keep pet_expr *expr)
2345 return pet_expr_access_get_access(expr, pet_expr_access_may_write);
2348 /* Return a relation mapping domain iterations to definitely
2349 * written data elements, assuming the statement containing
2350 * the expression is executed.
2352 __isl_give isl_union_map *pet_expr_access_get_must_write(
2353 __isl_keep pet_expr *expr)
2355 return pet_expr_access_get_access(expr, pet_expr_access_must_write);
2358 /* Return the relation of the given "type" mapping domain iterations to
2359 * accessed data elements, with its domain tagged with the reference
2360 * identifier.
2362 static __isl_give isl_union_map *pet_expr_access_get_tagged_access(
2363 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2365 isl_union_map *access;
2367 if (!expr)
2368 return NULL;
2370 access = pet_expr_access_get_access(expr, type);
2371 access = pet_expr_tag_access(expr, access);
2373 return access;
2376 /* Return the relation mapping domain iterations to all possibly
2377 * read data elements, with its domain tagged with the reference
2378 * identifier.
2380 __isl_give isl_union_map *pet_expr_access_get_tagged_may_read(
2381 __isl_keep pet_expr *expr)
2383 return pet_expr_access_get_tagged_access(expr,
2384 pet_expr_access_may_read);
2387 /* Return the relation mapping domain iterations to all possibly
2388 * written data elements, with its domain tagged with the reference
2389 * identifier.
2391 __isl_give isl_union_map *pet_expr_access_get_tagged_may_write(
2392 __isl_keep pet_expr *expr)
2394 return pet_expr_access_get_tagged_access(expr,
2395 pet_expr_access_may_write);
2398 /* Return the operation type of operation expression "expr".
2400 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
2402 if (!expr)
2403 return pet_op_last;
2404 if (expr->type != pet_expr_op)
2405 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2406 "not an operation expression", return pet_op_last);
2408 return expr->op;
2411 /* Replace the operation type of operation expression "expr" by "type".
2413 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
2414 enum pet_op_type type)
2416 if (!expr)
2417 return pet_expr_free(expr);
2418 if (expr->type != pet_expr_op)
2419 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2420 "not an operation expression",
2421 return pet_expr_free(expr));
2422 if (expr->op == type)
2423 return expr;
2424 expr = pet_expr_cow(expr);
2425 if (!expr)
2426 return NULL;
2427 expr->op = type;
2429 return expr;
2432 /* Return the name of the function called by "expr".
2434 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
2436 if (!expr)
2437 return NULL;
2438 if (expr->type != pet_expr_call)
2439 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2440 "not a call expression", return NULL);
2441 return expr->c.name;
2444 /* Replace the name of the function called by "expr" by "name".
2446 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
2447 __isl_keep const char *name)
2449 expr = pet_expr_cow(expr);
2450 if (!expr || !name)
2451 return pet_expr_free(expr);
2452 if (expr->type != pet_expr_call)
2453 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2454 "not a call expression", return pet_expr_free(expr));
2455 free(expr->c.name);
2456 expr->c.name = strdup(name);
2457 if (!expr->c.name)
2458 return pet_expr_free(expr);
2459 return expr;
2462 /* Does the call expression "expr" have an associated function summary?
2464 int pet_expr_call_has_summary(__isl_keep pet_expr *expr)
2466 if (!expr)
2467 return -1;
2468 if (expr->type != pet_expr_call)
2469 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2470 "not a call expression", return -1);
2472 return expr->c.summary != NULL;
2475 /* Return a copy of the function summary associated to
2476 * the call expression "expr".
2478 __isl_give pet_function_summary *pet_expr_call_get_summary(
2479 __isl_keep pet_expr *expr)
2481 if (!expr)
2482 return NULL;
2483 if (expr->type != pet_expr_call)
2484 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2485 "not a call expression", return NULL);
2487 return pet_function_summary_copy(expr->c.summary);
2490 /* Replace the function summary associated to the call expression "expr"
2491 * by "summary".
2493 __isl_give pet_expr *pet_expr_call_set_summary(__isl_take pet_expr *expr,
2494 __isl_take pet_function_summary *summary)
2496 expr = pet_expr_cow(expr);
2497 if (!expr || !summary)
2498 goto error;
2499 if (expr->type != pet_expr_call)
2500 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2501 "not a call expression", goto error);
2502 pet_function_summary_free(expr->c.summary);
2503 expr->c.summary = summary;
2504 return expr;
2505 error:
2506 pet_function_summary_free(summary);
2507 return pet_expr_free(expr);
2510 /* Replace the type of the cast performed by "expr" by "name".
2512 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
2513 __isl_keep const char *name)
2515 expr = pet_expr_cow(expr);
2516 if (!expr || !name)
2517 return pet_expr_free(expr);
2518 if (expr->type != pet_expr_cast)
2519 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2520 "not a cast expression", return pet_expr_free(expr));
2521 free(expr->type_name);
2522 expr->type_name = strdup(name);
2523 if (!expr->type_name)
2524 return pet_expr_free(expr);
2525 return expr;
2528 /* Return the value of the integer represented by "expr".
2530 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
2532 if (!expr)
2533 return NULL;
2534 if (expr->type != pet_expr_int)
2535 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2536 "not an int expression", return NULL);
2538 return isl_val_copy(expr->i);
2541 /* Replace the value of the integer represented by "expr" by "v".
2543 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
2544 __isl_take isl_val *v)
2546 expr = pet_expr_cow(expr);
2547 if (!expr || !v)
2548 goto error;
2549 if (expr->type != pet_expr_int)
2550 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2551 "not an int expression", goto error);
2552 isl_val_free(expr->i);
2553 expr->i = v;
2555 return expr;
2556 error:
2557 isl_val_free(v);
2558 pet_expr_free(expr);
2559 return NULL;
2562 /* Replace the value and string representation of the double
2563 * represented by "expr" by "d" and "s".
2565 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2566 double d, __isl_keep const char *s)
2568 expr = pet_expr_cow(expr);
2569 if (!expr || !s)
2570 return pet_expr_free(expr);
2571 if (expr->type != pet_expr_double)
2572 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2573 "not a double expression", return pet_expr_free(expr));
2574 expr->d.val = d;
2575 free(expr->d.s);
2576 expr->d.s = strdup(s);
2577 if (!expr->d.s)
2578 return pet_expr_free(expr);
2579 return expr;
2582 /* Return a string representation of the double expression "expr".
2584 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2586 if (!expr)
2587 return NULL;
2588 if (expr->type != pet_expr_double)
2589 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2590 "not a double expression", return NULL);
2591 return strdup(expr->d.s);
2594 /* Return a piecewise affine expression defined on the specified domain
2595 * that represents NaN.
2597 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2599 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2602 /* This function is called when we come across an access that is
2603 * nested in what is supposed to be an affine expression.
2604 * "pc" is the context in which the affine expression is created.
2605 * If nesting is allowed in "pc", we return an affine expression that is
2606 * equal to a new parameter corresponding to this nested access.
2607 * Otherwise, we return NaN.
2609 * Note that we currently don't allow nested accesses themselves
2610 * to contain any nested accesses, so we check if "expr" itself
2611 * involves any nested accesses (either explicitly as arguments
2612 * or implicitly through parameters) and return NaN if it does.
2614 * The new parameter is resolved in resolve_nested.
2616 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2617 __isl_keep pet_context *pc)
2619 isl_ctx *ctx;
2620 isl_id *id;
2621 isl_space *space;
2622 isl_local_space *ls;
2623 isl_aff *aff;
2624 int nested;
2626 if (!expr || !pc)
2627 return NULL;
2628 if (!pet_context_allow_nesting(pc))
2629 return non_affine(pet_context_get_space(pc));
2631 if (pet_expr_get_type(expr) != pet_expr_access)
2632 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2633 "not an access expression", return NULL);
2635 if (expr->n_arg > 0)
2636 return non_affine(pet_context_get_space(pc));
2638 space = pet_expr_access_get_parameter_space(expr);
2639 nested = pet_nested_any_in_space(space);
2640 isl_space_free(space);
2641 if (nested)
2642 return non_affine(pet_context_get_space(pc));
2644 ctx = pet_expr_get_ctx(expr);
2645 id = pet_nested_pet_expr(pet_expr_copy(expr));
2646 space = pet_context_get_space(pc);
2647 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2649 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2650 ls = isl_local_space_from_space(space);
2651 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2653 return isl_pw_aff_from_aff(aff);
2656 /* Extract an affine expression from the access pet_expr "expr".
2657 * "pc" is the context in which the affine expression is created.
2659 * If "expr" is actually an affine expression rather than
2660 * a real access, then we return that expression.
2661 * Otherwise, we require that "expr" is of an integral type.
2662 * If not, we return NaN.
2664 * If the variable has been assigned a known affine expression,
2665 * then we return that expression.
2667 * Otherwise, we return an expression that is equal to a parameter
2668 * representing "expr" (if "allow_nested" is set).
2670 static __isl_give isl_pw_aff *extract_affine_from_access(
2671 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2673 int pos;
2674 isl_id *id;
2676 if (pet_expr_is_affine(expr)) {
2677 isl_pw_aff *pa;
2678 isl_multi_pw_aff *mpa;
2680 mpa = pet_expr_access_get_index(expr);
2681 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
2682 isl_multi_pw_aff_free(mpa);
2683 return pa;
2686 if (pet_expr_get_type_size(expr) == 0)
2687 return non_affine(pet_context_get_space(pc));
2689 if (!pet_expr_is_scalar_access(expr))
2690 return nested_access(expr, pc);
2692 id = pet_expr_access_get_id(expr);
2693 if (pet_context_is_assigned(pc, id))
2694 return pet_context_get_value(pc, id);
2696 isl_id_free(id);
2697 return nested_access(expr, pc);
2700 /* Construct an affine expression from the integer constant "expr".
2701 * "pc" is the context in which the affine expression is created.
2703 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2704 __isl_keep pet_context *pc)
2706 isl_local_space *ls;
2707 isl_aff *aff;
2709 if (!expr)
2710 return NULL;
2712 ls = isl_local_space_from_space(pet_context_get_space(pc));
2713 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2715 return isl_pw_aff_from_aff(aff);
2718 /* Extract an affine expression from an addition or subtraction operation.
2719 * Return NaN if we are unable to extract an affine expression.
2721 * "pc" is the context in which the affine expression is created.
2723 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2724 __isl_keep pet_context *pc)
2726 isl_pw_aff *lhs;
2727 isl_pw_aff *rhs;
2729 if (!expr)
2730 return NULL;
2731 if (expr->n_arg != 2)
2732 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2733 "expecting two arguments", return NULL);
2735 lhs = pet_expr_extract_affine(expr->args[0], pc);
2736 rhs = pet_expr_extract_affine(expr->args[1], pc);
2738 switch (pet_expr_op_get_type(expr)) {
2739 case pet_op_add:
2740 return isl_pw_aff_add(lhs, rhs);
2741 case pet_op_sub:
2742 return isl_pw_aff_sub(lhs, rhs);
2743 default:
2744 isl_pw_aff_free(lhs);
2745 isl_pw_aff_free(rhs);
2746 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2747 "not an addition or subtraction operation",
2748 return NULL);
2753 /* Extract an affine expression from an integer division or a modulo operation.
2754 * Return NaN if we are unable to extract an affine expression.
2756 * "pc" is the context in which the affine expression is created.
2758 * In particular, if "expr" is lhs/rhs, then return
2760 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2762 * If "expr" is lhs%rhs, then return
2764 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2766 * If the second argument (rhs) is not a (positive) integer constant,
2767 * then we fail to extract an affine expression.
2769 * We simplify the result in the context of the domain of "pc" in case
2770 * this domain implies that lhs >= 0 (or < 0).
2772 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2773 __isl_keep pet_context *pc)
2775 int is_cst;
2776 isl_pw_aff *lhs;
2777 isl_pw_aff *rhs;
2778 isl_pw_aff *res;
2780 if (!expr)
2781 return NULL;
2782 if (expr->n_arg != 2)
2783 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2784 "expecting two arguments", return NULL);
2786 rhs = pet_expr_extract_affine(expr->args[1], pc);
2788 is_cst = isl_pw_aff_is_cst(rhs);
2789 if (is_cst < 0 || !is_cst) {
2790 isl_pw_aff_free(rhs);
2791 return non_affine(pet_context_get_space(pc));
2794 lhs = pet_expr_extract_affine(expr->args[0], pc);
2796 switch (pet_expr_op_get_type(expr)) {
2797 case pet_op_div:
2798 res = isl_pw_aff_tdiv_q(lhs, rhs);
2799 break;
2800 case pet_op_mod:
2801 res = isl_pw_aff_tdiv_r(lhs, rhs);
2802 break;
2803 default:
2804 isl_pw_aff_free(lhs);
2805 isl_pw_aff_free(rhs);
2806 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2807 "not a div or mod operator", return NULL);
2810 return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc));
2813 /* Extract an affine expression from a multiplication operation.
2814 * Return NaN if we are unable to extract an affine expression.
2815 * In particular, if neither of the arguments is a (piecewise) constant
2816 * then we return NaN.
2818 * "pc" is the context in which the affine expression is created.
2820 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2821 __isl_keep pet_context *pc)
2823 int lhs_cst, rhs_cst;
2824 isl_pw_aff *lhs;
2825 isl_pw_aff *rhs;
2827 if (!expr)
2828 return NULL;
2829 if (expr->n_arg != 2)
2830 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2831 "expecting two arguments", return NULL);
2833 lhs = pet_expr_extract_affine(expr->args[0], pc);
2834 rhs = pet_expr_extract_affine(expr->args[1], pc);
2836 lhs_cst = isl_pw_aff_is_cst(lhs);
2837 rhs_cst = isl_pw_aff_is_cst(rhs);
2838 if (lhs_cst >= 0 && rhs_cst >= 0 && (lhs_cst || rhs_cst))
2839 return isl_pw_aff_mul(lhs, rhs);
2841 isl_pw_aff_free(lhs);
2842 isl_pw_aff_free(rhs);
2844 if (lhs_cst < 0 || rhs_cst < 0)
2845 return NULL;
2847 return non_affine(pet_context_get_space(pc));
2850 /* Extract an affine expression from a negation operation.
2851 * Return NaN if we are unable to extract an affine expression.
2853 * "pc" is the context in which the affine expression is created.
2855 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
2856 __isl_keep pet_context *pc)
2858 isl_pw_aff *res;
2860 if (!expr)
2861 return NULL;
2862 if (expr->n_arg != 1)
2863 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2864 "expecting one argument", return NULL);
2866 res = pet_expr_extract_affine(expr->args[0], pc);
2867 return isl_pw_aff_neg(res);
2870 /* Extract an affine expression from a conditional operation.
2871 * Return NaN if we are unable to extract an affine expression.
2873 * "pc" is the context in which the affine expression is created.
2875 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
2876 __isl_keep pet_context *pc)
2878 isl_pw_aff *cond, *lhs, *rhs;
2880 if (!expr)
2881 return NULL;
2882 if (expr->n_arg != 3)
2883 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2884 "expecting three arguments", return NULL);
2886 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2887 lhs = pet_expr_extract_affine(expr->args[1], pc);
2888 rhs = pet_expr_extract_affine(expr->args[2], pc);
2890 return isl_pw_aff_cond(cond, lhs, rhs);
2893 /* Compute
2895 * pwaff mod 2^width
2897 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff, unsigned width)
2899 isl_ctx *ctx;
2900 isl_val *mod;
2902 ctx = isl_pw_aff_get_ctx(pwaff);
2903 mod = isl_val_int_from_ui(ctx, width);
2904 mod = isl_val_2exp(mod);
2906 pwaff = isl_pw_aff_mod_val(pwaff, mod);
2908 return pwaff;
2911 /* Limit the domain of "pwaff" to those elements where the function
2912 * value satisfies
2914 * 2^{width-1} <= pwaff < 2^{width-1}
2916 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
2917 unsigned width)
2919 isl_ctx *ctx;
2920 isl_val *v;
2921 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
2922 isl_local_space *ls = isl_local_space_from_space(space);
2923 isl_aff *bound;
2924 isl_set *dom;
2925 isl_pw_aff *b;
2927 ctx = isl_pw_aff_get_ctx(pwaff);
2928 v = isl_val_int_from_ui(ctx, width - 1);
2929 v = isl_val_2exp(v);
2931 bound = isl_aff_zero_on_domain(ls);
2932 bound = isl_aff_add_constant_val(bound, v);
2933 b = isl_pw_aff_from_aff(bound);
2935 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
2936 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2938 b = isl_pw_aff_neg(b);
2939 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
2940 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2942 return pwaff;
2945 /* Handle potential overflows on signed computations.
2947 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2948 * then we adjust the domain of "pa" to avoid overflows.
2950 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
2951 unsigned width)
2953 isl_ctx *ctx;
2954 struct pet_options *options;
2956 if (!pa)
2957 return NULL;
2959 ctx = isl_pw_aff_get_ctx(pa);
2960 options = isl_ctx_peek_pet_options(ctx);
2961 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
2962 pa = avoid_overflow(pa, width);
2964 return pa;
2967 /* Extract an affine expression from some an operation.
2968 * Return NaN if we are unable to extract an affine expression.
2969 * If the result of a binary (non boolean) operation is unsigned,
2970 * then we wrap it based on the size of the type. If the result is signed,
2971 * then we ensure that no overflow occurs.
2973 * "pc" is the context in which the affine expression is created.
2975 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
2976 __isl_keep pet_context *pc)
2978 isl_pw_aff *res;
2979 int type_size;
2981 switch (pet_expr_op_get_type(expr)) {
2982 case pet_op_add:
2983 case pet_op_sub:
2984 res = extract_affine_add_sub(expr, pc);
2985 break;
2986 case pet_op_div:
2987 case pet_op_mod:
2988 res = extract_affine_div_mod(expr, pc);
2989 break;
2990 case pet_op_mul:
2991 res = extract_affine_mul(expr, pc);
2992 break;
2993 case pet_op_minus:
2994 return extract_affine_neg(expr, pc);
2995 case pet_op_cond:
2996 return extract_affine_cond(expr, pc);
2997 case pet_op_eq:
2998 case pet_op_ne:
2999 case pet_op_le:
3000 case pet_op_ge:
3001 case pet_op_lt:
3002 case pet_op_gt:
3003 case pet_op_land:
3004 case pet_op_lor:
3005 case pet_op_lnot:
3006 return pet_expr_extract_affine_condition(expr, pc);
3007 default:
3008 return non_affine(pet_context_get_space(pc));
3011 if (!res)
3012 return NULL;
3013 if (isl_pw_aff_involves_nan(res)) {
3014 isl_space *space = isl_pw_aff_get_domain_space(res);
3015 isl_pw_aff_free(res);
3016 return non_affine(space);
3019 type_size = pet_expr_get_type_size(expr);
3020 if (type_size > 0)
3021 res = wrap(res, type_size);
3022 else
3023 res = signed_overflow(res, -type_size);
3025 return res;
3028 /* Internal data structure for affine builtin function declarations.
3030 * "pencil" is set if the builtin is pencil specific.
3031 * "n_args" is the number of arguments the function takes.
3032 * "name" is the function name.
3034 struct affine_builtin_decl {
3035 int pencil;
3036 int n_args;
3037 const char *name;
3040 static struct affine_builtin_decl affine_builtins[] = {
3041 { 0, 2, "min" },
3042 { 1, 2, "imin" },
3043 { 1, 2, "umin" },
3044 { 0, 2, "max" },
3045 { 1, 2, "imax" },
3046 { 1, 2, "umax" },
3047 { 0, 2, "intMod" },
3048 { 0, 2, "intFloor" },
3049 { 0, 2, "intCeil" },
3050 { 0, 2, "floord" },
3051 { 0, 2, "ceild" }
3054 /* List of min and max builtin functions.
3056 static const char *min_max_builtins[] = {
3057 "min", "imin", "umin",
3058 "max", "imax", "umax"
3061 /* Is a function call to "name" with "n_args" arguments a call to a
3062 * builtin function for which we can construct an affine expression?
3063 * pencil specific builtins are only recognized if "pencil" is set.
3065 static int is_affine_builtin(int pencil, int n_args, const char *name)
3067 int i;
3069 for (i = 0; i < ARRAY_SIZE(affine_builtins); ++i) {
3070 struct affine_builtin_decl *decl = &affine_builtins[i];
3072 if (decl->pencil && !pencil)
3073 continue;
3074 if (decl->n_args == n_args && !strcmp(decl->name, name))
3075 return 1;
3078 return 0;
3081 /* Is function "name" a known min or max builtin function?
3083 static int is_min_or_max_builtin(const char *name)
3085 int i;
3087 for (i = 0; i < ARRAY_SIZE(min_max_builtins); ++i)
3088 if (!strcmp(min_max_builtins[i], name))
3089 return 1;
3091 return 0;
3094 /* Extract an affine expression from some special function calls.
3095 * Return NaN if we are unable to extract an affine expression.
3096 * In particular, we handle "min", "max", "ceild", "floord",
3097 * "intMod", "intFloor" and "intCeil".
3098 * In case of the latter five, the second argument needs to be
3099 * a (positive) integer constant.
3100 * If the pencil option is set, then we also handle "{i,u}min" and
3101 * "{i,u}max".
3103 * "pc" is the context in which the affine expression is created.
3105 static __isl_give isl_pw_aff *extract_affine_from_call(
3106 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3108 isl_ctx *ctx;
3109 isl_pw_aff *aff1, *aff2;
3110 int n;
3111 const char *name;
3112 struct pet_options *options;
3114 if (!expr)
3115 return NULL;
3116 ctx = pet_expr_get_ctx(expr);
3117 options = isl_ctx_peek_pet_options(ctx);
3119 n = pet_expr_get_n_arg(expr);
3120 name = pet_expr_call_get_name(expr);
3121 if (!is_affine_builtin(options->pencil, n, name))
3122 return non_affine(pet_context_get_space(pc));
3124 if (is_min_or_max_builtin(name)) {
3125 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3126 aff2 = pet_expr_extract_affine(expr->args[1], pc);
3128 if (strstr(name, "min"))
3129 aff1 = isl_pw_aff_min(aff1, aff2);
3130 else
3131 aff1 = isl_pw_aff_max(aff1, aff2);
3132 } else if (!strcmp(name, "intMod")) {
3133 isl_val *v;
3135 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3136 return non_affine(pet_context_get_space(pc));
3137 v = pet_expr_int_get_val(expr->args[1]);
3138 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3139 aff1 = isl_pw_aff_mod_val(aff1, v);
3140 } else {
3141 isl_val *v;
3143 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3144 return non_affine(pet_context_get_space(pc));
3145 v = pet_expr_int_get_val(expr->args[1]);
3146 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3147 aff1 = isl_pw_aff_scale_down_val(aff1, v);
3148 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
3149 aff1 = isl_pw_aff_floor(aff1);
3150 else
3151 aff1 = isl_pw_aff_ceil(aff1);
3154 return aff1;
3157 /* Extract an affine expression from "expr", if possible.
3158 * Otherwise return NaN.
3160 * "pc" is the context in which the affine expression is created.
3162 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
3163 __isl_keep pet_context *pc)
3165 if (!expr)
3166 return NULL;
3168 switch (pet_expr_get_type(expr)) {
3169 case pet_expr_access:
3170 return extract_affine_from_access(expr, pc);
3171 case pet_expr_int:
3172 return extract_affine_from_int(expr, pc);
3173 case pet_expr_op:
3174 return extract_affine_from_op(expr, pc);
3175 case pet_expr_call:
3176 return extract_affine_from_call(expr, pc);
3177 case pet_expr_cast:
3178 case pet_expr_double:
3179 case pet_expr_error:
3180 return non_affine(pet_context_get_space(pc));
3184 /* Extract an affine expressions representing the comparison "LHS op RHS"
3185 * Return NaN if we are unable to extract such an affine expression.
3187 * "pc" is the context in which the affine expression is created.
3189 * If the comparison is of the form
3191 * a <= min(b,c)
3193 * then the expression is constructed as the conjunction of
3194 * the comparisons
3196 * a <= b and a <= c
3198 * A similar optimization is performed for max(a,b) <= c.
3199 * We do this because that will lead to simpler representations
3200 * of the expression.
3201 * If isl is ever enhanced to explicitly deal with min and max expressions,
3202 * this optimization can be removed.
3204 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
3205 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
3206 __isl_keep pet_context *pc)
3208 isl_pw_aff *lhs_pa, *rhs_pa;
3210 if (op == pet_op_gt)
3211 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
3212 if (op == pet_op_ge)
3213 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
3215 if (op == pet_op_lt || op == pet_op_le) {
3216 if (pet_expr_is_min(rhs)) {
3217 lhs_pa = pet_expr_extract_comparison(op, lhs,
3218 rhs->args[0], pc);
3219 rhs_pa = pet_expr_extract_comparison(op, lhs,
3220 rhs->args[1], pc);
3221 return pet_and(lhs_pa, rhs_pa);
3223 if (pet_expr_is_max(lhs)) {
3224 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
3225 rhs, pc);
3226 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
3227 rhs, pc);
3228 return pet_and(lhs_pa, rhs_pa);
3232 lhs_pa = pet_expr_extract_affine(lhs, pc);
3233 rhs_pa = pet_expr_extract_affine(rhs, pc);
3235 return pet_comparison(op, lhs_pa, rhs_pa);
3238 /* Extract an affine expressions from the comparison "expr".
3239 * Return NaN if we are unable to extract such an affine expression.
3241 * "pc" is the context in which the affine expression is created.
3243 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
3244 __isl_keep pet_context *pc)
3246 enum pet_op_type type;
3248 if (!expr)
3249 return NULL;
3250 if (expr->n_arg != 2)
3251 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3252 "expecting two arguments", return NULL);
3254 type = pet_expr_op_get_type(expr);
3255 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
3256 pc);
3259 /* Extract an affine expression representing the boolean operation
3260 * expressed by "expr".
3261 * Return NaN if we are unable to extract an affine expression.
3263 * "pc" is the context in which the affine expression is created.
3265 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
3266 __isl_keep pet_context *pc)
3268 isl_pw_aff *lhs, *rhs;
3269 int n;
3271 if (!expr)
3272 return NULL;
3274 n = pet_expr_get_n_arg(expr);
3275 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
3276 if (n == 1)
3277 return pet_not(lhs);
3279 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
3280 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
3283 /* Extract the affine expression "expr != 0 ? 1 : 0".
3284 * Return NaN if we are unable to extract an affine expression.
3286 * "pc" is the context in which the affine expression is created.
3288 static __isl_give isl_pw_aff *extract_implicit_condition(
3289 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3291 isl_pw_aff *res;
3293 res = pet_expr_extract_affine(expr, pc);
3294 return pet_to_bool(res);
3297 /* Extract a boolean affine expression from "expr".
3298 * Return NaN if we are unable to extract an affine expression.
3300 * "pc" is the context in which the affine expression is created.
3302 * If "expr" is neither a comparison nor a boolean operation,
3303 * then we assume it is an affine expression and return the
3304 * boolean expression "expr != 0 ? 1 : 0".
3306 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
3307 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3309 if (!expr)
3310 return NULL;
3312 if (pet_expr_is_comparison(expr))
3313 return extract_comparison(expr, pc);
3314 if (pet_expr_is_boolean(expr))
3315 return extract_boolean(expr, pc);
3317 return extract_implicit_condition(expr, pc);
3320 /* Check if "expr" is an assume expression and if its single argument
3321 * can be converted to an affine expression in the context of "pc".
3322 * If so, replace the argument by the affine expression.
3324 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
3325 __isl_keep pet_context *pc)
3327 isl_pw_aff *cond;
3328 isl_multi_pw_aff *index;
3330 if (!expr)
3331 return NULL;
3332 if (!pet_expr_is_assume(expr))
3333 return expr;
3334 if (expr->n_arg != 1)
3335 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3336 "expecting one argument", return pet_expr_free(expr));
3338 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3339 if (!cond)
3340 return pet_expr_free(expr);
3341 if (isl_pw_aff_involves_nan(cond)) {
3342 isl_pw_aff_free(cond);
3343 return expr;
3346 index = isl_multi_pw_aff_from_pw_aff(cond);
3347 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
3349 return expr;
3352 /* Return the number of bits needed to represent the type of "expr".
3353 * See the description of the type_size field of pet_expr.
3355 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
3357 return expr ? expr->type_size : 0;
3360 /* Replace the number of bits needed to represent the type of "expr"
3361 * by "type_size".
3362 * See the description of the type_size field of pet_expr.
3364 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
3365 int type_size)
3367 expr = pet_expr_cow(expr);
3368 if (!expr)
3369 return NULL;
3371 expr->type_size = type_size;
3373 return expr;
3376 /* Extend an access expression "expr" with an additional index "index".
3377 * In particular, add "index" as an extra argument to "expr" and
3378 * adjust the index expression of "expr" to refer to this extra argument.
3379 * The caller is responsible for calling pet_expr_access_set_depth
3380 * to update the corresponding access relation.
3382 * Note that we only collect the individual index expressions as
3383 * arguments of "expr" here.
3384 * An attempt to integrate them into the index expression of "expr"
3385 * is performed in pet_expr_access_plug_in_args.
3387 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
3388 __isl_take pet_expr *index)
3390 int n;
3391 isl_space *space;
3392 isl_local_space *ls;
3393 isl_pw_aff *pa;
3395 expr = pet_expr_cow(expr);
3396 if (!expr || !index)
3397 goto error;
3398 if (expr->type != pet_expr_access)
3399 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3400 "not an access pet_expr", goto error);
3402 n = pet_expr_get_n_arg(expr);
3403 expr = pet_expr_insert_arg(expr, n, index);
3404 if (!expr)
3405 return NULL;
3407 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3408 ls = isl_local_space_from_space(space);
3409 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
3410 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
3411 if (!expr->acc.index)
3412 return pet_expr_free(expr);
3414 return expr;
3415 error:
3416 pet_expr_free(expr);
3417 pet_expr_free(index);
3418 return NULL;
3421 /* Extend an access expression "expr" with an additional member acces to "id".
3422 * In particular, extend the index expression of "expr" to include
3423 * the additional member access.
3424 * The caller is responsible for calling pet_expr_access_set_depth
3425 * to update the corresponding access relation.
3427 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
3428 __isl_take isl_id *id)
3430 isl_space *space;
3431 isl_multi_pw_aff *field_access;
3433 expr = pet_expr_cow(expr);
3434 if (!expr || !id)
3435 goto error;
3436 if (expr->type != pet_expr_access)
3437 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3438 "not an access pet_expr", goto error);
3440 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3441 space = isl_space_from_domain(space);
3442 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3443 field_access = isl_multi_pw_aff_zero(space);
3444 expr->acc.index = pet_array_member(expr->acc.index, field_access);
3445 if (!expr->acc.index)
3446 return pet_expr_free(expr);
3448 return expr;
3449 error:
3450 pet_expr_free(expr);
3451 isl_id_free(id);
3452 return NULL;
3455 /* Prefix the access expression "expr" with "prefix".
3456 * If "add" is set, then it is not the index expression "prefix" itself
3457 * that was passed to the function, but its address.
3459 __isl_give pet_expr *pet_expr_access_patch(__isl_take pet_expr *expr,
3460 __isl_take isl_multi_pw_aff *prefix, int add)
3462 enum pet_expr_access_type type;
3464 expr = pet_expr_cow(expr);
3465 if (!expr || !prefix)
3466 goto error;
3467 if (expr->type != pet_expr_access)
3468 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3469 "not an access pet_expr", goto error);
3471 expr->acc.depth += isl_multi_pw_aff_dim(prefix, isl_dim_out) - add;
3472 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
3473 if (!expr->acc.access[type])
3474 continue;
3475 expr->acc.access[type] = pet_patch_union_map(
3476 isl_multi_pw_aff_copy(prefix), expr->acc.access[type],
3477 add, 0);
3478 if (!expr->acc.access[type])
3479 break;
3481 expr->acc.index = pet_patch_multi_pw_aff(prefix, expr->acc.index, add);
3482 if (!expr->acc.index || type < pet_expr_access_end)
3483 return pet_expr_free(expr);
3485 return expr;
3486 error:
3487 pet_expr_free(expr);
3488 isl_multi_pw_aff_free(prefix);
3489 return NULL;
3492 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
3494 int i;
3496 if (!expr)
3497 return;
3499 fprintf(stderr, "%*s", indent, "");
3501 switch (expr->type) {
3502 case pet_expr_double:
3503 fprintf(stderr, "%s\n", expr->d.s);
3504 break;
3505 case pet_expr_int:
3506 isl_val_dump(expr->i);
3507 break;
3508 case pet_expr_access:
3509 if (expr->acc.ref_id) {
3510 isl_id_dump(expr->acc.ref_id);
3511 fprintf(stderr, "%*s", indent, "");
3513 isl_multi_pw_aff_dump(expr->acc.index);
3514 fprintf(stderr, "%*sdepth: %d\n", indent + 2,
3515 "", expr->acc.depth);
3516 if (expr->acc.kill) {
3517 fprintf(stderr, "%*skill: 1\n", indent + 2, "");
3518 } else {
3519 fprintf(stderr, "%*sread: %d\n", indent + 2,
3520 "", expr->acc.read);
3521 fprintf(stderr, "%*swrite: %d\n", indent + 2,
3522 "", expr->acc.write);
3524 if (expr->acc.access[pet_expr_access_may_read]) {
3525 fprintf(stderr, "%*smay_read: ", indent + 2, "");
3526 isl_union_map_dump(
3527 expr->acc.access[pet_expr_access_may_read]);
3529 if (expr->acc.access[pet_expr_access_may_write]) {
3530 fprintf(stderr, "%*smay_write: ", indent + 2, "");
3531 isl_union_map_dump(
3532 expr->acc.access[pet_expr_access_may_write]);
3534 if (expr->acc.access[pet_expr_access_must_write]) {
3535 fprintf(stderr, "%*smust_write: ", indent + 2, "");
3536 isl_union_map_dump(
3537 expr->acc.access[pet_expr_access_must_write]);
3539 for (i = 0; i < expr->n_arg; ++i)
3540 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3541 break;
3542 case pet_expr_op:
3543 fprintf(stderr, "%s\n", op_str[expr->op]);
3544 for (i = 0; i < expr->n_arg; ++i)
3545 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3546 break;
3547 case pet_expr_call:
3548 fprintf(stderr, "%s/%d\n", expr->c.name, expr->n_arg);
3549 for (i = 0; i < expr->n_arg; ++i)
3550 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3551 if (expr->c.summary) {
3552 fprintf(stderr, "%*s", indent, "");
3553 fprintf(stderr, "summary:\n");
3554 pet_function_summary_dump_with_indent(expr->c.summary,
3555 indent + 2);
3557 break;
3558 case pet_expr_cast:
3559 fprintf(stderr, "(%s)\n", expr->type_name);
3560 for (i = 0; i < expr->n_arg; ++i)
3561 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3562 break;
3563 case pet_expr_error:
3564 fprintf(stderr, "ERROR\n");
3565 break;
3569 void pet_expr_dump(__isl_keep pet_expr *expr)
3571 pet_expr_dump_with_indent(expr, 0);