link against isl libraries first
[pet.git] / expr.c
blobaa693447f6a4eeb4a8d5c14a977e9315963f9441
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/ctx.h>
38 #include <isl/hash.h>
39 #include <isl/id.h>
40 #include <isl/val.h>
41 #include <isl/space.h>
42 #include <isl/local_space.h>
43 #include <isl/aff.h>
44 #include <isl/map.h>
45 #include <isl/union_set.h>
46 #include <isl/union_map.h>
47 #include <isl/printer.h>
49 #include "aff.h"
50 #include "array.h"
51 #include "expr.h"
52 #include "expr_arg.h"
53 #include "filter.h"
54 #include "nest.h"
55 #include "options.h"
56 #include "value_bounds.h"
57 #include "patch.h"
59 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
61 static char *type_str[] = {
62 [pet_expr_access] = "access",
63 [pet_expr_call] = "call",
64 [pet_expr_cast] = "cast",
65 [pet_expr_double] = "double",
66 [pet_expr_int] = "int",
67 [pet_expr_op] = "op",
70 static char *op_str[] = {
71 [pet_op_add_assign] = "+=",
72 [pet_op_sub_assign] = "-=",
73 [pet_op_mul_assign] = "*=",
74 [pet_op_div_assign] = "/=",
75 [pet_op_and_assign] = "&=",
76 [pet_op_xor_assign] = "^=",
77 [pet_op_or_assign] = "|=",
78 [pet_op_assign] = "=",
79 [pet_op_add] = "+",
80 [pet_op_sub] = "-",
81 [pet_op_mul] = "*",
82 [pet_op_div] = "/",
83 [pet_op_mod] = "%",
84 [pet_op_shl] = "<<",
85 [pet_op_shr] = ">>",
86 [pet_op_eq] = "==",
87 [pet_op_ne] = "!=",
88 [pet_op_le] = "<=",
89 [pet_op_ge] = ">=",
90 [pet_op_lt] = "<",
91 [pet_op_gt] = ">",
92 [pet_op_minus] = "-",
93 [pet_op_post_inc] = "++",
94 [pet_op_post_dec] = "--",
95 [pet_op_pre_inc] = "++",
96 [pet_op_pre_dec] = "--",
97 [pet_op_address_of] = "&",
98 [pet_op_and] = "&",
99 [pet_op_xor] = "^",
100 [pet_op_or] = "|",
101 [pet_op_not] = "~",
102 [pet_op_land] = "&&",
103 [pet_op_lor] = "||",
104 [pet_op_lnot] = "!",
105 [pet_op_cond] = "?:",
106 [pet_op_assume] = "assume",
107 [pet_op_kill] = "kill"
110 const char *pet_op_str(enum pet_op_type op)
112 return op_str[op];
115 int pet_op_is_inc_dec(enum pet_op_type op)
117 return op == pet_op_post_inc || op == pet_op_post_dec ||
118 op == pet_op_pre_inc || op == pet_op_pre_dec;
121 const char *pet_type_str(enum pet_expr_type type)
123 return type_str[type];
126 enum pet_op_type pet_str_op(const char *str)
128 int i;
130 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
131 if (!strcmp(op_str[i], str))
132 return i;
134 return -1;
137 enum pet_expr_type pet_str_type(const char *str)
139 int i;
141 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
142 if (!strcmp(type_str[i], str))
143 return i;
145 return -1;
148 /* Construct a pet_expr of the given type.
150 __isl_give pet_expr *pet_expr_alloc(isl_ctx *ctx, enum pet_expr_type type)
152 pet_expr *expr;
154 expr = isl_calloc_type(ctx, struct pet_expr);
155 if (!expr)
156 return NULL;
158 expr->ctx = ctx;
159 isl_ctx_ref(ctx);
160 expr->type = type;
161 expr->ref = 1;
163 return expr;
166 /* Construct an access pet_expr from an index expression.
167 * By default, the access is considered to be a read access.
168 * The initial depth is set from the index expression and
169 * may still be updated by the caller before the access relation
170 * is created.
172 __isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
174 isl_ctx *ctx;
175 pet_expr *expr;
177 if (!index)
178 return NULL;
179 ctx = isl_multi_pw_aff_get_ctx(index);
180 expr = pet_expr_alloc(ctx, pet_expr_access);
181 if (!expr)
182 goto error;
184 expr->acc.read = 1;
185 expr->acc.write = 0;
187 expr = pet_expr_access_set_index(expr, index);
189 return expr;
190 error:
191 isl_multi_pw_aff_free(index);
192 return NULL;
195 /* Extend the range of "access" with "n" dimensions, retaining
196 * the tuple identifier on this range.
198 * If "access" represents a member access, then extend the range
199 * of the member.
201 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
203 isl_id *id;
205 id = isl_map_get_tuple_id(access, isl_dim_out);
207 if (!isl_map_range_is_wrapping(access)) {
208 access = isl_map_add_dims(access, isl_dim_out, n);
209 } else {
210 isl_map *domain;
212 domain = isl_map_copy(access);
213 domain = isl_map_range_factor_domain(domain);
214 access = isl_map_range_factor_range(access);
215 access = extend_range(access, n);
216 access = isl_map_range_product(domain, access);
219 access = isl_map_set_tuple_id(access, isl_dim_out, id);
221 return access;
224 /* Does the access expression "expr" have any explicit access relation?
226 isl_bool pet_expr_access_has_any_access_relation(__isl_keep pet_expr *expr)
228 enum pet_expr_access_type type;
230 if (!expr)
231 return isl_bool_error;
233 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type)
234 if (expr->acc.access[type])
235 return isl_bool_true;
237 return isl_bool_false;
240 /* Are all relevant access relations explicitly available in "expr"?
242 static int has_relevant_access_relations(__isl_keep pet_expr *expr)
244 if (!expr)
245 return -1;
247 if (expr->acc.kill && !expr->acc.access[pet_expr_access_fake_killed])
248 return 0;
249 if (expr->acc.read && !expr->acc.access[pet_expr_access_may_read])
250 return 0;
251 if (expr->acc.write &&
252 (!expr->acc.access[pet_expr_access_may_write] ||
253 !expr->acc.access[pet_expr_access_must_write]))
254 return 0;
256 return 1;
259 /* Replace the depth of the access expr "expr" by "depth".
261 * To avoid inconsistencies between the depth and the access relation,
262 * we currently do not allow the depth to change once the access relation
263 * has been set or computed.
265 __isl_give pet_expr *pet_expr_access_set_depth(__isl_take pet_expr *expr,
266 int depth)
268 if (!expr)
269 return NULL;
270 if (expr->acc.depth == depth)
271 return expr;
272 if (pet_expr_access_has_any_access_relation(expr))
273 isl_die(pet_expr_get_ctx(expr), isl_error_unsupported,
274 "depth cannot be changed after access relation "
275 "has been set or computed", return pet_expr_free(expr));
277 expr = pet_expr_cow(expr);
278 if (!expr)
279 return NULL;
280 expr->acc.depth = depth;
282 return expr;
285 /* Construct a pet_expr that kills the elements specified by
286 * the index expression "index" and the access relation "access".
288 __isl_give pet_expr *pet_expr_kill_from_access_and_index(
289 __isl_take isl_map *access, __isl_take isl_multi_pw_aff *index)
291 int depth;
292 pet_expr *expr;
294 if (!access || !index)
295 goto error;
297 expr = pet_expr_from_index(index);
298 expr = pet_expr_access_set_read(expr, 0);
299 expr = pet_expr_access_set_kill(expr, 1);
300 depth = isl_map_dim(access, isl_dim_out);
301 expr = pet_expr_access_set_depth(expr, depth);
302 expr = pet_expr_access_set_access(expr, pet_expr_access_killed,
303 isl_union_map_from_map(access));
304 return pet_expr_new_unary(0, pet_op_kill, expr);
305 error:
306 isl_map_free(access);
307 isl_multi_pw_aff_free(index);
308 return NULL;
311 /* Construct a unary pet_expr that performs "op" on "arg",
312 * where the result is represented using a type of "type_size" bits
313 * (may be zero if unknown or if the type is not an integer).
315 __isl_give pet_expr *pet_expr_new_unary(int type_size, enum pet_op_type op,
316 __isl_take pet_expr *arg)
318 isl_ctx *ctx;
319 pet_expr *expr;
321 if (!arg)
322 return NULL;
323 ctx = pet_expr_get_ctx(arg);
324 expr = pet_expr_alloc(ctx, pet_expr_op);
325 expr = pet_expr_set_n_arg(expr, 1);
326 if (!expr)
327 goto error;
329 expr->op = op;
330 expr->type_size = type_size;
331 expr->args[pet_un_arg] = arg;
333 return expr;
334 error:
335 pet_expr_free(arg);
336 return NULL;
339 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
340 * where the result is represented using a type of "type_size" bits
341 * (may be zero if unknown or if the type is not an integer).
343 __isl_give pet_expr *pet_expr_new_binary(int type_size, enum pet_op_type op,
344 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
346 isl_ctx *ctx;
347 pet_expr *expr;
349 if (!lhs || !rhs)
350 goto error;
351 ctx = pet_expr_get_ctx(lhs);
352 expr = pet_expr_alloc(ctx, pet_expr_op);
353 expr = pet_expr_set_n_arg(expr, 2);
354 if (!expr)
355 goto error;
357 expr->op = op;
358 expr->type_size = type_size;
359 expr->args[pet_bin_lhs] = lhs;
360 expr->args[pet_bin_rhs] = rhs;
362 return expr;
363 error:
364 pet_expr_free(lhs);
365 pet_expr_free(rhs);
366 return NULL;
369 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
371 __isl_give pet_expr *pet_expr_new_ternary(__isl_take pet_expr *cond,
372 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
374 isl_ctx *ctx;
375 pet_expr *expr;
377 if (!cond || !lhs || !rhs)
378 goto error;
379 ctx = pet_expr_get_ctx(cond);
380 expr = pet_expr_alloc(ctx, pet_expr_op);
381 expr = pet_expr_set_n_arg(expr, 3);
382 if (!expr)
383 goto error;
385 expr->op = pet_op_cond;
386 expr->args[pet_ter_cond] = cond;
387 expr->args[pet_ter_true] = lhs;
388 expr->args[pet_ter_false] = rhs;
390 return expr;
391 error:
392 pet_expr_free(cond);
393 pet_expr_free(lhs);
394 pet_expr_free(rhs);
395 return NULL;
398 /* Construct a call pet_expr that calls function "name" with "n_arg"
399 * arguments. The caller is responsible for filling in the arguments.
401 __isl_give pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
402 unsigned n_arg)
404 pet_expr *expr;
406 expr = pet_expr_alloc(ctx, pet_expr_call);
407 expr = pet_expr_set_n_arg(expr, n_arg);
408 if (!expr)
409 return NULL;
411 expr->c.name = strdup(name);
412 if (!expr->c.name)
413 return pet_expr_free(expr);
415 return expr;
418 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
420 __isl_give pet_expr *pet_expr_new_cast(const char *type_name,
421 __isl_take pet_expr *arg)
423 isl_ctx *ctx;
424 pet_expr *expr;
426 if (!arg)
427 return NULL;
429 ctx = pet_expr_get_ctx(arg);
430 expr = pet_expr_alloc(ctx, pet_expr_cast);
431 expr = pet_expr_set_n_arg(expr, 1);
432 if (!expr)
433 goto error;
435 expr->type_name = strdup(type_name);
436 if (!expr->type_name)
437 goto error;
439 expr->args[0] = arg;
441 return expr;
442 error:
443 pet_expr_free(arg);
444 pet_expr_free(expr);
445 return NULL;
448 /* Construct a pet_expr that represents the double "d".
450 __isl_give pet_expr *pet_expr_new_double(isl_ctx *ctx,
451 double val, const char *s)
453 pet_expr *expr;
455 expr = pet_expr_alloc(ctx, pet_expr_double);
456 if (!expr)
457 return NULL;
459 expr->d.val = val;
460 expr->d.s = strdup(s);
461 if (!expr->d.s)
462 return pet_expr_free(expr);
464 return expr;
467 /* Construct a pet_expr that represents the integer value "v".
469 __isl_give pet_expr *pet_expr_new_int(__isl_take isl_val *v)
471 isl_ctx *ctx;
472 pet_expr *expr;
474 if (!v)
475 return NULL;
477 ctx = isl_val_get_ctx(v);
478 expr = pet_expr_alloc(ctx, pet_expr_int);
479 if (!expr)
480 goto error;
482 expr->i = v;
484 return expr;
485 error:
486 isl_val_free(v);
487 return NULL;
490 /* Return an independent duplicate of "expr".
492 * In case of an access expression, make sure the depth of the duplicate is set
493 * before the access relation (if any) is set and after the index expression
494 * is set.
496 static __isl_give pet_expr *pet_expr_dup(__isl_keep pet_expr *expr)
498 int i;
499 pet_expr *dup;
500 enum pet_expr_access_type type;
502 if (!expr)
503 return NULL;
505 dup = pet_expr_alloc(expr->ctx, expr->type);
506 dup = pet_expr_set_type_size(dup, expr->type_size);
507 dup = pet_expr_set_n_arg(dup, expr->n_arg);
508 for (i = 0; i < expr->n_arg; ++i)
509 dup = pet_expr_set_arg(dup, i, pet_expr_copy(expr->args[i]));
511 switch (expr->type) {
512 case pet_expr_access:
513 if (expr->acc.ref_id)
514 dup = pet_expr_access_set_ref_id(dup,
515 isl_id_copy(expr->acc.ref_id));
516 dup = pet_expr_access_set_index(dup,
517 isl_multi_pw_aff_copy(expr->acc.index));
518 dup = pet_expr_access_set_depth(dup, expr->acc.depth);
519 for (type = pet_expr_access_begin;
520 type < pet_expr_access_end; ++type) {
521 if (!expr->acc.access[type])
522 continue;
523 dup = pet_expr_access_set_access(dup, type,
524 isl_union_map_copy(expr->acc.access[type]));
526 dup = pet_expr_access_set_read(dup, expr->acc.read);
527 dup = pet_expr_access_set_write(dup, expr->acc.write);
528 dup = pet_expr_access_set_kill(dup, expr->acc.kill);
529 break;
530 case pet_expr_call:
531 dup = pet_expr_call_set_name(dup, expr->c.name);
532 if (expr->c.summary)
533 dup = pet_expr_call_set_summary(dup,
534 pet_function_summary_copy(expr->c.summary));
535 break;
536 case pet_expr_cast:
537 dup = pet_expr_cast_set_type_name(dup, expr->type_name);
538 break;
539 case pet_expr_double:
540 dup = pet_expr_double_set(dup, expr->d.val, expr->d.s);
541 break;
542 case pet_expr_int:
543 dup = pet_expr_int_set_val(dup, isl_val_copy(expr->i));
544 break;
545 case pet_expr_op:
546 dup = pet_expr_op_set_type(dup, expr->op);
547 break;
548 case pet_expr_error:
549 dup = pet_expr_free(dup);
550 break;
553 return dup;
556 /* Return a pet_expr that is equal to "expr" and that has only
557 * a single reference.
559 * If "expr" itself only has one reference, then clear its hash value
560 * since the returned pet_expr will be modified.
562 __isl_give pet_expr *pet_expr_cow(__isl_take pet_expr *expr)
564 if (!expr)
565 return NULL;
567 if (expr->ref == 1) {
568 expr->hash = 0;
569 return expr;
571 expr->ref--;
572 return pet_expr_dup(expr);
575 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr)
577 enum pet_expr_access_type type;
578 int i;
580 if (!expr)
581 return NULL;
582 if (--expr->ref > 0)
583 return NULL;
585 for (i = 0; i < expr->n_arg; ++i)
586 pet_expr_free(expr->args[i]);
587 free(expr->args);
589 switch (expr->type) {
590 case pet_expr_access:
591 isl_id_free(expr->acc.ref_id);
592 for (type = pet_expr_access_begin;
593 type < pet_expr_access_end; ++type)
594 isl_union_map_free(expr->acc.access[type]);
595 isl_multi_pw_aff_free(expr->acc.index);
596 break;
597 case pet_expr_call:
598 free(expr->c.name);
599 pet_function_summary_free(expr->c.summary);
600 break;
601 case pet_expr_cast:
602 free(expr->type_name);
603 break;
604 case pet_expr_double:
605 free(expr->d.s);
606 break;
607 case pet_expr_int:
608 isl_val_free(expr->i);
609 break;
610 case pet_expr_op:
611 case pet_expr_error:
612 break;
615 isl_ctx_deref(expr->ctx);
616 free(expr);
617 return NULL;
620 /* Return an additional reference to "expr".
622 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr)
624 if (!expr)
625 return NULL;
627 expr->ref++;
628 return expr;
631 /* Return the isl_ctx in which "expr" was created.
633 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr)
635 return expr ? expr->ctx : NULL;
638 /* Return the type of "expr".
640 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr)
642 if (!expr)
643 return pet_expr_error;
644 return expr->type;
647 /* Return the number of arguments of "expr".
649 int pet_expr_get_n_arg(__isl_keep pet_expr *expr)
651 if (!expr)
652 return -1;
654 return expr->n_arg;
657 /* Set the number of arguments of "expr" to "n".
659 * If "expr" originally had more arguments, then remove the extra arguments.
660 * If "expr" originally had fewer arguments, then create space for
661 * the extra arguments ans initialize them to NULL.
663 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n)
665 int i;
666 pet_expr **args;
668 if (!expr)
669 return NULL;
670 if (expr->n_arg == n)
671 return expr;
672 expr = pet_expr_cow(expr);
673 if (!expr)
674 return NULL;
676 if (n < expr->n_arg) {
677 for (i = n; i < expr->n_arg; ++i)
678 pet_expr_free(expr->args[i]);
679 expr->n_arg = n;
680 return expr;
683 args = isl_realloc_array(expr->ctx, expr->args, pet_expr *, n);
684 if (!args)
685 return pet_expr_free(expr);
686 expr->args = args;
687 for (i = expr->n_arg; i < n; ++i)
688 expr->args[i] = NULL;
689 expr->n_arg = n;
691 return expr;
694 /* Return the argument of "expr" at position "pos".
696 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos)
698 if (!expr)
699 return NULL;
700 if (pos < 0 || pos >= expr->n_arg)
701 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
702 "position out of bounds", return NULL);
704 return pet_expr_copy(expr->args[pos]);
707 /* Replace "expr" by its argument at position "pos".
709 __isl_give pet_expr *pet_expr_arg(__isl_take pet_expr *expr, int pos)
711 pet_expr *arg;
713 arg = pet_expr_get_arg(expr, pos);
714 pet_expr_free(expr);
716 return arg;
719 /* Replace the argument of "expr" at position "pos" by "arg".
721 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
722 __isl_take pet_expr *arg)
724 if (!expr || !arg)
725 goto error;
726 if (pos < 0 || pos >= expr->n_arg)
727 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
728 "position out of bounds", goto error);
729 if (expr->args[pos] == arg) {
730 pet_expr_free(arg);
731 return expr;
734 expr = pet_expr_cow(expr);
735 if (!expr)
736 goto error;
738 pet_expr_free(expr->args[pos]);
739 expr->args[pos] = arg;
741 return expr;
742 error:
743 pet_expr_free(expr);
744 pet_expr_free(arg);
745 return NULL;
748 /* Does "expr" perform a comparison operation?
750 int pet_expr_is_comparison(__isl_keep pet_expr *expr)
752 if (!expr)
753 return -1;
754 if (expr->type != pet_expr_op)
755 return 0;
756 switch (expr->op) {
757 case pet_op_eq:
758 case pet_op_ne:
759 case pet_op_le:
760 case pet_op_ge:
761 case pet_op_lt:
762 case pet_op_gt:
763 return 1;
764 default:
765 return 0;
769 /* Does "expr" perform a boolean operation?
771 int pet_expr_is_boolean(__isl_keep pet_expr *expr)
773 if (!expr)
774 return -1;
775 if (expr->type != pet_expr_op)
776 return 0;
777 switch (expr->op) {
778 case pet_op_land:
779 case pet_op_lor:
780 case pet_op_lnot:
781 return 1;
782 default:
783 return 0;
787 /* Is "expr" an address-of operation?
789 int pet_expr_is_address_of(__isl_keep pet_expr *expr)
791 if (!expr)
792 return -1;
793 if (expr->type != pet_expr_op)
794 return 0;
795 return expr->op == pet_op_address_of;
798 /* Is "expr" an assume statement?
800 int pet_expr_is_assume(__isl_keep pet_expr *expr)
802 if (!expr)
803 return -1;
804 if (expr->type != pet_expr_op)
805 return 0;
806 return expr->op == pet_op_assume;
809 /* Does "expr" perform a min operation?
811 int pet_expr_is_min(__isl_keep pet_expr *expr)
813 if (!expr)
814 return -1;
815 if (expr->type != pet_expr_call)
816 return 0;
817 if (expr->n_arg != 2)
818 return 0;
819 if (strcmp(expr->c.name, "min") != 0)
820 return 0;
821 return 1;
824 /* Does "expr" perform a max operation?
826 int pet_expr_is_max(__isl_keep pet_expr *expr)
828 if (!expr)
829 return -1;
830 if (expr->type != pet_expr_call)
831 return 0;
832 if (expr->n_arg != 2)
833 return 0;
834 if (strcmp(expr->c.name, "max") != 0)
835 return 0;
836 return 1;
839 /* Does "expr" represent an access to an unnamed space, i.e.,
840 * does it represent an affine expression?
842 isl_bool pet_expr_is_affine(__isl_keep pet_expr *expr)
844 int has_id;
846 if (!expr)
847 return isl_bool_error;
848 if (expr->type != pet_expr_access)
849 return isl_bool_false;
851 has_id = isl_multi_pw_aff_has_tuple_id(expr->acc.index, isl_dim_out);
852 if (has_id < 0)
853 return isl_bool_error;
855 return !has_id;
858 /* Given that "expr" represents an affine expression, i.e., that
859 * it is an access to an unnamed (1D) space, return this affine expression.
861 __isl_give isl_pw_aff *pet_expr_get_affine(__isl_keep pet_expr *expr)
863 isl_bool is_affine;
864 isl_pw_aff *pa;
865 isl_multi_pw_aff *mpa;
867 is_affine = pet_expr_is_affine(expr);
868 if (is_affine < 0)
869 return NULL;
870 if (!is_affine)
871 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
872 "not an affine expression", return NULL);
874 mpa = pet_expr_access_get_index(expr);
875 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
876 isl_multi_pw_aff_free(mpa);
877 return pa;
880 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
881 * not part of any struct?
883 int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
885 if (!expr)
886 return -1;
887 if (expr->type != pet_expr_access)
888 return 0;
889 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index))
890 return 0;
892 return expr->acc.depth == 0;
895 /* Construct an access relation from the index expression and
896 * the array depth of the access expression "expr".
898 * If the number of indices is smaller than the depth of the array,
899 * then we assume that all elements of the remaining dimensions
900 * are accessed.
902 static __isl_give isl_union_map *construct_access_relation(
903 __isl_keep pet_expr *expr)
905 isl_map *access;
906 int dim;
908 if (!expr)
909 return NULL;
911 access = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
912 if (!access)
913 return NULL;
915 dim = isl_map_dim(access, isl_dim_out);
916 if (dim > expr->acc.depth)
917 isl_die(isl_map_get_ctx(access), isl_error_internal,
918 "number of indices greater than depth",
919 access = isl_map_free(access));
921 if (dim != expr->acc.depth)
922 access = extend_range(access, expr->acc.depth - dim);
924 return isl_union_map_from_map(access);
927 /* Ensure that all relevant access relations are explicitly
928 * available in "expr".
930 * If "expr" does not already have the relevant access relations, then create
931 * them based on the index expression and the array depth.
933 * We do not cow since adding an explicit access relation
934 * does not change the meaning of the expression.
935 * However, the explicit access relations may modify the hash value,
936 * so the cached value is reset.
938 static __isl_give pet_expr *introduce_access_relations(
939 __isl_take pet_expr *expr)
941 isl_union_map *access;
942 int kill, read, write;
944 if (!expr)
945 return NULL;
946 if (has_relevant_access_relations(expr))
947 return expr;
949 access = construct_access_relation(expr);
950 if (!access)
951 return pet_expr_free(expr);
953 expr->hash = 0;
954 kill = expr->acc.kill;
955 read = expr->acc.read;
956 write = expr->acc.write;
957 if (kill && !expr->acc.access[pet_expr_access_fake_killed])
958 expr->acc.access[pet_expr_access_fake_killed] =
959 isl_union_map_copy(access);
960 if (read && !expr->acc.access[pet_expr_access_may_read])
961 expr->acc.access[pet_expr_access_may_read] =
962 isl_union_map_copy(access);
963 if (write && !expr->acc.access[pet_expr_access_may_write])
964 expr->acc.access[pet_expr_access_may_write] =
965 isl_union_map_copy(access);
966 if (write && !expr->acc.access[pet_expr_access_must_write])
967 expr->acc.access[pet_expr_access_must_write] =
968 isl_union_map_copy(access);
970 isl_union_map_free(access);
972 if (!has_relevant_access_relations(expr))
973 return pet_expr_free(expr);
975 return expr;
978 /* Return a hash value that digests "expr".
979 * If a hash value was computed already, then return that value.
980 * Otherwise, compute the hash value and store a copy in expr->hash.
982 uint32_t pet_expr_get_hash(__isl_keep pet_expr *expr)
984 int i;
985 enum pet_expr_access_type type;
986 uint32_t hash, hash_f;
988 if (!expr)
989 return 0;
990 if (expr->hash)
991 return expr->hash;
993 hash = isl_hash_init();
994 isl_hash_byte(hash, expr->type & 0xFF);
995 isl_hash_byte(hash, expr->n_arg & 0xFF);
996 for (i = 0; i < expr->n_arg; ++i) {
997 uint32_t hash_i;
998 hash_i = pet_expr_get_hash(expr->args[i]);
999 isl_hash_hash(hash, hash_i);
1001 switch (expr->type) {
1002 case pet_expr_error:
1003 return 0;
1004 case pet_expr_double:
1005 hash = isl_hash_string(hash, expr->d.s);
1006 break;
1007 case pet_expr_int:
1008 hash_f = isl_val_get_hash(expr->i);
1009 isl_hash_hash(hash, hash_f);
1010 break;
1011 case pet_expr_access:
1012 isl_hash_byte(hash, expr->acc.read & 0xFF);
1013 isl_hash_byte(hash, expr->acc.write & 0xFF);
1014 isl_hash_byte(hash, expr->acc.kill & 0xFF);
1015 hash_f = isl_id_get_hash(expr->acc.ref_id);
1016 isl_hash_hash(hash, hash_f);
1017 hash_f = isl_multi_pw_aff_get_hash(expr->acc.index);
1018 isl_hash_hash(hash, hash_f);
1019 isl_hash_byte(hash, expr->acc.depth & 0xFF);
1020 for (type = pet_expr_access_begin;
1021 type < pet_expr_access_end; ++type) {
1022 hash_f = isl_union_map_get_hash(expr->acc.access[type]);
1023 isl_hash_hash(hash, hash_f);
1025 break;
1026 case pet_expr_op:
1027 isl_hash_byte(hash, expr->op & 0xFF);
1028 break;
1029 case pet_expr_call:
1030 hash = isl_hash_string(hash, expr->c.name);
1031 break;
1032 case pet_expr_cast:
1033 hash = isl_hash_string(hash, expr->type_name);
1034 break;
1036 expr->hash = hash;
1037 return hash;
1040 /* Return 1 if the two pet_exprs are equivalent.
1042 int pet_expr_is_equal(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2)
1044 int i;
1045 enum pet_expr_access_type type;
1047 if (!expr1 || !expr2)
1048 return 0;
1050 if (expr1->type != expr2->type)
1051 return 0;
1052 if (expr1->n_arg != expr2->n_arg)
1053 return 0;
1054 for (i = 0; i < expr1->n_arg; ++i)
1055 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
1056 return 0;
1057 switch (expr1->type) {
1058 case pet_expr_error:
1059 return -1;
1060 case pet_expr_double:
1061 if (strcmp(expr1->d.s, expr2->d.s))
1062 return 0;
1063 if (expr1->d.val != expr2->d.val)
1064 return 0;
1065 break;
1066 case pet_expr_int:
1067 if (!isl_val_eq(expr1->i, expr2->i))
1068 return 0;
1069 break;
1070 case pet_expr_access:
1071 if (expr1->acc.read != expr2->acc.read)
1072 return 0;
1073 if (expr1->acc.write != expr2->acc.write)
1074 return 0;
1075 if (expr1->acc.kill != expr2->acc.kill)
1076 return 0;
1077 if (expr1->acc.ref_id != expr2->acc.ref_id)
1078 return 0;
1079 if (!expr1->acc.index || !expr2->acc.index)
1080 return 0;
1081 if (!isl_multi_pw_aff_is_equal(expr1->acc.index,
1082 expr2->acc.index))
1083 return 0;
1084 if (expr1->acc.depth != expr2->acc.depth)
1085 return 0;
1086 if (has_relevant_access_relations(expr1) !=
1087 has_relevant_access_relations(expr2)) {
1088 int equal;
1089 expr1 = pet_expr_copy(expr1);
1090 expr2 = pet_expr_copy(expr2);
1091 expr1 = introduce_access_relations(expr1);
1092 expr2 = introduce_access_relations(expr2);
1093 equal = pet_expr_is_equal(expr1, expr2);
1094 pet_expr_free(expr1);
1095 pet_expr_free(expr2);
1096 return equal;
1098 for (type = pet_expr_access_begin;
1099 type < pet_expr_access_end; ++type) {
1100 if (!expr1->acc.access[type] !=
1101 !expr2->acc.access[type])
1102 return 0;
1103 if (!expr1->acc.access[type])
1104 continue;
1105 if (!isl_union_map_is_equal(expr1->acc.access[type],
1106 expr2->acc.access[type]))
1107 return 0;
1109 break;
1110 case pet_expr_op:
1111 if (expr1->op != expr2->op)
1112 return 0;
1113 break;
1114 case pet_expr_call:
1115 if (strcmp(expr1->c.name, expr2->c.name))
1116 return 0;
1117 break;
1118 case pet_expr_cast:
1119 if (strcmp(expr1->type_name, expr2->type_name))
1120 return 0;
1121 break;
1124 return 1;
1127 /* Do "expr1" and "expr2" represent two accesses to the same array
1128 * that are also of the same type? That is, can these two accesses
1129 * be replaced by a single access?
1131 isl_bool pet_expr_is_same_access(__isl_keep pet_expr *expr1,
1132 __isl_keep pet_expr *expr2)
1134 isl_space *space1, *space2;
1135 isl_bool same;
1137 if (!expr1 || !expr2)
1138 return isl_bool_error;
1139 if (pet_expr_get_type(expr1) != pet_expr_access)
1140 return isl_bool_false;
1141 if (pet_expr_get_type(expr2) != pet_expr_access)
1142 return isl_bool_false;
1143 if (expr1->acc.read != expr2->acc.read)
1144 return isl_bool_false;
1145 if (expr1->acc.write != expr2->acc.write)
1146 return isl_bool_false;
1147 if (expr1->acc.kill != expr2->acc.kill)
1148 return isl_bool_false;
1149 if (expr1->acc.depth != expr2->acc.depth)
1150 return isl_bool_false;
1152 space1 = isl_multi_pw_aff_get_space(expr1->acc.index);
1153 space2 = isl_multi_pw_aff_get_space(expr2->acc.index);
1154 same = isl_space_tuple_is_equal(space1, isl_dim_out,
1155 space2, isl_dim_out);
1156 if (same >= 0 && same)
1157 same = isl_space_tuple_is_equal(space1, isl_dim_in,
1158 space2, isl_dim_in);
1159 isl_space_free(space1);
1160 isl_space_free(space2);
1162 return same;
1165 /* Does the access expression "expr" read the accessed elements?
1167 isl_bool pet_expr_access_is_read(__isl_keep pet_expr *expr)
1169 if (!expr)
1170 return isl_bool_error;
1171 if (expr->type != pet_expr_access)
1172 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1173 "not an access expression", return isl_bool_error);
1175 return expr->acc.read;
1178 /* Does the access expression "expr" write to the accessed elements?
1180 isl_bool pet_expr_access_is_write(__isl_keep pet_expr *expr)
1182 if (!expr)
1183 return isl_bool_error;
1184 if (expr->type != pet_expr_access)
1185 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1186 "not an access expression", return isl_bool_error);
1188 return expr->acc.write;
1191 /* Does the access expression "expr" kill the accessed elements?
1193 isl_bool pet_expr_access_is_kill(__isl_keep pet_expr *expr)
1195 if (!expr)
1196 return isl_bool_error;
1197 if (expr->type != pet_expr_access)
1198 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1199 "not an access expression", return isl_bool_error);
1201 return expr->acc.kill;
1204 /* Return the identifier of the array accessed by "expr".
1206 * If "expr" represents a member access, then return the identifier
1207 * of the outer structure array.
1209 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
1211 if (!expr)
1212 return NULL;
1213 if (expr->type != pet_expr_access)
1214 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1215 "not an access expression", return NULL);
1217 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index)) {
1218 isl_space *space;
1219 isl_id *id;
1221 space = isl_multi_pw_aff_get_space(expr->acc.index);
1222 space = isl_space_range(space);
1223 while (space && isl_space_is_wrapping(space))
1224 space = isl_space_domain(isl_space_unwrap(space));
1225 id = isl_space_get_tuple_id(space, isl_dim_set);
1226 isl_space_free(space);
1228 return id;
1231 return isl_multi_pw_aff_get_tuple_id(expr->acc.index, isl_dim_out);
1234 /* Return the parameter space of "expr".
1236 __isl_give isl_space *pet_expr_access_get_parameter_space(
1237 __isl_keep pet_expr *expr)
1239 isl_space *space;
1241 if (!expr)
1242 return NULL;
1243 if (expr->type != pet_expr_access)
1244 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1245 "not an access expression", return NULL);
1247 space = isl_multi_pw_aff_get_space(expr->acc.index);
1248 space = isl_space_params(space);
1250 return space;
1253 /* Return the domain space of "expr", including the arguments (if any).
1255 __isl_give isl_space *pet_expr_access_get_augmented_domain_space(
1256 __isl_keep pet_expr *expr)
1258 isl_space *space;
1260 if (!expr)
1261 return NULL;
1262 if (expr->type != pet_expr_access)
1263 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1264 "not an access expression", return NULL);
1266 space = isl_multi_pw_aff_get_space(expr->acc.index);
1267 space = isl_space_domain(space);
1269 return space;
1272 /* Return the domain space of "expr", without the arguments (if any).
1274 __isl_give isl_space *pet_expr_access_get_domain_space(
1275 __isl_keep pet_expr *expr)
1277 isl_space *space;
1279 space = pet_expr_access_get_augmented_domain_space(expr);
1280 if (isl_space_is_wrapping(space))
1281 space = isl_space_domain(isl_space_unwrap(space));
1283 return space;
1286 /* Internal data structure for pet_expr_access_foreach_data_space.
1288 struct pet_foreach_data_space_data {
1289 isl_stat (*fn)(__isl_take isl_space *space, void *user);
1290 void *user;
1293 /* Given a piece of an access relation, call data->fn on the data
1294 * (i.e., range) space.
1296 static isl_stat foreach_data_space(__isl_take isl_map *map, void *user)
1298 struct pet_foreach_data_space_data *data = user;
1299 isl_space *space;
1301 space = isl_map_get_space(map);
1302 space = isl_space_range(space);
1303 isl_map_free(map);
1305 return data->fn(space, data->user);
1308 /* Call "fn" on the data spaces accessed by "expr".
1309 * In particular, call "fn" on the range space of the index expression,
1310 * but if "expr" keeps track of any explicit access relations,
1311 * then also call "fn" on the corresponding range spaces.
1313 isl_stat pet_expr_access_foreach_data_space(__isl_keep pet_expr *expr,
1314 isl_stat (*fn)(__isl_take isl_space *space, void *user), void *user)
1316 struct pet_foreach_data_space_data data = { fn, user };
1317 enum pet_expr_access_type type;
1318 isl_space *space;
1320 if (!expr)
1321 return isl_stat_error;
1322 if (expr->type != pet_expr_access)
1323 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1324 "not an access expression", return isl_stat_error);
1326 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1327 if (!expr->acc.access[type])
1328 continue;
1329 if (isl_union_map_foreach_map(expr->acc.access[type],
1330 &foreach_data_space, &data) < 0)
1331 return isl_stat_error;
1334 space = isl_multi_pw_aff_get_space(expr->acc.index);
1335 space = isl_space_range(space);
1336 return fn(space, user);
1339 /* Modify all subexpressions of "expr" by calling "fn" on them.
1340 * The subexpressions are traversed in depth first preorder.
1342 __isl_give pet_expr *pet_expr_map_top_down(__isl_take pet_expr *expr,
1343 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1344 void *user)
1346 int i, n;
1348 if (!expr)
1349 return NULL;
1351 expr = fn(expr, user);
1353 n = pet_expr_get_n_arg(expr);
1354 for (i = 0; i < n; ++i) {
1355 pet_expr *arg = pet_expr_get_arg(expr, i);
1356 arg = pet_expr_map_top_down(arg, fn, user);
1357 expr = pet_expr_set_arg(expr, i, arg);
1360 return expr;
1363 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1365 static __isl_give pet_expr *pet_expr_map_expr_of_type(__isl_take pet_expr *expr,
1366 enum pet_expr_type type,
1367 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1368 void *user)
1370 int i, n;
1372 n = pet_expr_get_n_arg(expr);
1373 for (i = 0; i < n; ++i) {
1374 pet_expr *arg = pet_expr_get_arg(expr, i);
1375 arg = pet_expr_map_expr_of_type(arg, type, fn, user);
1376 expr = pet_expr_set_arg(expr, i, arg);
1379 if (!expr)
1380 return NULL;
1382 if (expr->type == type)
1383 expr = fn(expr, user);
1385 return expr;
1388 /* Modify all expressions of type pet_expr_access in "expr"
1389 * by calling "fn" on them.
1391 __isl_give pet_expr *pet_expr_map_access(__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_access, fn, user);
1398 /* Modify all expressions of type pet_expr_call in "expr"
1399 * by calling "fn" on them.
1401 __isl_give pet_expr *pet_expr_map_call(__isl_take pet_expr *expr,
1402 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1403 void *user)
1405 return pet_expr_map_expr_of_type(expr, pet_expr_call, fn, user);
1408 /* Modify all expressions of type pet_expr_op in "expr"
1409 * by calling "fn" on them.
1411 __isl_give pet_expr *pet_expr_map_op(__isl_take pet_expr *expr,
1412 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1413 void *user)
1415 return pet_expr_map_expr_of_type(expr, pet_expr_op, fn, user);
1418 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1420 * Return -1 on error (where fn returning a negative value is treated as
1421 * an error).
1422 * Otherwise return 0.
1424 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
1425 enum pet_expr_type type,
1426 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1428 int i;
1430 if (!expr)
1431 return -1;
1433 for (i = 0; i < expr->n_arg; ++i)
1434 if (pet_expr_foreach_expr_of_type(expr->args[i],
1435 type, fn, user) < 0)
1436 return -1;
1438 if (expr->type == type)
1439 return fn(expr, user);
1441 return 0;
1444 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1446 * Return -1 on error (where fn returning a negative value is treated as
1447 * an error).
1448 * Otherwise return 0.
1450 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
1451 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1453 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
1456 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1458 * Return -1 on error (where fn returning a negative value is treated as
1459 * an error).
1460 * Otherwise return 0.
1462 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
1463 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1465 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1468 /* Internal data structure for pet_expr_writes.
1469 * "id" is the identifier that we are looking for.
1470 * "found" is set if we have found the identifier being written to.
1472 struct pet_expr_writes_data {
1473 isl_id *id;
1474 int found;
1477 /* Given an access expression, check if it writes to data->id.
1478 * If so, set data->found and abort the search.
1480 static int writes(__isl_keep pet_expr *expr, void *user)
1482 struct pet_expr_writes_data *data = user;
1483 isl_id *write_id;
1485 if (!expr->acc.write)
1486 return 0;
1487 if (pet_expr_is_affine(expr))
1488 return 0;
1490 write_id = pet_expr_access_get_id(expr);
1491 isl_id_free(write_id);
1493 if (!write_id)
1494 return -1;
1496 if (write_id != data->id)
1497 return 0;
1499 data->found = 1;
1500 return -1;
1503 /* Does expression "expr" write to "id"?
1505 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1507 struct pet_expr_writes_data data;
1509 data.id = id;
1510 data.found = 0;
1511 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1512 !data.found)
1513 return -1;
1515 return data.found;
1518 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1519 * index expression and access relations of "expr" (if any)
1520 * to dimensions of "dst_type" at "dst_pos".
1522 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1523 enum isl_dim_type dst_type, unsigned dst_pos,
1524 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1526 enum pet_expr_access_type type;
1528 expr = pet_expr_cow(expr);
1529 if (!expr)
1530 return NULL;
1531 if (expr->type != pet_expr_access)
1532 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1533 "not an access pet_expr", return pet_expr_free(expr));
1535 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1536 if (!expr->acc.access[type])
1537 continue;
1538 expr->acc.access[type] =
1539 pet_union_map_move_dims(expr->acc.access[type],
1540 dst_type, dst_pos, src_type, src_pos, n);
1541 if (!expr->acc.access[type])
1542 break;
1544 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1545 dst_type, dst_pos, src_type, src_pos, n);
1546 if (!expr->acc.index || type < pet_expr_access_end)
1547 return pet_expr_free(expr);
1549 return expr;
1552 /* Replace the index expression and access relations (if any) of "expr"
1553 * by their preimages under the function represented by "ma".
1555 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1556 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1558 enum pet_expr_access_type type;
1560 expr = pet_expr_cow(expr);
1561 if (!expr || !ma)
1562 goto error;
1563 if (expr->type != pet_expr_access)
1564 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1565 "not an access pet_expr", goto error);
1567 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1568 if (!expr->acc.access[type])
1569 continue;
1570 expr->acc.access[type] =
1571 isl_union_map_preimage_domain_multi_aff(
1572 expr->acc.access[type], isl_multi_aff_copy(ma));
1573 if (!expr->acc.access[type])
1574 break;
1576 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1577 ma);
1578 if (!expr->acc.index || type < pet_expr_access_end)
1579 return pet_expr_free(expr);
1581 return expr;
1582 error:
1583 isl_multi_aff_free(ma);
1584 pet_expr_free(expr);
1585 return NULL;
1588 /* Replace the index expression and access relations (if any) of "expr"
1589 * by their preimages under the function represented by "mpa".
1591 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1592 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1594 enum pet_expr_access_type type;
1596 expr = pet_expr_cow(expr);
1597 if (!expr || !mpa)
1598 goto error;
1599 if (expr->type != pet_expr_access)
1600 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1601 "not an access pet_expr", goto error);
1603 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1604 if (!expr->acc.access[type])
1605 continue;
1606 expr->acc.access[type] =
1607 isl_union_map_preimage_domain_multi_pw_aff(
1608 expr->acc.access[type], isl_multi_pw_aff_copy(mpa));
1609 if (!expr->acc.access[type])
1610 break;
1612 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1613 expr->acc.index, mpa);
1614 if (!expr->acc.index || type < pet_expr_access_end)
1615 return pet_expr_free(expr);
1617 return expr;
1618 error:
1619 isl_multi_pw_aff_free(mpa);
1620 pet_expr_free(expr);
1621 return NULL;
1624 /* Return the index expression of access expression "expr".
1626 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1627 __isl_keep pet_expr *expr)
1629 if (!expr)
1630 return NULL;
1631 if (expr->type != pet_expr_access)
1632 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1633 "not an access expression", return NULL);
1635 return isl_multi_pw_aff_copy(expr->acc.index);
1638 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1640 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1642 isl_space *space;
1643 enum pet_expr_access_type type;
1645 expr = pet_expr_cow(expr);
1646 if (!expr)
1647 return NULL;
1648 if (expr->type != pet_expr_access)
1649 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1650 "not an access expression", return pet_expr_free(expr));
1652 if (!pet_expr_access_has_any_access_relation(expr))
1653 return expr;
1655 space = isl_multi_pw_aff_get_space(expr->acc.index);
1656 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1657 if (!expr->acc.access[type])
1658 continue;
1659 space = isl_space_align_params(space,
1660 isl_union_map_get_space(expr->acc.access[type]));
1662 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1663 isl_space_copy(space));
1664 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1665 if (!expr->acc.access[type])
1666 continue;
1667 expr->acc.access[type] =
1668 isl_union_map_align_params(expr->acc.access[type],
1669 isl_space_copy(space));
1670 if (!expr->acc.access[type])
1671 break;
1673 isl_space_free(space);
1674 if (!expr->acc.index || type < pet_expr_access_end)
1675 return pet_expr_free(expr);
1677 return expr;
1680 /* Are "expr1" and "expr2" both array accesses such that
1681 * the access relation of "expr1" is a subset of that of "expr2"?
1682 * Only take into account the first "n_arg" arguments.
1684 * This function is tailored for use by mark_self_dependences in nest.c.
1685 * In particular, the input expressions may have more than "n_arg"
1686 * elements in their arguments arrays, while only the first "n_arg"
1687 * elements are referenced from the access relations.
1689 int pet_expr_is_sub_access(__isl_keep pet_expr *expr1,
1690 __isl_keep pet_expr *expr2, int n_arg)
1692 isl_id *id1, *id2;
1693 int i, n1, n2;
1694 int is_subset;
1696 if (!expr1 || !expr2)
1697 return 0;
1698 if (pet_expr_get_type(expr1) != pet_expr_access)
1699 return 0;
1700 if (pet_expr_get_type(expr2) != pet_expr_access)
1701 return 0;
1702 if (pet_expr_is_affine(expr1))
1703 return 0;
1704 if (pet_expr_is_affine(expr2))
1705 return 0;
1706 n1 = pet_expr_get_n_arg(expr1);
1707 if (n1 > n_arg)
1708 n1 = n_arg;
1709 n2 = pet_expr_get_n_arg(expr2);
1710 if (n2 > n_arg)
1711 n2 = n_arg;
1712 if (n1 != n2)
1713 return 0;
1714 for (i = 0; i < n1; ++i) {
1715 int equal;
1716 equal = pet_expr_is_equal(expr1->args[i], expr2->args[i]);
1717 if (equal < 0 || !equal)
1718 return equal;
1720 id1 = pet_expr_access_get_id(expr1);
1721 id2 = pet_expr_access_get_id(expr2);
1722 isl_id_free(id1);
1723 isl_id_free(id2);
1724 if (!id1 || !id2)
1725 return 0;
1726 if (id1 != id2)
1727 return 0;
1729 expr1 = pet_expr_copy(expr1);
1730 expr2 = pet_expr_copy(expr2);
1731 expr1 = introduce_access_relations(expr1);
1732 expr2 = introduce_access_relations(expr2);
1733 if (!expr1 || !expr2)
1734 goto error;
1736 is_subset = isl_union_map_is_subset(
1737 expr1->acc.access[pet_expr_access_may_read],
1738 expr2->acc.access[pet_expr_access_may_read]);
1740 pet_expr_free(expr1);
1741 pet_expr_free(expr2);
1743 return is_subset;
1744 error:
1745 pet_expr_free(expr1);
1746 pet_expr_free(expr2);
1747 return -1;
1750 /* Given a set in the iteration space "domain", extend it to live in the space
1751 * of the domain of access relations.
1753 * That, is the number of arguments "n" is 0, then simply return domain.
1754 * Otherwise, return [domain -> [a_1,...,a_n]].
1756 static __isl_give isl_set *add_arguments(__isl_take isl_set *domain, int n)
1758 isl_map *map;
1760 if (n == 0)
1761 return domain;
1763 map = isl_map_from_domain(domain);
1764 map = isl_map_add_dims(map, isl_dim_out, n);
1765 return isl_map_wrap(map);
1768 /* Add extra conditions to the domains of all access relations in "expr",
1769 * introducing access relations if they are not already present.
1771 * The conditions are not added to the index expression. Instead, they
1772 * are used to try and simplify the index expression.
1774 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1775 __isl_take isl_set *cond)
1777 int i;
1778 isl_union_set *uset;
1779 enum pet_expr_access_type type;
1781 expr = pet_expr_cow(expr);
1782 if (!expr)
1783 goto error;
1785 for (i = 0; i < expr->n_arg; ++i) {
1786 expr->args[i] = pet_expr_restrict(expr->args[i],
1787 isl_set_copy(cond));
1788 if (!expr->args[i])
1789 goto error;
1792 if (expr->type != pet_expr_access) {
1793 isl_set_free(cond);
1794 return expr;
1797 expr = introduce_access_relations(expr);
1798 if (!expr)
1799 goto error;
1801 cond = add_arguments(cond, expr->n_arg);
1802 uset = isl_union_set_from_set(isl_set_copy(cond));
1803 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1804 if (!expr->acc.access[type])
1805 continue;
1806 expr->acc.access[type] =
1807 isl_union_map_intersect_domain(expr->acc.access[type],
1808 isl_union_set_copy(uset));
1809 if (!expr->acc.access[type])
1810 break;
1812 isl_union_set_free(uset);
1813 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, cond);
1814 if (type < pet_expr_access_end || !expr->acc.index)
1815 return pet_expr_free(expr);
1817 return expr;
1818 error:
1819 isl_set_free(cond);
1820 return pet_expr_free(expr);
1823 /* Modify the access relations (if any) and index expression
1824 * of the given access expression
1825 * based on the given iteration space transformation.
1826 * In particular, precompose the access relation and index expression
1827 * with the update function.
1829 * If the access has any arguments then the domain of the access relation
1830 * is a wrapped mapping from the iteration space to the space of
1831 * argument values. We only need to change the domain of this wrapped
1832 * mapping, so we extend the input transformation with an identity mapping
1833 * on the space of argument values.
1835 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1836 __isl_keep isl_multi_pw_aff *update)
1838 enum pet_expr_access_type type;
1840 expr = pet_expr_cow(expr);
1841 if (!expr)
1842 return NULL;
1843 if (expr->type != pet_expr_access)
1844 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1845 "not an access expression", return pet_expr_free(expr));
1847 update = isl_multi_pw_aff_copy(update);
1849 if (expr->n_arg > 0) {
1850 isl_space *space;
1851 isl_multi_pw_aff *id;
1853 space = isl_multi_pw_aff_get_space(expr->acc.index);
1854 space = isl_space_domain(space);
1855 space = isl_space_unwrap(space);
1856 space = isl_space_range(space);
1857 space = isl_space_map_from_set(space);
1858 id = isl_multi_pw_aff_identity(space);
1859 update = isl_multi_pw_aff_product(update, id);
1862 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1863 if (!expr->acc.access[type])
1864 continue;
1865 expr->acc.access[type] =
1866 isl_union_map_preimage_domain_multi_pw_aff(
1867 expr->acc.access[type],
1868 isl_multi_pw_aff_copy(update));
1869 if (!expr->acc.access[type])
1870 break;
1872 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1873 expr->acc.index, update);
1874 if (type < pet_expr_access_end || !expr->acc.index)
1875 return pet_expr_free(expr);
1877 return expr;
1880 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1882 isl_multi_pw_aff *update = user;
1884 return pet_expr_access_update_domain(expr, update);
1887 /* Modify all access relations in "expr" by precomposing them with
1888 * the given iteration space transformation.
1890 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1891 __isl_take isl_multi_pw_aff *update)
1893 expr = pet_expr_map_access(expr, &update_domain, update);
1894 isl_multi_pw_aff_free(update);
1895 return expr;
1898 /* Given an expression with accesses that have a 0D anonymous domain,
1899 * replace those domains by "space".
1901 __isl_give pet_expr *pet_expr_insert_domain(__isl_take pet_expr *expr,
1902 __isl_take isl_space *space)
1904 isl_multi_pw_aff *mpa;
1906 space = isl_space_from_domain(space);
1907 mpa = isl_multi_pw_aff_zero(space);
1908 return pet_expr_update_domain(expr, mpa);
1911 /* Add all parameters in "space" to the access relations (if any)
1912 * and index expression of "expr".
1914 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1916 isl_space *space = user;
1917 enum pet_expr_access_type type;
1919 expr = pet_expr_cow(expr);
1920 if (!expr)
1921 return NULL;
1922 if (expr->type != pet_expr_access)
1923 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1924 "not an access expression", return pet_expr_free(expr));
1926 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1927 if (!expr->acc.access[type])
1928 continue;
1929 expr->acc.access[type] =
1930 isl_union_map_align_params(expr->acc.access[type],
1931 isl_space_copy(space));
1932 if (!expr->acc.access[type])
1933 break;
1935 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1936 isl_space_copy(space));
1937 if (type < pet_expr_access_end || !expr->acc.index)
1938 return pet_expr_free(expr);
1940 return expr;
1943 /* Add all parameters in "space" to all access relations and index expressions
1944 * in "expr".
1946 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1947 __isl_take isl_space *space)
1949 expr = pet_expr_map_access(expr, &align_params, space);
1950 isl_space_free(space);
1951 return expr;
1954 /* Insert an argument expression corresponding to "test" in front
1955 * of the list of arguments described by *n_arg and *args.
1957 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1958 __isl_keep isl_multi_pw_aff *test)
1960 int i;
1961 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1963 if (!test)
1964 return pet_expr_free(expr);
1965 expr = pet_expr_cow(expr);
1966 if (!expr)
1967 return NULL;
1969 if (!expr->args) {
1970 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1971 if (!expr->args)
1972 return pet_expr_free(expr);
1973 } else {
1974 pet_expr **ext;
1975 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1976 if (!ext)
1977 return pet_expr_free(expr);
1978 for (i = 0; i < expr->n_arg; ++i)
1979 ext[1 + i] = expr->args[i];
1980 free(expr->args);
1981 expr->args = ext;
1983 expr->n_arg++;
1984 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1985 if (!expr->args[0])
1986 return pet_expr_free(expr);
1988 return expr;
1991 /* Make the expression "expr" depend on the value of "test"
1992 * being equal to "satisfied".
1994 * If "test" is an affine expression, we simply add the conditions
1995 * on the expression having the value "satisfied" to all access relations
1996 * (introducing access relations if they are missing) and index expressions.
1998 * Otherwise, we add a filter to "expr" (which is then assumed to be
1999 * an access expression) corresponding to "test" being equal to "satisfied".
2001 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
2002 __isl_take isl_multi_pw_aff *test, int satisfied)
2004 isl_id *id;
2005 isl_ctx *ctx;
2006 isl_space *space;
2007 isl_pw_multi_aff *pma;
2008 enum pet_expr_access_type type;
2010 expr = pet_expr_cow(expr);
2011 if (!expr || !test)
2012 goto error;
2014 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
2015 isl_pw_aff *pa;
2016 isl_set *cond;
2018 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
2019 isl_multi_pw_aff_free(test);
2020 if (satisfied)
2021 cond = isl_pw_aff_non_zero_set(pa);
2022 else
2023 cond = isl_pw_aff_zero_set(pa);
2024 return pet_expr_restrict(expr, cond);
2027 ctx = isl_multi_pw_aff_get_ctx(test);
2028 if (expr->type != pet_expr_access)
2029 isl_die(ctx, isl_error_invalid,
2030 "can only filter access expressions", goto error);
2032 expr = introduce_access_relations(expr);
2033 if (!expr)
2034 goto error;
2036 space = isl_space_domain(isl_multi_pw_aff_get_space(expr->acc.index));
2037 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2038 pma = pet_filter_insert_pma(space, id, satisfied);
2040 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
2041 if (!expr->acc.access[type])
2042 continue;
2043 expr->acc.access[type] =
2044 isl_union_map_preimage_domain_pw_multi_aff(
2045 expr->acc.access[type],
2046 isl_pw_multi_aff_copy(pma));
2047 if (!expr->acc.access[type])
2048 break;
2050 pma = isl_pw_multi_aff_gist(pma,
2051 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma)));
2052 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
2053 expr->acc.index, pma);
2054 if (type < pet_expr_access_end || !expr->acc.index)
2055 goto error;
2057 expr = insert_access_arg(expr, test);
2059 isl_multi_pw_aff_free(test);
2060 return expr;
2061 error:
2062 isl_multi_pw_aff_free(test);
2063 return pet_expr_free(expr);
2066 /* Add a reference identifier to access expression "expr".
2067 * "user" points to an integer that contains the sequence number
2068 * of the next reference.
2070 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
2071 void *user)
2073 isl_ctx *ctx;
2074 char name[50];
2075 int *n_ref = user;
2077 expr = pet_expr_cow(expr);
2078 if (!expr)
2079 return expr;
2080 if (expr->type != pet_expr_access)
2081 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2082 "not an access expression", return pet_expr_free(expr));
2084 ctx = pet_expr_get_ctx(expr);
2085 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
2086 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
2087 if (!expr->acc.ref_id)
2088 return pet_expr_free(expr);
2090 return expr;
2093 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
2095 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
2098 /* Reset the user pointer on all parameter and tuple ids in
2099 * the access relations (if any) and the index expression
2100 * of the access expression "expr".
2102 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
2103 void *user)
2105 enum pet_expr_access_type type;
2107 expr = pet_expr_cow(expr);
2108 if (!expr)
2109 return expr;
2110 if (expr->type != pet_expr_access)
2111 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2112 "not an access expression", return pet_expr_free(expr));
2114 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
2115 if (!expr->acc.access[type])
2116 continue;
2117 expr->acc.access[type] =
2118 isl_union_map_reset_user(expr->acc.access[type]);
2119 if (!expr->acc.access[type])
2120 break;
2122 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
2123 if (type < pet_expr_access_end || !expr->acc.index)
2124 return pet_expr_free(expr);
2126 return expr;
2129 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
2131 return pet_expr_map_access(expr, &access_anonymize, NULL);
2134 /* Data used in access_gist() callback.
2136 struct pet_access_gist_data {
2137 isl_set *domain;
2138 isl_union_map *value_bounds;
2141 /* Given an expression "expr" of type pet_expr_access, compute
2142 * the gist of the associated access relations (if any) and index expression
2143 * with respect to data->domain and the bounds on the values of the arguments
2144 * of the expression.
2146 * The arguments of "expr" have been gisted right before "expr" itself
2147 * is gisted. The gisted arguments may have become equal where before
2148 * they may not have been (obviously) equal. We therefore take
2149 * the opportunity to remove duplicate arguments here.
2151 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
2153 struct pet_access_gist_data *data = user;
2154 isl_set *domain;
2155 isl_union_set *uset;
2156 enum pet_expr_access_type type;
2158 expr = pet_expr_remove_duplicate_args(expr);
2159 expr = pet_expr_cow(expr);
2160 if (!expr)
2161 return expr;
2162 if (expr->type != pet_expr_access)
2163 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2164 "not an access expression", return pet_expr_free(expr));
2166 domain = isl_set_copy(data->domain);
2167 if (expr->n_arg > 0)
2168 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
2169 data->value_bounds);
2171 uset = isl_union_set_from_set(isl_set_copy(domain));
2172 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
2173 if (!expr->acc.access[type])
2174 continue;
2175 expr->acc.access[type] =
2176 isl_union_map_gist_domain(expr->acc.access[type],
2177 isl_union_set_copy(uset));
2178 if (!expr->acc.access[type])
2179 break;
2181 isl_union_set_free(uset);
2182 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
2183 if (type < pet_expr_access_end || !expr->acc.index)
2184 return pet_expr_free(expr);
2186 return expr;
2189 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
2190 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2192 struct pet_access_gist_data data = { context, value_bounds };
2194 return pet_expr_map_access(expr, &access_gist, &data);
2197 /* Mark "expr" as a read dependening on "read".
2199 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
2200 int read)
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.read == read)
2208 return expr;
2209 expr = pet_expr_cow(expr);
2210 if (!expr)
2211 return NULL;
2212 expr->acc.read = read;
2214 return expr;
2217 /* Mark "expr" as a write dependening on "write".
2219 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
2220 int write)
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.write == write)
2228 return expr;
2229 expr = pet_expr_cow(expr);
2230 if (!expr)
2231 return NULL;
2232 expr->acc.write = write;
2234 return expr;
2237 /* Mark "expr" as a kill dependening on "kill".
2239 __isl_give pet_expr *pet_expr_access_set_kill(__isl_take pet_expr *expr,
2240 int kill)
2242 if (!expr)
2243 return pet_expr_free(expr);
2244 if (expr->type != pet_expr_access)
2245 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2246 "not an access expression", return pet_expr_free(expr));
2247 if (expr->acc.kill == kill)
2248 return expr;
2249 expr = pet_expr_cow(expr);
2250 if (!expr)
2251 return NULL;
2252 expr->acc.kill = kill;
2254 return expr;
2257 /* Map the access type "type" to the corresponding location
2258 * in the access array.
2259 * In particular, the access relation of type pet_expr_access_killed is
2260 * stored in the element at position pet_expr_access_fake_killed.
2262 static enum pet_expr_access_type internalize_type(
2263 enum pet_expr_access_type type)
2265 if (type == pet_expr_access_killed)
2266 return pet_expr_access_fake_killed;
2267 return type;
2270 /* Replace the access relation of the given "type" of "expr" by "access".
2271 * If the access relation is non-empty and the type is a read or a write,
2272 * then also mark the access expression itself as a read or a write.
2274 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
2275 enum pet_expr_access_type type, __isl_take isl_union_map *access)
2277 int empty;
2279 expr = pet_expr_cow(expr);
2280 if (!expr || !access)
2281 goto error;
2282 if (expr->type != pet_expr_access)
2283 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2284 "not an access expression", goto error);
2285 type = internalize_type(type);
2286 isl_union_map_free(expr->acc.access[type]);
2287 expr->acc.access[type] = access;
2289 if (expr->acc.kill)
2290 return expr;
2292 empty = isl_union_map_is_empty(access);
2293 if (empty < 0)
2294 return pet_expr_free(expr);
2295 if (empty)
2296 return expr;
2298 if (type == pet_expr_access_may_read)
2299 expr = pet_expr_access_set_read(expr, 1);
2300 else
2301 expr = pet_expr_access_set_write(expr, 1);
2303 return expr;
2304 error:
2305 isl_union_map_free(access);
2306 pet_expr_free(expr);
2307 return NULL;
2310 /* Replace the index expression of "expr" by "index" and
2311 * set the array depth accordingly.
2313 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
2314 __isl_take isl_multi_pw_aff *index)
2316 expr = pet_expr_cow(expr);
2317 if (!expr || !index)
2318 goto error;
2319 if (expr->type != pet_expr_access)
2320 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2321 "not an access expression", goto error);
2322 isl_multi_pw_aff_free(expr->acc.index);
2323 expr->acc.index = index;
2324 expr->acc.depth = isl_multi_pw_aff_dim(index, isl_dim_out);
2326 return expr;
2327 error:
2328 isl_multi_pw_aff_free(index);
2329 pet_expr_free(expr);
2330 return NULL;
2333 /* Return the reference identifier of access expression "expr".
2335 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
2337 if (!expr)
2338 return NULL;
2339 if (expr->type != pet_expr_access)
2340 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2341 "not an access expression", return NULL);
2343 return isl_id_copy(expr->acc.ref_id);
2346 /* Replace the reference identifier of access expression "expr" by "ref_id".
2348 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
2349 __isl_take isl_id *ref_id)
2351 expr = pet_expr_cow(expr);
2352 if (!expr || !ref_id)
2353 goto error;
2354 if (expr->type != pet_expr_access)
2355 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2356 "not an access expression", goto error);
2357 isl_id_free(expr->acc.ref_id);
2358 expr->acc.ref_id = ref_id;
2360 return expr;
2361 error:
2362 isl_id_free(ref_id);
2363 pet_expr_free(expr);
2364 return NULL;
2367 /* Tag the access relation "access" with "id".
2368 * That is, insert the id as the range of a wrapped relation
2369 * in the domain of "access".
2371 * If "access" is of the form
2373 * D[i] -> A[a]
2375 * then the result is of the form
2377 * [D[i] -> id[]] -> A[a]
2379 __isl_give isl_union_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
2380 __isl_take isl_union_map *access)
2382 isl_space *space;
2383 isl_multi_aff *add_tag;
2384 isl_id *id;
2386 if (expr->type != pet_expr_access)
2387 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2388 "not an access expression",
2389 return isl_union_map_free(access));
2391 id = isl_id_copy(expr->acc.ref_id);
2392 space = pet_expr_access_get_domain_space(expr);
2393 space = isl_space_from_domain(space);
2394 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2395 add_tag = isl_multi_aff_domain_map(space);
2396 access = isl_union_map_preimage_domain_multi_aff(access, add_tag);
2398 return access;
2401 /* Return the access relation of the given "type" associated to "expr"
2402 * that maps pairs of domain iterations and argument values
2403 * to the corresponding accessed data elements.
2405 * If the requested access relation is explicitly available,
2406 * then return a copy. Otherwise, check if it is irrelevant for
2407 * the access expression and return an empty relation if this is the case.
2408 * Otherwise, introduce the requested access relation in "expr" and
2409 * return a copy.
2411 __isl_give isl_union_map *pet_expr_access_get_dependent_access(
2412 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2414 isl_union_map *access;
2415 int empty;
2417 if (!expr)
2418 return NULL;
2419 if (expr->type != pet_expr_access)
2420 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2421 "not an access expression", return NULL);
2423 type = internalize_type(type);
2424 if (expr->acc.access[type])
2425 return isl_union_map_copy(expr->acc.access[type]);
2427 if (type == pet_expr_access_may_read)
2428 empty = !expr->acc.read;
2429 else
2430 empty = !expr->acc.write;
2432 if (!empty) {
2433 expr = pet_expr_copy(expr);
2434 expr = introduce_access_relations(expr);
2435 if (!expr)
2436 return NULL;
2437 access = isl_union_map_copy(expr->acc.access[type]);
2438 pet_expr_free(expr);
2440 return access;
2443 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr));
2446 /* Return the may read access relation associated to "expr"
2447 * that maps pairs of domain iterations and argument values
2448 * to the corresponding accessed data elements.
2450 __isl_give isl_union_map *pet_expr_access_get_dependent_may_read(
2451 __isl_keep pet_expr *expr)
2453 return pet_expr_access_get_dependent_access(expr,
2454 pet_expr_access_may_read);
2457 /* Return the may write access relation associated to "expr"
2458 * that maps pairs of domain iterations and argument values
2459 * to the corresponding accessed data elements.
2461 __isl_give isl_union_map *pet_expr_access_get_dependent_may_write(
2462 __isl_keep pet_expr *expr)
2464 return pet_expr_access_get_dependent_access(expr,
2465 pet_expr_access_may_write);
2468 /* Return the must write access relation associated to "expr"
2469 * that maps pairs of domain iterations and argument values
2470 * to the corresponding accessed data elements.
2472 __isl_give isl_union_map *pet_expr_access_get_dependent_must_write(
2473 __isl_keep pet_expr *expr)
2475 return pet_expr_access_get_dependent_access(expr,
2476 pet_expr_access_must_write);
2479 /* Return the relation of the given "type" mapping domain iterations
2480 * to the accessed data elements.
2481 * In particular, take the access relation and, in case of may_read
2482 * or may_write, project out the values of the arguments, if any.
2483 * In case of must_write, return the empty relation if there are
2484 * any arguments.
2486 __isl_give isl_union_map *pet_expr_access_get_access(__isl_keep pet_expr *expr,
2487 enum pet_expr_access_type type)
2489 isl_union_map *access;
2490 isl_space *space;
2491 isl_map *map;
2493 if (!expr)
2494 return NULL;
2495 if (expr->type != pet_expr_access)
2496 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2497 "not an access expression", return NULL);
2499 if (expr->n_arg != 0 && type == pet_expr_access_must_write) {
2500 space = pet_expr_access_get_parameter_space(expr);
2501 return isl_union_map_empty(space);
2504 access = pet_expr_access_get_dependent_access(expr, type);
2505 if (expr->n_arg == 0)
2506 return access;
2508 space = isl_multi_pw_aff_get_space(expr->acc.index);
2509 space = isl_space_domain(space);
2510 map = isl_map_universe(isl_space_unwrap(space));
2511 map = isl_map_domain_map(map);
2512 access = isl_union_map_apply_domain(access,
2513 isl_union_map_from_map(map));
2515 return access;
2518 /* Return the relation mapping domain iterations to all possibly
2519 * read data elements.
2521 __isl_give isl_union_map *pet_expr_access_get_may_read(
2522 __isl_keep pet_expr *expr)
2524 return pet_expr_access_get_access(expr, pet_expr_access_may_read);
2527 /* Return the relation mapping domain iterations to all possibly
2528 * written data elements.
2530 __isl_give isl_union_map *pet_expr_access_get_may_write(
2531 __isl_keep pet_expr *expr)
2533 return pet_expr_access_get_access(expr, pet_expr_access_may_write);
2536 /* Return a relation mapping domain iterations to definitely
2537 * written data elements, assuming the statement containing
2538 * the expression is executed.
2540 __isl_give isl_union_map *pet_expr_access_get_must_write(
2541 __isl_keep pet_expr *expr)
2543 return pet_expr_access_get_access(expr, pet_expr_access_must_write);
2546 /* Return the relation of the given "type" mapping domain iterations to
2547 * accessed data elements, with its domain tagged with the reference
2548 * identifier.
2550 static __isl_give isl_union_map *pet_expr_access_get_tagged_access(
2551 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2553 isl_union_map *access;
2555 if (!expr)
2556 return NULL;
2558 access = pet_expr_access_get_access(expr, type);
2559 access = pet_expr_tag_access(expr, access);
2561 return access;
2564 /* Return the relation mapping domain iterations to all possibly
2565 * read data elements, with its domain tagged with the reference
2566 * identifier.
2568 __isl_give isl_union_map *pet_expr_access_get_tagged_may_read(
2569 __isl_keep pet_expr *expr)
2571 return pet_expr_access_get_tagged_access(expr,
2572 pet_expr_access_may_read);
2575 /* Return the relation mapping domain iterations to all possibly
2576 * written data elements, with its domain tagged with the reference
2577 * identifier.
2579 __isl_give isl_union_map *pet_expr_access_get_tagged_may_write(
2580 __isl_keep pet_expr *expr)
2582 return pet_expr_access_get_tagged_access(expr,
2583 pet_expr_access_may_write);
2586 /* Return the operation type of operation expression "expr".
2588 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
2590 if (!expr)
2591 return pet_op_last;
2592 if (expr->type != pet_expr_op)
2593 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2594 "not an operation expression", return pet_op_last);
2596 return expr->op;
2599 /* Replace the operation type of operation expression "expr" by "type".
2601 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
2602 enum pet_op_type type)
2604 if (!expr)
2605 return pet_expr_free(expr);
2606 if (expr->type != pet_expr_op)
2607 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2608 "not an operation expression",
2609 return pet_expr_free(expr));
2610 if (expr->op == type)
2611 return expr;
2612 expr = pet_expr_cow(expr);
2613 if (!expr)
2614 return NULL;
2615 expr->op = type;
2617 return expr;
2620 /* Return the name of the function called by "expr".
2622 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
2624 if (!expr)
2625 return NULL;
2626 if (expr->type != pet_expr_call)
2627 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2628 "not a call expression", return NULL);
2629 return expr->c.name;
2632 /* Replace the name of the function called by "expr" by "name".
2634 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
2635 __isl_keep const char *name)
2637 expr = pet_expr_cow(expr);
2638 if (!expr || !name)
2639 return pet_expr_free(expr);
2640 if (expr->type != pet_expr_call)
2641 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2642 "not a call expression", return pet_expr_free(expr));
2643 free(expr->c.name);
2644 expr->c.name = strdup(name);
2645 if (!expr->c.name)
2646 return pet_expr_free(expr);
2647 return expr;
2650 /* Does the call expression "expr" have an associated function summary?
2652 int pet_expr_call_has_summary(__isl_keep pet_expr *expr)
2654 if (!expr)
2655 return -1;
2656 if (expr->type != pet_expr_call)
2657 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2658 "not a call expression", return -1);
2660 return expr->c.summary != NULL;
2663 /* Return a copy of the function summary associated to
2664 * the call expression "expr".
2666 __isl_give pet_function_summary *pet_expr_call_get_summary(
2667 __isl_keep pet_expr *expr)
2669 if (!expr)
2670 return NULL;
2671 if (expr->type != pet_expr_call)
2672 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2673 "not a call expression", return NULL);
2675 return pet_function_summary_copy(expr->c.summary);
2678 /* Replace the function summary associated to the call expression "expr"
2679 * by "summary".
2681 __isl_give pet_expr *pet_expr_call_set_summary(__isl_take pet_expr *expr,
2682 __isl_take pet_function_summary *summary)
2684 expr = pet_expr_cow(expr);
2685 if (!expr || !summary)
2686 goto error;
2687 if (expr->type != pet_expr_call)
2688 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2689 "not a call expression", goto error);
2690 pet_function_summary_free(expr->c.summary);
2691 expr->c.summary = summary;
2692 return expr;
2693 error:
2694 pet_function_summary_free(summary);
2695 return pet_expr_free(expr);
2698 /* Replace the type of the cast performed by "expr" by "name".
2700 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
2701 __isl_keep const char *name)
2703 expr = pet_expr_cow(expr);
2704 if (!expr || !name)
2705 return pet_expr_free(expr);
2706 if (expr->type != pet_expr_cast)
2707 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2708 "not a cast expression", return pet_expr_free(expr));
2709 free(expr->type_name);
2710 expr->type_name = strdup(name);
2711 if (!expr->type_name)
2712 return pet_expr_free(expr);
2713 return expr;
2716 /* Return the value of the integer represented by "expr".
2718 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
2720 if (!expr)
2721 return NULL;
2722 if (expr->type != pet_expr_int)
2723 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2724 "not an int expression", return NULL);
2726 return isl_val_copy(expr->i);
2729 /* Replace the value of the integer represented by "expr" by "v".
2731 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
2732 __isl_take isl_val *v)
2734 expr = pet_expr_cow(expr);
2735 if (!expr || !v)
2736 goto error;
2737 if (expr->type != pet_expr_int)
2738 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2739 "not an int expression", goto error);
2740 isl_val_free(expr->i);
2741 expr->i = v;
2743 return expr;
2744 error:
2745 isl_val_free(v);
2746 pet_expr_free(expr);
2747 return NULL;
2750 /* Replace the value and string representation of the double
2751 * represented by "expr" by "d" and "s".
2753 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2754 double d, __isl_keep const char *s)
2756 expr = pet_expr_cow(expr);
2757 if (!expr || !s)
2758 return pet_expr_free(expr);
2759 if (expr->type != pet_expr_double)
2760 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2761 "not a double expression", return pet_expr_free(expr));
2762 expr->d.val = d;
2763 free(expr->d.s);
2764 expr->d.s = strdup(s);
2765 if (!expr->d.s)
2766 return pet_expr_free(expr);
2767 return expr;
2770 /* Return a string representation of the double expression "expr".
2772 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2774 if (!expr)
2775 return NULL;
2776 if (expr->type != pet_expr_double)
2777 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2778 "not a double expression", return NULL);
2779 return strdup(expr->d.s);
2782 /* Return a piecewise affine expression defined on the specified domain
2783 * that represents NaN.
2785 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2787 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2790 /* This function is called when we come across an access that is
2791 * nested in what is supposed to be an affine expression.
2792 * "pc" is the context in which the affine expression is created.
2793 * If nesting is allowed in "pc", we return an affine expression that is
2794 * equal to a new parameter corresponding to this nested access.
2795 * Otherwise, we return NaN.
2797 * Note that we currently don't allow nested accesses themselves
2798 * to contain any nested accesses, so we check if "expr" itself
2799 * involves any nested accesses (either explicitly as arguments
2800 * or implicitly through parameters) and return NaN if it does.
2802 * The new parameter is resolved in resolve_nested.
2804 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2805 __isl_keep pet_context *pc)
2807 isl_ctx *ctx;
2808 isl_id *id;
2809 isl_space *space;
2810 isl_local_space *ls;
2811 isl_aff *aff;
2812 int nested;
2814 if (!expr || !pc)
2815 return NULL;
2816 if (!pet_context_allow_nesting(pc))
2817 return non_affine(pet_context_get_space(pc));
2819 if (pet_expr_get_type(expr) != pet_expr_access)
2820 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2821 "not an access expression", return NULL);
2823 if (expr->n_arg > 0)
2824 return non_affine(pet_context_get_space(pc));
2826 space = pet_expr_access_get_parameter_space(expr);
2827 nested = pet_nested_any_in_space(space);
2828 isl_space_free(space);
2829 if (nested)
2830 return non_affine(pet_context_get_space(pc));
2832 ctx = pet_expr_get_ctx(expr);
2833 id = pet_nested_pet_expr(pet_expr_copy(expr));
2834 space = pet_context_get_space(pc);
2835 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2837 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2838 ls = isl_local_space_from_space(space);
2839 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2841 return isl_pw_aff_from_aff(aff);
2844 /* Extract an affine expression from the access pet_expr "expr".
2845 * "pc" is the context in which the affine expression is created.
2847 * If "expr" is actually an affine expression rather than
2848 * a real access, then we return that expression.
2849 * Otherwise, we require that "expr" is of an integral type.
2850 * If not, we return NaN.
2852 * If the variable has been assigned a known affine expression,
2853 * then we return that expression.
2855 * Otherwise, we return an expression that is equal to a parameter
2856 * representing "expr" (if "allow_nested" is set).
2858 static __isl_give isl_pw_aff *extract_affine_from_access(
2859 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2861 isl_id *id;
2863 if (pet_expr_is_affine(expr))
2864 return pet_expr_get_affine(expr);
2866 if (pet_expr_get_type_size(expr) == 0)
2867 return non_affine(pet_context_get_space(pc));
2869 if (!pet_expr_is_scalar_access(expr))
2870 return nested_access(expr, pc);
2872 id = pet_expr_access_get_id(expr);
2873 if (pet_context_is_assigned(pc, id))
2874 return pet_context_get_value(pc, id);
2876 isl_id_free(id);
2877 return nested_access(expr, pc);
2880 /* Construct an affine expression from the integer constant "expr".
2881 * "pc" is the context in which the affine expression is created.
2883 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2884 __isl_keep pet_context *pc)
2886 isl_local_space *ls;
2887 isl_aff *aff;
2889 if (!expr)
2890 return NULL;
2892 ls = isl_local_space_from_space(pet_context_get_space(pc));
2893 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2895 return isl_pw_aff_from_aff(aff);
2898 /* Extract an affine expression from an addition or subtraction operation.
2899 * Return NaN if we are unable to extract an affine expression.
2901 * "pc" is the context in which the affine expression is created.
2903 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2904 __isl_keep pet_context *pc)
2906 isl_pw_aff *lhs;
2907 isl_pw_aff *rhs;
2909 if (!expr)
2910 return NULL;
2911 if (expr->n_arg != 2)
2912 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2913 "expecting two arguments", return NULL);
2915 lhs = pet_expr_extract_affine(expr->args[0], pc);
2916 rhs = pet_expr_extract_affine(expr->args[1], pc);
2918 switch (pet_expr_op_get_type(expr)) {
2919 case pet_op_add:
2920 return isl_pw_aff_add(lhs, rhs);
2921 case pet_op_sub:
2922 return isl_pw_aff_sub(lhs, rhs);
2923 default:
2924 isl_pw_aff_free(lhs);
2925 isl_pw_aff_free(rhs);
2926 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2927 "not an addition or subtraction operation",
2928 return NULL);
2933 /* Extract an affine expression from an integer division or a modulo operation.
2934 * Return NaN if we are unable to extract an affine expression.
2936 * "pc" is the context in which the affine expression is created.
2938 * In particular, if "expr" is lhs/rhs, then return
2940 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2942 * If "expr" is lhs%rhs, then return
2944 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2946 * If the second argument (rhs) is not a (positive) integer constant,
2947 * then we fail to extract an affine expression.
2949 * We simplify the result in the context of the domain of "pc" in case
2950 * this domain implies that lhs >= 0 (or < 0).
2952 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2953 __isl_keep pet_context *pc)
2955 int is_cst;
2956 isl_pw_aff *lhs;
2957 isl_pw_aff *rhs;
2958 isl_pw_aff *res;
2960 if (!expr)
2961 return NULL;
2962 if (expr->n_arg != 2)
2963 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2964 "expecting two arguments", return NULL);
2966 rhs = pet_expr_extract_affine(expr->args[1], pc);
2968 is_cst = isl_pw_aff_is_cst(rhs);
2969 if (is_cst < 0 || !is_cst) {
2970 isl_pw_aff_free(rhs);
2971 return non_affine(pet_context_get_space(pc));
2974 lhs = pet_expr_extract_affine(expr->args[0], pc);
2976 switch (pet_expr_op_get_type(expr)) {
2977 case pet_op_div:
2978 res = isl_pw_aff_tdiv_q(lhs, rhs);
2979 break;
2980 case pet_op_mod:
2981 res = isl_pw_aff_tdiv_r(lhs, rhs);
2982 break;
2983 default:
2984 isl_pw_aff_free(lhs);
2985 isl_pw_aff_free(rhs);
2986 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2987 "not a div or mod operator", return NULL);
2990 return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc));
2993 /* Extract an affine expression from a multiplication operation.
2994 * Return NaN if we are unable to extract an affine expression.
2995 * In particular, if neither of the arguments is a (piecewise) constant
2996 * then we return NaN.
2998 * "pc" is the context in which the affine expression is created.
3000 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
3001 __isl_keep pet_context *pc)
3003 int lhs_cst, rhs_cst;
3004 isl_pw_aff *lhs;
3005 isl_pw_aff *rhs;
3007 if (!expr)
3008 return NULL;
3009 if (expr->n_arg != 2)
3010 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3011 "expecting two arguments", return NULL);
3013 lhs = pet_expr_extract_affine(expr->args[0], pc);
3014 rhs = pet_expr_extract_affine(expr->args[1], pc);
3016 lhs_cst = isl_pw_aff_is_cst(lhs);
3017 rhs_cst = isl_pw_aff_is_cst(rhs);
3018 if (lhs_cst >= 0 && rhs_cst >= 0 && (lhs_cst || rhs_cst))
3019 return isl_pw_aff_mul(lhs, rhs);
3021 isl_pw_aff_free(lhs);
3022 isl_pw_aff_free(rhs);
3024 if (lhs_cst < 0 || rhs_cst < 0)
3025 return NULL;
3027 return non_affine(pet_context_get_space(pc));
3030 /* Extract an affine expression from a negation operation.
3031 * Return NaN if we are unable to extract an affine expression.
3033 * "pc" is the context in which the affine expression is created.
3035 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
3036 __isl_keep pet_context *pc)
3038 isl_pw_aff *res;
3040 if (!expr)
3041 return NULL;
3042 if (expr->n_arg != 1)
3043 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3044 "expecting one argument", return NULL);
3046 res = pet_expr_extract_affine(expr->args[0], pc);
3047 return isl_pw_aff_neg(res);
3050 /* Extract an affine expression from a conditional operation.
3051 * Return NaN if we are unable to extract an affine expression.
3053 * "pc" is the context in which the affine expression is created.
3055 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
3056 __isl_keep pet_context *pc)
3058 isl_pw_aff *cond, *lhs, *rhs;
3060 if (!expr)
3061 return NULL;
3062 if (expr->n_arg != 3)
3063 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3064 "expecting three arguments", return NULL);
3066 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3067 lhs = pet_expr_extract_affine(expr->args[1], pc);
3068 rhs = pet_expr_extract_affine(expr->args[2], pc);
3070 return isl_pw_aff_cond(cond, lhs, rhs);
3073 /* Limit the domain of "pwaff" to those elements where the function
3074 * value satisfies
3076 * 2^{width-1} <= pwaff < 2^{width-1}
3078 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
3079 unsigned width)
3081 isl_ctx *ctx;
3082 isl_val *v;
3083 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
3084 isl_local_space *ls = isl_local_space_from_space(space);
3085 isl_aff *bound;
3086 isl_set *dom;
3087 isl_pw_aff *b;
3089 ctx = isl_pw_aff_get_ctx(pwaff);
3090 v = isl_val_int_from_ui(ctx, width - 1);
3091 v = isl_val_2exp(v);
3093 bound = isl_aff_zero_on_domain(ls);
3094 bound = isl_aff_add_constant_val(bound, v);
3095 b = isl_pw_aff_from_aff(bound);
3097 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
3098 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3100 b = isl_pw_aff_neg(b);
3101 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
3102 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3104 return pwaff;
3107 /* Handle potential overflows on signed computations.
3109 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
3110 * then we adjust the domain of "pa" to avoid overflows.
3112 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
3113 unsigned width)
3115 isl_ctx *ctx;
3116 struct pet_options *options;
3118 if (!pa)
3119 return NULL;
3121 ctx = isl_pw_aff_get_ctx(pa);
3122 options = isl_ctx_peek_pet_options(ctx);
3123 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
3124 pa = avoid_overflow(pa, width);
3126 return pa;
3129 /* Extract an affine expression from some an operation.
3130 * Return NaN if we are unable to extract an affine expression.
3131 * If the result of a binary (non boolean) operation is unsigned,
3132 * then we wrap it based on the size of the type. If the result is signed,
3133 * then we ensure that no overflow occurs.
3135 * "pc" is the context in which the affine expression is created.
3137 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
3138 __isl_keep pet_context *pc)
3140 isl_pw_aff *res;
3141 int type_size;
3143 switch (pet_expr_op_get_type(expr)) {
3144 case pet_op_add:
3145 case pet_op_sub:
3146 res = extract_affine_add_sub(expr, pc);
3147 break;
3148 case pet_op_div:
3149 case pet_op_mod:
3150 res = extract_affine_div_mod(expr, pc);
3151 break;
3152 case pet_op_mul:
3153 res = extract_affine_mul(expr, pc);
3154 break;
3155 case pet_op_minus:
3156 return extract_affine_neg(expr, pc);
3157 case pet_op_cond:
3158 return extract_affine_cond(expr, pc);
3159 case pet_op_eq:
3160 case pet_op_ne:
3161 case pet_op_le:
3162 case pet_op_ge:
3163 case pet_op_lt:
3164 case pet_op_gt:
3165 case pet_op_land:
3166 case pet_op_lor:
3167 case pet_op_lnot:
3168 return pet_expr_extract_affine_condition(expr, pc);
3169 default:
3170 return non_affine(pet_context_get_space(pc));
3173 if (!res)
3174 return NULL;
3175 if (isl_pw_aff_involves_nan(res)) {
3176 isl_space *space = isl_pw_aff_get_domain_space(res);
3177 isl_pw_aff_free(res);
3178 return non_affine(space);
3181 type_size = pet_expr_get_type_size(expr);
3182 if (type_size > 0)
3183 res = pet_wrap_pw_aff(res, type_size);
3184 else
3185 res = signed_overflow(res, -type_size);
3187 return res;
3190 /* Internal data structure for affine builtin function declarations.
3192 * "pencil" is set if the builtin is pencil specific.
3193 * "n_args" is the number of arguments the function takes.
3194 * "name" is the function name.
3196 struct affine_builtin_decl {
3197 int pencil;
3198 int n_args;
3199 const char *name;
3202 static struct affine_builtin_decl affine_builtins[] = {
3203 { 0, 2, "min" },
3204 { 1, 2, "imin" },
3205 { 1, 2, "umin" },
3206 { 0, 2, "max" },
3207 { 1, 2, "imax" },
3208 { 1, 2, "umax" },
3209 { 0, 2, "intMod" },
3210 { 0, 2, "intFloor" },
3211 { 0, 2, "intCeil" },
3212 { 0, 2, "floord" },
3213 { 0, 2, "ceild" }
3216 /* List of min and max builtin functions.
3218 static const char *min_max_builtins[] = {
3219 "min", "imin", "umin",
3220 "max", "imax", "umax"
3223 /* Is a function call to "name" with "n_args" arguments a call to a
3224 * builtin function for which we can construct an affine expression?
3225 * pencil specific builtins are only recognized if "pencil" is set.
3227 static int is_affine_builtin(int pencil, int n_args, const char *name)
3229 int i;
3231 for (i = 0; i < ARRAY_SIZE(affine_builtins); ++i) {
3232 struct affine_builtin_decl *decl = &affine_builtins[i];
3234 if (decl->pencil && !pencil)
3235 continue;
3236 if (decl->n_args == n_args && !strcmp(decl->name, name))
3237 return 1;
3240 return 0;
3243 /* Is function "name" a known min or max builtin function?
3245 static int is_min_or_max_builtin(const char *name)
3247 int i;
3249 for (i = 0; i < ARRAY_SIZE(min_max_builtins); ++i)
3250 if (!strcmp(min_max_builtins[i], name))
3251 return 1;
3253 return 0;
3256 /* Extract an affine expression from some special function calls.
3257 * Return NaN if we are unable to extract an affine expression.
3258 * In particular, we handle "min", "max", "ceild", "floord",
3259 * "intMod", "intFloor" and "intCeil".
3260 * In case of the latter five, the second argument needs to be
3261 * a (positive) integer constant.
3262 * If the pencil option is set, then we also handle "{i,u}min" and
3263 * "{i,u}max".
3265 * "pc" is the context in which the affine expression is created.
3267 static __isl_give isl_pw_aff *extract_affine_from_call(
3268 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3270 isl_ctx *ctx;
3271 isl_pw_aff *aff1, *aff2;
3272 int n;
3273 const char *name;
3274 struct pet_options *options;
3276 if (!expr)
3277 return NULL;
3278 ctx = pet_expr_get_ctx(expr);
3279 options = isl_ctx_peek_pet_options(ctx);
3281 n = pet_expr_get_n_arg(expr);
3282 name = pet_expr_call_get_name(expr);
3283 if (!is_affine_builtin(options->pencil, n, name))
3284 return non_affine(pet_context_get_space(pc));
3286 if (is_min_or_max_builtin(name)) {
3287 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3288 aff2 = pet_expr_extract_affine(expr->args[1], pc);
3290 if (strstr(name, "min"))
3291 aff1 = isl_pw_aff_min(aff1, aff2);
3292 else
3293 aff1 = isl_pw_aff_max(aff1, aff2);
3294 } else if (!strcmp(name, "intMod")) {
3295 isl_val *v;
3297 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3298 return non_affine(pet_context_get_space(pc));
3299 v = pet_expr_int_get_val(expr->args[1]);
3300 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3301 aff1 = isl_pw_aff_mod_val(aff1, v);
3302 } else {
3303 isl_val *v;
3305 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3306 return non_affine(pet_context_get_space(pc));
3307 v = pet_expr_int_get_val(expr->args[1]);
3308 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3309 aff1 = isl_pw_aff_scale_down_val(aff1, v);
3310 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
3311 aff1 = isl_pw_aff_floor(aff1);
3312 else
3313 aff1 = isl_pw_aff_ceil(aff1);
3316 return aff1;
3319 /* Extract an affine expression from "expr", if possible.
3320 * Otherwise return NaN.
3322 * "pc" is the context in which the affine expression is created.
3324 * Store the result in "pc" such that it can be reused in case
3325 * pet_expr_extract_affine is called again on the same pair of
3326 * "expr" and "pc".
3328 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
3329 __isl_keep pet_context *pc)
3331 isl_maybe_isl_pw_aff m;
3332 isl_pw_aff *pa;
3334 if (!expr)
3335 return NULL;
3337 m = pet_context_get_extracted_affine(pc, expr);
3338 if (m.valid < 0 || m.valid)
3339 return m.value;
3341 switch (pet_expr_get_type(expr)) {
3342 case pet_expr_access:
3343 pa = extract_affine_from_access(expr, pc);
3344 break;
3345 case pet_expr_int:
3346 pa = extract_affine_from_int(expr, pc);
3347 break;
3348 case pet_expr_op:
3349 pa = extract_affine_from_op(expr, pc);
3350 break;
3351 case pet_expr_call:
3352 pa = extract_affine_from_call(expr, pc);
3353 break;
3354 case pet_expr_cast:
3355 case pet_expr_double:
3356 case pet_expr_error:
3357 pa = non_affine(pet_context_get_space(pc));
3358 break;
3361 if (pet_context_set_extracted_affine(pc, expr, pa) < 0)
3362 return isl_pw_aff_free(pa);
3364 return pa;
3367 /* Extract an affine expressions representing the comparison "LHS op RHS"
3368 * Return NaN if we are unable to extract such an affine expression.
3370 * "pc" is the context in which the affine expression is created.
3372 * If the comparison is of the form
3374 * a <= min(b,c)
3376 * then the expression is constructed as the conjunction of
3377 * the comparisons
3379 * a <= b and a <= c
3381 * A similar optimization is performed for max(a,b) <= c.
3382 * We do this because that will lead to simpler representations
3383 * of the expression.
3384 * If isl is ever enhanced to explicitly deal with min and max expressions,
3385 * this optimization can be removed.
3387 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
3388 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
3389 __isl_keep pet_context *pc)
3391 isl_pw_aff *lhs_pa, *rhs_pa;
3393 if (op == pet_op_gt)
3394 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
3395 if (op == pet_op_ge)
3396 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
3398 if (op == pet_op_lt || op == pet_op_le) {
3399 if (pet_expr_is_min(rhs)) {
3400 lhs_pa = pet_expr_extract_comparison(op, lhs,
3401 rhs->args[0], pc);
3402 rhs_pa = pet_expr_extract_comparison(op, lhs,
3403 rhs->args[1], pc);
3404 return pet_and(lhs_pa, rhs_pa);
3406 if (pet_expr_is_max(lhs)) {
3407 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
3408 rhs, pc);
3409 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
3410 rhs, pc);
3411 return pet_and(lhs_pa, rhs_pa);
3415 lhs_pa = pet_expr_extract_affine(lhs, pc);
3416 rhs_pa = pet_expr_extract_affine(rhs, pc);
3418 return pet_comparison(op, lhs_pa, rhs_pa);
3421 /* Extract an affine expressions from the comparison "expr".
3422 * Return NaN if we are unable to extract such an affine expression.
3424 * "pc" is the context in which the affine expression is created.
3426 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
3427 __isl_keep pet_context *pc)
3429 enum pet_op_type type;
3431 if (!expr)
3432 return NULL;
3433 if (expr->n_arg != 2)
3434 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3435 "expecting two arguments", return NULL);
3437 type = pet_expr_op_get_type(expr);
3438 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
3439 pc);
3442 /* Extract an affine expression representing the boolean operation
3443 * expressed by "expr".
3444 * Return NaN if we are unable to extract an affine expression.
3446 * "pc" is the context in which the affine expression is created.
3448 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
3449 __isl_keep pet_context *pc)
3451 isl_pw_aff *lhs, *rhs;
3452 int n;
3454 if (!expr)
3455 return NULL;
3457 n = pet_expr_get_n_arg(expr);
3458 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
3459 if (n == 1)
3460 return pet_not(lhs);
3462 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
3463 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
3466 /* Extract the affine expression "expr != 0 ? 1 : 0".
3467 * Return NaN if we are unable to extract an affine expression.
3469 * "pc" is the context in which the affine expression is created.
3471 static __isl_give isl_pw_aff *extract_implicit_condition(
3472 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3474 isl_pw_aff *res;
3476 res = pet_expr_extract_affine(expr, pc);
3477 return pet_to_bool(res);
3480 /* Extract a boolean affine expression from "expr".
3481 * Return NaN if we are unable to extract an affine expression.
3483 * "pc" is the context in which the affine expression is created.
3485 * If "expr" is neither a comparison nor a boolean operation,
3486 * then we assume it is an affine expression and return the
3487 * boolean expression "expr != 0 ? 1 : 0".
3489 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
3490 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3492 if (!expr)
3493 return NULL;
3495 if (pet_expr_is_comparison(expr))
3496 return extract_comparison(expr, pc);
3497 if (pet_expr_is_boolean(expr))
3498 return extract_boolean(expr, pc);
3500 return extract_implicit_condition(expr, pc);
3503 /* Check if "expr" is an assume expression and if its single argument
3504 * can be converted to an affine expression in the context of "pc".
3505 * If so, replace the argument by the affine expression.
3507 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
3508 __isl_keep pet_context *pc)
3510 isl_pw_aff *cond;
3511 isl_multi_pw_aff *index;
3513 if (!expr)
3514 return NULL;
3515 if (!pet_expr_is_assume(expr))
3516 return expr;
3517 if (expr->n_arg != 1)
3518 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3519 "expecting one argument", return pet_expr_free(expr));
3521 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3522 if (!cond)
3523 return pet_expr_free(expr);
3524 if (isl_pw_aff_involves_nan(cond)) {
3525 isl_pw_aff_free(cond);
3526 return expr;
3529 index = isl_multi_pw_aff_from_pw_aff(cond);
3530 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
3532 return expr;
3535 /* Return the number of bits needed to represent the type of "expr".
3536 * See the description of the type_size field of pet_expr.
3538 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
3540 return expr ? expr->type_size : 0;
3543 /* Replace the number of bits needed to represent the type of "expr"
3544 * by "type_size".
3545 * See the description of the type_size field of pet_expr.
3547 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
3548 int type_size)
3550 expr = pet_expr_cow(expr);
3551 if (!expr)
3552 return NULL;
3554 expr->type_size = type_size;
3556 return expr;
3559 /* Extend an access expression "expr" with an additional index "index".
3560 * In particular, add "index" as an extra argument to "expr" and
3561 * adjust the index expression of "expr" to refer to this extra argument.
3562 * The caller is responsible for calling pet_expr_access_set_depth
3563 * to update the corresponding access relation.
3565 * Note that we only collect the individual index expressions as
3566 * arguments of "expr" here.
3567 * An attempt to integrate them into the index expression of "expr"
3568 * is performed in pet_expr_access_plug_in_args.
3570 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
3571 __isl_take pet_expr *index)
3573 int n;
3574 isl_space *space;
3575 isl_local_space *ls;
3576 isl_pw_aff *pa;
3578 expr = pet_expr_cow(expr);
3579 if (!expr || !index)
3580 goto error;
3581 if (expr->type != pet_expr_access)
3582 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3583 "not an access pet_expr", goto error);
3585 n = pet_expr_get_n_arg(expr);
3586 expr = pet_expr_insert_arg(expr, n, index);
3587 if (!expr)
3588 return NULL;
3590 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3591 ls = isl_local_space_from_space(space);
3592 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
3593 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
3594 if (!expr->acc.index)
3595 return pet_expr_free(expr);
3597 return expr;
3598 error:
3599 pet_expr_free(expr);
3600 pet_expr_free(index);
3601 return NULL;
3604 /* Extend an access expression "expr" with an additional member acces to "id".
3605 * In particular, extend the index expression of "expr" to include
3606 * the additional member access.
3607 * The caller is responsible for calling pet_expr_access_set_depth
3608 * to update the corresponding access relation.
3610 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
3611 __isl_take isl_id *id)
3613 isl_space *space;
3614 isl_multi_pw_aff *field_access;
3616 expr = pet_expr_cow(expr);
3617 if (!expr || !id)
3618 goto error;
3619 if (expr->type != pet_expr_access)
3620 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3621 "not an access pet_expr", goto error);
3623 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3624 space = isl_space_from_domain(space);
3625 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3626 field_access = isl_multi_pw_aff_zero(space);
3627 expr->acc.index = pet_array_member(expr->acc.index, field_access);
3628 if (!expr->acc.index)
3629 return pet_expr_free(expr);
3631 return expr;
3632 error:
3633 pet_expr_free(expr);
3634 isl_id_free(id);
3635 return NULL;
3638 /* Prefix the access expression "expr" with "prefix".
3639 * If "add" is set, then it is not the index expression "prefix" itself
3640 * that was passed to the function, but its address.
3642 __isl_give pet_expr *pet_expr_access_patch(__isl_take pet_expr *expr,
3643 __isl_take isl_multi_pw_aff *prefix, int add)
3645 enum pet_expr_access_type type;
3647 expr = pet_expr_cow(expr);
3648 if (!expr || !prefix)
3649 goto error;
3650 if (expr->type != pet_expr_access)
3651 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3652 "not an access pet_expr", goto error);
3654 expr->acc.depth += isl_multi_pw_aff_dim(prefix, isl_dim_out) - add;
3655 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
3656 if (!expr->acc.access[type])
3657 continue;
3658 expr->acc.access[type] = pet_patch_union_map(
3659 isl_multi_pw_aff_copy(prefix), expr->acc.access[type],
3660 add, 0);
3661 if (!expr->acc.access[type])
3662 break;
3664 expr->acc.index = pet_patch_multi_pw_aff(prefix, expr->acc.index, add);
3665 if (!expr->acc.index || type < pet_expr_access_end)
3666 return pet_expr_free(expr);
3668 return expr;
3669 error:
3670 pet_expr_free(expr);
3671 isl_multi_pw_aff_free(prefix);
3672 return NULL;
3675 /* Dump the arguments of "expr" to "p" as a YAML sequence keyed
3676 * by "args", if there are any such arguments.
3678 static __isl_give isl_printer *dump_arguments(__isl_keep pet_expr *expr,
3679 __isl_take isl_printer *p)
3681 int i;
3683 if (expr->n_arg == 0)
3684 return p;
3686 p = isl_printer_print_str(p, "args");
3687 p = isl_printer_yaml_next(p);
3688 p = isl_printer_yaml_start_sequence(p);
3689 for (i = 0; i < expr->n_arg; ++i) {
3690 p = pet_expr_print(expr->args[i], p);
3691 p = isl_printer_yaml_next(p);
3693 p = isl_printer_yaml_end_sequence(p);
3695 return p;
3698 /* Print "expr" to "p" in YAML format.
3700 __isl_give isl_printer *pet_expr_print(__isl_keep pet_expr *expr,
3701 __isl_take isl_printer *p)
3703 if (!expr || !p)
3704 return isl_printer_free(p);
3706 switch (expr->type) {
3707 case pet_expr_double:
3708 p = isl_printer_print_str(p, expr->d.s);
3709 break;
3710 case pet_expr_int:
3711 p = isl_printer_print_val(p, expr->i);
3712 break;
3713 case pet_expr_access:
3714 p = isl_printer_yaml_start_mapping(p);
3715 if (expr->acc.ref_id) {
3716 p = isl_printer_print_str(p, "ref_id");
3717 p = isl_printer_yaml_next(p);
3718 p = isl_printer_print_id(p, expr->acc.ref_id);
3719 p = isl_printer_yaml_next(p);
3721 p = isl_printer_print_str(p, "index");
3722 p = isl_printer_yaml_next(p);
3723 p = isl_printer_print_multi_pw_aff(p, expr->acc.index);
3724 p = isl_printer_yaml_next(p);
3725 p = isl_printer_print_str(p, "depth");
3726 p = isl_printer_yaml_next(p);
3727 p = isl_printer_print_int(p, expr->acc.depth);
3728 p = isl_printer_yaml_next(p);
3729 if (expr->acc.kill) {
3730 p = isl_printer_print_str(p, "kill");
3731 p = isl_printer_yaml_next(p);
3732 p = isl_printer_print_int(p, 1);
3733 p = isl_printer_yaml_next(p);
3734 } else {
3735 p = isl_printer_print_str(p, "read");
3736 p = isl_printer_yaml_next(p);
3737 p = isl_printer_print_int(p, expr->acc.read);
3738 p = isl_printer_yaml_next(p);
3739 p = isl_printer_print_str(p, "write");
3740 p = isl_printer_yaml_next(p);
3741 p = isl_printer_print_int(p, expr->acc.write);
3742 p = isl_printer_yaml_next(p);
3744 if (expr->acc.access[pet_expr_access_may_read]) {
3745 p = isl_printer_print_str(p, "may_read");
3746 p = isl_printer_yaml_next(p);
3747 p = isl_printer_print_union_map(p,
3748 expr->acc.access[pet_expr_access_may_read]);
3749 p = isl_printer_yaml_next(p);
3751 if (expr->acc.access[pet_expr_access_may_write]) {
3752 p = isl_printer_print_str(p, "may_write");
3753 p = isl_printer_yaml_next(p);
3754 p = isl_printer_print_union_map(p,
3755 expr->acc.access[pet_expr_access_may_write]);
3756 p = isl_printer_yaml_next(p);
3758 if (expr->acc.access[pet_expr_access_must_write]) {
3759 p = isl_printer_print_str(p, "must_write");
3760 p = isl_printer_yaml_next(p);
3761 p = isl_printer_print_union_map(p,
3762 expr->acc.access[pet_expr_access_must_write]);
3763 p = isl_printer_yaml_next(p);
3765 p = dump_arguments(expr, p);
3766 p = isl_printer_yaml_end_mapping(p);
3767 break;
3768 case pet_expr_op:
3769 p = isl_printer_yaml_start_mapping(p);
3770 p = isl_printer_print_str(p, "op");
3771 p = isl_printer_yaml_next(p);
3772 p = isl_printer_print_str(p, op_str[expr->op]);
3773 p = isl_printer_yaml_next(p);
3774 p = dump_arguments(expr, p);
3775 p = isl_printer_yaml_end_mapping(p);
3776 break;
3777 case pet_expr_call:
3778 p = isl_printer_yaml_start_mapping(p);
3779 p = isl_printer_print_str(p, "call");
3780 p = isl_printer_yaml_next(p);
3781 p = isl_printer_print_str(p, expr->c.name);
3782 p = isl_printer_print_str(p, "/");
3783 p = isl_printer_print_int(p, expr->n_arg);
3784 p = isl_printer_yaml_next(p);
3785 p = dump_arguments(expr, p);
3786 if (expr->c.summary) {
3787 p = isl_printer_print_str(p, "summary");
3788 p = isl_printer_yaml_next(p);
3789 p = pet_function_summary_print(expr->c.summary, p);
3791 p = isl_printer_yaml_end_mapping(p);
3792 break;
3793 case pet_expr_cast:
3794 p = isl_printer_yaml_start_mapping(p);
3795 p = isl_printer_print_str(p, "cast");
3796 p = isl_printer_yaml_next(p);
3797 p = isl_printer_print_str(p, expr->type_name);
3798 p = isl_printer_yaml_next(p);
3799 p = dump_arguments(expr, p);
3800 p = isl_printer_yaml_end_mapping(p);
3801 break;
3802 case pet_expr_error:
3803 p = isl_printer_print_str(p, "ERROR");
3804 break;
3807 return p;
3810 /* Dump "expr" to stderr with indentation "indent".
3812 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
3814 isl_printer *p;
3816 if (!expr)
3817 return;
3819 p = isl_printer_to_file(pet_expr_get_ctx(expr), stderr);
3820 p = isl_printer_set_indent(p, indent);
3821 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
3822 p = isl_printer_start_line(p);
3823 p = pet_expr_print(expr, p);
3825 isl_printer_free(p);
3828 void pet_expr_dump(__isl_keep pet_expr *expr)
3830 pet_expr_dump_with_indent(expr, 0);