pet_expr_access_set_depth: drop unused variables
[pet.git] / expr.c
blob4763d1a9a3e3e01656f7ce0bc0ecc318f8ee28cb
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_assign] = "=",
76 [pet_op_add] = "+",
77 [pet_op_sub] = "-",
78 [pet_op_mul] = "*",
79 [pet_op_div] = "/",
80 [pet_op_mod] = "%",
81 [pet_op_shl] = "<<",
82 [pet_op_shr] = ">>",
83 [pet_op_eq] = "==",
84 [pet_op_ne] = "!=",
85 [pet_op_le] = "<=",
86 [pet_op_ge] = ">=",
87 [pet_op_lt] = "<",
88 [pet_op_gt] = ">",
89 [pet_op_minus] = "-",
90 [pet_op_post_inc] = "++",
91 [pet_op_post_dec] = "--",
92 [pet_op_pre_inc] = "++",
93 [pet_op_pre_dec] = "--",
94 [pet_op_address_of] = "&",
95 [pet_op_and] = "&",
96 [pet_op_xor] = "^",
97 [pet_op_or] = "|",
98 [pet_op_not] = "~",
99 [pet_op_land] = "&&",
100 [pet_op_lor] = "||",
101 [pet_op_lnot] = "!",
102 [pet_op_cond] = "?:",
103 [pet_op_assume] = "assume",
104 [pet_op_kill] = "kill"
107 const char *pet_op_str(enum pet_op_type op)
109 return op_str[op];
112 int pet_op_is_inc_dec(enum pet_op_type op)
114 return op == pet_op_post_inc || op == pet_op_post_dec ||
115 op == pet_op_pre_inc || op == pet_op_pre_dec;
118 const char *pet_type_str(enum pet_expr_type type)
120 return type_str[type];
123 enum pet_op_type pet_str_op(const char *str)
125 int i;
127 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
128 if (!strcmp(op_str[i], str))
129 return i;
131 return -1;
134 enum pet_expr_type pet_str_type(const char *str)
136 int i;
138 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
139 if (!strcmp(type_str[i], str))
140 return i;
142 return -1;
145 /* Construct a pet_expr of the given type.
147 __isl_give pet_expr *pet_expr_alloc(isl_ctx *ctx, enum pet_expr_type type)
149 pet_expr *expr;
151 expr = isl_calloc_type(ctx, struct pet_expr);
152 if (!expr)
153 return NULL;
155 expr->ctx = ctx;
156 isl_ctx_ref(ctx);
157 expr->type = type;
158 expr->ref = 1;
160 return expr;
163 /* Construct an access pet_expr from an index expression.
164 * By default, the access is considered to be a read access.
165 * The initial depth is set from the index expression and
166 * may still be updated by the caller before the access relation
167 * is created.
169 __isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
171 isl_ctx *ctx;
172 pet_expr *expr;
174 if (!index)
175 return NULL;
176 ctx = isl_multi_pw_aff_get_ctx(index);
177 expr = pet_expr_alloc(ctx, pet_expr_access);
178 if (!expr)
179 goto error;
181 expr->acc.read = 1;
182 expr->acc.write = 0;
184 expr = pet_expr_access_set_index(expr, index);
186 return expr;
187 error:
188 isl_multi_pw_aff_free(index);
189 return NULL;
192 /* Extend the range of "access" with "n" dimensions, retaining
193 * the tuple identifier on this range.
195 * If "access" represents a member access, then extend the range
196 * of the member.
198 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
200 isl_id *id;
202 id = isl_map_get_tuple_id(access, isl_dim_out);
204 if (!isl_map_range_is_wrapping(access)) {
205 access = isl_map_add_dims(access, isl_dim_out, n);
206 } else {
207 isl_map *domain;
209 domain = isl_map_copy(access);
210 domain = isl_map_range_factor_domain(domain);
211 access = isl_map_range_factor_range(access);
212 access = extend_range(access, n);
213 access = isl_map_range_product(domain, access);
216 access = isl_map_set_tuple_id(access, isl_dim_out, id);
218 return access;
221 /* Does the access expression "expr" have any explicit access relation?
223 isl_bool pet_expr_access_has_any_access_relation(__isl_keep pet_expr *expr)
225 enum pet_expr_access_type type;
227 if (!expr)
228 return isl_bool_error;
230 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type)
231 if (expr->acc.access[type])
232 return isl_bool_true;
234 return isl_bool_false;
237 /* Are all relevant access relations explicitly available in "expr"?
239 static int has_relevant_access_relations(__isl_keep pet_expr *expr)
241 if (!expr)
242 return -1;
244 if (expr->acc.kill && !expr->acc.access[pet_expr_access_fake_killed])
245 return 0;
246 if (expr->acc.read && !expr->acc.access[pet_expr_access_may_read])
247 return 0;
248 if (expr->acc.write &&
249 (!expr->acc.access[pet_expr_access_may_write] ||
250 !expr->acc.access[pet_expr_access_must_write]))
251 return 0;
253 return 1;
256 /* Replace the depth of the access expr "expr" by "depth".
258 * To avoid inconsistencies between the depth and the access relation,
259 * we currently do not allow the depth to change once the access relation
260 * has been set or computed.
262 __isl_give pet_expr *pet_expr_access_set_depth(__isl_take pet_expr *expr,
263 int depth)
265 if (!expr)
266 return NULL;
267 if (expr->acc.depth == depth)
268 return expr;
269 if (pet_expr_access_has_any_access_relation(expr))
270 isl_die(pet_expr_get_ctx(expr), isl_error_unsupported,
271 "depth cannot be changed after access relation "
272 "has been set or computed", return pet_expr_free(expr));
274 expr = pet_expr_cow(expr);
275 if (!expr)
276 return NULL;
277 expr->acc.depth = depth;
279 return expr;
282 /* Construct a pet_expr that kills the elements specified by
283 * the index expression "index" and the access relation "access".
285 __isl_give pet_expr *pet_expr_kill_from_access_and_index(
286 __isl_take isl_map *access, __isl_take isl_multi_pw_aff *index)
288 int depth;
289 pet_expr *expr;
291 if (!access || !index)
292 goto error;
294 expr = pet_expr_from_index(index);
295 expr = pet_expr_access_set_read(expr, 0);
296 expr = pet_expr_access_set_kill(expr, 1);
297 depth = isl_map_dim(access, isl_dim_out);
298 expr = pet_expr_access_set_depth(expr, depth);
299 expr = pet_expr_access_set_access(expr, pet_expr_access_killed,
300 isl_union_map_from_map(access));
301 return pet_expr_new_unary(0, pet_op_kill, expr);
302 error:
303 isl_map_free(access);
304 isl_multi_pw_aff_free(index);
305 return NULL;
308 /* Construct a unary pet_expr that performs "op" on "arg",
309 * where the result is represented using a type of "type_size" bits
310 * (may be zero if unknown or if the type is not an integer).
312 __isl_give pet_expr *pet_expr_new_unary(int type_size, enum pet_op_type op,
313 __isl_take pet_expr *arg)
315 isl_ctx *ctx;
316 pet_expr *expr;
318 if (!arg)
319 return NULL;
320 ctx = pet_expr_get_ctx(arg);
321 expr = pet_expr_alloc(ctx, pet_expr_op);
322 expr = pet_expr_set_n_arg(expr, 1);
323 if (!expr)
324 goto error;
326 expr->op = op;
327 expr->type_size = type_size;
328 expr->args[pet_un_arg] = arg;
330 return expr;
331 error:
332 pet_expr_free(arg);
333 return NULL;
336 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
337 * where the result is represented using a type of "type_size" bits
338 * (may be zero if unknown or if the type is not an integer).
340 __isl_give pet_expr *pet_expr_new_binary(int type_size, enum pet_op_type op,
341 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
343 isl_ctx *ctx;
344 pet_expr *expr;
346 if (!lhs || !rhs)
347 goto error;
348 ctx = pet_expr_get_ctx(lhs);
349 expr = pet_expr_alloc(ctx, pet_expr_op);
350 expr = pet_expr_set_n_arg(expr, 2);
351 if (!expr)
352 goto error;
354 expr->op = op;
355 expr->type_size = type_size;
356 expr->args[pet_bin_lhs] = lhs;
357 expr->args[pet_bin_rhs] = rhs;
359 return expr;
360 error:
361 pet_expr_free(lhs);
362 pet_expr_free(rhs);
363 return NULL;
366 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
368 __isl_give pet_expr *pet_expr_new_ternary(__isl_take pet_expr *cond,
369 __isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
371 isl_ctx *ctx;
372 pet_expr *expr;
374 if (!cond || !lhs || !rhs)
375 goto error;
376 ctx = pet_expr_get_ctx(cond);
377 expr = pet_expr_alloc(ctx, pet_expr_op);
378 expr = pet_expr_set_n_arg(expr, 3);
379 if (!expr)
380 goto error;
382 expr->op = pet_op_cond;
383 expr->args[pet_ter_cond] = cond;
384 expr->args[pet_ter_true] = lhs;
385 expr->args[pet_ter_false] = rhs;
387 return expr;
388 error:
389 pet_expr_free(cond);
390 pet_expr_free(lhs);
391 pet_expr_free(rhs);
392 return NULL;
395 /* Construct a call pet_expr that calls function "name" with "n_arg"
396 * arguments. The caller is responsible for filling in the arguments.
398 __isl_give pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
399 unsigned n_arg)
401 pet_expr *expr;
403 expr = pet_expr_alloc(ctx, pet_expr_call);
404 expr = pet_expr_set_n_arg(expr, n_arg);
405 if (!expr)
406 return NULL;
408 expr->c.name = strdup(name);
409 if (!expr->c.name)
410 return pet_expr_free(expr);
412 return expr;
415 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
417 __isl_give pet_expr *pet_expr_new_cast(const char *type_name,
418 __isl_take pet_expr *arg)
420 isl_ctx *ctx;
421 pet_expr *expr;
423 if (!arg)
424 return NULL;
426 ctx = pet_expr_get_ctx(arg);
427 expr = pet_expr_alloc(ctx, pet_expr_cast);
428 expr = pet_expr_set_n_arg(expr, 1);
429 if (!expr)
430 goto error;
432 expr->type_name = strdup(type_name);
433 if (!expr->type_name)
434 goto error;
436 expr->args[0] = arg;
438 return expr;
439 error:
440 pet_expr_free(arg);
441 pet_expr_free(expr);
442 return NULL;
445 /* Construct a pet_expr that represents the double "d".
447 __isl_give pet_expr *pet_expr_new_double(isl_ctx *ctx,
448 double val, const char *s)
450 pet_expr *expr;
452 expr = pet_expr_alloc(ctx, pet_expr_double);
453 if (!expr)
454 return NULL;
456 expr->d.val = val;
457 expr->d.s = strdup(s);
458 if (!expr->d.s)
459 return pet_expr_free(expr);
461 return expr;
464 /* Construct a pet_expr that represents the integer value "v".
466 __isl_give pet_expr *pet_expr_new_int(__isl_take isl_val *v)
468 isl_ctx *ctx;
469 pet_expr *expr;
471 if (!v)
472 return NULL;
474 ctx = isl_val_get_ctx(v);
475 expr = pet_expr_alloc(ctx, pet_expr_int);
476 if (!expr)
477 goto error;
479 expr->i = v;
481 return expr;
482 error:
483 isl_val_free(v);
484 return NULL;
487 /* Return an independent duplicate of "expr".
489 * In case of an access expression, make sure the depth of the duplicate is set
490 * before the access relation (if any) is set and after the index expression
491 * is set.
493 static __isl_give pet_expr *pet_expr_dup(__isl_keep pet_expr *expr)
495 int i;
496 pet_expr *dup;
497 enum pet_expr_access_type type;
499 if (!expr)
500 return NULL;
502 dup = pet_expr_alloc(expr->ctx, expr->type);
503 dup = pet_expr_set_type_size(dup, expr->type_size);
504 dup = pet_expr_set_n_arg(dup, expr->n_arg);
505 for (i = 0; i < expr->n_arg; ++i)
506 dup = pet_expr_set_arg(dup, i, pet_expr_copy(expr->args[i]));
508 switch (expr->type) {
509 case pet_expr_access:
510 if (expr->acc.ref_id)
511 dup = pet_expr_access_set_ref_id(dup,
512 isl_id_copy(expr->acc.ref_id));
513 dup = pet_expr_access_set_index(dup,
514 isl_multi_pw_aff_copy(expr->acc.index));
515 dup = pet_expr_access_set_depth(dup, expr->acc.depth);
516 for (type = pet_expr_access_begin;
517 type < pet_expr_access_end; ++type) {
518 if (!expr->acc.access[type])
519 continue;
520 dup = pet_expr_access_set_access(dup, type,
521 isl_union_map_copy(expr->acc.access[type]));
523 dup = pet_expr_access_set_read(dup, expr->acc.read);
524 dup = pet_expr_access_set_write(dup, expr->acc.write);
525 dup = pet_expr_access_set_kill(dup, expr->acc.kill);
526 break;
527 case pet_expr_call:
528 dup = pet_expr_call_set_name(dup, expr->c.name);
529 if (expr->c.summary)
530 dup = pet_expr_call_set_summary(dup,
531 pet_function_summary_copy(expr->c.summary));
532 break;
533 case pet_expr_cast:
534 dup = pet_expr_cast_set_type_name(dup, expr->type_name);
535 break;
536 case pet_expr_double:
537 dup = pet_expr_double_set(dup, expr->d.val, expr->d.s);
538 break;
539 case pet_expr_int:
540 dup = pet_expr_int_set_val(dup, isl_val_copy(expr->i));
541 break;
542 case pet_expr_op:
543 dup = pet_expr_op_set_type(dup, expr->op);
544 break;
545 case pet_expr_error:
546 dup = pet_expr_free(dup);
547 break;
550 return dup;
553 /* Return a pet_expr that is equal to "expr" and that has only
554 * a single reference.
556 * If "expr" itself only has one reference, then clear its hash value
557 * since the returned pet_expr will be modified.
559 __isl_give pet_expr *pet_expr_cow(__isl_take pet_expr *expr)
561 if (!expr)
562 return NULL;
564 if (expr->ref == 1) {
565 expr->hash = 0;
566 return expr;
568 expr->ref--;
569 return pet_expr_dup(expr);
572 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr)
574 enum pet_expr_access_type type;
575 int i;
577 if (!expr)
578 return NULL;
579 if (--expr->ref > 0)
580 return NULL;
582 for (i = 0; i < expr->n_arg; ++i)
583 pet_expr_free(expr->args[i]);
584 free(expr->args);
586 switch (expr->type) {
587 case pet_expr_access:
588 isl_id_free(expr->acc.ref_id);
589 for (type = pet_expr_access_begin;
590 type < pet_expr_access_end; ++type)
591 isl_union_map_free(expr->acc.access[type]);
592 isl_multi_pw_aff_free(expr->acc.index);
593 break;
594 case pet_expr_call:
595 free(expr->c.name);
596 pet_function_summary_free(expr->c.summary);
597 break;
598 case pet_expr_cast:
599 free(expr->type_name);
600 break;
601 case pet_expr_double:
602 free(expr->d.s);
603 break;
604 case pet_expr_int:
605 isl_val_free(expr->i);
606 break;
607 case pet_expr_op:
608 case pet_expr_error:
609 break;
612 isl_ctx_deref(expr->ctx);
613 free(expr);
614 return NULL;
617 /* Return an additional reference to "expr".
619 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr)
621 if (!expr)
622 return NULL;
624 expr->ref++;
625 return expr;
628 /* Return the isl_ctx in which "expr" was created.
630 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr)
632 return expr ? expr->ctx : NULL;
635 /* Return the type of "expr".
637 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr)
639 if (!expr)
640 return pet_expr_error;
641 return expr->type;
644 /* Return the number of arguments of "expr".
646 int pet_expr_get_n_arg(__isl_keep pet_expr *expr)
648 if (!expr)
649 return -1;
651 return expr->n_arg;
654 /* Set the number of arguments of "expr" to "n".
656 * If "expr" originally had more arguments, then remove the extra arguments.
657 * If "expr" originally had fewer arguments, then create space for
658 * the extra arguments ans initialize them to NULL.
660 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n)
662 int i;
663 pet_expr **args;
665 if (!expr)
666 return NULL;
667 if (expr->n_arg == n)
668 return expr;
669 expr = pet_expr_cow(expr);
670 if (!expr)
671 return NULL;
673 if (n < expr->n_arg) {
674 for (i = n; i < expr->n_arg; ++i)
675 pet_expr_free(expr->args[i]);
676 expr->n_arg = n;
677 return expr;
680 args = isl_realloc_array(expr->ctx, expr->args, pet_expr *, n);
681 if (!args)
682 return pet_expr_free(expr);
683 expr->args = args;
684 for (i = expr->n_arg; i < n; ++i)
685 expr->args[i] = NULL;
686 expr->n_arg = n;
688 return expr;
691 /* Return the argument of "expr" at position "pos".
693 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos)
695 if (!expr)
696 return NULL;
697 if (pos < 0 || pos >= expr->n_arg)
698 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
699 "position out of bounds", return NULL);
701 return pet_expr_copy(expr->args[pos]);
704 /* Replace "expr" by its argument at position "pos".
706 __isl_give pet_expr *pet_expr_arg(__isl_take pet_expr *expr, int pos)
708 pet_expr *arg;
710 arg = pet_expr_get_arg(expr, pos);
711 pet_expr_free(expr);
713 return arg;
716 /* Replace the argument of "expr" at position "pos" by "arg".
718 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
719 __isl_take pet_expr *arg)
721 if (!expr || !arg)
722 goto error;
723 if (pos < 0 || pos >= expr->n_arg)
724 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
725 "position out of bounds", goto error);
726 if (expr->args[pos] == arg) {
727 pet_expr_free(arg);
728 return expr;
731 expr = pet_expr_cow(expr);
732 if (!expr)
733 goto error;
735 pet_expr_free(expr->args[pos]);
736 expr->args[pos] = arg;
738 return expr;
739 error:
740 pet_expr_free(expr);
741 pet_expr_free(arg);
742 return NULL;
745 /* Does "expr" perform a comparison operation?
747 int pet_expr_is_comparison(__isl_keep pet_expr *expr)
749 if (!expr)
750 return -1;
751 if (expr->type != pet_expr_op)
752 return 0;
753 switch (expr->op) {
754 case pet_op_eq:
755 case pet_op_ne:
756 case pet_op_le:
757 case pet_op_ge:
758 case pet_op_lt:
759 case pet_op_gt:
760 return 1;
761 default:
762 return 0;
766 /* Does "expr" perform a boolean operation?
768 int pet_expr_is_boolean(__isl_keep pet_expr *expr)
770 if (!expr)
771 return -1;
772 if (expr->type != pet_expr_op)
773 return 0;
774 switch (expr->op) {
775 case pet_op_land:
776 case pet_op_lor:
777 case pet_op_lnot:
778 return 1;
779 default:
780 return 0;
784 /* Is "expr" an address-of operation?
786 int pet_expr_is_address_of(__isl_keep pet_expr *expr)
788 if (!expr)
789 return -1;
790 if (expr->type != pet_expr_op)
791 return 0;
792 return expr->op == pet_op_address_of;
795 /* Is "expr" an assume statement?
797 int pet_expr_is_assume(__isl_keep pet_expr *expr)
799 if (!expr)
800 return -1;
801 if (expr->type != pet_expr_op)
802 return 0;
803 return expr->op == pet_op_assume;
806 /* Does "expr" perform a min operation?
808 int pet_expr_is_min(__isl_keep pet_expr *expr)
810 if (!expr)
811 return -1;
812 if (expr->type != pet_expr_call)
813 return 0;
814 if (expr->n_arg != 2)
815 return 0;
816 if (strcmp(expr->c.name, "min") != 0)
817 return 0;
818 return 1;
821 /* Does "expr" perform a max operation?
823 int pet_expr_is_max(__isl_keep pet_expr *expr)
825 if (!expr)
826 return -1;
827 if (expr->type != pet_expr_call)
828 return 0;
829 if (expr->n_arg != 2)
830 return 0;
831 if (strcmp(expr->c.name, "max") != 0)
832 return 0;
833 return 1;
836 /* Does "expr" represent an access to an unnamed space, i.e.,
837 * does it represent an affine expression?
839 isl_bool pet_expr_is_affine(__isl_keep pet_expr *expr)
841 int has_id;
843 if (!expr)
844 return isl_bool_error;
845 if (expr->type != pet_expr_access)
846 return isl_bool_false;
848 has_id = isl_multi_pw_aff_has_tuple_id(expr->acc.index, isl_dim_out);
849 if (has_id < 0)
850 return isl_bool_error;
852 return !has_id;
855 /* Given that "expr" represents an affine expression, i.e., that
856 * it is an access to an unnamed (1D) space, return this affine expression.
858 __isl_give isl_pw_aff *pet_expr_get_affine(__isl_keep pet_expr *expr)
860 isl_bool is_affine;
861 isl_pw_aff *pa;
862 isl_multi_pw_aff *mpa;
864 is_affine = pet_expr_is_affine(expr);
865 if (is_affine < 0)
866 return NULL;
867 if (!is_affine)
868 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
869 "not an affine expression", return NULL);
871 mpa = pet_expr_access_get_index(expr);
872 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
873 isl_multi_pw_aff_free(mpa);
874 return pa;
877 /* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
878 * not part of any struct?
880 int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
882 if (!expr)
883 return -1;
884 if (expr->type != pet_expr_access)
885 return 0;
886 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index))
887 return 0;
889 return expr->acc.depth == 0;
892 /* Are "mpa1" and "mpa2" obviously equal to each other, up to reordering
893 * of parameters.
895 static int multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
896 __isl_keep isl_multi_pw_aff *mpa2)
898 int equal;
900 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
901 if (equal < 0 || equal)
902 return equal;
903 mpa2 = isl_multi_pw_aff_copy(mpa2);
904 mpa2 = isl_multi_pw_aff_align_params(mpa2,
905 isl_multi_pw_aff_get_space(mpa1));
906 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
907 isl_multi_pw_aff_free(mpa2);
909 return equal;
912 /* Construct an access relation from the index expression and
913 * the array depth of the access expression "expr".
915 * If the number of indices is smaller than the depth of the array,
916 * then we assume that all elements of the remaining dimensions
917 * are accessed.
919 static __isl_give isl_union_map *construct_access_relation(
920 __isl_keep pet_expr *expr)
922 isl_map *access;
923 int dim;
924 int read, write;
926 if (!expr)
927 return NULL;
929 access = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
930 if (!access)
931 return NULL;
933 dim = isl_map_dim(access, isl_dim_out);
934 if (dim > expr->acc.depth)
935 isl_die(isl_map_get_ctx(access), isl_error_internal,
936 "number of indices greater than depth",
937 access = isl_map_free(access));
939 if (dim != expr->acc.depth)
940 access = extend_range(access, expr->acc.depth - dim);
942 return isl_union_map_from_map(access);
945 /* Ensure that all relevant access relations are explicitly
946 * available in "expr".
948 * If "expr" does not already have the relevant access relations, then create
949 * them based on the index expression and the array depth.
951 * We do not cow since adding an explicit access relation
952 * does not change the meaning of the expression.
953 * However, the explicit access relations may modify the hash value,
954 * so the cached value is reset.
956 static __isl_give pet_expr *introduce_access_relations(
957 __isl_take pet_expr *expr)
959 enum pet_expr_access_type type;
960 isl_union_map *access;
961 int dim;
962 int kill, read, write;
964 if (!expr)
965 return NULL;
966 if (has_relevant_access_relations(expr))
967 return expr;
969 access = construct_access_relation(expr);
970 if (!access)
971 return pet_expr_free(expr);
973 expr->hash = 0;
974 kill = expr->acc.kill;
975 read = expr->acc.read;
976 write = expr->acc.write;
977 if (kill && !expr->acc.access[pet_expr_access_fake_killed])
978 expr->acc.access[pet_expr_access_fake_killed] =
979 isl_union_map_copy(access);
980 if (read && !expr->acc.access[pet_expr_access_may_read])
981 expr->acc.access[pet_expr_access_may_read] =
982 isl_union_map_copy(access);
983 if (write && !expr->acc.access[pet_expr_access_may_write])
984 expr->acc.access[pet_expr_access_may_write] =
985 isl_union_map_copy(access);
986 if (write && !expr->acc.access[pet_expr_access_must_write])
987 expr->acc.access[pet_expr_access_must_write] =
988 isl_union_map_copy(access);
990 isl_union_map_free(access);
992 if (!has_relevant_access_relations(expr))
993 return pet_expr_free(expr);
995 return expr;
998 /* Return a hash value that digests "expr".
999 * If a hash value was computed already, then return that value.
1000 * Otherwise, compute the hash value and store a copy in expr->hash.
1002 uint32_t pet_expr_get_hash(__isl_keep pet_expr *expr)
1004 int i;
1005 enum pet_expr_access_type type;
1006 uint32_t hash, hash_f;
1008 if (!expr)
1009 return 0;
1010 if (expr->hash)
1011 return expr->hash;
1013 hash = isl_hash_init();
1014 isl_hash_byte(hash, expr->type & 0xFF);
1015 isl_hash_byte(hash, expr->n_arg & 0xFF);
1016 for (i = 0; i < expr->n_arg; ++i) {
1017 uint32_t hash_i;
1018 hash_i = pet_expr_get_hash(expr->args[i]);
1019 isl_hash_hash(hash, hash_i);
1021 switch (expr->type) {
1022 case pet_expr_error:
1023 return 0;
1024 case pet_expr_double:
1025 hash = isl_hash_string(hash, expr->d.s);
1026 break;
1027 case pet_expr_int:
1028 hash_f = isl_val_get_hash(expr->i);
1029 isl_hash_hash(hash, hash_f);
1030 break;
1031 case pet_expr_access:
1032 isl_hash_byte(hash, expr->acc.read & 0xFF);
1033 isl_hash_byte(hash, expr->acc.write & 0xFF);
1034 isl_hash_byte(hash, expr->acc.kill & 0xFF);
1035 hash_f = isl_id_get_hash(expr->acc.ref_id);
1036 isl_hash_hash(hash, hash_f);
1037 hash_f = isl_multi_pw_aff_get_hash(expr->acc.index);
1038 isl_hash_hash(hash, hash_f);
1039 isl_hash_byte(hash, expr->acc.depth & 0xFF);
1040 for (type = pet_expr_access_begin;
1041 type < pet_expr_access_end; ++type) {
1042 hash_f = isl_union_map_get_hash(expr->acc.access[type]);
1043 isl_hash_hash(hash, hash_f);
1045 break;
1046 case pet_expr_op:
1047 isl_hash_byte(hash, expr->op & 0xFF);
1048 break;
1049 case pet_expr_call:
1050 hash = isl_hash_string(hash, expr->c.name);
1051 break;
1052 case pet_expr_cast:
1053 hash = isl_hash_string(hash, expr->type_name);
1054 break;
1056 expr->hash = hash;
1057 return hash;
1060 /* Return 1 if the two pet_exprs are equivalent.
1062 int pet_expr_is_equal(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2)
1064 int i;
1065 enum pet_expr_access_type type;
1067 if (!expr1 || !expr2)
1068 return 0;
1070 if (expr1->type != expr2->type)
1071 return 0;
1072 if (expr1->n_arg != expr2->n_arg)
1073 return 0;
1074 for (i = 0; i < expr1->n_arg; ++i)
1075 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
1076 return 0;
1077 switch (expr1->type) {
1078 case pet_expr_error:
1079 return -1;
1080 case pet_expr_double:
1081 if (strcmp(expr1->d.s, expr2->d.s))
1082 return 0;
1083 if (expr1->d.val != expr2->d.val)
1084 return 0;
1085 break;
1086 case pet_expr_int:
1087 if (!isl_val_eq(expr1->i, expr2->i))
1088 return 0;
1089 break;
1090 case pet_expr_access:
1091 if (expr1->acc.read != expr2->acc.read)
1092 return 0;
1093 if (expr1->acc.write != expr2->acc.write)
1094 return 0;
1095 if (expr1->acc.kill != expr2->acc.kill)
1096 return 0;
1097 if (expr1->acc.ref_id != expr2->acc.ref_id)
1098 return 0;
1099 if (!expr1->acc.index || !expr2->acc.index)
1100 return 0;
1101 if (!multi_pw_aff_is_equal(expr1->acc.index, expr2->acc.index))
1102 return 0;
1103 if (expr1->acc.depth != expr2->acc.depth)
1104 return 0;
1105 if (has_relevant_access_relations(expr1) !=
1106 has_relevant_access_relations(expr2)) {
1107 int equal;
1108 expr1 = pet_expr_copy(expr1);
1109 expr2 = pet_expr_copy(expr2);
1110 expr1 = introduce_access_relations(expr1);
1111 expr2 = introduce_access_relations(expr2);
1112 equal = pet_expr_is_equal(expr1, expr2);
1113 pet_expr_free(expr1);
1114 pet_expr_free(expr2);
1115 return equal;
1117 for (type = pet_expr_access_begin;
1118 type < pet_expr_access_end; ++type) {
1119 if (!expr1->acc.access[type] !=
1120 !expr2->acc.access[type])
1121 return 0;
1122 if (!expr1->acc.access[type])
1123 continue;
1124 if (!isl_union_map_is_equal(expr1->acc.access[type],
1125 expr2->acc.access[type]))
1126 return 0;
1128 break;
1129 case pet_expr_op:
1130 if (expr1->op != expr2->op)
1131 return 0;
1132 break;
1133 case pet_expr_call:
1134 if (strcmp(expr1->c.name, expr2->c.name))
1135 return 0;
1136 break;
1137 case pet_expr_cast:
1138 if (strcmp(expr1->type_name, expr2->type_name))
1139 return 0;
1140 break;
1143 return 1;
1146 /* Do "expr1" and "expr2" represent two accesses to the same array
1147 * that are also of the same type? That is, can these two accesses
1148 * be replaced by a single access?
1150 isl_bool pet_expr_is_same_access(__isl_keep pet_expr *expr1,
1151 __isl_keep pet_expr *expr2)
1153 isl_space *space1, *space2;
1154 isl_bool same;
1156 if (!expr1 || !expr2)
1157 return isl_bool_error;
1158 if (pet_expr_get_type(expr1) != pet_expr_access)
1159 return isl_bool_false;
1160 if (pet_expr_get_type(expr2) != pet_expr_access)
1161 return isl_bool_false;
1162 if (expr1->acc.read != expr2->acc.read)
1163 return isl_bool_false;
1164 if (expr1->acc.write != expr2->acc.write)
1165 return isl_bool_false;
1166 if (expr1->acc.kill != expr2->acc.kill)
1167 return isl_bool_false;
1168 if (expr1->acc.depth != expr2->acc.depth)
1169 return isl_bool_false;
1171 space1 = isl_multi_pw_aff_get_space(expr1->acc.index);
1172 space2 = isl_multi_pw_aff_get_space(expr2->acc.index);
1173 same = isl_space_tuple_is_equal(space1, isl_dim_out,
1174 space2, isl_dim_out);
1175 if (same >= 0 && same)
1176 same = isl_space_tuple_is_equal(space1, isl_dim_in,
1177 space2, isl_dim_in);
1178 isl_space_free(space1);
1179 isl_space_free(space2);
1181 return same;
1184 /* Does the access expression "expr" read the accessed elements?
1186 isl_bool pet_expr_access_is_read(__isl_keep pet_expr *expr)
1188 if (!expr)
1189 return isl_bool_error;
1190 if (expr->type != pet_expr_access)
1191 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1192 "not an access expression", return isl_bool_error);
1194 return expr->acc.read;
1197 /* Does the access expression "expr" write to the accessed elements?
1199 isl_bool pet_expr_access_is_write(__isl_keep pet_expr *expr)
1201 if (!expr)
1202 return isl_bool_error;
1203 if (expr->type != pet_expr_access)
1204 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1205 "not an access expression", return isl_bool_error);
1207 return expr->acc.write;
1210 /* Does the access expression "expr" kill the accessed elements?
1212 isl_bool pet_expr_access_is_kill(__isl_keep pet_expr *expr)
1214 if (!expr)
1215 return isl_bool_error;
1216 if (expr->type != pet_expr_access)
1217 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1218 "not an access expression", return isl_bool_error);
1220 return expr->acc.kill;
1223 /* Return the identifier of the array accessed by "expr".
1225 * If "expr" represents a member access, then return the identifier
1226 * of the outer structure array.
1228 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr)
1230 if (!expr)
1231 return NULL;
1232 if (expr->type != pet_expr_access)
1233 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1234 "not an access expression", return NULL);
1236 if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index)) {
1237 isl_space *space;
1238 isl_id *id;
1240 space = isl_multi_pw_aff_get_space(expr->acc.index);
1241 space = isl_space_range(space);
1242 while (space && isl_space_is_wrapping(space))
1243 space = isl_space_domain(isl_space_unwrap(space));
1244 id = isl_space_get_tuple_id(space, isl_dim_set);
1245 isl_space_free(space);
1247 return id;
1250 return isl_multi_pw_aff_get_tuple_id(expr->acc.index, isl_dim_out);
1253 /* Return the parameter space of "expr".
1255 __isl_give isl_space *pet_expr_access_get_parameter_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_params(space);
1269 return space;
1272 /* Return the domain space of "expr", including the arguments (if any).
1274 __isl_give isl_space *pet_expr_access_get_augmented_domain_space(
1275 __isl_keep pet_expr *expr)
1277 isl_space *space;
1279 if (!expr)
1280 return NULL;
1281 if (expr->type != pet_expr_access)
1282 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1283 "not an access expression", return NULL);
1285 space = isl_multi_pw_aff_get_space(expr->acc.index);
1286 space = isl_space_domain(space);
1288 return space;
1291 /* Return the domain space of "expr", without the arguments (if any).
1293 __isl_give isl_space *pet_expr_access_get_domain_space(
1294 __isl_keep pet_expr *expr)
1296 isl_space *space;
1298 space = pet_expr_access_get_augmented_domain_space(expr);
1299 if (isl_space_is_wrapping(space))
1300 space = isl_space_domain(isl_space_unwrap(space));
1302 return space;
1305 /* Return the space of the data accessed by "expr".
1307 __isl_give isl_space *pet_expr_access_get_data_space(__isl_keep pet_expr *expr)
1309 isl_space *space;
1311 if (!expr)
1312 return NULL;
1313 if (expr->type != pet_expr_access)
1314 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1315 "not an access expression", return NULL);
1317 space = isl_multi_pw_aff_get_space(expr->acc.index);
1318 space = isl_space_range(space);
1320 return space;
1323 /* Modify all subexpressions of "expr" by calling "fn" on them.
1324 * The subexpressions are traversed in depth first preorder.
1326 __isl_give pet_expr *pet_expr_map_top_down(__isl_take pet_expr *expr,
1327 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1328 void *user)
1330 int i, n;
1332 if (!expr)
1333 return NULL;
1335 expr = fn(expr, user);
1337 n = pet_expr_get_n_arg(expr);
1338 for (i = 0; i < n; ++i) {
1339 pet_expr *arg = pet_expr_get_arg(expr, i);
1340 arg = pet_expr_map_top_down(arg, fn, user);
1341 expr = pet_expr_set_arg(expr, i, arg);
1344 return expr;
1347 /* Modify all expressions of type "type" in "expr" by calling "fn" on them.
1349 static __isl_give pet_expr *pet_expr_map_expr_of_type(__isl_take pet_expr *expr,
1350 enum pet_expr_type type,
1351 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1352 void *user)
1354 int i, n;
1356 n = pet_expr_get_n_arg(expr);
1357 for (i = 0; i < n; ++i) {
1358 pet_expr *arg = pet_expr_get_arg(expr, i);
1359 arg = pet_expr_map_expr_of_type(arg, type, fn, user);
1360 expr = pet_expr_set_arg(expr, i, arg);
1363 if (!expr)
1364 return NULL;
1366 if (expr->type == type)
1367 expr = fn(expr, user);
1369 return expr;
1372 /* Modify all expressions of type pet_expr_access in "expr"
1373 * by calling "fn" on them.
1375 __isl_give pet_expr *pet_expr_map_access(__isl_take pet_expr *expr,
1376 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1377 void *user)
1379 return pet_expr_map_expr_of_type(expr, pet_expr_access, fn, user);
1382 /* Modify all expressions of type pet_expr_call in "expr"
1383 * by calling "fn" on them.
1385 __isl_give pet_expr *pet_expr_map_call(__isl_take pet_expr *expr,
1386 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1387 void *user)
1389 return pet_expr_map_expr_of_type(expr, pet_expr_call, fn, user);
1392 /* Modify all expressions of type pet_expr_op in "expr"
1393 * by calling "fn" on them.
1395 __isl_give pet_expr *pet_expr_map_op(__isl_take pet_expr *expr,
1396 __isl_give pet_expr *(*fn)(__isl_take pet_expr *expr, void *user),
1397 void *user)
1399 return pet_expr_map_expr_of_type(expr, pet_expr_op, fn, user);
1402 /* Call "fn" on each of the subexpressions of "expr" of type "type".
1404 * Return -1 on error (where fn returning a negative value is treated as
1405 * an error).
1406 * Otherwise return 0.
1408 int pet_expr_foreach_expr_of_type(__isl_keep pet_expr *expr,
1409 enum pet_expr_type type,
1410 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1412 int i;
1414 if (!expr)
1415 return -1;
1417 for (i = 0; i < expr->n_arg; ++i)
1418 if (pet_expr_foreach_expr_of_type(expr->args[i],
1419 type, fn, user) < 0)
1420 return -1;
1422 if (expr->type == type)
1423 return fn(expr, user);
1425 return 0;
1428 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
1430 * Return -1 on error (where fn returning a negative value is treated as
1431 * an error).
1432 * Otherwise return 0.
1434 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
1435 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1437 return pet_expr_foreach_expr_of_type(expr, pet_expr_access, fn, user);
1440 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call.
1442 * Return -1 on error (where fn returning a negative value is treated as
1443 * an error).
1444 * Otherwise return 0.
1446 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
1447 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user)
1449 return pet_expr_foreach_expr_of_type(expr, pet_expr_call, fn, user);
1452 /* Internal data structure for pet_expr_writes.
1453 * "id" is the identifier that we are looking for.
1454 * "found" is set if we have found the identifier being written to.
1456 struct pet_expr_writes_data {
1457 isl_id *id;
1458 int found;
1461 /* Given an access expression, check if it writes to data->id.
1462 * If so, set data->found and abort the search.
1464 static int writes(__isl_keep pet_expr *expr, void *user)
1466 struct pet_expr_writes_data *data = user;
1467 isl_id *write_id;
1469 if (!expr->acc.write)
1470 return 0;
1471 if (pet_expr_is_affine(expr))
1472 return 0;
1474 write_id = pet_expr_access_get_id(expr);
1475 isl_id_free(write_id);
1477 if (!write_id)
1478 return -1;
1480 if (write_id != data->id)
1481 return 0;
1483 data->found = 1;
1484 return -1;
1487 /* Does expression "expr" write to "id"?
1489 int pet_expr_writes(__isl_keep pet_expr *expr, __isl_keep isl_id *id)
1491 struct pet_expr_writes_data data;
1493 data.id = id;
1494 data.found = 0;
1495 if (pet_expr_foreach_access_expr(expr, &writes, &data) < 0 &&
1496 !data.found)
1497 return -1;
1499 return data.found;
1502 /* Move the "n" dimensions of "src_type" starting at "src_pos" of
1503 * index expression and access relations of "expr" (if any)
1504 * to dimensions of "dst_type" at "dst_pos".
1506 __isl_give pet_expr *pet_expr_access_move_dims(__isl_take pet_expr *expr,
1507 enum isl_dim_type dst_type, unsigned dst_pos,
1508 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1510 enum pet_expr_access_type type;
1512 expr = pet_expr_cow(expr);
1513 if (!expr)
1514 return NULL;
1515 if (expr->type != pet_expr_access)
1516 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1517 "not an access pet_expr", return pet_expr_free(expr));
1519 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1520 if (!expr->acc.access[type])
1521 continue;
1522 expr->acc.access[type] =
1523 pet_union_map_move_dims(expr->acc.access[type],
1524 dst_type, dst_pos, src_type, src_pos, n);
1525 if (!expr->acc.access[type])
1526 break;
1528 expr->acc.index = isl_multi_pw_aff_move_dims(expr->acc.index,
1529 dst_type, dst_pos, src_type, src_pos, n);
1530 if (!expr->acc.index || type < pet_expr_access_end)
1531 return pet_expr_free(expr);
1533 return expr;
1536 /* Replace the index expression and access relations (if any) of "expr"
1537 * by their preimages under the function represented by "ma".
1539 __isl_give pet_expr *pet_expr_access_pullback_multi_aff(
1540 __isl_take pet_expr *expr, __isl_take isl_multi_aff *ma)
1542 enum pet_expr_access_type type;
1544 expr = pet_expr_cow(expr);
1545 if (!expr || !ma)
1546 goto error;
1547 if (expr->type != pet_expr_access)
1548 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1549 "not an access pet_expr", goto error);
1551 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1552 if (!expr->acc.access[type])
1553 continue;
1554 expr->acc.access[type] =
1555 isl_union_map_preimage_domain_multi_aff(
1556 expr->acc.access[type], isl_multi_aff_copy(ma));
1557 if (!expr->acc.access[type])
1558 break;
1560 expr->acc.index = isl_multi_pw_aff_pullback_multi_aff(expr->acc.index,
1561 ma);
1562 if (!expr->acc.index || type < pet_expr_access_end)
1563 return pet_expr_free(expr);
1565 return expr;
1566 error:
1567 isl_multi_aff_free(ma);
1568 pet_expr_free(expr);
1569 return NULL;
1572 /* Replace the index expression and access relations (if any) of "expr"
1573 * by their preimages under the function represented by "mpa".
1575 __isl_give pet_expr *pet_expr_access_pullback_multi_pw_aff(
1576 __isl_take pet_expr *expr, __isl_take isl_multi_pw_aff *mpa)
1578 enum pet_expr_access_type type;
1580 expr = pet_expr_cow(expr);
1581 if (!expr || !mpa)
1582 goto error;
1583 if (expr->type != pet_expr_access)
1584 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1585 "not an access pet_expr", goto error);
1587 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1588 if (!expr->acc.access[type])
1589 continue;
1590 expr->acc.access[type] =
1591 isl_union_map_preimage_domain_multi_pw_aff(
1592 expr->acc.access[type], isl_multi_pw_aff_copy(mpa));
1593 if (!expr->acc.access[type])
1594 break;
1596 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1597 expr->acc.index, mpa);
1598 if (!expr->acc.index || type < pet_expr_access_end)
1599 return pet_expr_free(expr);
1601 return expr;
1602 error:
1603 isl_multi_pw_aff_free(mpa);
1604 pet_expr_free(expr);
1605 return NULL;
1608 /* Return the index expression of access expression "expr".
1610 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
1611 __isl_keep pet_expr *expr)
1613 if (!expr)
1614 return NULL;
1615 if (expr->type != pet_expr_access)
1616 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1617 "not an access expression", return NULL);
1619 return isl_multi_pw_aff_copy(expr->acc.index);
1622 /* Align the parameters of expr->acc.index and expr->acc.access[*] (if set).
1624 __isl_give pet_expr *pet_expr_access_align_params(__isl_take pet_expr *expr)
1626 isl_space *space;
1627 enum pet_expr_access_type type;
1629 expr = pet_expr_cow(expr);
1630 if (!expr)
1631 return NULL;
1632 if (expr->type != pet_expr_access)
1633 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1634 "not an access expression", return pet_expr_free(expr));
1636 if (!pet_expr_access_has_any_access_relation(expr))
1637 return expr;
1639 space = isl_multi_pw_aff_get_space(expr->acc.index);
1640 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1641 if (!expr->acc.access[type])
1642 continue;
1643 space = isl_space_align_params(space,
1644 isl_union_map_get_space(expr->acc.access[type]));
1646 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1647 isl_space_copy(space));
1648 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1649 if (!expr->acc.access[type])
1650 continue;
1651 expr->acc.access[type] =
1652 isl_union_map_align_params(expr->acc.access[type],
1653 isl_space_copy(space));
1654 if (!expr->acc.access[type])
1655 break;
1657 isl_space_free(space);
1658 if (!expr->acc.index || type < pet_expr_access_end)
1659 return pet_expr_free(expr);
1661 return expr;
1664 /* Are "expr1" and "expr2" both array accesses such that
1665 * the access relation of "expr1" is a subset of that of "expr2"?
1666 * Only take into account the first "n_arg" arguments.
1668 * This function is tailored for use by mark_self_dependences in nest.c.
1669 * In particular, the input expressions may have more than "n_arg"
1670 * elements in their arguments arrays, while only the first "n_arg"
1671 * elements are referenced from the access relations.
1673 int pet_expr_is_sub_access(__isl_keep pet_expr *expr1,
1674 __isl_keep pet_expr *expr2, int n_arg)
1676 isl_id *id1, *id2;
1677 int i, n1, n2;
1678 int is_subset;
1680 if (!expr1 || !expr2)
1681 return 0;
1682 if (pet_expr_get_type(expr1) != pet_expr_access)
1683 return 0;
1684 if (pet_expr_get_type(expr2) != pet_expr_access)
1685 return 0;
1686 if (pet_expr_is_affine(expr1))
1687 return 0;
1688 if (pet_expr_is_affine(expr2))
1689 return 0;
1690 n1 = pet_expr_get_n_arg(expr1);
1691 if (n1 > n_arg)
1692 n1 = n_arg;
1693 n2 = pet_expr_get_n_arg(expr2);
1694 if (n2 > n_arg)
1695 n2 = n_arg;
1696 if (n1 != n2)
1697 return 0;
1698 for (i = 0; i < n1; ++i) {
1699 int equal;
1700 equal = pet_expr_is_equal(expr1->args[i], expr2->args[i]);
1701 if (equal < 0 || !equal)
1702 return equal;
1704 id1 = pet_expr_access_get_id(expr1);
1705 id2 = pet_expr_access_get_id(expr2);
1706 isl_id_free(id1);
1707 isl_id_free(id2);
1708 if (!id1 || !id2)
1709 return 0;
1710 if (id1 != id2)
1711 return 0;
1713 expr1 = pet_expr_copy(expr1);
1714 expr2 = pet_expr_copy(expr2);
1715 expr1 = introduce_access_relations(expr1);
1716 expr2 = introduce_access_relations(expr2);
1717 if (!expr1 || !expr2)
1718 goto error;
1720 is_subset = isl_union_map_is_subset(
1721 expr1->acc.access[pet_expr_access_may_read],
1722 expr2->acc.access[pet_expr_access_may_read]);
1724 pet_expr_free(expr1);
1725 pet_expr_free(expr2);
1727 return is_subset;
1728 error:
1729 pet_expr_free(expr1);
1730 pet_expr_free(expr2);
1731 return -1;
1734 /* Given a set in the iteration space "domain", extend it to live in the space
1735 * of the domain of access relations.
1737 * That, is the number of arguments "n" is 0, then simply return domain.
1738 * Otherwise, return [domain -> [a_1,...,a_n]].
1740 static __isl_give isl_set *add_arguments(__isl_take isl_set *domain, int n)
1742 isl_map *map;
1744 if (n == 0)
1745 return domain;
1747 map = isl_map_from_domain(domain);
1748 map = isl_map_add_dims(map, isl_dim_out, n);
1749 return isl_map_wrap(map);
1752 /* Add extra conditions to the domains of all access relations in "expr",
1753 * introducing access relations if they are not already present.
1755 * The conditions are not added to the index expression. Instead, they
1756 * are used to try and simplify the index expression.
1758 __isl_give pet_expr *pet_expr_restrict(__isl_take pet_expr *expr,
1759 __isl_take isl_set *cond)
1761 int i;
1762 isl_union_set *uset;
1763 enum pet_expr_access_type type;
1765 expr = pet_expr_cow(expr);
1766 if (!expr)
1767 goto error;
1769 for (i = 0; i < expr->n_arg; ++i) {
1770 expr->args[i] = pet_expr_restrict(expr->args[i],
1771 isl_set_copy(cond));
1772 if (!expr->args[i])
1773 goto error;
1776 if (expr->type != pet_expr_access) {
1777 isl_set_free(cond);
1778 return expr;
1781 expr = introduce_access_relations(expr);
1782 if (!expr)
1783 goto error;
1785 cond = add_arguments(cond, expr->n_arg);
1786 uset = isl_union_set_from_set(isl_set_copy(cond));
1787 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1788 if (!expr->acc.access[type])
1789 continue;
1790 expr->acc.access[type] =
1791 isl_union_map_intersect_domain(expr->acc.access[type],
1792 isl_union_set_copy(uset));
1793 if (!expr->acc.access[type])
1794 break;
1796 isl_union_set_free(uset);
1797 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, cond);
1798 if (type < pet_expr_access_end || !expr->acc.index)
1799 return pet_expr_free(expr);
1801 return expr;
1802 error:
1803 isl_set_free(cond);
1804 return pet_expr_free(expr);
1807 /* Modify the access relations (if any) and index expression
1808 * of the given access expression
1809 * based on the given iteration space transformation.
1810 * In particular, precompose the access relation and index expression
1811 * with the update function.
1813 * If the access has any arguments then the domain of the access relation
1814 * is a wrapped mapping from the iteration space to the space of
1815 * argument values. We only need to change the domain of this wrapped
1816 * mapping, so we extend the input transformation with an identity mapping
1817 * on the space of argument values.
1819 __isl_give pet_expr *pet_expr_access_update_domain(__isl_take pet_expr *expr,
1820 __isl_keep isl_multi_pw_aff *update)
1822 enum pet_expr_access_type type;
1824 expr = pet_expr_cow(expr);
1825 if (!expr)
1826 return NULL;
1827 if (expr->type != pet_expr_access)
1828 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1829 "not an access expression", return pet_expr_free(expr));
1831 update = isl_multi_pw_aff_copy(update);
1833 if (expr->n_arg > 0) {
1834 isl_space *space;
1835 isl_multi_pw_aff *id;
1837 space = isl_multi_pw_aff_get_space(expr->acc.index);
1838 space = isl_space_domain(space);
1839 space = isl_space_unwrap(space);
1840 space = isl_space_range(space);
1841 space = isl_space_map_from_set(space);
1842 id = isl_multi_pw_aff_identity(space);
1843 update = isl_multi_pw_aff_product(update, id);
1846 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1847 if (!expr->acc.access[type])
1848 continue;
1849 expr->acc.access[type] =
1850 isl_union_map_preimage_domain_multi_pw_aff(
1851 expr->acc.access[type],
1852 isl_multi_pw_aff_copy(update));
1853 if (!expr->acc.access[type])
1854 break;
1856 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
1857 expr->acc.index, update);
1858 if (type < pet_expr_access_end || !expr->acc.index)
1859 return pet_expr_free(expr);
1861 return expr;
1864 static __isl_give pet_expr *update_domain(__isl_take pet_expr *expr, void *user)
1866 isl_multi_pw_aff *update = user;
1868 return pet_expr_access_update_domain(expr, update);
1871 /* Modify all access relations in "expr" by precomposing them with
1872 * the given iteration space transformation.
1874 __isl_give pet_expr *pet_expr_update_domain(__isl_take pet_expr *expr,
1875 __isl_take isl_multi_pw_aff *update)
1877 expr = pet_expr_map_access(expr, &update_domain, update);
1878 isl_multi_pw_aff_free(update);
1879 return expr;
1882 /* Given an expression with accesses that have a 0D anonymous domain,
1883 * replace those domains by "space".
1885 __isl_give pet_expr *pet_expr_insert_domain(__isl_take pet_expr *expr,
1886 __isl_take isl_space *space)
1888 isl_multi_pw_aff *mpa;
1890 space = isl_space_from_domain(space);
1891 mpa = isl_multi_pw_aff_zero(space);
1892 return pet_expr_update_domain(expr, mpa);
1895 /* Add all parameters in "space" to the access relations (if any)
1896 * and index expression of "expr".
1898 static __isl_give pet_expr *align_params(__isl_take pet_expr *expr, void *user)
1900 isl_space *space = user;
1901 enum pet_expr_access_type type;
1903 expr = pet_expr_cow(expr);
1904 if (!expr)
1905 return NULL;
1906 if (expr->type != pet_expr_access)
1907 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
1908 "not an access expression", return pet_expr_free(expr));
1910 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
1911 if (!expr->acc.access[type])
1912 continue;
1913 expr->acc.access[type] =
1914 isl_union_map_align_params(expr->acc.access[type],
1915 isl_space_copy(space));
1916 if (!expr->acc.access[type])
1917 break;
1919 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
1920 isl_space_copy(space));
1921 if (type < pet_expr_access_end || !expr->acc.index)
1922 return pet_expr_free(expr);
1924 return expr;
1927 /* Add all parameters in "space" to all access relations and index expressions
1928 * in "expr".
1930 __isl_give pet_expr *pet_expr_align_params(__isl_take pet_expr *expr,
1931 __isl_take isl_space *space)
1933 expr = pet_expr_map_access(expr, &align_params, space);
1934 isl_space_free(space);
1935 return expr;
1938 /* Insert an argument expression corresponding to "test" in front
1939 * of the list of arguments described by *n_arg and *args.
1941 static __isl_give pet_expr *insert_access_arg(__isl_take pet_expr *expr,
1942 __isl_keep isl_multi_pw_aff *test)
1944 int i;
1945 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1947 if (!test)
1948 return pet_expr_free(expr);
1949 expr = pet_expr_cow(expr);
1950 if (!expr)
1951 return NULL;
1953 if (!expr->args) {
1954 expr->args = isl_calloc_array(ctx, pet_expr *, 1);
1955 if (!expr->args)
1956 return pet_expr_free(expr);
1957 } else {
1958 pet_expr **ext;
1959 ext = isl_calloc_array(ctx, pet_expr *, 1 + expr->n_arg);
1960 if (!ext)
1961 return pet_expr_free(expr);
1962 for (i = 0; i < expr->n_arg; ++i)
1963 ext[1 + i] = expr->args[i];
1964 free(expr->args);
1965 expr->args = ext;
1967 expr->n_arg++;
1968 expr->args[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1969 if (!expr->args[0])
1970 return pet_expr_free(expr);
1972 return expr;
1975 /* Make the expression "expr" depend on the value of "test"
1976 * being equal to "satisfied".
1978 * If "test" is an affine expression, we simply add the conditions
1979 * on the expression having the value "satisfied" to all access relations
1980 * (introducing access relations if they are missing) and index expressions.
1982 * Otherwise, we add a filter to "expr" (which is then assumed to be
1983 * an access expression) corresponding to "test" being equal to "satisfied".
1985 __isl_give pet_expr *pet_expr_filter(__isl_take pet_expr *expr,
1986 __isl_take isl_multi_pw_aff *test, int satisfied)
1988 isl_id *id;
1989 isl_ctx *ctx;
1990 isl_space *space;
1991 isl_pw_multi_aff *pma;
1992 enum pet_expr_access_type type;
1994 expr = pet_expr_cow(expr);
1995 if (!expr || !test)
1996 goto error;
1998 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
1999 isl_pw_aff *pa;
2000 isl_set *cond;
2002 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
2003 isl_multi_pw_aff_free(test);
2004 if (satisfied)
2005 cond = isl_pw_aff_non_zero_set(pa);
2006 else
2007 cond = isl_pw_aff_zero_set(pa);
2008 return pet_expr_restrict(expr, cond);
2011 ctx = isl_multi_pw_aff_get_ctx(test);
2012 if (expr->type != pet_expr_access)
2013 isl_die(ctx, isl_error_invalid,
2014 "can only filter access expressions", goto error);
2016 expr = introduce_access_relations(expr);
2017 if (!expr)
2018 goto error;
2020 space = isl_space_domain(isl_multi_pw_aff_get_space(expr->acc.index));
2021 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2022 pma = pet_filter_insert_pma(space, id, satisfied);
2024 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
2025 if (!expr->acc.access[type])
2026 continue;
2027 expr->acc.access[type] =
2028 isl_union_map_preimage_domain_pw_multi_aff(
2029 expr->acc.access[type],
2030 isl_pw_multi_aff_copy(pma));
2031 if (!expr->acc.access[type])
2032 break;
2034 pma = isl_pw_multi_aff_gist(pma,
2035 isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma)));
2036 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
2037 expr->acc.index, pma);
2038 if (type < pet_expr_access_end || !expr->acc.index)
2039 goto error;
2041 expr = insert_access_arg(expr, test);
2043 isl_multi_pw_aff_free(test);
2044 return expr;
2045 error:
2046 isl_multi_pw_aff_free(test);
2047 return pet_expr_free(expr);
2050 /* Add a reference identifier to access expression "expr".
2051 * "user" points to an integer that contains the sequence number
2052 * of the next reference.
2054 static __isl_give pet_expr *access_add_ref_id(__isl_take pet_expr *expr,
2055 void *user)
2057 isl_ctx *ctx;
2058 char name[50];
2059 int *n_ref = user;
2061 expr = pet_expr_cow(expr);
2062 if (!expr)
2063 return expr;
2064 if (expr->type != pet_expr_access)
2065 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2066 "not an access expression", return pet_expr_free(expr));
2068 ctx = pet_expr_get_ctx(expr);
2069 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
2070 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
2071 if (!expr->acc.ref_id)
2072 return pet_expr_free(expr);
2074 return expr;
2077 __isl_give pet_expr *pet_expr_add_ref_ids(__isl_take pet_expr *expr, int *n_ref)
2079 return pet_expr_map_access(expr, &access_add_ref_id, n_ref);
2082 /* Reset the user pointer on all parameter and tuple ids in
2083 * the access relations (if any) and the index expression
2084 * of the access expression "expr".
2086 static __isl_give pet_expr *access_anonymize(__isl_take pet_expr *expr,
2087 void *user)
2089 enum pet_expr_access_type type;
2091 expr = pet_expr_cow(expr);
2092 if (!expr)
2093 return expr;
2094 if (expr->type != pet_expr_access)
2095 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2096 "not an access expression", return pet_expr_free(expr));
2098 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
2099 if (!expr->acc.access[type])
2100 continue;
2101 expr->acc.access[type] =
2102 isl_union_map_reset_user(expr->acc.access[type]);
2103 if (!expr->acc.access[type])
2104 break;
2106 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
2107 if (type < pet_expr_access_end || !expr->acc.index)
2108 return pet_expr_free(expr);
2110 return expr;
2113 __isl_give pet_expr *pet_expr_anonymize(__isl_take pet_expr *expr)
2115 return pet_expr_map_access(expr, &access_anonymize, NULL);
2118 /* Data used in access_gist() callback.
2120 struct pet_access_gist_data {
2121 isl_set *domain;
2122 isl_union_map *value_bounds;
2125 /* Given an expression "expr" of type pet_expr_access, compute
2126 * the gist of the associated access relations (if any) and index expression
2127 * with respect to data->domain and the bounds on the values of the arguments
2128 * of the expression.
2130 * The arguments of "expr" have been gisted right before "expr" itself
2131 * is gisted. The gisted arguments may have become equal where before
2132 * they may not have been (obviously) equal. We therefore take
2133 * the opportunity to remove duplicate arguments here.
2135 static __isl_give pet_expr *access_gist(__isl_take pet_expr *expr, void *user)
2137 struct pet_access_gist_data *data = user;
2138 isl_set *domain;
2139 isl_union_set *uset;
2140 enum pet_expr_access_type type;
2142 expr = pet_expr_remove_duplicate_args(expr);
2143 expr = pet_expr_cow(expr);
2144 if (!expr)
2145 return expr;
2146 if (expr->type != pet_expr_access)
2147 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2148 "not an access expression", return pet_expr_free(expr));
2150 domain = isl_set_copy(data->domain);
2151 if (expr->n_arg > 0)
2152 domain = pet_value_bounds_apply(domain, expr->n_arg, expr->args,
2153 data->value_bounds);
2155 uset = isl_union_set_from_set(isl_set_copy(domain));
2156 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
2157 if (!expr->acc.access[type])
2158 continue;
2159 expr->acc.access[type] =
2160 isl_union_map_gist_domain(expr->acc.access[type],
2161 isl_union_set_copy(uset));
2162 if (!expr->acc.access[type])
2163 break;
2165 isl_union_set_free(uset);
2166 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
2167 if (type < pet_expr_access_end || !expr->acc.index)
2168 return pet_expr_free(expr);
2170 return expr;
2173 __isl_give pet_expr *pet_expr_gist(__isl_take pet_expr *expr,
2174 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2176 struct pet_access_gist_data data = { context, value_bounds };
2178 return pet_expr_map_access(expr, &access_gist, &data);
2181 /* Mark "expr" as a read dependening on "read".
2183 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
2184 int read)
2186 if (!expr)
2187 return pet_expr_free(expr);
2188 if (expr->type != pet_expr_access)
2189 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2190 "not an access expression", return pet_expr_free(expr));
2191 if (expr->acc.read == read)
2192 return expr;
2193 expr = pet_expr_cow(expr);
2194 if (!expr)
2195 return NULL;
2196 expr->acc.read = read;
2198 return expr;
2201 /* Mark "expr" as a write dependening on "write".
2203 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
2204 int write)
2206 if (!expr)
2207 return pet_expr_free(expr);
2208 if (expr->type != pet_expr_access)
2209 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2210 "not an access expression", return pet_expr_free(expr));
2211 if (expr->acc.write == write)
2212 return expr;
2213 expr = pet_expr_cow(expr);
2214 if (!expr)
2215 return NULL;
2216 expr->acc.write = write;
2218 return expr;
2221 /* Mark "expr" as a kill dependening on "kill".
2223 __isl_give pet_expr *pet_expr_access_set_kill(__isl_take pet_expr *expr,
2224 int kill)
2226 if (!expr)
2227 return pet_expr_free(expr);
2228 if (expr->type != pet_expr_access)
2229 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2230 "not an access expression", return pet_expr_free(expr));
2231 if (expr->acc.kill == kill)
2232 return expr;
2233 expr = pet_expr_cow(expr);
2234 if (!expr)
2235 return NULL;
2236 expr->acc.kill = kill;
2238 return expr;
2241 /* Map the access type "type" to the corresponding location
2242 * in the access array.
2243 * In particular, the access relation of type pet_expr_access_killed is
2244 * stored in the element at position pet_expr_access_fake_killed.
2246 static enum pet_expr_access_type internalize_type(
2247 enum pet_expr_access_type type)
2249 if (type == pet_expr_access_killed)
2250 return pet_expr_access_fake_killed;
2251 return type;
2254 /* Replace the access relation of the given "type" of "expr" by "access".
2255 * If the access relation is non-empty and the type is a read or a write,
2256 * then also mark the access expression itself as a read or a write.
2258 __isl_give pet_expr *pet_expr_access_set_access(__isl_take pet_expr *expr,
2259 enum pet_expr_access_type type, __isl_take isl_union_map *access)
2261 int empty;
2263 expr = pet_expr_cow(expr);
2264 if (!expr || !access)
2265 goto error;
2266 if (expr->type != pet_expr_access)
2267 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2268 "not an access expression", goto error);
2269 type = internalize_type(type);
2270 isl_union_map_free(expr->acc.access[type]);
2271 expr->acc.access[type] = access;
2273 if (expr->acc.kill)
2274 return expr;
2276 empty = isl_union_map_is_empty(access);
2277 if (empty < 0)
2278 return pet_expr_free(expr);
2279 if (empty)
2280 return expr;
2282 if (type == pet_expr_access_may_read)
2283 expr = pet_expr_access_set_read(expr, 1);
2284 else
2285 expr = pet_expr_access_set_write(expr, 1);
2287 return expr;
2288 error:
2289 isl_union_map_free(access);
2290 pet_expr_free(expr);
2291 return NULL;
2294 /* Replace the index expression of "expr" by "index" and
2295 * set the array depth accordingly.
2297 __isl_give pet_expr *pet_expr_access_set_index(__isl_take pet_expr *expr,
2298 __isl_take isl_multi_pw_aff *index)
2300 expr = pet_expr_cow(expr);
2301 if (!expr || !index)
2302 goto error;
2303 if (expr->type != pet_expr_access)
2304 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2305 "not an access expression", goto error);
2306 isl_multi_pw_aff_free(expr->acc.index);
2307 expr->acc.index = index;
2308 expr->acc.depth = isl_multi_pw_aff_dim(index, isl_dim_out);
2310 return expr;
2311 error:
2312 isl_multi_pw_aff_free(index);
2313 pet_expr_free(expr);
2314 return NULL;
2317 /* Return the reference identifier of access expression "expr".
2319 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr)
2321 if (!expr)
2322 return NULL;
2323 if (expr->type != pet_expr_access)
2324 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2325 "not an access expression", return NULL);
2327 return isl_id_copy(expr->acc.ref_id);
2330 /* Replace the reference identifier of access expression "expr" by "ref_id".
2332 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
2333 __isl_take isl_id *ref_id)
2335 expr = pet_expr_cow(expr);
2336 if (!expr || !ref_id)
2337 goto error;
2338 if (expr->type != pet_expr_access)
2339 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2340 "not an access expression", goto error);
2341 isl_id_free(expr->acc.ref_id);
2342 expr->acc.ref_id = ref_id;
2344 return expr;
2345 error:
2346 isl_id_free(ref_id);
2347 pet_expr_free(expr);
2348 return NULL;
2351 /* Tag the access relation "access" with "id".
2352 * That is, insert the id as the range of a wrapped relation
2353 * in the domain of "access".
2355 * If "access" is of the form
2357 * D[i] -> A[a]
2359 * then the result is of the form
2361 * [D[i] -> id[]] -> A[a]
2363 __isl_give isl_union_map *pet_expr_tag_access(__isl_keep pet_expr *expr,
2364 __isl_take isl_union_map *access)
2366 isl_space *space;
2367 isl_multi_aff *add_tag;
2368 isl_id *id;
2370 if (expr->type != pet_expr_access)
2371 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2372 "not an access expression",
2373 return isl_union_map_free(access));
2375 id = isl_id_copy(expr->acc.ref_id);
2376 space = pet_expr_access_get_domain_space(expr);
2377 space = isl_space_from_domain(space);
2378 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2379 add_tag = isl_multi_aff_domain_map(space);
2380 access = isl_union_map_preimage_domain_multi_aff(access, add_tag);
2382 return access;
2385 /* Return the access relation of the given "type" associated to "expr"
2386 * that maps pairs of domain iterations and argument values
2387 * to the corresponding accessed data elements.
2389 * If the requested access relation is explicitly available,
2390 * then return a copy. Otherwise, check if it is irrelevant for
2391 * the access expression and return an empty relation if this is the case.
2392 * Otherwise, introduce the requested access relation in "expr" and
2393 * return a copy.
2395 __isl_give isl_union_map *pet_expr_access_get_dependent_access(
2396 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2398 isl_union_map *access;
2399 int empty;
2401 if (!expr)
2402 return NULL;
2403 if (expr->type != pet_expr_access)
2404 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2405 "not an access expression", return NULL);
2407 type = internalize_type(type);
2408 if (expr->acc.access[type])
2409 return isl_union_map_copy(expr->acc.access[type]);
2411 if (type == pet_expr_access_may_read)
2412 empty = !expr->acc.read;
2413 else
2414 empty = !expr->acc.write;
2416 if (!empty) {
2417 expr = pet_expr_copy(expr);
2418 expr = introduce_access_relations(expr);
2419 if (!expr)
2420 return NULL;
2421 access = isl_union_map_copy(expr->acc.access[type]);
2422 pet_expr_free(expr);
2424 return access;
2427 return isl_union_map_empty(pet_expr_access_get_parameter_space(expr));
2430 /* Return the may read access relation associated to "expr"
2431 * that maps pairs of domain iterations and argument values
2432 * to the corresponding accessed data elements.
2434 __isl_give isl_union_map *pet_expr_access_get_dependent_may_read(
2435 __isl_keep pet_expr *expr)
2437 return pet_expr_access_get_dependent_access(expr,
2438 pet_expr_access_may_read);
2441 /* Return the may write access relation associated to "expr"
2442 * that maps pairs of domain iterations and argument values
2443 * to the corresponding accessed data elements.
2445 __isl_give isl_union_map *pet_expr_access_get_dependent_may_write(
2446 __isl_keep pet_expr *expr)
2448 return pet_expr_access_get_dependent_access(expr,
2449 pet_expr_access_may_write);
2452 /* Return the must write access relation associated to "expr"
2453 * that maps pairs of domain iterations and argument values
2454 * to the corresponding accessed data elements.
2456 __isl_give isl_union_map *pet_expr_access_get_dependent_must_write(
2457 __isl_keep pet_expr *expr)
2459 return pet_expr_access_get_dependent_access(expr,
2460 pet_expr_access_must_write);
2463 /* Return the relation of the given "type" mapping domain iterations
2464 * to the accessed data elements.
2465 * In particular, take the access relation and, in case of may_read
2466 * or may_write, project out the values of the arguments, if any.
2467 * In case of must_write, return the empty relation if there are
2468 * any arguments.
2470 __isl_give isl_union_map *pet_expr_access_get_access(__isl_keep pet_expr *expr,
2471 enum pet_expr_access_type type)
2473 isl_union_map *access;
2474 isl_space *space;
2475 isl_map *map;
2477 if (!expr)
2478 return NULL;
2479 if (expr->type != pet_expr_access)
2480 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2481 "not an access expression", return NULL);
2483 if (expr->n_arg != 0 && type == pet_expr_access_must_write) {
2484 space = pet_expr_access_get_parameter_space(expr);
2485 return isl_union_map_empty(space);
2488 access = pet_expr_access_get_dependent_access(expr, type);
2489 if (expr->n_arg == 0)
2490 return access;
2492 space = isl_multi_pw_aff_get_space(expr->acc.index);
2493 space = isl_space_domain(space);
2494 map = isl_map_universe(isl_space_unwrap(space));
2495 map = isl_map_domain_map(map);
2496 access = isl_union_map_apply_domain(access,
2497 isl_union_map_from_map(map));
2499 return access;
2502 /* Return the relation mapping domain iterations to all possibly
2503 * read data elements.
2505 __isl_give isl_union_map *pet_expr_access_get_may_read(
2506 __isl_keep pet_expr *expr)
2508 return pet_expr_access_get_access(expr, pet_expr_access_may_read);
2511 /* Return the relation mapping domain iterations to all possibly
2512 * written data elements.
2514 __isl_give isl_union_map *pet_expr_access_get_may_write(
2515 __isl_keep pet_expr *expr)
2517 return pet_expr_access_get_access(expr, pet_expr_access_may_write);
2520 /* Return a relation mapping domain iterations to definitely
2521 * written data elements, assuming the statement containing
2522 * the expression is executed.
2524 __isl_give isl_union_map *pet_expr_access_get_must_write(
2525 __isl_keep pet_expr *expr)
2527 return pet_expr_access_get_access(expr, pet_expr_access_must_write);
2530 /* Return the relation of the given "type" mapping domain iterations to
2531 * accessed data elements, with its domain tagged with the reference
2532 * identifier.
2534 static __isl_give isl_union_map *pet_expr_access_get_tagged_access(
2535 __isl_keep pet_expr *expr, enum pet_expr_access_type type)
2537 isl_union_map *access;
2539 if (!expr)
2540 return NULL;
2542 access = pet_expr_access_get_access(expr, type);
2543 access = pet_expr_tag_access(expr, access);
2545 return access;
2548 /* Return the relation mapping domain iterations to all possibly
2549 * read data elements, with its domain tagged with the reference
2550 * identifier.
2552 __isl_give isl_union_map *pet_expr_access_get_tagged_may_read(
2553 __isl_keep pet_expr *expr)
2555 return pet_expr_access_get_tagged_access(expr,
2556 pet_expr_access_may_read);
2559 /* Return the relation mapping domain iterations to all possibly
2560 * written data elements, with its domain tagged with the reference
2561 * identifier.
2563 __isl_give isl_union_map *pet_expr_access_get_tagged_may_write(
2564 __isl_keep pet_expr *expr)
2566 return pet_expr_access_get_tagged_access(expr,
2567 pet_expr_access_may_write);
2570 /* Return the operation type of operation expression "expr".
2572 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr)
2574 if (!expr)
2575 return pet_op_last;
2576 if (expr->type != pet_expr_op)
2577 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2578 "not an operation expression", return pet_op_last);
2580 return expr->op;
2583 /* Replace the operation type of operation expression "expr" by "type".
2585 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
2586 enum pet_op_type type)
2588 if (!expr)
2589 return pet_expr_free(expr);
2590 if (expr->type != pet_expr_op)
2591 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2592 "not an operation expression",
2593 return pet_expr_free(expr));
2594 if (expr->op == type)
2595 return expr;
2596 expr = pet_expr_cow(expr);
2597 if (!expr)
2598 return NULL;
2599 expr->op = type;
2601 return expr;
2604 /* Return the name of the function called by "expr".
2606 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr)
2608 if (!expr)
2609 return NULL;
2610 if (expr->type != pet_expr_call)
2611 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2612 "not a call expression", return NULL);
2613 return expr->c.name;
2616 /* Replace the name of the function called by "expr" by "name".
2618 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
2619 __isl_keep const char *name)
2621 expr = pet_expr_cow(expr);
2622 if (!expr || !name)
2623 return pet_expr_free(expr);
2624 if (expr->type != pet_expr_call)
2625 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2626 "not a call expression", return pet_expr_free(expr));
2627 free(expr->c.name);
2628 expr->c.name = strdup(name);
2629 if (!expr->c.name)
2630 return pet_expr_free(expr);
2631 return expr;
2634 /* Does the call expression "expr" have an associated function summary?
2636 int pet_expr_call_has_summary(__isl_keep pet_expr *expr)
2638 if (!expr)
2639 return -1;
2640 if (expr->type != pet_expr_call)
2641 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2642 "not a call expression", return -1);
2644 return expr->c.summary != NULL;
2647 /* Return a copy of the function summary associated to
2648 * the call expression "expr".
2650 __isl_give pet_function_summary *pet_expr_call_get_summary(
2651 __isl_keep pet_expr *expr)
2653 if (!expr)
2654 return NULL;
2655 if (expr->type != pet_expr_call)
2656 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2657 "not a call expression", return NULL);
2659 return pet_function_summary_copy(expr->c.summary);
2662 /* Replace the function summary associated to the call expression "expr"
2663 * by "summary".
2665 __isl_give pet_expr *pet_expr_call_set_summary(__isl_take pet_expr *expr,
2666 __isl_take pet_function_summary *summary)
2668 expr = pet_expr_cow(expr);
2669 if (!expr || !summary)
2670 goto error;
2671 if (expr->type != pet_expr_call)
2672 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2673 "not a call expression", goto error);
2674 pet_function_summary_free(expr->c.summary);
2675 expr->c.summary = summary;
2676 return expr;
2677 error:
2678 pet_function_summary_free(summary);
2679 return pet_expr_free(expr);
2682 /* Replace the type of the cast performed by "expr" by "name".
2684 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
2685 __isl_keep const char *name)
2687 expr = pet_expr_cow(expr);
2688 if (!expr || !name)
2689 return pet_expr_free(expr);
2690 if (expr->type != pet_expr_cast)
2691 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2692 "not a cast expression", return pet_expr_free(expr));
2693 free(expr->type_name);
2694 expr->type_name = strdup(name);
2695 if (!expr->type_name)
2696 return pet_expr_free(expr);
2697 return expr;
2700 /* Return the value of the integer represented by "expr".
2702 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr)
2704 if (!expr)
2705 return NULL;
2706 if (expr->type != pet_expr_int)
2707 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2708 "not an int expression", return NULL);
2710 return isl_val_copy(expr->i);
2713 /* Replace the value of the integer represented by "expr" by "v".
2715 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
2716 __isl_take isl_val *v)
2718 expr = pet_expr_cow(expr);
2719 if (!expr || !v)
2720 goto error;
2721 if (expr->type != pet_expr_int)
2722 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2723 "not an int expression", goto error);
2724 isl_val_free(expr->i);
2725 expr->i = v;
2727 return expr;
2728 error:
2729 isl_val_free(v);
2730 pet_expr_free(expr);
2731 return NULL;
2734 /* Replace the value and string representation of the double
2735 * represented by "expr" by "d" and "s".
2737 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
2738 double d, __isl_keep const char *s)
2740 expr = pet_expr_cow(expr);
2741 if (!expr || !s)
2742 return pet_expr_free(expr);
2743 if (expr->type != pet_expr_double)
2744 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2745 "not a double expression", return pet_expr_free(expr));
2746 expr->d.val = d;
2747 free(expr->d.s);
2748 expr->d.s = strdup(s);
2749 if (!expr->d.s)
2750 return pet_expr_free(expr);
2751 return expr;
2754 /* Return a string representation of the double expression "expr".
2756 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr)
2758 if (!expr)
2759 return NULL;
2760 if (expr->type != pet_expr_double)
2761 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2762 "not a double expression", return NULL);
2763 return strdup(expr->d.s);
2766 /* Return a piecewise affine expression defined on the specified domain
2767 * that represents NaN.
2769 static __isl_give isl_pw_aff *non_affine(__isl_take isl_space *space)
2771 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
2774 /* This function is called when we come across an access that is
2775 * nested in what is supposed to be an affine expression.
2776 * "pc" is the context in which the affine expression is created.
2777 * If nesting is allowed in "pc", we return an affine expression that is
2778 * equal to a new parameter corresponding to this nested access.
2779 * Otherwise, we return NaN.
2781 * Note that we currently don't allow nested accesses themselves
2782 * to contain any nested accesses, so we check if "expr" itself
2783 * involves any nested accesses (either explicitly as arguments
2784 * or implicitly through parameters) and return NaN if it does.
2786 * The new parameter is resolved in resolve_nested.
2788 static __isl_give isl_pw_aff *nested_access(__isl_keep pet_expr *expr,
2789 __isl_keep pet_context *pc)
2791 isl_ctx *ctx;
2792 isl_id *id;
2793 isl_space *space;
2794 isl_local_space *ls;
2795 isl_aff *aff;
2796 int nested;
2798 if (!expr || !pc)
2799 return NULL;
2800 if (!pet_context_allow_nesting(pc))
2801 return non_affine(pet_context_get_space(pc));
2803 if (pet_expr_get_type(expr) != pet_expr_access)
2804 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2805 "not an access expression", return NULL);
2807 if (expr->n_arg > 0)
2808 return non_affine(pet_context_get_space(pc));
2810 space = pet_expr_access_get_parameter_space(expr);
2811 nested = pet_nested_any_in_space(space);
2812 isl_space_free(space);
2813 if (nested)
2814 return non_affine(pet_context_get_space(pc));
2816 ctx = pet_expr_get_ctx(expr);
2817 id = pet_nested_pet_expr(pet_expr_copy(expr));
2818 space = pet_context_get_space(pc);
2819 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2821 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
2822 ls = isl_local_space_from_space(space);
2823 aff = isl_aff_var_on_domain(ls, isl_dim_param, 0);
2825 return isl_pw_aff_from_aff(aff);
2828 /* Extract an affine expression from the access pet_expr "expr".
2829 * "pc" is the context in which the affine expression is created.
2831 * If "expr" is actually an affine expression rather than
2832 * a real access, then we return that expression.
2833 * Otherwise, we require that "expr" is of an integral type.
2834 * If not, we return NaN.
2836 * If the variable has been assigned a known affine expression,
2837 * then we return that expression.
2839 * Otherwise, we return an expression that is equal to a parameter
2840 * representing "expr" (if "allow_nested" is set).
2842 static __isl_give isl_pw_aff *extract_affine_from_access(
2843 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
2845 int pos;
2846 isl_id *id;
2848 if (pet_expr_is_affine(expr))
2849 return pet_expr_get_affine(expr);
2851 if (pet_expr_get_type_size(expr) == 0)
2852 return non_affine(pet_context_get_space(pc));
2854 if (!pet_expr_is_scalar_access(expr))
2855 return nested_access(expr, pc);
2857 id = pet_expr_access_get_id(expr);
2858 if (pet_context_is_assigned(pc, id))
2859 return pet_context_get_value(pc, id);
2861 isl_id_free(id);
2862 return nested_access(expr, pc);
2865 /* Construct an affine expression from the integer constant "expr".
2866 * "pc" is the context in which the affine expression is created.
2868 static __isl_give isl_pw_aff *extract_affine_from_int(__isl_keep pet_expr *expr,
2869 __isl_keep pet_context *pc)
2871 isl_local_space *ls;
2872 isl_aff *aff;
2874 if (!expr)
2875 return NULL;
2877 ls = isl_local_space_from_space(pet_context_get_space(pc));
2878 aff = isl_aff_val_on_domain(ls, pet_expr_int_get_val(expr));
2880 return isl_pw_aff_from_aff(aff);
2883 /* Extract an affine expression from an addition or subtraction operation.
2884 * Return NaN if we are unable to extract an affine expression.
2886 * "pc" is the context in which the affine expression is created.
2888 static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr,
2889 __isl_keep pet_context *pc)
2891 isl_pw_aff *lhs;
2892 isl_pw_aff *rhs;
2894 if (!expr)
2895 return NULL;
2896 if (expr->n_arg != 2)
2897 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2898 "expecting two arguments", return NULL);
2900 lhs = pet_expr_extract_affine(expr->args[0], pc);
2901 rhs = pet_expr_extract_affine(expr->args[1], pc);
2903 switch (pet_expr_op_get_type(expr)) {
2904 case pet_op_add:
2905 return isl_pw_aff_add(lhs, rhs);
2906 case pet_op_sub:
2907 return isl_pw_aff_sub(lhs, rhs);
2908 default:
2909 isl_pw_aff_free(lhs);
2910 isl_pw_aff_free(rhs);
2911 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2912 "not an addition or subtraction operation",
2913 return NULL);
2918 /* Extract an affine expression from an integer division or a modulo operation.
2919 * Return NaN if we are unable to extract an affine expression.
2921 * "pc" is the context in which the affine expression is created.
2923 * In particular, if "expr" is lhs/rhs, then return
2925 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
2927 * If "expr" is lhs%rhs, then return
2929 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
2931 * If the second argument (rhs) is not a (positive) integer constant,
2932 * then we fail to extract an affine expression.
2934 * We simplify the result in the context of the domain of "pc" in case
2935 * this domain implies that lhs >= 0 (or < 0).
2937 static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr,
2938 __isl_keep pet_context *pc)
2940 int is_cst;
2941 isl_pw_aff *lhs;
2942 isl_pw_aff *rhs;
2943 isl_pw_aff *res;
2945 if (!expr)
2946 return NULL;
2947 if (expr->n_arg != 2)
2948 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2949 "expecting two arguments", return NULL);
2951 rhs = pet_expr_extract_affine(expr->args[1], pc);
2953 is_cst = isl_pw_aff_is_cst(rhs);
2954 if (is_cst < 0 || !is_cst) {
2955 isl_pw_aff_free(rhs);
2956 return non_affine(pet_context_get_space(pc));
2959 lhs = pet_expr_extract_affine(expr->args[0], pc);
2961 switch (pet_expr_op_get_type(expr)) {
2962 case pet_op_div:
2963 res = isl_pw_aff_tdiv_q(lhs, rhs);
2964 break;
2965 case pet_op_mod:
2966 res = isl_pw_aff_tdiv_r(lhs, rhs);
2967 break;
2968 default:
2969 isl_pw_aff_free(lhs);
2970 isl_pw_aff_free(rhs);
2971 isl_die(pet_expr_get_ctx(expr), isl_error_internal,
2972 "not a div or mod operator", return NULL);
2975 return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc));
2978 /* Extract an affine expression from a multiplication operation.
2979 * Return NaN if we are unable to extract an affine expression.
2980 * In particular, if neither of the arguments is a (piecewise) constant
2981 * then we return NaN.
2983 * "pc" is the context in which the affine expression is created.
2985 static __isl_give isl_pw_aff *extract_affine_mul(__isl_keep pet_expr *expr,
2986 __isl_keep pet_context *pc)
2988 int lhs_cst, rhs_cst;
2989 isl_pw_aff *lhs;
2990 isl_pw_aff *rhs;
2992 if (!expr)
2993 return NULL;
2994 if (expr->n_arg != 2)
2995 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
2996 "expecting two arguments", return NULL);
2998 lhs = pet_expr_extract_affine(expr->args[0], pc);
2999 rhs = pet_expr_extract_affine(expr->args[1], pc);
3001 lhs_cst = isl_pw_aff_is_cst(lhs);
3002 rhs_cst = isl_pw_aff_is_cst(rhs);
3003 if (lhs_cst >= 0 && rhs_cst >= 0 && (lhs_cst || rhs_cst))
3004 return isl_pw_aff_mul(lhs, rhs);
3006 isl_pw_aff_free(lhs);
3007 isl_pw_aff_free(rhs);
3009 if (lhs_cst < 0 || rhs_cst < 0)
3010 return NULL;
3012 return non_affine(pet_context_get_space(pc));
3015 /* Extract an affine expression from a negation operation.
3016 * Return NaN if we are unable to extract an affine expression.
3018 * "pc" is the context in which the affine expression is created.
3020 static __isl_give isl_pw_aff *extract_affine_neg(__isl_keep pet_expr *expr,
3021 __isl_keep pet_context *pc)
3023 isl_pw_aff *res;
3025 if (!expr)
3026 return NULL;
3027 if (expr->n_arg != 1)
3028 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3029 "expecting one argument", return NULL);
3031 res = pet_expr_extract_affine(expr->args[0], pc);
3032 return isl_pw_aff_neg(res);
3035 /* Extract an affine expression from a conditional operation.
3036 * Return NaN if we are unable to extract an affine expression.
3038 * "pc" is the context in which the affine expression is created.
3040 static __isl_give isl_pw_aff *extract_affine_cond(__isl_keep pet_expr *expr,
3041 __isl_keep pet_context *pc)
3043 isl_pw_aff *cond, *lhs, *rhs;
3045 if (!expr)
3046 return NULL;
3047 if (expr->n_arg != 3)
3048 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3049 "expecting three arguments", return NULL);
3051 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3052 lhs = pet_expr_extract_affine(expr->args[1], pc);
3053 rhs = pet_expr_extract_affine(expr->args[2], pc);
3055 return isl_pw_aff_cond(cond, lhs, rhs);
3058 /* Limit the domain of "pwaff" to those elements where the function
3059 * value satisfies
3061 * 2^{width-1} <= pwaff < 2^{width-1}
3063 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
3064 unsigned width)
3066 isl_ctx *ctx;
3067 isl_val *v;
3068 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
3069 isl_local_space *ls = isl_local_space_from_space(space);
3070 isl_aff *bound;
3071 isl_set *dom;
3072 isl_pw_aff *b;
3074 ctx = isl_pw_aff_get_ctx(pwaff);
3075 v = isl_val_int_from_ui(ctx, width - 1);
3076 v = isl_val_2exp(v);
3078 bound = isl_aff_zero_on_domain(ls);
3079 bound = isl_aff_add_constant_val(bound, v);
3080 b = isl_pw_aff_from_aff(bound);
3082 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
3083 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3085 b = isl_pw_aff_neg(b);
3086 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
3087 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3089 return pwaff;
3092 /* Handle potential overflows on signed computations.
3094 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
3095 * then we adjust the domain of "pa" to avoid overflows.
3097 static __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
3098 unsigned width)
3100 isl_ctx *ctx;
3101 struct pet_options *options;
3103 if (!pa)
3104 return NULL;
3106 ctx = isl_pw_aff_get_ctx(pa);
3107 options = isl_ctx_peek_pet_options(ctx);
3108 if (!options || options->signed_overflow == PET_OVERFLOW_AVOID)
3109 pa = avoid_overflow(pa, width);
3111 return pa;
3114 /* Extract an affine expression from some an operation.
3115 * Return NaN if we are unable to extract an affine expression.
3116 * If the result of a binary (non boolean) operation is unsigned,
3117 * then we wrap it based on the size of the type. If the result is signed,
3118 * then we ensure that no overflow occurs.
3120 * "pc" is the context in which the affine expression is created.
3122 static __isl_give isl_pw_aff *extract_affine_from_op(__isl_keep pet_expr *expr,
3123 __isl_keep pet_context *pc)
3125 isl_pw_aff *res;
3126 int type_size;
3128 switch (pet_expr_op_get_type(expr)) {
3129 case pet_op_add:
3130 case pet_op_sub:
3131 res = extract_affine_add_sub(expr, pc);
3132 break;
3133 case pet_op_div:
3134 case pet_op_mod:
3135 res = extract_affine_div_mod(expr, pc);
3136 break;
3137 case pet_op_mul:
3138 res = extract_affine_mul(expr, pc);
3139 break;
3140 case pet_op_minus:
3141 return extract_affine_neg(expr, pc);
3142 case pet_op_cond:
3143 return extract_affine_cond(expr, pc);
3144 case pet_op_eq:
3145 case pet_op_ne:
3146 case pet_op_le:
3147 case pet_op_ge:
3148 case pet_op_lt:
3149 case pet_op_gt:
3150 case pet_op_land:
3151 case pet_op_lor:
3152 case pet_op_lnot:
3153 return pet_expr_extract_affine_condition(expr, pc);
3154 default:
3155 return non_affine(pet_context_get_space(pc));
3158 if (!res)
3159 return NULL;
3160 if (isl_pw_aff_involves_nan(res)) {
3161 isl_space *space = isl_pw_aff_get_domain_space(res);
3162 isl_pw_aff_free(res);
3163 return non_affine(space);
3166 type_size = pet_expr_get_type_size(expr);
3167 if (type_size > 0)
3168 res = pet_wrap_pw_aff(res, type_size);
3169 else
3170 res = signed_overflow(res, -type_size);
3172 return res;
3175 /* Internal data structure for affine builtin function declarations.
3177 * "pencil" is set if the builtin is pencil specific.
3178 * "n_args" is the number of arguments the function takes.
3179 * "name" is the function name.
3181 struct affine_builtin_decl {
3182 int pencil;
3183 int n_args;
3184 const char *name;
3187 static struct affine_builtin_decl affine_builtins[] = {
3188 { 0, 2, "min" },
3189 { 1, 2, "imin" },
3190 { 1, 2, "umin" },
3191 { 0, 2, "max" },
3192 { 1, 2, "imax" },
3193 { 1, 2, "umax" },
3194 { 0, 2, "intMod" },
3195 { 0, 2, "intFloor" },
3196 { 0, 2, "intCeil" },
3197 { 0, 2, "floord" },
3198 { 0, 2, "ceild" }
3201 /* List of min and max builtin functions.
3203 static const char *min_max_builtins[] = {
3204 "min", "imin", "umin",
3205 "max", "imax", "umax"
3208 /* Is a function call to "name" with "n_args" arguments a call to a
3209 * builtin function for which we can construct an affine expression?
3210 * pencil specific builtins are only recognized if "pencil" is set.
3212 static int is_affine_builtin(int pencil, int n_args, const char *name)
3214 int i;
3216 for (i = 0; i < ARRAY_SIZE(affine_builtins); ++i) {
3217 struct affine_builtin_decl *decl = &affine_builtins[i];
3219 if (decl->pencil && !pencil)
3220 continue;
3221 if (decl->n_args == n_args && !strcmp(decl->name, name))
3222 return 1;
3225 return 0;
3228 /* Is function "name" a known min or max builtin function?
3230 static int is_min_or_max_builtin(const char *name)
3232 int i;
3234 for (i = 0; i < ARRAY_SIZE(min_max_builtins); ++i)
3235 if (!strcmp(min_max_builtins[i], name))
3236 return 1;
3238 return 0;
3241 /* Extract an affine expression from some special function calls.
3242 * Return NaN if we are unable to extract an affine expression.
3243 * In particular, we handle "min", "max", "ceild", "floord",
3244 * "intMod", "intFloor" and "intCeil".
3245 * In case of the latter five, the second argument needs to be
3246 * a (positive) integer constant.
3247 * If the pencil option is set, then we also handle "{i,u}min" and
3248 * "{i,u}max".
3250 * "pc" is the context in which the affine expression is created.
3252 static __isl_give isl_pw_aff *extract_affine_from_call(
3253 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3255 isl_ctx *ctx;
3256 isl_pw_aff *aff1, *aff2;
3257 int n;
3258 const char *name;
3259 struct pet_options *options;
3261 if (!expr)
3262 return NULL;
3263 ctx = pet_expr_get_ctx(expr);
3264 options = isl_ctx_peek_pet_options(ctx);
3266 n = pet_expr_get_n_arg(expr);
3267 name = pet_expr_call_get_name(expr);
3268 if (!is_affine_builtin(options->pencil, n, name))
3269 return non_affine(pet_context_get_space(pc));
3271 if (is_min_or_max_builtin(name)) {
3272 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3273 aff2 = pet_expr_extract_affine(expr->args[1], pc);
3275 if (strstr(name, "min"))
3276 aff1 = isl_pw_aff_min(aff1, aff2);
3277 else
3278 aff1 = isl_pw_aff_max(aff1, aff2);
3279 } else if (!strcmp(name, "intMod")) {
3280 isl_val *v;
3282 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3283 return non_affine(pet_context_get_space(pc));
3284 v = pet_expr_int_get_val(expr->args[1]);
3285 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3286 aff1 = isl_pw_aff_mod_val(aff1, v);
3287 } else {
3288 isl_val *v;
3290 if (pet_expr_get_type(expr->args[1]) != pet_expr_int)
3291 return non_affine(pet_context_get_space(pc));
3292 v = pet_expr_int_get_val(expr->args[1]);
3293 aff1 = pet_expr_extract_affine(expr->args[0], pc);
3294 aff1 = isl_pw_aff_scale_down_val(aff1, v);
3295 if (!strcmp(name, "floord") || !strcmp(name, "intFloor"))
3296 aff1 = isl_pw_aff_floor(aff1);
3297 else
3298 aff1 = isl_pw_aff_ceil(aff1);
3301 return aff1;
3304 /* Extract an affine expression from "expr", if possible.
3305 * Otherwise return NaN.
3307 * "pc" is the context in which the affine expression is created.
3309 * Store the result in "pc" such that it can be reused in case
3310 * pet_expr_extract_affine is called again on the same pair of
3311 * "expr" and "pc".
3313 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
3314 __isl_keep pet_context *pc)
3316 isl_maybe_isl_pw_aff m;
3317 isl_pw_aff *pa;
3319 if (!expr)
3320 return NULL;
3322 m = pet_context_get_extracted_affine(pc, expr);
3323 if (m.valid < 0 || m.valid)
3324 return m.value;
3326 switch (pet_expr_get_type(expr)) {
3327 case pet_expr_access:
3328 pa = extract_affine_from_access(expr, pc);
3329 break;
3330 case pet_expr_int:
3331 pa = extract_affine_from_int(expr, pc);
3332 break;
3333 case pet_expr_op:
3334 pa = extract_affine_from_op(expr, pc);
3335 break;
3336 case pet_expr_call:
3337 pa = extract_affine_from_call(expr, pc);
3338 break;
3339 case pet_expr_cast:
3340 case pet_expr_double:
3341 case pet_expr_error:
3342 pa = non_affine(pet_context_get_space(pc));
3343 break;
3346 if (pet_context_set_extracted_affine(pc, expr, pa) < 0)
3347 return isl_pw_aff_free(pa);
3349 return pa;
3352 /* Extract an affine expressions representing the comparison "LHS op RHS"
3353 * Return NaN if we are unable to extract such an affine expression.
3355 * "pc" is the context in which the affine expression is created.
3357 * If the comparison is of the form
3359 * a <= min(b,c)
3361 * then the expression is constructed as the conjunction of
3362 * the comparisons
3364 * a <= b and a <= c
3366 * A similar optimization is performed for max(a,b) <= c.
3367 * We do this because that will lead to simpler representations
3368 * of the expression.
3369 * If isl is ever enhanced to explicitly deal with min and max expressions,
3370 * this optimization can be removed.
3372 __isl_give isl_pw_aff *pet_expr_extract_comparison(enum pet_op_type op,
3373 __isl_keep pet_expr *lhs, __isl_keep pet_expr *rhs,
3374 __isl_keep pet_context *pc)
3376 isl_pw_aff *lhs_pa, *rhs_pa;
3378 if (op == pet_op_gt)
3379 return pet_expr_extract_comparison(pet_op_lt, rhs, lhs, pc);
3380 if (op == pet_op_ge)
3381 return pet_expr_extract_comparison(pet_op_le, rhs, lhs, pc);
3383 if (op == pet_op_lt || op == pet_op_le) {
3384 if (pet_expr_is_min(rhs)) {
3385 lhs_pa = pet_expr_extract_comparison(op, lhs,
3386 rhs->args[0], pc);
3387 rhs_pa = pet_expr_extract_comparison(op, lhs,
3388 rhs->args[1], pc);
3389 return pet_and(lhs_pa, rhs_pa);
3391 if (pet_expr_is_max(lhs)) {
3392 lhs_pa = pet_expr_extract_comparison(op, lhs->args[0],
3393 rhs, pc);
3394 rhs_pa = pet_expr_extract_comparison(op, lhs->args[1],
3395 rhs, pc);
3396 return pet_and(lhs_pa, rhs_pa);
3400 lhs_pa = pet_expr_extract_affine(lhs, pc);
3401 rhs_pa = pet_expr_extract_affine(rhs, pc);
3403 return pet_comparison(op, lhs_pa, rhs_pa);
3406 /* Extract an affine expressions from the comparison "expr".
3407 * Return NaN if we are unable to extract such an affine expression.
3409 * "pc" is the context in which the affine expression is created.
3411 static __isl_give isl_pw_aff *extract_comparison(__isl_keep pet_expr *expr,
3412 __isl_keep pet_context *pc)
3414 enum pet_op_type type;
3416 if (!expr)
3417 return NULL;
3418 if (expr->n_arg != 2)
3419 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3420 "expecting two arguments", return NULL);
3422 type = pet_expr_op_get_type(expr);
3423 return pet_expr_extract_comparison(type, expr->args[0], expr->args[1],
3424 pc);
3427 /* Extract an affine expression representing the boolean operation
3428 * expressed by "expr".
3429 * Return NaN if we are unable to extract an affine expression.
3431 * "pc" is the context in which the affine expression is created.
3433 static __isl_give isl_pw_aff *extract_boolean(__isl_keep pet_expr *expr,
3434 __isl_keep pet_context *pc)
3436 isl_pw_aff *lhs, *rhs;
3437 int n;
3439 if (!expr)
3440 return NULL;
3442 n = pet_expr_get_n_arg(expr);
3443 lhs = pet_expr_extract_affine_condition(expr->args[0], pc);
3444 if (n == 1)
3445 return pet_not(lhs);
3447 rhs = pet_expr_extract_affine_condition(expr->args[1], pc);
3448 return pet_boolean(pet_expr_op_get_type(expr), lhs, rhs);
3451 /* Extract the affine expression "expr != 0 ? 1 : 0".
3452 * Return NaN if we are unable to extract an affine expression.
3454 * "pc" is the context in which the affine expression is created.
3456 static __isl_give isl_pw_aff *extract_implicit_condition(
3457 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3459 isl_pw_aff *res;
3461 res = pet_expr_extract_affine(expr, pc);
3462 return pet_to_bool(res);
3465 /* Extract a boolean affine expression from "expr".
3466 * Return NaN if we are unable to extract an affine expression.
3468 * "pc" is the context in which the affine expression is created.
3470 * If "expr" is neither a comparison nor a boolean operation,
3471 * then we assume it is an affine expression and return the
3472 * boolean expression "expr != 0 ? 1 : 0".
3474 __isl_give isl_pw_aff *pet_expr_extract_affine_condition(
3475 __isl_keep pet_expr *expr, __isl_keep pet_context *pc)
3477 if (!expr)
3478 return NULL;
3480 if (pet_expr_is_comparison(expr))
3481 return extract_comparison(expr, pc);
3482 if (pet_expr_is_boolean(expr))
3483 return extract_boolean(expr, pc);
3485 return extract_implicit_condition(expr, pc);
3488 /* Check if "expr" is an assume expression and if its single argument
3489 * can be converted to an affine expression in the context of "pc".
3490 * If so, replace the argument by the affine expression.
3492 __isl_give pet_expr *pet_expr_resolve_assume(__isl_take pet_expr *expr,
3493 __isl_keep pet_context *pc)
3495 isl_pw_aff *cond;
3496 isl_multi_pw_aff *index;
3498 if (!expr)
3499 return NULL;
3500 if (!pet_expr_is_assume(expr))
3501 return expr;
3502 if (expr->n_arg != 1)
3503 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3504 "expecting one argument", return pet_expr_free(expr));
3506 cond = pet_expr_extract_affine_condition(expr->args[0], pc);
3507 if (!cond)
3508 return pet_expr_free(expr);
3509 if (isl_pw_aff_involves_nan(cond)) {
3510 isl_pw_aff_free(cond);
3511 return expr;
3514 index = isl_multi_pw_aff_from_pw_aff(cond);
3515 expr = pet_expr_set_arg(expr, 0, pet_expr_from_index(index));
3517 return expr;
3520 /* Return the number of bits needed to represent the type of "expr".
3521 * See the description of the type_size field of pet_expr.
3523 int pet_expr_get_type_size(__isl_keep pet_expr *expr)
3525 return expr ? expr->type_size : 0;
3528 /* Replace the number of bits needed to represent the type of "expr"
3529 * by "type_size".
3530 * See the description of the type_size field of pet_expr.
3532 __isl_give pet_expr *pet_expr_set_type_size(__isl_take pet_expr *expr,
3533 int type_size)
3535 expr = pet_expr_cow(expr);
3536 if (!expr)
3537 return NULL;
3539 expr->type_size = type_size;
3541 return expr;
3544 /* Extend an access expression "expr" with an additional index "index".
3545 * In particular, add "index" as an extra argument to "expr" and
3546 * adjust the index expression of "expr" to refer to this extra argument.
3547 * The caller is responsible for calling pet_expr_access_set_depth
3548 * to update the corresponding access relation.
3550 * Note that we only collect the individual index expressions as
3551 * arguments of "expr" here.
3552 * An attempt to integrate them into the index expression of "expr"
3553 * is performed in pet_expr_access_plug_in_args.
3555 __isl_give pet_expr *pet_expr_access_subscript(__isl_take pet_expr *expr,
3556 __isl_take pet_expr *index)
3558 int n;
3559 isl_space *space;
3560 isl_local_space *ls;
3561 isl_pw_aff *pa;
3563 expr = pet_expr_cow(expr);
3564 if (!expr || !index)
3565 goto error;
3566 if (expr->type != pet_expr_access)
3567 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3568 "not an access pet_expr", goto error);
3570 n = pet_expr_get_n_arg(expr);
3571 expr = pet_expr_insert_arg(expr, n, index);
3572 if (!expr)
3573 return NULL;
3575 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3576 ls = isl_local_space_from_space(space);
3577 pa = isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, isl_dim_set, n));
3578 expr->acc.index = pet_array_subscript(expr->acc.index, pa);
3579 if (!expr->acc.index)
3580 return pet_expr_free(expr);
3582 return expr;
3583 error:
3584 pet_expr_free(expr);
3585 pet_expr_free(index);
3586 return NULL;
3589 /* Extend an access expression "expr" with an additional member acces to "id".
3590 * In particular, extend the index expression of "expr" to include
3591 * the additional member access.
3592 * The caller is responsible for calling pet_expr_access_set_depth
3593 * to update the corresponding access relation.
3595 __isl_give pet_expr *pet_expr_access_member(__isl_take pet_expr *expr,
3596 __isl_take isl_id *id)
3598 isl_space *space;
3599 isl_multi_pw_aff *field_access;
3601 expr = pet_expr_cow(expr);
3602 if (!expr || !id)
3603 goto error;
3604 if (expr->type != pet_expr_access)
3605 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3606 "not an access pet_expr", goto error);
3608 space = isl_multi_pw_aff_get_domain_space(expr->acc.index);
3609 space = isl_space_from_domain(space);
3610 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3611 field_access = isl_multi_pw_aff_zero(space);
3612 expr->acc.index = pet_array_member(expr->acc.index, field_access);
3613 if (!expr->acc.index)
3614 return pet_expr_free(expr);
3616 return expr;
3617 error:
3618 pet_expr_free(expr);
3619 isl_id_free(id);
3620 return NULL;
3623 /* Prefix the access expression "expr" with "prefix".
3624 * If "add" is set, then it is not the index expression "prefix" itself
3625 * that was passed to the function, but its address.
3627 __isl_give pet_expr *pet_expr_access_patch(__isl_take pet_expr *expr,
3628 __isl_take isl_multi_pw_aff *prefix, int add)
3630 enum pet_expr_access_type type;
3632 expr = pet_expr_cow(expr);
3633 if (!expr || !prefix)
3634 goto error;
3635 if (expr->type != pet_expr_access)
3636 isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
3637 "not an access pet_expr", goto error);
3639 expr->acc.depth += isl_multi_pw_aff_dim(prefix, isl_dim_out) - add;
3640 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
3641 if (!expr->acc.access[type])
3642 continue;
3643 expr->acc.access[type] = pet_patch_union_map(
3644 isl_multi_pw_aff_copy(prefix), expr->acc.access[type],
3645 add, 0);
3646 if (!expr->acc.access[type])
3647 break;
3649 expr->acc.index = pet_patch_multi_pw_aff(prefix, expr->acc.index, add);
3650 if (!expr->acc.index || type < pet_expr_access_end)
3651 return pet_expr_free(expr);
3653 return expr;
3654 error:
3655 pet_expr_free(expr);
3656 isl_multi_pw_aff_free(prefix);
3657 return NULL;
3660 /* Dump the arguments of "expr" to "p" as a YAML sequence keyed
3661 * by "args", if there are any such arguments.
3663 static __isl_give isl_printer *dump_arguments(__isl_keep pet_expr *expr,
3664 __isl_take isl_printer *p)
3666 int i;
3668 if (expr->n_arg == 0)
3669 return p;
3671 p = isl_printer_print_str(p, "args");
3672 p = isl_printer_yaml_next(p);
3673 p = isl_printer_yaml_start_sequence(p);
3674 for (i = 0; i < expr->n_arg; ++i) {
3675 p = pet_expr_print(expr->args[i], p);
3676 p = isl_printer_yaml_next(p);
3678 p = isl_printer_yaml_end_sequence(p);
3680 return p;
3683 /* Print "expr" to "p" in YAML format.
3685 __isl_give isl_printer *pet_expr_print(__isl_keep pet_expr *expr,
3686 __isl_take isl_printer *p)
3688 if (!expr || !p)
3689 return isl_printer_free(p);
3691 switch (expr->type) {
3692 case pet_expr_double:
3693 p = isl_printer_print_str(p, expr->d.s);
3694 break;
3695 case pet_expr_int:
3696 p = isl_printer_print_val(p, expr->i);
3697 break;
3698 case pet_expr_access:
3699 p = isl_printer_yaml_start_mapping(p);
3700 if (expr->acc.ref_id) {
3701 p = isl_printer_print_str(p, "ref_id");
3702 p = isl_printer_yaml_next(p);
3703 p = isl_printer_print_id(p, expr->acc.ref_id);
3704 p = isl_printer_yaml_next(p);
3706 p = isl_printer_print_str(p, "index");
3707 p = isl_printer_yaml_next(p);
3708 p = isl_printer_print_multi_pw_aff(p, expr->acc.index);
3709 p = isl_printer_yaml_next(p);
3710 p = isl_printer_print_str(p, "depth");
3711 p = isl_printer_yaml_next(p);
3712 p = isl_printer_print_int(p, expr->acc.depth);
3713 p = isl_printer_yaml_next(p);
3714 if (expr->acc.kill) {
3715 p = isl_printer_print_str(p, "kill");
3716 p = isl_printer_yaml_next(p);
3717 p = isl_printer_print_int(p, 1);
3718 p = isl_printer_yaml_next(p);
3719 } else {
3720 p = isl_printer_print_str(p, "read");
3721 p = isl_printer_yaml_next(p);
3722 p = isl_printer_print_int(p, expr->acc.read);
3723 p = isl_printer_yaml_next(p);
3724 p = isl_printer_print_str(p, "write");
3725 p = isl_printer_yaml_next(p);
3726 p = isl_printer_print_int(p, expr->acc.write);
3727 p = isl_printer_yaml_next(p);
3729 if (expr->acc.access[pet_expr_access_may_read]) {
3730 p = isl_printer_print_str(p, "may_read");
3731 p = isl_printer_yaml_next(p);
3732 p = isl_printer_print_union_map(p,
3733 expr->acc.access[pet_expr_access_may_read]);
3734 p = isl_printer_yaml_next(p);
3736 if (expr->acc.access[pet_expr_access_may_write]) {
3737 p = isl_printer_print_str(p, "may_write");
3738 p = isl_printer_yaml_next(p);
3739 p = isl_printer_print_union_map(p,
3740 expr->acc.access[pet_expr_access_may_write]);
3741 p = isl_printer_yaml_next(p);
3743 if (expr->acc.access[pet_expr_access_must_write]) {
3744 p = isl_printer_print_str(p, "must_write");
3745 p = isl_printer_yaml_next(p);
3746 p = isl_printer_print_union_map(p,
3747 expr->acc.access[pet_expr_access_must_write]);
3748 p = isl_printer_yaml_next(p);
3750 p = dump_arguments(expr, p);
3751 p = isl_printer_yaml_end_mapping(p);
3752 break;
3753 case pet_expr_op:
3754 p = isl_printer_yaml_start_mapping(p);
3755 p = isl_printer_print_str(p, "op");
3756 p = isl_printer_yaml_next(p);
3757 p = isl_printer_print_str(p, op_str[expr->op]);
3758 p = isl_printer_yaml_next(p);
3759 p = dump_arguments(expr, p);
3760 p = isl_printer_yaml_end_mapping(p);
3761 break;
3762 case pet_expr_call:
3763 p = isl_printer_yaml_start_mapping(p);
3764 p = isl_printer_print_str(p, "call");
3765 p = isl_printer_yaml_next(p);
3766 p = isl_printer_print_str(p, expr->c.name);
3767 p = isl_printer_print_str(p, "/");
3768 p = isl_printer_print_int(p, expr->n_arg);
3769 p = isl_printer_yaml_next(p);
3770 p = dump_arguments(expr, p);
3771 if (expr->c.summary) {
3772 p = isl_printer_print_str(p, "summary");
3773 p = isl_printer_yaml_next(p);
3774 p = pet_function_summary_print(expr->c.summary, p);
3776 p = isl_printer_yaml_end_mapping(p);
3777 break;
3778 case pet_expr_cast:
3779 p = isl_printer_yaml_start_mapping(p);
3780 p = isl_printer_print_str(p, "cast");
3781 p = isl_printer_yaml_next(p);
3782 p = isl_printer_print_str(p, expr->type_name);
3783 p = isl_printer_yaml_next(p);
3784 p = dump_arguments(expr, p);
3785 p = isl_printer_yaml_end_mapping(p);
3786 break;
3787 case pet_expr_error:
3788 p = isl_printer_print_str(p, "ERROR");
3789 break;
3792 return p;
3795 /* Dump "expr" to stderr with indentation "indent".
3797 void pet_expr_dump_with_indent(__isl_keep pet_expr *expr, int indent)
3799 isl_printer *p;
3801 if (!expr)
3802 return;
3804 p = isl_printer_to_file(pet_expr_get_ctx(expr), stderr);
3805 p = isl_printer_set_indent(p, indent);
3806 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
3807 p = isl_printer_start_line(p);
3808 p = pet_expr_print(expr, p);
3810 isl_printer_free(p);
3813 void pet_expr_dump(__isl_keep pet_expr *expr)
3815 pet_expr_dump_with_indent(expr, 0);