use isl_*_reset_user instead of *_anonymize
[pet.git] / scop.c
blobf2ab1345ad877bec5061e5c4152b1360437b6456
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2013 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 "scop.h"
40 #include "print.h"
42 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
44 static char *type_str[] = {
45 [pet_expr_access] = "access",
46 [pet_expr_call] = "call",
47 [pet_expr_cast] = "cast",
48 [pet_expr_double] = "double",
49 [pet_expr_unary] = "unary",
50 [pet_expr_binary] = "binary",
51 [pet_expr_ternary] = "ternary"
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_eq] = "==",
66 [pet_op_le] = "<=",
67 [pet_op_lt] = "<",
68 [pet_op_gt] = ">",
69 [pet_op_minus] = "-",
70 [pet_op_post_inc] = "++",
71 [pet_op_post_dec] = "--",
72 [pet_op_pre_inc] = "++",
73 [pet_op_pre_dec] = "--",
74 [pet_op_address_of] = "&",
75 [pet_op_kill] = "kill"
78 /* pet_scop with extra information that is used during parsing and printing.
80 * In particular, we keep track of conditions under which we want
81 * to skip the rest of the current loop iteration (skip[pet_skip_now])
82 * and of conditions under which we want to skip subsequent
83 * loop iterations (skip[pet_skip_later]).
85 * The conditions are represented as index expressions defined
86 * over a zero-dimensiona domain. The index expression is either
87 * a boolean affine expression or an access to a variable, which
88 * is assumed to attain values zero and one. The condition holds
89 * if the variable has value one or if the affine expression
90 * has value one (typically for only part of the parameter space).
92 * A missing condition (skip[type] == NULL) means that we don't want
93 * to skip anything.
95 * Additionally, we keep track of the original input file
96 * inside pet_transform_C_source.
98 struct pet_scop_ext {
99 struct pet_scop scop;
101 isl_multi_pw_aff *skip[2];
102 FILE *input;
105 const char *pet_op_str(enum pet_op_type op)
107 return op_str[op];
110 int pet_op_is_inc_dec(enum pet_op_type op)
112 return op == pet_op_post_inc || op == pet_op_post_dec ||
113 op == pet_op_pre_inc || op == pet_op_pre_dec;
116 const char *pet_type_str(enum pet_expr_type type)
118 return type_str[type];
121 enum pet_op_type pet_str_op(const char *str)
123 int i;
125 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
126 if (!strcmp(op_str[i], str))
127 return i;
129 return -1;
132 enum pet_expr_type pet_str_type(const char *str)
134 int i;
136 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
137 if (!strcmp(type_str[i], str))
138 return i;
140 return -1;
143 /* Construct an access pet_expr from an access relation and an index expression.
144 * By default, it is considered to be a read access.
146 struct pet_expr *pet_expr_from_access_and_index( __isl_take isl_map *access,
147 __isl_take isl_multi_pw_aff *index)
149 isl_ctx *ctx = isl_map_get_ctx(access);
150 struct pet_expr *expr;
152 if (!index || !access)
153 goto error;
154 expr = isl_calloc_type(ctx, struct pet_expr);
155 if (!expr)
156 goto error;
158 expr->type = pet_expr_access;
159 expr->acc.access = access;
160 expr->acc.index = index;
161 expr->acc.read = 1;
162 expr->acc.write = 0;
164 return expr;
165 error:
166 isl_map_free(access);
167 isl_multi_pw_aff_free(index);
168 return NULL;
171 /* Construct an access pet_expr from an index expression.
172 * By default, the access is considered to be a read access.
174 struct pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
176 isl_map *access;
178 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
179 return pet_expr_from_access_and_index(access, index);
182 /* Construct an access pet_expr from an index expression and
183 * the depth of the accessed array.
184 * By default, the access is considered to be a read access.
186 * If the number of indices is smaller than the depth of the array,
187 * then we assume that all elements of the remaining dimensions
188 * are accessed.
190 struct pet_expr *pet_expr_from_index_and_depth(
191 __isl_take isl_multi_pw_aff *index, int depth)
193 isl_id *id;
194 isl_map *access;
195 int dim;
197 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
198 if (!access)
199 goto error;
200 dim = isl_map_dim(access, isl_dim_out);
201 if (dim > depth)
202 isl_die(isl_map_get_ctx(access), isl_error_internal,
203 "number of indices greater than depth",
204 access = isl_map_free(access));
205 if (dim == depth)
206 return pet_expr_from_access_and_index(access, index);
208 id = isl_map_get_tuple_id(access, isl_dim_out);
209 access = isl_map_add_dims(access, isl_dim_out, depth - dim);
210 access = isl_map_set_tuple_id(access, isl_dim_out, id);
212 return pet_expr_from_access_and_index(access, index);
213 error:
214 isl_multi_pw_aff_free(index);
215 return NULL;
218 /* Construct a pet_expr that kills the elements specified by
219 * the index expression "index" and the access relation "access".
221 struct pet_expr *pet_expr_kill_from_access_and_index(__isl_take isl_map *access,
222 __isl_take isl_multi_pw_aff *index)
224 isl_ctx *ctx;
225 struct pet_expr *expr;
227 if (!access || !index)
228 goto error;
230 ctx = isl_multi_pw_aff_get_ctx(index);
231 expr = pet_expr_from_access_and_index(access, index);
232 if (!expr)
233 return NULL;
234 expr->acc.read = 0;
235 return pet_expr_new_unary(ctx, pet_op_kill, expr);
236 error:
237 isl_map_free(access);
238 isl_multi_pw_aff_free(index);
239 return NULL;
242 /* Construct a unary pet_expr that performs "op" on "arg".
244 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
245 struct pet_expr *arg)
247 struct pet_expr *expr;
249 if (!arg)
250 goto error;
251 expr = isl_alloc_type(ctx, struct pet_expr);
252 if (!expr)
253 goto error;
255 expr->type = pet_expr_unary;
256 expr->op = op;
257 expr->n_arg = 1;
258 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
259 if (!expr->args)
260 goto error;
261 expr->args[pet_un_arg] = arg;
263 return expr;
264 error:
265 pet_expr_free(arg);
266 return NULL;
269 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
271 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
272 struct pet_expr *lhs, struct pet_expr *rhs)
274 struct pet_expr *expr;
276 if (!lhs || !rhs)
277 goto error;
278 expr = isl_alloc_type(ctx, struct pet_expr);
279 if (!expr)
280 goto error;
282 expr->type = pet_expr_binary;
283 expr->op = op;
284 expr->n_arg = 2;
285 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
286 if (!expr->args)
287 goto error;
288 expr->args[pet_bin_lhs] = lhs;
289 expr->args[pet_bin_rhs] = rhs;
291 return expr;
292 error:
293 pet_expr_free(lhs);
294 pet_expr_free(rhs);
295 return NULL;
298 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
300 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
301 struct pet_expr *lhs, struct pet_expr *rhs)
303 struct pet_expr *expr;
305 if (!cond || !lhs || !rhs)
306 goto error;
307 expr = isl_alloc_type(ctx, struct pet_expr);
308 if (!expr)
309 goto error;
311 expr->type = pet_expr_ternary;
312 expr->n_arg = 3;
313 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
314 if (!expr->args)
315 goto error;
316 expr->args[pet_ter_cond] = cond;
317 expr->args[pet_ter_true] = lhs;
318 expr->args[pet_ter_false] = rhs;
320 return expr;
321 error:
322 pet_expr_free(cond);
323 pet_expr_free(lhs);
324 pet_expr_free(rhs);
325 return NULL;
328 /* Construct a call pet_expr that calls function "name" with "n_arg"
329 * arguments. The caller is responsible for filling in the arguments.
331 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
332 unsigned n_arg)
334 struct pet_expr *expr;
336 expr = isl_alloc_type(ctx, struct pet_expr);
337 if (!expr)
338 return NULL;
340 expr->type = pet_expr_call;
341 expr->n_arg = n_arg;
342 expr->name = strdup(name);
343 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
344 if (!expr->name || !expr->args)
345 return pet_expr_free(expr);
347 return expr;
350 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
352 struct pet_expr *pet_expr_new_cast(isl_ctx *ctx, const char *type_name,
353 struct pet_expr *arg)
355 struct pet_expr *expr;
357 if (!arg)
358 return NULL;
360 expr = isl_alloc_type(ctx, struct pet_expr);
361 if (!expr)
362 goto error;
364 expr->type = pet_expr_cast;
365 expr->n_arg = 1;
366 expr->type_name = strdup(type_name);
367 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
368 if (!expr->type_name || !expr->args)
369 goto error;
371 expr->args[0] = arg;
373 return expr;
374 error:
375 pet_expr_free(arg);
376 pet_expr_free(expr);
377 return NULL;
380 /* Construct a pet_expr that represents the double "d".
382 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
384 struct pet_expr *expr;
386 expr = isl_calloc_type(ctx, struct pet_expr);
387 if (!expr)
388 return NULL;
390 expr->type = pet_expr_double;
391 expr->d.val = val;
392 expr->d.s = strdup(s);
393 if (!expr->d.s)
394 return pet_expr_free(expr);
396 return expr;
399 struct pet_expr *pet_expr_free(struct pet_expr *expr)
401 int i;
403 if (!expr)
404 return NULL;
406 for (i = 0; i < expr->n_arg; ++i)
407 pet_expr_free(expr->args[i]);
408 free(expr->args);
410 switch (expr->type) {
411 case pet_expr_access:
412 isl_id_free(expr->acc.ref_id);
413 isl_map_free(expr->acc.access);
414 isl_multi_pw_aff_free(expr->acc.index);
415 break;
416 case pet_expr_call:
417 free(expr->name);
418 break;
419 case pet_expr_cast:
420 free(expr->type_name);
421 break;
422 case pet_expr_double:
423 free(expr->d.s);
424 break;
425 case pet_expr_unary:
426 case pet_expr_binary:
427 case pet_expr_ternary:
428 break;
431 free(expr);
432 return NULL;
435 static void expr_dump(struct pet_expr *expr, int indent)
437 int i;
439 if (!expr)
440 return;
442 fprintf(stderr, "%*s", indent, "");
444 switch (expr->type) {
445 case pet_expr_double:
446 fprintf(stderr, "%s\n", expr->d.s);
447 break;
448 case pet_expr_access:
449 isl_id_dump(expr->acc.ref_id);
450 fprintf(stderr, "%*s", indent, "");
451 isl_map_dump(expr->acc.access);
452 fprintf(stderr, "%*s", indent, "");
453 isl_multi_pw_aff_dump(expr->acc.index);
454 fprintf(stderr, "%*sread: %d\n", indent + 2,
455 "", expr->acc.read);
456 fprintf(stderr, "%*swrite: %d\n", indent + 2,
457 "", expr->acc.write);
458 for (i = 0; i < expr->n_arg; ++i)
459 expr_dump(expr->args[i], indent + 2);
460 break;
461 case pet_expr_unary:
462 fprintf(stderr, "%s\n", op_str[expr->op]);
463 expr_dump(expr->args[pet_un_arg], indent + 2);
464 break;
465 case pet_expr_binary:
466 fprintf(stderr, "%s\n", op_str[expr->op]);
467 expr_dump(expr->args[pet_bin_lhs], indent + 2);
468 expr_dump(expr->args[pet_bin_rhs], indent + 2);
469 break;
470 case pet_expr_ternary:
471 fprintf(stderr, "?:\n");
472 expr_dump(expr->args[pet_ter_cond], indent + 2);
473 expr_dump(expr->args[pet_ter_true], indent + 2);
474 expr_dump(expr->args[pet_ter_false], indent + 2);
475 break;
476 case pet_expr_call:
477 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
478 for (i = 0; i < expr->n_arg; ++i)
479 expr_dump(expr->args[i], indent + 2);
480 break;
481 case pet_expr_cast:
482 fprintf(stderr, "(%s)\n", expr->type_name);
483 for (i = 0; i < expr->n_arg; ++i)
484 expr_dump(expr->args[i], indent + 2);
485 break;
489 void pet_expr_dump(struct pet_expr *expr)
491 expr_dump(expr, 0);
494 /* Does "expr" represent an access to an unnamed space, i.e.,
495 * does it represent an affine expression?
497 int pet_expr_is_affine(struct pet_expr *expr)
499 int has_id;
501 if (!expr)
502 return -1;
503 if (expr->type != pet_expr_access)
504 return 0;
506 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
507 if (has_id < 0)
508 return -1;
510 return !has_id;
513 /* Return the identifier of the array accessed by "expr".
515 __isl_give isl_id *pet_expr_access_get_id(struct pet_expr *expr)
517 if (!expr)
518 return NULL;
519 if (expr->type != pet_expr_access)
520 return NULL;
521 return isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
524 /* Align the parameters of expr->acc.index and expr->acc.access.
526 struct pet_expr *pet_expr_access_align_params(struct pet_expr *expr)
528 if (!expr)
529 return NULL;
530 if (expr->type != pet_expr_access)
531 return pet_expr_free(expr);
533 expr->acc.access = isl_map_align_params(expr->acc.access,
534 isl_multi_pw_aff_get_space(expr->acc.index));
535 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
536 isl_map_get_space(expr->acc.access));
537 if (!expr->acc.access || !expr->acc.index)
538 return pet_expr_free(expr);
540 return expr;
543 /* Does "expr" represent an access to a scalar, i.e., zero-dimensional array?
545 int pet_expr_is_scalar_access(struct pet_expr *expr)
547 if (!expr)
548 return -1;
549 if (expr->type != pet_expr_access)
550 return 0;
552 return isl_map_dim(expr->acc.access, isl_dim_out) == 0;
555 /* Return 1 if the two pet_exprs are equivalent.
557 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
559 int i;
561 if (!expr1 || !expr2)
562 return 0;
564 if (expr1->type != expr2->type)
565 return 0;
566 if (expr1->n_arg != expr2->n_arg)
567 return 0;
568 for (i = 0; i < expr1->n_arg; ++i)
569 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
570 return 0;
571 switch (expr1->type) {
572 case pet_expr_double:
573 if (strcmp(expr1->d.s, expr2->d.s))
574 return 0;
575 if (expr1->d.val != expr2->d.val)
576 return 0;
577 break;
578 case pet_expr_access:
579 if (expr1->acc.read != expr2->acc.read)
580 return 0;
581 if (expr1->acc.write != expr2->acc.write)
582 return 0;
583 if (expr1->acc.ref_id != expr2->acc.ref_id)
584 return 0;
585 if (!expr1->acc.access || !expr2->acc.access)
586 return 0;
587 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
588 return 0;
589 if (!expr1->acc.index || !expr2->acc.index)
590 return 0;
591 if (!isl_multi_pw_aff_plain_is_equal(expr1->acc.index,
592 expr2->acc.index))
593 return 0;
594 break;
595 case pet_expr_unary:
596 case pet_expr_binary:
597 case pet_expr_ternary:
598 if (expr1->op != expr2->op)
599 return 0;
600 break;
601 case pet_expr_call:
602 if (strcmp(expr1->name, expr2->name))
603 return 0;
604 break;
605 case pet_expr_cast:
606 if (strcmp(expr1->type_name, expr2->type_name))
607 return 0;
608 break;
611 return 1;
614 /* Add extra conditions on the parameters to all access relations in "expr".
616 * The conditions are not added to the index expression. Instead, they
617 * are used to try and simplifty the index expression.
619 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
620 __isl_take isl_set *cond)
622 int i;
624 if (!expr)
625 goto error;
627 for (i = 0; i < expr->n_arg; ++i) {
628 expr->args[i] = pet_expr_restrict(expr->args[i],
629 isl_set_copy(cond));
630 if (!expr->args[i])
631 goto error;
634 if (expr->type == pet_expr_access) {
635 expr->acc.access = isl_map_intersect_params(expr->acc.access,
636 isl_set_copy(cond));
637 expr->acc.index = isl_multi_pw_aff_gist_params(
638 expr->acc.index, isl_set_copy(cond));
639 if (!expr->acc.access || !expr->acc.index)
640 goto error;
643 isl_set_free(cond);
644 return expr;
645 error:
646 isl_set_free(cond);
647 return pet_expr_free(expr);
650 /* Modify all expressions of type pet_expr_access in "expr"
651 * by calling "fn" on them.
653 struct pet_expr *pet_expr_map_access(struct pet_expr *expr,
654 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
655 void *user)
657 int i;
659 if (!expr)
660 return NULL;
662 for (i = 0; i < expr->n_arg; ++i) {
663 expr->args[i] = pet_expr_map_access(expr->args[i], fn, user);
664 if (!expr->args[i])
665 return pet_expr_free(expr);
668 if (expr->type == pet_expr_access)
669 expr = fn(expr, user);
671 return expr;
674 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
676 * Return -1 on error (where fn return a negative value is treated as an error).
677 * Otherwise return 0.
679 int pet_expr_foreach_access_expr(struct pet_expr *expr,
680 int (*fn)(struct pet_expr *expr, void *user), void *user)
682 int i;
684 if (!expr)
685 return -1;
687 for (i = 0; i < expr->n_arg; ++i)
688 if (pet_expr_foreach_access_expr(expr->args[i], fn, user) < 0)
689 return -1;
691 if (expr->type == pet_expr_access)
692 return fn(expr, user);
694 return 0;
697 /* Modify the access relation and index expression
698 * of the given access expression
699 * based on the given iteration space transformation.
700 * In particular, precompose the access relation and index expression
701 * with the update function.
703 * If the access has any arguments then the domain of the access relation
704 * is a wrapped mapping from the iteration space to the space of
705 * argument values. We only need to change the domain of this wrapped
706 * mapping, so we extend the input transformation with an identity mapping
707 * on the space of argument values.
709 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
711 isl_multi_pw_aff *update = user;
712 isl_space *space;
714 update = isl_multi_pw_aff_copy(update);
716 space = isl_map_get_space(expr->acc.access);
717 space = isl_space_domain(space);
718 if (!isl_space_is_wrapping(space))
719 isl_space_free(space);
720 else {
721 isl_multi_pw_aff *id;
722 space = isl_space_unwrap(space);
723 space = isl_space_range(space);
724 space = isl_space_map_from_set(space);
725 id = isl_multi_pw_aff_identity(space);
726 update = isl_multi_pw_aff_product(update, id);
729 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
730 expr->acc.access,
731 isl_multi_pw_aff_copy(update));
732 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
733 expr->acc.index, update);
734 if (!expr->acc.access || !expr->acc.index)
735 return pet_expr_free(expr);
737 return expr;
740 /* Modify all access relations in "expr" by precomposing them with
741 * the given iteration space transformation.
743 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
744 __isl_take isl_multi_pw_aff *update)
746 expr = pet_expr_map_access(expr, &update_domain, update);
747 isl_multi_pw_aff_free(update);
748 return expr;
751 /* Construct a pet_stmt with given line number and statement
752 * number from a pet_expr.
753 * The initial iteration domain is the zero-dimensional universe.
754 * The name of the domain is given by "label" if it is non-NULL.
755 * Otherwise, the name is constructed as S_<id>.
756 * The domains of all access relations are modified to refer
757 * to the statement iteration domain.
759 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
760 __isl_take isl_id *label, int id, struct pet_expr *expr)
762 struct pet_stmt *stmt;
763 isl_space *dim;
764 isl_set *dom;
765 isl_map *sched;
766 isl_multi_pw_aff *add_name;
767 char name[50];
769 if (!expr)
770 goto error;
772 stmt = isl_calloc_type(ctx, struct pet_stmt);
773 if (!stmt)
774 goto error;
776 dim = isl_space_set_alloc(ctx, 0, 0);
777 if (label)
778 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
779 else {
780 snprintf(name, sizeof(name), "S_%d", id);
781 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
783 dom = isl_set_universe(isl_space_copy(dim));
784 sched = isl_map_from_domain(isl_set_copy(dom));
786 dim = isl_space_from_domain(dim);
787 add_name = isl_multi_pw_aff_zero(dim);
788 expr = expr_update_domain(expr, add_name);
790 stmt->line = line;
791 stmt->domain = dom;
792 stmt->schedule = sched;
793 stmt->body = expr;
795 if (!stmt->domain || !stmt->schedule || !stmt->body)
796 return pet_stmt_free(stmt);
798 return stmt;
799 error:
800 isl_id_free(label);
801 pet_expr_free(expr);
802 return NULL;
805 void *pet_stmt_free(struct pet_stmt *stmt)
807 int i;
809 if (!stmt)
810 return NULL;
812 isl_set_free(stmt->domain);
813 isl_map_free(stmt->schedule);
814 pet_expr_free(stmt->body);
816 for (i = 0; i < stmt->n_arg; ++i)
817 pet_expr_free(stmt->args[i]);
818 free(stmt->args);
820 free(stmt);
821 return NULL;
824 static void stmt_dump(struct pet_stmt *stmt, int indent)
826 int i;
828 if (!stmt)
829 return;
831 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
832 fprintf(stderr, "%*s", indent, "");
833 isl_set_dump(stmt->domain);
834 fprintf(stderr, "%*s", indent, "");
835 isl_map_dump(stmt->schedule);
836 expr_dump(stmt->body, indent);
837 for (i = 0; i < stmt->n_arg; ++i)
838 expr_dump(stmt->args[i], indent + 2);
841 void pet_stmt_dump(struct pet_stmt *stmt)
843 stmt_dump(stmt, 0);
846 struct pet_array *pet_array_free(struct pet_array *array)
848 if (!array)
849 return NULL;
851 isl_set_free(array->context);
852 isl_set_free(array->extent);
853 isl_set_free(array->value_bounds);
854 free(array->element_type);
856 free(array);
857 return NULL;
860 void pet_array_dump(struct pet_array *array)
862 if (!array)
863 return;
865 isl_set_dump(array->context);
866 isl_set_dump(array->extent);
867 isl_set_dump(array->value_bounds);
868 fprintf(stderr, "%s %s\n", array->element_type,
869 array->live_out ? "live-out" : "");
872 /* Alloc a pet_scop structure, with extra room for information that
873 * is only used during parsing.
875 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
877 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
880 /* Construct a pet_scop with room for n statements.
882 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
884 isl_space *space;
885 struct pet_scop *scop;
887 scop = pet_scop_alloc(ctx);
888 if (!scop)
889 return NULL;
891 space = isl_space_params_alloc(ctx, 0);
892 scop->context = isl_set_universe(isl_space_copy(space));
893 scop->context_value = isl_set_universe(space);
894 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
895 if (!scop->context || !scop->stmts)
896 return pet_scop_free(scop);
898 scop->n_stmt = n;
900 return scop;
903 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
905 return scop_alloc(ctx, 0);
908 /* Update "context" with respect to the valid parameter values for "access".
910 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
911 __isl_take isl_set *context)
913 context = isl_set_intersect(context,
914 isl_map_params(isl_map_copy(access)));
915 return context;
918 /* Update "context" with respect to the valid parameter values for "expr".
920 * If "expr" represents a ternary operator, then a parameter value
921 * needs to be valid for the condition and for at least one of the
922 * remaining two arguments.
923 * If the condition is an affine expression, then we can be a bit more specific.
924 * The parameter then has to be valid for the second argument for
925 * non-zero accesses and valid for the third argument for zero accesses.
927 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
928 __isl_take isl_set *context)
930 int i;
932 if (expr->type == pet_expr_ternary) {
933 int is_aff;
934 isl_set *context1, *context2;
936 is_aff = pet_expr_is_affine(expr->args[0]);
937 if (is_aff < 0)
938 goto error;
940 context = expr_extract_context(expr->args[0], context);
941 context1 = expr_extract_context(expr->args[1],
942 isl_set_copy(context));
943 context2 = expr_extract_context(expr->args[2], context);
945 if (is_aff) {
946 isl_map *access;
947 isl_set *zero_set;
949 access = isl_map_copy(expr->args[0]->acc.access);
950 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
951 zero_set = isl_map_params(access);
952 context1 = isl_set_subtract(context1,
953 isl_set_copy(zero_set));
954 context2 = isl_set_intersect(context2, zero_set);
957 context = isl_set_union(context1, context2);
958 context = isl_set_coalesce(context);
960 return context;
963 for (i = 0; i < expr->n_arg; ++i)
964 context = expr_extract_context(expr->args[i], context);
966 if (expr->type == pet_expr_access)
967 context = access_extract_context(expr->acc.access, context);
969 return context;
970 error:
971 isl_set_free(context);
972 return NULL;
975 /* Update "context" with respect to the valid parameter values for "stmt".
977 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
978 __isl_take isl_set *context)
980 int i;
982 for (i = 0; i < stmt->n_arg; ++i)
983 context = expr_extract_context(stmt->args[i], context);
985 context = expr_extract_context(stmt->body, context);
987 return context;
990 /* Construct a pet_scop that contains the given pet_stmt.
992 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
994 struct pet_scop *scop;
996 if (!stmt)
997 return NULL;
999 scop = scop_alloc(ctx, 1);
1000 if (!scop)
1001 goto error;
1003 scop->context = stmt_extract_context(stmt, scop->context);
1004 if (!scop->context)
1005 goto error;
1007 scop->stmts[0] = stmt;
1009 return scop;
1010 error:
1011 pet_stmt_free(stmt);
1012 pet_scop_free(scop);
1013 return NULL;
1016 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
1017 * does it represent an affine expression?
1019 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
1021 int has_id;
1023 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
1024 if (has_id < 0)
1025 return -1;
1027 return !has_id;
1030 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
1032 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
1033 __isl_take isl_set *dom)
1035 isl_pw_aff *pa;
1036 pa = isl_set_indicator_function(set);
1037 pa = isl_pw_aff_intersect_domain(pa, dom);
1038 return pa;
1041 /* Return "lhs || rhs", defined on the shared definition domain.
1043 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
1044 __isl_take isl_pw_aff *rhs)
1046 isl_set *cond;
1047 isl_set *dom;
1049 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
1050 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
1051 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
1052 isl_pw_aff_non_zero_set(rhs));
1053 cond = isl_set_coalesce(cond);
1054 return indicator_function(cond, dom);
1057 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
1058 * ext may be equal to either ext1 or ext2.
1060 * The two skips that need to be combined are assumed to be affine expressions.
1062 * We need to skip in ext if we need to skip in either ext1 or ext2.
1063 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
1065 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
1066 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
1067 enum pet_skip type)
1069 isl_pw_aff *skip, *skip1, *skip2;
1071 if (!ext)
1072 return NULL;
1073 if (!ext1->skip[type] && !ext2->skip[type])
1074 return ext;
1075 if (!ext1->skip[type]) {
1076 if (ext == ext2)
1077 return ext;
1078 ext->skip[type] = ext2->skip[type];
1079 ext2->skip[type] = NULL;
1080 return ext;
1082 if (!ext2->skip[type]) {
1083 if (ext == ext1)
1084 return ext;
1085 ext->skip[type] = ext1->skip[type];
1086 ext1->skip[type] = NULL;
1087 return ext;
1090 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
1091 !multi_pw_aff_is_affine(ext2->skip[type]))
1092 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
1093 isl_error_internal, "can only combine affine skips",
1094 goto error);
1096 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
1097 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
1098 skip = pw_aff_or(skip1, skip2);
1099 isl_multi_pw_aff_free(ext1->skip[type]);
1100 ext1->skip[type] = NULL;
1101 isl_multi_pw_aff_free(ext2->skip[type]);
1102 ext2->skip[type] = NULL;
1103 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1104 if (!ext->skip[type])
1105 goto error;
1107 return ext;
1108 error:
1109 pet_scop_free(&ext->scop);
1110 return NULL;
1113 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
1114 * where type takes on the values pet_skip_now and pet_skip_later.
1115 * scop may be equal to either scop1 or scop2.
1117 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
1118 struct pet_scop *scop1, struct pet_scop *scop2)
1120 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1121 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
1122 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
1124 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
1125 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
1126 return &ext->scop;
1129 /* Update scop->start and scop->end to include the region from "start"
1130 * to "end". In particular, if scop->end == 0, then "scop" does not
1131 * have any offset information yet and we simply take the information
1132 * from "start" and "end". Otherwise, we update the fields if the
1133 * region from "start" to "end" is not already included.
1135 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
1136 unsigned start, unsigned end)
1138 if (!scop)
1139 return NULL;
1140 if (scop->end == 0) {
1141 scop->start = start;
1142 scop->end = end;
1143 } else {
1144 if (start < scop->start)
1145 scop->start = start;
1146 if (end > scop->end)
1147 scop->end = end;
1150 return scop;
1153 /* Does "implication" appear in the list of implications of "scop"?
1155 static int is_known_implication(struct pet_scop *scop,
1156 struct pet_implication *implication)
1158 int i;
1160 for (i = 0; i < scop->n_implication; ++i) {
1161 struct pet_implication *pi = scop->implications[i];
1162 int equal;
1164 if (pi->satisfied != implication->satisfied)
1165 continue;
1166 equal = isl_map_is_equal(pi->extension, implication->extension);
1167 if (equal < 0)
1168 return -1;
1169 if (equal)
1170 return 1;
1173 return 0;
1176 /* Store the concatenation of the impliciations of "scop1" and "scop2"
1177 * in "scop", removing duplicates (i.e., implications in "scop2" that
1178 * already appear in "scop1").
1180 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
1181 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
1183 int i, j;
1185 if (!scop)
1186 return NULL;
1188 if (scop2->n_implication == 0) {
1189 scop->n_implication = scop1->n_implication;
1190 scop->implications = scop1->implications;
1191 scop1->n_implication = 0;
1192 scop1->implications = NULL;
1193 return scop;
1196 if (scop1->n_implication == 0) {
1197 scop->n_implication = scop2->n_implication;
1198 scop->implications = scop2->implications;
1199 scop2->n_implication = 0;
1200 scop2->implications = NULL;
1201 return scop;
1204 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1205 scop1->n_implication + scop2->n_implication);
1206 if (!scop->implications)
1207 return pet_scop_free(scop);
1209 for (i = 0; i < scop1->n_implication; ++i) {
1210 scop->implications[i] = scop1->implications[i];
1211 scop1->implications[i] = NULL;
1214 scop->n_implication = scop1->n_implication;
1215 j = scop1->n_implication;
1216 for (i = 0; i < scop2->n_implication; ++i) {
1217 int known;
1219 known = is_known_implication(scop, scop2->implications[i]);
1220 if (known < 0)
1221 return pet_scop_free(scop);
1222 if (known)
1223 continue;
1224 scop->implications[j++] = scop2->implications[i];
1225 scop2->implications[i] = NULL;
1227 scop->n_implication = j;
1229 return scop;
1232 /* Combine the offset information of "scop1" and "scop2" into "scop".
1234 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
1235 struct pet_scop *scop1, struct pet_scop *scop2)
1237 if (scop1->end)
1238 scop = pet_scop_update_start_end(scop,
1239 scop1->start, scop1->end);
1240 if (scop2->end)
1241 scop = pet_scop_update_start_end(scop,
1242 scop2->start, scop2->end);
1243 return scop;
1246 /* Construct a pet_scop that contains the offset information,
1247 * arrays, statements and skip information in "scop1" and "scop2".
1249 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1250 struct pet_scop *scop2)
1252 int i;
1253 struct pet_scop *scop = NULL;
1255 if (!scop1 || !scop2)
1256 goto error;
1258 if (scop1->n_stmt == 0) {
1259 scop2 = scop_combine_skips(scop2, scop1, scop2);
1260 pet_scop_free(scop1);
1261 return scop2;
1264 if (scop2->n_stmt == 0) {
1265 scop1 = scop_combine_skips(scop1, scop1, scop2);
1266 pet_scop_free(scop2);
1267 return scop1;
1270 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1271 if (!scop)
1272 goto error;
1274 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1275 scop1->n_array + scop2->n_array);
1276 if (!scop->arrays)
1277 goto error;
1278 scop->n_array = scop1->n_array + scop2->n_array;
1280 for (i = 0; i < scop1->n_stmt; ++i) {
1281 scop->stmts[i] = scop1->stmts[i];
1282 scop1->stmts[i] = NULL;
1285 for (i = 0; i < scop2->n_stmt; ++i) {
1286 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1287 scop2->stmts[i] = NULL;
1290 for (i = 0; i < scop1->n_array; ++i) {
1291 scop->arrays[i] = scop1->arrays[i];
1292 scop1->arrays[i] = NULL;
1295 for (i = 0; i < scop2->n_array; ++i) {
1296 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1297 scop2->arrays[i] = NULL;
1300 scop = scop_collect_implications(ctx, scop, scop1, scop2);
1301 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1302 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1303 scop = scop_combine_skips(scop, scop1, scop2);
1304 scop = scop_combine_start_end(scop, scop1, scop2);
1306 pet_scop_free(scop1);
1307 pet_scop_free(scop2);
1308 return scop;
1309 error:
1310 pet_scop_free(scop1);
1311 pet_scop_free(scop2);
1312 pet_scop_free(scop);
1313 return NULL;
1316 /* Apply the skip condition "skip" to "scop".
1317 * That is, make sure "scop" is not executed when the condition holds.
1319 * If "skip" is an affine expression, we add the conditions under
1320 * which the expression is zero to the iteration domains.
1321 * Otherwise, we add a filter on the variable attaining the value zero.
1323 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1324 __isl_take isl_multi_pw_aff *skip)
1326 isl_set *zero;
1327 isl_pw_aff *pa;
1328 int is_aff;
1330 if (!scop || !skip)
1331 goto error;
1333 is_aff = multi_pw_aff_is_affine(skip);
1334 if (is_aff < 0)
1335 goto error;
1337 if (!is_aff)
1338 return pet_scop_filter(scop, skip, 0);
1340 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1341 isl_multi_pw_aff_free(skip);
1342 zero = isl_set_params(isl_pw_aff_zero_set(pa));
1343 scop = pet_scop_restrict(scop, zero);
1345 return scop;
1346 error:
1347 isl_multi_pw_aff_free(skip);
1348 return pet_scop_free(scop);
1351 /* Construct a pet_scop that contains the arrays, statements and
1352 * skip information in "scop1" and "scop2", where the two scops
1353 * are executed "in sequence". That is, breaks and continues
1354 * in scop1 have an effect on scop2.
1356 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1357 struct pet_scop *scop2)
1359 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1360 scop2 = restrict_skip(scop2,
1361 pet_scop_get_skip(scop1, pet_skip_now));
1362 return pet_scop_add(ctx, scop1, scop2);
1365 /* Construct a pet_scop that contains the arrays, statements and
1366 * skip information in "scop1" and "scop2", where the two scops
1367 * are executed "in parallel". That is, any break or continue
1368 * in scop1 has no effect on scop2.
1370 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1371 struct pet_scop *scop2)
1373 return pet_scop_add(ctx, scop1, scop2);
1376 void *pet_implication_free(struct pet_implication *implication)
1378 int i;
1380 if (!implication)
1381 return NULL;
1383 isl_map_free(implication->extension);
1385 free(implication);
1386 return NULL;
1389 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1391 int i;
1392 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1394 if (!scop)
1395 return NULL;
1396 isl_set_free(scop->context);
1397 isl_set_free(scop->context_value);
1398 if (scop->arrays)
1399 for (i = 0; i < scop->n_array; ++i)
1400 pet_array_free(scop->arrays[i]);
1401 free(scop->arrays);
1402 if (scop->stmts)
1403 for (i = 0; i < scop->n_stmt; ++i)
1404 pet_stmt_free(scop->stmts[i]);
1405 free(scop->stmts);
1406 if (scop->implications)
1407 for (i = 0; i < scop->n_implication; ++i)
1408 pet_implication_free(scop->implications[i]);
1409 free(scop->implications);
1410 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1411 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1412 free(scop);
1413 return NULL;
1416 void pet_implication_dump(struct pet_implication *implication)
1418 if (!implication)
1419 return;
1421 fprintf(stderr, "%d\n", implication->satisfied);
1422 isl_map_dump(implication->extension);
1425 void pet_scop_dump(struct pet_scop *scop)
1427 int i;
1428 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1430 if (!scop)
1431 return;
1433 isl_set_dump(scop->context);
1434 isl_set_dump(scop->context_value);
1435 for (i = 0; i < scop->n_array; ++i)
1436 pet_array_dump(scop->arrays[i]);
1437 for (i = 0; i < scop->n_stmt; ++i)
1438 pet_stmt_dump(scop->stmts[i]);
1439 for (i = 0; i < scop->n_implication; ++i)
1440 pet_implication_dump(scop->implications[i]);
1442 if (ext->skip[0]) {
1443 fprintf(stderr, "skip\n");
1444 isl_multi_pw_aff_dump(ext->skip[0]);
1445 isl_multi_pw_aff_dump(ext->skip[1]);
1449 /* Return 1 if the two pet_arrays are equivalent.
1451 * We don't compare element_size as this may be target dependent.
1453 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1455 if (!array1 || !array2)
1456 return 0;
1458 if (!isl_set_is_equal(array1->context, array2->context))
1459 return 0;
1460 if (!isl_set_is_equal(array1->extent, array2->extent))
1461 return 0;
1462 if (!!array1->value_bounds != !!array2->value_bounds)
1463 return 0;
1464 if (array1->value_bounds &&
1465 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1466 return 0;
1467 if (strcmp(array1->element_type, array2->element_type))
1468 return 0;
1469 if (array1->live_out != array2->live_out)
1470 return 0;
1471 if (array1->uniquely_defined != array2->uniquely_defined)
1472 return 0;
1473 if (array1->declared != array2->declared)
1474 return 0;
1475 if (array1->exposed != array2->exposed)
1476 return 0;
1478 return 1;
1481 /* Return 1 if the two pet_stmts are equivalent.
1483 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1485 int i;
1487 if (!stmt1 || !stmt2)
1488 return 0;
1490 if (stmt1->line != stmt2->line)
1491 return 0;
1492 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1493 return 0;
1494 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1495 return 0;
1496 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1497 return 0;
1498 if (stmt1->n_arg != stmt2->n_arg)
1499 return 0;
1500 for (i = 0; i < stmt1->n_arg; ++i) {
1501 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1502 return 0;
1505 return 1;
1508 /* Return 1 if the two pet_implications are equivalent.
1510 int pet_implication_is_equal(struct pet_implication *implication1,
1511 struct pet_implication *implication2)
1513 if (!implication1 || !implication2)
1514 return 0;
1516 if (implication1->satisfied != implication2->satisfied)
1517 return 0;
1518 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1519 return 0;
1521 return 1;
1524 /* Return 1 if the two pet_scops are equivalent.
1526 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1528 int i;
1530 if (!scop1 || !scop2)
1531 return 0;
1533 if (!isl_set_is_equal(scop1->context, scop2->context))
1534 return 0;
1535 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1536 return 0;
1538 if (scop1->n_array != scop2->n_array)
1539 return 0;
1540 for (i = 0; i < scop1->n_array; ++i)
1541 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1542 return 0;
1544 if (scop1->n_stmt != scop2->n_stmt)
1545 return 0;
1546 for (i = 0; i < scop1->n_stmt; ++i)
1547 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1548 return 0;
1550 if (scop1->n_implication != scop2->n_implication)
1551 return 0;
1552 for (i = 0; i < scop1->n_implication; ++i)
1553 if (!pet_implication_is_equal(scop1->implications[i],
1554 scop2->implications[i]))
1555 return 0;
1557 return 1;
1560 /* Prefix the schedule of "stmt" with an extra dimension with constant
1561 * value "pos".
1563 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1565 if (!stmt)
1566 return NULL;
1568 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1569 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1570 if (!stmt->schedule)
1571 return pet_stmt_free(stmt);
1573 return stmt;
1576 /* Prefix the schedules of all statements in "scop" with an extra
1577 * dimension with constant value "pos".
1579 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1581 int i;
1583 if (!scop)
1584 return NULL;
1586 for (i = 0; i < scop->n_stmt; ++i) {
1587 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1588 if (!scop->stmts[i])
1589 return pet_scop_free(scop);
1592 return scop;
1595 /* Given a set with a parameter at "param_pos" that refers to the
1596 * iterator, "move" the iterator to the first set dimension.
1597 * That is, essentially equate the parameter to the first set dimension
1598 * and then project it out.
1600 * The first set dimension may however refer to a virtual iterator,
1601 * while the parameter refers to the "real" iterator.
1602 * We therefore need to take into account the affine expression "iv_map", which
1603 * expresses the real iterator in terms of the virtual iterator.
1604 * In particular, we equate the set dimension to the input of the map
1605 * and the parameter to the output of the map and then project out
1606 * everything we don't need anymore.
1608 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1609 int param_pos, __isl_take isl_aff *iv_map)
1611 isl_map *map, *map2;
1612 map = isl_map_from_domain(set);
1613 map = isl_map_add_dims(map, isl_dim_out, 1);
1614 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1615 map2 = isl_map_from_aff(iv_map);
1616 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1617 map = isl_map_apply_range(map, map2);
1618 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1619 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1620 return isl_map_domain(map);
1623 /* Data used in embed_access.
1624 * extend adds an iterator to the iteration domain (through precomposition).
1625 * iv_map expresses the real iterator in terms of the virtual iterator
1626 * var_id represents the induction variable of the corresponding loop
1628 struct pet_embed_access {
1629 isl_multi_pw_aff *extend;
1630 isl_aff *iv_map;
1631 isl_id *var_id;
1634 /* Given an index expression, return an expression for the outer iterator.
1636 static __isl_give isl_aff *index_outer_iterator(
1637 __isl_take isl_multi_pw_aff *index)
1639 isl_space *space;
1640 isl_local_space *ls;
1642 space = isl_multi_pw_aff_get_domain_space(index);
1643 isl_multi_pw_aff_free(index);
1645 ls = isl_local_space_from_space(space);
1646 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1649 /* Replace an index expression that references the new (outer) iterator variable
1650 * by one that references the corresponding (real) iterator.
1652 * The input index expression is of the form
1654 * { S[i',...] -> i[] }
1656 * where i' refers to the virtual iterator.
1658 * iv_map is of the form
1660 * { [i'] -> [i] }
1662 * Return the index expression
1664 * { S[i',...] -> [i] }
1666 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1667 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1669 isl_space *space;
1670 isl_aff *aff;
1672 aff = index_outer_iterator(index);
1673 space = isl_aff_get_space(aff);
1674 iv_map = isl_aff_align_params(iv_map, space);
1675 aff = isl_aff_pullback_aff(iv_map, aff);
1677 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1680 /* Given an index expression "index" that refers to the (real) iterator
1681 * through the parameter at position "pos", plug in "iv_map", expressing
1682 * the real iterator in terms of the virtual (outer) iterator.
1684 * In particular, the index expression is of the form
1686 * [..., i, ...] -> { S[i',...] -> ... i ... }
1688 * where i refers to the real iterator and i' refers to the virtual iterator.
1690 * iv_map is of the form
1692 * { [i'] -> [i] }
1694 * Return the index expression
1696 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1699 * We first move the parameter to the input
1701 * [..., ...] -> { [i, i',...] -> ... i ... }
1703 * and construct
1705 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1707 * and then combine the two to obtain the desired result.
1709 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1710 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1712 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1713 isl_multi_aff *ma;
1715 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1716 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1717 isl_dim_param, pos, 1);
1719 space = isl_space_map_from_set(space);
1720 ma = isl_multi_aff_identity(isl_space_copy(space));
1721 iv_map = isl_aff_align_params(iv_map, space);
1722 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1723 ma = isl_multi_aff_flat_range_product(
1724 isl_multi_aff_from_aff(iv_map), ma);
1725 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1727 return index;
1730 /* Embed the given index expression in an extra outer loop.
1731 * The domain of the index expression has already been updated.
1733 * If the access refers to the induction variable, then it is
1734 * turned into an access to the set of integers with index (and value)
1735 * equal to the induction variable.
1737 * If the accessed array is a virtual array (with user
1738 * pointer equal to NULL), as created by create_test_index,
1739 * then it is extended along with the domain of the index expression.
1741 static __isl_give isl_multi_pw_aff *embed_index_expression(
1742 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1744 isl_id *array_id = NULL;
1745 int pos;
1747 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1748 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1749 if (array_id == data->var_id) {
1750 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1751 } else if (array_id && !isl_id_get_user(array_id)) {
1752 isl_aff *aff;
1753 isl_multi_pw_aff *mpa;
1755 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1756 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1757 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1758 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1759 isl_id_copy(array_id));
1761 isl_id_free(array_id);
1763 pos = isl_multi_pw_aff_find_dim_by_id(index,
1764 isl_dim_param, data->var_id);
1765 if (pos >= 0)
1766 index = index_internalize_iv(index, pos,
1767 isl_aff_copy(data->iv_map));
1768 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1769 isl_id_copy(data->var_id));
1771 return index;
1774 /* Embed the given access relation in an extra outer loop.
1775 * The domain of the access relation has already been updated.
1777 * If the access refers to the induction variable, then it is
1778 * turned into an access to the set of integers with index (and value)
1779 * equal to the induction variable.
1781 * If the induction variable appears in the constraints (as a parameter),
1782 * then the parameter is equated to the newly introduced iteration
1783 * domain dimension and subsequently projected out.
1785 * Similarly, if the accessed array is a virtual array (with user
1786 * pointer equal to NULL), as created by create_test_index,
1787 * then it is extended along with the domain of the access.
1789 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
1790 struct pet_embed_access *data)
1792 isl_id *array_id = NULL;
1793 int pos;
1795 if (isl_map_has_tuple_id(access, isl_dim_out))
1796 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1797 if (array_id == data->var_id ||
1798 (array_id && !isl_id_get_user(array_id))) {
1799 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1800 access = isl_map_equate(access,
1801 isl_dim_in, 0, isl_dim_out, 0);
1802 if (array_id == data->var_id)
1803 access = isl_map_apply_range(access,
1804 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1805 else
1806 access = isl_map_set_tuple_id(access, isl_dim_out,
1807 isl_id_copy(array_id));
1809 isl_id_free(array_id);
1811 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1812 if (pos >= 0) {
1813 isl_set *set = isl_map_wrap(access);
1814 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1815 access = isl_set_unwrap(set);
1817 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1818 isl_id_copy(data->var_id));
1820 return access;
1823 /* Given an access expression, embed the associated access relation and
1824 * index expression in an extra outer loop.
1826 * We first update the domains to insert the extra dimension and
1827 * then update the access relation and index expression to take
1828 * into account the mapping "iv_map" from virtual iterator
1829 * to real iterator.
1831 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
1833 int dim;
1834 struct pet_embed_access *data = user;
1836 expr = update_domain(expr, data->extend);
1837 if (!expr)
1838 return NULL;
1840 expr->acc.access = embed_access_relation(expr->acc.access, data);
1841 expr->acc.index = embed_index_expression(expr->acc.index, data);
1842 if (!expr->acc.access || !expr->acc.index)
1843 return pet_expr_free(expr);
1845 return expr;
1848 /* Embed all access subexpressions of "expr" in an extra loop.
1849 * "extend" inserts an outer loop iterator in the iteration domains
1850 * (through precomposition).
1851 * "iv_map" expresses the real iterator in terms of the virtual iterator
1852 * "var_id" represents the induction variable.
1854 static struct pet_expr *expr_embed(struct pet_expr *expr,
1855 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
1856 __isl_keep isl_id *var_id)
1858 struct pet_embed_access data =
1859 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1861 expr = pet_expr_map_access(expr, &embed_access, &data);
1862 isl_aff_free(iv_map);
1863 isl_multi_pw_aff_free(extend);
1864 return expr;
1867 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1868 * "dom" and schedule "sched". "var_id" represents the induction variable
1869 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1870 * That is, it expresses the iterator that some of the parameters in "stmt"
1871 * may refer to in terms of the iterator used in "dom" and
1872 * the domain of "sched".
1874 * The iteration domain and schedule of the statement are updated
1875 * according to the iteration domain and schedule of the new loop.
1876 * If stmt->domain is a wrapped map, then the iteration domain
1877 * is the domain of this map, so we need to be careful to adjust
1878 * this domain.
1880 * If the induction variable appears in the constraints (as a parameter)
1881 * of the current iteration domain or the schedule of the statement,
1882 * then the parameter is equated to the newly introduced iteration
1883 * domain dimension and subsequently projected out.
1885 * Finally, all access relations are updated based on the extra loop.
1887 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1888 __isl_take isl_set *dom, __isl_take isl_map *sched,
1889 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1891 int i;
1892 int pos;
1893 isl_id *stmt_id;
1894 isl_space *dim;
1895 isl_multi_pw_aff *extend;
1897 if (!stmt)
1898 goto error;
1900 if (isl_set_is_wrapping(stmt->domain)) {
1901 isl_map *map;
1902 isl_map *ext;
1903 isl_space *ran_dim;
1905 map = isl_set_unwrap(stmt->domain);
1906 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1907 ran_dim = isl_space_range(isl_map_get_space(map));
1908 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1909 isl_set_universe(ran_dim));
1910 map = isl_map_flat_domain_product(ext, map);
1911 map = isl_map_set_tuple_id(map, isl_dim_in,
1912 isl_id_copy(stmt_id));
1913 dim = isl_space_domain(isl_map_get_space(map));
1914 stmt->domain = isl_map_wrap(map);
1915 } else {
1916 stmt_id = isl_set_get_tuple_id(stmt->domain);
1917 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1918 stmt->domain);
1919 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1920 isl_id_copy(stmt_id));
1921 dim = isl_set_get_space(stmt->domain);
1924 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1925 if (pos >= 0)
1926 stmt->domain = internalize_iv(stmt->domain, pos,
1927 isl_aff_copy(iv_map));
1929 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1930 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1931 isl_dim_in, stmt_id);
1933 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1934 if (pos >= 0) {
1935 isl_set *set = isl_map_wrap(stmt->schedule);
1936 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
1937 stmt->schedule = isl_set_unwrap(set);
1940 dim = isl_space_map_from_set(dim);
1941 extend = isl_multi_pw_aff_identity(dim);
1942 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
1943 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
1944 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
1945 for (i = 0; i < stmt->n_arg; ++i)
1946 stmt->args[i] = expr_embed(stmt->args[i],
1947 isl_multi_pw_aff_copy(extend),
1948 isl_aff_copy(iv_map), var_id);
1949 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1951 isl_set_free(dom);
1952 isl_id_free(var_id);
1954 for (i = 0; i < stmt->n_arg; ++i)
1955 if (!stmt->args[i])
1956 return pet_stmt_free(stmt);
1957 if (!stmt->domain || !stmt->schedule || !stmt->body)
1958 return pet_stmt_free(stmt);
1959 return stmt;
1960 error:
1961 isl_set_free(dom);
1962 isl_map_free(sched);
1963 isl_aff_free(iv_map);
1964 isl_id_free(var_id);
1965 return NULL;
1968 /* Embed the given pet_array in an extra outer loop with iteration domain
1969 * "dom".
1970 * This embedding only has an effect on virtual arrays (those with
1971 * user pointer equal to NULL), which need to be extended along with
1972 * the iteration domain.
1974 static struct pet_array *pet_array_embed(struct pet_array *array,
1975 __isl_take isl_set *dom)
1977 isl_id *array_id = NULL;
1979 if (!array)
1980 goto error;
1982 if (isl_set_has_tuple_id(array->extent))
1983 array_id = isl_set_get_tuple_id(array->extent);
1985 if (array_id && !isl_id_get_user(array_id)) {
1986 array->extent = isl_set_flat_product(dom, array->extent);
1987 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1988 if (!array->extent)
1989 return pet_array_free(array);
1990 } else {
1991 isl_set_free(dom);
1992 isl_id_free(array_id);
1995 return array;
1996 error:
1997 isl_set_free(dom);
1998 return NULL;
2001 /* Project out all unnamed parameters from "set" and return the result.
2003 static __isl_give isl_set *set_project_out_unnamed_params(
2004 __isl_take isl_set *set)
2006 int i, n;
2008 n = isl_set_dim(set, isl_dim_param);
2009 for (i = n - 1; i >= 0; --i) {
2010 if (isl_set_has_dim_name(set, isl_dim_param, i))
2011 continue;
2012 set = isl_set_project_out(set, isl_dim_param, i, 1);
2015 return set;
2018 /* Update the context with respect to an embedding into a loop
2019 * with iteration domain "dom" and induction variable "id".
2020 * "iv_map" expresses the real iterator (parameter "id") in terms
2021 * of a possibly virtual iterator (used in "dom").
2023 * If the current context is independent of "id", we don't need
2024 * to do anything.
2025 * Otherwise, a parameter value is invalid for the embedding if
2026 * any of the corresponding iterator values is invalid.
2027 * That is, a parameter value is valid only if all the corresponding
2028 * iterator values are valid.
2029 * We therefore compute the set of parameters
2031 * forall i in dom : valid (i)
2033 * or
2035 * not exists i in dom : not valid(i)
2037 * i.e.,
2039 * not exists i in dom \ valid(i)
2041 * Before we subtract valid(i) from dom, we first need to substitute
2042 * the real iterator for the virtual iterator.
2044 * If there are any unnamed parameters in "dom", then we consider
2045 * a parameter value to be valid if it is valid for any value of those
2046 * unnamed parameters. They are therefore projected out at the end.
2048 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
2049 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
2050 __isl_keep isl_id *id)
2052 int pos;
2053 isl_multi_aff *ma;
2055 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
2056 if (pos < 0)
2057 return context;
2059 context = isl_set_from_params(context);
2060 context = isl_set_add_dims(context, isl_dim_set, 1);
2061 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
2062 context = isl_set_project_out(context, isl_dim_param, pos, 1);
2063 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
2064 context = isl_set_preimage_multi_aff(context, ma);
2065 context = isl_set_subtract(isl_set_copy(dom), context);
2066 context = isl_set_params(context);
2067 context = isl_set_complement(context);
2068 context = set_project_out_unnamed_params(context);
2069 return context;
2072 /* Update the implication with respect to an embedding into a loop
2073 * with iteration domain "dom".
2075 * Since embed_access extends virtual arrays along with the domain
2076 * of the access, we need to do the same with domain and range
2077 * of the implication. Since the original implication is only valid
2078 * within a given iteration of the loop, the extended implication
2079 * maps the extra array dimension corresponding to the extra loop
2080 * to itself.
2082 static struct pet_implication *pet_implication_embed(
2083 struct pet_implication *implication, __isl_take isl_set *dom)
2085 isl_id *id;
2086 isl_map *map;
2088 if (!implication)
2089 goto error;
2091 map = isl_set_identity(dom);
2092 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
2093 map = isl_map_flat_product(map, implication->extension);
2094 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
2095 map = isl_map_set_tuple_id(map, isl_dim_out, id);
2096 implication->extension = map;
2097 if (!implication->extension)
2098 return pet_implication_free(implication);
2100 return implication;
2101 error:
2102 isl_set_free(dom);
2103 return NULL;
2106 /* Embed all statements and arrays in "scop" in an extra outer loop
2107 * with iteration domain "dom" and schedule "sched".
2108 * "id" represents the induction variable of the loop.
2109 * "iv_map" maps a possibly virtual iterator to the real iterator.
2110 * That is, it expresses the iterator that some of the parameters in "scop"
2111 * may refer to in terms of the iterator used in "dom" and
2112 * the domain of "sched".
2114 * Any skip conditions within the loop have no effect outside of the loop.
2115 * The caller is responsible for making sure skip[pet_skip_later] has been
2116 * taken into account.
2118 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
2119 __isl_take isl_map *sched, __isl_take isl_aff *iv_map,
2120 __isl_take isl_id *id)
2122 int i;
2124 if (!scop)
2125 goto error;
2127 pet_scop_reset_skip(scop, pet_skip_now);
2128 pet_scop_reset_skip(scop, pet_skip_later);
2130 scop->context = context_embed(scop->context, dom, iv_map, id);
2131 if (!scop->context)
2132 goto error;
2134 for (i = 0; i < scop->n_stmt; ++i) {
2135 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
2136 isl_set_copy(dom), isl_map_copy(sched),
2137 isl_aff_copy(iv_map), isl_id_copy(id));
2138 if (!scop->stmts[i])
2139 goto error;
2142 for (i = 0; i < scop->n_array; ++i) {
2143 scop->arrays[i] = pet_array_embed(scop->arrays[i],
2144 isl_set_copy(dom));
2145 if (!scop->arrays[i])
2146 goto error;
2149 for (i = 0; i < scop->n_implication; ++i) {
2150 scop->implications[i] =
2151 pet_implication_embed(scop->implications[i],
2152 isl_set_copy(dom));
2153 if (!scop->implications[i])
2154 goto error;
2157 isl_set_free(dom);
2158 isl_map_free(sched);
2159 isl_aff_free(iv_map);
2160 isl_id_free(id);
2161 return scop;
2162 error:
2163 isl_set_free(dom);
2164 isl_map_free(sched);
2165 isl_aff_free(iv_map);
2166 isl_id_free(id);
2167 return pet_scop_free(scop);
2170 /* Add extra conditions on the parameters to iteration domain of "stmt".
2172 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
2173 __isl_take isl_set *cond)
2175 if (!stmt)
2176 goto error;
2178 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
2180 return stmt;
2181 error:
2182 isl_set_free(cond);
2183 return pet_stmt_free(stmt);
2186 /* Add extra conditions to scop->skip[type].
2188 * The new skip condition only holds if it held before
2189 * and the condition is true. It does not hold if it did not hold
2190 * before or the condition is false.
2192 * The skip condition is assumed to be an affine expression.
2194 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
2195 enum pet_skip type, __isl_keep isl_set *cond)
2197 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2198 isl_pw_aff *skip;
2199 isl_set *dom;
2201 if (!scop)
2202 return NULL;
2203 if (!ext->skip[type])
2204 return scop;
2206 if (!multi_pw_aff_is_affine(ext->skip[type]))
2207 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
2208 isl_error_internal, "can only resrict affine skips",
2209 return pet_scop_free(scop));
2211 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2212 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
2213 cond = isl_set_copy(cond);
2214 cond = isl_set_from_params(cond);
2215 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
2216 skip = indicator_function(cond, dom);
2217 isl_multi_pw_aff_free(ext->skip[type]);
2218 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
2219 if (!ext->skip[type])
2220 return pet_scop_free(scop);
2222 return scop;
2225 /* Add extra conditions on the parameters to all iteration domains
2226 * and skip conditions.
2228 * A parameter value is valid for the result if it was valid
2229 * for the original scop and satisfies "cond" or if it does
2230 * not satisfy "cond" as in this case the scop is not executed
2231 * and the original constraints on the parameters are irrelevant.
2233 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
2234 __isl_take isl_set *cond)
2236 int i;
2238 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
2239 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
2241 if (!scop)
2242 goto error;
2244 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
2245 scop->context = isl_set_union(scop->context,
2246 isl_set_complement(isl_set_copy(cond)));
2247 scop->context = isl_set_coalesce(scop->context);
2248 scop->context = set_project_out_unnamed_params(scop->context);
2249 if (!scop->context)
2250 goto error;
2252 for (i = 0; i < scop->n_stmt; ++i) {
2253 scop->stmts[i] = stmt_restrict(scop->stmts[i],
2254 isl_set_copy(cond));
2255 if (!scop->stmts[i])
2256 goto error;
2259 isl_set_free(cond);
2260 return scop;
2261 error:
2262 isl_set_free(cond);
2263 return pet_scop_free(scop);
2266 /* Construct a function that (upon precomposition) inserts
2267 * a filter value with name "id" and value "satisfied"
2268 * in the list of filter values embedded in the set space "space".
2270 * If "space" does not contain any filter values yet, we first create
2271 * a function that inserts 0 filter values, i.e.,
2273 * [space -> []] -> space
2275 * We can now assume that space is of the form [dom -> [filters]]
2276 * We construct an identity mapping on dom and a mapping on filters
2277 * that (upon precomposition) inserts the new filter
2279 * dom -> dom
2280 * [satisfied, filters] -> [filters]
2282 * and then compute the cross product
2284 * [dom -> [satisfied, filters]] -> [dom -> [filters]]
2286 static __isl_give isl_pw_multi_aff *insert_filter_pma(
2287 __isl_take isl_space *space, __isl_take isl_id *id, int satisfied)
2289 isl_space *space2;
2290 isl_multi_aff *ma;
2291 isl_pw_multi_aff *pma0, *pma, *pma_dom, *pma_ran;
2292 isl_set *dom;
2294 if (isl_space_is_wrapping(space)) {
2295 space2 = isl_space_map_from_set(isl_space_copy(space));
2296 ma = isl_multi_aff_identity(space2);
2297 space = isl_space_unwrap(space);
2298 } else {
2299 space = isl_space_from_domain(space);
2300 ma = isl_multi_aff_domain_map(isl_space_copy(space));
2303 space2 = isl_space_domain(isl_space_copy(space));
2304 pma_dom = isl_pw_multi_aff_identity(isl_space_map_from_set(space2));
2305 space = isl_space_range(space);
2306 space = isl_space_insert_dims(space, isl_dim_set, 0, 1);
2307 pma_ran = isl_pw_multi_aff_project_out_map(space, isl_dim_set, 0, 1);
2308 pma_ran = isl_pw_multi_aff_set_dim_id(pma_ran, isl_dim_in, 0, id);
2309 pma_ran = isl_pw_multi_aff_fix_si(pma_ran, isl_dim_in, 0, satisfied);
2310 pma = isl_pw_multi_aff_product(pma_dom, pma_ran);
2312 pma0 = isl_pw_multi_aff_from_multi_aff(ma);
2313 pma = isl_pw_multi_aff_pullback_pw_multi_aff(pma0, pma);
2315 return pma;
2318 /* Insert an argument expression corresponding to "test" in front
2319 * of the list of arguments described by *n_arg and *args.
2321 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
2322 __isl_keep isl_multi_pw_aff *test)
2324 int i;
2325 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
2327 if (!test)
2328 return -1;
2330 if (!*args) {
2331 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
2332 if (!*args)
2333 return -1;
2334 } else {
2335 struct pet_expr **ext;
2336 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
2337 if (!ext)
2338 return -1;
2339 for (i = 0; i < *n_arg; ++i)
2340 ext[1 + i] = (*args)[i];
2341 free(*args);
2342 *args = ext;
2344 (*n_arg)++;
2345 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
2346 if (!(*args)[0])
2347 return -1;
2349 return 0;
2352 /* Make the expression "expr" depend on the value of "test"
2353 * being equal to "satisfied".
2355 * If "test" is an affine expression, we simply add the conditions
2356 * on the expression having the value "satisfied" to all access relations
2357 * and index expressions.
2359 * Otherwise, we add a filter to "expr" (which is then assumed to be
2360 * an access expression) corresponding to "test" being equal to "satisfied".
2362 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
2363 __isl_take isl_multi_pw_aff *test, int satisfied)
2365 isl_id *id;
2366 isl_ctx *ctx;
2367 isl_space *space;
2368 isl_pw_multi_aff *pma;
2370 if (!expr || !test)
2371 goto error;
2373 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
2374 isl_pw_aff *pa;
2375 isl_set *cond;
2377 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
2378 isl_multi_pw_aff_free(test);
2379 if (satisfied)
2380 cond = isl_pw_aff_non_zero_set(pa);
2381 else
2382 cond = isl_pw_aff_zero_set(pa);
2383 return pet_expr_restrict(expr, isl_set_params(cond));
2386 ctx = isl_multi_pw_aff_get_ctx(test);
2387 if (expr->type != pet_expr_access)
2388 isl_die(ctx, isl_error_invalid,
2389 "can only filter access expressions", goto error);
2391 space = isl_space_domain(isl_map_get_space(expr->acc.access));
2392 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2393 pma = insert_filter_pma(space, id, satisfied);
2395 expr->acc.access = isl_map_preimage_domain_pw_multi_aff(
2396 expr->acc.access,
2397 isl_pw_multi_aff_copy(pma));
2398 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
2399 expr->acc.index, pma);
2400 if (!expr->acc.access || !expr->acc.index)
2401 goto error;
2403 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
2404 goto error;
2406 isl_multi_pw_aff_free(test);
2407 return expr;
2408 error:
2409 isl_multi_pw_aff_free(test);
2410 return pet_expr_free(expr);
2413 /* Look through the applications in "scop" for any that can be
2414 * applied to the filter expressed by "map" and "satisified".
2415 * If there is any, then apply it to "map" and return the result.
2416 * Otherwise, return "map".
2417 * "id" is the identifier of the virtual array.
2419 * We only introduce at most one implication for any given virtual array,
2420 * so we can apply the implication and return as soon as we find one.
2422 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
2423 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
2425 int i;
2427 for (i = 0; i < scop->n_implication; ++i) {
2428 struct pet_implication *pi = scop->implications[i];
2429 isl_id *pi_id;
2431 if (pi->satisfied != satisfied)
2432 continue;
2433 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
2434 isl_id_free(pi_id);
2435 if (pi_id != id)
2436 continue;
2438 return isl_map_apply_range(map, isl_map_copy(pi->extension));
2441 return map;
2444 /* Is the filter expressed by "test" and "satisfied" implied
2445 * by filter "pos" on "domain", with filter "expr", taking into
2446 * account the implications of "scop"?
2448 * For filter on domain implying that expressed by "test" and "satisfied",
2449 * the filter needs to be an access to the same (virtual) array as "test" and
2450 * the filter value needs to be equal to "satisfied".
2451 * Moreover, the filter access relation, possibly extended by
2452 * the implications in "scop" needs to contain "test".
2454 static int implies_filter(struct pet_scop *scop,
2455 __isl_keep isl_map *domain, int pos, struct pet_expr *expr,
2456 __isl_keep isl_map *test, int satisfied)
2458 isl_id *test_id, *arg_id;
2459 isl_val *val;
2460 int is_int;
2461 int s;
2462 int is_subset;
2463 isl_map *implied;
2465 if (expr->type != pet_expr_access)
2466 return 0;
2467 test_id = isl_map_get_tuple_id(test, isl_dim_out);
2468 arg_id = pet_expr_access_get_id(expr);
2469 isl_id_free(arg_id);
2470 isl_id_free(test_id);
2471 if (test_id != arg_id)
2472 return 0;
2473 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
2474 is_int = isl_val_is_int(val);
2475 if (is_int)
2476 s = isl_val_get_num_si(val);
2477 isl_val_free(val);
2478 if (!val)
2479 return -1;
2480 if (!is_int)
2481 return 0;
2482 if (s != satisfied)
2483 return 0;
2485 implied = isl_map_copy(expr->acc.access);
2486 implied = apply_implications(scop, implied, test_id, satisfied);
2487 is_subset = isl_map_is_subset(test, implied);
2488 isl_map_free(implied);
2490 return is_subset;
2493 /* Is the filter expressed by "test" and "satisfied" implied
2494 * by any of the filters on the domain of "stmt", taking into
2495 * account the implications of "scop"?
2497 static int filter_implied(struct pet_scop *scop,
2498 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
2500 int i;
2501 int implied;
2502 isl_id *test_id;
2503 isl_map *domain;
2504 isl_map *test_map;
2506 if (!scop || !stmt || !test)
2507 return -1;
2508 if (scop->n_implication == 0)
2509 return 0;
2510 if (stmt->n_arg == 0)
2511 return 0;
2513 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
2514 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
2516 implied = 0;
2517 for (i = 0; i < stmt->n_arg; ++i) {
2518 implied = implies_filter(scop, domain, i, stmt->args[i],
2519 test_map, satisfied);
2520 if (implied < 0 || implied)
2521 break;
2524 isl_map_free(test_map);
2525 isl_map_free(domain);
2526 return implied;
2529 /* Make the statement "stmt" depend on the value of "test"
2530 * being equal to "satisfied" by adjusting stmt->domain.
2532 * The domain of "test" corresponds to the (zero or more) outer dimensions
2533 * of the iteration domain.
2535 * We first extend "test" to apply to the entire iteration domain and
2536 * then check if the filter that we are about to add is implied
2537 * by any of the current filters, possibly taking into account
2538 * the implications in "scop". If so, we leave "stmt" untouched and return.
2540 * Otherwise, we insert an argument corresponding to a read to "test"
2541 * from the iteration domain of "stmt" in front of the list of arguments.
2542 * We also insert a corresponding output dimension in the wrapped
2543 * map contained in stmt->domain, with value set to "satisfied".
2545 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
2546 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
2548 int i;
2549 int implied;
2550 isl_id *id;
2551 isl_ctx *ctx;
2552 isl_pw_multi_aff *pma;
2553 isl_multi_aff *add_dom;
2554 isl_space *space;
2555 isl_local_space *ls;
2556 int n_test_dom;
2558 if (!stmt || !test)
2559 goto error;
2561 space = isl_set_get_space(stmt->domain);
2562 if (isl_space_is_wrapping(space))
2563 space = isl_space_domain(isl_space_unwrap(space));
2564 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
2565 space = isl_space_from_domain(space);
2566 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
2567 add_dom = isl_multi_aff_zero(isl_space_copy(space));
2568 ls = isl_local_space_from_space(isl_space_domain(space));
2569 for (i = 0; i < n_test_dom; ++i) {
2570 isl_aff *aff;
2571 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2572 isl_dim_set, i);
2573 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
2575 isl_local_space_free(ls);
2576 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
2578 implied = filter_implied(scop, stmt, test, satisfied);
2579 if (implied < 0)
2580 goto error;
2581 if (implied) {
2582 isl_multi_pw_aff_free(test);
2583 return stmt;
2586 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2587 pma = insert_filter_pma(isl_set_get_space(stmt->domain), id, satisfied);
2588 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
2590 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
2591 goto error;
2593 isl_multi_pw_aff_free(test);
2594 return stmt;
2595 error:
2596 isl_multi_pw_aff_free(test);
2597 return pet_stmt_free(stmt);
2600 /* Does "scop" have a skip condition of the given "type"?
2602 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2604 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2606 if (!scop)
2607 return -1;
2608 return ext->skip[type] != NULL;
2611 /* Does "scop" have a skip condition of the given "type" that
2612 * is an affine expression?
2614 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2616 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2618 if (!scop)
2619 return -1;
2620 if (!ext->skip[type])
2621 return 0;
2622 return multi_pw_aff_is_affine(ext->skip[type]);
2625 /* Does "scop" have a skip condition of the given "type" that
2626 * is not an affine expression?
2628 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2630 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2631 int aff;
2633 if (!scop)
2634 return -1;
2635 if (!ext->skip[type])
2636 return 0;
2637 aff = multi_pw_aff_is_affine(ext->skip[type]);
2638 if (aff < 0)
2639 return -1;
2640 return !aff;
2643 /* Does "scop" have a skip condition of the given "type" that
2644 * is affine and holds on the entire domain?
2646 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2648 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2649 isl_pw_aff *pa;
2650 isl_set *set;
2651 int is_aff;
2652 int is_univ;
2654 is_aff = pet_scop_has_affine_skip(scop, type);
2655 if (is_aff < 0 || !is_aff)
2656 return is_aff;
2658 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2659 set = isl_pw_aff_non_zero_set(pa);
2660 is_univ = isl_set_plain_is_universe(set);
2661 isl_set_free(set);
2663 return is_univ;
2666 /* Replace scop->skip[type] by "skip".
2668 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2669 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2671 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2673 if (!scop || !skip)
2674 goto error;
2676 isl_multi_pw_aff_free(ext->skip[type]);
2677 ext->skip[type] = skip;
2679 return scop;
2680 error:
2681 isl_multi_pw_aff_free(skip);
2682 return pet_scop_free(scop);
2685 /* Return a copy of scop->skip[type].
2687 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2688 enum pet_skip type)
2690 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2692 if (!scop)
2693 return NULL;
2695 return isl_multi_pw_aff_copy(ext->skip[type]);
2698 /* Assuming scop->skip[type] is an affine expression,
2699 * return the constraints on the parameters for which the skip condition
2700 * holds.
2702 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2703 enum pet_skip type)
2705 isl_multi_pw_aff *skip;
2706 isl_pw_aff *pa;
2708 skip = pet_scop_get_skip(scop, type);
2709 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2710 isl_multi_pw_aff_free(skip);
2711 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2714 /* Return the identifier of the variable that is accessed by
2715 * the skip condition of the given type.
2717 * The skip condition is assumed not to be an affine condition.
2719 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2720 enum pet_skip type)
2722 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2724 if (!scop)
2725 return NULL;
2727 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2730 /* Return an access pet_expr corresponding to the skip condition
2731 * of the given type.
2733 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2734 enum pet_skip type)
2736 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2739 /* Drop the the skip condition scop->skip[type].
2741 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2743 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2745 if (!scop)
2746 return;
2748 isl_multi_pw_aff_free(ext->skip[type]);
2749 ext->skip[type] = NULL;
2752 /* Make the skip condition (if any) depend on the value of "test" being
2753 * equal to "satisfied".
2755 * We only support the case where the original skip condition is universal,
2756 * i.e., where skipping is unconditional, and where satisfied == 1.
2757 * In this case, the skip condition is changed to skip only when
2758 * "test" is equal to one.
2760 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2761 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2763 int is_univ = 0;
2765 if (!scop)
2766 return NULL;
2767 if (!pet_scop_has_skip(scop, type))
2768 return scop;
2770 if (satisfied)
2771 is_univ = pet_scop_has_universal_skip(scop, type);
2772 if (is_univ < 0)
2773 return pet_scop_free(scop);
2774 if (satisfied && is_univ) {
2775 isl_space *space = isl_multi_pw_aff_get_space(test);
2776 isl_multi_pw_aff *skip;
2777 skip = isl_multi_pw_aff_zero(space);
2778 scop = pet_scop_set_skip(scop, type, skip);
2779 if (!scop)
2780 return NULL;
2781 } else {
2782 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2783 "skip expression cannot be filtered",
2784 return pet_scop_free(scop));
2787 return scop;
2790 /* Make all statements in "scop" depend on the value of "test"
2791 * being equal to "satisfied" by adjusting their domains.
2793 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2794 __isl_take isl_multi_pw_aff *test, int satisfied)
2796 int i;
2798 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2799 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2801 if (!scop || !test)
2802 goto error;
2804 for (i = 0; i < scop->n_stmt; ++i) {
2805 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2806 isl_multi_pw_aff_copy(test), satisfied);
2807 if (!scop->stmts[i])
2808 goto error;
2811 isl_multi_pw_aff_free(test);
2812 return scop;
2813 error:
2814 isl_multi_pw_aff_free(test);
2815 return pet_scop_free(scop);
2818 /* Add all parameters in "expr" to "dim" and return the result.
2820 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2821 __isl_take isl_space *dim)
2823 int i;
2825 if (!expr)
2826 goto error;
2827 for (i = 0; i < expr->n_arg; ++i)
2829 dim = expr_collect_params(expr->args[i], dim);
2831 if (expr->type == pet_expr_access)
2832 dim = isl_space_align_params(dim,
2833 isl_map_get_space(expr->acc.access));
2835 return dim;
2836 error:
2837 pet_expr_free(expr);
2838 return isl_space_free(dim);
2841 /* Add all parameters in "stmt" to "dim" and return the result.
2843 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2844 __isl_take isl_space *dim)
2846 if (!stmt)
2847 goto error;
2849 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2850 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2851 dim = expr_collect_params(stmt->body, dim);
2853 return dim;
2854 error:
2855 isl_space_free(dim);
2856 return pet_stmt_free(stmt);
2859 /* Add all parameters in "array" to "dim" and return the result.
2861 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2862 __isl_take isl_space *dim)
2864 if (!array)
2865 goto error;
2867 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2868 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2870 return dim;
2871 error:
2872 pet_array_free(array);
2873 return isl_space_free(dim);
2876 /* Add all parameters in "scop" to "dim" and return the result.
2878 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2879 __isl_take isl_space *dim)
2881 int i;
2883 if (!scop)
2884 goto error;
2886 for (i = 0; i < scop->n_array; ++i)
2887 dim = array_collect_params(scop->arrays[i], dim);
2889 for (i = 0; i < scop->n_stmt; ++i)
2890 dim = stmt_collect_params(scop->stmts[i], dim);
2892 return dim;
2893 error:
2894 isl_space_free(dim);
2895 pet_scop_free(scop);
2896 return NULL;
2899 /* Add all parameters in "dim" to all access relations and index expressions
2900 * in "expr".
2902 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2903 __isl_take isl_space *dim)
2905 int i;
2907 if (!expr)
2908 goto error;
2910 for (i = 0; i < expr->n_arg; ++i) {
2911 expr->args[i] =
2912 expr_propagate_params(expr->args[i],
2913 isl_space_copy(dim));
2914 if (!expr->args[i])
2915 goto error;
2918 if (expr->type == pet_expr_access) {
2919 expr->acc.access = isl_map_align_params(expr->acc.access,
2920 isl_space_copy(dim));
2921 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
2922 isl_space_copy(dim));
2923 if (!expr->acc.access || !expr->acc.index)
2924 goto error;
2927 isl_space_free(dim);
2928 return expr;
2929 error:
2930 isl_space_free(dim);
2931 return pet_expr_free(expr);
2934 /* Add all parameters in "dim" to the domain, schedule and
2935 * all access relations in "stmt".
2937 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2938 __isl_take isl_space *dim)
2940 if (!stmt)
2941 goto error;
2943 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2944 stmt->schedule = isl_map_align_params(stmt->schedule,
2945 isl_space_copy(dim));
2946 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2948 if (!stmt->domain || !stmt->schedule || !stmt->body)
2949 goto error;
2951 isl_space_free(dim);
2952 return stmt;
2953 error:
2954 isl_space_free(dim);
2955 return pet_stmt_free(stmt);
2958 /* Add all parameters in "dim" to "array".
2960 static struct pet_array *array_propagate_params(struct pet_array *array,
2961 __isl_take isl_space *dim)
2963 if (!array)
2964 goto error;
2966 array->context = isl_set_align_params(array->context,
2967 isl_space_copy(dim));
2968 array->extent = isl_set_align_params(array->extent,
2969 isl_space_copy(dim));
2970 if (array->value_bounds) {
2971 array->value_bounds = isl_set_align_params(array->value_bounds,
2972 isl_space_copy(dim));
2973 if (!array->value_bounds)
2974 goto error;
2977 if (!array->context || !array->extent)
2978 goto error;
2980 isl_space_free(dim);
2981 return array;
2982 error:
2983 isl_space_free(dim);
2984 return pet_array_free(array);
2987 /* Add all parameters in "dim" to "scop".
2989 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2990 __isl_take isl_space *dim)
2992 int i;
2994 if (!scop)
2995 goto error;
2997 for (i = 0; i < scop->n_array; ++i) {
2998 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2999 isl_space_copy(dim));
3000 if (!scop->arrays[i])
3001 goto error;
3004 for (i = 0; i < scop->n_stmt; ++i) {
3005 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
3006 isl_space_copy(dim));
3007 if (!scop->stmts[i])
3008 goto error;
3011 isl_space_free(dim);
3012 return scop;
3013 error:
3014 isl_space_free(dim);
3015 return pet_scop_free(scop);
3018 /* Update all isl_sets and isl_maps in "scop" such that they all
3019 * have the same parameters.
3021 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
3023 isl_space *dim;
3025 if (!scop)
3026 return NULL;
3028 dim = isl_set_get_space(scop->context);
3029 dim = scop_collect_params(scop, dim);
3031 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
3032 scop = scop_propagate_params(scop, dim);
3034 return scop;
3037 /* Check if the given index expression accesses a (0D) array that corresponds
3038 * to one of the parameters in "dim". If so, replace the array access
3039 * by an access to the set of integers with as index (and value)
3040 * that parameter.
3042 static __isl_give isl_multi_pw_aff *index_detect_parameter(
3043 __isl_take isl_multi_pw_aff *index, __isl_take isl_space *space)
3045 isl_local_space *ls;
3046 isl_id *array_id = NULL;
3047 isl_aff *aff;
3048 int pos = -1;
3050 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out)) {
3051 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
3052 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3054 isl_space_free(space);
3056 if (pos < 0) {
3057 isl_id_free(array_id);
3058 return index;
3061 space = isl_multi_pw_aff_get_domain_space(index);
3062 isl_multi_pw_aff_free(index);
3064 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3065 if (pos < 0) {
3066 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
3067 space = isl_space_set_dim_id(space, isl_dim_param, 0, array_id);
3068 pos = 0;
3069 } else
3070 isl_id_free(array_id);
3072 ls = isl_local_space_from_space(space);
3073 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
3074 index = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
3076 return index;
3079 /* Check if the given access relation accesses a (0D) array that corresponds
3080 * to one of the parameters in "dim". If so, replace the array access
3081 * by an access to the set of integers with as index (and value)
3082 * that parameter.
3084 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
3085 __isl_take isl_space *dim)
3087 isl_id *array_id = NULL;
3088 int pos = -1;
3090 if (isl_map_has_tuple_id(access, isl_dim_out)) {
3091 array_id = isl_map_get_tuple_id(access, isl_dim_out);
3092 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
3094 isl_space_free(dim);
3096 if (pos < 0) {
3097 isl_id_free(array_id);
3098 return access;
3101 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
3102 if (pos < 0) {
3103 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
3104 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
3105 pos = 0;
3106 } else
3107 isl_id_free(array_id);
3109 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
3110 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
3112 return access;
3115 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3116 * in "dim" by a value equal to the corresponding parameter.
3118 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
3119 __isl_take isl_space *dim)
3121 int i;
3123 if (!expr)
3124 goto error;
3126 for (i = 0; i < expr->n_arg; ++i) {
3127 expr->args[i] =
3128 expr_detect_parameter_accesses(expr->args[i],
3129 isl_space_copy(dim));
3130 if (!expr->args[i])
3131 goto error;
3134 if (expr->type == pet_expr_access) {
3135 expr->acc.access = access_detect_parameter(expr->acc.access,
3136 isl_space_copy(dim));
3137 expr->acc.index = index_detect_parameter(expr->acc.index,
3138 isl_space_copy(dim));
3139 if (!expr->acc.access || !expr->acc.index)
3140 goto error;
3143 isl_space_free(dim);
3144 return expr;
3145 error:
3146 isl_space_free(dim);
3147 return pet_expr_free(expr);
3150 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3151 * in "dim" by a value equal to the corresponding parameter.
3153 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
3154 __isl_take isl_space *dim)
3156 if (!stmt)
3157 goto error;
3159 stmt->body = expr_detect_parameter_accesses(stmt->body,
3160 isl_space_copy(dim));
3162 if (!stmt->domain || !stmt->schedule || !stmt->body)
3163 goto error;
3165 isl_space_free(dim);
3166 return stmt;
3167 error:
3168 isl_space_free(dim);
3169 return pet_stmt_free(stmt);
3172 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3173 * in "dim" by a value equal to the corresponding parameter.
3175 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
3176 __isl_take isl_space *dim)
3178 int i;
3180 if (!scop)
3181 goto error;
3183 for (i = 0; i < scop->n_stmt; ++i) {
3184 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
3185 isl_space_copy(dim));
3186 if (!scop->stmts[i])
3187 goto error;
3190 isl_space_free(dim);
3191 return scop;
3192 error:
3193 isl_space_free(dim);
3194 return pet_scop_free(scop);
3197 /* Replace all accesses to (0D) arrays that correspond to any of
3198 * the parameters used in "scop" by a value equal
3199 * to the corresponding parameter.
3201 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
3203 isl_space *dim;
3205 if (!scop)
3206 return NULL;
3208 dim = isl_set_get_space(scop->context);
3209 dim = scop_collect_params(scop, dim);
3211 scop = scop_detect_parameter_accesses(scop, dim);
3213 return scop;
3216 /* Return the relation mapping domain iterations to all possibly
3217 * accessed data elements.
3218 * In particular, take the access relation and project out the values
3219 * of the arguments, if any.
3221 static __isl_give isl_map *expr_access_get_may_access(struct pet_expr *expr)
3223 isl_map *access;
3224 isl_space *space;
3225 isl_map *map;
3227 if (!expr)
3228 return NULL;
3229 if (expr->type != pet_expr_access)
3230 return NULL;
3232 access = isl_map_copy(expr->acc.access);
3233 if (expr->n_arg == 0)
3234 return access;
3236 space = isl_space_domain(isl_map_get_space(access));
3237 map = isl_map_universe(isl_space_unwrap(space));
3238 map = isl_map_domain_map(map);
3239 access = isl_map_apply_domain(access, map);
3241 return access;
3244 /* Add all read access relations (if "read" is set) and/or all write
3245 * access relations (if "write" is set) to "accesses" and return the result.
3247 * If "must" is set, then we only add the accesses that are definitely
3248 * performed. Otherwise, we add all potential accesses.
3249 * In particular, if the access has any arguments, then if "must" is
3250 * set we currently skip the access completely. If "must" is not set,
3251 * we project out the values of the access arguments.
3253 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
3254 int read, int write, int must, __isl_take isl_union_map *accesses)
3256 int i;
3257 isl_id *id;
3258 isl_space *dim;
3260 if (!expr)
3261 return NULL;
3263 for (i = 0; i < expr->n_arg; ++i)
3264 accesses = expr_collect_accesses(expr->args[i],
3265 read, write, must, accesses);
3267 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
3268 ((read && expr->acc.read) || (write && expr->acc.write)) &&
3269 (!must || expr->n_arg == 0)) {
3270 isl_map *access;
3272 access = expr_access_get_may_access(expr);
3273 accesses = isl_union_map_add_map(accesses, access);
3276 return accesses;
3279 /* Collect and return all read access relations (if "read" is set)
3280 * and/or all write access relations (if "write" is set) in "stmt".
3282 * If "must" is set, then we only add the accesses that are definitely
3283 * performed. Otherwise, we add all potential accesses.
3284 * In particular, if the statement has any arguments, then if "must" is
3285 * set we currently skip the statement completely. If "must" is not set,
3286 * we project out the values of the statement arguments.
3288 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
3289 int read, int write, int must, __isl_take isl_space *dim)
3291 isl_union_map *accesses;
3292 isl_set *domain;
3294 if (!stmt)
3295 return NULL;
3297 accesses = isl_union_map_empty(dim);
3299 if (must && stmt->n_arg > 0)
3300 return accesses;
3302 domain = isl_set_copy(stmt->domain);
3303 if (isl_set_is_wrapping(domain))
3304 domain = isl_map_domain(isl_set_unwrap(domain));
3306 accesses = expr_collect_accesses(stmt->body,
3307 read, write, must, accesses);
3308 accesses = isl_union_map_intersect_domain(accesses,
3309 isl_union_set_from_set(domain));
3311 return accesses;
3314 /* Collect and return all read access relations (if "read" is set)
3315 * and/or all write access relations (if "write" is set) in "scop".
3316 * If "must" is set, then we only add the accesses that are definitely
3317 * performed. Otherwise, we add all potential accesses.
3319 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
3320 int read, int write, int must)
3322 int i;
3323 isl_union_map *accesses;
3324 isl_union_set *arrays;
3326 if (!scop)
3327 return NULL;
3329 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
3331 for (i = 0; i < scop->n_stmt; ++i) {
3332 isl_union_map *accesses_i;
3333 isl_space *dim = isl_set_get_space(scop->context);
3334 accesses_i = stmt_collect_accesses(scop->stmts[i],
3335 read, write, must, dim);
3336 accesses = isl_union_map_union(accesses, accesses_i);
3339 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
3340 for (i = 0; i < scop->n_array; ++i) {
3341 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
3342 arrays = isl_union_set_add_set(arrays, extent);
3344 accesses = isl_union_map_intersect_range(accesses, arrays);
3346 return accesses;
3349 /* Collect all potential read access relations.
3351 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
3353 return scop_collect_accesses(scop, 1, 0, 0);
3356 /* Collect all potential write access relations.
3358 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
3360 return scop_collect_accesses(scop, 0, 1, 0);
3363 /* Collect all definite write access relations.
3365 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
3367 return scop_collect_accesses(scop, 0, 1, 1);
3370 /* Collect and return the union of iteration domains in "scop".
3372 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
3374 int i;
3375 isl_set *domain_i;
3376 isl_union_set *domain;
3378 if (!scop)
3379 return NULL;
3381 domain = isl_union_set_empty(isl_set_get_space(scop->context));
3383 for (i = 0; i < scop->n_stmt; ++i) {
3384 domain_i = isl_set_copy(scop->stmts[i]->domain);
3385 domain = isl_union_set_add_set(domain, domain_i);
3388 return domain;
3391 /* Collect and return the schedules of the statements in "scop".
3392 * The range is normalized to the maximal number of scheduling
3393 * dimensions.
3395 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
3397 int i, j;
3398 isl_map *schedule_i;
3399 isl_union_map *schedule;
3400 int depth, max_depth = 0;
3402 if (!scop)
3403 return NULL;
3405 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
3407 for (i = 0; i < scop->n_stmt; ++i) {
3408 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
3409 if (depth > max_depth)
3410 max_depth = depth;
3413 for (i = 0; i < scop->n_stmt; ++i) {
3414 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
3415 depth = isl_map_dim(schedule_i, isl_dim_out);
3416 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
3417 max_depth - depth);
3418 for (j = depth; j < max_depth; ++j)
3419 schedule_i = isl_map_fix_si(schedule_i,
3420 isl_dim_out, j, 0);
3421 schedule = isl_union_map_add_map(schedule, schedule_i);
3424 return schedule;
3427 /* Does expression "expr" write to "id"?
3429 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
3431 int i;
3432 isl_id *write_id;
3434 for (i = 0; i < expr->n_arg; ++i) {
3435 int writes = expr_writes(expr->args[i], id);
3436 if (writes < 0 || writes)
3437 return writes;
3440 if (expr->type != pet_expr_access)
3441 return 0;
3442 if (!expr->acc.write)
3443 return 0;
3444 if (pet_expr_is_affine(expr))
3445 return 0;
3447 write_id = pet_expr_access_get_id(expr);
3448 isl_id_free(write_id);
3450 if (!write_id)
3451 return -1;
3453 return write_id == id;
3456 /* Does statement "stmt" write to "id"?
3458 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
3460 return expr_writes(stmt->body, id);
3463 /* Is there any write access in "scop" that accesses "id"?
3465 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
3467 int i;
3469 if (!scop)
3470 return -1;
3472 for (i = 0; i < scop->n_stmt; ++i) {
3473 int writes = stmt_writes(scop->stmts[i], id);
3474 if (writes < 0 || writes)
3475 return writes;
3478 return 0;
3481 /* Add a reference identifier to access expression "expr".
3482 * "user" points to an integer that contains the sequence number
3483 * of the next reference.
3485 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
3487 isl_ctx *ctx;
3488 char name[50];
3489 int *n_ref = user;
3491 if (!expr)
3492 return expr;
3494 ctx = isl_map_get_ctx(expr->acc.access);
3495 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
3496 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
3497 if (!expr->acc.ref_id)
3498 return pet_expr_free(expr);
3500 return expr;
3503 /* Add a reference identifier to all access expressions in "stmt".
3504 * "n_ref" points to an integer that contains the sequence number
3505 * of the next reference.
3507 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
3509 int i;
3511 if (!stmt)
3512 return NULL;
3514 for (i = 0; i < stmt->n_arg; ++i) {
3515 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3516 &access_add_ref_id, n_ref);
3517 if (!stmt->args[i])
3518 return pet_stmt_free(stmt);
3521 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
3522 if (!stmt->body)
3523 return pet_stmt_free(stmt);
3525 return stmt;
3528 /* Add a reference identifier to all access expressions in "scop".
3530 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
3532 int i;
3533 int n_ref;
3535 if (!scop)
3536 return NULL;
3538 n_ref = 0;
3539 for (i = 0; i < scop->n_stmt; ++i) {
3540 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
3541 if (!scop->stmts[i])
3542 return pet_scop_free(scop);
3545 return scop;
3548 /* Reset the user pointer on all parameter ids in "array".
3550 static struct pet_array *array_anonymize(struct pet_array *array)
3552 if (!array)
3553 return NULL;
3555 array->context = isl_set_reset_user(array->context);
3556 array->extent = isl_set_reset_user(array->extent);
3557 if (!array->context || !array->extent)
3558 return pet_array_free(array);
3560 return array;
3563 /* Reset the user pointer on all parameter and tuple ids in
3564 * the access relation and the index expressions
3565 * of the access expression "expr".
3567 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
3569 expr->acc.access = isl_map_reset_user(expr->acc.access);
3570 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
3571 if (!expr->acc.access || !expr->acc.index)
3572 return pet_expr_free(expr);
3574 return expr;
3577 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3579 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3581 int i;
3582 isl_space *space;
3583 isl_set *domain;
3585 if (!stmt)
3586 return NULL;
3588 stmt->domain = isl_set_reset_user(stmt->domain);
3589 stmt->schedule = isl_map_reset_user(stmt->schedule);
3590 if (!stmt->domain || !stmt->schedule)
3591 return pet_stmt_free(stmt);
3593 for (i = 0; i < stmt->n_arg; ++i) {
3594 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3595 &access_anonymize, NULL);
3596 if (!stmt->args[i])
3597 return pet_stmt_free(stmt);
3600 stmt->body = pet_expr_map_access(stmt->body,
3601 &access_anonymize, NULL);
3602 if (!stmt->body)
3603 return pet_stmt_free(stmt);
3605 return stmt;
3608 /* Reset the user pointer on the tuple ids and all parameter ids
3609 * in "implication".
3611 static struct pet_implication *implication_anonymize(
3612 struct pet_implication *implication)
3614 if (!implication)
3615 return NULL;
3617 implication->extension = isl_map_reset_user(implication->extension);
3618 if (!implication->extension)
3619 return pet_implication_free(implication);
3621 return implication;
3624 /* Reset the user pointer on all parameter and tuple ids in "scop".
3626 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
3628 int i;
3630 if (!scop)
3631 return NULL;
3633 scop->context = isl_set_reset_user(scop->context);
3634 scop->context_value = isl_set_reset_user(scop->context_value);
3635 if (!scop->context || !scop->context_value)
3636 return pet_scop_free(scop);
3638 for (i = 0; i < scop->n_array; ++i) {
3639 scop->arrays[i] = array_anonymize(scop->arrays[i]);
3640 if (!scop->arrays[i])
3641 return pet_scop_free(scop);
3644 for (i = 0; i < scop->n_stmt; ++i) {
3645 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
3646 if (!scop->stmts[i])
3647 return pet_scop_free(scop);
3650 for (i = 0; i < scop->n_implication; ++i) {
3651 scop->implications[i] =
3652 implication_anonymize(scop->implications[i]);
3653 if (!scop->implications[i])
3654 return pet_scop_free(scop);
3657 return scop;
3660 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
3661 * then intersect the range of "map" with the valid set of values.
3663 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
3664 struct pet_expr *arg, __isl_keep isl_union_map *value_bounds)
3666 isl_id *id;
3667 isl_map *vb;
3668 isl_space *space;
3669 isl_ctx *ctx = isl_map_get_ctx(map);
3671 id = pet_expr_access_get_id(arg);
3672 space = isl_space_alloc(ctx, 0, 0, 1);
3673 space = isl_space_set_tuple_id(space, isl_dim_in, id);
3674 vb = isl_union_map_extract_map(value_bounds, space);
3675 if (!isl_map_plain_is_empty(vb))
3676 map = isl_map_intersect_range(map, isl_map_range(vb));
3677 else
3678 isl_map_free(vb);
3680 return map;
3683 /* Given a set "domain", return a wrapped relation with the given set
3684 * as domain and a range of dimension "n_arg", where each coordinate
3685 * is either unbounded or, if the corresponding element of args is of
3686 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
3688 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
3689 unsigned n_arg, struct pet_expr **args,
3690 __isl_keep isl_union_map *value_bounds)
3692 int i;
3693 isl_map *map;
3694 isl_space *space;
3696 map = isl_map_from_domain(domain);
3697 space = isl_map_get_space(map);
3698 space = isl_space_add_dims(space, isl_dim_out, 1);
3700 for (i = 0; i < n_arg; ++i) {
3701 isl_map *map_i;
3702 struct pet_expr *arg = args[i];
3704 map_i = isl_map_universe(isl_space_copy(space));
3705 if (arg->type == pet_expr_access)
3706 map_i = access_apply_value_bounds(map_i, arg,
3707 value_bounds);
3708 map = isl_map_flat_range_product(map, map_i);
3710 isl_space_free(space);
3712 return isl_map_wrap(map);
3715 /* Data used in access_gist() callback.
3717 struct pet_access_gist_data {
3718 isl_set *domain;
3719 isl_union_map *value_bounds;
3722 /* Given an expression "expr" of type pet_expr_access, compute
3723 * the gist of the associated access relation and index expression
3724 * with respect to data->domain and the bounds on the values of the arguments
3725 * of the expression.
3727 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
3729 struct pet_access_gist_data *data = user;
3730 isl_set *domain;
3732 domain = isl_set_copy(data->domain);
3733 if (expr->n_arg > 0)
3734 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
3735 data->value_bounds);
3737 expr->acc.access = isl_map_gist_domain(expr->acc.access,
3738 isl_set_copy(domain));
3739 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
3740 if (!expr->acc.access || !expr->acc.index)
3741 return pet_expr_free(expr);
3743 return expr;
3746 /* Compute the gist of the iteration domain and all access relations
3747 * of "stmt" based on the constraints on the parameters specified by "context"
3748 * and the constraints on the values of nested accesses specified
3749 * by "value_bounds".
3751 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3752 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3754 int i;
3755 isl_space *space;
3756 isl_set *domain;
3757 struct pet_access_gist_data data;
3759 if (!stmt)
3760 return NULL;
3762 data.domain = isl_set_copy(stmt->domain);
3763 data.value_bounds = value_bounds;
3764 if (stmt->n_arg > 0)
3765 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3767 data.domain = isl_set_intersect_params(data.domain,
3768 isl_set_copy(context));
3770 for (i = 0; i < stmt->n_arg; ++i) {
3771 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3772 &access_gist, &data);
3773 if (!stmt->args[i])
3774 goto error;
3777 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
3778 if (!stmt->body)
3779 goto error;
3781 isl_set_free(data.domain);
3783 space = isl_set_get_space(stmt->domain);
3784 if (isl_space_is_wrapping(space))
3785 space = isl_space_domain(isl_space_unwrap(space));
3786 domain = isl_set_universe(space);
3787 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3788 if (stmt->n_arg > 0)
3789 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3790 value_bounds);
3791 stmt->domain = isl_set_gist(stmt->domain, domain);
3792 if (!stmt->domain)
3793 return pet_stmt_free(stmt);
3795 return stmt;
3796 error:
3797 isl_set_free(data.domain);
3798 return pet_stmt_free(stmt);
3801 /* Compute the gist of the extent of the array
3802 * based on the constraints on the parameters specified by "context".
3804 static struct pet_array *array_gist(struct pet_array *array,
3805 __isl_keep isl_set *context)
3807 if (!array)
3808 return NULL;
3810 array->extent = isl_set_gist_params(array->extent,
3811 isl_set_copy(context));
3812 if (!array->extent)
3813 return pet_array_free(array);
3815 return array;
3818 /* Compute the gist of all sets and relations in "scop"
3819 * based on the constraints on the parameters specified by "scop->context"
3820 * and the constraints on the values of nested accesses specified
3821 * by "value_bounds".
3823 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3824 __isl_keep isl_union_map *value_bounds)
3826 int i;
3828 if (!scop)
3829 return NULL;
3831 scop->context = isl_set_coalesce(scop->context);
3832 if (!scop->context)
3833 return pet_scop_free(scop);
3835 for (i = 0; i < scop->n_array; ++i) {
3836 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3837 if (!scop->arrays[i])
3838 return pet_scop_free(scop);
3841 for (i = 0; i < scop->n_stmt; ++i) {
3842 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3843 value_bounds);
3844 if (!scop->stmts[i])
3845 return pet_scop_free(scop);
3848 return scop;
3851 /* Intersect the context of "scop" with "context".
3852 * To ensure that we don't introduce any unnamed parameters in
3853 * the context of "scop", we first remove the unnamed parameters
3854 * from "context".
3856 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3857 __isl_take isl_set *context)
3859 if (!scop)
3860 goto error;
3862 context = set_project_out_unnamed_params(context);
3863 scop->context = isl_set_intersect(scop->context, context);
3864 if (!scop->context)
3865 return pet_scop_free(scop);
3867 return scop;
3868 error:
3869 isl_set_free(context);
3870 return pet_scop_free(scop);
3873 /* Drop the current context of "scop". That is, replace the context
3874 * by a universal set.
3876 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3878 isl_space *space;
3880 if (!scop)
3881 return NULL;
3883 space = isl_set_get_space(scop->context);
3884 isl_set_free(scop->context);
3885 scop->context = isl_set_universe(space);
3886 if (!scop->context)
3887 return pet_scop_free(scop);
3889 return scop;
3892 /* Append "array" to the arrays of "scop".
3894 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3895 struct pet_array *array)
3897 isl_ctx *ctx;
3898 struct pet_array **arrays;
3900 if (!array || !scop)
3901 goto error;
3903 ctx = isl_set_get_ctx(scop->context);
3904 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3905 scop->n_array + 1);
3906 if (!arrays)
3907 goto error;
3908 scop->arrays = arrays;
3909 scop->arrays[scop->n_array] = array;
3910 scop->n_array++;
3912 return scop;
3913 error:
3914 pet_array_free(array);
3915 return pet_scop_free(scop);
3918 /* Create and return an implication on filter values equal to "satisfied"
3919 * with extension "map".
3921 static struct pet_implication *new_implication(__isl_take isl_map *map,
3922 int satisfied)
3924 isl_ctx *ctx;
3925 struct pet_implication *implication;
3927 if (!map)
3928 return NULL;
3929 ctx = isl_map_get_ctx(map);
3930 implication = isl_alloc_type(ctx, struct pet_implication);
3931 if (!implication)
3932 goto error;
3934 implication->extension = map;
3935 implication->satisfied = satisfied;
3937 return implication;
3938 error:
3939 isl_map_free(map);
3940 return NULL;
3943 /* Add an implication on filter values equal to "satisfied"
3944 * with extension "map" to "scop".
3946 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3947 __isl_take isl_map *map, int satisfied)
3949 isl_ctx *ctx;
3950 struct pet_implication *implication;
3951 struct pet_implication **implications;
3953 implication = new_implication(map, satisfied);
3954 if (!scop || !implication)
3955 goto error;
3957 ctx = isl_set_get_ctx(scop->context);
3958 implications = isl_realloc_array(ctx, scop->implications,
3959 struct pet_implication *,
3960 scop->n_implication + 1);
3961 if (!implications)
3962 goto error;
3963 scop->implications = implications;
3964 scop->implications[scop->n_implication] = implication;
3965 scop->n_implication++;
3967 return scop;
3968 error:
3969 pet_implication_free(implication);
3970 return pet_scop_free(scop);
3973 /* Given an access expression, check if it is data dependent.
3974 * If so, set *found and abort the search.
3976 static int is_data_dependent(struct pet_expr *expr, void *user)
3978 int *found = user;
3980 if (expr->n_arg) {
3981 *found = 1;
3982 return -1;
3985 return 0;
3988 /* Does "scop" contain any data dependent accesses?
3990 * Check the body of each statement for such accesses.
3992 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3994 int i;
3995 int found = 0;
3997 if (!scop)
3998 return -1;
4000 for (i = 0; i < scop->n_stmt; ++i) {
4001 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
4002 &is_data_dependent, &found);
4003 if (r < 0 && !found)
4004 return -1;
4005 if (found)
4006 return found;
4009 return found;
4012 /* Does "scop" contain and data dependent conditions?
4014 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
4016 int i;
4018 if (!scop)
4019 return -1;
4021 for (i = 0; i < scop->n_stmt; ++i)
4022 if (scop->stmts[i]->n_arg > 0)
4023 return 1;
4025 return 0;
4028 /* Keep track of the "input" file inside the (extended) "scop".
4030 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
4032 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4034 if (!scop)
4035 return NULL;
4037 ext->input = input;
4039 return scop;
4042 /* Print the original code corresponding to "scop" to printer "p".
4044 * pet_scop_print_original can only be called from
4045 * a pet_transform_C_source callback. This means that the input
4046 * file is stored in the extended scop and that the printer prints
4047 * to a file.
4049 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
4050 __isl_take isl_printer *p)
4052 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4053 FILE *output;
4055 if (!scop || !p)
4056 return isl_printer_free(p);
4058 if (!ext->input)
4059 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
4060 "no input file stored in scop",
4061 return isl_printer_free(p));
4063 output = isl_printer_get_file(p);
4064 if (!output)
4065 return isl_printer_free(p);
4067 if (copy(ext->input, output, scop->start, scop->end) < 0)
4068 return isl_printer_free(p);
4070 return p;