privately export pet_stmt_is_affine_assume
[pet.git] / expr.c
blob2e0843e34e23741f8e8f5290a26d21c281e60883
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/hash.h>
38 #include <isl/union_set.h>
40 #include "aff.h"
41 #include "array.h"
42 #include "expr.h"
43 #include "expr_arg.h"
44 #include "filter.h"
45 #include "nest.h"
46 #include "options.h"
47 #include "value_bounds.h"
48 #include "patch.h"
50 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
52 static char *type_str[] = {
53 [pet_expr_access] = "access",
54 [pet_expr_call] = "call",
55 [pet_expr_cast] = "cast",
56 [pet_expr_double] = "double",
57 [pet_expr_int] = "int",
58 [pet_expr_op] = "op",
61 static char *op_str[] = {
62 [pet_op_add_assign] = "+=",
63 [pet_op_sub_assign] = "-=",
64 [pet_op_mul_assign] = "*=",
65 [pet_op_div_assign] = "/=",
66 [pet_op_assign] = "=",
67 [pet_op_add] = "+",
68 [pet_op_sub] = "-",
69 [pet_op_mul] = "*",
70 [pet_op_div] = "/",
71 [pet_op_mod] = "%",
72 [pet_op_shl] = "<<",
73 [pet_op_shr] = ">>",
74 [pet_op_eq] = "==",
75 [pet_op_ne] = "!=",
76 [pet_op_le] = "<=",
77 [pet_op_ge] = ">=",
78 [pet_op_lt] = "<",
79 [pet_op_gt] = ">",
80 [pet_op_minus] = "-",
81 [pet_op_post_inc] = "++",
82 [pet_op_post_dec] = "--",
83 [pet_op_pre_inc] = "++",
84 [pet_op_pre_dec] = "--",
85 [pet_op_address_of] = "&",
86 [pet_op_and] = "&",
87 [pet_op_xor] = "^",
88 [pet_op_or] = "|",
89 [pet_op_not] = "~",
90 [pet_op_land] = "&&",
91 [pet_op_lor] = "||",
92 [pet_op_lnot] = "!",
93 [pet_op_cond] = "?:",
94 [pet_op_assume] = "assume",
95 [pet_op_kill] = "kill"
98 const char *pet_op_str(enum pet_op_type op)
100 return op_str[op];
103 int pet_op_is_inc_dec(enum pet_op_type op)
105 return op == pet_op_post_inc || op == pet_op_post_dec ||
106 op == pet_op_pre_inc || op == pet_op_pre_dec;
109 const char *pet_type_str(enum pet_expr_type type)
111 return type_str[type];
114 enum pet_op_type pet_str_op(const char *str)
116 int i;
118 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
119 if (!strcmp(op_str[i], str))
120 return i;
122 return -1;
125 enum pet_expr_type pet_str_type(const char *str)
127 int i;
129 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
130 if (!strcmp(type_str[i], str))
131 return i;
133 return -1;
136 /* Construct a pet_expr of the given type.
138 __isl_give pet_expr *pet_expr_alloc(isl_ctx *ctx, enum pet_expr_type type)
140 pet_expr *expr;
142 expr = isl_calloc_type(ctx, struct pet_expr);
143 if (!expr)
144 return NULL;
146 expr->ctx = ctx;
147 isl_ctx_ref(ctx);
148 expr->type = type;
149 expr->ref = 1;
151 return expr;
154 /* Construct an access pet_expr from an index expression.
155 * By default, the access is considered to be a read access.
156 * The initial depth is set from the index expression and
157 * may still be updated by the caller before the access relation
158 * is created.
160 __isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
162 isl_ctx *ctx;
163 pet_expr *expr;
165 if (!index)
166 return NULL;
167 ctx = isl_multi_pw_aff_get_ctx(index);
168 expr = pet_expr_alloc(ctx, pet_expr_access);
169 if (!expr)
170 goto error;
172 expr->acc.read = 1;
173 expr->acc.write = 0;
175 expr = pet_expr_access_set_index(expr, index);
177 return expr;
178 error:
179 isl_multi_pw_aff_free(index);
180 return NULL;
183 /* Extend the range of "access" with "n" dimensions, retaining
184 * the tuple identifier on this range.
186 * If "access" represents a member access, then extend the range
187 * of the member.
189 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
191 isl_id *id;
193 id = isl_map_get_tuple_id(access, isl_dim_out);
195 if (!isl_map_range_is_wrapping(access)) {
196 access = isl_map_add_dims(access, isl_dim_out, n);
197 } else {
198 isl_map *domain;
200 domain = isl_map_copy(access);
201 domain = isl_map_range_factor_domain(domain);
202 access = isl_map_range_factor_range(access);
203 access = extend_range(access, n);
204 access = isl_map_range_product(domain, access);
207 access = isl_map_set_tuple_id(access, isl_dim_out, id);
209 return access;
212 /* Does the access expression "expr" have any explicit access relation?
214 isl_bool pet_expr_access_has_any_access_relation(__isl_keep pet_expr *expr)
216 enum pet_expr_access_type type;
218 if (!expr)
219 return isl_bool_error;
221 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type)
222 if (expr->acc.access[type])
223 return isl_bool_true;
225 return isl_bool_false;
228 /* Are all relevant access relations explicitly available in "expr"?
230 static int has_relevant_access_relations(__isl_keep pet_expr *expr)
232 enum pet_expr_access_type type;
234 if (!expr)
235 return -1;
237 if (expr->acc.kill && !expr->acc.access[pet_expr_access_fake_killed])
238 return 0;
239 if (expr->acc.read && !expr->acc.access[pet_expr_access_may_read])
240 return 0;
241 if (expr->acc.write &&
242 (!expr->acc.access[pet_expr_access_may_write] ||
243 !expr->acc.access[pet_expr_access_must_write]))
244 return 0;
246 return 1;
249 /* Replace the depth of the access expr "expr" by "depth".
251 * To avoid inconsistencies between the depth and the access relation,
252 * we currently do not allow the depth to change once the access relation
253 * has been set or computed.
255 __isl_give pet_expr *pet_expr_access_set_depth(__isl_take pet_expr *expr,
256 int depth)
258 isl_map *access;
259 int dim;
261 if (!expr)
262 return NULL;
263 if (expr->acc.depth == depth)
264 return expr;
265 if (pet_expr_access_has_any_access_relation(expr))
266 isl_die(pet_expr_get_ctx(expr), isl_error_unsupported,
267 "depth cannot be changed after access relation "
268 "has been set or computed", return pet_expr_free(expr));
270 expr = pet_expr_cow(expr);
271 if (!expr)
272 return NULL;
273 expr->acc.depth = depth;
275 return expr;
278 /* Construct a pet_expr that kills the elements specified by
279 * the index expression "index" and the access relation "access".
281 __isl_give pet_expr *pet_expr_kill_from_access_and_index(
282 __isl_take isl_map *access, __isl_take isl_multi_pw_aff *index)
284 int depth;
285 pet_expr *expr;
287 if (!access || !index)
288 goto error;
290 expr = pet_expr_from_index(index);
291 expr = pet_expr_access_set_read(expr, 0);
292 expr = pet_expr_access_set_kill(expr, 1);
293 depth = isl_map_dim(access, isl_dim_out);
294 expr = pet_expr_access_set_depth(expr, depth);
295 expr = pet_expr_access_set_access(expr, pet_expr_access_killed,
296 isl_union_map_from_map(access));
297 return pet_expr_new_unary(0, pet_op_kill, expr);
298 error:
299 isl_map_free(access);
300 isl_multi_pw_aff_free(index);
301 return NULL;
304 /* Construct a unary pet_expr that performs "op" on "arg",
305 * where the result is represented using a type of "type_size" bits
306 * (may be zero if unknown or if the type is not an integer).
308 __isl_give pet_expr *pet_expr_new_unary(int type_size, enum pet_op_type op,
309 __isl_take pet_expr *arg)
311 isl_ctx *ctx;
312 pet_expr *expr;
314 if (!arg)
315 return NULL;
316 ctx = pet_expr_get_ctx(arg);
317 expr = pet_expr_alloc(ctx, pet_expr_op);
318 expr = pet_expr_set_n_arg(expr, 1);
319 if (!expr)
320 goto error;
322 expr->op = op;
323 expr->type_size = type_size;
324 expr->args[pet_un_arg] = arg;
326 return expr;
327 error:
328 pet_expr_free(arg);
329 return NULL;
332 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
333 * where the result is represented using a type of "type_size" bits
334 * (may be zero if unknown or if the type is not an integer).
336 __isl_give pet_expr *pet_expr_new_binary(int type_size, enum pet_op_type op,
337 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
339 isl_ctx *ctx;
340 pet_expr *expr;
342 if (!lhs || !rhs)
343 goto error;
344 ctx = pet_expr_get_ctx(lhs);
345 expr = pet_expr_alloc(ctx, pet_expr_op);
346 expr = pet_expr_set_n_arg(expr, 2);
347 if (!expr)
348 goto error;
350 expr->op = op;
351 expr->type_size = type_size;
352 expr->args[pet_bin_lhs] = lhs;
353 expr->args[pet_bin_rhs] = rhs;
355 return expr;
356 error:
357 pet_expr_free(lhs);
358 pet_expr_free(rhs);
359 return NULL;
362 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
364 __isl_give pet_expr *pet_expr_new_ternary(__isl_take pet_expr *cond,
365 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
367 isl_ctx *ctx;
368 pet_expr *expr;
370 if (!cond || !lhs || !rhs)
371 goto error;
372 ctx = pet_expr_get_ctx(cond);
373 expr = pet_expr_alloc(ctx, pet_expr_op);
374 expr = pet_expr_set_n_arg(expr, 3);
375 if (!expr)
376 goto error;
378 expr->op = pet_op_cond;
379 expr->args[pet_ter_cond] = cond;
380 expr->args[pet_ter_true] = lhs;
381 expr->args[pet_ter_false] = rhs;
383 return expr;
384 error:
385 pet_expr_free(cond);
386 pet_expr_free(lhs);
387 pet_expr_free(rhs);
388 return NULL;
391 /* Construct a call pet_expr that calls function "name" with "n_arg"
392 * arguments. The caller is responsible for filling in the arguments.
394 __isl_give pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
395 unsigned n_arg)
397 pet_expr *expr;
399 expr = pet_expr_alloc(ctx, pet_expr_call);
400 expr = pet_expr_set_n_arg(expr, n_arg);
401 if (!expr)
402 return NULL;
404 expr->c.name = strdup(name);
405 if (!expr->c.name)
406 return pet_expr_free(expr);
408 return expr;
411 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
413 __isl_give pet_expr *pet_expr_new_cast(const char *type_name,
414 __isl_take pet_expr *arg)
416 isl_ctx *ctx;
417 pet_expr *expr;
419 if (!arg)
420 return NULL;
422 ctx = pet_expr_get_ctx(arg);
423 expr = pet_expr_alloc(ctx, pet_expr_cast);
424 expr = pet_expr_set_n_arg(expr, 1);
425 if (!expr)
426 goto error;
428 expr->type_name = strdup(type_name);
429 if (!expr->type_name)
430 goto error;
432 expr->args[0] = arg;
434 return expr;
435 error:
436 pet_expr_free(arg);
437 pet_expr_free(expr);
438 return NULL;
441 /* Construct a pet_expr that represents the double "d".
443 __isl_give pet_expr *pet_expr_new_double(isl_ctx *ctx,
444 double val, const char *s)
446 pet_expr *expr;
448 expr = pet_expr_alloc(ctx, pet_expr_double);
449 if (!expr)
450 return NULL;
452 expr->d.val = val;
453 expr->d.s = strdup(s);
454 if (!expr->d.s)
455 return pet_expr_free(expr);
457 return expr;
460 /* Construct a pet_expr that represents the integer value "v".
462 __isl_give pet_expr *pet_expr_new_int(__isl_take isl_val *v)
464 isl_ctx *ctx;
465 pet_expr *expr;
467 if (!v)
468 return NULL;
470 ctx = isl_val_get_ctx(v);
471 expr = pet_expr_alloc(ctx, pet_expr_int);
472 if (!expr)
473 goto error;
475 expr->i = v;
477 return expr;
478 error:
479 isl_val_free(v);
480 return NULL;
483 /* Return an independent duplicate of "expr".
485 * In case of an access expression, make sure the depth of the duplicate is set
486 * before the access relation (if any) is set and after the index expression
487 * is set.
489 static __isl_give pet_expr *pet_expr_dup(__isl_keep pet_expr *expr)
491 int i;
492 pet_expr *dup;
493 enum pet_expr_access_type type;
495 if (!expr)
496 return NULL;
498 dup = pet_expr_alloc(expr->ctx, expr->type);
499 dup = pet_expr_set_type_size(dup, expr->type_size);
500 dup = pet_expr_set_n_arg(dup, expr->n_arg);
501 for (i = 0; i < expr->n_arg; ++i)
502 dup = pet_expr_set_arg(dup, i, pet_expr_copy(expr->args[i]));
504 switch (expr->type) {
505 case pet_expr_access:
506 if (expr->acc.ref_id)
507 dup = pet_expr_access_set_ref_id(dup,
508 isl_id_copy(expr->acc.ref_id));
509 dup = pet_expr_access_set_index(dup,
510 isl_multi_pw_aff_copy(expr->acc.index));
511 dup = pet_expr_access_set_depth(dup, expr->acc.depth);
512 for (type = pet_expr_access_begin;
513 type < pet_expr_access_end; ++type) {
514 if (!expr->acc.access[type])
515 continue;
516 dup = pet_expr_access_set_access(dup, type,
517 isl_union_map_copy(expr->acc.access[type]));
519 dup = pet_expr_access_set_read(dup, expr->acc.read);
520 dup = pet_expr_access_set_write(dup, expr->acc.write);
521 dup = pet_expr_access_set_kill(dup, expr->acc.kill);
522 break;
523 case pet_expr_call:
524 dup = pet_expr_call_set_name(dup, expr->c.name);
525 if (expr->c.summary)
526 dup = pet_expr_call_set_summary(dup,
527 pet_function_summary_copy(expr->c.summary));
528 break;
529 case pet_expr_cast:
530 dup = pet_expr_cast_set_type_name(dup, expr->type_name);
531 break;
532 case pet_expr_double:
533 dup = pet_expr_double_set(dup, expr->d.val, expr->d.s);
534 break;
535 case pet_expr_int:
536 dup = pet_expr_int_set_val(dup, isl_val_copy(expr->i));
537 break;
538 case pet_expr_op:
539 dup = pet_expr_op_set_type(dup, expr->op);
540 break;
541 case pet_expr_error:
542 dup = pet_expr_free(dup);
543 break;
546 return dup;
549 /* Return a pet_expr that is equal to "expr" and that has only
550 * a single reference.
552 * If "expr" itself only has one reference, then clear its hash value
553 * since the returned pet_expr will be modified.
555 __isl_give pet_expr *pet_expr_cow(__isl_take pet_expr *expr)
557 if (!expr)
558 return NULL;
560 if (expr->ref == 1) {
561 expr->hash = 0;
562 return expr;
564 expr->ref--;
565 return pet_expr_dup(expr);
568 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr)
570 enum pet_expr_access_type type;
571 int i;
573 if (!expr)
574 return NULL;
575 if (--expr->ref > 0)
576 return NULL;
578 for (i = 0; i < expr->n_arg; ++i)
579 pet_expr_free(expr->args[i]);
580 free(expr->args);
582 switch (expr->type) {
583 case pet_expr_access:
584 isl_id_free(expr->acc.ref_id);
585 for (type = pet_expr_access_begin;
586 type < pet_expr_access_end; ++type)
587 isl_union_map_free(expr->acc.access[type]);
588 isl_multi_pw_aff_free(expr->acc.index);
589 break;
590 case pet_expr_call:
591 free(expr->c.name);
592 pet_function_summary_free(expr->c.summary);
593 break;
594 case pet_expr_cast:
595 free(expr->type_name);
596 break;
597 case pet_expr_double:
598 free(expr->d.s);
599 break;
600 case pet_expr_int:
601 isl_val_free(expr->i);
602 break;
603 case pet_expr_op:
604 case pet_expr_error:
605 break;
608 isl_ctx_deref(expr->ctx);
609 free(expr);
610 return NULL;
613 /* Return an additional reference to "expr".
615 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr)
617 if (!expr)
618 return NULL;
620 expr->ref++;
621 return expr;
624 /* Return the isl_ctx in which "expr" was created.
626 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr)
628 return expr ? expr->ctx : NULL;
631 /* Return the type of "expr".
633 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr)
635 if (!expr)
636 return pet_expr_error;
637 return expr->type;
640 /* Return the number of arguments of "expr".
642 int pet_expr_get_n_arg(__isl_keep pet_expr *expr)
644 if (!expr)
645 return -1;
647 return expr->n_arg;
650 /* Set the number of arguments of "expr" to "n".
652 * If "expr" originally had more arguments, then remove the extra arguments.
653 * If "expr" originally had fewer arguments, then create space for
654 * the extra arguments ans initialize them to NULL.
656 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n)
658 int i;
659 pet_expr **args;
661 if (!expr)
662 return NULL;
663 if (expr->n_arg == n)
664 return expr;
665 expr = pet_expr_cow(expr);
666 if (!expr)
667 return NULL;
669 if (n < expr->n_arg) {
670 for (i = n; i < expr->n_arg; ++i)
671 pet_expr_free(expr->args[i]);
672 expr->n_arg = n;
673 return expr;
676 args = isl_realloc_array(expr->ctx, expr->args, pet_expr *, n);
677 if (!args)
678 return pet_expr_free(expr);
679 expr->args = args;
680 for (i = expr->n_arg; i < n; ++i)
681 expr->args[i] = NULL;
682 expr->n_arg = n;
684 return expr;
687 /* Return the argument of "expr" at position "pos".
689 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos)
691 if (!expr)
692 return NULL;
693 if (pos < 0 || pos >= expr->n_arg)
694 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
695 "position out of bounds", return NULL);
697 return pet_expr_copy(expr->args[pos]);
700 /* Replace "expr" by its argument at position "pos".
702 __isl_give pet_expr *pet_expr_arg(__isl_take pet_expr *expr, int pos)
704 pet_expr *arg;
706 arg = pet_expr_get_arg(expr, pos);
707 pet_expr_free(expr);
709 return arg;
712 /* Replace the argument of "expr" at position "pos" by "arg".
714 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
715 __isl_take pet_expr *arg)
717 if (!expr || !arg)
718 goto error;
719 if (pos < 0 || pos >= expr->n_arg)
720 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
721 "position out of bounds", goto error);
722 if (expr->args[pos] == arg) {
723 pet_expr_free(arg);
724 return expr;
727 expr = pet_expr_cow(expr);
728 if (!expr)
729 goto error;
731 pet_expr_free(expr->args[pos]);
732 expr->args[pos] = arg;
734 return expr;
735 error:
736 pet_expr_free(expr);
737 pet_expr_free(arg);
738 return NULL;
741 /* Does "expr" perform a comparison operation?
743 int pet_expr_is_comparison(__isl_keep pet_expr *expr)
745 if (!expr)
746 return -1;
747 if (expr->type != pet_expr_op)
748 return 0;
749 switch (expr->op) {
750 case pet_op_eq:
751 case pet_op_ne:
752 case pet_op_le:
753 case pet_op_ge:
754 case pet_op_lt:
755 case pet_op_gt:
756 return 1;
757 default:
758 return 0;
762 /* Does "expr" perform a boolean operation?
764 int pet_expr_is_boolean(__isl_keep pet_expr *expr)
766 if (!expr)
767 return -1;
768 if (expr->type != pet_expr_op)
769 return 0;
770 switch (expr->op) {
771 case pet_op_land:
772 case pet_op_lor:
773 case pet_op_lnot:
774 return 1;
775 default:
776 return 0;
780 /* Is "expr" an address-of operation?
782 int pet_expr_is_address_of(__isl_keep pet_expr *expr)
784 if (!expr)
785 return -1;
786 if (expr->type != pet_expr_op)
787 return 0;
788 return expr->op == pet_op_address_of;
791 /* Is "expr" an assume statement?
793 int pet_expr_is_assume(__isl_keep pet_expr *expr)
795 if (!expr)
796 return -1;
797 if (expr->type != pet_expr_op)
798 return 0;
799 return expr->op == pet_op_assume;
802 /* Does "expr" perform a min operation?
804 int pet_expr_is_min(__isl_keep pet_expr *expr)
806 if (!expr)
807 return -1;
808 if (expr->type != pet_expr_call)
809 return 0;
810 if (expr->n_arg != 2)
811 return 0;
812 if (strcmp(expr->c.name, "min") != 0)
813 return 0;
814 return 1;
817 /* Does "expr" perform a max operation?
819 int pet_expr_is_max(__isl_keep pet_expr *expr)
821 if (!expr)
822 return -1;
823 if (expr->type != pet_expr_call)
824 return 0;
825 if (expr->n_arg != 2)
826 return 0;
827 if (strcmp(expr->c.name, "max") != 0)
828 return 0;
829 return 1;
832 /* Does "expr" represent an access to an unnamed space, i.e.,
833 * does it represent an affine expression?
835 isl_bool pet_expr_is_affine(__isl_keep pet_expr *expr)
837 int has_id;
839 if (!expr)
840 return isl_bool_error;
841 if (expr->type != pet_expr_access)
842 return isl_bool_false;
844 has_id = isl_multi_pw_aff_has_tuple_id(expr->acc.index, isl_dim_out);
845 if (has_id < 0)
846 return isl_bool_error;
848 return !has_id;
851 /* Given that "expr" represents an affine expression, i.e., that
852 * it is an access to an unnamed (1D) space, return this affine expression.
854 __isl_give isl_pw_aff *pet_expr_get_affine(__isl_keep pet_expr *expr)
856 isl_bool is_affine;
857 isl_pw_aff *pa;
858 isl_multi_pw_aff *mpa;
860 is_affine = pet_expr_is_affine(expr);
861 if (is_affine < 0)
862 return NULL;
863 if (!is_affine)
864 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
865 "not an affine expression", return NULL);
867 mpa = pet_expr_access_get_index(expr);
868 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
869 isl_multi_pw_aff_free(mpa);
870 return pa;
873 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
874 * not part of any struct?
876 int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
878 if (!expr)
879 return -1;
880 if (expr->type != pet_expr_access)
881 return 0;
882 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index))
883 return 0;
885 return expr->acc.depth == 0;
888 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
889 * of parameters.
891 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
892 __isl_keep isl_multi_pw_aff *mpa2)
894 int equal;
896 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
897 if (equal < 0 || equal)
898 return equal;
899 mpa2 = isl_multi_pw_aff_copy(mpa2);
900 mpa2 = isl_multi_pw_aff_align_params(mpa2,
901 isl_multi_pw_aff_get_space(mpa1));
902 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
903 isl_multi_pw_aff_free(mpa2);
905 return equal;
908 /* Construct an access relation from the index expression and
909 * the array depth of the access expression "expr".
911 * If the number of indices is smaller than the depth of the array,
912 * then we assume that all elements of the remaining dimensions
913 * are accessed.
915 static __isl_give isl_union_map *construct_access_relation(
916 __isl_keep pet_expr *expr)
918 isl_map *access;
919 int dim;
920 int read, write;
922 if (!expr)
923 return NULL;
925 access = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
926 if (!access)
927 return NULL;
929 dim = isl_map_dim(access, isl_dim_out);
930 if (dim > expr->acc.depth)
931 isl_die(isl_map_get_ctx(access), isl_error_internal,
932 "number of indices greater than depth",
933 access = isl_map_free(access));
935 if (dim != expr->acc.depth)
936 access = extend_range(access, expr->acc.depth - dim);
938 return isl_union_map_from_map(access);
941 /* Ensure that all relevant access relations are explicitly
942 * available in "expr".
944 * If "expr" does not already have the relevant access relations, then create
945 * them based on the index expression and the array depth.
947 * We do not cow since adding an explicit access relation
948 * does not change the meaning of the expression.
949 * However, the explicit access relations may modify the hash value,
950 * so the cached value is reset.
952 static __isl_give pet_expr *introduce_access_relations(
953 __isl_take pet_expr *expr)
955 enum pet_expr_access_type type;
956 isl_union_map *access;
957 int dim;
958 int kill, read, write;
960 if (!expr)
961 return NULL;
962 if (has_relevant_access_relations(expr))
963 return expr;
965 access = construct_access_relation(expr);
966 if (!access)
967 return pet_expr_free(expr);
969 expr->hash = 0;
970 kill = expr->acc.kill;
971 read = expr->acc.read;
972 write = expr->acc.write;
973 if (kill && !expr->acc.access[pet_expr_access_fake_killed])
974 expr->acc.access[pet_expr_access_fake_killed] =
975 isl_union_map_copy(access);
976 if (read && !expr->acc.access[pet_expr_access_may_read])
977 expr->acc.access[pet_expr_access_may_read] =
978 isl_union_map_copy(access);
979 if (write && !expr->acc.access[pet_expr_access_may_write])
980 expr->acc.access[pet_expr_access_may_write] =
981 isl_union_map_copy(access);
982 if (write && !expr->acc.access[pet_expr_access_must_write])
983 expr->acc.access[pet_expr_access_must_write] =
984 isl_union_map_copy(access);
986 isl_union_map_free(access);
988 if (!has_relevant_access_relations(expr))
989 return pet_expr_free(expr);
991 return expr;
994 /* Return a hash value that digests "expr".
995 * If a hash value was computed already, then return that value.
996 * Otherwise, compute the hash value and store a copy in expr->hash.
998 uint32_t pet_expr_get_hash(__isl_keep pet_expr *expr)
1000 int i;
1001 enum pet_expr_access_type type;
1002 uint32_t hash, hash_f;
1004 if (!expr)
1005 return 0;
1006 if (expr->hash)
1007 return expr->hash;
1009 hash = isl_hash_init();
1010 isl_hash_byte(hash, expr->type & 0xFF);
1011 isl_hash_byte(hash, expr->n_arg & 0xFF);
1012 for (i = 0; i < expr->n_arg; ++i) {
1013 uint32_t hash_i;
1014 hash_i = pet_expr_get_hash(expr->args[i]);
1015 isl_hash_hash(hash, hash_i);
1017 switch (expr->type) {
1018 case pet_expr_error:
1019 return 0;
1020 case pet_expr_double:
1021 hash = isl_hash_string(hash, expr->d.s);
1022 break;
1023 case pet_expr_int:
1024 hash_f = isl_val_get_hash(expr->i);
1025 isl_hash_hash(hash, hash_f);
1026 break;
1027 case pet_expr_access:
1028 isl_hash_byte(hash, expr->acc.read & 0xFF);
1029 isl_hash_byte(hash, expr->acc.write & 0xFF);
1030 isl_hash_byte(hash, expr->acc.kill & 0xFF);
1031 hash_f = isl_id_get_hash(expr->acc.ref_id);
1032 isl_hash_hash(hash, hash_f);
1033 hash_f = isl_multi_pw_aff_get_hash(expr->acc.index);
1034 isl_hash_hash(hash, hash_f);
1035 isl_hash_byte(hash, expr->acc.depth & 0xFF);
1036 for (type = pet_expr_access_begin;
1037 type < pet_expr_access_end; ++type) {
1038 hash_f = isl_union_map_get_hash(expr->acc.access[type]);
1039 isl_hash_hash(hash, hash_f);
1041 break;
1042 case pet_expr_op:
1043 isl_hash_byte(hash, expr->op & 0xFF);
1044 break;
1045 case pet_expr_call:
1046 hash = isl_hash_string(hash, expr->c.name);
1047 break;
1048 case pet_expr_cast:
1049 hash = isl_hash_string(hash, expr->type_name);
1050 break;
1052 expr->hash = hash;
1053 return hash;
1056 /* Return 1 if the two pet_exprs are equivalent.
1058 int pet_expr_is_equal(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2)
1060 int i;
1061 enum pet_expr_access_type type;
1063 if (!expr1 || !expr2)
1064 return 0;
1066 if (expr1->type != expr2->type)
1067 return 0;
1068 if (expr1->n_arg != expr2->n_arg)
1069 return 0;
1070 for (i = 0; i < expr1->n_arg; ++i)
1071 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
1072 return 0;
1073 switch (expr1->type) {
1074 case pet_expr_error:
1075 return -1;
1076 case pet_expr_double:
1077 if (strcmp(expr1->d.s, expr2->d.s))
1078 return 0;
1079 if (expr1->d.val != expr2->d.val)
1080 return 0;
1081 break;
1082 case pet_expr_int:
1083 if (!isl_val_eq(expr1->i, expr2->i))
1084 return 0;
1085 break;
1086 case pet_expr_access:
1087 if (expr1->acc.read != expr2->acc.read)
1088 return 0;
1089 if (expr1->acc.write != expr2->acc.write)
1090 return 0;
1091 if (expr1->acc.kill != expr2->acc.kill)
1092 return 0;
1093 if (expr1->acc.ref_id != expr2->acc.ref_id)
1094 return 0;
1095 if (!expr1->acc.index || !expr2->acc.index)
1096 return 0;
1097 if (!multi_pw_aff_is_equal(expr1->acc.index, expr2->acc.index))
1098 return 0;
1099 if (expr1->acc.depth != expr2->acc.depth)
1100 return 0;
1101 if (has_relevant_access_relations(expr1) !=
1102 has_relevant_access_relations(expr2)) {
1103 int equal;
1104 expr1 = pet_expr_copy(expr1);
1105 expr2 = pet_expr_copy(expr2);
1106 expr1 = introduce_access_relations(expr1);
1107 expr2 = introduce_access_relations(expr2);
1108 equal = pet_expr_is_equal(expr1, expr2);
1109 pet_expr_free(expr1);
1110 pet_expr_free(expr2);
1111 return equal;
1113 for (type = pet_expr_access_begin;
1114 type < pet_expr_access_end; ++type) {
1115 if (!expr1->acc.access[type] !=
1116 !expr2->acc.access[type])
1117 return 0;
1118 if (!expr1->acc.access[type])
1119 continue;
1120 if (!isl_union_map_is_equal(expr1->acc.access[type],
1121 expr2->acc.access[type]))
1122 return 0;
1124 break;
1125 case pet_expr_op:
1126 if (expr1->op != expr2->op)
1127 return 0;
1128 break;
1129 case pet_expr_call:
1130 if (strcmp(expr1->c.name, expr2->c.name))
1131 return 0;
1132 break;
1133 case pet_expr_cast:
1134 if (strcmp(expr1->type_name, expr2->type_name))
1135 return 0;
1136 break;
1139 return 1;
1142 /* Do "expr1" and "expr2" represent two accesses to the same array
1143 * that are also of the same type? That is, can these two accesses
1144 * be replaced by a single access?
1146 isl_bool pet_expr_is_same_access(__isl_keep pet_expr *expr1,
1147 __isl_keep pet_expr *expr2)
1149 isl_space *space1, *space2;
1150 isl_bool same;
1152 if (!expr1 || !expr2)
1153 return isl_bool_error;
1154 if (pet_expr_get_type(expr1) != pet_expr_access)
1155 return isl_bool_false;
1156 if (pet_expr_get_type(expr2) != pet_expr_access)
1157 return isl_bool_false;
1158 if (expr1->acc.read != expr2->acc.read)
1159 return isl_bool_false;
1160 if (expr1->acc.write != expr2->acc.write)
1161 return isl_bool_false;
1162 if (expr1->acc.kill != expr2->acc.kill)
1163 return isl_bool_false;
1164 if (expr1->acc.depth != expr2->acc.depth)
1165 return isl_bool_false;
1167 space1 = isl_multi_pw_aff_get_space(expr1->acc.index);
1168 space2 = isl_multi_pw_aff_get_space(expr2->acc.index);
1169 same = isl_space_tuple_is_equal(space1, isl_dim_out,
1170 space2, isl_dim_out);
1171 if (same >= 0 && same)
1172 same = isl_space_tuple_is_equal(space1, isl_dim_in,
1173 space2, isl_dim_in);
1174 isl_space_free(space1);
1175 isl_space_free(space2);
1177 return same;
1180 /* Does the access expression "expr" read the accessed elements?
1182 isl_bool pet_expr_access_is_read(__isl_keep pet_expr *expr)
1184 if (!expr)
1185 return isl_bool_error;
1186 if (expr->type != pet_expr_access)
1187 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1188 "not an access expression", return isl_bool_error);
1190 return expr->acc.read;
1193 /* Does the access expression "expr" write to the accessed elements?
1195 isl_bool pet_expr_access_is_write(__isl_keep pet_expr *expr)
1197 if (!expr)
1198 return isl_bool_error;
1199 if (expr->type != pet_expr_access)
1200 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1201 "not an access expression", return isl_bool_error);
1203 return expr->acc.write;
1206 /* Does the access expression "expr" kill the accessed elements?
1208 isl_bool pet_expr_access_is_kill(__isl_keep pet_expr *expr)
1210 if (!expr)
1211 return isl_bool_error;
1212 if (expr->type != pet_expr_access)
1213 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1214 "not an access expression", return isl_bool_error);
1216 return expr->acc.kill;
1219 /* Return the identifier of the array accessed by "expr".
1221 * If "expr" represents a member access, then return the identifier
1222 * of the outer structure array.
1224 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
1226 if (!expr)
1227 return NULL;
1228 if (expr->type != pet_expr_access)
1229 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1230 "not an access expression", return NULL);
1232 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index)) {
1233 isl_space *space;
1234 isl_id *id;
1236 space = isl_multi_pw_aff_get_space(expr->acc.index);
1237 space = isl_space_range(space);
1238 while (space && isl_space_is_wrapping(space))
1239 space = isl_space_domain(isl_space_unwrap(space));
1240 id = isl_space_get_tuple_id(space, isl_dim_set);
1241 isl_space_free(space);
1243 return id;
1246 return isl_multi_pw_aff_get_tuple_id(expr->acc.index, isl_dim_out);
1249 /* Return the parameter space of "expr".
1251 __isl_give isl_space *pet_expr_access_get_parameter_space(
1252 __isl_keep pet_expr *expr)
1254 isl_space *space;
1256 if (!expr)
1257 return NULL;
1258 if (expr->type != pet_expr_access)
1259 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1260 "not an access expression", return NULL);
1262 space = isl_multi_pw_aff_get_space(expr->acc.index);
1263 space = isl_space_params(space);
1265 return space;
1268 /* Return the domain space of "expr", including the arguments (if any).
1270 __isl_give isl_space *pet_expr_access_get_augmented_domain_space(
1271 __isl_keep pet_expr *expr)
1273 isl_space *space;
1275 if (!expr)
1276 return NULL;
1277 if (expr->type != pet_expr_access)
1278 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1279 "not an access expression", return NULL);
1281 space = isl_multi_pw_aff_get_space(expr->acc.index);
1282 space = isl_space_domain(space);
1284 return space;
1287 /* Return the domain space of "expr", without the arguments (if any).
1289 __isl_give isl_space *pet_expr_access_get_domain_space(
1290 __isl_keep pet_expr *expr)
1292 isl_space *space;
1294 space = pet_expr_access_get_augmented_domain_space(expr);
1295 if (isl_space_is_wrapping(space))
1296 space = isl_space_domain(isl_space_unwrap(space));
1298 return space;
1301 /* Return the space of the data accessed by "expr".
1303 __isl_give isl_space *pet_expr_access_get_data_space(__isl_keep pet_expr *expr)
1305 isl_space *space;
1307 if (!expr)
1308 return NULL;
1309 if (expr->type != pet_expr_access)
1310 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1311 "not an access expression", return NULL);
1313 space = isl_multi_pw_aff_get_space(expr->acc.index);
1314 space = isl_space_range(space);
1316 return space;
1319 /* Modify all subexpressions of "expr" by calling "fn" on them.
1320 * The subexpressions are traversed in depth first preorder.
1322 __isl_give pet_expr *pet_expr_map_top_down(__isl_take pet_expr *expr,
1323 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1324 void *user)
1326 int i, n;
1328 if (!expr)
1329 return NULL;
1331 expr = fn(expr, user);
1333 n = pet_expr_get_n_arg(expr);
1334 for (i = 0; i < n; ++i) {
1335 pet_expr *arg = pet_expr_get_arg(expr, i);
1336 arg = pet_expr_map_top_down(arg, fn, user);
1337 expr = pet_expr_set_arg(expr, i, arg);
1340 return expr;
1343 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1345 static __isl_give pet_expr *pet_expr_map_expr_of_type(__isl_take pet_expr *expr,
1346 enum pet_expr_type type,
1347 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1348 void *user)
1350 int i, n;
1352 n = pet_expr_get_n_arg(expr);
1353 for (i = 0; i < n; ++i) {
1354 pet_expr *arg = pet_expr_get_arg(expr, i);
1355 arg = pet_expr_map_expr_of_type(arg, type, fn, user);
1356 expr = pet_expr_set_arg(expr, i, arg);
1359 if (!expr)
1360 return NULL;
1362 if (expr->type == type)
1363 expr = fn(expr, user);
1365 return expr;
1368 /* Modify all expressions of type pet_expr_access in "expr"
1369 * by calling "fn" on them.
1371 __isl_give pet_expr *pet_expr_map_access(__isl_take pet_expr *expr,
1372 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1373 void *user)
1375 return pet_expr_map_expr_of_type(expr, pet_expr_access, fn, user);
1378 /* Modify all expressions of type pet_expr_call in "expr"
1379 * by calling "fn" on them.
1381 __isl_give pet_expr *pet_expr_map_call(__isl_take pet_expr *expr,
1382 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1383 void *user)
1385 return pet_expr_map_expr_of_type(expr, pet_expr_call, fn, user);
1388 /* Modify all expressions of type pet_expr_op in "expr"
1389 * by calling "fn" on them.
1391 __isl_give pet_expr *pet_expr_map_op(__isl_take pet_expr *expr,
1392 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1393 void *user)
1395 return pet_expr_map_expr_of_type(expr, pet_expr_op, fn, user);
1398 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1400 * Return -1 on error (where fn returning a negative value is treated as
1401 * an error).
1402 * Otherwise return 0.
1404 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
1405 enum pet_expr_type type,
1406 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1408 int i;
1410 if (!expr)
1411 return -1;
1413 for (i = 0; i < expr->n_arg; ++i)
1414 if (pet_expr_foreach_expr_of_type(expr->args[i],
1415 type, fn, user) < 0)
1416 return -1;
1418 if (expr->type == type)
1419 return fn(expr, user);
1421 return 0;
1424 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1426 * Return -1 on error (where fn returning a negative value is treated as
1427 * an error).
1428 * Otherwise return 0.
1430 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
1431 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1433 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
1436 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1438 * Return -1 on error (where fn returning a negative value is treated as
1439 * an error).
1440 * Otherwise return 0.
1442 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
1443 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1445 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1448 /* Internal data structure for pet_expr_writes.
1449 * "id" is the identifier that we are looking for.
1450 * "found" is set if we have found the identifier being written to.
1452 struct pet_expr_writes_data {
1453 isl_id *id;
1454 int found;
1457 /* Given an access expression, check if it writes to data->id.
1458 * If so, set data->found and abort the search.
1460 static int writes(__isl_keep pet_expr *expr, void *user)
1462 struct pet_expr_writes_data *data = user;
1463 isl_id *write_id;
1465 if (!expr->acc.write)
1466 return 0;
1467 if (pet_expr_is_affine(expr))
1468 return 0;
1470 write_id = pet_expr_access_get_id(expr);
1471 isl_id_free(write_id);
1473 if (!write_id)
1474 return -1;
1476 if (write_id != data->id)
1477 return 0;
1479 data->found = 1;
1480 return -1;
1483 /* Does expression "expr" write to "id"?
1485 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1487 struct pet_expr_writes_data data;
1489 data.id = id;
1490 data.found = 0;
1491 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1492 !data.found)
1493 return -1;
1495 return data.found;
1498 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1499 * index expression and access relations of "expr" (if any)
1500 * to dimensions of "dst_type" at "dst_pos".
1502 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1503 enum isl_dim_type dst_type, unsigned dst_pos,
1504 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1506 enum pet_expr_access_type type;
1508 expr = pet_expr_cow(expr);
1509 if (!expr)
1510 return NULL;
1511 if (expr->type != pet_expr_access)
1512 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1513 "not an access pet_expr", return pet_expr_free(expr));
1515 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1516 if (!expr->acc.access[type])
1517 continue;
1518 expr->acc.access[type] =
1519 pet_union_map_move_dims(expr->acc.access[type],
1520 dst_type, dst_pos, src_type, src_pos, n);
1521 if (!expr->acc.access[type])
1522 break;
1524 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1525 dst_type, dst_pos, src_type, src_pos, n);
1526 if (!expr->acc.index || type < pet_expr_access_end)
1527 return pet_expr_free(expr);
1529 return expr;
1532 /* Replace the index expression and access relations (if any) of "expr"
1533 * by their preimages under the function represented by "ma".
1535 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1536 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1538 enum pet_expr_access_type type;
1540 expr = pet_expr_cow(expr);
1541 if (!expr || !ma)
1542 goto error;
1543 if (expr->type != pet_expr_access)
1544 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1545 "not an access pet_expr", goto error);
1547 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1548 if (!expr->acc.access[type])
1549 continue;
1550 expr->acc.access[type] =
1551 isl_union_map_preimage_domain_multi_aff(
1552 expr->acc.access[type], isl_multi_aff_copy(ma));
1553 if (!expr->acc.access[type])
1554 break;
1556 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1557 ma);
1558 if (!expr->acc.index || type < pet_expr_access_end)
1559 return pet_expr_free(expr);
1561 return expr;
1562 error:
1563 isl_multi_aff_free(ma);
1564 pet_expr_free(expr);
1565 return NULL;
1568 /* Replace the index expression and access relations (if any) of "expr"
1569 * by their preimages under the function represented by "mpa".
1571 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1572 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1574 enum pet_expr_access_type type;
1576 expr = pet_expr_cow(expr);
1577 if (!expr || !mpa)
1578 goto error;
1579 if (expr->type != pet_expr_access)
1580 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1581 "not an access pet_expr", goto error);
1583 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1584 if (!expr->acc.access[type])
1585 continue;
1586 expr->acc.access[type] =
1587 isl_union_map_preimage_domain_multi_pw_aff(
1588 expr->acc.access[type], isl_multi_pw_aff_copy(mpa));
1589 if (!expr->acc.access[type])
1590 break;
1592 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1593 expr->acc.index, mpa);
1594 if (!expr->acc.index || type < pet_expr_access_end)
1595 return pet_expr_free(expr);
1597 return expr;
1598 error:
1599 isl_multi_pw_aff_free(mpa);
1600 pet_expr_free(expr);
1601 return NULL;
1604 /* Return the index expression of access expression "expr".
1606 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1607 __isl_keep pet_expr *expr)
1609 if (!expr)
1610 return NULL;
1611 if (expr->type != pet_expr_access)
1612 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1613 "not an access expression", return NULL);
1615 return isl_multi_pw_aff_copy(expr->acc.index);
1618 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1620 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1622 isl_space *space;
1623 enum pet_expr_access_type type;
1625 expr = pet_expr_cow(expr);
1626 if (!expr)
1627 return NULL;
1628 if (expr->type != pet_expr_access)
1629 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1630 "not an access expression", return pet_expr_free(expr));
1632 if (!pet_expr_access_has_any_access_relation(expr))
1633 return expr;
1635 space = isl_multi_pw_aff_get_space(expr->acc.index);
1636 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1637 if (!expr->acc.access[type])
1638 continue;
1639 space = isl_space_align_params(space,
1640 isl_union_map_get_space(expr->acc.access[type]));
1642 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1643 isl_space_copy(space));
1644 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1645 if (!expr->acc.access[type])
1646 continue;
1647 expr->acc.access[type] =
1648 isl_union_map_align_params(expr->acc.access[type],
1649 isl_space_copy(space));
1650 if (!expr->acc.access[type])
1651 break;
1653 isl_space_free(space);
1654 if (!expr->acc.index || type < pet_expr_access_end)
1655 return pet_expr_free(expr);
1657 return expr;
1660 /* Are "expr1" and "expr2" both array accesses such that
1661 * the access relation of "expr1" is a subset of that of "expr2"?
1662 * Only take into account the first "n_arg" arguments.
1664 * This function is tailored for use by mark_self_dependences in nest.c.
1665 * In particular, the input expressions may have more than "n_arg"
1666 * elements in their arguments arrays, while only the first "n_arg"
1667 * elements are referenced from the access relations.
1669 int pet_expr_is_sub_access(__isl_keep pet_expr *expr1,
1670 __isl_keep pet_expr *expr2, int n_arg)
1672 isl_id *id1, *id2;
1673 int i, n1, n2;
1674 int is_subset;
1676 if (!expr1 || !expr2)
1677 return 0;
1678 if (pet_expr_get_type(expr1) != pet_expr_access)
1679 return 0;
1680 if (pet_expr_get_type(expr2) != pet_expr_access)
1681 return 0;
1682 if (pet_expr_is_affine(expr1))
1683 return 0;
1684 if (pet_expr_is_affine(expr2))
1685 return 0;
1686 n1 = pet_expr_get_n_arg(expr1);
1687 if (n1 > n_arg)
1688 n1 = n_arg;
1689 n2 = pet_expr_get_n_arg(expr2);
1690 if (n2 > n_arg)
1691 n2 = n_arg;
1692 if (n1 != n2)
1693 return 0;
1694 for (i = 0; i < n1; ++i) {
1695 int equal;
1696 equal = pet_expr_is_equal(expr1->args[i], expr2->args[i]);
1697 if (equal < 0 || !equal)
1698 return equal;
1700 id1 = pet_expr_access_get_id(expr1);
1701 id2 = pet_expr_access_get_id(expr2);
1702 isl_id_free(id1);
1703 isl_id_free(id2);
1704 if (!id1 || !id2)
1705 return 0;
1706 if (id1 != id2)
1707 return 0;
1709 expr1 = pet_expr_copy(expr1);
1710 expr2 = pet_expr_copy(expr2);
1711 expr1 = introduce_access_relations(expr1);
1712 expr2 = introduce_access_relations(expr2);
1713 if (!expr1 || !expr2)
1714 goto error;
1716 is_subset = isl_union_map_is_subset(
1717 expr1->acc.access[pet_expr_access_may_read],
1718 expr2->acc.access[pet_expr_access_may_read]);
1720 pet_expr_free(expr1);
1721 pet_expr_free(expr2);
1723 return is_subset;
1724 error:
1725 pet_expr_free(expr1);
1726 pet_expr_free(expr2);
1727 return -1;
1730 /* Given a set in the iteration space "domain", extend it to live in the space
1731 * of the domain of access relations.
1733 * That, is the number of arguments "n" is 0, then simply return domain.
1734 * Otherwise, return [domain -> [a_1,...,a_n]].
1736 static __isl_give isl_set *add_arguments(__isl_take isl_set *domain, int n)
1738 isl_map *map;
1740 if (n == 0)
1741 return domain;
1743 map = isl_map_from_domain(domain);
1744 map = isl_map_add_dims(map, isl_dim_out, n);
1745 return isl_map_wrap(map);
1748 /* Add extra conditions to the domains of all access relations in "expr",
1749 * introducing access relations if they are not already present.
1751 * The conditions are not added to the index expression. Instead, they
1752 * are used to try and simplify the index expression.
1754 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1755 __isl_take isl_set *cond)
1757 int i;
1758 isl_union_set *uset;
1759 enum pet_expr_access_type type;
1761 expr = pet_expr_cow(expr);
1762 if (!expr)
1763 goto error;
1765 for (i = 0; i < expr->n_arg; ++i) {
1766 expr->args[i] = pet_expr_restrict(expr->args[i],
1767 isl_set_copy(cond));
1768 if (!expr->args[i])
1769 goto error;
1772 if (expr->type != pet_expr_access) {
1773 isl_set_free(cond);
1774 return expr;
1777 expr = introduce_access_relations(expr);
1778 if (!expr)
1779 goto error;
1781 cond = add_arguments(cond, expr->n_arg);
1782 uset = isl_union_set_from_set(isl_set_copy(cond));
1783 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1784 if (!expr->acc.access[type])
1785 continue;
1786 expr->acc.access[type] =
1787 isl_union_map_intersect_domain(expr->acc.access[type],
1788 isl_union_set_copy(uset));
1789 if (!expr->acc.access[type])
1790 break;
1792 isl_union_set_free(uset);
1793 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, cond);
1794 if (type < pet_expr_access_end || !expr->acc.index)
1795 return pet_expr_free(expr);
1797 return expr;
1798 error:
1799 isl_set_free(cond);
1800 return pet_expr_free(expr);
1803 /* Modify the access relations (if any) and index expression
1804 * of the given access expression
1805 * based on the given iteration space transformation.
1806 * In particular, precompose the access relation and index expression
1807 * with the update function.
1809 * If the access has any arguments then the domain of the access relation
1810 * is a wrapped mapping from the iteration space to the space of
1811 * argument values. We only need to change the domain of this wrapped
1812 * mapping, so we extend the input transformation with an identity mapping
1813 * on the space of argument values.
1815 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1816 __isl_keep isl_multi_pw_aff *update)
1818 enum pet_expr_access_type type;
1820 expr = pet_expr_cow(expr);
1821 if (!expr)
1822 return NULL;
1823 if (expr->type != pet_expr_access)
1824 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1825 "not an access expression", return pet_expr_free(expr));
1827 update = isl_multi_pw_aff_copy(update);
1829 if (expr->n_arg > 0) {
1830 isl_space *space;
1831 isl_multi_pw_aff *id;
1833 space = isl_multi_pw_aff_get_space(expr->acc.index);
1834 space = isl_space_domain(space);
1835 space = isl_space_unwrap(space);
1836 space = isl_space_range(space);
1837 space = isl_space_map_from_set(space);
1838 id = isl_multi_pw_aff_identity(space);
1839 update = isl_multi_pw_aff_product(update, id);
1842 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1843 if (!expr->acc.access[type])
1844 continue;
1845 expr->acc.access[type] =
1846 isl_union_map_preimage_domain_multi_pw_aff(
1847 expr->acc.access[type],
1848 isl_multi_pw_aff_copy(update));
1849 if (!expr->acc.access[type])
1850 break;
1852 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1853 expr->acc.index, update);
1854 if (type < pet_expr_access_end || !expr->acc.index)
1855 return pet_expr_free(expr);
1857 return expr;
1860 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1862 isl_multi_pw_aff *update = user;
1864 return pet_expr_access_update_domain(expr, update);
1867 /* Modify all access relations in "expr" by precomposing them with
1868 * the given iteration space transformation.
1870 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1871 __isl_take isl_multi_pw_aff *update)
1873 expr = pet_expr_map_access(expr, &update_domain, update);
1874 isl_multi_pw_aff_free(update);
1875 return expr;
1878 /* Given an expression with accesses that have a 0D anonymous domain,
1879 * replace those domains by "space".
1881 __isl_give pet_expr *pet_expr_insert_domain(__isl_take pet_expr *expr,
1882 __isl_take isl_space *space)
1884 isl_multi_pw_aff *mpa;
1886 space = isl_space_from_domain(space);
1887 mpa = isl_multi_pw_aff_zero(space);
1888 return pet_expr_update_domain(expr, mpa);
1891 /* Add all parameters in "space" to the access relations (if any)
1892 * and index expression of "expr".
1894 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1896 isl_space *space = user;
1897 enum pet_expr_access_type type;
1899 expr = pet_expr_cow(expr);
1900 if (!expr)
1901 return NULL;
1902 if (expr->type != pet_expr_access)
1903 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1904 "not an access expression", return pet_expr_free(expr));
1906 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1907 if (!expr->acc.access[type])
1908 continue;
1909 expr->acc.access[type] =
1910 isl_union_map_align_params(expr->acc.access[type],
1911 isl_space_copy(space));
1912 if (!expr->acc.access[type])
1913 break;
1915 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1916 isl_space_copy(space));
1917 if (type < pet_expr_access_end || !expr->acc.index)
1918 return pet_expr_free(expr);
1920 return expr;
1923 /* Add all parameters in "space" to all access relations and index expressions
1924 * in "expr".
1926 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1927 __isl_take isl_space *space)
1929 expr = pet_expr_map_access(expr, &align_params, space);
1930 isl_space_free(space);
1931 return expr;
1934 /* Insert an argument expression corresponding to "test" in front
1935 * of the list of arguments described by *n_arg and *args.
1937 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1938 __isl_keep isl_multi_pw_aff *test)
1940 int i;
1941 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1943 if (!test)
1944 return pet_expr_free(expr);
1945 expr = pet_expr_cow(expr);
1946 if (!expr)
1947 return NULL;
1949 if (!expr->args) {
1950 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1951 if (!expr->args)
1952 return pet_expr_free(expr);
1953 } else {
1954 pet_expr **ext;
1955 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1956 if (!ext)
1957 return pet_expr_free(expr);
1958 for (i = 0; i < expr->n_arg; ++i)
1959 ext[1 + i] = expr->args[i];
1960 free(expr->args);
1961 expr->args = ext;
1963 expr->n_arg++;
1964 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1965 if (!expr->args[0])
1966 return pet_expr_free(expr);
1968 return expr;
1971 /* Make the expression "expr" depend on the value of "test"
1972 * being equal to "satisfied".
1974 * If "test" is an affine expression, we simply add the conditions
1975 * on the expression having the value "satisfied" to all access relations
1976 * (introducing access relations if they are missing) and index expressions.
1978 * Otherwise, we add a filter to "expr" (which is then assumed to be
1979 * an access expression) corresponding to "test" being equal to "satisfied".
1981 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
1982 __isl_take isl_multi_pw_aff *test, int satisfied)
1984 isl_id *id;
1985 isl_ctx *ctx;
1986 isl_space *space;
1987 isl_pw_multi_aff *pma;
1988 enum pet_expr_access_type type;
1990 expr = pet_expr_cow(expr);
1991 if (!expr || !test)
1992 goto error;
1994 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
1995 isl_pw_aff *pa;
1996 isl_set *cond;
1998 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
1999 isl_multi_pw_aff_free(test);
2000 if (satisfied)
2001 cond = isl_pw_aff_non_zero_set(pa);
2002 else
2003 cond = isl_pw_aff_zero_set(pa);
2004 return pet_expr_restrict(expr, cond);
2007 ctx = isl_multi_pw_aff_get_ctx(test);
2008 if (expr->type != pet_expr_access)
2009 isl_die(ctx, isl_error_invalid,
2010 "can only filter access expressions", goto error);
2012 expr = introduce_access_relations(expr);
2013 if (!expr)
2014 goto error;
2016 space = isl_space_domain(isl_multi_pw_aff_get_space(expr->acc.index));
2017 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2018 pma = pet_filter_insert_pma(space, id, satisfied);
2020 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
2021 if (!expr->acc.access[type])
2022 continue;
2023 expr->acc.access[type] =
2024 isl_union_map_preimage_domain_pw_multi_aff(
2025 expr->acc.access[type],
2026 isl_pw_multi_aff_copy(pma));
2027 if (!expr->acc.access[type])
2028 break;
2030 pma = isl_pw_multi_aff_gist(pma,
2031 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma)));
2032 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
2033 expr->acc.index, pma);
2034 if (type < pet_expr_access_end || !expr->acc.index)
2035 goto error;
2037 expr = insert_access_arg(expr, test);
2039 isl_multi_pw_aff_free(test);
2040 return expr;
2041 error:
2042 isl_multi_pw_aff_free(test);
2043 return pet_expr_free(expr);
2046 /* Add a reference identifier to access expression "expr".
2047 * "user" points to an integer that contains the sequence number
2048 * of the next reference.
2050 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
2051 void *user)
2053 isl_ctx *ctx;
2054 char name[50];
2055 int *n_ref = user;
2057 expr = pet_expr_cow(expr);
2058 if (!expr)
2059 return expr;
2060 if (expr->type != pet_expr_access)
2061 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2062 "not an access expression", return pet_expr_free(expr));
2064 ctx = pet_expr_get_ctx(expr);
2065 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
2066 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
2067 if (!expr->acc.ref_id)
2068 return pet_expr_free(expr);
2070 return expr;
2073 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
2075 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
2078 /* Reset the user pointer on all parameter and tuple ids in
2079 * the access relations (if any) and the index expression
2080 * of the access expression "expr".
2082 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
2083 void *user)
2085 enum pet_expr_access_type type;
2087 expr = pet_expr_cow(expr);
2088 if (!expr)
2089 return expr;
2090 if (expr->type != pet_expr_access)
2091 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2092 "not an access expression", return pet_expr_free(expr));
2094 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
2095 if (!expr->acc.access[type])
2096 continue;
2097 expr->acc.access[type] =
2098 isl_union_map_reset_user(expr->acc.access[type]);
2099 if (!expr->acc.access[type])
2100 break;
2102 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
2103 if (type < pet_expr_access_end || !expr->acc.index)
2104 return pet_expr_free(expr);
2106 return expr;
2109 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
2111 return pet_expr_map_access(expr, &access_anonymize, NULL);
2114 /* Data used in access_gist() callback.
2116 struct pet_access_gist_data {
2117 isl_set *domain;
2118 isl_union_map *value_bounds;
2121 /* Given an expression "expr" of type pet_expr_access, compute
2122 * the gist of the associated access relations (if any) and index expression
2123 * with respect to data->domain and the bounds on the values of the arguments
2124 * of the expression.
2126 * The arguments of "expr" have been gisted right before "expr" itself
2127 * is gisted. The gisted arguments may have become equal where before
2128 * they may not have been (obviously) equal. We therefore take
2129 * the opportunity to remove duplicate arguments here.
2131 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
2133 struct pet_access_gist_data *data = user;
2134 isl_set *domain;
2135 isl_union_set *uset;
2136 enum pet_expr_access_type type;
2138 expr = pet_expr_remove_duplicate_args(expr);
2139 expr = pet_expr_cow(expr);
2140 if (!expr)
2141 return expr;
2142 if (expr->type != pet_expr_access)
2143 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2144 "not an access expression", return pet_expr_free(expr));
2146 domain = isl_set_copy(data->domain);
2147 if (expr->n_arg > 0)
2148 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
2149 data->value_bounds);
2151 uset = isl_union_set_from_set(isl_set_copy(domain));
2152 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
2153 if (!expr->acc.access[type])
2154 continue;
2155 expr->acc.access[type] =
2156 isl_union_map_gist_domain(expr->acc.access[type],
2157 isl_union_set_copy(uset));
2158 if (!expr->acc.access[type])
2159 break;
2161 isl_union_set_free(uset);
2162 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
2163 if (type < pet_expr_access_end || !expr->acc.index)
2164 return pet_expr_free(expr);
2166 return expr;
2169 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
2170 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2172 struct pet_access_gist_data data = { context, value_bounds };
2174 return pet_expr_map_access(expr, &access_gist, &data);
2177 /* Mark "expr" as a read dependening on "read".
2179 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
2180 int read)
2182 if (!expr)
2183 return pet_expr_free(expr);
2184 if (expr->type != pet_expr_access)
2185 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2186 "not an access expression", return pet_expr_free(expr));
2187 if (expr->acc.read == read)
2188 return expr;
2189 expr = pet_expr_cow(expr);
2190 if (!expr)
2191 return NULL;
2192 expr->acc.read = read;
2194 return expr;
2197 /* Mark "expr" as a write dependening on "write".
2199 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
2200 int write)
2202 if (!expr)
2203 return pet_expr_free(expr);
2204 if (expr->type != pet_expr_access)
2205 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2206 "not an access expression", return pet_expr_free(expr));
2207 if (expr->acc.write == write)
2208 return expr;
2209 expr = pet_expr_cow(expr);
2210 if (!expr)
2211 return NULL;
2212 expr->acc.write = write;
2214 return expr;
2217 /* Mark "expr" as a kill dependening on "kill".
2219 __isl_give pet_expr *pet_expr_access_set_kill(__isl_take pet_expr *expr,
2220 int kill)
2222 if (!expr)
2223 return pet_expr_free(expr);
2224 if (expr->type != pet_expr_access)
2225 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2226 "not an access expression", return pet_expr_free(expr));
2227 if (expr->acc.kill == kill)
2228 return expr;
2229 expr = pet_expr_cow(expr);
2230 if (!expr)
2231 return NULL;
2232 expr->acc.kill = kill;
2234 return expr;
2237 /* Map the access type "type" to the corresponding location
2238 * in the access array.
2239 * In particular, the access relation of type pet_expr_access_killed is
2240 * stored in the element at position pet_expr_access_fake_killed.
2242 static enum pet_expr_access_type internalize_type(
2243 enum pet_expr_access_type type)
2245 if (type == pet_expr_access_killed)
2246 return pet_expr_access_fake_killed;
2247 return type;
2250 /* Replace the access relation of the given "type" of "expr" by "access".
2251 * If the access relation is non-empty and the type is a read or a write,
2252 * then also mark the access expression itself as a read or a write.
2254 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
2255 enum pet_expr_access_type type, __isl_take isl_union_map *access)
2257 int empty;
2259 expr = pet_expr_cow(expr);
2260 if (!expr || !access)
2261 goto error;
2262 if (expr->type != pet_expr_access)
2263 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2264 "not an access expression", goto error);
2265 type = internalize_type(type);
2266 isl_union_map_free(expr->acc.access[type]);
2267 expr->acc.access[type] = access;
2269 if (expr->acc.kill)
2270 return expr;
2272 empty = isl_union_map_is_empty(access);
2273 if (empty < 0)
2274 return pet_expr_free(expr);
2275 if (empty)
2276 return expr;
2278 if (type == pet_expr_access_may_read)
2279 expr = pet_expr_access_set_read(expr, 1);
2280 else
2281 expr = pet_expr_access_set_write(expr, 1);
2283 return expr;
2284 error:
2285 isl_union_map_free(access);
2286 pet_expr_free(expr);
2287 return NULL;
2290 /* Replace the index expression of "expr" by "index" and
2291 * set the array depth accordingly.
2293 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
2294 __isl_take isl_multi_pw_aff *index)
2296 expr = pet_expr_cow(expr);
2297 if (!expr || !index)
2298 goto error;
2299 if (expr->type != pet_expr_access)
2300 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2301 "not an access expression", goto error);
2302 isl_multi_pw_aff_free(expr->acc.index);
2303 expr->acc.index = index;
2304 expr->acc.depth = isl_multi_pw_aff_dim(index, isl_dim_out);
2306 return expr;
2307 error:
2308 isl_multi_pw_aff_free(index);
2309 pet_expr_free(expr);
2310 return NULL;
2313 /* Return the reference identifier of access expression "expr".
2315 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
2317 if (!expr)
2318 return NULL;
2319 if (expr->type != pet_expr_access)
2320 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2321 "not an access expression", return NULL);
2323 return isl_id_copy(expr->acc.ref_id);
2326 /* Replace the reference identifier of access expression "expr" by "ref_id".
2328 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
2329 __isl_take isl_id *ref_id)
2331 expr = pet_expr_cow(expr);
2332 if (!expr || !ref_id)
2333 goto error;
2334 if (expr->type != pet_expr_access)
2335 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2336 "not an access expression", goto error);
2337 isl_id_free(expr->acc.ref_id);
2338 expr->acc.ref_id = ref_id;
2340 return expr;
2341 error:
2342 isl_id_free(ref_id);
2343 pet_expr_free(expr);
2344 return NULL;
2347 /* Tag the access relation "access" with "id".
2348 * That is, insert the id as the range of a wrapped relation
2349 * in the domain of "access".
2351 * If "access" is of the form
2353 * D[i] -> A[a]
2355 * then the result is of the form
2357 * [D[i] -> id[]] -> A[a]
2359 __isl_give isl_union_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
2360 __isl_take isl_union_map *access)
2362 isl_space *space;
2363 isl_multi_aff *add_tag;
2364 isl_id *id;
2366 if (expr->type != pet_expr_access)
2367 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2368 "not an access expression",
2369 return isl_union_map_free(access));
2371 id = isl_id_copy(expr->acc.ref_id);
2372 space = pet_expr_access_get_domain_space(expr);
2373 space = isl_space_from_domain(space);
2374 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2375 add_tag = isl_multi_aff_domain_map(space);
2376 access = isl_union_map_preimage_domain_multi_aff(access, add_tag);
2378 return access;
2381 /* Return the access relation of the given "type" associated to "expr"
2382 * that maps pairs of domain iterations and argument values
2383 * to the corresponding accessed data elements.
2385 * If the requested access relation is explicitly available,
2386 * then return a copy. Otherwise, check if it is irrelevant for
2387 * the access expression and return an empty relation if this is the case.
2388 * Otherwise, introduce the requested access relation in "expr" and
2389 * return a copy.
2391 __isl_give isl_union_map *pet_expr_access_get_dependent_access(
2392 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2394 isl_union_map *access;
2395 int empty;
2397 if (!expr)
2398 return NULL;
2399 if (expr->type != pet_expr_access)
2400 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2401 "not an access expression", return NULL);
2403 type = internalize_type(type);
2404 if (expr->acc.access[type])
2405 return isl_union_map_copy(expr->acc.access[type]);
2407 if (type == pet_expr_access_may_read)
2408 empty = !expr->acc.read;
2409 else
2410 empty = !expr->acc.write;
2412 if (!empty) {
2413 expr = pet_expr_copy(expr);
2414 expr = introduce_access_relations(expr);
2415 if (!expr)
2416 return NULL;
2417 access = isl_union_map_copy(expr->acc.access[type]);
2418 pet_expr_free(expr);
2420 return access;
2423 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr));
2426 /* Return the may read access relation associated to "expr"
2427 * that maps pairs of domain iterations and argument values
2428 * to the corresponding accessed data elements.
2430 __isl_give isl_union_map *pet_expr_access_get_dependent_may_read(
2431 __isl_keep pet_expr *expr)
2433 return pet_expr_access_get_dependent_access(expr,
2434 pet_expr_access_may_read);
2437 /* Return the may write access relation associated to "expr"
2438 * that maps pairs of domain iterations and argument values
2439 * to the corresponding accessed data elements.
2441 __isl_give isl_union_map *pet_expr_access_get_dependent_may_write(
2442 __isl_keep pet_expr *expr)
2444 return pet_expr_access_get_dependent_access(expr,
2445 pet_expr_access_may_write);
2448 /* Return the must write access relation associated to "expr"
2449 * that maps pairs of domain iterations and argument values
2450 * to the corresponding accessed data elements.
2452 __isl_give isl_union_map *pet_expr_access_get_dependent_must_write(
2453 __isl_keep pet_expr *expr)
2455 return pet_expr_access_get_dependent_access(expr,
2456 pet_expr_access_must_write);
2459 /* Return the relation of the given "type" mapping domain iterations
2460 * to the accessed data elements.
2461 * In particular, take the access relation and, in case of may_read
2462 * or may_write, project out the values of the arguments, if any.
2463 * In case of must_write, return the empty relation if there are
2464 * any arguments.
2466 __isl_give isl_union_map *pet_expr_access_get_access(__isl_keep pet_expr *expr,
2467 enum pet_expr_access_type type)
2469 isl_union_map *access;
2470 isl_space *space;
2471 isl_map *map;
2473 if (!expr)
2474 return NULL;
2475 if (expr->type != pet_expr_access)
2476 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2477 "not an access expression", return NULL);
2479 if (expr->n_arg != 0 && type == pet_expr_access_must_write) {
2480 space = pet_expr_access_get_parameter_space(expr);
2481 return isl_union_map_empty(space);
2484 access = pet_expr_access_get_dependent_access(expr, type);
2485 if (expr->n_arg == 0)
2486 return access;
2488 space = isl_multi_pw_aff_get_space(expr->acc.index);
2489 space = isl_space_domain(space);
2490 map = isl_map_universe(isl_space_unwrap(space));
2491 map = isl_map_domain_map(map);
2492 access = isl_union_map_apply_domain(access,
2493 isl_union_map_from_map(map));
2495 return access;
2498 /* Return the relation mapping domain iterations to all possibly
2499 * read data elements.
2501 __isl_give isl_union_map *pet_expr_access_get_may_read(
2502 __isl_keep pet_expr *expr)
2504 return pet_expr_access_get_access(expr, pet_expr_access_may_read);
2507 /* Return the relation mapping domain iterations to all possibly
2508 * written data elements.
2510 __isl_give isl_union_map *pet_expr_access_get_may_write(
2511 __isl_keep pet_expr *expr)
2513 return pet_expr_access_get_access(expr, pet_expr_access_may_write);
2516 /* Return a relation mapping domain iterations to definitely
2517 * written data elements, assuming the statement containing
2518 * the expression is executed.
2520 __isl_give isl_union_map *pet_expr_access_get_must_write(
2521 __isl_keep pet_expr *expr)
2523 return pet_expr_access_get_access(expr, pet_expr_access_must_write);
2526 /* Return the relation of the given "type" mapping domain iterations to
2527 * accessed data elements, with its domain tagged with the reference
2528 * identifier.
2530 static __isl_give isl_union_map *pet_expr_access_get_tagged_access(
2531 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2533 isl_union_map *access;
2535 if (!expr)
2536 return NULL;
2538 access = pet_expr_access_get_access(expr, type);
2539 access = pet_expr_tag_access(expr, access);
2541 return access;
2544 /* Return the relation mapping domain iterations to all possibly
2545 * read data elements, with its domain tagged with the reference
2546 * identifier.
2548 __isl_give isl_union_map *pet_expr_access_get_tagged_may_read(
2549 __isl_keep pet_expr *expr)
2551 return pet_expr_access_get_tagged_access(expr,
2552 pet_expr_access_may_read);
2555 /* Return the relation mapping domain iterations to all possibly
2556 * written data elements, with its domain tagged with the reference
2557 * identifier.
2559 __isl_give isl_union_map *pet_expr_access_get_tagged_may_write(
2560 __isl_keep pet_expr *expr)
2562 return pet_expr_access_get_tagged_access(expr,
2563 pet_expr_access_may_write);
2566 /* Return the operation type of operation expression "expr".
2568 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
2570 if (!expr)
2571 return pet_op_last;
2572 if (expr->type != pet_expr_op)
2573 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2574 "not an operation expression", return pet_op_last);
2576 return expr->op;
2579 /* Replace the operation type of operation expression "expr" by "type".
2581 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
2582 enum pet_op_type type)
2584 if (!expr)
2585 return pet_expr_free(expr);
2586 if (expr->type != pet_expr_op)
2587 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2588 "not an operation expression",
2589 return pet_expr_free(expr));
2590 if (expr->op == type)
2591 return expr;
2592 expr = pet_expr_cow(expr);
2593 if (!expr)
2594 return NULL;
2595 expr->op = type;
2597 return expr;
2600 /* Return the name of the function called by "expr".
2602 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
2604 if (!expr)
2605 return NULL;
2606 if (expr->type != pet_expr_call)
2607 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2608 "not a call expression", return NULL);
2609 return expr->c.name;
2612 /* Replace the name of the function called by "expr" by "name".
2614 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
2615 __isl_keep const char *name)
2617 expr = pet_expr_cow(expr);
2618 if (!expr || !name)
2619 return pet_expr_free(expr);
2620 if (expr->type != pet_expr_call)
2621 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2622 "not a call expression", return pet_expr_free(expr));
2623 free(expr->c.name);
2624 expr->c.name = strdup(name);
2625 if (!expr->c.name)
2626 return pet_expr_free(expr);
2627 return expr;
2630 /* Does the call expression "expr" have an associated function summary?
2632 int pet_expr_call_has_summary(__isl_keep pet_expr *expr)
2634 if (!expr)
2635 return -1;
2636 if (expr->type != pet_expr_call)
2637 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2638 "not a call expression", return -1);
2640 return expr->c.summary != NULL;
2643 /* Return a copy of the function summary associated to
2644 * the call expression "expr".
2646 __isl_give pet_function_summary *pet_expr_call_get_summary(
2647 __isl_keep pet_expr *expr)
2649 if (!expr)
2650 return NULL;
2651 if (expr->type != pet_expr_call)
2652 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2653 "not a call expression", return NULL);
2655 return pet_function_summary_copy(expr->c.summary);
2658 /* Replace the function summary associated to the call expression "expr"
2659 * by "summary".
2661 __isl_give pet_expr *pet_expr_call_set_summary(__isl_take pet_expr *expr,
2662 __isl_take pet_function_summary *summary)
2664 expr = pet_expr_cow(expr);
2665 if (!expr || !summary)
2666 goto error;
2667 if (expr->type != pet_expr_call)
2668 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2669 "not a call expression", goto error);
2670 pet_function_summary_free(expr->c.summary);
2671 expr->c.summary = summary;
2672 return expr;
2673 error:
2674 pet_function_summary_free(summary);
2675 return pet_expr_free(expr);
2678 /* Replace the type of the cast performed by "expr" by "name".
2680 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
2681 __isl_keep const char *name)
2683 expr = pet_expr_cow(expr);
2684 if (!expr || !name)
2685 return pet_expr_free(expr);
2686 if (expr->type != pet_expr_cast)
2687 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2688 "not a cast expression", return pet_expr_free(expr));
2689 free(expr->type_name);
2690 expr->type_name = strdup(name);
2691 if (!expr->type_name)
2692 return pet_expr_free(expr);
2693 return expr;
2696 /* Return the value of the integer represented by "expr".
2698 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
2700 if (!expr)
2701 return NULL;
2702 if (expr->type != pet_expr_int)
2703 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2704 "not an int expression", return NULL);
2706 return isl_val_copy(expr->i);
2709 /* Replace the value of the integer represented by "expr" by "v".
2711 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
2712 __isl_take isl_val *v)
2714 expr = pet_expr_cow(expr);
2715 if (!expr || !v)
2716 goto error;
2717 if (expr->type != pet_expr_int)
2718 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2719 "not an int expression", goto error);
2720 isl_val_free(expr->i);
2721 expr->i = v;
2723 return expr;
2724 error:
2725 isl_val_free(v);
2726 pet_expr_free(expr);
2727 return NULL;
2730 /* Replace the value and string representation of the double
2731 * represented by "expr" by "d" and "s".
2733 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2734 double d, __isl_keep const char *s)
2736 expr = pet_expr_cow(expr);
2737 if (!expr || !s)
2738 return pet_expr_free(expr);
2739 if (expr->type != pet_expr_double)
2740 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2741 "not a double expression", return pet_expr_free(expr));
2742 expr->d.val = d;
2743 free(expr->d.s);
2744 expr->d.s = strdup(s);
2745 if (!expr->d.s)
2746 return pet_expr_free(expr);
2747 return expr;
2750 /* Return a string representation of the double expression "expr".
2752 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2754 if (!expr)
2755 return NULL;
2756 if (expr->type != pet_expr_double)
2757 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2758 "not a double expression", return NULL);
2759 return strdup(expr->d.s);
2762 /* Return a piecewise affine expression defined on the specified domain
2763 * that represents NaN.
2765 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2767 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2770 /* This function is called when we come across an access that is
2771 * nested in what is supposed to be an affine expression.
2772 * "pc" is the context in which the affine expression is created.
2773 * If nesting is allowed in "pc", we return an affine expression that is
2774 * equal to a new parameter corresponding to this nested access.
2775 * Otherwise, we return NaN.
2777 * Note that we currently don't allow nested accesses themselves
2778 * to contain any nested accesses, so we check if "expr" itself
2779 * involves any nested accesses (either explicitly as arguments
2780 * or implicitly through parameters) and return NaN if it does.
2782 * The new parameter is resolved in resolve_nested.
2784 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2785 __isl_keep pet_context *pc)
2787 isl_ctx *ctx;
2788 isl_id *id;
2789 isl_space *space;
2790 isl_local_space *ls;
2791 isl_aff *aff;
2792 int nested;
2794 if (!expr || !pc)
2795 return NULL;
2796 if (!pet_context_allow_nesting(pc))
2797 return non_affine(pet_context_get_space(pc));
2799 if (pet_expr_get_type(expr) != pet_expr_access)
2800 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2801 "not an access expression", return NULL);
2803 if (expr->n_arg > 0)
2804 return non_affine(pet_context_get_space(pc));
2806 space = pet_expr_access_get_parameter_space(expr);
2807 nested = pet_nested_any_in_space(space);
2808 isl_space_free(space);
2809 if (nested)
2810 return non_affine(pet_context_get_space(pc));
2812 ctx = pet_expr_get_ctx(expr);
2813 id = pet_nested_pet_expr(pet_expr_copy(expr));
2814 space = pet_context_get_space(pc);
2815 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2817 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2818 ls = isl_local_space_from_space(space);
2819 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2821 return isl_pw_aff_from_aff(aff);
2824 /* Extract an affine expression from the access pet_expr "expr".
2825 * "pc" is the context in which the affine expression is created.
2827 * If "expr" is actually an affine expression rather than
2828 * a real access, then we return that expression.
2829 * Otherwise, we require that "expr" is of an integral type.
2830 * If not, we return NaN.
2832 * If the variable has been assigned a known affine expression,
2833 * then we return that expression.
2835 * Otherwise, we return an expression that is equal to a parameter
2836 * representing "expr" (if "allow_nested" is set).
2838 static __isl_give isl_pw_aff *extract_affine_from_access(
2839 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2841 int pos;
2842 isl_id *id;
2844 if (pet_expr_is_affine(expr))
2845 return pet_expr_get_affine(expr);
2847 if (pet_expr_get_type_size(expr) == 0)
2848 return non_affine(pet_context_get_space(pc));
2850 if (!pet_expr_is_scalar_access(expr))
2851 return nested_access(expr, pc);
2853 id = pet_expr_access_get_id(expr);
2854 if (pet_context_is_assigned(pc, id))
2855 return pet_context_get_value(pc, id);
2857 isl_id_free(id);
2858 return nested_access(expr, pc);
2861 /* Construct an affine expression from the integer constant "expr".
2862 * "pc" is the context in which the affine expression is created.
2864 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2865 __isl_keep pet_context *pc)
2867 isl_local_space *ls;
2868 isl_aff *aff;
2870 if (!expr)
2871 return NULL;
2873 ls = isl_local_space_from_space(pet_context_get_space(pc));
2874 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2876 return isl_pw_aff_from_aff(aff);
2879 /* Extract an affine expression from an addition or subtraction operation.
2880 * Return NaN if we are unable to extract an affine expression.
2882 * "pc" is the context in which the affine expression is created.
2884 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2885 __isl_keep pet_context *pc)
2887 isl_pw_aff *lhs;
2888 isl_pw_aff *rhs;
2890 if (!expr)
2891 return NULL;
2892 if (expr->n_arg != 2)
2893 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2894 "expecting two arguments", return NULL);
2896 lhs = pet_expr_extract_affine(expr->args[0], pc);
2897 rhs = pet_expr_extract_affine(expr->args[1], pc);
2899 switch (pet_expr_op_get_type(expr)) {
2900 case pet_op_add:
2901 return isl_pw_aff_add(lhs, rhs);
2902 case pet_op_sub:
2903 return isl_pw_aff_sub(lhs, rhs);
2904 default:
2905 isl_pw_aff_free(lhs);
2906 isl_pw_aff_free(rhs);
2907 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2908 "not an addition or subtraction operation",
2909 return NULL);
2914 /* Extract an affine expression from an integer division or a modulo operation.
2915 * Return NaN if we are unable to extract an affine expression.
2917 * "pc" is the context in which the affine expression is created.
2919 * In particular, if "expr" is lhs/rhs, then return
2921 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2923 * If "expr" is lhs%rhs, then return
2925 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2927 * If the second argument (rhs) is not a (positive) integer constant,
2928 * then we fail to extract an affine expression.
2930 * We simplify the result in the context of the domain of "pc" in case
2931 * this domain implies that lhs >= 0 (or < 0).
2933 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2934 __isl_keep pet_context *pc)
2936 int is_cst;
2937 isl_pw_aff *lhs;
2938 isl_pw_aff *rhs;
2939 isl_pw_aff *res;
2941 if (!expr)
2942 return NULL;
2943 if (expr->n_arg != 2)
2944 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2945 "expecting two arguments", return NULL);
2947 rhs = pet_expr_extract_affine(expr->args[1], pc);
2949 is_cst = isl_pw_aff_is_cst(rhs);
2950 if (is_cst < 0 || !is_cst) {
2951 isl_pw_aff_free(rhs);
2952 return non_affine(pet_context_get_space(pc));
2955 lhs = pet_expr_extract_affine(expr->args[0], pc);
2957 switch (pet_expr_op_get_type(expr)) {
2958 case pet_op_div:
2959 res = isl_pw_aff_tdiv_q(lhs, rhs);
2960 break;
2961 case pet_op_mod:
2962 res = isl_pw_aff_tdiv_r(lhs, rhs);
2963 break;
2964 default:
2965 isl_pw_aff_free(lhs);
2966 isl_pw_aff_free(rhs);
2967 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2968 "not a div or mod operator", return NULL);
2971 return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc));
2974 /* Extract an affine expression from a multiplication operation.
2975 * Return NaN if we are unable to extract an affine expression.
2976 * In particular, if neither of the arguments is a (piecewise) constant
2977 * then we return NaN.
2979 * "pc" is the context in which the affine expression is created.
2981 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2982 __isl_keep pet_context *pc)
2984 int lhs_cst, rhs_cst;
2985 isl_pw_aff *lhs;
2986 isl_pw_aff *rhs;
2988 if (!expr)
2989 return NULL;
2990 if (expr->n_arg != 2)
2991 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2992 "expecting two arguments", return NULL);
2994 lhs = pet_expr_extract_affine(expr->args[0], pc);
2995 rhs = pet_expr_extract_affine(expr->args[1], pc);
2997 lhs_cst = isl_pw_aff_is_cst(lhs);
2998 rhs_cst = isl_pw_aff_is_cst(rhs);
2999 if (lhs_cst >= 0 && rhs_cst >= 0 && (lhs_cst || rhs_cst))
3000 return isl_pw_aff_mul(lhs, rhs);
3002 isl_pw_aff_free(lhs);
3003 isl_pw_aff_free(rhs);
3005 if (lhs_cst < 0 || rhs_cst < 0)
3006 return NULL;
3008 return non_affine(pet_context_get_space(pc));
3011 /* Extract an affine expression from a negation operation.
3012 * Return NaN if we are unable to extract an affine expression.
3014 * "pc" is the context in which the affine expression is created.
3016 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
3017 __isl_keep pet_context *pc)
3019 isl_pw_aff *res;
3021 if (!expr)
3022 return NULL;
3023 if (expr->n_arg != 1)
3024 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3025 "expecting one argument", return NULL);
3027 res = pet_expr_extract_affine(expr->args[0], pc);
3028 return isl_pw_aff_neg(res);
3031 /* Extract an affine expression from a conditional operation.
3032 * Return NaN if we are unable to extract an affine expression.
3034 * "pc" is the context in which the affine expression is created.
3036 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
3037 __isl_keep pet_context *pc)
3039 isl_pw_aff *cond, *lhs, *rhs;
3041 if (!expr)
3042 return NULL;
3043 if (expr->n_arg != 3)
3044 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3045 "expecting three arguments", return NULL);
3047 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3048 lhs = pet_expr_extract_affine(expr->args[1], pc);
3049 rhs = pet_expr_extract_affine(expr->args[2], pc);
3051 return isl_pw_aff_cond(cond, lhs, rhs);
3054 /* Limit the domain of "pwaff" to those elements where the function
3055 * value satisfies
3057 * 2^{width-1} <= pwaff < 2^{width-1}
3059 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
3060 unsigned width)
3062 isl_ctx *ctx;
3063 isl_val *v;
3064 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
3065 isl_local_space *ls = isl_local_space_from_space(space);
3066 isl_aff *bound;
3067 isl_set *dom;
3068 isl_pw_aff *b;
3070 ctx = isl_pw_aff_get_ctx(pwaff);
3071 v = isl_val_int_from_ui(ctx, width - 1);
3072 v = isl_val_2exp(v);
3074 bound = isl_aff_zero_on_domain(ls);
3075 bound = isl_aff_add_constant_val(bound, v);
3076 b = isl_pw_aff_from_aff(bound);
3078 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
3079 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3081 b = isl_pw_aff_neg(b);
3082 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
3083 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3085 return pwaff;
3088 /* Handle potential overflows on signed computations.
3090 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
3091 * then we adjust the domain of "pa" to avoid overflows.
3093 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
3094 unsigned width)
3096 isl_ctx *ctx;
3097 struct pet_options *options;
3099 if (!pa)
3100 return NULL;
3102 ctx = isl_pw_aff_get_ctx(pa);
3103 options = isl_ctx_peek_pet_options(ctx);
3104 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
3105 pa = avoid_overflow(pa, width);
3107 return pa;
3110 /* Extract an affine expression from some an operation.
3111 * Return NaN if we are unable to extract an affine expression.
3112 * If the result of a binary (non boolean) operation is unsigned,
3113 * then we wrap it based on the size of the type. If the result is signed,
3114 * then we ensure that no overflow occurs.
3116 * "pc" is the context in which the affine expression is created.
3118 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
3119 __isl_keep pet_context *pc)
3121 isl_pw_aff *res;
3122 int type_size;
3124 switch (pet_expr_op_get_type(expr)) {
3125 case pet_op_add:
3126 case pet_op_sub:
3127 res = extract_affine_add_sub(expr, pc);
3128 break;
3129 case pet_op_div:
3130 case pet_op_mod:
3131 res = extract_affine_div_mod(expr, pc);
3132 break;
3133 case pet_op_mul:
3134 res = extract_affine_mul(expr, pc);
3135 break;
3136 case pet_op_minus:
3137 return extract_affine_neg(expr, pc);
3138 case pet_op_cond:
3139 return extract_affine_cond(expr, pc);
3140 case pet_op_eq:
3141 case pet_op_ne:
3142 case pet_op_le:
3143 case pet_op_ge:
3144 case pet_op_lt:
3145 case pet_op_gt:
3146 case pet_op_land:
3147 case pet_op_lor:
3148 case pet_op_lnot:
3149 return pet_expr_extract_affine_condition(expr, pc);
3150 default:
3151 return non_affine(pet_context_get_space(pc));
3154 if (!res)
3155 return NULL;
3156 if (isl_pw_aff_involves_nan(res)) {
3157 isl_space *space = isl_pw_aff_get_domain_space(res);
3158 isl_pw_aff_free(res);
3159 return non_affine(space);
3162 type_size = pet_expr_get_type_size(expr);
3163 if (type_size > 0)
3164 res = pet_wrap_pw_aff(res, type_size);
3165 else
3166 res = signed_overflow(res, -type_size);
3168 return res;
3171 /* Internal data structure for affine builtin function declarations.
3173 * "pencil" is set if the builtin is pencil specific.
3174 * "n_args" is the number of arguments the function takes.
3175 * "name" is the function name.
3177 struct affine_builtin_decl {
3178 int pencil;
3179 int n_args;
3180 const char *name;
3183 static struct affine_builtin_decl affine_builtins[] = {
3184 { 0, 2, "min" },
3185 { 1, 2, "imin" },
3186 { 1, 2, "umin" },
3187 { 0, 2, "max" },
3188 { 1, 2, "imax" },
3189 { 1, 2, "umax" },
3190 { 0, 2, "intMod" },
3191 { 0, 2, "intFloor" },
3192 { 0, 2, "intCeil" },
3193 { 0, 2, "floord" },
3194 { 0, 2, "ceild" }
3197 /* List of min and max builtin functions.
3199 static const char *min_max_builtins[] = {
3200 "min", "imin", "umin",
3201 "max", "imax", "umax"
3204 /* Is a function call to "name" with "n_args" arguments a call to a
3205 * builtin function for which we can construct an affine expression?
3206 * pencil specific builtins are only recognized if "pencil" is set.
3208 static int is_affine_builtin(int pencil, int n_args, const char *name)
3210 int i;
3212 for (i = 0; i < ARRAY_SIZE(affine_builtins); ++i) {
3213 struct affine_builtin_decl *decl = &affine_builtins[i];
3215 if (decl->pencil && !pencil)
3216 continue;
3217 if (decl->n_args == n_args && !strcmp(decl->name, name))
3218 return 1;
3221 return 0;
3224 /* Is function "name" a known min or max builtin function?
3226 static int is_min_or_max_builtin(const char *name)
3228 int i;
3230 for (i = 0; i < ARRAY_SIZE(min_max_builtins); ++i)
3231 if (!strcmp(min_max_builtins[i], name))
3232 return 1;
3234 return 0;
3237 /* Extract an affine expression from some special function calls.
3238 * Return NaN if we are unable to extract an affine expression.
3239 * In particular, we handle "min", "max", "ceild", "floord",
3240 * "intMod", "intFloor" and "intCeil".
3241 * In case of the latter five, the second argument needs to be
3242 * a (positive) integer constant.
3243 * If the pencil option is set, then we also handle "{i,u}min" and
3244 * "{i,u}max".
3246 * "pc" is the context in which the affine expression is created.
3248 static __isl_give isl_pw_aff *extract_affine_from_call(
3249 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3251 isl_ctx *ctx;
3252 isl_pw_aff *aff1, *aff2;
3253 int n;
3254 const char *name;
3255 struct pet_options *options;
3257 if (!expr)
3258 return NULL;
3259 ctx = pet_expr_get_ctx(expr);
3260 options = isl_ctx_peek_pet_options(ctx);
3262 n = pet_expr_get_n_arg(expr);
3263 name = pet_expr_call_get_name(expr);
3264 if (!is_affine_builtin(options->pencil, n, name))
3265 return non_affine(pet_context_get_space(pc));
3267 if (is_min_or_max_builtin(name)) {
3268 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3269 aff2 = pet_expr_extract_affine(expr->args[1], pc);
3271 if (strstr(name, "min"))
3272 aff1 = isl_pw_aff_min(aff1, aff2);
3273 else
3274 aff1 = isl_pw_aff_max(aff1, aff2);
3275 } else if (!strcmp(name, "intMod")) {
3276 isl_val *v;
3278 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3279 return non_affine(pet_context_get_space(pc));
3280 v = pet_expr_int_get_val(expr->args[1]);
3281 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3282 aff1 = isl_pw_aff_mod_val(aff1, v);
3283 } else {
3284 isl_val *v;
3286 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3287 return non_affine(pet_context_get_space(pc));
3288 v = pet_expr_int_get_val(expr->args[1]);
3289 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3290 aff1 = isl_pw_aff_scale_down_val(aff1, v);
3291 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
3292 aff1 = isl_pw_aff_floor(aff1);
3293 else
3294 aff1 = isl_pw_aff_ceil(aff1);
3297 return aff1;
3300 /* Extract an affine expression from "expr", if possible.
3301 * Otherwise return NaN.
3303 * "pc" is the context in which the affine expression is created.
3305 * Store the result in "pc" such that it can be reused in case
3306 * pet_expr_extract_affine is called again on the same pair of
3307 * "expr" and "pc".
3309 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
3310 __isl_keep pet_context *pc)
3312 isl_maybe_isl_pw_aff m;
3313 isl_pw_aff *pa;
3315 if (!expr)
3316 return NULL;
3318 m = pet_context_get_extracted_affine(pc, expr);
3319 if (m.valid < 0 || m.valid)
3320 return m.value;
3322 switch (pet_expr_get_type(expr)) {
3323 case pet_expr_access:
3324 pa = extract_affine_from_access(expr, pc);
3325 break;
3326 case pet_expr_int:
3327 pa = extract_affine_from_int(expr, pc);
3328 break;
3329 case pet_expr_op:
3330 pa = extract_affine_from_op(expr, pc);
3331 break;
3332 case pet_expr_call:
3333 pa = extract_affine_from_call(expr, pc);
3334 break;
3335 case pet_expr_cast:
3336 case pet_expr_double:
3337 case pet_expr_error:
3338 pa = non_affine(pet_context_get_space(pc));
3339 break;
3342 if (pet_context_set_extracted_affine(pc, expr, pa) < 0)
3343 return isl_pw_aff_free(pa);
3345 return pa;
3348 /* Extract an affine expressions representing the comparison "LHS op RHS"
3349 * Return NaN if we are unable to extract such an affine expression.
3351 * "pc" is the context in which the affine expression is created.
3353 * If the comparison is of the form
3355 * a <= min(b,c)
3357 * then the expression is constructed as the conjunction of
3358 * the comparisons
3360 * a <= b and a <= c
3362 * A similar optimization is performed for max(a,b) <= c.
3363 * We do this because that will lead to simpler representations
3364 * of the expression.
3365 * If isl is ever enhanced to explicitly deal with min and max expressions,
3366 * this optimization can be removed.
3368 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
3369 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
3370 __isl_keep pet_context *pc)
3372 isl_pw_aff *lhs_pa, *rhs_pa;
3374 if (op == pet_op_gt)
3375 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
3376 if (op == pet_op_ge)
3377 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
3379 if (op == pet_op_lt || op == pet_op_le) {
3380 if (pet_expr_is_min(rhs)) {
3381 lhs_pa = pet_expr_extract_comparison(op, lhs,
3382 rhs->args[0], pc);
3383 rhs_pa = pet_expr_extract_comparison(op, lhs,
3384 rhs->args[1], pc);
3385 return pet_and(lhs_pa, rhs_pa);
3387 if (pet_expr_is_max(lhs)) {
3388 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
3389 rhs, pc);
3390 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
3391 rhs, pc);
3392 return pet_and(lhs_pa, rhs_pa);
3396 lhs_pa = pet_expr_extract_affine(lhs, pc);
3397 rhs_pa = pet_expr_extract_affine(rhs, pc);
3399 return pet_comparison(op, lhs_pa, rhs_pa);
3402 /* Extract an affine expressions from the comparison "expr".
3403 * Return NaN if we are unable to extract such an affine expression.
3405 * "pc" is the context in which the affine expression is created.
3407 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
3408 __isl_keep pet_context *pc)
3410 enum pet_op_type type;
3412 if (!expr)
3413 return NULL;
3414 if (expr->n_arg != 2)
3415 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3416 "expecting two arguments", return NULL);
3418 type = pet_expr_op_get_type(expr);
3419 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
3420 pc);
3423 /* Extract an affine expression representing the boolean operation
3424 * expressed by "expr".
3425 * Return NaN if we are unable to extract an affine expression.
3427 * "pc" is the context in which the affine expression is created.
3429 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
3430 __isl_keep pet_context *pc)
3432 isl_pw_aff *lhs, *rhs;
3433 int n;
3435 if (!expr)
3436 return NULL;
3438 n = pet_expr_get_n_arg(expr);
3439 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
3440 if (n == 1)
3441 return pet_not(lhs);
3443 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
3444 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
3447 /* Extract the affine expression "expr != 0 ? 1 : 0".
3448 * Return NaN if we are unable to extract an affine expression.
3450 * "pc" is the context in which the affine expression is created.
3452 static __isl_give isl_pw_aff *extract_implicit_condition(
3453 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3455 isl_pw_aff *res;
3457 res = pet_expr_extract_affine(expr, pc);
3458 return pet_to_bool(res);
3461 /* Extract a boolean affine expression from "expr".
3462 * Return NaN if we are unable to extract an affine expression.
3464 * "pc" is the context in which the affine expression is created.
3466 * If "expr" is neither a comparison nor a boolean operation,
3467 * then we assume it is an affine expression and return the
3468 * boolean expression "expr != 0 ? 1 : 0".
3470 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
3471 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3473 if (!expr)
3474 return NULL;
3476 if (pet_expr_is_comparison(expr))
3477 return extract_comparison(expr, pc);
3478 if (pet_expr_is_boolean(expr))
3479 return extract_boolean(expr, pc);
3481 return extract_implicit_condition(expr, pc);
3484 /* Check if "expr" is an assume expression and if its single argument
3485 * can be converted to an affine expression in the context of "pc".
3486 * If so, replace the argument by the affine expression.
3488 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
3489 __isl_keep pet_context *pc)
3491 isl_pw_aff *cond;
3492 isl_multi_pw_aff *index;
3494 if (!expr)
3495 return NULL;
3496 if (!pet_expr_is_assume(expr))
3497 return expr;
3498 if (expr->n_arg != 1)
3499 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3500 "expecting one argument", return pet_expr_free(expr));
3502 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3503 if (!cond)
3504 return pet_expr_free(expr);
3505 if (isl_pw_aff_involves_nan(cond)) {
3506 isl_pw_aff_free(cond);
3507 return expr;
3510 index = isl_multi_pw_aff_from_pw_aff(cond);
3511 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
3513 return expr;
3516 /* Return the number of bits needed to represent the type of "expr".
3517 * See the description of the type_size field of pet_expr.
3519 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
3521 return expr ? expr->type_size : 0;
3524 /* Replace the number of bits needed to represent the type of "expr"
3525 * by "type_size".
3526 * See the description of the type_size field of pet_expr.
3528 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
3529 int type_size)
3531 expr = pet_expr_cow(expr);
3532 if (!expr)
3533 return NULL;
3535 expr->type_size = type_size;
3537 return expr;
3540 /* Extend an access expression "expr" with an additional index "index".
3541 * In particular, add "index" as an extra argument to "expr" and
3542 * adjust the index expression of "expr" to refer to this extra argument.
3543 * The caller is responsible for calling pet_expr_access_set_depth
3544 * to update the corresponding access relation.
3546 * Note that we only collect the individual index expressions as
3547 * arguments of "expr" here.
3548 * An attempt to integrate them into the index expression of "expr"
3549 * is performed in pet_expr_access_plug_in_args.
3551 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
3552 __isl_take pet_expr *index)
3554 int n;
3555 isl_space *space;
3556 isl_local_space *ls;
3557 isl_pw_aff *pa;
3559 expr = pet_expr_cow(expr);
3560 if (!expr || !index)
3561 goto error;
3562 if (expr->type != pet_expr_access)
3563 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3564 "not an access pet_expr", goto error);
3566 n = pet_expr_get_n_arg(expr);
3567 expr = pet_expr_insert_arg(expr, n, index);
3568 if (!expr)
3569 return NULL;
3571 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3572 ls = isl_local_space_from_space(space);
3573 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
3574 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
3575 if (!expr->acc.index)
3576 return pet_expr_free(expr);
3578 return expr;
3579 error:
3580 pet_expr_free(expr);
3581 pet_expr_free(index);
3582 return NULL;
3585 /* Extend an access expression "expr" with an additional member acces to "id".
3586 * In particular, extend the index expression of "expr" to include
3587 * the additional member access.
3588 * The caller is responsible for calling pet_expr_access_set_depth
3589 * to update the corresponding access relation.
3591 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
3592 __isl_take isl_id *id)
3594 isl_space *space;
3595 isl_multi_pw_aff *field_access;
3597 expr = pet_expr_cow(expr);
3598 if (!expr || !id)
3599 goto error;
3600 if (expr->type != pet_expr_access)
3601 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3602 "not an access pet_expr", goto error);
3604 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3605 space = isl_space_from_domain(space);
3606 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3607 field_access = isl_multi_pw_aff_zero(space);
3608 expr->acc.index = pet_array_member(expr->acc.index, field_access);
3609 if (!expr->acc.index)
3610 return pet_expr_free(expr);
3612 return expr;
3613 error:
3614 pet_expr_free(expr);
3615 isl_id_free(id);
3616 return NULL;
3619 /* Prefix the access expression "expr" with "prefix".
3620 * If "add" is set, then it is not the index expression "prefix" itself
3621 * that was passed to the function, but its address.
3623 __isl_give pet_expr *pet_expr_access_patch(__isl_take pet_expr *expr,
3624 __isl_take isl_multi_pw_aff *prefix, int add)
3626 enum pet_expr_access_type type;
3628 expr = pet_expr_cow(expr);
3629 if (!expr || !prefix)
3630 goto error;
3631 if (expr->type != pet_expr_access)
3632 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3633 "not an access pet_expr", goto error);
3635 expr->acc.depth += isl_multi_pw_aff_dim(prefix, isl_dim_out) - add;
3636 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
3637 if (!expr->acc.access[type])
3638 continue;
3639 expr->acc.access[type] = pet_patch_union_map(
3640 isl_multi_pw_aff_copy(prefix), expr->acc.access[type],
3641 add, 0);
3642 if (!expr->acc.access[type])
3643 break;
3645 expr->acc.index = pet_patch_multi_pw_aff(prefix, expr->acc.index, add);
3646 if (!expr->acc.index || type < pet_expr_access_end)
3647 return pet_expr_free(expr);
3649 return expr;
3650 error:
3651 pet_expr_free(expr);
3652 isl_multi_pw_aff_free(prefix);
3653 return NULL;
3656 /* Dump the arguments of "expr" to "p" as a YAML sequence keyed
3657 * by "args", if there are any such arguments.
3659 static __isl_give isl_printer *dump_arguments(__isl_keep pet_expr *expr,
3660 __isl_take isl_printer *p)
3662 int i;
3664 if (expr->n_arg == 0)
3665 return p;
3667 p = isl_printer_print_str(p, "args");
3668 p = isl_printer_yaml_next(p);
3669 p = isl_printer_yaml_start_sequence(p);
3670 for (i = 0; i < expr->n_arg; ++i) {
3671 p = pet_expr_print(expr->args[i], p);
3672 p = isl_printer_yaml_next(p);
3674 p = isl_printer_yaml_end_sequence(p);
3676 return p;
3679 /* Print "expr" to "p" in YAML format.
3681 __isl_give isl_printer *pet_expr_print(__isl_keep pet_expr *expr,
3682 __isl_take isl_printer *p)
3684 if (!expr || !p)
3685 return isl_printer_free(p);
3687 switch (expr->type) {
3688 case pet_expr_double:
3689 p = isl_printer_print_str(p, expr->d.s);
3690 break;
3691 case pet_expr_int:
3692 p = isl_printer_print_val(p, expr->i);
3693 break;
3694 case pet_expr_access:
3695 p = isl_printer_yaml_start_mapping(p);
3696 if (expr->acc.ref_id) {
3697 p = isl_printer_print_str(p, "ref_id");
3698 p = isl_printer_yaml_next(p);
3699 p = isl_printer_print_id(p, expr->acc.ref_id);
3700 p = isl_printer_yaml_next(p);
3702 p = isl_printer_print_str(p, "index");
3703 p = isl_printer_yaml_next(p);
3704 p = isl_printer_print_multi_pw_aff(p, expr->acc.index);
3705 p = isl_printer_yaml_next(p);
3706 p = isl_printer_print_str(p, "depth");
3707 p = isl_printer_yaml_next(p);
3708 p = isl_printer_print_int(p, expr->acc.depth);
3709 p = isl_printer_yaml_next(p);
3710 if (expr->acc.kill) {
3711 p = isl_printer_print_str(p, "kill");
3712 p = isl_printer_yaml_next(p);
3713 p = isl_printer_print_int(p, 1);
3714 p = isl_printer_yaml_next(p);
3715 } else {
3716 p = isl_printer_print_str(p, "read");
3717 p = isl_printer_yaml_next(p);
3718 p = isl_printer_print_int(p, expr->acc.read);
3719 p = isl_printer_yaml_next(p);
3720 p = isl_printer_print_str(p, "write");
3721 p = isl_printer_yaml_next(p);
3722 p = isl_printer_print_int(p, expr->acc.write);
3723 p = isl_printer_yaml_next(p);
3725 if (expr->acc.access[pet_expr_access_may_read]) {
3726 p = isl_printer_print_str(p, "may_read");
3727 p = isl_printer_yaml_next(p);
3728 p = isl_printer_print_union_map(p,
3729 expr->acc.access[pet_expr_access_may_read]);
3730 p = isl_printer_yaml_next(p);
3732 if (expr->acc.access[pet_expr_access_may_write]) {
3733 p = isl_printer_print_str(p, "may_write");
3734 p = isl_printer_yaml_next(p);
3735 p = isl_printer_print_union_map(p,
3736 expr->acc.access[pet_expr_access_may_write]);
3737 p = isl_printer_yaml_next(p);
3739 if (expr->acc.access[pet_expr_access_must_write]) {
3740 p = isl_printer_print_str(p, "must_write");
3741 p = isl_printer_yaml_next(p);
3742 p = isl_printer_print_union_map(p,
3743 expr->acc.access[pet_expr_access_must_write]);
3744 p = isl_printer_yaml_next(p);
3746 p = dump_arguments(expr, p);
3747 p = isl_printer_yaml_end_mapping(p);
3748 break;
3749 case pet_expr_op:
3750 p = isl_printer_yaml_start_mapping(p);
3751 p = isl_printer_print_str(p, "op");
3752 p = isl_printer_yaml_next(p);
3753 p = isl_printer_print_str(p, op_str[expr->op]);
3754 p = isl_printer_yaml_next(p);
3755 p = dump_arguments(expr, p);
3756 p = isl_printer_yaml_end_mapping(p);
3757 break;
3758 case pet_expr_call:
3759 p = isl_printer_yaml_start_mapping(p);
3760 p = isl_printer_print_str(p, "call");
3761 p = isl_printer_yaml_next(p);
3762 p = isl_printer_print_str(p, expr->c.name);
3763 p = isl_printer_print_str(p, "/");
3764 p = isl_printer_print_int(p, expr->n_arg);
3765 p = isl_printer_yaml_next(p);
3766 p = dump_arguments(expr, p);
3767 if (expr->c.summary) {
3768 p = isl_printer_print_str(p, "summary");
3769 p = isl_printer_yaml_next(p);
3770 p = pet_function_summary_print(expr->c.summary, p);
3772 p = isl_printer_yaml_end_mapping(p);
3773 break;
3774 case pet_expr_cast:
3775 p = isl_printer_yaml_start_mapping(p);
3776 p = isl_printer_print_str(p, "cast");
3777 p = isl_printer_yaml_next(p);
3778 p = isl_printer_print_str(p, expr->type_name);
3779 p = isl_printer_yaml_next(p);
3780 p = dump_arguments(expr, p);
3781 p = isl_printer_yaml_end_mapping(p);
3782 break;
3783 case pet_expr_error:
3784 p = isl_printer_print_str(p, "ERROR");
3785 break;
3788 return p;
3791 /* Dump "expr" to stderr with indentation "indent".
3793 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
3795 isl_printer *p;
3797 if (!expr)
3798 return;
3800 p = isl_printer_to_file(pet_expr_get_ctx(expr), stderr);
3801 p = isl_printer_set_indent(p, indent);
3802 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
3803 p = isl_printer_start_line(p);
3804 p = pet_expr_print(expr, p);
3806 isl_printer_free(p);
3809 void pet_expr_dump(__isl_keep pet_expr *expr)
3811 pet_expr_dump_with_indent(expr, 0);