scop.c: expr_propagate_params: use pet_expr_map_access
[pet.git] / scop.c
bloba0c0d3fcea832c438d0d676a3ef204d929c52d04
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
33 */
35 #include <string.h>
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
39 #include "filter.h"
40 #include "scop.h"
41 #include "print.h"
43 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
45 static char *type_str[] = {
46 [pet_expr_access] = "access",
47 [pet_expr_call] = "call",
48 [pet_expr_cast] = "cast",
49 [pet_expr_double] = "double",
50 [pet_expr_int] = "int",
51 [pet_expr_op] = "op",
54 static char *op_str[] = {
55 [pet_op_add_assign] = "+=",
56 [pet_op_sub_assign] = "-=",
57 [pet_op_mul_assign] = "*=",
58 [pet_op_div_assign] = "/=",
59 [pet_op_assign] = "=",
60 [pet_op_add] = "+",
61 [pet_op_sub] = "-",
62 [pet_op_mul] = "*",
63 [pet_op_div] = "/",
64 [pet_op_mod] = "%",
65 [pet_op_shl] = "<<",
66 [pet_op_shr] = ">>",
67 [pet_op_eq] = "==",
68 [pet_op_ne] = "!=",
69 [pet_op_le] = "<=",
70 [pet_op_ge] = ">=",
71 [pet_op_lt] = "<",
72 [pet_op_gt] = ">",
73 [pet_op_minus] = "-",
74 [pet_op_post_inc] = "++",
75 [pet_op_post_dec] = "--",
76 [pet_op_pre_inc] = "++",
77 [pet_op_pre_dec] = "--",
78 [pet_op_address_of] = "&",
79 [pet_op_and] = "&",
80 [pet_op_xor] = "^",
81 [pet_op_or] = "|",
82 [pet_op_not] = "~",
83 [pet_op_land] = "&&",
84 [pet_op_lor] = "||",
85 [pet_op_lnot] = "!",
86 [pet_op_cond] = "?:",
87 [pet_op_assume] = "assume",
88 [pet_op_kill] = "kill"
91 /* pet_scop with extra information that is used during parsing and printing.
93 * In particular, we keep track of conditions under which we want
94 * to skip the rest of the current loop iteration (skip[pet_skip_now])
95 * and of conditions under which we want to skip subsequent
96 * loop iterations (skip[pet_skip_later]).
98 * The conditions are represented as index expressions defined
99 * over a zero-dimensional domain. The index expression is either
100 * a boolean affine expression or an access to a variable, which
101 * is assumed to attain values zero and one. The condition holds
102 * if the variable has value one or if the affine expression
103 * has value one (typically for only part of the parameter space).
105 * A missing condition (skip[type] == NULL) means that we don't want
106 * to skip anything.
108 * Additionally, we keep track of the original input file
109 * inside pet_transform_C_source.
111 struct pet_scop_ext {
112 struct pet_scop scop;
114 isl_multi_pw_aff *skip[2];
115 FILE *input;
118 const char *pet_op_str(enum pet_op_type op)
120 return op_str[op];
123 int pet_op_is_inc_dec(enum pet_op_type op)
125 return op == pet_op_post_inc || op == pet_op_post_dec ||
126 op == pet_op_pre_inc || op == pet_op_pre_dec;
129 const char *pet_type_str(enum pet_expr_type type)
131 return type_str[type];
134 enum pet_op_type pet_str_op(const char *str)
136 int i;
138 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
139 if (!strcmp(op_str[i], str))
140 return i;
142 return -1;
145 enum pet_expr_type pet_str_type(const char *str)
147 int i;
149 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
150 if (!strcmp(type_str[i], str))
151 return i;
153 return -1;
156 /* Construct an access pet_expr from an access relation and an index expression.
157 * By default, it is considered to be a read access.
159 struct pet_expr *pet_expr_from_access_and_index( __isl_take isl_map *access,
160 __isl_take isl_multi_pw_aff *index)
162 isl_ctx *ctx = isl_map_get_ctx(access);
163 struct pet_expr *expr;
165 if (!index || !access)
166 goto error;
167 expr = isl_calloc_type(ctx, struct pet_expr);
168 if (!expr)
169 goto error;
171 expr->type = pet_expr_access;
172 expr->acc.access = access;
173 expr->acc.index = index;
174 expr->acc.read = 1;
175 expr->acc.write = 0;
177 return expr;
178 error:
179 isl_map_free(access);
180 isl_multi_pw_aff_free(index);
181 return NULL;
184 /* Construct an access pet_expr from an index expression.
185 * By default, the access is considered to be a read access.
187 struct pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
189 isl_map *access;
191 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
192 return pet_expr_from_access_and_index(access, index);
195 /* Extend the range of "access" with "n" dimensions, retaining
196 * the tuple identifier on this range.
198 * If "access" represents a member access, then extend the range
199 * of the member.
201 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
203 isl_id *id;
205 id = isl_map_get_tuple_id(access, isl_dim_out);
207 if (!isl_map_range_is_wrapping(access)) {
208 access = isl_map_add_dims(access, isl_dim_out, n);
209 } else {
210 isl_map *domain;
212 domain = isl_map_copy(access);
213 domain = isl_map_range_factor_domain(domain);
214 access = isl_map_range_factor_range(access);
215 access = extend_range(access, n);
216 access = isl_map_range_product(domain, access);
219 access = isl_map_set_tuple_id(access, isl_dim_out, id);
221 return access;
224 /* Construct an access pet_expr from an index expression and
225 * the depth of the accessed array.
226 * By default, the access is considered to be a read access.
228 * If the number of indices is smaller than the depth of the array,
229 * then we assume that all elements of the remaining dimensions
230 * are accessed.
232 struct pet_expr *pet_expr_from_index_and_depth(
233 __isl_take isl_multi_pw_aff *index, int depth)
235 isl_map *access;
236 int dim;
238 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
239 if (!access)
240 goto error;
241 dim = isl_map_dim(access, isl_dim_out);
242 if (dim > depth)
243 isl_die(isl_map_get_ctx(access), isl_error_internal,
244 "number of indices greater than depth",
245 access = isl_map_free(access));
246 if (dim == depth)
247 return pet_expr_from_access_and_index(access, index);
249 access = extend_range(access, depth - dim);
251 return pet_expr_from_access_and_index(access, index);
252 error:
253 isl_multi_pw_aff_free(index);
254 return NULL;
257 /* Construct a pet_expr that kills the elements specified by
258 * the index expression "index" and the access relation "access".
260 struct pet_expr *pet_expr_kill_from_access_and_index(__isl_take isl_map *access,
261 __isl_take isl_multi_pw_aff *index)
263 isl_ctx *ctx;
264 struct pet_expr *expr;
266 if (!access || !index)
267 goto error;
269 ctx = isl_multi_pw_aff_get_ctx(index);
270 expr = pet_expr_from_access_and_index(access, index);
271 if (!expr)
272 return NULL;
273 expr->acc.read = 0;
274 return pet_expr_new_unary(ctx, pet_op_kill, expr);
275 error:
276 isl_map_free(access);
277 isl_multi_pw_aff_free(index);
278 return NULL;
281 /* Construct a unary pet_expr that performs "op" on "arg".
283 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
284 struct pet_expr *arg)
286 struct pet_expr *expr;
288 if (!arg)
289 goto error;
290 expr = isl_alloc_type(ctx, struct pet_expr);
291 if (!expr)
292 goto error;
294 expr->type = pet_expr_op;
295 expr->op = op;
296 expr->n_arg = 1;
297 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
298 if (!expr->args)
299 goto error;
300 expr->args[pet_un_arg] = arg;
302 return expr;
303 error:
304 pet_expr_free(arg);
305 return NULL;
308 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
310 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
311 struct pet_expr *lhs, struct pet_expr *rhs)
313 struct pet_expr *expr;
315 if (!lhs || !rhs)
316 goto error;
317 expr = isl_alloc_type(ctx, struct pet_expr);
318 if (!expr)
319 goto error;
321 expr->type = pet_expr_op;
322 expr->op = op;
323 expr->n_arg = 2;
324 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
325 if (!expr->args)
326 goto error;
327 expr->args[pet_bin_lhs] = lhs;
328 expr->args[pet_bin_rhs] = rhs;
330 return expr;
331 error:
332 pet_expr_free(lhs);
333 pet_expr_free(rhs);
334 return NULL;
337 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
339 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
340 struct pet_expr *lhs, struct pet_expr *rhs)
342 struct pet_expr *expr;
344 if (!cond || !lhs || !rhs)
345 goto error;
346 expr = isl_alloc_type(ctx, struct pet_expr);
347 if (!expr)
348 goto error;
350 expr->type = pet_expr_op;
351 expr->op = pet_op_cond;
352 expr->n_arg = 3;
353 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
354 if (!expr->args)
355 goto error;
356 expr->args[pet_ter_cond] = cond;
357 expr->args[pet_ter_true] = lhs;
358 expr->args[pet_ter_false] = rhs;
360 return expr;
361 error:
362 pet_expr_free(cond);
363 pet_expr_free(lhs);
364 pet_expr_free(rhs);
365 return NULL;
368 /* Construct a call pet_expr that calls function "name" with "n_arg"
369 * arguments. The caller is responsible for filling in the arguments.
371 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
372 unsigned n_arg)
374 struct pet_expr *expr;
376 expr = isl_alloc_type(ctx, struct pet_expr);
377 if (!expr)
378 return NULL;
380 expr->type = pet_expr_call;
381 expr->n_arg = n_arg;
382 expr->name = strdup(name);
383 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
384 if (!expr->name || !expr->args)
385 return pet_expr_free(expr);
387 return expr;
390 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
392 struct pet_expr *pet_expr_new_cast(isl_ctx *ctx, const char *type_name,
393 struct pet_expr *arg)
395 struct pet_expr *expr;
397 if (!arg)
398 return NULL;
400 expr = isl_alloc_type(ctx, struct pet_expr);
401 if (!expr)
402 goto error;
404 expr->type = pet_expr_cast;
405 expr->n_arg = 1;
406 expr->type_name = strdup(type_name);
407 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
408 if (!expr->type_name || !expr->args)
409 goto error;
411 expr->args[0] = arg;
413 return expr;
414 error:
415 pet_expr_free(arg);
416 pet_expr_free(expr);
417 return NULL;
420 /* Construct a pet_expr that represents the double "d".
422 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
424 struct pet_expr *expr;
426 expr = isl_calloc_type(ctx, struct pet_expr);
427 if (!expr)
428 return NULL;
430 expr->type = pet_expr_double;
431 expr->d.val = val;
432 expr->d.s = strdup(s);
433 if (!expr->d.s)
434 return pet_expr_free(expr);
436 return expr;
439 /* Construct a pet_expr that represents the integer value "v".
441 struct pet_expr *pet_expr_new_int(__isl_take isl_val *v)
443 isl_ctx *ctx;
444 struct pet_expr *expr;
446 if (!v)
447 return NULL;
449 ctx = isl_val_get_ctx(v);
450 expr = isl_calloc_type(ctx, struct pet_expr);
451 if (!expr)
452 goto error;
454 expr->type = pet_expr_int;
455 expr->i = v;
457 return expr;
458 error:
459 isl_val_free(v);
460 return NULL;
463 struct pet_expr *pet_expr_free(struct pet_expr *expr)
465 int i;
467 if (!expr)
468 return NULL;
470 for (i = 0; i < expr->n_arg; ++i)
471 pet_expr_free(expr->args[i]);
472 free(expr->args);
474 switch (expr->type) {
475 case pet_expr_access:
476 isl_id_free(expr->acc.ref_id);
477 isl_map_free(expr->acc.access);
478 isl_multi_pw_aff_free(expr->acc.index);
479 break;
480 case pet_expr_call:
481 free(expr->name);
482 break;
483 case pet_expr_cast:
484 free(expr->type_name);
485 break;
486 case pet_expr_double:
487 free(expr->d.s);
488 break;
489 case pet_expr_int:
490 isl_val_free(expr->i);
491 break;
492 case pet_expr_op:
493 break;
496 free(expr);
497 return NULL;
500 static void expr_dump(struct pet_expr *expr, int indent)
502 int i;
504 if (!expr)
505 return;
507 fprintf(stderr, "%*s", indent, "");
509 switch (expr->type) {
510 case pet_expr_double:
511 fprintf(stderr, "%s\n", expr->d.s);
512 break;
513 case pet_expr_int:
514 isl_val_dump(expr->i);
515 break;
516 case pet_expr_access:
517 if (expr->acc.ref_id) {
518 isl_id_dump(expr->acc.ref_id);
519 fprintf(stderr, "%*s", indent, "");
521 isl_map_dump(expr->acc.access);
522 fprintf(stderr, "%*s", indent, "");
523 isl_multi_pw_aff_dump(expr->acc.index);
524 fprintf(stderr, "%*sread: %d\n", indent + 2,
525 "", expr->acc.read);
526 fprintf(stderr, "%*swrite: %d\n", indent + 2,
527 "", expr->acc.write);
528 for (i = 0; i < expr->n_arg; ++i)
529 expr_dump(expr->args[i], indent + 2);
530 break;
531 case pet_expr_op:
532 fprintf(stderr, "%s\n", op_str[expr->op]);
533 for (i = 0; i < expr->n_arg; ++i)
534 expr_dump(expr->args[i], indent + 2);
535 break;
536 case pet_expr_call:
537 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
538 for (i = 0; i < expr->n_arg; ++i)
539 expr_dump(expr->args[i], indent + 2);
540 break;
541 case pet_expr_cast:
542 fprintf(stderr, "(%s)\n", expr->type_name);
543 for (i = 0; i < expr->n_arg; ++i)
544 expr_dump(expr->args[i], indent + 2);
545 break;
549 void pet_expr_dump(struct pet_expr *expr)
551 expr_dump(expr, 0);
554 /* Does "expr" represent an access to an unnamed space, i.e.,
555 * does it represent an affine expression?
557 int pet_expr_is_affine(struct pet_expr *expr)
559 int has_id;
561 if (!expr)
562 return -1;
563 if (expr->type != pet_expr_access)
564 return 0;
566 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
567 if (has_id < 0)
568 return -1;
570 return !has_id;
573 /* Return the identifier of the array accessed by "expr".
575 * If "expr" represents a member access, then return the identifier
576 * of the outer structure array.
578 __isl_give isl_id *pet_expr_access_get_id(struct pet_expr *expr)
580 if (!expr)
581 return NULL;
582 if (expr->type != pet_expr_access)
583 return NULL;
585 if (isl_map_range_is_wrapping(expr->acc.access)) {
586 isl_space *space;
587 isl_id *id;
589 space = isl_map_get_space(expr->acc.access);
590 space = isl_space_range(space);
591 while (space && isl_space_is_wrapping(space))
592 space = isl_space_domain(isl_space_unwrap(space));
593 id = isl_space_get_tuple_id(space, isl_dim_set);
594 isl_space_free(space);
596 return id;
599 return isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
602 /* Align the parameters of expr->acc.index and expr->acc.access.
604 struct pet_expr *pet_expr_access_align_params(struct pet_expr *expr)
606 if (!expr)
607 return NULL;
608 if (expr->type != pet_expr_access)
609 return pet_expr_free(expr);
611 expr->acc.access = isl_map_align_params(expr->acc.access,
612 isl_multi_pw_aff_get_space(expr->acc.index));
613 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
614 isl_map_get_space(expr->acc.access));
615 if (!expr->acc.access || !expr->acc.index)
616 return pet_expr_free(expr);
618 return expr;
621 /* Does "expr" represent an access to a scalar, i.e., zero-dimensional array?
623 int pet_expr_is_scalar_access(struct pet_expr *expr)
625 if (!expr)
626 return -1;
627 if (expr->type != pet_expr_access)
628 return 0;
630 return isl_map_dim(expr->acc.access, isl_dim_out) == 0;
633 /* Return 1 if the two pet_exprs are equivalent.
635 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
637 int i;
639 if (!expr1 || !expr2)
640 return 0;
642 if (expr1->type != expr2->type)
643 return 0;
644 if (expr1->n_arg != expr2->n_arg)
645 return 0;
646 for (i = 0; i < expr1->n_arg; ++i)
647 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
648 return 0;
649 switch (expr1->type) {
650 case pet_expr_double:
651 if (strcmp(expr1->d.s, expr2->d.s))
652 return 0;
653 if (expr1->d.val != expr2->d.val)
654 return 0;
655 break;
656 case pet_expr_int:
657 if (!isl_val_eq(expr1->i, expr2->i))
658 return 0;
659 break;
660 case pet_expr_access:
661 if (expr1->acc.read != expr2->acc.read)
662 return 0;
663 if (expr1->acc.write != expr2->acc.write)
664 return 0;
665 if (expr1->acc.ref_id != expr2->acc.ref_id)
666 return 0;
667 if (!expr1->acc.access || !expr2->acc.access)
668 return 0;
669 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
670 return 0;
671 if (!expr1->acc.index || !expr2->acc.index)
672 return 0;
673 if (!isl_multi_pw_aff_plain_is_equal(expr1->acc.index,
674 expr2->acc.index))
675 return 0;
676 break;
677 case pet_expr_op:
678 if (expr1->op != expr2->op)
679 return 0;
680 break;
681 case pet_expr_call:
682 if (strcmp(expr1->name, expr2->name))
683 return 0;
684 break;
685 case pet_expr_cast:
686 if (strcmp(expr1->type_name, expr2->type_name))
687 return 0;
688 break;
691 return 1;
694 /* Add extra conditions on the parameters to all access relations in "expr".
696 * The conditions are not added to the index expression. Instead, they
697 * are used to try and simplify the index expression.
699 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
700 __isl_take isl_set *cond)
702 int i;
704 if (!expr)
705 goto error;
707 for (i = 0; i < expr->n_arg; ++i) {
708 expr->args[i] = pet_expr_restrict(expr->args[i],
709 isl_set_copy(cond));
710 if (!expr->args[i])
711 goto error;
714 if (expr->type == pet_expr_access) {
715 expr->acc.access = isl_map_intersect_params(expr->acc.access,
716 isl_set_copy(cond));
717 expr->acc.index = isl_multi_pw_aff_gist_params(
718 expr->acc.index, isl_set_copy(cond));
719 if (!expr->acc.access || !expr->acc.index)
720 goto error;
723 isl_set_free(cond);
724 return expr;
725 error:
726 isl_set_free(cond);
727 return pet_expr_free(expr);
730 /* Tag the access relation "access" with "id".
731 * That is, insert the id as the range of a wrapped relation
732 * in the domain of "access".
734 * If "access" is of the form
736 * D[i] -> A[a]
738 * then the result is of the form
740 * [D[i] -> id[]] -> A[a]
742 static __isl_give isl_map *tag_access(__isl_take isl_map *access,
743 __isl_take isl_id *id)
745 isl_space *space;
746 isl_map *add_tag;
748 space = isl_space_range(isl_map_get_space(access));
749 space = isl_space_from_range(space);
750 space = isl_space_set_tuple_id(space, isl_dim_in, id);
751 add_tag = isl_map_universe(space);
752 access = isl_map_domain_product(access, add_tag);
754 return access;
757 /* Modify all expressions of type pet_expr_access in "expr"
758 * by calling "fn" on them.
760 struct pet_expr *pet_expr_map_access(struct pet_expr *expr,
761 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
762 void *user)
764 int i;
766 if (!expr)
767 return NULL;
769 for (i = 0; i < expr->n_arg; ++i) {
770 expr->args[i] = pet_expr_map_access(expr->args[i], fn, user);
771 if (!expr->args[i])
772 return pet_expr_free(expr);
775 if (expr->type == pet_expr_access)
776 expr = fn(expr, user);
778 return expr;
781 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
783 * Return -1 on error (where fn return a negative value is treated as an error).
784 * Otherwise return 0.
786 int pet_expr_foreach_access_expr(struct pet_expr *expr,
787 int (*fn)(struct pet_expr *expr, void *user), void *user)
789 int i;
791 if (!expr)
792 return -1;
794 for (i = 0; i < expr->n_arg; ++i)
795 if (pet_expr_foreach_access_expr(expr->args[i], fn, user) < 0)
796 return -1;
798 if (expr->type == pet_expr_access)
799 return fn(expr, user);
801 return 0;
804 /* Modify the access relation and index expression
805 * of the given access expression
806 * based on the given iteration space transformation.
807 * In particular, precompose the access relation and index expression
808 * with the update function.
810 * If the access has any arguments then the domain of the access relation
811 * is a wrapped mapping from the iteration space to the space of
812 * argument values. We only need to change the domain of this wrapped
813 * mapping, so we extend the input transformation with an identity mapping
814 * on the space of argument values.
816 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
818 isl_multi_pw_aff *update = user;
819 isl_space *space;
821 update = isl_multi_pw_aff_copy(update);
823 space = isl_map_get_space(expr->acc.access);
824 space = isl_space_domain(space);
825 if (!isl_space_is_wrapping(space))
826 isl_space_free(space);
827 else {
828 isl_multi_pw_aff *id;
829 space = isl_space_unwrap(space);
830 space = isl_space_range(space);
831 space = isl_space_map_from_set(space);
832 id = isl_multi_pw_aff_identity(space);
833 update = isl_multi_pw_aff_product(update, id);
836 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
837 expr->acc.access,
838 isl_multi_pw_aff_copy(update));
839 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
840 expr->acc.index, update);
841 if (!expr->acc.access || !expr->acc.index)
842 return pet_expr_free(expr);
844 return expr;
847 /* Modify all access relations in "expr" by precomposing them with
848 * the given iteration space transformation.
850 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
851 __isl_take isl_multi_pw_aff *update)
853 expr = pet_expr_map_access(expr, &update_domain, update);
854 isl_multi_pw_aff_free(update);
855 return expr;
858 /* Construct a pet_stmt with given line number and statement
859 * number from a pet_expr.
860 * The initial iteration domain is the zero-dimensional universe.
861 * The name of the domain is given by "label" if it is non-NULL.
862 * Otherwise, the name is constructed as S_<id>.
863 * The domains of all access relations are modified to refer
864 * to the statement iteration domain.
866 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
867 __isl_take isl_id *label, int id, struct pet_expr *expr)
869 struct pet_stmt *stmt;
870 isl_space *dim;
871 isl_set *dom;
872 isl_map *sched;
873 isl_multi_pw_aff *add_name;
874 char name[50];
876 if (!expr)
877 goto error;
879 stmt = isl_calloc_type(ctx, struct pet_stmt);
880 if (!stmt)
881 goto error;
883 dim = isl_space_set_alloc(ctx, 0, 0);
884 if (label)
885 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
886 else {
887 snprintf(name, sizeof(name), "S_%d", id);
888 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
890 dom = isl_set_universe(isl_space_copy(dim));
891 sched = isl_map_from_domain(isl_set_copy(dom));
893 dim = isl_space_from_domain(dim);
894 add_name = isl_multi_pw_aff_zero(dim);
895 expr = expr_update_domain(expr, add_name);
897 stmt->line = line;
898 stmt->domain = dom;
899 stmt->schedule = sched;
900 stmt->body = expr;
902 if (!stmt->domain || !stmt->schedule || !stmt->body)
903 return pet_stmt_free(stmt);
905 return stmt;
906 error:
907 isl_id_free(label);
908 pet_expr_free(expr);
909 return NULL;
912 void *pet_stmt_free(struct pet_stmt *stmt)
914 int i;
916 if (!stmt)
917 return NULL;
919 isl_set_free(stmt->domain);
920 isl_map_free(stmt->schedule);
921 pet_expr_free(stmt->body);
923 for (i = 0; i < stmt->n_arg; ++i)
924 pet_expr_free(stmt->args[i]);
925 free(stmt->args);
927 free(stmt);
928 return NULL;
931 /* Return the iteration space of "stmt".
933 * If the statement has arguments, then stmt->domain is a wrapped map
934 * mapping the iteration domain to the values of the arguments
935 * for which this statement is executed.
936 * In this case, we need to extract the domain space of this wrapped map.
938 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt)
940 isl_space *space;
942 if (!stmt)
943 return NULL;
945 space = isl_set_get_space(stmt->domain);
946 if (isl_space_is_wrapping(space))
947 space = isl_space_domain(isl_space_unwrap(space));
949 return space;
952 static void stmt_dump(struct pet_stmt *stmt, int indent)
954 int i;
956 if (!stmt)
957 return;
959 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
960 fprintf(stderr, "%*s", indent, "");
961 isl_set_dump(stmt->domain);
962 fprintf(stderr, "%*s", indent, "");
963 isl_map_dump(stmt->schedule);
964 expr_dump(stmt->body, indent);
965 for (i = 0; i < stmt->n_arg; ++i)
966 expr_dump(stmt->args[i], indent + 2);
969 void pet_stmt_dump(struct pet_stmt *stmt)
971 stmt_dump(stmt, 0);
974 /* Allocate a new pet_type with the given "name" and "definition".
976 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
977 const char *definition)
979 struct pet_type *type;
981 type = isl_alloc_type(ctx, struct pet_type);
982 if (!type)
983 return NULL;
985 type->name = strdup(name);
986 type->definition = strdup(definition);
988 if (!type->name || !type->definition)
989 return pet_type_free(type);
991 return type;
994 /* Free "type" and return NULL.
996 struct pet_type *pet_type_free(struct pet_type *type)
998 if (!type)
999 return NULL;
1001 free(type->name);
1002 free(type->definition);
1004 free(type);
1005 return NULL;
1008 struct pet_array *pet_array_free(struct pet_array *array)
1010 if (!array)
1011 return NULL;
1013 isl_set_free(array->context);
1014 isl_set_free(array->extent);
1015 isl_set_free(array->value_bounds);
1016 free(array->element_type);
1018 free(array);
1019 return NULL;
1022 void pet_array_dump(struct pet_array *array)
1024 if (!array)
1025 return;
1027 isl_set_dump(array->context);
1028 isl_set_dump(array->extent);
1029 isl_set_dump(array->value_bounds);
1030 fprintf(stderr, "%s%s%s\n", array->element_type,
1031 array->element_is_record ? " element-is-record" : "",
1032 array->live_out ? " live-out" : "");
1035 /* Alloc a pet_scop structure, with extra room for information that
1036 * is only used during parsing.
1038 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
1040 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
1043 /* Construct a pet_scop with room for n statements.
1045 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
1047 isl_space *space;
1048 struct pet_scop *scop;
1050 scop = pet_scop_alloc(ctx);
1051 if (!scop)
1052 return NULL;
1054 space = isl_space_params_alloc(ctx, 0);
1055 scop->context = isl_set_universe(isl_space_copy(space));
1056 scop->context_value = isl_set_universe(space);
1057 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
1058 if (!scop->context || !scop->stmts)
1059 return pet_scop_free(scop);
1061 scop->n_stmt = n;
1063 return scop;
1066 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
1068 return scop_alloc(ctx, 0);
1071 /* Update "context" with respect to the valid parameter values for "access".
1073 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
1074 __isl_take isl_set *context)
1076 context = isl_set_intersect(context,
1077 isl_map_params(isl_map_copy(access)));
1078 return context;
1081 /* Update "context" with respect to the valid parameter values for "expr".
1083 * If "expr" represents a conditional operator, then a parameter value
1084 * needs to be valid for the condition and for at least one of the
1085 * remaining two arguments.
1086 * If the condition is an affine expression, then we can be a bit more specific.
1087 * The parameter then has to be valid for the second argument for
1088 * non-zero accesses and valid for the third argument for zero accesses.
1090 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
1091 __isl_take isl_set *context)
1093 int i;
1095 if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
1096 int is_aff;
1097 isl_set *context1, *context2;
1099 is_aff = pet_expr_is_affine(expr->args[0]);
1100 if (is_aff < 0)
1101 goto error;
1103 context = expr_extract_context(expr->args[0], context);
1104 context1 = expr_extract_context(expr->args[1],
1105 isl_set_copy(context));
1106 context2 = expr_extract_context(expr->args[2], context);
1108 if (is_aff) {
1109 isl_map *access;
1110 isl_set *zero_set;
1112 access = isl_map_copy(expr->args[0]->acc.access);
1113 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
1114 zero_set = isl_map_params(access);
1115 context1 = isl_set_subtract(context1,
1116 isl_set_copy(zero_set));
1117 context2 = isl_set_intersect(context2, zero_set);
1120 context = isl_set_union(context1, context2);
1121 context = isl_set_coalesce(context);
1123 return context;
1126 for (i = 0; i < expr->n_arg; ++i)
1127 context = expr_extract_context(expr->args[i], context);
1129 if (expr->type == pet_expr_access)
1130 context = access_extract_context(expr->acc.access, context);
1132 return context;
1133 error:
1134 isl_set_free(context);
1135 return NULL;
1138 /* Update "context" with respect to the valid parameter values for "stmt".
1140 * If the statement is an assume statement with an affine expression,
1141 * then intersect "context" with that expression.
1142 * Otherwise, intersect "context" with the contexts of the expressions
1143 * inside "stmt".
1145 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
1146 __isl_take isl_set *context)
1148 int i;
1150 if (pet_stmt_is_assume(stmt) &&
1151 pet_expr_is_affine(stmt->body->args[0])) {
1152 isl_multi_pw_aff *index;
1153 isl_pw_aff *pa;
1154 isl_set *cond;
1156 index = stmt->body->args[0]->acc.index;
1157 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
1158 cond = isl_set_params(isl_pw_aff_non_zero_set(pa));
1159 return isl_set_intersect(context, cond);
1162 for (i = 0; i < stmt->n_arg; ++i)
1163 context = expr_extract_context(stmt->args[i], context);
1165 context = expr_extract_context(stmt->body, context);
1167 return context;
1170 /* Construct a pet_scop that contains the given pet_stmt.
1172 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
1174 struct pet_scop *scop;
1176 if (!stmt)
1177 return NULL;
1179 scop = scop_alloc(ctx, 1);
1180 if (!scop)
1181 goto error;
1183 scop->context = stmt_extract_context(stmt, scop->context);
1184 if (!scop->context)
1185 goto error;
1187 scop->stmts[0] = stmt;
1189 return scop;
1190 error:
1191 pet_stmt_free(stmt);
1192 pet_scop_free(scop);
1193 return NULL;
1196 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
1197 * does it represent an affine expression?
1199 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
1201 int has_id;
1203 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
1204 if (has_id < 0)
1205 return -1;
1207 return !has_id;
1210 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
1212 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
1213 __isl_take isl_set *dom)
1215 isl_pw_aff *pa;
1216 pa = isl_set_indicator_function(set);
1217 pa = isl_pw_aff_intersect_domain(pa, dom);
1218 return pa;
1221 /* Return "lhs || rhs", defined on the shared definition domain.
1223 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
1224 __isl_take isl_pw_aff *rhs)
1226 isl_set *cond;
1227 isl_set *dom;
1229 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
1230 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
1231 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
1232 isl_pw_aff_non_zero_set(rhs));
1233 cond = isl_set_coalesce(cond);
1234 return indicator_function(cond, dom);
1237 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
1238 * ext may be equal to either ext1 or ext2.
1240 * The two skips that need to be combined are assumed to be affine expressions.
1242 * We need to skip in ext if we need to skip in either ext1 or ext2.
1243 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
1245 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
1246 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
1247 enum pet_skip type)
1249 isl_pw_aff *skip, *skip1, *skip2;
1251 if (!ext)
1252 return NULL;
1253 if (!ext1->skip[type] && !ext2->skip[type])
1254 return ext;
1255 if (!ext1->skip[type]) {
1256 if (ext == ext2)
1257 return ext;
1258 ext->skip[type] = ext2->skip[type];
1259 ext2->skip[type] = NULL;
1260 return ext;
1262 if (!ext2->skip[type]) {
1263 if (ext == ext1)
1264 return ext;
1265 ext->skip[type] = ext1->skip[type];
1266 ext1->skip[type] = NULL;
1267 return ext;
1270 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
1271 !multi_pw_aff_is_affine(ext2->skip[type]))
1272 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
1273 isl_error_internal, "can only combine affine skips",
1274 goto error);
1276 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
1277 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
1278 skip = pw_aff_or(skip1, skip2);
1279 isl_multi_pw_aff_free(ext1->skip[type]);
1280 ext1->skip[type] = NULL;
1281 isl_multi_pw_aff_free(ext2->skip[type]);
1282 ext2->skip[type] = NULL;
1283 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1284 if (!ext->skip[type])
1285 goto error;
1287 return ext;
1288 error:
1289 pet_scop_free(&ext->scop);
1290 return NULL;
1293 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
1294 * where type takes on the values pet_skip_now and pet_skip_later.
1295 * scop may be equal to either scop1 or scop2.
1297 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
1298 struct pet_scop *scop1, struct pet_scop *scop2)
1300 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1301 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
1302 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
1304 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
1305 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
1306 return &ext->scop;
1309 /* Update scop->start and scop->end to include the region from "start"
1310 * to "end". In particular, if scop->end == 0, then "scop" does not
1311 * have any offset information yet and we simply take the information
1312 * from "start" and "end". Otherwise, we update the fields if the
1313 * region from "start" to "end" is not already included.
1315 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
1316 unsigned start, unsigned end)
1318 if (!scop)
1319 return NULL;
1320 if (scop->end == 0) {
1321 scop->start = start;
1322 scop->end = end;
1323 } else {
1324 if (start < scop->start)
1325 scop->start = start;
1326 if (end > scop->end)
1327 scop->end = end;
1330 return scop;
1333 /* Does "implication" appear in the list of implications of "scop"?
1335 static int is_known_implication(struct pet_scop *scop,
1336 struct pet_implication *implication)
1338 int i;
1340 for (i = 0; i < scop->n_implication; ++i) {
1341 struct pet_implication *pi = scop->implications[i];
1342 int equal;
1344 if (pi->satisfied != implication->satisfied)
1345 continue;
1346 equal = isl_map_is_equal(pi->extension, implication->extension);
1347 if (equal < 0)
1348 return -1;
1349 if (equal)
1350 return 1;
1353 return 0;
1356 /* Store the concatenation of the implications of "scop1" and "scop2"
1357 * in "scop", removing duplicates (i.e., implications in "scop2" that
1358 * already appear in "scop1").
1360 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
1361 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
1363 int i, j;
1365 if (!scop)
1366 return NULL;
1368 if (scop2->n_implication == 0) {
1369 scop->n_implication = scop1->n_implication;
1370 scop->implications = scop1->implications;
1371 scop1->n_implication = 0;
1372 scop1->implications = NULL;
1373 return scop;
1376 if (scop1->n_implication == 0) {
1377 scop->n_implication = scop2->n_implication;
1378 scop->implications = scop2->implications;
1379 scop2->n_implication = 0;
1380 scop2->implications = NULL;
1381 return scop;
1384 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1385 scop1->n_implication + scop2->n_implication);
1386 if (!scop->implications)
1387 return pet_scop_free(scop);
1389 for (i = 0; i < scop1->n_implication; ++i) {
1390 scop->implications[i] = scop1->implications[i];
1391 scop1->implications[i] = NULL;
1394 scop->n_implication = scop1->n_implication;
1395 j = scop1->n_implication;
1396 for (i = 0; i < scop2->n_implication; ++i) {
1397 int known;
1399 known = is_known_implication(scop, scop2->implications[i]);
1400 if (known < 0)
1401 return pet_scop_free(scop);
1402 if (known)
1403 continue;
1404 scop->implications[j++] = scop2->implications[i];
1405 scop2->implications[i] = NULL;
1407 scop->n_implication = j;
1409 return scop;
1412 /* Combine the offset information of "scop1" and "scop2" into "scop".
1414 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
1415 struct pet_scop *scop1, struct pet_scop *scop2)
1417 if (scop1->end)
1418 scop = pet_scop_update_start_end(scop,
1419 scop1->start, scop1->end);
1420 if (scop2->end)
1421 scop = pet_scop_update_start_end(scop,
1422 scop2->start, scop2->end);
1423 return scop;
1426 /* Construct a pet_scop that contains the offset information,
1427 * arrays, statements and skip information in "scop1" and "scop2".
1429 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1430 struct pet_scop *scop2)
1432 int i;
1433 struct pet_scop *scop = NULL;
1435 if (!scop1 || !scop2)
1436 goto error;
1438 if (scop1->n_stmt == 0) {
1439 scop2 = scop_combine_skips(scop2, scop1, scop2);
1440 pet_scop_free(scop1);
1441 return scop2;
1444 if (scop2->n_stmt == 0) {
1445 scop1 = scop_combine_skips(scop1, scop1, scop2);
1446 pet_scop_free(scop2);
1447 return scop1;
1450 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1451 if (!scop)
1452 goto error;
1454 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1455 scop1->n_array + scop2->n_array);
1456 if (!scop->arrays)
1457 goto error;
1458 scop->n_array = scop1->n_array + scop2->n_array;
1460 for (i = 0; i < scop1->n_stmt; ++i) {
1461 scop->stmts[i] = scop1->stmts[i];
1462 scop1->stmts[i] = NULL;
1465 for (i = 0; i < scop2->n_stmt; ++i) {
1466 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1467 scop2->stmts[i] = NULL;
1470 for (i = 0; i < scop1->n_array; ++i) {
1471 scop->arrays[i] = scop1->arrays[i];
1472 scop1->arrays[i] = NULL;
1475 for (i = 0; i < scop2->n_array; ++i) {
1476 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1477 scop2->arrays[i] = NULL;
1480 scop = scop_collect_implications(ctx, scop, scop1, scop2);
1481 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1482 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1483 scop = scop_combine_skips(scop, scop1, scop2);
1484 scop = scop_combine_start_end(scop, scop1, scop2);
1486 pet_scop_free(scop1);
1487 pet_scop_free(scop2);
1488 return scop;
1489 error:
1490 pet_scop_free(scop1);
1491 pet_scop_free(scop2);
1492 pet_scop_free(scop);
1493 return NULL;
1496 /* Apply the skip condition "skip" to "scop".
1497 * That is, make sure "scop" is not executed when the condition holds.
1499 * If "skip" is an affine expression, we add the conditions under
1500 * which the expression is zero to the iteration domains.
1501 * Otherwise, we add a filter on the variable attaining the value zero.
1503 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1504 __isl_take isl_multi_pw_aff *skip)
1506 isl_set *zero;
1507 isl_pw_aff *pa;
1508 int is_aff;
1510 if (!scop || !skip)
1511 goto error;
1513 is_aff = multi_pw_aff_is_affine(skip);
1514 if (is_aff < 0)
1515 goto error;
1517 if (!is_aff)
1518 return pet_scop_filter(scop, skip, 0);
1520 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1521 isl_multi_pw_aff_free(skip);
1522 zero = isl_set_params(isl_pw_aff_zero_set(pa));
1523 scop = pet_scop_restrict(scop, zero);
1525 return scop;
1526 error:
1527 isl_multi_pw_aff_free(skip);
1528 return pet_scop_free(scop);
1531 /* Construct a pet_scop that contains the arrays, statements and
1532 * skip information in "scop1" and "scop2", where the two scops
1533 * are executed "in sequence". That is, breaks and continues
1534 * in scop1 have an effect on scop2.
1536 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1537 struct pet_scop *scop2)
1539 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1540 scop2 = restrict_skip(scop2,
1541 pet_scop_get_skip(scop1, pet_skip_now));
1542 return pet_scop_add(ctx, scop1, scop2);
1545 /* Construct a pet_scop that contains the arrays, statements and
1546 * skip information in "scop1" and "scop2", where the two scops
1547 * are executed "in parallel". That is, any break or continue
1548 * in scop1 has no effect on scop2.
1550 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1551 struct pet_scop *scop2)
1553 return pet_scop_add(ctx, scop1, scop2);
1556 void *pet_implication_free(struct pet_implication *implication)
1558 int i;
1560 if (!implication)
1561 return NULL;
1563 isl_map_free(implication->extension);
1565 free(implication);
1566 return NULL;
1569 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1571 int i;
1572 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1574 if (!scop)
1575 return NULL;
1576 isl_set_free(scop->context);
1577 isl_set_free(scop->context_value);
1578 if (scop->types)
1579 for (i = 0; i < scop->n_type; ++i)
1580 pet_type_free(scop->types[i]);
1581 free(scop->types);
1582 if (scop->arrays)
1583 for (i = 0; i < scop->n_array; ++i)
1584 pet_array_free(scop->arrays[i]);
1585 free(scop->arrays);
1586 if (scop->stmts)
1587 for (i = 0; i < scop->n_stmt; ++i)
1588 pet_stmt_free(scop->stmts[i]);
1589 free(scop->stmts);
1590 if (scop->implications)
1591 for (i = 0; i < scop->n_implication; ++i)
1592 pet_implication_free(scop->implications[i]);
1593 free(scop->implications);
1594 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1595 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1596 free(scop);
1597 return NULL;
1600 void pet_type_dump(struct pet_type *type)
1602 if (!type)
1603 return;
1605 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1608 void pet_implication_dump(struct pet_implication *implication)
1610 if (!implication)
1611 return;
1613 fprintf(stderr, "%d\n", implication->satisfied);
1614 isl_map_dump(implication->extension);
1617 void pet_scop_dump(struct pet_scop *scop)
1619 int i;
1620 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1622 if (!scop)
1623 return;
1625 isl_set_dump(scop->context);
1626 isl_set_dump(scop->context_value);
1627 for (i = 0; i < scop->n_type; ++i)
1628 pet_type_dump(scop->types[i]);
1629 for (i = 0; i < scop->n_array; ++i)
1630 pet_array_dump(scop->arrays[i]);
1631 for (i = 0; i < scop->n_stmt; ++i)
1632 pet_stmt_dump(scop->stmts[i]);
1633 for (i = 0; i < scop->n_implication; ++i)
1634 pet_implication_dump(scop->implications[i]);
1636 if (ext->skip[0]) {
1637 fprintf(stderr, "skip\n");
1638 isl_multi_pw_aff_dump(ext->skip[0]);
1639 isl_multi_pw_aff_dump(ext->skip[1]);
1643 /* Return 1 if the two pet_arrays are equivalent.
1645 * We don't compare element_size as this may be target dependent.
1647 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1649 if (!array1 || !array2)
1650 return 0;
1652 if (!isl_set_is_equal(array1->context, array2->context))
1653 return 0;
1654 if (!isl_set_is_equal(array1->extent, array2->extent))
1655 return 0;
1656 if (!!array1->value_bounds != !!array2->value_bounds)
1657 return 0;
1658 if (array1->value_bounds &&
1659 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1660 return 0;
1661 if (strcmp(array1->element_type, array2->element_type))
1662 return 0;
1663 if (array1->element_is_record != array2->element_is_record)
1664 return 0;
1665 if (array1->live_out != array2->live_out)
1666 return 0;
1667 if (array1->uniquely_defined != array2->uniquely_defined)
1668 return 0;
1669 if (array1->declared != array2->declared)
1670 return 0;
1671 if (array1->exposed != array2->exposed)
1672 return 0;
1674 return 1;
1677 /* Return 1 if the two pet_stmts are equivalent.
1679 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1681 int i;
1683 if (!stmt1 || !stmt2)
1684 return 0;
1686 if (stmt1->line != stmt2->line)
1687 return 0;
1688 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1689 return 0;
1690 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1691 return 0;
1692 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1693 return 0;
1694 if (stmt1->n_arg != stmt2->n_arg)
1695 return 0;
1696 for (i = 0; i < stmt1->n_arg; ++i) {
1697 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1698 return 0;
1701 return 1;
1704 /* Return 1 if the two pet_types are equivalent.
1706 * We only compare the names of the types since the exact representation
1707 * of the definition may depend on the version of clang being used.
1709 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1711 if (!type1 || !type2)
1712 return 0;
1714 if (strcmp(type1->name, type2->name))
1715 return 0;
1717 return 1;
1720 /* Return 1 if the two pet_implications are equivalent.
1722 int pet_implication_is_equal(struct pet_implication *implication1,
1723 struct pet_implication *implication2)
1725 if (!implication1 || !implication2)
1726 return 0;
1728 if (implication1->satisfied != implication2->satisfied)
1729 return 0;
1730 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1731 return 0;
1733 return 1;
1736 /* Return 1 if the two pet_scops are equivalent.
1738 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1740 int i;
1742 if (!scop1 || !scop2)
1743 return 0;
1745 if (!isl_set_is_equal(scop1->context, scop2->context))
1746 return 0;
1747 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1748 return 0;
1750 if (scop1->n_type != scop2->n_type)
1751 return 0;
1752 for (i = 0; i < scop1->n_type; ++i)
1753 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1754 return 0;
1756 if (scop1->n_array != scop2->n_array)
1757 return 0;
1758 for (i = 0; i < scop1->n_array; ++i)
1759 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1760 return 0;
1762 if (scop1->n_stmt != scop2->n_stmt)
1763 return 0;
1764 for (i = 0; i < scop1->n_stmt; ++i)
1765 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1766 return 0;
1768 if (scop1->n_implication != scop2->n_implication)
1769 return 0;
1770 for (i = 0; i < scop1->n_implication; ++i)
1771 if (!pet_implication_is_equal(scop1->implications[i],
1772 scop2->implications[i]))
1773 return 0;
1775 return 1;
1778 /* Prefix the schedule of "stmt" with an extra dimension with constant
1779 * value "pos".
1781 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1783 if (!stmt)
1784 return NULL;
1786 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1787 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1788 if (!stmt->schedule)
1789 return pet_stmt_free(stmt);
1791 return stmt;
1794 /* Prefix the schedules of all statements in "scop" with an extra
1795 * dimension with constant value "pos".
1797 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1799 int i;
1801 if (!scop)
1802 return NULL;
1804 for (i = 0; i < scop->n_stmt; ++i) {
1805 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1806 if (!scop->stmts[i])
1807 return pet_scop_free(scop);
1810 return scop;
1813 /* Given a set with a parameter at "param_pos" that refers to the
1814 * iterator, "move" the iterator to the first set dimension.
1815 * That is, essentially equate the parameter to the first set dimension
1816 * and then project it out.
1818 * The first set dimension may however refer to a virtual iterator,
1819 * while the parameter refers to the "real" iterator.
1820 * We therefore need to take into account the affine expression "iv_map", which
1821 * expresses the real iterator in terms of the virtual iterator.
1822 * In particular, we equate the set dimension to the input of the map
1823 * and the parameter to the output of the map and then project out
1824 * everything we don't need anymore.
1826 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1827 int param_pos, __isl_take isl_aff *iv_map)
1829 isl_map *map, *map2;
1830 map = isl_map_from_domain(set);
1831 map = isl_map_add_dims(map, isl_dim_out, 1);
1832 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1833 map2 = isl_map_from_aff(iv_map);
1834 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1835 map = isl_map_apply_range(map, map2);
1836 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1837 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1838 return isl_map_domain(map);
1841 /* Data used in embed_access.
1842 * extend adds an iterator to the iteration domain (through precomposition).
1843 * iv_map expresses the real iterator in terms of the virtual iterator
1844 * var_id represents the induction variable of the corresponding loop
1846 struct pet_embed_access {
1847 isl_multi_pw_aff *extend;
1848 isl_aff *iv_map;
1849 isl_id *var_id;
1852 /* Given an index expression, return an expression for the outer iterator.
1854 static __isl_give isl_aff *index_outer_iterator(
1855 __isl_take isl_multi_pw_aff *index)
1857 isl_space *space;
1858 isl_local_space *ls;
1860 space = isl_multi_pw_aff_get_domain_space(index);
1861 isl_multi_pw_aff_free(index);
1863 ls = isl_local_space_from_space(space);
1864 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1867 /* Replace an index expression that references the new (outer) iterator variable
1868 * by one that references the corresponding (real) iterator.
1870 * The input index expression is of the form
1872 * { S[i',...] -> i[] }
1874 * where i' refers to the virtual iterator.
1876 * iv_map is of the form
1878 * { [i'] -> [i] }
1880 * Return the index expression
1882 * { S[i',...] -> [i] }
1884 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1885 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1887 isl_space *space;
1888 isl_aff *aff;
1890 aff = index_outer_iterator(index);
1891 space = isl_aff_get_space(aff);
1892 iv_map = isl_aff_align_params(iv_map, space);
1893 aff = isl_aff_pullback_aff(iv_map, aff);
1895 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1898 /* Given an index expression "index" that refers to the (real) iterator
1899 * through the parameter at position "pos", plug in "iv_map", expressing
1900 * the real iterator in terms of the virtual (outer) iterator.
1902 * In particular, the index expression is of the form
1904 * [..., i, ...] -> { S[i',...] -> ... i ... }
1906 * where i refers to the real iterator and i' refers to the virtual iterator.
1908 * iv_map is of the form
1910 * { [i'] -> [i] }
1912 * Return the index expression
1914 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1917 * We first move the parameter to the input
1919 * [..., ...] -> { [i, i',...] -> ... i ... }
1921 * and construct
1923 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1925 * and then combine the two to obtain the desired result.
1927 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1928 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1930 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1931 isl_multi_aff *ma;
1933 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1934 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1935 isl_dim_param, pos, 1);
1937 space = isl_space_map_from_set(space);
1938 ma = isl_multi_aff_identity(isl_space_copy(space));
1939 iv_map = isl_aff_align_params(iv_map, space);
1940 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1941 ma = isl_multi_aff_flat_range_product(
1942 isl_multi_aff_from_aff(iv_map), ma);
1943 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1945 return index;
1948 /* Does the index expression "index" reference a virtual array, i.e.,
1949 * one with user pointer equal to NULL?
1950 * A virtual array does not have any members.
1952 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff *index)
1954 isl_id *id;
1955 int is_virtual;
1957 if (!isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1958 return 0;
1959 if (isl_multi_pw_aff_range_is_wrapping(index))
1960 return 0;
1961 id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1962 is_virtual = !isl_id_get_user(id);
1963 isl_id_free(id);
1965 return is_virtual;
1968 /* Does the access relation "access" reference a virtual array, i.e.,
1969 * one with user pointer equal to NULL?
1970 * A virtual array does not have any members.
1972 static int access_is_virtual_array(__isl_keep isl_map *access)
1974 isl_id *id;
1975 int is_virtual;
1977 if (!isl_map_has_tuple_id(access, isl_dim_out))
1978 return 0;
1979 if (isl_map_range_is_wrapping(access))
1980 return 0;
1981 id = isl_map_get_tuple_id(access, isl_dim_out);
1982 is_virtual = !isl_id_get_user(id);
1983 isl_id_free(id);
1985 return is_virtual;
1988 /* Embed the given index expression in an extra outer loop.
1989 * The domain of the index expression has already been updated.
1991 * If the access refers to the induction variable, then it is
1992 * turned into an access to the set of integers with index (and value)
1993 * equal to the induction variable.
1995 * If the accessed array is a virtual array (with user
1996 * pointer equal to NULL), as created by create_test_index,
1997 * then it is extended along with the domain of the index expression.
1999 static __isl_give isl_multi_pw_aff *embed_index_expression(
2000 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
2002 isl_id *array_id = NULL;
2003 int pos;
2005 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
2006 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
2007 if (array_id == data->var_id) {
2008 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
2009 } else if (index_is_virtual_array(index)) {
2010 isl_aff *aff;
2011 isl_multi_pw_aff *mpa;
2013 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
2014 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
2015 index = isl_multi_pw_aff_flat_range_product(mpa, index);
2016 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
2017 isl_id_copy(array_id));
2019 isl_id_free(array_id);
2021 pos = isl_multi_pw_aff_find_dim_by_id(index,
2022 isl_dim_param, data->var_id);
2023 if (pos >= 0)
2024 index = index_internalize_iv(index, pos,
2025 isl_aff_copy(data->iv_map));
2026 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
2027 isl_id_copy(data->var_id));
2029 return index;
2032 /* Embed the given access relation in an extra outer loop.
2033 * The domain of the access relation has already been updated.
2035 * If the access refers to the induction variable, then it is
2036 * turned into an access to the set of integers with index (and value)
2037 * equal to the induction variable.
2039 * If the induction variable appears in the constraints (as a parameter),
2040 * then the parameter is equated to the newly introduced iteration
2041 * domain dimension and subsequently projected out.
2043 * Similarly, if the accessed array is a virtual array (with user
2044 * pointer equal to NULL), as created by create_test_index,
2045 * then it is extended along with the domain of the access.
2047 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
2048 struct pet_embed_access *data)
2050 isl_id *array_id = NULL;
2051 int pos;
2053 if (isl_map_has_tuple_id(access, isl_dim_out))
2054 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2055 if (array_id == data->var_id || access_is_virtual_array(access)) {
2056 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2057 access = isl_map_equate(access,
2058 isl_dim_in, 0, isl_dim_out, 0);
2059 if (array_id == data->var_id)
2060 access = isl_map_apply_range(access,
2061 isl_map_from_aff(isl_aff_copy(data->iv_map)));
2062 else
2063 access = isl_map_set_tuple_id(access, isl_dim_out,
2064 isl_id_copy(array_id));
2066 isl_id_free(array_id);
2068 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
2069 if (pos >= 0) {
2070 isl_set *set = isl_map_wrap(access);
2071 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
2072 access = isl_set_unwrap(set);
2074 access = isl_map_set_dim_id(access, isl_dim_in, 0,
2075 isl_id_copy(data->var_id));
2077 return access;
2080 /* Given an access expression, embed the associated access relation and
2081 * index expression in an extra outer loop.
2083 * We first update the domains to insert the extra dimension and
2084 * then update the access relation and index expression to take
2085 * into account the mapping "iv_map" from virtual iterator
2086 * to real iterator.
2088 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
2090 struct pet_embed_access *data = user;
2092 expr = update_domain(expr, data->extend);
2093 if (!expr)
2094 return NULL;
2096 expr->acc.access = embed_access_relation(expr->acc.access, data);
2097 expr->acc.index = embed_index_expression(expr->acc.index, data);
2098 if (!expr->acc.access || !expr->acc.index)
2099 return pet_expr_free(expr);
2101 return expr;
2104 /* Embed all access subexpressions of "expr" in an extra loop.
2105 * "extend" inserts an outer loop iterator in the iteration domains
2106 * (through precomposition).
2107 * "iv_map" expresses the real iterator in terms of the virtual iterator
2108 * "var_id" represents the induction variable.
2110 static struct pet_expr *expr_embed(struct pet_expr *expr,
2111 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
2112 __isl_keep isl_id *var_id)
2114 struct pet_embed_access data =
2115 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
2117 expr = pet_expr_map_access(expr, &embed_access, &data);
2118 isl_aff_free(iv_map);
2119 isl_multi_pw_aff_free(extend);
2120 return expr;
2123 /* Embed the given pet_stmt in an extra outer loop with iteration domain
2124 * "dom" and schedule "sched". "var_id" represents the induction variable
2125 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
2126 * That is, it expresses the iterator that some of the parameters in "stmt"
2127 * may refer to in terms of the iterator used in "dom" and
2128 * the domain of "sched".
2130 * The iteration domain and schedule of the statement are updated
2131 * according to the iteration domain and schedule of the new loop.
2132 * If stmt->domain is a wrapped map, then the iteration domain
2133 * is the domain of this map, so we need to be careful to adjust
2134 * this domain.
2136 * If the induction variable appears in the constraints (as a parameter)
2137 * of the current iteration domain or the schedule of the statement,
2138 * then the parameter is equated to the newly introduced iteration
2139 * domain dimension and subsequently projected out.
2141 * Finally, all access relations are updated based on the extra loop.
2143 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
2144 __isl_take isl_set *dom, __isl_take isl_map *sched,
2145 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
2147 int i;
2148 int pos;
2149 isl_id *stmt_id;
2150 isl_space *dim;
2151 isl_multi_pw_aff *extend;
2153 if (!stmt)
2154 goto error;
2156 if (isl_set_is_wrapping(stmt->domain)) {
2157 isl_map *map;
2158 isl_map *ext;
2159 isl_space *ran_dim;
2161 map = isl_set_unwrap(stmt->domain);
2162 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
2163 ran_dim = isl_space_range(isl_map_get_space(map));
2164 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
2165 isl_set_universe(ran_dim));
2166 map = isl_map_flat_domain_product(ext, map);
2167 map = isl_map_set_tuple_id(map, isl_dim_in,
2168 isl_id_copy(stmt_id));
2169 dim = isl_space_domain(isl_map_get_space(map));
2170 stmt->domain = isl_map_wrap(map);
2171 } else {
2172 stmt_id = isl_set_get_tuple_id(stmt->domain);
2173 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
2174 stmt->domain);
2175 stmt->domain = isl_set_set_tuple_id(stmt->domain,
2176 isl_id_copy(stmt_id));
2177 dim = isl_set_get_space(stmt->domain);
2180 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
2181 if (pos >= 0)
2182 stmt->domain = internalize_iv(stmt->domain, pos,
2183 isl_aff_copy(iv_map));
2185 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
2186 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
2187 isl_dim_in, stmt_id);
2189 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
2190 if (pos >= 0) {
2191 isl_set *set = isl_map_wrap(stmt->schedule);
2192 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
2193 stmt->schedule = isl_set_unwrap(set);
2196 dim = isl_space_map_from_set(dim);
2197 extend = isl_multi_pw_aff_identity(dim);
2198 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
2199 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
2200 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
2201 for (i = 0; i < stmt->n_arg; ++i)
2202 stmt->args[i] = expr_embed(stmt->args[i],
2203 isl_multi_pw_aff_copy(extend),
2204 isl_aff_copy(iv_map), var_id);
2205 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
2207 isl_set_free(dom);
2208 isl_id_free(var_id);
2210 for (i = 0; i < stmt->n_arg; ++i)
2211 if (!stmt->args[i])
2212 return pet_stmt_free(stmt);
2213 if (!stmt->domain || !stmt->schedule || !stmt->body)
2214 return pet_stmt_free(stmt);
2215 return stmt;
2216 error:
2217 isl_set_free(dom);
2218 isl_map_free(sched);
2219 isl_aff_free(iv_map);
2220 isl_id_free(var_id);
2221 return NULL;
2224 /* Embed the given pet_array in an extra outer loop with iteration domain
2225 * "dom".
2226 * This embedding only has an effect on virtual arrays (those with
2227 * user pointer equal to NULL), which need to be extended along with
2228 * the iteration domain.
2230 static struct pet_array *pet_array_embed(struct pet_array *array,
2231 __isl_take isl_set *dom)
2233 isl_id *array_id = NULL;
2235 if (!array)
2236 goto error;
2238 if (isl_set_has_tuple_id(array->extent))
2239 array_id = isl_set_get_tuple_id(array->extent);
2241 if (array_id && !isl_id_get_user(array_id)) {
2242 array->extent = isl_set_flat_product(dom, array->extent);
2243 array->extent = isl_set_set_tuple_id(array->extent, array_id);
2244 if (!array->extent)
2245 return pet_array_free(array);
2246 } else {
2247 isl_set_free(dom);
2248 isl_id_free(array_id);
2251 return array;
2252 error:
2253 isl_set_free(dom);
2254 return NULL;
2257 /* Project out all unnamed parameters from "set" and return the result.
2259 static __isl_give isl_set *set_project_out_unnamed_params(
2260 __isl_take isl_set *set)
2262 int i, n;
2264 n = isl_set_dim(set, isl_dim_param);
2265 for (i = n - 1; i >= 0; --i) {
2266 if (isl_set_has_dim_name(set, isl_dim_param, i))
2267 continue;
2268 set = isl_set_project_out(set, isl_dim_param, i, 1);
2271 return set;
2274 /* Update the context with respect to an embedding into a loop
2275 * with iteration domain "dom" and induction variable "id".
2276 * "iv_map" expresses the real iterator (parameter "id") in terms
2277 * of a possibly virtual iterator (used in "dom").
2279 * If the current context is independent of "id", we don't need
2280 * to do anything.
2281 * Otherwise, a parameter value is invalid for the embedding if
2282 * any of the corresponding iterator values is invalid.
2283 * That is, a parameter value is valid only if all the corresponding
2284 * iterator values are valid.
2285 * We therefore compute the set of parameters
2287 * forall i in dom : valid (i)
2289 * or
2291 * not exists i in dom : not valid(i)
2293 * i.e.,
2295 * not exists i in dom \ valid(i)
2297 * Before we subtract valid(i) from dom, we first need to substitute
2298 * the real iterator for the virtual iterator.
2300 * If there are any unnamed parameters in "dom", then we consider
2301 * a parameter value to be valid if it is valid for any value of those
2302 * unnamed parameters. They are therefore projected out at the end.
2304 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
2305 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
2306 __isl_keep isl_id *id)
2308 int pos;
2309 isl_multi_aff *ma;
2311 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
2312 if (pos < 0)
2313 return context;
2315 context = isl_set_from_params(context);
2316 context = isl_set_add_dims(context, isl_dim_set, 1);
2317 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
2318 context = isl_set_project_out(context, isl_dim_param, pos, 1);
2319 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
2320 context = isl_set_preimage_multi_aff(context, ma);
2321 context = isl_set_subtract(isl_set_copy(dom), context);
2322 context = isl_set_params(context);
2323 context = isl_set_complement(context);
2324 context = set_project_out_unnamed_params(context);
2325 return context;
2328 /* Update the implication with respect to an embedding into a loop
2329 * with iteration domain "dom".
2331 * Since embed_access extends virtual arrays along with the domain
2332 * of the access, we need to do the same with domain and range
2333 * of the implication. Since the original implication is only valid
2334 * within a given iteration of the loop, the extended implication
2335 * maps the extra array dimension corresponding to the extra loop
2336 * to itself.
2338 static struct pet_implication *pet_implication_embed(
2339 struct pet_implication *implication, __isl_take isl_set *dom)
2341 isl_id *id;
2342 isl_map *map;
2344 if (!implication)
2345 goto error;
2347 map = isl_set_identity(dom);
2348 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
2349 map = isl_map_flat_product(map, implication->extension);
2350 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
2351 map = isl_map_set_tuple_id(map, isl_dim_out, id);
2352 implication->extension = map;
2353 if (!implication->extension)
2354 return pet_implication_free(implication);
2356 return implication;
2357 error:
2358 isl_set_free(dom);
2359 return NULL;
2362 /* Embed all statements and arrays in "scop" in an extra outer loop
2363 * with iteration domain "dom" and schedule "sched".
2364 * "id" represents the induction variable of the loop.
2365 * "iv_map" maps a possibly virtual iterator to the real iterator.
2366 * That is, it expresses the iterator that some of the parameters in "scop"
2367 * may refer to in terms of the iterator used in "dom" and
2368 * the domain of "sched".
2370 * Any skip conditions within the loop have no effect outside of the loop.
2371 * The caller is responsible for making sure skip[pet_skip_later] has been
2372 * taken into account.
2374 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
2375 __isl_take isl_aff *sched, __isl_take isl_aff *iv_map,
2376 __isl_take isl_id *id)
2378 int i;
2379 isl_map *sched_map;
2381 sched_map = isl_map_from_aff(sched);
2383 if (!scop)
2384 goto error;
2386 pet_scop_reset_skip(scop, pet_skip_now);
2387 pet_scop_reset_skip(scop, pet_skip_later);
2389 scop->context = context_embed(scop->context, dom, iv_map, id);
2390 if (!scop->context)
2391 goto error;
2393 for (i = 0; i < scop->n_stmt; ++i) {
2394 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
2395 isl_set_copy(dom), isl_map_copy(sched_map),
2396 isl_aff_copy(iv_map), isl_id_copy(id));
2397 if (!scop->stmts[i])
2398 goto error;
2401 for (i = 0; i < scop->n_array; ++i) {
2402 scop->arrays[i] = pet_array_embed(scop->arrays[i],
2403 isl_set_copy(dom));
2404 if (!scop->arrays[i])
2405 goto error;
2408 for (i = 0; i < scop->n_implication; ++i) {
2409 scop->implications[i] =
2410 pet_implication_embed(scop->implications[i],
2411 isl_set_copy(dom));
2412 if (!scop->implications[i])
2413 goto error;
2416 isl_set_free(dom);
2417 isl_map_free(sched_map);
2418 isl_aff_free(iv_map);
2419 isl_id_free(id);
2420 return scop;
2421 error:
2422 isl_set_free(dom);
2423 isl_map_free(sched_map);
2424 isl_aff_free(iv_map);
2425 isl_id_free(id);
2426 return pet_scop_free(scop);
2429 /* Add extra conditions on the parameters to the iteration domain of "stmt".
2431 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
2432 __isl_take isl_set *cond)
2434 if (!stmt)
2435 goto error;
2437 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
2439 return stmt;
2440 error:
2441 isl_set_free(cond);
2442 return pet_stmt_free(stmt);
2445 /* Add extra conditions to scop->skip[type].
2447 * The new skip condition only holds if it held before
2448 * and the condition is true. It does not hold if it did not hold
2449 * before or the condition is false.
2451 * The skip condition is assumed to be an affine expression.
2453 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
2454 enum pet_skip type, __isl_keep isl_set *cond)
2456 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2457 isl_pw_aff *skip;
2458 isl_set *dom;
2460 if (!scop)
2461 return NULL;
2462 if (!ext->skip[type])
2463 return scop;
2465 if (!multi_pw_aff_is_affine(ext->skip[type]))
2466 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
2467 isl_error_internal, "can only restrict affine skips",
2468 return pet_scop_free(scop));
2470 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2471 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
2472 cond = isl_set_copy(cond);
2473 cond = isl_set_from_params(cond);
2474 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
2475 skip = indicator_function(cond, dom);
2476 isl_multi_pw_aff_free(ext->skip[type]);
2477 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
2478 if (!ext->skip[type])
2479 return pet_scop_free(scop);
2481 return scop;
2484 /* Add extra conditions on the parameters to all iteration domains
2485 * and skip conditions.
2487 * A parameter value is valid for the result if it was valid
2488 * for the original scop and satisfies "cond" or if it does
2489 * not satisfy "cond" as in this case the scop is not executed
2490 * and the original constraints on the parameters are irrelevant.
2492 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
2493 __isl_take isl_set *cond)
2495 int i;
2497 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
2498 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
2500 if (!scop)
2501 goto error;
2503 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
2504 scop->context = isl_set_union(scop->context,
2505 isl_set_complement(isl_set_copy(cond)));
2506 scop->context = isl_set_coalesce(scop->context);
2507 scop->context = set_project_out_unnamed_params(scop->context);
2508 if (!scop->context)
2509 goto error;
2511 for (i = 0; i < scop->n_stmt; ++i) {
2512 scop->stmts[i] = stmt_restrict(scop->stmts[i],
2513 isl_set_copy(cond));
2514 if (!scop->stmts[i])
2515 goto error;
2518 isl_set_free(cond);
2519 return scop;
2520 error:
2521 isl_set_free(cond);
2522 return pet_scop_free(scop);
2525 /* Insert an argument expression corresponding to "test" in front
2526 * of the list of arguments described by *n_arg and *args.
2528 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
2529 __isl_keep isl_multi_pw_aff *test)
2531 int i;
2532 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
2534 if (!test)
2535 return -1;
2537 if (!*args) {
2538 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
2539 if (!*args)
2540 return -1;
2541 } else {
2542 struct pet_expr **ext;
2543 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
2544 if (!ext)
2545 return -1;
2546 for (i = 0; i < *n_arg; ++i)
2547 ext[1 + i] = (*args)[i];
2548 free(*args);
2549 *args = ext;
2551 (*n_arg)++;
2552 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
2553 if (!(*args)[0])
2554 return -1;
2556 return 0;
2559 /* Make the expression "expr" depend on the value of "test"
2560 * being equal to "satisfied".
2562 * If "test" is an affine expression, we simply add the conditions
2563 * on the expression having the value "satisfied" to all access relations
2564 * and index expressions.
2566 * Otherwise, we add a filter to "expr" (which is then assumed to be
2567 * an access expression) corresponding to "test" being equal to "satisfied".
2569 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
2570 __isl_take isl_multi_pw_aff *test, int satisfied)
2572 isl_id *id;
2573 isl_ctx *ctx;
2574 isl_space *space;
2575 isl_pw_multi_aff *pma;
2577 if (!expr || !test)
2578 goto error;
2580 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
2581 isl_pw_aff *pa;
2582 isl_set *cond;
2584 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
2585 isl_multi_pw_aff_free(test);
2586 if (satisfied)
2587 cond = isl_pw_aff_non_zero_set(pa);
2588 else
2589 cond = isl_pw_aff_zero_set(pa);
2590 return pet_expr_restrict(expr, isl_set_params(cond));
2593 ctx = isl_multi_pw_aff_get_ctx(test);
2594 if (expr->type != pet_expr_access)
2595 isl_die(ctx, isl_error_invalid,
2596 "can only filter access expressions", goto error);
2598 space = isl_space_domain(isl_map_get_space(expr->acc.access));
2599 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2600 pma = pet_filter_insert_pma(space, id, satisfied);
2602 expr->acc.access = isl_map_preimage_domain_pw_multi_aff(
2603 expr->acc.access,
2604 isl_pw_multi_aff_copy(pma));
2605 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
2606 expr->acc.index, pma);
2607 if (!expr->acc.access || !expr->acc.index)
2608 goto error;
2610 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
2611 goto error;
2613 isl_multi_pw_aff_free(test);
2614 return expr;
2615 error:
2616 isl_multi_pw_aff_free(test);
2617 return pet_expr_free(expr);
2620 /* Look through the applications in "scop" for any that can be
2621 * applied to the filter expressed by "map" and "satisified".
2622 * If there is any, then apply it to "map" and return the result.
2623 * Otherwise, return "map".
2624 * "id" is the identifier of the virtual array.
2626 * We only introduce at most one implication for any given virtual array,
2627 * so we can apply the implication and return as soon as we find one.
2629 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
2630 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
2632 int i;
2634 for (i = 0; i < scop->n_implication; ++i) {
2635 struct pet_implication *pi = scop->implications[i];
2636 isl_id *pi_id;
2638 if (pi->satisfied != satisfied)
2639 continue;
2640 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
2641 isl_id_free(pi_id);
2642 if (pi_id != id)
2643 continue;
2645 return isl_map_apply_range(map, isl_map_copy(pi->extension));
2648 return map;
2651 /* Is the filter expressed by "test" and "satisfied" implied
2652 * by filter "pos" on "domain", with filter "expr", taking into
2653 * account the implications of "scop"?
2655 * For filter on domain implying that expressed by "test" and "satisfied",
2656 * the filter needs to be an access to the same (virtual) array as "test" and
2657 * the filter value needs to be equal to "satisfied".
2658 * Moreover, the filter access relation, possibly extended by
2659 * the implications in "scop" needs to contain "test".
2661 static int implies_filter(struct pet_scop *scop,
2662 __isl_keep isl_map *domain, int pos, struct pet_expr *expr,
2663 __isl_keep isl_map *test, int satisfied)
2665 isl_id *test_id, *arg_id;
2666 isl_val *val;
2667 int is_int;
2668 int s;
2669 int is_subset;
2670 isl_map *implied;
2672 if (expr->type != pet_expr_access)
2673 return 0;
2674 test_id = isl_map_get_tuple_id(test, isl_dim_out);
2675 arg_id = pet_expr_access_get_id(expr);
2676 isl_id_free(arg_id);
2677 isl_id_free(test_id);
2678 if (test_id != arg_id)
2679 return 0;
2680 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
2681 is_int = isl_val_is_int(val);
2682 if (is_int)
2683 s = isl_val_get_num_si(val);
2684 isl_val_free(val);
2685 if (!val)
2686 return -1;
2687 if (!is_int)
2688 return 0;
2689 if (s != satisfied)
2690 return 0;
2692 implied = isl_map_copy(expr->acc.access);
2693 implied = apply_implications(scop, implied, test_id, satisfied);
2694 is_subset = isl_map_is_subset(test, implied);
2695 isl_map_free(implied);
2697 return is_subset;
2700 /* Is the filter expressed by "test" and "satisfied" implied
2701 * by any of the filters on the domain of "stmt", taking into
2702 * account the implications of "scop"?
2704 static int filter_implied(struct pet_scop *scop,
2705 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
2707 int i;
2708 int implied;
2709 isl_id *test_id;
2710 isl_map *domain;
2711 isl_map *test_map;
2713 if (!scop || !stmt || !test)
2714 return -1;
2715 if (scop->n_implication == 0)
2716 return 0;
2717 if (stmt->n_arg == 0)
2718 return 0;
2720 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
2721 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
2723 implied = 0;
2724 for (i = 0; i < stmt->n_arg; ++i) {
2725 implied = implies_filter(scop, domain, i, stmt->args[i],
2726 test_map, satisfied);
2727 if (implied < 0 || implied)
2728 break;
2731 isl_map_free(test_map);
2732 isl_map_free(domain);
2733 return implied;
2736 /* Make the statement "stmt" depend on the value of "test"
2737 * being equal to "satisfied" by adjusting stmt->domain.
2739 * The domain of "test" corresponds to the (zero or more) outer dimensions
2740 * of the iteration domain.
2742 * We first extend "test" to apply to the entire iteration domain and
2743 * then check if the filter that we are about to add is implied
2744 * by any of the current filters, possibly taking into account
2745 * the implications in "scop". If so, we leave "stmt" untouched and return.
2747 * Otherwise, we insert an argument corresponding to a read to "test"
2748 * from the iteration domain of "stmt" in front of the list of arguments.
2749 * We also insert a corresponding output dimension in the wrapped
2750 * map contained in stmt->domain, with value set to "satisfied".
2752 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
2753 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
2755 int i;
2756 int implied;
2757 isl_id *id;
2758 isl_ctx *ctx;
2759 isl_pw_multi_aff *pma;
2760 isl_multi_aff *add_dom;
2761 isl_space *space;
2762 isl_local_space *ls;
2763 int n_test_dom;
2765 if (!stmt || !test)
2766 goto error;
2768 space = pet_stmt_get_space(stmt);
2769 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
2770 space = isl_space_from_domain(space);
2771 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
2772 add_dom = isl_multi_aff_zero(isl_space_copy(space));
2773 ls = isl_local_space_from_space(isl_space_domain(space));
2774 for (i = 0; i < n_test_dom; ++i) {
2775 isl_aff *aff;
2776 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2777 isl_dim_set, i);
2778 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
2780 isl_local_space_free(ls);
2781 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
2783 implied = filter_implied(scop, stmt, test, satisfied);
2784 if (implied < 0)
2785 goto error;
2786 if (implied) {
2787 isl_multi_pw_aff_free(test);
2788 return stmt;
2791 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2792 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
2793 id, satisfied);
2794 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
2796 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
2797 goto error;
2799 isl_multi_pw_aff_free(test);
2800 return stmt;
2801 error:
2802 isl_multi_pw_aff_free(test);
2803 return pet_stmt_free(stmt);
2806 /* Does "scop" have a skip condition of the given "type"?
2808 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2810 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2812 if (!scop)
2813 return -1;
2814 return ext->skip[type] != NULL;
2817 /* Does "scop" have a skip condition of the given "type" that
2818 * is an affine expression?
2820 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2822 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2824 if (!scop)
2825 return -1;
2826 if (!ext->skip[type])
2827 return 0;
2828 return multi_pw_aff_is_affine(ext->skip[type]);
2831 /* Does "scop" have a skip condition of the given "type" that
2832 * is not an affine expression?
2834 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2836 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2837 int aff;
2839 if (!scop)
2840 return -1;
2841 if (!ext->skip[type])
2842 return 0;
2843 aff = multi_pw_aff_is_affine(ext->skip[type]);
2844 if (aff < 0)
2845 return -1;
2846 return !aff;
2849 /* Does "scop" have a skip condition of the given "type" that
2850 * is affine and holds on the entire domain?
2852 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2854 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2855 isl_pw_aff *pa;
2856 isl_set *set;
2857 int is_aff;
2858 int is_univ;
2860 is_aff = pet_scop_has_affine_skip(scop, type);
2861 if (is_aff < 0 || !is_aff)
2862 return is_aff;
2864 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2865 set = isl_pw_aff_non_zero_set(pa);
2866 is_univ = isl_set_plain_is_universe(set);
2867 isl_set_free(set);
2869 return is_univ;
2872 /* Replace scop->skip[type] by "skip".
2874 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2875 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2877 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2879 if (!scop || !skip)
2880 goto error;
2882 isl_multi_pw_aff_free(ext->skip[type]);
2883 ext->skip[type] = skip;
2885 return scop;
2886 error:
2887 isl_multi_pw_aff_free(skip);
2888 return pet_scop_free(scop);
2891 /* Return a copy of scop->skip[type].
2893 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2894 enum pet_skip type)
2896 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2898 if (!scop)
2899 return NULL;
2901 return isl_multi_pw_aff_copy(ext->skip[type]);
2904 /* Assuming scop->skip[type] is an affine expression,
2905 * return the constraints on the parameters for which the skip condition
2906 * holds.
2908 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2909 enum pet_skip type)
2911 isl_multi_pw_aff *skip;
2912 isl_pw_aff *pa;
2914 skip = pet_scop_get_skip(scop, type);
2915 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2916 isl_multi_pw_aff_free(skip);
2917 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2920 /* Return the identifier of the variable that is accessed by
2921 * the skip condition of the given type.
2923 * The skip condition is assumed not to be an affine condition.
2925 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2926 enum pet_skip type)
2928 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2930 if (!scop)
2931 return NULL;
2933 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2936 /* Return an access pet_expr corresponding to the skip condition
2937 * of the given type.
2939 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2940 enum pet_skip type)
2942 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2945 /* Drop the the skip condition scop->skip[type].
2947 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2949 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2951 if (!scop)
2952 return;
2954 isl_multi_pw_aff_free(ext->skip[type]);
2955 ext->skip[type] = NULL;
2958 /* Make the skip condition (if any) depend on the value of "test" being
2959 * equal to "satisfied".
2961 * We only support the case where the original skip condition is universal,
2962 * i.e., where skipping is unconditional, and where satisfied == 1.
2963 * In this case, the skip condition is changed to skip only when
2964 * "test" is equal to one.
2966 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2967 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2969 int is_univ = 0;
2971 if (!scop)
2972 return NULL;
2973 if (!pet_scop_has_skip(scop, type))
2974 return scop;
2976 if (satisfied)
2977 is_univ = pet_scop_has_universal_skip(scop, type);
2978 if (is_univ < 0)
2979 return pet_scop_free(scop);
2980 if (satisfied && is_univ) {
2981 isl_multi_pw_aff *skip;
2982 skip = isl_multi_pw_aff_copy(test);
2983 scop = pet_scop_set_skip(scop, type, skip);
2984 if (!scop)
2985 return NULL;
2986 } else {
2987 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2988 "skip expression cannot be filtered",
2989 return pet_scop_free(scop));
2992 return scop;
2995 /* Make all statements in "scop" depend on the value of "test"
2996 * being equal to "satisfied" by adjusting their domains.
2998 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2999 __isl_take isl_multi_pw_aff *test, int satisfied)
3001 int i;
3003 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
3004 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
3006 if (!scop || !test)
3007 goto error;
3009 for (i = 0; i < scop->n_stmt; ++i) {
3010 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
3011 isl_multi_pw_aff_copy(test), satisfied);
3012 if (!scop->stmts[i])
3013 goto error;
3016 isl_multi_pw_aff_free(test);
3017 return scop;
3018 error:
3019 isl_multi_pw_aff_free(test);
3020 return pet_scop_free(scop);
3023 /* Add all parameters in "expr" to "space" and return the result.
3025 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
3026 __isl_take isl_space *space)
3028 int i;
3030 if (!expr)
3031 goto error;
3032 for (i = 0; i < expr->n_arg; ++i)
3033 space = expr_collect_params(expr->args[i], space);
3035 if (expr->type == pet_expr_access)
3036 space = isl_space_align_params(space,
3037 isl_map_get_space(expr->acc.access));
3039 return space;
3040 error:
3041 pet_expr_free(expr);
3042 return isl_space_free(space);
3045 /* Add all parameters in "stmt" to "space" and return the result.
3047 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
3048 __isl_take isl_space *space)
3050 int i;
3052 if (!stmt)
3053 return isl_space_free(space);
3055 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
3056 space = isl_space_align_params(space,
3057 isl_map_get_space(stmt->schedule));
3058 for (i = 0; i < stmt->n_arg; ++i)
3059 space = expr_collect_params(stmt->args[i], space);
3060 space = expr_collect_params(stmt->body, space);
3062 return space;
3065 /* Add all parameters in "array" to "space" and return the result.
3067 static __isl_give isl_space *array_collect_params(struct pet_array *array,
3068 __isl_take isl_space *space)
3070 if (!array)
3071 return isl_space_free(space);
3073 space = isl_space_align_params(space,
3074 isl_set_get_space(array->context));
3075 space = isl_space_align_params(space, isl_set_get_space(array->extent));
3077 return space;
3080 /* Add all parameters in "scop" to "space" and return the result.
3082 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
3083 __isl_take isl_space *space)
3085 int i;
3087 if (!scop)
3088 return isl_space_free(space);
3090 for (i = 0; i < scop->n_array; ++i)
3091 space = array_collect_params(scop->arrays[i], space);
3093 for (i = 0; i < scop->n_stmt; ++i)
3094 space = stmt_collect_params(scop->stmts[i], space);
3096 return space;
3099 /* Add all parameters in "space" to the access relation and index expression
3100 * of "expr".
3102 static struct pet_expr *propagate_params(struct pet_expr *expr, void *user)
3104 isl_space *space = user;
3106 expr->acc.access = isl_map_align_params(expr->acc.access,
3107 isl_space_copy(space));
3108 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
3109 isl_space_copy(space));
3110 if (!expr->acc.access || !expr->acc.index)
3111 return pet_expr_free(expr);
3113 return expr;
3116 /* Add all parameters in "space" to all access relations and index expressions
3117 * in "expr".
3119 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
3120 __isl_take isl_space *space)
3122 expr = pet_expr_map_access(expr, &propagate_params, space);
3123 isl_space_free(space);
3124 return expr;
3127 /* Add all parameters in "space" to the domain, schedule and
3128 * all access relations in "stmt".
3130 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
3131 __isl_take isl_space *space)
3133 int i;
3135 if (!stmt)
3136 goto error;
3138 stmt->domain = isl_set_align_params(stmt->domain,
3139 isl_space_copy(space));
3140 stmt->schedule = isl_map_align_params(stmt->schedule,
3141 isl_space_copy(space));
3143 for (i = 0; i < stmt->n_arg; ++i) {
3144 stmt->args[i] = expr_propagate_params(stmt->args[i],
3145 isl_space_copy(space));
3146 if (!stmt->args[i])
3147 goto error;
3149 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(space));
3151 if (!stmt->domain || !stmt->schedule || !stmt->body)
3152 goto error;
3154 isl_space_free(space);
3155 return stmt;
3156 error:
3157 isl_space_free(space);
3158 return pet_stmt_free(stmt);
3161 /* Add all parameters in "space" to "array".
3163 static struct pet_array *array_propagate_params(struct pet_array *array,
3164 __isl_take isl_space *space)
3166 if (!array)
3167 goto error;
3169 array->context = isl_set_align_params(array->context,
3170 isl_space_copy(space));
3171 array->extent = isl_set_align_params(array->extent,
3172 isl_space_copy(space));
3173 if (array->value_bounds) {
3174 array->value_bounds = isl_set_align_params(array->value_bounds,
3175 isl_space_copy(space));
3176 if (!array->value_bounds)
3177 goto error;
3180 if (!array->context || !array->extent)
3181 goto error;
3183 isl_space_free(space);
3184 return array;
3185 error:
3186 isl_space_free(space);
3187 return pet_array_free(array);
3190 /* Add all parameters in "space" to "scop".
3192 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
3193 __isl_take isl_space *space)
3195 int i;
3197 if (!scop)
3198 goto error;
3200 for (i = 0; i < scop->n_array; ++i) {
3201 scop->arrays[i] = array_propagate_params(scop->arrays[i],
3202 isl_space_copy(space));
3203 if (!scop->arrays[i])
3204 goto error;
3207 for (i = 0; i < scop->n_stmt; ++i) {
3208 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
3209 isl_space_copy(space));
3210 if (!scop->stmts[i])
3211 goto error;
3214 isl_space_free(space);
3215 return scop;
3216 error:
3217 isl_space_free(space);
3218 return pet_scop_free(scop);
3221 /* Update all isl_sets and isl_maps in "scop" such that they all
3222 * have the same parameters.
3224 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
3226 isl_space *space;
3228 if (!scop)
3229 return NULL;
3231 space = isl_set_get_space(scop->context);
3232 space = scop_collect_params(scop, space);
3234 scop->context = isl_set_align_params(scop->context,
3235 isl_space_copy(space));
3236 scop = scop_propagate_params(scop, space);
3238 if (scop && !scop->context)
3239 return pet_scop_free(scop);
3241 return scop;
3244 /* Check if the given index expression accesses a (0D) array that corresponds
3245 * to one of the parameters in "dim". If so, replace the array access
3246 * by an access to the set of integers with as index (and value)
3247 * that parameter.
3249 static __isl_give isl_multi_pw_aff *index_detect_parameter(
3250 __isl_take isl_multi_pw_aff *index, __isl_take isl_space *space)
3252 isl_local_space *ls;
3253 isl_id *array_id = NULL;
3254 isl_aff *aff;
3255 int pos = -1;
3257 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out)) {
3258 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
3259 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3261 isl_space_free(space);
3263 if (pos < 0) {
3264 isl_id_free(array_id);
3265 return index;
3268 space = isl_multi_pw_aff_get_domain_space(index);
3269 isl_multi_pw_aff_free(index);
3271 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3272 if (pos < 0) {
3273 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
3274 space = isl_space_set_dim_id(space, isl_dim_param, 0, array_id);
3275 pos = 0;
3276 } else
3277 isl_id_free(array_id);
3279 ls = isl_local_space_from_space(space);
3280 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
3281 index = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
3283 return index;
3286 /* Check if the given access relation accesses a (0D) array that corresponds
3287 * to one of the parameters in "dim". If so, replace the array access
3288 * by an access to the set of integers with as index (and value)
3289 * that parameter.
3291 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
3292 __isl_take isl_space *dim)
3294 isl_id *array_id = NULL;
3295 int pos = -1;
3297 if (isl_map_has_tuple_id(access, isl_dim_out)) {
3298 array_id = isl_map_get_tuple_id(access, isl_dim_out);
3299 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
3301 isl_space_free(dim);
3303 if (pos < 0) {
3304 isl_id_free(array_id);
3305 return access;
3308 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
3309 if (pos < 0) {
3310 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
3311 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
3312 pos = 0;
3313 } else
3314 isl_id_free(array_id);
3316 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
3317 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
3319 return access;
3322 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3323 * in "dim" by a value equal to the corresponding parameter.
3325 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
3326 __isl_take isl_space *dim)
3328 int i;
3330 if (!expr)
3331 goto error;
3333 for (i = 0; i < expr->n_arg; ++i) {
3334 expr->args[i] =
3335 expr_detect_parameter_accesses(expr->args[i],
3336 isl_space_copy(dim));
3337 if (!expr->args[i])
3338 goto error;
3341 if (expr->type == pet_expr_access) {
3342 expr->acc.access = access_detect_parameter(expr->acc.access,
3343 isl_space_copy(dim));
3344 expr->acc.index = index_detect_parameter(expr->acc.index,
3345 isl_space_copy(dim));
3346 if (!expr->acc.access || !expr->acc.index)
3347 goto error;
3350 isl_space_free(dim);
3351 return expr;
3352 error:
3353 isl_space_free(dim);
3354 return pet_expr_free(expr);
3357 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3358 * in "dim" by a value equal to the corresponding parameter.
3360 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
3361 __isl_take isl_space *dim)
3363 if (!stmt)
3364 goto error;
3366 stmt->body = expr_detect_parameter_accesses(stmt->body,
3367 isl_space_copy(dim));
3369 if (!stmt->domain || !stmt->schedule || !stmt->body)
3370 goto error;
3372 isl_space_free(dim);
3373 return stmt;
3374 error:
3375 isl_space_free(dim);
3376 return pet_stmt_free(stmt);
3379 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3380 * in "dim" by a value equal to the corresponding parameter.
3382 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
3383 __isl_take isl_space *dim)
3385 int i;
3387 if (!scop)
3388 goto error;
3390 for (i = 0; i < scop->n_stmt; ++i) {
3391 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
3392 isl_space_copy(dim));
3393 if (!scop->stmts[i])
3394 goto error;
3397 isl_space_free(dim);
3398 return scop;
3399 error:
3400 isl_space_free(dim);
3401 return pet_scop_free(scop);
3404 /* Replace all accesses to (0D) arrays that correspond to any of
3405 * the parameters used in "scop" by a value equal
3406 * to the corresponding parameter.
3408 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
3410 isl_space *dim;
3412 if (!scop)
3413 return NULL;
3415 dim = isl_set_get_space(scop->context);
3416 dim = scop_collect_params(scop, dim);
3418 scop = scop_detect_parameter_accesses(scop, dim);
3420 return scop;
3423 /* Return the relation mapping domain iterations to all possibly
3424 * accessed data elements.
3425 * In particular, take the access relation and project out the values
3426 * of the arguments, if any.
3428 __isl_give isl_map *pet_expr_access_get_may_access(struct pet_expr *expr)
3430 isl_map *access;
3431 isl_space *space;
3432 isl_map *map;
3434 if (!expr)
3435 return NULL;
3436 if (expr->type != pet_expr_access)
3437 return NULL;
3439 access = isl_map_copy(expr->acc.access);
3440 if (expr->n_arg == 0)
3441 return access;
3443 space = isl_space_domain(isl_map_get_space(access));
3444 map = isl_map_universe(isl_space_unwrap(space));
3445 map = isl_map_domain_map(map);
3446 access = isl_map_apply_domain(access, map);
3448 return access;
3451 /* Return the relation mapping domain iterations to all possibly
3452 * accessed data elements, with its domain tagged with the reference
3453 * identifier.
3455 __isl_give isl_map *pet_expr_access_get_tagged_may_access(
3456 struct pet_expr *expr)
3458 isl_map *access;
3460 if (!expr)
3461 return NULL;
3463 access = pet_expr_access_get_may_access(expr);
3464 access = tag_access(access, isl_id_copy(expr->acc.ref_id));
3466 return access;
3469 /* Add the access relation of the access expression "expr" to "accesses" and
3470 * return the result.
3471 * The domain of the access relation is intersected with "domain".
3472 * If "tag" is set, then the access relation is tagged with
3473 * the corresponding reference identifier.
3475 static __isl_give isl_union_map *expr_collect_access(struct pet_expr *expr,
3476 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
3478 isl_map *access;
3480 access = pet_expr_access_get_may_access(expr);
3481 access = isl_map_intersect_domain(access, isl_set_copy(domain));
3482 if (tag)
3483 access = tag_access(access, isl_id_copy(expr->acc.ref_id));
3484 return isl_union_map_add_map(accesses, access);
3487 /* Add all read access relations (if "read" is set) and/or all write
3488 * access relations (if "write" is set) to "accesses" and return the result.
3489 * The domains of the access relations are intersected with "domain".
3490 * If "tag" is set, then the access relations are tagged with
3491 * the corresponding reference identifiers.
3493 * If "must" is set, then we only add the accesses that are definitely
3494 * performed. Otherwise, we add all potential accesses.
3495 * In particular, if the access has any arguments, then if "must" is
3496 * set we currently skip the access completely. If "must" is not set,
3497 * we project out the values of the access arguments.
3499 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
3500 int read, int write, int must, int tag,
3501 __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
3503 int i;
3504 isl_id *id;
3505 isl_space *dim;
3507 if (!expr)
3508 return isl_union_map_free(accesses);
3510 for (i = 0; i < expr->n_arg; ++i)
3511 accesses = expr_collect_accesses(expr->args[i],
3512 read, write, must, tag, accesses, domain);
3514 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
3515 ((read && expr->acc.read) || (write && expr->acc.write)) &&
3516 (!must || expr->n_arg == 0)) {
3517 accesses = expr_collect_access(expr, tag, accesses, domain);
3520 return accesses;
3523 /* Collect and return all read access relations (if "read" is set)
3524 * and/or all write access relations (if "write" is set) in "stmt".
3525 * If "tag" is set, then the access relations are tagged with
3526 * the corresponding reference identifiers.
3527 * If "kill" is set, then "stmt" is a kill statement and we simply
3528 * add the argument of the kill operation.
3530 * If "must" is set, then we only add the accesses that are definitely
3531 * performed. Otherwise, we add all potential accesses.
3532 * In particular, if the statement has any arguments, then if "must" is
3533 * set we currently skip the statement completely. If "must" is not set,
3534 * we project out the values of the statement arguments.
3536 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
3537 int read, int write, int kill, int must, int tag,
3538 __isl_take isl_space *dim)
3540 isl_union_map *accesses;
3541 isl_set *domain;
3543 if (!stmt)
3544 return NULL;
3546 accesses = isl_union_map_empty(dim);
3548 if (must && stmt->n_arg > 0)
3549 return accesses;
3551 domain = isl_set_copy(stmt->domain);
3552 if (isl_set_is_wrapping(domain))
3553 domain = isl_map_domain(isl_set_unwrap(domain));
3555 if (kill)
3556 accesses = expr_collect_access(stmt->body->args[0], tag,
3557 accesses, domain);
3558 else
3559 accesses = expr_collect_accesses(stmt->body, read, write,
3560 must, tag, accesses, domain);
3561 isl_set_free(domain);
3563 return accesses;
3566 /* Is "stmt" an assignment statement?
3568 int pet_stmt_is_assign(struct pet_stmt *stmt)
3570 if (!stmt)
3571 return 0;
3572 if (stmt->body->type != pet_expr_op)
3573 return 0;
3574 return stmt->body->op == pet_op_assign;
3577 /* Is "stmt" a kill statement?
3579 int pet_stmt_is_kill(struct pet_stmt *stmt)
3581 if (!stmt)
3582 return 0;
3583 if (stmt->body->type != pet_expr_op)
3584 return 0;
3585 return stmt->body->op == pet_op_kill;
3588 /* Is "stmt" an assume statement?
3590 int pet_stmt_is_assume(struct pet_stmt *stmt)
3592 if (stmt->body->type != pet_expr_op)
3593 return 0;
3594 return stmt->body->op == pet_op_assume;
3597 /* Compute a mapping from all arrays (of structs) in scop
3598 * to their innermost arrays.
3600 * In particular, for each array of a primitive type, the result
3601 * contains the identity mapping on that array.
3602 * For each array involving member accesses, the result
3603 * contains a mapping from the elements of any intermediate array of structs
3604 * to all corresponding elements of the innermost nested arrays.
3606 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
3608 int i;
3609 isl_union_map *to_inner;
3611 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
3613 for (i = 0; i < scop->n_array; ++i) {
3614 struct pet_array *array = scop->arrays[i];
3615 isl_set *set;
3616 isl_map *map, *gist;
3618 if (array->element_is_record)
3619 continue;
3621 map = isl_set_identity(isl_set_copy(array->extent));
3623 set = isl_map_domain(isl_map_copy(map));
3624 gist = isl_map_copy(map);
3625 gist = isl_map_gist_domain(gist, isl_set_copy(set));
3626 to_inner = isl_union_map_add_map(to_inner, gist);
3628 while (set && isl_set_is_wrapping(set)) {
3629 isl_id *id;
3630 isl_map *wrapped;
3632 id = isl_set_get_tuple_id(set);
3633 wrapped = isl_set_unwrap(set);
3634 wrapped = isl_map_domain_map(wrapped);
3635 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
3636 map = isl_map_apply_domain(map, wrapped);
3637 set = isl_map_domain(isl_map_copy(map));
3638 gist = isl_map_copy(map);
3639 gist = isl_map_gist_domain(gist, isl_set_copy(set));
3640 to_inner = isl_union_map_add_map(to_inner, gist);
3643 isl_set_free(set);
3644 isl_map_free(map);
3647 return to_inner;
3650 /* Collect and return all read access relations (if "read" is set)
3651 * and/or all write access relations (if "write" is set) in "scop".
3652 * If "kill" is set, then we only add the arguments of kill operations.
3653 * If "must" is set, then we only add the accesses that are definitely
3654 * performed. Otherwise, we add all potential accesses.
3655 * If "tag" is set, then the access relations are tagged with
3656 * the corresponding reference identifiers.
3657 * For accesses to structures, the returned access relation accesses
3658 * all individual fields in the structures.
3660 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
3661 int read, int write, int kill, int must, int tag)
3663 int i;
3664 isl_union_map *accesses;
3665 isl_union_set *arrays;
3666 isl_union_map *to_inner;
3668 if (!scop)
3669 return NULL;
3671 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
3673 for (i = 0; i < scop->n_stmt; ++i) {
3674 struct pet_stmt *stmt = scop->stmts[i];
3675 isl_union_map *accesses_i;
3676 isl_space *space;
3678 if (kill && !pet_stmt_is_kill(stmt))
3679 continue;
3681 space = isl_set_get_space(scop->context);
3682 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
3683 must, tag, space);
3684 accesses = isl_union_map_union(accesses, accesses_i);
3687 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
3688 for (i = 0; i < scop->n_array; ++i) {
3689 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
3690 arrays = isl_union_set_add_set(arrays, extent);
3692 accesses = isl_union_map_intersect_range(accesses, arrays);
3694 to_inner = compute_to_inner(scop);
3695 accesses = isl_union_map_apply_range(accesses, to_inner);
3697 return accesses;
3700 /* Collect all potential read access relations.
3702 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
3704 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
3707 /* Collect all potential write access relations.
3709 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
3711 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
3714 /* Collect all definite write access relations.
3716 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
3718 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
3721 /* Collect all definite kill access relations.
3723 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
3725 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
3728 /* Collect all tagged potential read access relations.
3730 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
3731 struct pet_scop *scop)
3733 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
3736 /* Collect all tagged potential write access relations.
3738 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
3739 struct pet_scop *scop)
3741 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
3744 /* Collect all tagged definite write access relations.
3746 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
3747 struct pet_scop *scop)
3749 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
3752 /* Collect all tagged definite kill access relations.
3754 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
3755 struct pet_scop *scop)
3757 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
3760 /* Collect and return the union of iteration domains in "scop".
3762 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
3764 int i;
3765 isl_set *domain_i;
3766 isl_union_set *domain;
3768 if (!scop)
3769 return NULL;
3771 domain = isl_union_set_empty(isl_set_get_space(scop->context));
3773 for (i = 0; i < scop->n_stmt; ++i) {
3774 domain_i = isl_set_copy(scop->stmts[i]->domain);
3775 domain = isl_union_set_add_set(domain, domain_i);
3778 return domain;
3781 /* Collect and return the schedules of the statements in "scop".
3782 * The range is normalized to the maximal number of scheduling
3783 * dimensions.
3785 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
3787 int i, j;
3788 isl_map *schedule_i;
3789 isl_union_map *schedule;
3790 int depth, max_depth = 0;
3792 if (!scop)
3793 return NULL;
3795 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
3797 for (i = 0; i < scop->n_stmt; ++i) {
3798 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
3799 if (depth > max_depth)
3800 max_depth = depth;
3803 for (i = 0; i < scop->n_stmt; ++i) {
3804 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
3805 depth = isl_map_dim(schedule_i, isl_dim_out);
3806 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
3807 max_depth - depth);
3808 for (j = depth; j < max_depth; ++j)
3809 schedule_i = isl_map_fix_si(schedule_i,
3810 isl_dim_out, j, 0);
3811 schedule = isl_union_map_add_map(schedule, schedule_i);
3814 return schedule;
3817 /* Does expression "expr" write to "id"?
3819 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
3821 int i;
3822 isl_id *write_id;
3824 for (i = 0; i < expr->n_arg; ++i) {
3825 int writes = expr_writes(expr->args[i], id);
3826 if (writes < 0 || writes)
3827 return writes;
3830 if (expr->type != pet_expr_access)
3831 return 0;
3832 if (!expr->acc.write)
3833 return 0;
3834 if (pet_expr_is_affine(expr))
3835 return 0;
3837 write_id = pet_expr_access_get_id(expr);
3838 isl_id_free(write_id);
3840 if (!write_id)
3841 return -1;
3843 return write_id == id;
3846 /* Does statement "stmt" write to "id"?
3848 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
3850 return expr_writes(stmt->body, id);
3853 /* Is there any write access in "scop" that accesses "id"?
3855 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
3857 int i;
3859 if (!scop)
3860 return -1;
3862 for (i = 0; i < scop->n_stmt; ++i) {
3863 int writes = stmt_writes(scop->stmts[i], id);
3864 if (writes < 0 || writes)
3865 return writes;
3868 return 0;
3871 /* Add a reference identifier to access expression "expr".
3872 * "user" points to an integer that contains the sequence number
3873 * of the next reference.
3875 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
3877 isl_ctx *ctx;
3878 char name[50];
3879 int *n_ref = user;
3881 if (!expr)
3882 return expr;
3884 ctx = isl_map_get_ctx(expr->acc.access);
3885 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
3886 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
3887 if (!expr->acc.ref_id)
3888 return pet_expr_free(expr);
3890 return expr;
3893 /* Add a reference identifier to all access expressions in "stmt".
3894 * "n_ref" points to an integer that contains the sequence number
3895 * of the next reference.
3897 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
3899 int i;
3901 if (!stmt)
3902 return NULL;
3904 for (i = 0; i < stmt->n_arg; ++i) {
3905 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3906 &access_add_ref_id, n_ref);
3907 if (!stmt->args[i])
3908 return pet_stmt_free(stmt);
3911 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
3912 if (!stmt->body)
3913 return pet_stmt_free(stmt);
3915 return stmt;
3918 /* Add a reference identifier to all access expressions in "scop".
3920 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
3922 int i;
3923 int n_ref;
3925 if (!scop)
3926 return NULL;
3928 n_ref = 0;
3929 for (i = 0; i < scop->n_stmt; ++i) {
3930 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
3931 if (!scop->stmts[i])
3932 return pet_scop_free(scop);
3935 return scop;
3938 /* Reset the user pointer on all parameter ids in "array".
3940 static struct pet_array *array_anonymize(struct pet_array *array)
3942 if (!array)
3943 return NULL;
3945 array->context = isl_set_reset_user(array->context);
3946 array->extent = isl_set_reset_user(array->extent);
3947 if (!array->context || !array->extent)
3948 return pet_array_free(array);
3950 return array;
3953 /* Reset the user pointer on all parameter and tuple ids in
3954 * the access relation and the index expressions
3955 * of the access expression "expr".
3957 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
3959 expr->acc.access = isl_map_reset_user(expr->acc.access);
3960 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
3961 if (!expr->acc.access || !expr->acc.index)
3962 return pet_expr_free(expr);
3964 return expr;
3967 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3969 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3971 int i;
3972 isl_space *space;
3973 isl_set *domain;
3975 if (!stmt)
3976 return NULL;
3978 stmt->domain = isl_set_reset_user(stmt->domain);
3979 stmt->schedule = isl_map_reset_user(stmt->schedule);
3980 if (!stmt->domain || !stmt->schedule)
3981 return pet_stmt_free(stmt);
3983 for (i = 0; i < stmt->n_arg; ++i) {
3984 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3985 &access_anonymize, NULL);
3986 if (!stmt->args[i])
3987 return pet_stmt_free(stmt);
3990 stmt->body = pet_expr_map_access(stmt->body,
3991 &access_anonymize, NULL);
3992 if (!stmt->body)
3993 return pet_stmt_free(stmt);
3995 return stmt;
3998 /* Reset the user pointer on the tuple ids and all parameter ids
3999 * in "implication".
4001 static struct pet_implication *implication_anonymize(
4002 struct pet_implication *implication)
4004 if (!implication)
4005 return NULL;
4007 implication->extension = isl_map_reset_user(implication->extension);
4008 if (!implication->extension)
4009 return pet_implication_free(implication);
4011 return implication;
4014 /* Reset the user pointer on all parameter and tuple ids in "scop".
4016 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
4018 int i;
4020 if (!scop)
4021 return NULL;
4023 scop->context = isl_set_reset_user(scop->context);
4024 scop->context_value = isl_set_reset_user(scop->context_value);
4025 if (!scop->context || !scop->context_value)
4026 return pet_scop_free(scop);
4028 for (i = 0; i < scop->n_array; ++i) {
4029 scop->arrays[i] = array_anonymize(scop->arrays[i]);
4030 if (!scop->arrays[i])
4031 return pet_scop_free(scop);
4034 for (i = 0; i < scop->n_stmt; ++i) {
4035 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
4036 if (!scop->stmts[i])
4037 return pet_scop_free(scop);
4040 for (i = 0; i < scop->n_implication; ++i) {
4041 scop->implications[i] =
4042 implication_anonymize(scop->implications[i]);
4043 if (!scop->implications[i])
4044 return pet_scop_free(scop);
4047 return scop;
4050 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
4051 * then intersect the range of "map" with the valid set of values.
4053 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
4054 struct pet_expr *arg, __isl_keep isl_union_map *value_bounds)
4056 isl_id *id;
4057 isl_map *vb;
4058 isl_space *space;
4059 isl_ctx *ctx = isl_map_get_ctx(map);
4061 id = pet_expr_access_get_id(arg);
4062 space = isl_space_alloc(ctx, 0, 0, 1);
4063 space = isl_space_set_tuple_id(space, isl_dim_in, id);
4064 vb = isl_union_map_extract_map(value_bounds, space);
4065 if (!isl_map_plain_is_empty(vb))
4066 map = isl_map_intersect_range(map, isl_map_range(vb));
4067 else
4068 isl_map_free(vb);
4070 return map;
4073 /* Given a set "domain", return a wrapped relation with the given set
4074 * as domain and a range of dimension "n_arg", where each coordinate
4075 * is either unbounded or, if the corresponding element of args is of
4076 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
4078 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
4079 unsigned n_arg, struct pet_expr **args,
4080 __isl_keep isl_union_map *value_bounds)
4082 int i;
4083 isl_map *map;
4084 isl_space *space;
4086 map = isl_map_from_domain(domain);
4087 space = isl_map_get_space(map);
4088 space = isl_space_add_dims(space, isl_dim_out, 1);
4090 for (i = 0; i < n_arg; ++i) {
4091 isl_map *map_i;
4092 struct pet_expr *arg = args[i];
4094 map_i = isl_map_universe(isl_space_copy(space));
4095 if (arg->type == pet_expr_access)
4096 map_i = access_apply_value_bounds(map_i, arg,
4097 value_bounds);
4098 map = isl_map_flat_range_product(map, map_i);
4100 isl_space_free(space);
4102 return isl_map_wrap(map);
4105 /* Data used in access_gist() callback.
4107 struct pet_access_gist_data {
4108 isl_set *domain;
4109 isl_union_map *value_bounds;
4112 /* Given an expression "expr" of type pet_expr_access, compute
4113 * the gist of the associated access relation and index expression
4114 * with respect to data->domain and the bounds on the values of the arguments
4115 * of the expression.
4117 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
4119 struct pet_access_gist_data *data = user;
4120 isl_set *domain;
4122 domain = isl_set_copy(data->domain);
4123 if (expr->n_arg > 0)
4124 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
4125 data->value_bounds);
4127 expr->acc.access = isl_map_gist_domain(expr->acc.access,
4128 isl_set_copy(domain));
4129 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
4130 if (!expr->acc.access || !expr->acc.index)
4131 return pet_expr_free(expr);
4133 return expr;
4136 /* Compute the gist of the iteration domain and all access relations
4137 * of "stmt" based on the constraints on the parameters specified by "context"
4138 * and the constraints on the values of nested accesses specified
4139 * by "value_bounds".
4141 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
4142 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
4144 int i;
4145 isl_set *domain;
4146 struct pet_access_gist_data data;
4148 if (!stmt)
4149 return NULL;
4151 data.domain = isl_set_copy(stmt->domain);
4152 data.value_bounds = value_bounds;
4153 if (stmt->n_arg > 0)
4154 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
4156 data.domain = isl_set_intersect_params(data.domain,
4157 isl_set_copy(context));
4159 for (i = 0; i < stmt->n_arg; ++i) {
4160 stmt->args[i] = pet_expr_map_access(stmt->args[i],
4161 &access_gist, &data);
4162 if (!stmt->args[i])
4163 goto error;
4166 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
4167 if (!stmt->body)
4168 goto error;
4170 isl_set_free(data.domain);
4172 domain = isl_set_universe(pet_stmt_get_space(stmt));
4173 domain = isl_set_intersect_params(domain, isl_set_copy(context));
4174 if (stmt->n_arg > 0)
4175 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
4176 value_bounds);
4177 stmt->domain = isl_set_gist(stmt->domain, domain);
4178 if (!stmt->domain)
4179 return pet_stmt_free(stmt);
4181 return stmt;
4182 error:
4183 isl_set_free(data.domain);
4184 return pet_stmt_free(stmt);
4187 /* Compute the gist of the extent of the array
4188 * based on the constraints on the parameters specified by "context".
4190 static struct pet_array *array_gist(struct pet_array *array,
4191 __isl_keep isl_set *context)
4193 if (!array)
4194 return NULL;
4196 array->extent = isl_set_gist_params(array->extent,
4197 isl_set_copy(context));
4198 if (!array->extent)
4199 return pet_array_free(array);
4201 return array;
4204 /* Compute the gist of all sets and relations in "scop"
4205 * based on the constraints on the parameters specified by "scop->context"
4206 * and the constraints on the values of nested accesses specified
4207 * by "value_bounds".
4209 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
4210 __isl_keep isl_union_map *value_bounds)
4212 int i;
4214 if (!scop)
4215 return NULL;
4217 scop->context = isl_set_coalesce(scop->context);
4218 if (!scop->context)
4219 return pet_scop_free(scop);
4221 for (i = 0; i < scop->n_array; ++i) {
4222 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
4223 if (!scop->arrays[i])
4224 return pet_scop_free(scop);
4227 for (i = 0; i < scop->n_stmt; ++i) {
4228 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
4229 value_bounds);
4230 if (!scop->stmts[i])
4231 return pet_scop_free(scop);
4234 return scop;
4237 /* Intersect the context of "scop" with "context".
4238 * To ensure that we don't introduce any unnamed parameters in
4239 * the context of "scop", we first remove the unnamed parameters
4240 * from "context".
4242 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
4243 __isl_take isl_set *context)
4245 if (!scop)
4246 goto error;
4248 context = set_project_out_unnamed_params(context);
4249 scop->context = isl_set_intersect(scop->context, context);
4250 if (!scop->context)
4251 return pet_scop_free(scop);
4253 return scop;
4254 error:
4255 isl_set_free(context);
4256 return pet_scop_free(scop);
4259 /* Drop the current context of "scop". That is, replace the context
4260 * by a universal set.
4262 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
4264 isl_space *space;
4266 if (!scop)
4267 return NULL;
4269 space = isl_set_get_space(scop->context);
4270 isl_set_free(scop->context);
4271 scop->context = isl_set_universe(space);
4272 if (!scop->context)
4273 return pet_scop_free(scop);
4275 return scop;
4278 /* Append "array" to the arrays of "scop".
4280 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
4281 struct pet_array *array)
4283 isl_ctx *ctx;
4284 struct pet_array **arrays;
4286 if (!array || !scop)
4287 goto error;
4289 ctx = isl_set_get_ctx(scop->context);
4290 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
4291 scop->n_array + 1);
4292 if (!arrays)
4293 goto error;
4294 scop->arrays = arrays;
4295 scop->arrays[scop->n_array] = array;
4296 scop->n_array++;
4298 return scop;
4299 error:
4300 pet_array_free(array);
4301 return pet_scop_free(scop);
4304 /* Create and return an implication on filter values equal to "satisfied"
4305 * with extension "map".
4307 static struct pet_implication *new_implication(__isl_take isl_map *map,
4308 int satisfied)
4310 isl_ctx *ctx;
4311 struct pet_implication *implication;
4313 if (!map)
4314 return NULL;
4315 ctx = isl_map_get_ctx(map);
4316 implication = isl_alloc_type(ctx, struct pet_implication);
4317 if (!implication)
4318 goto error;
4320 implication->extension = map;
4321 implication->satisfied = satisfied;
4323 return implication;
4324 error:
4325 isl_map_free(map);
4326 return NULL;
4329 /* Add an implication on filter values equal to "satisfied"
4330 * with extension "map" to "scop".
4332 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
4333 __isl_take isl_map *map, int satisfied)
4335 isl_ctx *ctx;
4336 struct pet_implication *implication;
4337 struct pet_implication **implications;
4339 implication = new_implication(map, satisfied);
4340 if (!scop || !implication)
4341 goto error;
4343 ctx = isl_set_get_ctx(scop->context);
4344 implications = isl_realloc_array(ctx, scop->implications,
4345 struct pet_implication *,
4346 scop->n_implication + 1);
4347 if (!implications)
4348 goto error;
4349 scop->implications = implications;
4350 scop->implications[scop->n_implication] = implication;
4351 scop->n_implication++;
4353 return scop;
4354 error:
4355 pet_implication_free(implication);
4356 return pet_scop_free(scop);
4359 /* Given an access expression, check if it is data dependent.
4360 * If so, set *found and abort the search.
4362 static int is_data_dependent(struct pet_expr *expr, void *user)
4364 int *found = user;
4366 if (expr->n_arg) {
4367 *found = 1;
4368 return -1;
4371 return 0;
4374 /* Does "scop" contain any data dependent accesses?
4376 * Check the body of each statement for such accesses.
4378 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
4380 int i;
4381 int found = 0;
4383 if (!scop)
4384 return -1;
4386 for (i = 0; i < scop->n_stmt; ++i) {
4387 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
4388 &is_data_dependent, &found);
4389 if (r < 0 && !found)
4390 return -1;
4391 if (found)
4392 return found;
4395 return found;
4398 /* Does "scop" contain and data dependent conditions?
4400 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
4402 int i;
4404 if (!scop)
4405 return -1;
4407 for (i = 0; i < scop->n_stmt; ++i)
4408 if (scop->stmts[i]->n_arg > 0)
4409 return 1;
4411 return 0;
4414 /* Keep track of the "input" file inside the (extended) "scop".
4416 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
4418 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4420 if (!scop)
4421 return NULL;
4423 ext->input = input;
4425 return scop;
4428 /* Print the original code corresponding to "scop" to printer "p".
4430 * pet_scop_print_original can only be called from
4431 * a pet_transform_C_source callback. This means that the input
4432 * file is stored in the extended scop and that the printer prints
4433 * to a file.
4435 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
4436 __isl_take isl_printer *p)
4438 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4439 FILE *output;
4441 if (!scop || !p)
4442 return isl_printer_free(p);
4444 if (!ext->input)
4445 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
4446 "no input file stored in scop",
4447 return isl_printer_free(p));
4449 output = isl_printer_get_file(p);
4450 if (!output)
4451 return isl_printer_free(p);
4453 if (copy(ext->input, output, scop->start, scop->end) < 0)
4454 return isl_printer_free(p);
4456 return p;