codegen_test.sh: remove test output file on success
[pet.git] / expr.c
blob42d3fb38e24a843ee07060fbf53c1dad146a232b
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 int pet_expr_is_affine(__isl_keep pet_expr *expr)
828 int has_id;
830 if (!expr)
831 return -1;
832 if (expr->type != pet_expr_access)
833 return 0;
835 has_id = isl_multi_pw_aff_has_tuple_id(expr->acc.index, isl_dim_out);
836 if (has_id < 0)
837 return -1;
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 int pet_expr_access_is_read(__isl_keep pet_expr *expr)
1050 if (!expr)
1051 return -1;
1052 if (expr->type != pet_expr_access)
1053 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1054 "not an access expression", return -1);
1056 return expr->acc.read;
1059 /* Does the access expression "expr" write to the accessed elements?
1061 int pet_expr_access_is_write(__isl_keep pet_expr *expr)
1063 if (!expr)
1064 return -1;
1065 if (expr->type != pet_expr_access)
1066 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1067 "not an access expression", return -1);
1069 return expr->acc.write;
1072 /* Return the identifier of the array accessed by "expr".
1074 * If "expr" represents a member access, then return the identifier
1075 * of the outer structure array.
1077 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
1079 if (!expr)
1080 return NULL;
1081 if (expr->type != pet_expr_access)
1082 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1083 "not an access expression", return NULL);
1085 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index)) {
1086 isl_space *space;
1087 isl_id *id;
1089 space = isl_multi_pw_aff_get_space(expr->acc.index);
1090 space = isl_space_range(space);
1091 while (space && isl_space_is_wrapping(space))
1092 space = isl_space_domain(isl_space_unwrap(space));
1093 id = isl_space_get_tuple_id(space, isl_dim_set);
1094 isl_space_free(space);
1096 return id;
1099 return isl_multi_pw_aff_get_tuple_id(expr->acc.index, isl_dim_out);
1102 /* Return the parameter space of "expr".
1104 __isl_give isl_space *pet_expr_access_get_parameter_space(
1105 __isl_keep pet_expr *expr)
1107 isl_space *space;
1109 if (!expr)
1110 return NULL;
1111 if (expr->type != pet_expr_access)
1112 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1113 "not an access expression", return NULL);
1115 space = isl_multi_pw_aff_get_space(expr->acc.index);
1116 space = isl_space_params(space);
1118 return space;
1121 /* Return the domain space of "expr", including the arguments (if any).
1123 __isl_give isl_space *pet_expr_access_get_augmented_domain_space(
1124 __isl_keep pet_expr *expr)
1126 isl_space *space;
1128 if (!expr)
1129 return NULL;
1130 if (expr->type != pet_expr_access)
1131 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1132 "not an access expression", return NULL);
1134 space = isl_multi_pw_aff_get_space(expr->acc.index);
1135 space = isl_space_domain(space);
1137 return space;
1140 /* Return the domain space of "expr", without the arguments (if any).
1142 __isl_give isl_space *pet_expr_access_get_domain_space(
1143 __isl_keep pet_expr *expr)
1145 isl_space *space;
1147 space = pet_expr_access_get_augmented_domain_space(expr);
1148 if (isl_space_is_wrapping(space))
1149 space = isl_space_domain(isl_space_unwrap(space));
1151 return space;
1154 /* Return the space of the data accessed by "expr".
1156 __isl_give isl_space *pet_expr_access_get_data_space(__isl_keep pet_expr *expr)
1158 isl_space *space;
1160 if (!expr)
1161 return NULL;
1162 if (expr->type != pet_expr_access)
1163 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1164 "not an access expression", return NULL);
1166 space = isl_multi_pw_aff_get_space(expr->acc.index);
1167 space = isl_space_range(space);
1169 return space;
1172 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1174 static __isl_give pet_expr *pet_expr_map_expr_of_type(__isl_take pet_expr *expr,
1175 enum pet_expr_type type,
1176 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1177 void *user)
1179 int i, n;
1181 n = pet_expr_get_n_arg(expr);
1182 for (i = 0; i < n; ++i) {
1183 pet_expr *arg = pet_expr_get_arg(expr, i);
1184 arg = pet_expr_map_expr_of_type(arg, type, fn, user);
1185 expr = pet_expr_set_arg(expr, i, arg);
1188 if (!expr)
1189 return NULL;
1191 if (expr->type == type)
1192 expr = fn(expr, user);
1194 return expr;
1197 /* Modify all expressions of type pet_expr_access in "expr"
1198 * by calling "fn" on them.
1200 __isl_give pet_expr *pet_expr_map_access(__isl_take pet_expr *expr,
1201 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1202 void *user)
1204 return pet_expr_map_expr_of_type(expr, pet_expr_access, fn, user);
1207 /* Modify all expressions of type pet_expr_call in "expr"
1208 * by calling "fn" on them.
1210 __isl_give pet_expr *pet_expr_map_call(__isl_take pet_expr *expr,
1211 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1212 void *user)
1214 return pet_expr_map_expr_of_type(expr, pet_expr_call, fn, user);
1217 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1219 * Return -1 on error (where fn returning a negative value is treated as
1220 * an error).
1221 * Otherwise return 0.
1223 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
1224 enum pet_expr_type type,
1225 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1227 int i;
1229 if (!expr)
1230 return -1;
1232 for (i = 0; i < expr->n_arg; ++i)
1233 if (pet_expr_foreach_expr_of_type(expr->args[i],
1234 type, fn, user) < 0)
1235 return -1;
1237 if (expr->type == type)
1238 return fn(expr, user);
1240 return 0;
1243 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1245 * Return -1 on error (where fn returning a negative value is treated as
1246 * an error).
1247 * Otherwise return 0.
1249 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
1250 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1252 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
1255 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1257 * Return -1 on error (where fn returning a negative value is treated as
1258 * an error).
1259 * Otherwise return 0.
1261 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
1262 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1264 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1267 /* Internal data structure for pet_expr_writes.
1268 * "id" is the identifier that we are looking for.
1269 * "found" is set if we have found the identifier being written to.
1271 struct pet_expr_writes_data {
1272 isl_id *id;
1273 int found;
1276 /* Given an access expression, check if it writes to data->id.
1277 * If so, set data->found and abort the search.
1279 static int writes(__isl_keep pet_expr *expr, void *user)
1281 struct pet_expr_writes_data *data = user;
1282 isl_id *write_id;
1284 if (!expr->acc.write)
1285 return 0;
1286 if (pet_expr_is_affine(expr))
1287 return 0;
1289 write_id = pet_expr_access_get_id(expr);
1290 isl_id_free(write_id);
1292 if (!write_id)
1293 return -1;
1295 if (write_id != data->id)
1296 return 0;
1298 data->found = 1;
1299 return -1;
1302 /* Does expression "expr" write to "id"?
1304 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1306 struct pet_expr_writes_data data;
1308 data.id = id;
1309 data.found = 0;
1310 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1311 !data.found)
1312 return -1;
1314 return data.found;
1317 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1318 * index expression and access relations of "expr" (if any)
1319 * to dimensions of "dst_type" at "dst_pos".
1321 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1322 enum isl_dim_type dst_type, unsigned dst_pos,
1323 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1325 enum pet_expr_access_type type;
1327 expr = pet_expr_cow(expr);
1328 if (!expr)
1329 return NULL;
1330 if (expr->type != pet_expr_access)
1331 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1332 "not an access pet_expr", return pet_expr_free(expr));
1334 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1335 if (!expr->acc.access[type])
1336 continue;
1337 expr->acc.access[type] =
1338 pet_union_map_move_dims(expr->acc.access[type],
1339 dst_type, dst_pos, src_type, src_pos, n);
1340 if (!expr->acc.access[type])
1341 break;
1343 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1344 dst_type, dst_pos, src_type, src_pos, n);
1345 if (!expr->acc.index || type < pet_expr_access_end)
1346 return pet_expr_free(expr);
1348 return expr;
1351 /* Replace the index expression and access relations (if any) of "expr"
1352 * by their preimages under the function represented by "ma".
1354 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1355 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1357 enum pet_expr_access_type type;
1359 expr = pet_expr_cow(expr);
1360 if (!expr || !ma)
1361 goto error;
1362 if (expr->type != pet_expr_access)
1363 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1364 "not an access pet_expr", goto error);
1366 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1367 if (!expr->acc.access[type])
1368 continue;
1369 expr->acc.access[type] =
1370 isl_union_map_preimage_domain_multi_aff(
1371 expr->acc.access[type], isl_multi_aff_copy(ma));
1372 if (!expr->acc.access[type])
1373 break;
1375 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1376 ma);
1377 if (!expr->acc.index || type < pet_expr_access_end)
1378 return pet_expr_free(expr);
1380 return expr;
1381 error:
1382 isl_multi_aff_free(ma);
1383 pet_expr_free(expr);
1384 return NULL;
1387 /* Replace the index expression and access relations (if any) of "expr"
1388 * by their preimages under the function represented by "mpa".
1390 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1391 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1393 enum pet_expr_access_type type;
1395 expr = pet_expr_cow(expr);
1396 if (!expr || !mpa)
1397 goto error;
1398 if (expr->type != pet_expr_access)
1399 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1400 "not an access pet_expr", goto error);
1402 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1403 if (!expr->acc.access[type])
1404 continue;
1405 expr->acc.access[type] =
1406 isl_union_map_preimage_domain_multi_pw_aff(
1407 expr->acc.access[type], isl_multi_pw_aff_copy(mpa));
1408 if (!expr->acc.access[type])
1409 break;
1411 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1412 expr->acc.index, mpa);
1413 if (!expr->acc.index || type < pet_expr_access_end)
1414 return pet_expr_free(expr);
1416 return expr;
1417 error:
1418 isl_multi_pw_aff_free(mpa);
1419 pet_expr_free(expr);
1420 return NULL;
1423 /* Return the index expression of access expression "expr".
1425 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1426 __isl_keep pet_expr *expr)
1428 if (!expr)
1429 return NULL;
1430 if (expr->type != pet_expr_access)
1431 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1432 "not an access expression", return NULL);
1434 return isl_multi_pw_aff_copy(expr->acc.index);
1437 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1439 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1441 isl_space *space;
1442 enum pet_expr_access_type type;
1444 expr = pet_expr_cow(expr);
1445 if (!expr)
1446 return NULL;
1447 if (expr->type != pet_expr_access)
1448 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1449 "not an access expression", return pet_expr_free(expr));
1451 if (!has_any_access_relation(expr))
1452 return expr;
1454 space = isl_multi_pw_aff_get_space(expr->acc.index);
1455 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1456 if (!expr->acc.access[type])
1457 continue;
1458 space = isl_space_align_params(space,
1459 isl_union_map_get_space(expr->acc.access[type]));
1461 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1462 isl_space_copy(space));
1463 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1464 if (!expr->acc.access[type])
1465 continue;
1466 expr->acc.access[type] =
1467 isl_union_map_align_params(expr->acc.access[type],
1468 isl_space_copy(space));
1469 if (!expr->acc.access[type])
1470 break;
1472 isl_space_free(space);
1473 if (!expr->acc.index || type < pet_expr_access_end)
1474 return pet_expr_free(expr);
1476 return expr;
1479 /* Are "expr1" and "expr2" both array accesses such that
1480 * the access relation of "expr1" is a subset of that of "expr2"?
1481 * Only take into account the first "n_arg" arguments.
1483 * This function is tailored for use by mark_self_dependences in nest.c.
1484 * In particular, the input expressions may have more than "n_arg"
1485 * elements in their arguments arrays, while only the first "n_arg"
1486 * elements are referenced from the access relations.
1488 int pet_expr_is_sub_access(__isl_keep pet_expr *expr1,
1489 __isl_keep pet_expr *expr2, int n_arg)
1491 isl_id *id1, *id2;
1492 int i, n1, n2;
1493 int is_subset;
1495 if (!expr1 || !expr2)
1496 return 0;
1497 if (pet_expr_get_type(expr1) != pet_expr_access)
1498 return 0;
1499 if (pet_expr_get_type(expr2) != pet_expr_access)
1500 return 0;
1501 if (pet_expr_is_affine(expr1))
1502 return 0;
1503 if (pet_expr_is_affine(expr2))
1504 return 0;
1505 n1 = pet_expr_get_n_arg(expr1);
1506 if (n1 > n_arg)
1507 n1 = n_arg;
1508 n2 = pet_expr_get_n_arg(expr2);
1509 if (n2 > n_arg)
1510 n2 = n_arg;
1511 if (n1 != n2)
1512 return 0;
1513 for (i = 0; i < n1; ++i) {
1514 int equal;
1515 equal = pet_expr_is_equal(expr1->args[i], expr2->args[i]);
1516 if (equal < 0 || !equal)
1517 return equal;
1519 id1 = pet_expr_access_get_id(expr1);
1520 id2 = pet_expr_access_get_id(expr2);
1521 isl_id_free(id1);
1522 isl_id_free(id2);
1523 if (!id1 || !id2)
1524 return 0;
1525 if (id1 != id2)
1526 return 0;
1528 expr1 = pet_expr_copy(expr1);
1529 expr2 = pet_expr_copy(expr2);
1530 expr1 = introduce_access_relations(expr1);
1531 expr2 = introduce_access_relations(expr2);
1532 if (!expr1 || !expr2)
1533 goto error;
1535 is_subset = isl_union_map_is_subset(
1536 expr1->acc.access[pet_expr_access_may_read],
1537 expr2->acc.access[pet_expr_access_may_read]);
1539 pet_expr_free(expr1);
1540 pet_expr_free(expr2);
1542 return is_subset;
1543 error:
1544 pet_expr_free(expr1);
1545 pet_expr_free(expr2);
1546 return -1;
1549 /* Given a set in the iteration space "domain", extend it to live in the space
1550 * of the domain of access relations.
1552 * That, is the number of arguments "n" is 0, then simply return domain.
1553 * Otherwise, return [domain -> [a_1,...,a_n]].
1555 static __isl_give isl_set *add_arguments(__isl_take isl_set *domain, int n)
1557 isl_map *map;
1559 if (n == 0)
1560 return domain;
1562 map = isl_map_from_domain(domain);
1563 map = isl_map_add_dims(map, isl_dim_out, n);
1564 return isl_map_wrap(map);
1567 /* Add extra conditions to the domains of all access relations in "expr",
1568 * introducing access relations if they are not already present.
1570 * The conditions are not added to the index expression. Instead, they
1571 * are used to try and simplify the index expression.
1573 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1574 __isl_take isl_set *cond)
1576 int i;
1577 isl_union_set *uset;
1578 enum pet_expr_access_type type;
1580 expr = pet_expr_cow(expr);
1581 if (!expr)
1582 goto error;
1584 for (i = 0; i < expr->n_arg; ++i) {
1585 expr->args[i] = pet_expr_restrict(expr->args[i],
1586 isl_set_copy(cond));
1587 if (!expr->args[i])
1588 goto error;
1591 if (expr->type != pet_expr_access) {
1592 isl_set_free(cond);
1593 return expr;
1596 expr = introduce_access_relations(expr);
1597 if (!expr)
1598 goto error;
1600 cond = add_arguments(cond, expr->n_arg);
1601 uset = isl_union_set_from_set(isl_set_copy(cond));
1602 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1603 if (!expr->acc.access[type])
1604 continue;
1605 expr->acc.access[type] =
1606 isl_union_map_intersect_domain(expr->acc.access[type],
1607 isl_union_set_copy(uset));
1608 if (!expr->acc.access[type])
1609 break;
1611 isl_union_set_free(uset);
1612 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, cond);
1613 if (type < pet_expr_access_end || !expr->acc.index)
1614 return pet_expr_free(expr);
1616 return expr;
1617 error:
1618 isl_set_free(cond);
1619 return pet_expr_free(expr);
1622 /* Modify the access relations (if any) and index expression
1623 * of the given access expression
1624 * based on the given iteration space transformation.
1625 * In particular, precompose the access relation and index expression
1626 * with the update function.
1628 * If the access has any arguments then the domain of the access relation
1629 * is a wrapped mapping from the iteration space to the space of
1630 * argument values. We only need to change the domain of this wrapped
1631 * mapping, so we extend the input transformation with an identity mapping
1632 * on the space of argument values.
1634 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1635 __isl_keep isl_multi_pw_aff *update)
1637 enum pet_expr_access_type type;
1639 expr = pet_expr_cow(expr);
1640 if (!expr)
1641 return NULL;
1642 if (expr->type != pet_expr_access)
1643 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1644 "not an access expression", return pet_expr_free(expr));
1646 update = isl_multi_pw_aff_copy(update);
1648 if (expr->n_arg > 0) {
1649 isl_space *space;
1650 isl_multi_pw_aff *id;
1652 space = isl_multi_pw_aff_get_space(expr->acc.index);
1653 space = isl_space_domain(space);
1654 space = isl_space_unwrap(space);
1655 space = isl_space_range(space);
1656 space = isl_space_map_from_set(space);
1657 id = isl_multi_pw_aff_identity(space);
1658 update = isl_multi_pw_aff_product(update, id);
1661 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1662 if (!expr->acc.access[type])
1663 continue;
1664 expr->acc.access[type] =
1665 isl_union_map_preimage_domain_multi_pw_aff(
1666 expr->acc.access[type],
1667 isl_multi_pw_aff_copy(update));
1668 if (!expr->acc.access[type])
1669 break;
1671 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1672 expr->acc.index, update);
1673 if (type < pet_expr_access_end || !expr->acc.index)
1674 return pet_expr_free(expr);
1676 return expr;
1679 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1681 isl_multi_pw_aff *update = user;
1683 return pet_expr_access_update_domain(expr, update);
1686 /* Modify all access relations in "expr" by precomposing them with
1687 * the given iteration space transformation.
1689 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1690 __isl_take isl_multi_pw_aff *update)
1692 expr = pet_expr_map_access(expr, &update_domain, update);
1693 isl_multi_pw_aff_free(update);
1694 return expr;
1697 /* Given an expression with accesses that have a 0D anonymous domain,
1698 * replace those domains by "space".
1700 __isl_give pet_expr *pet_expr_insert_domain(__isl_take pet_expr *expr,
1701 __isl_take isl_space *space)
1703 isl_multi_pw_aff *mpa;
1705 space = isl_space_from_domain(space);
1706 mpa = isl_multi_pw_aff_zero(space);
1707 return pet_expr_update_domain(expr, mpa);
1710 /* Add all parameters in "space" to the access relations (if any)
1711 * and index expression of "expr".
1713 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1715 isl_space *space = user;
1716 enum pet_expr_access_type type;
1718 expr = pet_expr_cow(expr);
1719 if (!expr)
1720 return NULL;
1721 if (expr->type != pet_expr_access)
1722 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1723 "not an access expression", return pet_expr_free(expr));
1725 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1726 if (!expr->acc.access[type])
1727 continue;
1728 expr->acc.access[type] =
1729 isl_union_map_align_params(expr->acc.access[type],
1730 isl_space_copy(space));
1731 if (!expr->acc.access[type])
1732 break;
1734 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1735 isl_space_copy(space));
1736 if (type < pet_expr_access_end || !expr->acc.index)
1737 return pet_expr_free(expr);
1739 return expr;
1742 /* Add all parameters in "space" to all access relations and index expressions
1743 * in "expr".
1745 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1746 __isl_take isl_space *space)
1748 expr = pet_expr_map_access(expr, &align_params, space);
1749 isl_space_free(space);
1750 return expr;
1753 /* Insert an argument expression corresponding to "test" in front
1754 * of the list of arguments described by *n_arg and *args.
1756 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1757 __isl_keep isl_multi_pw_aff *test)
1759 int i;
1760 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1762 if (!test)
1763 return pet_expr_free(expr);
1764 expr = pet_expr_cow(expr);
1765 if (!expr)
1766 return NULL;
1768 if (!expr->args) {
1769 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1770 if (!expr->args)
1771 return pet_expr_free(expr);
1772 } else {
1773 pet_expr **ext;
1774 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1775 if (!ext)
1776 return pet_expr_free(expr);
1777 for (i = 0; i < expr->n_arg; ++i)
1778 ext[1 + i] = expr->args[i];
1779 free(expr->args);
1780 expr->args = ext;
1782 expr->n_arg++;
1783 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1784 if (!expr->args[0])
1785 return pet_expr_free(expr);
1787 return expr;
1790 /* Make the expression "expr" depend on the value of "test"
1791 * being equal to "satisfied".
1793 * If "test" is an affine expression, we simply add the conditions
1794 * on the expression having the value "satisfied" to all access relations
1795 * (introducing access relations if they are missing) and index expressions.
1797 * Otherwise, we add a filter to "expr" (which is then assumed to be
1798 * an access expression) corresponding to "test" being equal to "satisfied".
1800 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
1801 __isl_take isl_multi_pw_aff *test, int satisfied)
1803 isl_id *id;
1804 isl_ctx *ctx;
1805 isl_space *space;
1806 isl_pw_multi_aff *pma;
1807 enum pet_expr_access_type type;
1809 expr = pet_expr_cow(expr);
1810 if (!expr || !test)
1811 goto error;
1813 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
1814 isl_pw_aff *pa;
1815 isl_set *cond;
1817 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
1818 isl_multi_pw_aff_free(test);
1819 if (satisfied)
1820 cond = isl_pw_aff_non_zero_set(pa);
1821 else
1822 cond = isl_pw_aff_zero_set(pa);
1823 return pet_expr_restrict(expr, cond);
1826 ctx = isl_multi_pw_aff_get_ctx(test);
1827 if (expr->type != pet_expr_access)
1828 isl_die(ctx, isl_error_invalid,
1829 "can only filter access expressions", goto error);
1831 expr = introduce_access_relations(expr);
1832 if (!expr)
1833 goto error;
1835 space = isl_space_domain(isl_multi_pw_aff_get_space(expr->acc.index));
1836 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1837 pma = pet_filter_insert_pma(space, id, satisfied);
1839 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1840 if (!expr->acc.access[type])
1841 continue;
1842 expr->acc.access[type] =
1843 isl_union_map_preimage_domain_pw_multi_aff(
1844 expr->acc.access[type],
1845 isl_pw_multi_aff_copy(pma));
1846 if (!expr->acc.access[type])
1847 break;
1849 pma = isl_pw_multi_aff_gist(pma,
1850 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma)));
1851 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
1852 expr->acc.index, pma);
1853 if (type < pet_expr_access_end || !expr->acc.index)
1854 goto error;
1856 expr = insert_access_arg(expr, test);
1858 isl_multi_pw_aff_free(test);
1859 return expr;
1860 error:
1861 isl_multi_pw_aff_free(test);
1862 return pet_expr_free(expr);
1865 /* Add a reference identifier to access expression "expr".
1866 * "user" points to an integer that contains the sequence number
1867 * of the next reference.
1869 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
1870 void *user)
1872 isl_ctx *ctx;
1873 char name[50];
1874 int *n_ref = user;
1876 expr = pet_expr_cow(expr);
1877 if (!expr)
1878 return expr;
1879 if (expr->type != pet_expr_access)
1880 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1881 "not an access expression", return pet_expr_free(expr));
1883 ctx = pet_expr_get_ctx(expr);
1884 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
1885 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
1886 if (!expr->acc.ref_id)
1887 return pet_expr_free(expr);
1889 return expr;
1892 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
1894 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
1897 /* Reset the user pointer on all parameter and tuple ids in
1898 * the access relations (if any) and the index expression
1899 * of the access expression "expr".
1901 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
1902 void *user)
1904 enum pet_expr_access_type type;
1906 expr = pet_expr_cow(expr);
1907 if (!expr)
1908 return expr;
1909 if (expr->type != pet_expr_access)
1910 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1911 "not an access expression", return pet_expr_free(expr));
1913 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1914 if (!expr->acc.access[type])
1915 continue;
1916 expr->acc.access[type] =
1917 isl_union_map_reset_user(expr->acc.access[type]);
1918 if (!expr->acc.access[type])
1919 break;
1921 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
1922 if (type < pet_expr_access_end || !expr->acc.index)
1923 return pet_expr_free(expr);
1925 return expr;
1928 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
1930 return pet_expr_map_access(expr, &access_anonymize, NULL);
1933 /* Data used in access_gist() callback.
1935 struct pet_access_gist_data {
1936 isl_set *domain;
1937 isl_union_map *value_bounds;
1940 /* Given an expression "expr" of type pet_expr_access, compute
1941 * the gist of the associated access relations (if any) and index expression
1942 * with respect to data->domain and the bounds on the values of the arguments
1943 * of the expression.
1945 * The arguments of "expr" have been gisted right before "expr" itself
1946 * is gisted. The gisted arguments may have become equal where before
1947 * they may not have been (obviously) equal. We therefore take
1948 * the opportunity to remove duplicate arguments here.
1950 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
1952 struct pet_access_gist_data *data = user;
1953 isl_set *domain;
1954 isl_union_set *uset;
1955 enum pet_expr_access_type type;
1957 expr = pet_expr_remove_duplicate_args(expr);
1958 expr = pet_expr_cow(expr);
1959 if (!expr)
1960 return expr;
1961 if (expr->type != pet_expr_access)
1962 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1963 "not an access expression", return pet_expr_free(expr));
1965 domain = isl_set_copy(data->domain);
1966 if (expr->n_arg > 0)
1967 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
1968 data->value_bounds);
1970 uset = isl_union_set_from_set(isl_set_copy(domain));
1971 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1972 if (!expr->acc.access[type])
1973 continue;
1974 expr->acc.access[type] =
1975 isl_union_map_gist_domain(expr->acc.access[type],
1976 isl_union_set_copy(uset));
1977 if (!expr->acc.access[type])
1978 break;
1980 isl_union_set_free(uset);
1981 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
1982 if (type < pet_expr_access_end || !expr->acc.index)
1983 return pet_expr_free(expr);
1985 return expr;
1988 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
1989 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
1991 struct pet_access_gist_data data = { context, value_bounds };
1993 return pet_expr_map_access(expr, &access_gist, &data);
1996 /* Mark "expr" as a read dependening on "read".
1998 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
1999 int read)
2001 if (!expr)
2002 return pet_expr_free(expr);
2003 if (expr->type != pet_expr_access)
2004 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2005 "not an access expression", return pet_expr_free(expr));
2006 if (expr->acc.read == read)
2007 return expr;
2008 expr = pet_expr_cow(expr);
2009 if (!expr)
2010 return NULL;
2011 expr->acc.read = read;
2013 return expr;
2016 /* Mark "expr" as a write dependening on "write".
2018 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
2019 int write)
2021 if (!expr)
2022 return pet_expr_free(expr);
2023 if (expr->type != pet_expr_access)
2024 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2025 "not an access expression", return pet_expr_free(expr));
2026 if (expr->acc.write == write)
2027 return expr;
2028 expr = pet_expr_cow(expr);
2029 if (!expr)
2030 return NULL;
2031 expr->acc.write = write;
2033 return expr;
2036 /* Mark "expr" as a kill dependening on "kill".
2038 __isl_give pet_expr *pet_expr_access_set_kill(__isl_take pet_expr *expr,
2039 int kill)
2041 if (!expr)
2042 return pet_expr_free(expr);
2043 if (expr->type != pet_expr_access)
2044 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2045 "not an access expression", return pet_expr_free(expr));
2046 if (expr->acc.kill == kill)
2047 return expr;
2048 expr = pet_expr_cow(expr);
2049 if (!expr)
2050 return NULL;
2051 expr->acc.kill = kill;
2053 return expr;
2056 /* Map the access type "type" to the corresponding location
2057 * in the access array.
2058 * In particular, the access relation of type pet_expr_access_killed is
2059 * stored in the element at position pet_expr_access_fake_killed.
2061 static enum pet_expr_access_type internalize_type(
2062 enum pet_expr_access_type type)
2064 if (type == pet_expr_access_killed)
2065 return pet_expr_access_fake_killed;
2066 return type;
2069 /* Replace the access relation of the given "type" of "expr" by "access".
2070 * If the access relation is non-empty and the type is a read or a write,
2071 * then also mark the access expression itself as a read or a write.
2073 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
2074 enum pet_expr_access_type type, __isl_take isl_union_map *access)
2076 int empty;
2078 expr = pet_expr_cow(expr);
2079 if (!expr || !access)
2080 goto error;
2081 if (expr->type != pet_expr_access)
2082 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2083 "not an access expression", goto error);
2084 type = internalize_type(type);
2085 isl_union_map_free(expr->acc.access[type]);
2086 expr->acc.access[type] = access;
2088 if (expr->acc.kill)
2089 return expr;
2091 empty = isl_union_map_is_empty(access);
2092 if (empty < 0)
2093 return pet_expr_free(expr);
2094 if (empty)
2095 return expr;
2097 if (type == pet_expr_access_may_read)
2098 expr = pet_expr_access_set_read(expr, 1);
2099 else
2100 expr = pet_expr_access_set_write(expr, 1);
2102 return expr;
2103 error:
2104 isl_union_map_free(access);
2105 pet_expr_free(expr);
2106 return NULL;
2109 /* Replace the index expression of "expr" by "index" and
2110 * set the array depth accordingly.
2112 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
2113 __isl_take isl_multi_pw_aff *index)
2115 expr = pet_expr_cow(expr);
2116 if (!expr || !index)
2117 goto error;
2118 if (expr->type != pet_expr_access)
2119 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2120 "not an access expression", goto error);
2121 isl_multi_pw_aff_free(expr->acc.index);
2122 expr->acc.index = index;
2123 expr->acc.depth = isl_multi_pw_aff_dim(index, isl_dim_out);
2125 return expr;
2126 error:
2127 isl_multi_pw_aff_free(index);
2128 pet_expr_free(expr);
2129 return NULL;
2132 /* Return the reference identifier of access expression "expr".
2134 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
2136 if (!expr)
2137 return NULL;
2138 if (expr->type != pet_expr_access)
2139 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2140 "not an access expression", return NULL);
2142 return isl_id_copy(expr->acc.ref_id);
2145 /* Replace the reference identifier of access expression "expr" by "ref_id".
2147 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
2148 __isl_take isl_id *ref_id)
2150 expr = pet_expr_cow(expr);
2151 if (!expr || !ref_id)
2152 goto error;
2153 if (expr->type != pet_expr_access)
2154 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2155 "not an access expression", goto error);
2156 isl_id_free(expr->acc.ref_id);
2157 expr->acc.ref_id = ref_id;
2159 return expr;
2160 error:
2161 isl_id_free(ref_id);
2162 pet_expr_free(expr);
2163 return NULL;
2166 /* Tag the access relation "access" with "id".
2167 * That is, insert the id as the range of a wrapped relation
2168 * in the domain of "access".
2170 * If "access" is of the form
2172 * D[i] -> A[a]
2174 * then the result is of the form
2176 * [D[i] -> id[]] -> A[a]
2178 __isl_give isl_union_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
2179 __isl_take isl_union_map *access)
2181 isl_space *space;
2182 isl_multi_aff *add_tag;
2183 isl_id *id;
2185 if (expr->type != pet_expr_access)
2186 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2187 "not an access expression",
2188 return isl_union_map_free(access));
2190 id = isl_id_copy(expr->acc.ref_id);
2191 space = pet_expr_access_get_domain_space(expr);
2192 space = isl_space_from_domain(space);
2193 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2194 add_tag = isl_multi_aff_domain_map(space);
2195 access = isl_union_map_preimage_domain_multi_aff(access, add_tag);
2197 return access;
2200 /* Return the access relation of the given "type" associated to "expr"
2201 * that maps pairs of domain iterations and argument values
2202 * to the corresponding accessed data elements.
2204 * If the requested access relation is explicitly available,
2205 * then return a copy. Otherwise, check if it is irrelevant for
2206 * the access expression and return an empty relation if this is the case.
2207 * Otherwise, introduce the requested access relation in "expr" and
2208 * return a copy.
2210 __isl_give isl_union_map *pet_expr_access_get_dependent_access(
2211 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2213 isl_union_map *access;
2214 int empty;
2216 if (!expr)
2217 return NULL;
2218 if (expr->type != pet_expr_access)
2219 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2220 "not an access expression", return NULL);
2222 type = internalize_type(type);
2223 if (expr->acc.access[type])
2224 return isl_union_map_copy(expr->acc.access[type]);
2226 if (type == pet_expr_access_may_read)
2227 empty = !expr->acc.read;
2228 else
2229 empty = !expr->acc.write;
2231 if (!empty) {
2232 expr = pet_expr_copy(expr);
2233 expr = introduce_access_relations(expr);
2234 if (!expr)
2235 return NULL;
2236 access = isl_union_map_copy(expr->acc.access[type]);
2237 pet_expr_free(expr);
2239 return access;
2242 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr));
2245 /* Return the may read access relation associated to "expr"
2246 * that maps pairs of domain iterations and argument values
2247 * to the corresponding accessed data elements.
2249 __isl_give isl_union_map *pet_expr_access_get_dependent_may_read(
2250 __isl_keep pet_expr *expr)
2252 return pet_expr_access_get_dependent_access(expr,
2253 pet_expr_access_may_read);
2256 /* Return the may write access relation associated to "expr"
2257 * that maps pairs of domain iterations and argument values
2258 * to the corresponding accessed data elements.
2260 __isl_give isl_union_map *pet_expr_access_get_dependent_may_write(
2261 __isl_keep pet_expr *expr)
2263 return pet_expr_access_get_dependent_access(expr,
2264 pet_expr_access_may_write);
2267 /* Return the must write access relation associated to "expr"
2268 * that maps pairs of domain iterations and argument values
2269 * to the corresponding accessed data elements.
2271 __isl_give isl_union_map *pet_expr_access_get_dependent_must_write(
2272 __isl_keep pet_expr *expr)
2274 return pet_expr_access_get_dependent_access(expr,
2275 pet_expr_access_must_write);
2278 /* Return the relation of the given "type" mapping domain iterations
2279 * to the accessed data elements.
2280 * In particular, take the access relation and, in case of may_read
2281 * or may_write, project out the values of the arguments, if any.
2282 * In case of must_write, return the empty relation if there are
2283 * any arguments.
2285 __isl_give isl_union_map *pet_expr_access_get_access(__isl_keep pet_expr *expr,
2286 enum pet_expr_access_type type)
2288 isl_union_map *access;
2289 isl_space *space;
2290 isl_map *map;
2292 if (!expr)
2293 return NULL;
2294 if (expr->type != pet_expr_access)
2295 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2296 "not an access expression", return NULL);
2298 if (expr->n_arg != 0 && type == pet_expr_access_must_write) {
2299 space = pet_expr_access_get_parameter_space(expr);
2300 return isl_union_map_empty(space);
2303 access = pet_expr_access_get_dependent_access(expr, type);
2304 if (expr->n_arg == 0)
2305 return access;
2307 space = isl_multi_pw_aff_get_space(expr->acc.index);
2308 space = isl_space_domain(space);
2309 map = isl_map_universe(isl_space_unwrap(space));
2310 map = isl_map_domain_map(map);
2311 access = isl_union_map_apply_domain(access,
2312 isl_union_map_from_map(map));
2314 return access;
2317 /* Return the relation mapping domain iterations to all possibly
2318 * read data elements.
2320 __isl_give isl_union_map *pet_expr_access_get_may_read(
2321 __isl_keep pet_expr *expr)
2323 return pet_expr_access_get_access(expr, pet_expr_access_may_read);
2326 /* Return the relation mapping domain iterations to all possibly
2327 * written data elements.
2329 __isl_give isl_union_map *pet_expr_access_get_may_write(
2330 __isl_keep pet_expr *expr)
2332 return pet_expr_access_get_access(expr, pet_expr_access_may_write);
2335 /* Return a relation mapping domain iterations to definitely
2336 * written data elements, assuming the statement containing
2337 * the expression is executed.
2339 __isl_give isl_union_map *pet_expr_access_get_must_write(
2340 __isl_keep pet_expr *expr)
2342 return pet_expr_access_get_access(expr, pet_expr_access_must_write);
2345 /* Return the relation of the given "type" mapping domain iterations to
2346 * accessed data elements, with its domain tagged with the reference
2347 * identifier.
2349 static __isl_give isl_union_map *pet_expr_access_get_tagged_access(
2350 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2352 isl_union_map *access;
2354 if (!expr)
2355 return NULL;
2357 access = pet_expr_access_get_access(expr, type);
2358 access = pet_expr_tag_access(expr, access);
2360 return access;
2363 /* Return the relation mapping domain iterations to all possibly
2364 * read data elements, with its domain tagged with the reference
2365 * identifier.
2367 __isl_give isl_union_map *pet_expr_access_get_tagged_may_read(
2368 __isl_keep pet_expr *expr)
2370 return pet_expr_access_get_tagged_access(expr,
2371 pet_expr_access_may_read);
2374 /* Return the relation mapping domain iterations to all possibly
2375 * written data elements, with its domain tagged with the reference
2376 * identifier.
2378 __isl_give isl_union_map *pet_expr_access_get_tagged_may_write(
2379 __isl_keep pet_expr *expr)
2381 return pet_expr_access_get_tagged_access(expr,
2382 pet_expr_access_may_write);
2385 /* Return the operation type of operation expression "expr".
2387 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
2389 if (!expr)
2390 return pet_op_last;
2391 if (expr->type != pet_expr_op)
2392 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2393 "not an operation expression", return pet_op_last);
2395 return expr->op;
2398 /* Replace the operation type of operation expression "expr" by "type".
2400 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
2401 enum pet_op_type type)
2403 if (!expr)
2404 return pet_expr_free(expr);
2405 if (expr->type != pet_expr_op)
2406 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2407 "not an operation expression",
2408 return pet_expr_free(expr));
2409 if (expr->op == type)
2410 return expr;
2411 expr = pet_expr_cow(expr);
2412 if (!expr)
2413 return NULL;
2414 expr->op = type;
2416 return expr;
2419 /* Return the name of the function called by "expr".
2421 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
2423 if (!expr)
2424 return NULL;
2425 if (expr->type != pet_expr_call)
2426 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2427 "not a call expression", return NULL);
2428 return expr->c.name;
2431 /* Replace the name of the function called by "expr" by "name".
2433 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
2434 __isl_keep const char *name)
2436 expr = pet_expr_cow(expr);
2437 if (!expr || !name)
2438 return pet_expr_free(expr);
2439 if (expr->type != pet_expr_call)
2440 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2441 "not a call expression", return pet_expr_free(expr));
2442 free(expr->c.name);
2443 expr->c.name = strdup(name);
2444 if (!expr->c.name)
2445 return pet_expr_free(expr);
2446 return expr;
2449 /* Does the call expression "expr" have an associated function summary?
2451 int pet_expr_call_has_summary(__isl_keep pet_expr *expr)
2453 if (!expr)
2454 return -1;
2455 if (expr->type != pet_expr_call)
2456 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2457 "not a call expression", return -1);
2459 return expr->c.summary != NULL;
2462 /* Return a copy of the function summary associated to
2463 * the call expression "expr".
2465 __isl_give pet_function_summary *pet_expr_call_get_summary(
2466 __isl_keep pet_expr *expr)
2468 if (!expr)
2469 return NULL;
2470 if (expr->type != pet_expr_call)
2471 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2472 "not a call expression", return NULL);
2474 return pet_function_summary_copy(expr->c.summary);
2477 /* Replace the function summary associated to the call expression "expr"
2478 * by "summary".
2480 __isl_give pet_expr *pet_expr_call_set_summary(__isl_take pet_expr *expr,
2481 __isl_take pet_function_summary *summary)
2483 expr = pet_expr_cow(expr);
2484 if (!expr || !summary)
2485 goto error;
2486 if (expr->type != pet_expr_call)
2487 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2488 "not a call expression", goto error);
2489 pet_function_summary_free(expr->c.summary);
2490 expr->c.summary = summary;
2491 return expr;
2492 error:
2493 pet_function_summary_free(summary);
2494 return pet_expr_free(expr);
2497 /* Replace the type of the cast performed by "expr" by "name".
2499 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
2500 __isl_keep const char *name)
2502 expr = pet_expr_cow(expr);
2503 if (!expr || !name)
2504 return pet_expr_free(expr);
2505 if (expr->type != pet_expr_cast)
2506 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2507 "not a cast expression", return pet_expr_free(expr));
2508 free(expr->type_name);
2509 expr->type_name = strdup(name);
2510 if (!expr->type_name)
2511 return pet_expr_free(expr);
2512 return expr;
2515 /* Return the value of the integer represented by "expr".
2517 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
2519 if (!expr)
2520 return NULL;
2521 if (expr->type != pet_expr_int)
2522 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2523 "not an int expression", return NULL);
2525 return isl_val_copy(expr->i);
2528 /* Replace the value of the integer represented by "expr" by "v".
2530 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
2531 __isl_take isl_val *v)
2533 expr = pet_expr_cow(expr);
2534 if (!expr || !v)
2535 goto error;
2536 if (expr->type != pet_expr_int)
2537 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2538 "not an int expression", goto error);
2539 isl_val_free(expr->i);
2540 expr->i = v;
2542 return expr;
2543 error:
2544 isl_val_free(v);
2545 pet_expr_free(expr);
2546 return NULL;
2549 /* Replace the value and string representation of the double
2550 * represented by "expr" by "d" and "s".
2552 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2553 double d, __isl_keep const char *s)
2555 expr = pet_expr_cow(expr);
2556 if (!expr || !s)
2557 return pet_expr_free(expr);
2558 if (expr->type != pet_expr_double)
2559 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2560 "not a double expression", return pet_expr_free(expr));
2561 expr->d.val = d;
2562 free(expr->d.s);
2563 expr->d.s = strdup(s);
2564 if (!expr->d.s)
2565 return pet_expr_free(expr);
2566 return expr;
2569 /* Return a string representation of the double expression "expr".
2571 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2573 if (!expr)
2574 return NULL;
2575 if (expr->type != pet_expr_double)
2576 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2577 "not a double expression", return NULL);
2578 return strdup(expr->d.s);
2581 /* Return a piecewise affine expression defined on the specified domain
2582 * that represents NaN.
2584 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2586 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2589 /* This function is called when we come across an access that is
2590 * nested in what is supposed to be an affine expression.
2591 * "pc" is the context in which the affine expression is created.
2592 * If nesting is allowed in "pc", we return an affine expression that is
2593 * equal to a new parameter corresponding to this nested access.
2594 * Otherwise, we return NaN.
2596 * Note that we currently don't allow nested accesses themselves
2597 * to contain any nested accesses, so we check if "expr" itself
2598 * involves any nested accesses (either explicitly as arguments
2599 * or implicitly through parameters) and return NaN if it does.
2601 * The new parameter is resolved in resolve_nested.
2603 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2604 __isl_keep pet_context *pc)
2606 isl_ctx *ctx;
2607 isl_id *id;
2608 isl_space *space;
2609 isl_local_space *ls;
2610 isl_aff *aff;
2611 int nested;
2613 if (!expr || !pc)
2614 return NULL;
2615 if (!pet_context_allow_nesting(pc))
2616 return non_affine(pet_context_get_space(pc));
2618 if (pet_expr_get_type(expr) != pet_expr_access)
2619 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2620 "not an access expression", return NULL);
2622 if (expr->n_arg > 0)
2623 return non_affine(pet_context_get_space(pc));
2625 space = pet_expr_access_get_parameter_space(expr);
2626 nested = pet_nested_any_in_space(space);
2627 isl_space_free(space);
2628 if (nested)
2629 return non_affine(pet_context_get_space(pc));
2631 ctx = pet_expr_get_ctx(expr);
2632 id = pet_nested_pet_expr(pet_expr_copy(expr));
2633 space = pet_context_get_space(pc);
2634 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2636 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2637 ls = isl_local_space_from_space(space);
2638 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2640 return isl_pw_aff_from_aff(aff);
2643 /* Extract an affine expression from the access pet_expr "expr".
2644 * "pc" is the context in which the affine expression is created.
2646 * If "expr" is actually an affine expression rather than
2647 * a real access, then we return that expression.
2648 * Otherwise, we require that "expr" is of an integral type.
2649 * If not, we return NaN.
2651 * If the variable has been assigned a known affine expression,
2652 * then we return that expression.
2654 * Otherwise, we return an expression that is equal to a parameter
2655 * representing "expr" (if "allow_nested" is set).
2657 static __isl_give isl_pw_aff *extract_affine_from_access(
2658 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2660 int pos;
2661 isl_id *id;
2663 if (pet_expr_is_affine(expr)) {
2664 isl_pw_aff *pa;
2665 isl_multi_pw_aff *mpa;
2667 mpa = pet_expr_access_get_index(expr);
2668 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
2669 isl_multi_pw_aff_free(mpa);
2670 return pa;
2673 if (pet_expr_get_type_size(expr) == 0)
2674 return non_affine(pet_context_get_space(pc));
2676 if (!pet_expr_is_scalar_access(expr))
2677 return nested_access(expr, pc);
2679 id = pet_expr_access_get_id(expr);
2680 if (pet_context_is_assigned(pc, id))
2681 return pet_context_get_value(pc, id);
2683 isl_id_free(id);
2684 return nested_access(expr, pc);
2687 /* Construct an affine expression from the integer constant "expr".
2688 * "pc" is the context in which the affine expression is created.
2690 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2691 __isl_keep pet_context *pc)
2693 isl_local_space *ls;
2694 isl_aff *aff;
2696 if (!expr)
2697 return NULL;
2699 ls = isl_local_space_from_space(pet_context_get_space(pc));
2700 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2702 return isl_pw_aff_from_aff(aff);
2705 /* Extract an affine expression from an addition or subtraction operation.
2706 * Return NaN if we are unable to extract an affine expression.
2708 * "pc" is the context in which the affine expression is created.
2710 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2711 __isl_keep pet_context *pc)
2713 isl_pw_aff *lhs;
2714 isl_pw_aff *rhs;
2716 if (!expr)
2717 return NULL;
2718 if (expr->n_arg != 2)
2719 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2720 "expecting two arguments", return NULL);
2722 lhs = pet_expr_extract_affine(expr->args[0], pc);
2723 rhs = pet_expr_extract_affine(expr->args[1], pc);
2725 switch (pet_expr_op_get_type(expr)) {
2726 case pet_op_add:
2727 return isl_pw_aff_add(lhs, rhs);
2728 case pet_op_sub:
2729 return isl_pw_aff_sub(lhs, rhs);
2730 default:
2731 isl_pw_aff_free(lhs);
2732 isl_pw_aff_free(rhs);
2733 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2734 "not an addition or subtraction operation",
2735 return NULL);
2740 /* Extract an affine expression from an integer division or a modulo operation.
2741 * Return NaN if we are unable to extract an affine expression.
2743 * "pc" is the context in which the affine expression is created.
2745 * In particular, if "expr" is lhs/rhs, then return
2747 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2749 * If "expr" is lhs%rhs, then return
2751 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2753 * If the second argument (rhs) is not a (positive) integer constant,
2754 * then we fail to extract an affine expression.
2756 * We simplify the result in the context of the domain of "pc" in case
2757 * this domain implies that lhs >= 0 (or < 0).
2759 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2760 __isl_keep pet_context *pc)
2762 int is_cst;
2763 isl_pw_aff *lhs;
2764 isl_pw_aff *rhs;
2765 isl_pw_aff *res;
2767 if (!expr)
2768 return NULL;
2769 if (expr->n_arg != 2)
2770 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2771 "expecting two arguments", return NULL);
2773 rhs = pet_expr_extract_affine(expr->args[1], pc);
2775 is_cst = isl_pw_aff_is_cst(rhs);
2776 if (is_cst < 0 || !is_cst) {
2777 isl_pw_aff_free(rhs);
2778 return non_affine(pet_context_get_space(pc));
2781 lhs = pet_expr_extract_affine(expr->args[0], pc);
2783 switch (pet_expr_op_get_type(expr)) {
2784 case pet_op_div:
2785 res = isl_pw_aff_tdiv_q(lhs, rhs);
2786 break;
2787 case pet_op_mod:
2788 res = isl_pw_aff_tdiv_r(lhs, rhs);
2789 break;
2790 default:
2791 isl_pw_aff_free(lhs);
2792 isl_pw_aff_free(rhs);
2793 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2794 "not a div or mod operator", return NULL);
2797 return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc));
2800 /* Extract an affine expression from a multiplication operation.
2801 * Return NaN if we are unable to extract an affine expression.
2802 * In particular, if neither of the arguments is a (piecewise) constant
2803 * then we return NaN.
2805 * "pc" is the context in which the affine expression is created.
2807 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2808 __isl_keep pet_context *pc)
2810 int lhs_cst, rhs_cst;
2811 isl_pw_aff *lhs;
2812 isl_pw_aff *rhs;
2814 if (!expr)
2815 return NULL;
2816 if (expr->n_arg != 2)
2817 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2818 "expecting two arguments", return NULL);
2820 lhs = pet_expr_extract_affine(expr->args[0], pc);
2821 rhs = pet_expr_extract_affine(expr->args[1], pc);
2823 lhs_cst = isl_pw_aff_is_cst(lhs);
2824 rhs_cst = isl_pw_aff_is_cst(rhs);
2825 if (lhs_cst >= 0 && rhs_cst >= 0 && (lhs_cst || rhs_cst))
2826 return isl_pw_aff_mul(lhs, rhs);
2828 isl_pw_aff_free(lhs);
2829 isl_pw_aff_free(rhs);
2831 if (lhs_cst < 0 || rhs_cst < 0)
2832 return NULL;
2834 return non_affine(pet_context_get_space(pc));
2837 /* Extract an affine expression from a negation operation.
2838 * Return NaN if we are unable to extract an affine expression.
2840 * "pc" is the context in which the affine expression is created.
2842 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
2843 __isl_keep pet_context *pc)
2845 isl_pw_aff *res;
2847 if (!expr)
2848 return NULL;
2849 if (expr->n_arg != 1)
2850 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2851 "expecting one argument", return NULL);
2853 res = pet_expr_extract_affine(expr->args[0], pc);
2854 return isl_pw_aff_neg(res);
2857 /* Extract an affine expression from a conditional operation.
2858 * Return NaN if we are unable to extract an affine expression.
2860 * "pc" is the context in which the affine expression is created.
2862 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
2863 __isl_keep pet_context *pc)
2865 isl_pw_aff *cond, *lhs, *rhs;
2867 if (!expr)
2868 return NULL;
2869 if (expr->n_arg != 3)
2870 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2871 "expecting three arguments", return NULL);
2873 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
2874 lhs = pet_expr_extract_affine(expr->args[1], pc);
2875 rhs = pet_expr_extract_affine(expr->args[2], pc);
2877 return isl_pw_aff_cond(cond, lhs, rhs);
2880 /* Compute
2882 * pwaff mod 2^width
2884 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff, unsigned width)
2886 isl_ctx *ctx;
2887 isl_val *mod;
2889 ctx = isl_pw_aff_get_ctx(pwaff);
2890 mod = isl_val_int_from_ui(ctx, width);
2891 mod = isl_val_2exp(mod);
2893 pwaff = isl_pw_aff_mod_val(pwaff, mod);
2895 return pwaff;
2898 /* Limit the domain of "pwaff" to those elements where the function
2899 * value satisfies
2901 * 2^{width-1} <= pwaff < 2^{width-1}
2903 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
2904 unsigned width)
2906 isl_ctx *ctx;
2907 isl_val *v;
2908 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
2909 isl_local_space *ls = isl_local_space_from_space(space);
2910 isl_aff *bound;
2911 isl_set *dom;
2912 isl_pw_aff *b;
2914 ctx = isl_pw_aff_get_ctx(pwaff);
2915 v = isl_val_int_from_ui(ctx, width - 1);
2916 v = isl_val_2exp(v);
2918 bound = isl_aff_zero_on_domain(ls);
2919 bound = isl_aff_add_constant_val(bound, v);
2920 b = isl_pw_aff_from_aff(bound);
2922 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
2923 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2925 b = isl_pw_aff_neg(b);
2926 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
2927 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2929 return pwaff;
2932 /* Handle potential overflows on signed computations.
2934 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
2935 * then we adjust the domain of "pa" to avoid overflows.
2937 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
2938 unsigned width)
2940 isl_ctx *ctx;
2941 struct pet_options *options;
2943 if (!pa)
2944 return NULL;
2946 ctx = isl_pw_aff_get_ctx(pa);
2947 options = isl_ctx_peek_pet_options(ctx);
2948 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
2949 pa = avoid_overflow(pa, width);
2951 return pa;
2954 /* Extract an affine expression from some an operation.
2955 * Return NaN if we are unable to extract an affine expression.
2956 * If the result of a binary (non boolean) operation is unsigned,
2957 * then we wrap it based on the size of the type. If the result is signed,
2958 * then we ensure that no overflow occurs.
2960 * "pc" is the context in which the affine expression is created.
2962 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
2963 __isl_keep pet_context *pc)
2965 isl_pw_aff *res;
2966 int type_size;
2968 switch (pet_expr_op_get_type(expr)) {
2969 case pet_op_add:
2970 case pet_op_sub:
2971 res = extract_affine_add_sub(expr, pc);
2972 break;
2973 case pet_op_div:
2974 case pet_op_mod:
2975 res = extract_affine_div_mod(expr, pc);
2976 break;
2977 case pet_op_mul:
2978 res = extract_affine_mul(expr, pc);
2979 break;
2980 case pet_op_minus:
2981 return extract_affine_neg(expr, pc);
2982 case pet_op_cond:
2983 return extract_affine_cond(expr, pc);
2984 case pet_op_eq:
2985 case pet_op_ne:
2986 case pet_op_le:
2987 case pet_op_ge:
2988 case pet_op_lt:
2989 case pet_op_gt:
2990 case pet_op_land:
2991 case pet_op_lor:
2992 case pet_op_lnot:
2993 return pet_expr_extract_affine_condition(expr, pc);
2994 default:
2995 return non_affine(pet_context_get_space(pc));
2998 if (!res)
2999 return NULL;
3000 if (isl_pw_aff_involves_nan(res)) {
3001 isl_space *space = isl_pw_aff_get_domain_space(res);
3002 isl_pw_aff_free(res);
3003 return non_affine(space);
3006 type_size = pet_expr_get_type_size(expr);
3007 if (type_size > 0)
3008 res = wrap(res, type_size);
3009 else
3010 res = signed_overflow(res, -type_size);
3012 return res;
3015 /* Internal data structure for affine builtin function declarations.
3017 * "pencil" is set if the builtin is pencil specific.
3018 * "n_args" is the number of arguments the function takes.
3019 * "name" is the function name.
3021 struct affine_builtin_decl {
3022 int pencil;
3023 int n_args;
3024 const char *name;
3027 static struct affine_builtin_decl affine_builtins[] = {
3028 { 0, 2, "min" },
3029 { 1, 2, "imin" },
3030 { 1, 2, "umin" },
3031 { 0, 2, "max" },
3032 { 1, 2, "imax" },
3033 { 1, 2, "umax" },
3034 { 0, 2, "intMod" },
3035 { 0, 2, "intFloor" },
3036 { 0, 2, "intCeil" },
3037 { 0, 2, "floord" },
3038 { 0, 2, "ceild" }
3041 /* List of min and max builtin functions.
3043 static const char *min_max_builtins[] = {
3044 "min", "imin", "umin",
3045 "max", "imax", "umax"
3048 /* Is a function call to "name" with "n_args" arguments a call to a
3049 * builtin function for which we can construct an affine expression?
3050 * pencil specific builtins are only recognized if "pencil" is set.
3052 static int is_affine_builtin(int pencil, int n_args, const char *name)
3054 int i;
3056 for (i = 0; i < ARRAY_SIZE(affine_builtins); ++i) {
3057 struct affine_builtin_decl *decl = &affine_builtins[i];
3059 if (decl->pencil && !pencil)
3060 continue;
3061 if (decl->n_args == n_args && !strcmp(decl->name, name))
3062 return 1;
3065 return 0;
3068 /* Is function "name" a known min or max builtin function?
3070 static int is_min_or_max_builtin(const char *name)
3072 int i;
3074 for (i = 0; i < ARRAY_SIZE(min_max_builtins); ++i)
3075 if (!strcmp(min_max_builtins[i], name))
3076 return 1;
3078 return 0;
3081 /* Extract an affine expression from some special function calls.
3082 * Return NaN if we are unable to extract an affine expression.
3083 * In particular, we handle "min", "max", "ceild", "floord",
3084 * "intMod", "intFloor" and "intCeil".
3085 * In case of the latter five, the second argument needs to be
3086 * a (positive) integer constant.
3087 * If the pencil option is set, then we also handle "{i,u}min" and
3088 * "{i,u}max".
3090 * "pc" is the context in which the affine expression is created.
3092 static __isl_give isl_pw_aff *extract_affine_from_call(
3093 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3095 isl_ctx *ctx;
3096 isl_pw_aff *aff1, *aff2;
3097 int n;
3098 const char *name;
3099 struct pet_options *options;
3101 if (!expr)
3102 return NULL;
3103 ctx = pet_expr_get_ctx(expr);
3104 options = isl_ctx_peek_pet_options(ctx);
3106 n = pet_expr_get_n_arg(expr);
3107 name = pet_expr_call_get_name(expr);
3108 if (!is_affine_builtin(options->pencil, n, name))
3109 return non_affine(pet_context_get_space(pc));
3111 if (is_min_or_max_builtin(name)) {
3112 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3113 aff2 = pet_expr_extract_affine(expr->args[1], pc);
3115 if (strstr(name, "min"))
3116 aff1 = isl_pw_aff_min(aff1, aff2);
3117 else
3118 aff1 = isl_pw_aff_max(aff1, aff2);
3119 } else if (!strcmp(name, "intMod")) {
3120 isl_val *v;
3122 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3123 return non_affine(pet_context_get_space(pc));
3124 v = pet_expr_int_get_val(expr->args[1]);
3125 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3126 aff1 = isl_pw_aff_mod_val(aff1, v);
3127 } else {
3128 isl_val *v;
3130 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3131 return non_affine(pet_context_get_space(pc));
3132 v = pet_expr_int_get_val(expr->args[1]);
3133 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3134 aff1 = isl_pw_aff_scale_down_val(aff1, v);
3135 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
3136 aff1 = isl_pw_aff_floor(aff1);
3137 else
3138 aff1 = isl_pw_aff_ceil(aff1);
3141 return aff1;
3144 /* Extract an affine expression from "expr", if possible.
3145 * Otherwise return NaN.
3147 * "pc" is the context in which the affine expression is created.
3149 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
3150 __isl_keep pet_context *pc)
3152 if (!expr)
3153 return NULL;
3155 switch (pet_expr_get_type(expr)) {
3156 case pet_expr_access:
3157 return extract_affine_from_access(expr, pc);
3158 case pet_expr_int:
3159 return extract_affine_from_int(expr, pc);
3160 case pet_expr_op:
3161 return extract_affine_from_op(expr, pc);
3162 case pet_expr_call:
3163 return extract_affine_from_call(expr, pc);
3164 case pet_expr_cast:
3165 case pet_expr_double:
3166 case pet_expr_error:
3167 return non_affine(pet_context_get_space(pc));
3171 /* Extract an affine expressions representing the comparison "LHS op RHS"
3172 * Return NaN if we are unable to extract such an affine expression.
3174 * "pc" is the context in which the affine expression is created.
3176 * If the comparison is of the form
3178 * a <= min(b,c)
3180 * then the expression is constructed as the conjunction of
3181 * the comparisons
3183 * a <= b and a <= c
3185 * A similar optimization is performed for max(a,b) <= c.
3186 * We do this because that will lead to simpler representations
3187 * of the expression.
3188 * If isl is ever enhanced to explicitly deal with min and max expressions,
3189 * this optimization can be removed.
3191 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
3192 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
3193 __isl_keep pet_context *pc)
3195 isl_pw_aff *lhs_pa, *rhs_pa;
3197 if (op == pet_op_gt)
3198 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
3199 if (op == pet_op_ge)
3200 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
3202 if (op == pet_op_lt || op == pet_op_le) {
3203 if (pet_expr_is_min(rhs)) {
3204 lhs_pa = pet_expr_extract_comparison(op, lhs,
3205 rhs->args[0], pc);
3206 rhs_pa = pet_expr_extract_comparison(op, lhs,
3207 rhs->args[1], pc);
3208 return pet_and(lhs_pa, rhs_pa);
3210 if (pet_expr_is_max(lhs)) {
3211 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
3212 rhs, pc);
3213 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
3214 rhs, pc);
3215 return pet_and(lhs_pa, rhs_pa);
3219 lhs_pa = pet_expr_extract_affine(lhs, pc);
3220 rhs_pa = pet_expr_extract_affine(rhs, pc);
3222 return pet_comparison(op, lhs_pa, rhs_pa);
3225 /* Extract an affine expressions from the comparison "expr".
3226 * Return NaN if we are unable to extract such an affine expression.
3228 * "pc" is the context in which the affine expression is created.
3230 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
3231 __isl_keep pet_context *pc)
3233 enum pet_op_type type;
3235 if (!expr)
3236 return NULL;
3237 if (expr->n_arg != 2)
3238 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3239 "expecting two arguments", return NULL);
3241 type = pet_expr_op_get_type(expr);
3242 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
3243 pc);
3246 /* Extract an affine expression representing the boolean operation
3247 * expressed by "expr".
3248 * Return NaN if we are unable to extract an affine expression.
3250 * "pc" is the context in which the affine expression is created.
3252 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
3253 __isl_keep pet_context *pc)
3255 isl_pw_aff *lhs, *rhs;
3256 int n;
3258 if (!expr)
3259 return NULL;
3261 n = pet_expr_get_n_arg(expr);
3262 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
3263 if (n == 1)
3264 return pet_not(lhs);
3266 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
3267 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
3270 /* Extract the affine expression "expr != 0 ? 1 : 0".
3271 * Return NaN if we are unable to extract an affine expression.
3273 * "pc" is the context in which the affine expression is created.
3275 static __isl_give isl_pw_aff *extract_implicit_condition(
3276 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3278 isl_pw_aff *res;
3280 res = pet_expr_extract_affine(expr, pc);
3281 return pet_to_bool(res);
3284 /* Extract a boolean affine expression from "expr".
3285 * Return NaN if we are unable to extract an affine expression.
3287 * "pc" is the context in which the affine expression is created.
3289 * If "expr" is neither a comparison nor a boolean operation,
3290 * then we assume it is an affine expression and return the
3291 * boolean expression "expr != 0 ? 1 : 0".
3293 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
3294 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3296 if (!expr)
3297 return NULL;
3299 if (pet_expr_is_comparison(expr))
3300 return extract_comparison(expr, pc);
3301 if (pet_expr_is_boolean(expr))
3302 return extract_boolean(expr, pc);
3304 return extract_implicit_condition(expr, pc);
3307 /* Check if "expr" is an assume expression and if its single argument
3308 * can be converted to an affine expression in the context of "pc".
3309 * If so, replace the argument by the affine expression.
3311 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
3312 __isl_keep pet_context *pc)
3314 isl_pw_aff *cond;
3315 isl_multi_pw_aff *index;
3317 if (!expr)
3318 return NULL;
3319 if (!pet_expr_is_assume(expr))
3320 return expr;
3321 if (expr->n_arg != 1)
3322 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3323 "expecting one argument", return pet_expr_free(expr));
3325 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3326 if (!cond)
3327 return pet_expr_free(expr);
3328 if (isl_pw_aff_involves_nan(cond)) {
3329 isl_pw_aff_free(cond);
3330 return expr;
3333 index = isl_multi_pw_aff_from_pw_aff(cond);
3334 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
3336 return expr;
3339 /* Return the number of bits needed to represent the type of "expr".
3340 * See the description of the type_size field of pet_expr.
3342 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
3344 return expr ? expr->type_size : 0;
3347 /* Replace the number of bits needed to represent the type of "expr"
3348 * by "type_size".
3349 * See the description of the type_size field of pet_expr.
3351 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
3352 int type_size)
3354 expr = pet_expr_cow(expr);
3355 if (!expr)
3356 return NULL;
3358 expr->type_size = type_size;
3360 return expr;
3363 /* Extend an access expression "expr" with an additional index "index".
3364 * In particular, add "index" as an extra argument to "expr" and
3365 * adjust the index expression of "expr" to refer to this extra argument.
3366 * The caller is responsible for calling pet_expr_access_set_depth
3367 * to update the corresponding access relation.
3369 * Note that we only collect the individual index expressions as
3370 * arguments of "expr" here.
3371 * An attempt to integrate them into the index expression of "expr"
3372 * is performed in pet_expr_access_plug_in_args.
3374 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
3375 __isl_take pet_expr *index)
3377 int n;
3378 isl_space *space;
3379 isl_local_space *ls;
3380 isl_pw_aff *pa;
3382 expr = pet_expr_cow(expr);
3383 if (!expr || !index)
3384 goto error;
3385 if (expr->type != pet_expr_access)
3386 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3387 "not an access pet_expr", goto error);
3389 n = pet_expr_get_n_arg(expr);
3390 expr = pet_expr_insert_arg(expr, n, index);
3391 if (!expr)
3392 return NULL;
3394 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3395 ls = isl_local_space_from_space(space);
3396 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
3397 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
3398 if (!expr->acc.index)
3399 return pet_expr_free(expr);
3401 return expr;
3402 error:
3403 pet_expr_free(expr);
3404 pet_expr_free(index);
3405 return NULL;
3408 /* Extend an access expression "expr" with an additional member acces to "id".
3409 * In particular, extend the index expression of "expr" to include
3410 * the additional member access.
3411 * The caller is responsible for calling pet_expr_access_set_depth
3412 * to update the corresponding access relation.
3414 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
3415 __isl_take isl_id *id)
3417 isl_space *space;
3418 isl_multi_pw_aff *field_access;
3420 expr = pet_expr_cow(expr);
3421 if (!expr || !id)
3422 goto error;
3423 if (expr->type != pet_expr_access)
3424 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3425 "not an access pet_expr", goto error);
3427 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3428 space = isl_space_from_domain(space);
3429 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3430 field_access = isl_multi_pw_aff_zero(space);
3431 expr->acc.index = pet_array_member(expr->acc.index, field_access);
3432 if (!expr->acc.index)
3433 return pet_expr_free(expr);
3435 return expr;
3436 error:
3437 pet_expr_free(expr);
3438 isl_id_free(id);
3439 return NULL;
3442 /* Prefix the access expression "expr" with "prefix".
3443 * If "add" is set, then it is not the index expression "prefix" itself
3444 * that was passed to the function, but its address.
3446 __isl_give pet_expr *pet_expr_access_patch(__isl_take pet_expr *expr,
3447 __isl_take isl_multi_pw_aff *prefix, int add)
3449 enum pet_expr_access_type type;
3451 expr = pet_expr_cow(expr);
3452 if (!expr || !prefix)
3453 goto error;
3454 if (expr->type != pet_expr_access)
3455 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3456 "not an access pet_expr", goto error);
3458 expr->acc.depth += isl_multi_pw_aff_dim(prefix, isl_dim_out) - add;
3459 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
3460 if (!expr->acc.access[type])
3461 continue;
3462 expr->acc.access[type] = pet_patch_union_map(
3463 isl_multi_pw_aff_copy(prefix), expr->acc.access[type],
3464 add, 0);
3465 if (!expr->acc.access[type])
3466 break;
3468 expr->acc.index = pet_patch_multi_pw_aff(prefix, expr->acc.index, add);
3469 if (!expr->acc.index || type < pet_expr_access_end)
3470 return pet_expr_free(expr);
3472 return expr;
3473 error:
3474 pet_expr_free(expr);
3475 isl_multi_pw_aff_free(prefix);
3476 return NULL;
3479 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
3481 int i;
3483 if (!expr)
3484 return;
3486 fprintf(stderr, "%*s", indent, "");
3488 switch (expr->type) {
3489 case pet_expr_double:
3490 fprintf(stderr, "%s\n", expr->d.s);
3491 break;
3492 case pet_expr_int:
3493 isl_val_dump(expr->i);
3494 break;
3495 case pet_expr_access:
3496 if (expr->acc.ref_id) {
3497 isl_id_dump(expr->acc.ref_id);
3498 fprintf(stderr, "%*s", indent, "");
3500 isl_multi_pw_aff_dump(expr->acc.index);
3501 fprintf(stderr, "%*sdepth: %d\n", indent + 2,
3502 "", expr->acc.depth);
3503 if (expr->acc.kill) {
3504 fprintf(stderr, "%*skill: 1\n", indent + 2, "");
3505 } else {
3506 fprintf(stderr, "%*sread: %d\n", indent + 2,
3507 "", expr->acc.read);
3508 fprintf(stderr, "%*swrite: %d\n", indent + 2,
3509 "", expr->acc.write);
3511 if (expr->acc.access[pet_expr_access_may_read]) {
3512 fprintf(stderr, "%*smay_read: ", indent + 2, "");
3513 isl_union_map_dump(
3514 expr->acc.access[pet_expr_access_may_read]);
3516 if (expr->acc.access[pet_expr_access_may_write]) {
3517 fprintf(stderr, "%*smay_write: ", indent + 2, "");
3518 isl_union_map_dump(
3519 expr->acc.access[pet_expr_access_may_write]);
3521 if (expr->acc.access[pet_expr_access_must_write]) {
3522 fprintf(stderr, "%*smust_write: ", indent + 2, "");
3523 isl_union_map_dump(
3524 expr->acc.access[pet_expr_access_must_write]);
3526 for (i = 0; i < expr->n_arg; ++i)
3527 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3528 break;
3529 case pet_expr_op:
3530 fprintf(stderr, "%s\n", op_str[expr->op]);
3531 for (i = 0; i < expr->n_arg; ++i)
3532 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3533 break;
3534 case pet_expr_call:
3535 fprintf(stderr, "%s/%d\n", expr->c.name, expr->n_arg);
3536 for (i = 0; i < expr->n_arg; ++i)
3537 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3538 if (expr->c.summary) {
3539 fprintf(stderr, "%*s", indent, "");
3540 fprintf(stderr, "summary:\n");
3541 pet_function_summary_dump_with_indent(expr->c.summary,
3542 indent + 2);
3544 break;
3545 case pet_expr_cast:
3546 fprintf(stderr, "(%s)\n", expr->type_name);
3547 for (i = 0; i < expr->n_arg; ++i)
3548 pet_expr_dump_with_indent(expr->args[i], indent + 2);
3549 break;
3550 case pet_expr_error:
3551 fprintf(stderr, "ERROR\n");
3552 break;
3556 void pet_expr_dump(__isl_keep pet_expr *expr)
3558 pet_expr_dump_with_indent(expr, 0);