PetScan::resolve_nested(struct pet_expr *): remove unused label
[pet.git] / scop.c
blob25ebe11b06327989d321060ab4fc5c22f64e3a30
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 void *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 return pet_expr_free(expr);
804 void *pet_stmt_free(struct pet_stmt *stmt)
806 int i;
808 if (!stmt)
809 return NULL;
811 isl_set_free(stmt->domain);
812 isl_map_free(stmt->schedule);
813 pet_expr_free(stmt->body);
815 for (i = 0; i < stmt->n_arg; ++i)
816 pet_expr_free(stmt->args[i]);
817 free(stmt->args);
819 free(stmt);
820 return NULL;
823 static void stmt_dump(struct pet_stmt *stmt, int indent)
825 int i;
827 if (!stmt)
828 return;
830 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
831 fprintf(stderr, "%*s", indent, "");
832 isl_set_dump(stmt->domain);
833 fprintf(stderr, "%*s", indent, "");
834 isl_map_dump(stmt->schedule);
835 expr_dump(stmt->body, indent);
836 for (i = 0; i < stmt->n_arg; ++i)
837 expr_dump(stmt->args[i], indent + 2);
840 void pet_stmt_dump(struct pet_stmt *stmt)
842 stmt_dump(stmt, 0);
845 struct pet_array *pet_array_free(struct pet_array *array)
847 if (!array)
848 return NULL;
850 isl_set_free(array->context);
851 isl_set_free(array->extent);
852 isl_set_free(array->value_bounds);
853 free(array->element_type);
855 free(array);
856 return NULL;
859 void pet_array_dump(struct pet_array *array)
861 if (!array)
862 return;
864 isl_set_dump(array->context);
865 isl_set_dump(array->extent);
866 isl_set_dump(array->value_bounds);
867 fprintf(stderr, "%s %s\n", array->element_type,
868 array->live_out ? "live-out" : "");
871 /* Alloc a pet_scop structure, with extra room for information that
872 * is only used during parsing.
874 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
876 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
879 /* Construct a pet_scop with room for n statements.
881 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
883 isl_space *space;
884 struct pet_scop *scop;
886 scop = pet_scop_alloc(ctx);
887 if (!scop)
888 return NULL;
890 space = isl_space_params_alloc(ctx, 0);
891 scop->context = isl_set_universe(isl_space_copy(space));
892 scop->context_value = isl_set_universe(space);
893 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
894 if (!scop->context || !scop->stmts)
895 return pet_scop_free(scop);
897 scop->n_stmt = n;
899 return scop;
902 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
904 return scop_alloc(ctx, 0);
907 /* Update "context" with respect to the valid parameter values for "access".
909 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
910 __isl_take isl_set *context)
912 context = isl_set_intersect(context,
913 isl_map_params(isl_map_copy(access)));
914 return context;
917 /* Update "context" with respect to the valid parameter values for "expr".
919 * If "expr" represents a ternary operator, then a parameter value
920 * needs to be valid for the condition and for at least one of the
921 * remaining two arguments.
922 * If the condition is an affine expression, then we can be a bit more specific.
923 * The parameter then has to be valid for the second argument for
924 * non-zero accesses and valid for the third argument for zero accesses.
926 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
927 __isl_take isl_set *context)
929 int i;
931 if (expr->type == pet_expr_ternary) {
932 int is_aff;
933 isl_set *context1, *context2;
935 is_aff = pet_expr_is_affine(expr->args[0]);
936 if (is_aff < 0)
937 goto error;
939 context = expr_extract_context(expr->args[0], context);
940 context1 = expr_extract_context(expr->args[1],
941 isl_set_copy(context));
942 context2 = expr_extract_context(expr->args[2], context);
944 if (is_aff) {
945 isl_map *access;
946 isl_set *zero_set;
948 access = isl_map_copy(expr->args[0]->acc.access);
949 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
950 zero_set = isl_map_params(access);
951 context1 = isl_set_subtract(context1,
952 isl_set_copy(zero_set));
953 context2 = isl_set_intersect(context2, zero_set);
956 context = isl_set_union(context1, context2);
957 context = isl_set_coalesce(context);
959 return context;
962 for (i = 0; i < expr->n_arg; ++i)
963 context = expr_extract_context(expr->args[i], context);
965 if (expr->type == pet_expr_access)
966 context = access_extract_context(expr->acc.access, context);
968 return context;
969 error:
970 isl_set_free(context);
971 return NULL;
974 /* Update "context" with respect to the valid parameter values for "stmt".
976 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
977 __isl_take isl_set *context)
979 int i;
981 for (i = 0; i < stmt->n_arg; ++i)
982 context = expr_extract_context(stmt->args[i], context);
984 context = expr_extract_context(stmt->body, context);
986 return context;
989 /* Construct a pet_scop that contains the given pet_stmt.
991 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
993 struct pet_scop *scop;
995 if (!stmt)
996 return NULL;
998 scop = scop_alloc(ctx, 1);
999 if (!scop)
1000 goto error;
1002 scop->context = stmt_extract_context(stmt, scop->context);
1003 if (!scop->context)
1004 goto error;
1006 scop->stmts[0] = stmt;
1008 return scop;
1009 error:
1010 pet_stmt_free(stmt);
1011 pet_scop_free(scop);
1012 return NULL;
1015 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
1016 * does it represent an affine expression?
1018 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
1020 int has_id;
1022 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
1023 if (has_id < 0)
1024 return -1;
1026 return !has_id;
1029 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
1031 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
1032 __isl_take isl_set *dom)
1034 isl_pw_aff *pa;
1035 pa = isl_set_indicator_function(set);
1036 pa = isl_pw_aff_intersect_domain(pa, dom);
1037 return pa;
1040 /* Return "lhs || rhs", defined on the shared definition domain.
1042 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
1043 __isl_take isl_pw_aff *rhs)
1045 isl_set *cond;
1046 isl_set *dom;
1048 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
1049 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
1050 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
1051 isl_pw_aff_non_zero_set(rhs));
1052 cond = isl_set_coalesce(cond);
1053 return indicator_function(cond, dom);
1056 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
1057 * ext may be equal to either ext1 or ext2.
1059 * The two skips that need to be combined are assumed to be affine expressions.
1061 * We need to skip in ext if we need to skip in either ext1 or ext2.
1062 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
1064 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
1065 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
1066 enum pet_skip type)
1068 isl_pw_aff *skip, *skip1, *skip2;
1070 if (!ext)
1071 return NULL;
1072 if (!ext1->skip[type] && !ext2->skip[type])
1073 return ext;
1074 if (!ext1->skip[type]) {
1075 if (ext == ext2)
1076 return ext;
1077 ext->skip[type] = ext2->skip[type];
1078 ext2->skip[type] = NULL;
1079 return ext;
1081 if (!ext2->skip[type]) {
1082 if (ext == ext1)
1083 return ext;
1084 ext->skip[type] = ext1->skip[type];
1085 ext1->skip[type] = NULL;
1086 return ext;
1089 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
1090 !multi_pw_aff_is_affine(ext2->skip[type]))
1091 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
1092 isl_error_internal, "can only combine affine skips",
1093 goto error);
1095 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
1096 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
1097 skip = pw_aff_or(skip1, skip2);
1098 isl_multi_pw_aff_free(ext1->skip[type]);
1099 ext1->skip[type] = NULL;
1100 isl_multi_pw_aff_free(ext2->skip[type]);
1101 ext2->skip[type] = NULL;
1102 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1103 if (!ext->skip[type])
1104 goto error;
1106 return ext;
1107 error:
1108 pet_scop_free(&ext->scop);
1109 return NULL;
1112 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
1113 * where type takes on the values pet_skip_now and pet_skip_later.
1114 * scop may be equal to either scop1 or scop2.
1116 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
1117 struct pet_scop *scop1, struct pet_scop *scop2)
1119 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1120 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
1121 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
1123 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
1124 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
1125 return &ext->scop;
1128 /* Update scop->start and scop->end to include the region from "start"
1129 * to "end". In particular, if scop->end == 0, then "scop" does not
1130 * have any offset information yet and we simply take the information
1131 * from "start" and "end". Otherwise, we update the fields if the
1132 * region from "start" to "end" is not already included.
1134 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
1135 unsigned start, unsigned end)
1137 if (!scop)
1138 return NULL;
1139 if (scop->end == 0) {
1140 scop->start = start;
1141 scop->end = end;
1142 } else {
1143 if (start < scop->start)
1144 scop->start = start;
1145 if (end > scop->end)
1146 scop->end = end;
1149 return scop;
1152 /* Does "implication" appear in the list of implications of "scop"?
1154 static int is_known_implication(struct pet_scop *scop,
1155 struct pet_implication *implication)
1157 int i;
1159 for (i = 0; i < scop->n_implication; ++i) {
1160 struct pet_implication *pi = scop->implications[i];
1161 int equal;
1163 if (pi->satisfied != implication->satisfied)
1164 continue;
1165 equal = isl_map_is_equal(pi->extension, implication->extension);
1166 if (equal < 0)
1167 return -1;
1168 if (equal)
1169 return 1;
1172 return 0;
1175 /* Store the concatenation of the impliciations of "scop1" and "scop2"
1176 * in "scop", removing duplicates (i.e., implications in "scop2" that
1177 * already appear in "scop1").
1179 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
1180 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
1182 int i, j;
1184 if (!scop)
1185 return NULL;
1187 if (scop2->n_implication == 0) {
1188 scop->n_implication = scop1->n_implication;
1189 scop->implications = scop1->implications;
1190 scop1->n_implication = 0;
1191 scop1->implications = NULL;
1192 return scop;
1195 if (scop1->n_implication == 0) {
1196 scop->n_implication = scop2->n_implication;
1197 scop->implications = scop2->implications;
1198 scop2->n_implication = 0;
1199 scop2->implications = NULL;
1200 return scop;
1203 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1204 scop1->n_implication + scop2->n_implication);
1205 if (!scop->implications)
1206 return pet_scop_free(scop);
1208 for (i = 0; i < scop1->n_implication; ++i) {
1209 scop->implications[i] = scop1->implications[i];
1210 scop1->implications[i] = NULL;
1213 scop->n_implication = scop1->n_implication;
1214 j = scop1->n_implication;
1215 for (i = 0; i < scop2->n_implication; ++i) {
1216 int known;
1218 known = is_known_implication(scop, scop2->implications[i]);
1219 if (known < 0)
1220 return pet_scop_free(scop);
1221 if (known)
1222 continue;
1223 scop->implications[j++] = scop2->implications[i];
1224 scop2->implications[i] = NULL;
1226 scop->n_implication = j;
1228 return scop;
1231 /* Combine the offset information of "scop1" and "scop2" into "scop".
1233 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
1234 struct pet_scop *scop1, struct pet_scop *scop2)
1236 if (scop1->end)
1237 scop = pet_scop_update_start_end(scop,
1238 scop1->start, scop1->end);
1239 if (scop2->end)
1240 scop = pet_scop_update_start_end(scop,
1241 scop2->start, scop2->end);
1242 return scop;
1245 /* Construct a pet_scop that contains the offset information,
1246 * arrays, statements and skip information in "scop1" and "scop2".
1248 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1249 struct pet_scop *scop2)
1251 int i;
1252 struct pet_scop *scop = NULL;
1254 if (!scop1 || !scop2)
1255 goto error;
1257 if (scop1->n_stmt == 0) {
1258 scop2 = scop_combine_skips(scop2, scop1, scop2);
1259 pet_scop_free(scop1);
1260 return scop2;
1263 if (scop2->n_stmt == 0) {
1264 scop1 = scop_combine_skips(scop1, scop1, scop2);
1265 pet_scop_free(scop2);
1266 return scop1;
1269 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1270 if (!scop)
1271 goto error;
1273 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1274 scop1->n_array + scop2->n_array);
1275 if (!scop->arrays)
1276 goto error;
1277 scop->n_array = scop1->n_array + scop2->n_array;
1279 for (i = 0; i < scop1->n_stmt; ++i) {
1280 scop->stmts[i] = scop1->stmts[i];
1281 scop1->stmts[i] = NULL;
1284 for (i = 0; i < scop2->n_stmt; ++i) {
1285 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1286 scop2->stmts[i] = NULL;
1289 for (i = 0; i < scop1->n_array; ++i) {
1290 scop->arrays[i] = scop1->arrays[i];
1291 scop1->arrays[i] = NULL;
1294 for (i = 0; i < scop2->n_array; ++i) {
1295 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1296 scop2->arrays[i] = NULL;
1299 scop = scop_collect_implications(ctx, scop, scop1, scop2);
1300 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1301 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1302 scop = scop_combine_skips(scop, scop1, scop2);
1303 scop = scop_combine_start_end(scop, scop1, scop2);
1305 pet_scop_free(scop1);
1306 pet_scop_free(scop2);
1307 return scop;
1308 error:
1309 pet_scop_free(scop1);
1310 pet_scop_free(scop2);
1311 pet_scop_free(scop);
1312 return NULL;
1315 /* Apply the skip condition "skip" to "scop".
1316 * That is, make sure "scop" is not executed when the condition holds.
1318 * If "skip" is an affine expression, we add the conditions under
1319 * which the expression is zero to the iteration domains.
1320 * Otherwise, we add a filter on the variable attaining the value zero.
1322 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1323 __isl_take isl_multi_pw_aff *skip)
1325 isl_set *zero;
1326 isl_pw_aff *pa;
1327 int is_aff;
1329 if (!scop || !skip)
1330 goto error;
1332 is_aff = multi_pw_aff_is_affine(skip);
1333 if (is_aff < 0)
1334 goto error;
1336 if (!is_aff)
1337 return pet_scop_filter(scop, skip, 0);
1339 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1340 isl_multi_pw_aff_free(skip);
1341 zero = isl_set_params(isl_pw_aff_zero_set(pa));
1342 scop = pet_scop_restrict(scop, zero);
1344 return scop;
1345 error:
1346 isl_multi_pw_aff_free(skip);
1347 return pet_scop_free(scop);
1350 /* Construct a pet_scop that contains the arrays, statements and
1351 * skip information in "scop1" and "scop2", where the two scops
1352 * are executed "in sequence". That is, breaks and continues
1353 * in scop1 have an effect on scop2.
1355 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1356 struct pet_scop *scop2)
1358 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1359 scop2 = restrict_skip(scop2,
1360 pet_scop_get_skip(scop1, pet_skip_now));
1361 return pet_scop_add(ctx, scop1, scop2);
1364 /* Construct a pet_scop that contains the arrays, statements and
1365 * skip information in "scop1" and "scop2", where the two scops
1366 * are executed "in parallel". That is, any break or continue
1367 * in scop1 has no effect on scop2.
1369 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1370 struct pet_scop *scop2)
1372 return pet_scop_add(ctx, scop1, scop2);
1375 void *pet_implication_free(struct pet_implication *implication)
1377 int i;
1379 if (!implication)
1380 return NULL;
1382 isl_map_free(implication->extension);
1384 free(implication);
1385 return NULL;
1388 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1390 int i;
1391 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1393 if (!scop)
1394 return NULL;
1395 isl_set_free(scop->context);
1396 isl_set_free(scop->context_value);
1397 if (scop->arrays)
1398 for (i = 0; i < scop->n_array; ++i)
1399 pet_array_free(scop->arrays[i]);
1400 free(scop->arrays);
1401 if (scop->stmts)
1402 for (i = 0; i < scop->n_stmt; ++i)
1403 pet_stmt_free(scop->stmts[i]);
1404 free(scop->stmts);
1405 if (scop->implications)
1406 for (i = 0; i < scop->n_implication; ++i)
1407 pet_implication_free(scop->implications[i]);
1408 free(scop->implications);
1409 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1410 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1411 free(scop);
1412 return NULL;
1415 void pet_implication_dump(struct pet_implication *implication)
1417 if (!implication)
1418 return;
1420 fprintf(stderr, "%d\n", implication->satisfied);
1421 isl_map_dump(implication->extension);
1424 void pet_scop_dump(struct pet_scop *scop)
1426 int i;
1427 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1429 if (!scop)
1430 return;
1432 isl_set_dump(scop->context);
1433 isl_set_dump(scop->context_value);
1434 for (i = 0; i < scop->n_array; ++i)
1435 pet_array_dump(scop->arrays[i]);
1436 for (i = 0; i < scop->n_stmt; ++i)
1437 pet_stmt_dump(scop->stmts[i]);
1438 for (i = 0; i < scop->n_implication; ++i)
1439 pet_implication_dump(scop->implications[i]);
1441 if (ext->skip[0]) {
1442 fprintf(stderr, "skip\n");
1443 isl_multi_pw_aff_dump(ext->skip[0]);
1444 isl_multi_pw_aff_dump(ext->skip[1]);
1448 /* Return 1 if the two pet_arrays are equivalent.
1450 * We don't compare element_size as this may be target dependent.
1452 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1454 if (!array1 || !array2)
1455 return 0;
1457 if (!isl_set_is_equal(array1->context, array2->context))
1458 return 0;
1459 if (!isl_set_is_equal(array1->extent, array2->extent))
1460 return 0;
1461 if (!!array1->value_bounds != !!array2->value_bounds)
1462 return 0;
1463 if (array1->value_bounds &&
1464 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1465 return 0;
1466 if (strcmp(array1->element_type, array2->element_type))
1467 return 0;
1468 if (array1->live_out != array2->live_out)
1469 return 0;
1470 if (array1->uniquely_defined != array2->uniquely_defined)
1471 return 0;
1472 if (array1->declared != array2->declared)
1473 return 0;
1474 if (array1->exposed != array2->exposed)
1475 return 0;
1477 return 1;
1480 /* Return 1 if the two pet_stmts are equivalent.
1482 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1484 int i;
1486 if (!stmt1 || !stmt2)
1487 return 0;
1489 if (stmt1->line != stmt2->line)
1490 return 0;
1491 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1492 return 0;
1493 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1494 return 0;
1495 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1496 return 0;
1497 if (stmt1->n_arg != stmt2->n_arg)
1498 return 0;
1499 for (i = 0; i < stmt1->n_arg; ++i) {
1500 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1501 return 0;
1504 return 1;
1507 /* Return 1 if the two pet_implications are equivalent.
1509 int pet_implication_is_equal(struct pet_implication *implication1,
1510 struct pet_implication *implication2)
1512 if (!implication1 || !implication2)
1513 return 0;
1515 if (implication1->satisfied != implication2->satisfied)
1516 return 0;
1517 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1518 return 0;
1520 return 1;
1523 /* Return 1 if the two pet_scops are equivalent.
1525 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1527 int i;
1529 if (!scop1 || !scop2)
1530 return 0;
1532 if (!isl_set_is_equal(scop1->context, scop2->context))
1533 return 0;
1534 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1535 return 0;
1537 if (scop1->n_array != scop2->n_array)
1538 return 0;
1539 for (i = 0; i < scop1->n_array; ++i)
1540 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1541 return 0;
1543 if (scop1->n_stmt != scop2->n_stmt)
1544 return 0;
1545 for (i = 0; i < scop1->n_stmt; ++i)
1546 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1547 return 0;
1549 if (scop1->n_implication != scop2->n_implication)
1550 return 0;
1551 for (i = 0; i < scop1->n_implication; ++i)
1552 if (!pet_implication_is_equal(scop1->implications[i],
1553 scop2->implications[i]))
1554 return 0;
1556 return 1;
1559 /* Prefix the schedule of "stmt" with an extra dimension with constant
1560 * value "pos".
1562 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1564 if (!stmt)
1565 return NULL;
1567 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1568 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1569 if (!stmt->schedule)
1570 return pet_stmt_free(stmt);
1572 return stmt;
1575 /* Prefix the schedules of all statements in "scop" with an extra
1576 * dimension with constant value "pos".
1578 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1580 int i;
1582 if (!scop)
1583 return NULL;
1585 for (i = 0; i < scop->n_stmt; ++i) {
1586 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1587 if (!scop->stmts[i])
1588 return pet_scop_free(scop);
1591 return scop;
1594 /* Given a set with a parameter at "param_pos" that refers to the
1595 * iterator, "move" the iterator to the first set dimension.
1596 * That is, essentially equate the parameter to the first set dimension
1597 * and then project it out.
1599 * The first set dimension may however refer to a virtual iterator,
1600 * while the parameter refers to the "real" iterator.
1601 * We therefore need to take into account the affine expression "iv_map", which
1602 * expresses the real iterator in terms of the virtual iterator.
1603 * In particular, we equate the set dimension to the input of the map
1604 * and the parameter to the output of the map and then project out
1605 * everything we don't need anymore.
1607 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1608 int param_pos, __isl_take isl_aff *iv_map)
1610 isl_map *map, *map2;
1611 map = isl_map_from_domain(set);
1612 map = isl_map_add_dims(map, isl_dim_out, 1);
1613 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1614 map2 = isl_map_from_aff(iv_map);
1615 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1616 map = isl_map_apply_range(map, map2);
1617 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1618 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1619 return isl_map_domain(map);
1622 /* Data used in embed_access.
1623 * extend adds an iterator to the iteration domain (through precomposition).
1624 * iv_map expresses the real iterator in terms of the virtual iterator
1625 * var_id represents the induction variable of the corresponding loop
1627 struct pet_embed_access {
1628 isl_multi_pw_aff *extend;
1629 isl_aff *iv_map;
1630 isl_id *var_id;
1633 /* Given an index expression, return an expression for the outer iterator.
1635 static __isl_give isl_aff *index_outer_iterator(
1636 __isl_take isl_multi_pw_aff *index)
1638 isl_space *space;
1639 isl_local_space *ls;
1641 space = isl_multi_pw_aff_get_domain_space(index);
1642 isl_multi_pw_aff_free(index);
1644 ls = isl_local_space_from_space(space);
1645 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1648 /* Replace an index expression that references the new (outer) iterator variable
1649 * by one that references the corresponding (real) iterator.
1651 * The input index expression is of the form
1653 * { S[i',...] -> i[] }
1655 * where i' refers to the virtual iterator.
1657 * iv_map is of the form
1659 * { [i'] -> [i] }
1661 * Return the index expression
1663 * { S[i',...] -> [i] }
1665 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1666 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1668 isl_space *space;
1669 isl_aff *aff;
1671 aff = index_outer_iterator(index);
1672 space = isl_aff_get_space(aff);
1673 iv_map = isl_aff_align_params(iv_map, space);
1674 aff = isl_aff_pullback_aff(iv_map, aff);
1676 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1679 /* Given an index expression "index" that refers to the (real) iterator
1680 * through the parameter at position "pos", plug in "iv_map", expressing
1681 * the real iterator in terms of the virtual (outer) iterator.
1683 * In particular, the index expression is of the form
1685 * [..., i, ...] -> { S[i',...] -> ... i ... }
1687 * where i refers to the real iterator and i' refers to the virtual iterator.
1689 * iv_map is of the form
1691 * { [i'] -> [i] }
1693 * Return the index expression
1695 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1698 * We first move the parameter to the input
1700 * [..., ...] -> { [i, i',...] -> ... i ... }
1702 * and construct
1704 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1706 * and then combine the two to obtain the desired result.
1708 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1709 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1711 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1712 isl_multi_aff *ma;
1714 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1715 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1716 isl_dim_param, pos, 1);
1718 space = isl_space_map_from_set(space);
1719 ma = isl_multi_aff_identity(isl_space_copy(space));
1720 iv_map = isl_aff_align_params(iv_map, space);
1721 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1722 ma = isl_multi_aff_flat_range_product(
1723 isl_multi_aff_from_aff(iv_map), ma);
1724 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1726 return index;
1729 /* Embed the given index expression in an extra outer loop.
1730 * The domain of the index expression has already been updated.
1732 * If the access refers to the induction variable, then it is
1733 * turned into an access to the set of integers with index (and value)
1734 * equal to the induction variable.
1736 * If the accessed array is a virtual array (with user
1737 * pointer equal to NULL), as created by create_test_index,
1738 * then it is extended along with the domain of the index expression.
1740 static __isl_give isl_multi_pw_aff *embed_index_expression(
1741 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1743 isl_id *array_id = NULL;
1744 int pos;
1746 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1747 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1748 if (array_id == data->var_id) {
1749 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1750 } else if (array_id && !isl_id_get_user(array_id)) {
1751 isl_aff *aff;
1752 isl_multi_pw_aff *mpa;
1754 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1755 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1756 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1757 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1758 isl_id_copy(array_id));
1760 isl_id_free(array_id);
1762 pos = isl_multi_pw_aff_find_dim_by_id(index,
1763 isl_dim_param, data->var_id);
1764 if (pos >= 0)
1765 index = index_internalize_iv(index, pos,
1766 isl_aff_copy(data->iv_map));
1767 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1768 isl_id_copy(data->var_id));
1770 return index;
1773 /* Embed the given access relation in an extra outer loop.
1774 * The domain of the access relation has already been updated.
1776 * If the access refers to the induction variable, then it is
1777 * turned into an access to the set of integers with index (and value)
1778 * equal to the induction variable.
1780 * If the induction variable appears in the constraints (as a parameter),
1781 * then the parameter is equated to the newly introduced iteration
1782 * domain dimension and subsequently projected out.
1784 * Similarly, if the accessed array is a virtual array (with user
1785 * pointer equal to NULL), as created by create_test_index,
1786 * then it is extended along with the domain of the access.
1788 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
1789 struct pet_embed_access *data)
1791 isl_id *array_id = NULL;
1792 int pos;
1794 if (isl_map_has_tuple_id(access, isl_dim_out))
1795 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1796 if (array_id == data->var_id ||
1797 (array_id && !isl_id_get_user(array_id))) {
1798 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1799 access = isl_map_equate(access,
1800 isl_dim_in, 0, isl_dim_out, 0);
1801 if (array_id == data->var_id)
1802 access = isl_map_apply_range(access,
1803 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1804 else
1805 access = isl_map_set_tuple_id(access, isl_dim_out,
1806 isl_id_copy(array_id));
1808 isl_id_free(array_id);
1810 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1811 if (pos >= 0) {
1812 isl_set *set = isl_map_wrap(access);
1813 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1814 access = isl_set_unwrap(set);
1816 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1817 isl_id_copy(data->var_id));
1819 return access;
1822 /* Given an access expression, embed the associated access relation and
1823 * index expression in an extra outer loop.
1825 * We first update the domains to insert the extra dimension and
1826 * then update the access relation and index expression to take
1827 * into account the mapping "iv_map" from virtual iterator
1828 * to real iterator.
1830 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
1832 int dim;
1833 struct pet_embed_access *data = user;
1835 expr = update_domain(expr, data->extend);
1836 if (!expr)
1837 return NULL;
1839 expr->acc.access = embed_access_relation(expr->acc.access, data);
1840 expr->acc.index = embed_index_expression(expr->acc.index, data);
1841 if (!expr->acc.access || !expr->acc.index)
1842 return pet_expr_free(expr);
1844 return expr;
1847 /* Embed all access subexpressions of "expr" in an extra loop.
1848 * "extend" inserts an outer loop iterator in the iteration domains
1849 * (through precomposition).
1850 * "iv_map" expresses the real iterator in terms of the virtual iterator
1851 * "var_id" represents the induction variable.
1853 static struct pet_expr *expr_embed(struct pet_expr *expr,
1854 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
1855 __isl_keep isl_id *var_id)
1857 struct pet_embed_access data =
1858 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1860 expr = pet_expr_map_access(expr, &embed_access, &data);
1861 isl_aff_free(iv_map);
1862 isl_multi_pw_aff_free(extend);
1863 return expr;
1866 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1867 * "dom" and schedule "sched". "var_id" represents the induction variable
1868 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1869 * That is, it expresses the iterator that some of the parameters in "stmt"
1870 * may refer to in terms of the iterator used in "dom" and
1871 * the domain of "sched".
1873 * The iteration domain and schedule of the statement are updated
1874 * according to the iteration domain and schedule of the new loop.
1875 * If stmt->domain is a wrapped map, then the iteration domain
1876 * is the domain of this map, so we need to be careful to adjust
1877 * this domain.
1879 * If the induction variable appears in the constraints (as a parameter)
1880 * of the current iteration domain or the schedule of the statement,
1881 * then the parameter is equated to the newly introduced iteration
1882 * domain dimension and subsequently projected out.
1884 * Finally, all access relations are updated based on the extra loop.
1886 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1887 __isl_take isl_set *dom, __isl_take isl_map *sched,
1888 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1890 int i;
1891 int pos;
1892 isl_id *stmt_id;
1893 isl_space *dim;
1894 isl_multi_pw_aff *extend;
1896 if (!stmt)
1897 goto error;
1899 if (isl_set_is_wrapping(stmt->domain)) {
1900 isl_map *map;
1901 isl_map *ext;
1902 isl_space *ran_dim;
1904 map = isl_set_unwrap(stmt->domain);
1905 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1906 ran_dim = isl_space_range(isl_map_get_space(map));
1907 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1908 isl_set_universe(ran_dim));
1909 map = isl_map_flat_domain_product(ext, map);
1910 map = isl_map_set_tuple_id(map, isl_dim_in,
1911 isl_id_copy(stmt_id));
1912 dim = isl_space_domain(isl_map_get_space(map));
1913 stmt->domain = isl_map_wrap(map);
1914 } else {
1915 stmt_id = isl_set_get_tuple_id(stmt->domain);
1916 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1917 stmt->domain);
1918 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1919 isl_id_copy(stmt_id));
1920 dim = isl_set_get_space(stmt->domain);
1923 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1924 if (pos >= 0)
1925 stmt->domain = internalize_iv(stmt->domain, pos,
1926 isl_aff_copy(iv_map));
1928 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1929 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1930 isl_dim_in, stmt_id);
1932 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1933 if (pos >= 0) {
1934 isl_set *set = isl_map_wrap(stmt->schedule);
1935 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
1936 stmt->schedule = isl_set_unwrap(set);
1939 dim = isl_space_map_from_set(dim);
1940 extend = isl_multi_pw_aff_identity(dim);
1941 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
1942 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
1943 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
1944 for (i = 0; i < stmt->n_arg; ++i)
1945 stmt->args[i] = expr_embed(stmt->args[i],
1946 isl_multi_pw_aff_copy(extend),
1947 isl_aff_copy(iv_map), var_id);
1948 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1950 isl_set_free(dom);
1951 isl_id_free(var_id);
1953 for (i = 0; i < stmt->n_arg; ++i)
1954 if (!stmt->args[i])
1955 return pet_stmt_free(stmt);
1956 if (!stmt->domain || !stmt->schedule || !stmt->body)
1957 return pet_stmt_free(stmt);
1958 return stmt;
1959 error:
1960 isl_set_free(dom);
1961 isl_map_free(sched);
1962 isl_aff_free(iv_map);
1963 isl_id_free(var_id);
1964 return NULL;
1967 /* Embed the given pet_array in an extra outer loop with iteration domain
1968 * "dom".
1969 * This embedding only has an effect on virtual arrays (those with
1970 * user pointer equal to NULL), which need to be extended along with
1971 * the iteration domain.
1973 static struct pet_array *pet_array_embed(struct pet_array *array,
1974 __isl_take isl_set *dom)
1976 isl_id *array_id = NULL;
1978 if (!array)
1979 goto error;
1981 if (isl_set_has_tuple_id(array->extent))
1982 array_id = isl_set_get_tuple_id(array->extent);
1984 if (array_id && !isl_id_get_user(array_id)) {
1985 array->extent = isl_set_flat_product(dom, array->extent);
1986 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1987 if (!array->extent)
1988 return pet_array_free(array);
1989 } else {
1990 isl_set_free(dom);
1991 isl_id_free(array_id);
1994 return array;
1995 error:
1996 isl_set_free(dom);
1997 return NULL;
2000 /* Project out all unnamed parameters from "set" and return the result.
2002 static __isl_give isl_set *set_project_out_unnamed_params(
2003 __isl_take isl_set *set)
2005 int i, n;
2007 n = isl_set_dim(set, isl_dim_param);
2008 for (i = n - 1; i >= 0; --i) {
2009 if (isl_set_has_dim_name(set, isl_dim_param, i))
2010 continue;
2011 set = isl_set_project_out(set, isl_dim_param, i, 1);
2014 return set;
2017 /* Update the context with respect to an embedding into a loop
2018 * with iteration domain "dom" and induction variable "id".
2019 * "iv_map" expresses the real iterator (parameter "id") in terms
2020 * of a possibly virtual iterator (used in "dom").
2022 * If the current context is independent of "id", we don't need
2023 * to do anything.
2024 * Otherwise, a parameter value is invalid for the embedding if
2025 * any of the corresponding iterator values is invalid.
2026 * That is, a parameter value is valid only if all the corresponding
2027 * iterator values are valid.
2028 * We therefore compute the set of parameters
2030 * forall i in dom : valid (i)
2032 * or
2034 * not exists i in dom : not valid(i)
2036 * i.e.,
2038 * not exists i in dom \ valid(i)
2040 * Before we subtract valid(i) from dom, we first need to substitute
2041 * the real iterator for the virtual iterator.
2043 * If there are any unnamed parameters in "dom", then we consider
2044 * a parameter value to be valid if it is valid for any value of those
2045 * unnamed parameters. They are therefore projected out at the end.
2047 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
2048 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
2049 __isl_keep isl_id *id)
2051 int pos;
2052 isl_multi_aff *ma;
2054 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
2055 if (pos < 0)
2056 return context;
2058 context = isl_set_from_params(context);
2059 context = isl_set_add_dims(context, isl_dim_set, 1);
2060 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
2061 context = isl_set_project_out(context, isl_dim_param, pos, 1);
2062 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
2063 context = isl_set_preimage_multi_aff(context, ma);
2064 context = isl_set_subtract(isl_set_copy(dom), context);
2065 context = isl_set_params(context);
2066 context = isl_set_complement(context);
2067 context = set_project_out_unnamed_params(context);
2068 return context;
2071 /* Update the implication with respect to an embedding into a loop
2072 * with iteration domain "dom".
2074 * Since embed_access extends virtual arrays along with the domain
2075 * of the access, we need to do the same with domain and range
2076 * of the implication. Since the original implication is only valid
2077 * within a given iteration of the loop, the extended implication
2078 * maps the extra array dimension corresponding to the extra loop
2079 * to itself.
2081 static struct pet_implication *pet_implication_embed(
2082 struct pet_implication *implication, __isl_take isl_set *dom)
2084 isl_id *id;
2085 isl_map *map;
2087 if (!implication)
2088 goto error;
2090 map = isl_set_identity(dom);
2091 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
2092 map = isl_map_flat_product(map, implication->extension);
2093 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
2094 map = isl_map_set_tuple_id(map, isl_dim_out, id);
2095 implication->extension = map;
2096 if (!implication->extension)
2097 return pet_implication_free(implication);
2099 return implication;
2100 error:
2101 isl_set_free(dom);
2102 return NULL;
2105 /* Embed all statements and arrays in "scop" in an extra outer loop
2106 * with iteration domain "dom" and schedule "sched".
2107 * "id" represents the induction variable of the loop.
2108 * "iv_map" maps a possibly virtual iterator to the real iterator.
2109 * That is, it expresses the iterator that some of the parameters in "scop"
2110 * may refer to in terms of the iterator used in "dom" and
2111 * the domain of "sched".
2113 * Any skip conditions within the loop have no effect outside of the loop.
2114 * The caller is responsible for making sure skip[pet_skip_later] has been
2115 * taken into account.
2117 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
2118 __isl_take isl_map *sched, __isl_take isl_aff *iv_map,
2119 __isl_take isl_id *id)
2121 int i;
2123 if (!scop)
2124 goto error;
2126 pet_scop_reset_skip(scop, pet_skip_now);
2127 pet_scop_reset_skip(scop, pet_skip_later);
2129 scop->context = context_embed(scop->context, dom, iv_map, id);
2130 if (!scop->context)
2131 goto error;
2133 for (i = 0; i < scop->n_stmt; ++i) {
2134 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
2135 isl_set_copy(dom), isl_map_copy(sched),
2136 isl_aff_copy(iv_map), isl_id_copy(id));
2137 if (!scop->stmts[i])
2138 goto error;
2141 for (i = 0; i < scop->n_array; ++i) {
2142 scop->arrays[i] = pet_array_embed(scop->arrays[i],
2143 isl_set_copy(dom));
2144 if (!scop->arrays[i])
2145 goto error;
2148 for (i = 0; i < scop->n_implication; ++i) {
2149 scop->implications[i] =
2150 pet_implication_embed(scop->implications[i],
2151 isl_set_copy(dom));
2152 if (!scop->implications[i])
2153 goto error;
2156 isl_set_free(dom);
2157 isl_map_free(sched);
2158 isl_aff_free(iv_map);
2159 isl_id_free(id);
2160 return scop;
2161 error:
2162 isl_set_free(dom);
2163 isl_map_free(sched);
2164 isl_aff_free(iv_map);
2165 isl_id_free(id);
2166 return pet_scop_free(scop);
2169 /* Add extra conditions on the parameters to iteration domain of "stmt".
2171 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
2172 __isl_take isl_set *cond)
2174 if (!stmt)
2175 goto error;
2177 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
2179 return stmt;
2180 error:
2181 isl_set_free(cond);
2182 return pet_stmt_free(stmt);
2185 /* Add extra conditions to scop->skip[type].
2187 * The new skip condition only holds if it held before
2188 * and the condition is true. It does not hold if it did not hold
2189 * before or the condition is false.
2191 * The skip condition is assumed to be an affine expression.
2193 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
2194 enum pet_skip type, __isl_keep isl_set *cond)
2196 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2197 isl_pw_aff *skip;
2198 isl_set *dom;
2200 if (!scop)
2201 return NULL;
2202 if (!ext->skip[type])
2203 return scop;
2205 if (!multi_pw_aff_is_affine(ext->skip[type]))
2206 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
2207 isl_error_internal, "can only resrict affine skips",
2208 return pet_scop_free(scop));
2210 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2211 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
2212 cond = isl_set_copy(cond);
2213 cond = isl_set_from_params(cond);
2214 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
2215 skip = indicator_function(cond, dom);
2216 isl_multi_pw_aff_free(ext->skip[type]);
2217 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
2218 if (!ext->skip[type])
2219 return pet_scop_free(scop);
2221 return scop;
2224 /* Add extra conditions on the parameters to all iteration domains
2225 * and skip conditions.
2227 * A parameter value is valid for the result if it was valid
2228 * for the original scop and satisfies "cond" or if it does
2229 * not satisfy "cond" as in this case the scop is not executed
2230 * and the original constraints on the parameters are irrelevant.
2232 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
2233 __isl_take isl_set *cond)
2235 int i;
2237 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
2238 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
2240 if (!scop)
2241 goto error;
2243 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
2244 scop->context = isl_set_union(scop->context,
2245 isl_set_complement(isl_set_copy(cond)));
2246 scop->context = isl_set_coalesce(scop->context);
2247 scop->context = set_project_out_unnamed_params(scop->context);
2248 if (!scop->context)
2249 goto error;
2251 for (i = 0; i < scop->n_stmt; ++i) {
2252 scop->stmts[i] = stmt_restrict(scop->stmts[i],
2253 isl_set_copy(cond));
2254 if (!scop->stmts[i])
2255 goto error;
2258 isl_set_free(cond);
2259 return scop;
2260 error:
2261 isl_set_free(cond);
2262 return pet_scop_free(scop);
2265 /* Construct a function that (upon precomposition) inserts
2266 * a filter value with name "id" and value "satisfied"
2267 * in the list of filter values embedded in the set space "space".
2269 * If "space" does not contain any filter values yet, we first create
2270 * a function that inserts 0 filter values, i.e.,
2272 * [space -> []] -> space
2274 * We can now assume that space is of the form [dom -> [filters]]
2275 * We construct an identity mapping on dom and a mapping on filters
2276 * that (upon precomposition) inserts the new filter
2278 * dom -> dom
2279 * [satisfied, filters] -> [filters]
2281 * and then compute the cross product
2283 * [dom -> [satisfied, filters]] -> [dom -> [filters]]
2285 static __isl_give isl_pw_multi_aff *insert_filter_pma(
2286 __isl_take isl_space *space, __isl_take isl_id *id, int satisfied)
2288 isl_space *space2;
2289 isl_multi_aff *ma;
2290 isl_pw_multi_aff *pma0, *pma, *pma_dom, *pma_ran;
2291 isl_set *dom;
2293 if (isl_space_is_wrapping(space)) {
2294 space2 = isl_space_map_from_set(isl_space_copy(space));
2295 ma = isl_multi_aff_identity(space2);
2296 space = isl_space_unwrap(space);
2297 } else {
2298 space = isl_space_from_domain(space);
2299 ma = isl_multi_aff_domain_map(isl_space_copy(space));
2302 space2 = isl_space_domain(isl_space_copy(space));
2303 pma_dom = isl_pw_multi_aff_identity(isl_space_map_from_set(space2));
2304 space = isl_space_range(space);
2305 space = isl_space_insert_dims(space, isl_dim_set, 0, 1);
2306 pma_ran = isl_pw_multi_aff_project_out_map(space, isl_dim_set, 0, 1);
2307 pma_ran = isl_pw_multi_aff_set_dim_id(pma_ran, isl_dim_in, 0, id);
2308 pma_ran = isl_pw_multi_aff_fix_si(pma_ran, isl_dim_in, 0, satisfied);
2309 pma = isl_pw_multi_aff_product(pma_dom, pma_ran);
2311 pma0 = isl_pw_multi_aff_from_multi_aff(ma);
2312 pma = isl_pw_multi_aff_pullback_pw_multi_aff(pma0, pma);
2314 return pma;
2317 /* Insert an argument expression corresponding to "test" in front
2318 * of the list of arguments described by *n_arg and *args.
2320 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
2321 __isl_keep isl_multi_pw_aff *test)
2323 int i;
2324 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
2326 if (!test)
2327 return -1;
2329 if (!*args) {
2330 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
2331 if (!*args)
2332 return -1;
2333 } else {
2334 struct pet_expr **ext;
2335 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
2336 if (!ext)
2337 return -1;
2338 for (i = 0; i < *n_arg; ++i)
2339 ext[1 + i] = (*args)[i];
2340 free(*args);
2341 *args = ext;
2343 (*n_arg)++;
2344 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
2345 if (!(*args)[0])
2346 return -1;
2348 return 0;
2351 /* Make the expression "expr" depend on the value of "test"
2352 * being equal to "satisfied".
2354 * If "test" is an affine expression, we simply add the conditions
2355 * on the expression having the value "satisfied" to all access relations
2356 * and index expressions.
2358 * Otherwise, we add a filter to "expr" (which is then assumed to be
2359 * an access expression) corresponding to "test" being equal to "satisfied".
2361 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
2362 __isl_take isl_multi_pw_aff *test, int satisfied)
2364 isl_id *id;
2365 isl_ctx *ctx;
2366 isl_space *space;
2367 isl_pw_multi_aff *pma;
2369 if (!expr || !test)
2370 goto error;
2372 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
2373 isl_pw_aff *pa;
2374 isl_set *cond;
2376 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
2377 isl_multi_pw_aff_free(test);
2378 if (satisfied)
2379 cond = isl_pw_aff_non_zero_set(pa);
2380 else
2381 cond = isl_pw_aff_zero_set(pa);
2382 return pet_expr_restrict(expr, isl_set_params(cond));
2385 ctx = isl_multi_pw_aff_get_ctx(test);
2386 if (expr->type != pet_expr_access)
2387 isl_die(ctx, isl_error_invalid,
2388 "can only filter access expressions", goto error);
2390 space = isl_space_domain(isl_map_get_space(expr->acc.access));
2391 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2392 pma = insert_filter_pma(space, id, satisfied);
2394 expr->acc.access = isl_map_preimage_domain_pw_multi_aff(
2395 expr->acc.access,
2396 isl_pw_multi_aff_copy(pma));
2397 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
2398 expr->acc.index, pma);
2399 if (!expr->acc.access || !expr->acc.index)
2400 goto error;
2402 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
2403 goto error;
2405 isl_multi_pw_aff_free(test);
2406 return expr;
2407 error:
2408 isl_multi_pw_aff_free(test);
2409 return pet_expr_free(expr);
2412 /* Look through the applications in "scop" for any that can be
2413 * applied to the filter expressed by "map" and "satisified".
2414 * If there is any, then apply it to "map" and return the result.
2415 * Otherwise, return "map".
2416 * "id" is the identifier of the virtual array.
2418 * We only introduce at most one implication for any given virtual array,
2419 * so we can apply the implication and return as soon as we find one.
2421 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
2422 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
2424 int i;
2426 for (i = 0; i < scop->n_implication; ++i) {
2427 struct pet_implication *pi = scop->implications[i];
2428 isl_id *pi_id;
2430 if (pi->satisfied != satisfied)
2431 continue;
2432 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
2433 isl_id_free(pi_id);
2434 if (pi_id != id)
2435 continue;
2437 return isl_map_apply_range(map, isl_map_copy(pi->extension));
2440 return map;
2443 /* Is the filter expressed by "test" and "satisfied" implied
2444 * by filter "pos" on "domain", with filter "expr", taking into
2445 * account the implications of "scop"?
2447 * For filter on domain implying that expressed by "test" and "satisfied",
2448 * the filter needs to be an access to the same (virtual) array as "test" and
2449 * the filter value needs to be equal to "satisfied".
2450 * Moreover, the filter access relation, possibly extended by
2451 * the implications in "scop" needs to contain "test".
2453 static int implies_filter(struct pet_scop *scop,
2454 __isl_keep isl_map *domain, int pos, struct pet_expr *expr,
2455 __isl_keep isl_map *test, int satisfied)
2457 isl_id *test_id, *arg_id;
2458 isl_val *val;
2459 int is_int;
2460 int s;
2461 int is_subset;
2462 isl_map *implied;
2464 if (expr->type != pet_expr_access)
2465 return 0;
2466 test_id = isl_map_get_tuple_id(test, isl_dim_out);
2467 arg_id = pet_expr_access_get_id(expr);
2468 isl_id_free(arg_id);
2469 isl_id_free(test_id);
2470 if (test_id != arg_id)
2471 return 0;
2472 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
2473 is_int = isl_val_is_int(val);
2474 if (is_int)
2475 s = isl_val_get_num_si(val);
2476 isl_val_free(val);
2477 if (!val)
2478 return -1;
2479 if (!is_int)
2480 return 0;
2481 if (s != satisfied)
2482 return 0;
2484 implied = isl_map_copy(expr->acc.access);
2485 implied = apply_implications(scop, implied, test_id, satisfied);
2486 is_subset = isl_map_is_subset(test, implied);
2487 isl_map_free(implied);
2489 return is_subset;
2492 /* Is the filter expressed by "test" and "satisfied" implied
2493 * by any of the filters on the domain of "stmt", taking into
2494 * account the implications of "scop"?
2496 static int filter_implied(struct pet_scop *scop,
2497 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
2499 int i;
2500 int implied;
2501 isl_id *test_id;
2502 isl_map *domain;
2503 isl_map *test_map;
2505 if (!scop || !stmt || !test)
2506 return -1;
2507 if (scop->n_implication == 0)
2508 return 0;
2509 if (stmt->n_arg == 0)
2510 return 0;
2512 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
2513 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
2515 implied = 0;
2516 for (i = 0; i < stmt->n_arg; ++i) {
2517 implied = implies_filter(scop, domain, i, stmt->args[i],
2518 test_map, satisfied);
2519 if (implied < 0 || implied)
2520 break;
2523 isl_map_free(test_map);
2524 isl_map_free(domain);
2525 return implied;
2528 /* Make the statement "stmt" depend on the value of "test"
2529 * being equal to "satisfied" by adjusting stmt->domain.
2531 * The domain of "test" corresponds to the (zero or more) outer dimensions
2532 * of the iteration domain.
2534 * We first extend "test" to apply to the entire iteration domain and
2535 * then check if the filter that we are about to add is implied
2536 * by any of the current filters, possibly taking into account
2537 * the implications in "scop". If so, we leave "stmt" untouched and return.
2539 * Otherwise, we insert an argument corresponding to a read to "test"
2540 * from the iteration domain of "stmt" in front of the list of arguments.
2541 * We also insert a corresponding output dimension in the wrapped
2542 * map contained in stmt->domain, with value set to "satisfied".
2544 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
2545 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
2547 int i;
2548 int implied;
2549 isl_id *id;
2550 isl_ctx *ctx;
2551 isl_pw_multi_aff *pma;
2552 isl_multi_aff *add_dom;
2553 isl_space *space;
2554 isl_local_space *ls;
2555 int n_test_dom;
2557 if (!stmt || !test)
2558 goto error;
2560 space = isl_set_get_space(stmt->domain);
2561 if (isl_space_is_wrapping(space))
2562 space = isl_space_domain(isl_space_unwrap(space));
2563 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
2564 space = isl_space_from_domain(space);
2565 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
2566 add_dom = isl_multi_aff_zero(isl_space_copy(space));
2567 ls = isl_local_space_from_space(isl_space_domain(space));
2568 for (i = 0; i < n_test_dom; ++i) {
2569 isl_aff *aff;
2570 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2571 isl_dim_set, i);
2572 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
2574 isl_local_space_free(ls);
2575 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
2577 implied = filter_implied(scop, stmt, test, satisfied);
2578 if (implied < 0)
2579 goto error;
2580 if (implied) {
2581 isl_multi_pw_aff_free(test);
2582 return stmt;
2585 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2586 pma = insert_filter_pma(isl_set_get_space(stmt->domain), id, satisfied);
2587 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
2589 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
2590 goto error;
2592 isl_multi_pw_aff_free(test);
2593 return stmt;
2594 error:
2595 isl_multi_pw_aff_free(test);
2596 return pet_stmt_free(stmt);
2599 /* Does "scop" have a skip condition of the given "type"?
2601 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2603 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2605 if (!scop)
2606 return -1;
2607 return ext->skip[type] != NULL;
2610 /* Does "scop" have a skip condition of the given "type" that
2611 * is an affine expression?
2613 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2615 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2617 if (!scop)
2618 return -1;
2619 if (!ext->skip[type])
2620 return 0;
2621 return multi_pw_aff_is_affine(ext->skip[type]);
2624 /* Does "scop" have a skip condition of the given "type" that
2625 * is not an affine expression?
2627 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2629 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2630 int aff;
2632 if (!scop)
2633 return -1;
2634 if (!ext->skip[type])
2635 return 0;
2636 aff = multi_pw_aff_is_affine(ext->skip[type]);
2637 if (aff < 0)
2638 return -1;
2639 return !aff;
2642 /* Does "scop" have a skip condition of the given "type" that
2643 * is affine and holds on the entire domain?
2645 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2647 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2648 isl_pw_aff *pa;
2649 isl_set *set;
2650 int is_aff;
2651 int is_univ;
2653 is_aff = pet_scop_has_affine_skip(scop, type);
2654 if (is_aff < 0 || !is_aff)
2655 return is_aff;
2657 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2658 set = isl_pw_aff_non_zero_set(pa);
2659 is_univ = isl_set_plain_is_universe(set);
2660 isl_set_free(set);
2662 return is_univ;
2665 /* Replace scop->skip[type] by "skip".
2667 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2668 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2670 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2672 if (!scop || !skip)
2673 goto error;
2675 isl_multi_pw_aff_free(ext->skip[type]);
2676 ext->skip[type] = skip;
2678 return scop;
2679 error:
2680 isl_multi_pw_aff_free(skip);
2681 return pet_scop_free(scop);
2684 /* Return a copy of scop->skip[type].
2686 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2687 enum pet_skip type)
2689 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2691 if (!scop)
2692 return NULL;
2694 return isl_multi_pw_aff_copy(ext->skip[type]);
2697 /* Assuming scop->skip[type] is an affine expression,
2698 * return the constraints on the parameters for which the skip condition
2699 * holds.
2701 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2702 enum pet_skip type)
2704 isl_multi_pw_aff *skip;
2705 isl_pw_aff *pa;
2707 skip = pet_scop_get_skip(scop, type);
2708 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2709 isl_multi_pw_aff_free(skip);
2710 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2713 /* Return the identifier of the variable that is accessed by
2714 * the skip condition of the given type.
2716 * The skip condition is assumed not to be an affine condition.
2718 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2719 enum pet_skip type)
2721 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2723 if (!scop)
2724 return NULL;
2726 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2729 /* Return an access pet_expr corresponding to the skip condition
2730 * of the given type.
2732 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2733 enum pet_skip type)
2735 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2738 /* Drop the the skip condition scop->skip[type].
2740 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2742 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2744 if (!scop)
2745 return;
2747 isl_multi_pw_aff_free(ext->skip[type]);
2748 ext->skip[type] = NULL;
2751 /* Make the skip condition (if any) depend on the value of "test" being
2752 * equal to "satisfied".
2754 * We only support the case where the original skip condition is universal,
2755 * i.e., where skipping is unconditional, and where satisfied == 1.
2756 * In this case, the skip condition is changed to skip only when
2757 * "test" is equal to one.
2759 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2760 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2762 int is_univ = 0;
2764 if (!scop)
2765 return NULL;
2766 if (!pet_scop_has_skip(scop, type))
2767 return scop;
2769 if (satisfied)
2770 is_univ = pet_scop_has_universal_skip(scop, type);
2771 if (is_univ < 0)
2772 return pet_scop_free(scop);
2773 if (satisfied && is_univ) {
2774 isl_space *space = isl_multi_pw_aff_get_space(test);
2775 isl_multi_pw_aff *skip;
2776 skip = isl_multi_pw_aff_zero(space);
2777 scop = pet_scop_set_skip(scop, type, skip);
2778 if (!scop)
2779 return NULL;
2780 } else {
2781 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2782 "skip expression cannot be filtered",
2783 return pet_scop_free(scop));
2786 return scop;
2789 /* Make all statements in "scop" depend on the value of "test"
2790 * being equal to "satisfied" by adjusting their domains.
2792 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2793 __isl_take isl_multi_pw_aff *test, int satisfied)
2795 int i;
2797 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2798 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2800 if (!scop || !test)
2801 goto error;
2803 for (i = 0; i < scop->n_stmt; ++i) {
2804 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2805 isl_multi_pw_aff_copy(test), satisfied);
2806 if (!scop->stmts[i])
2807 goto error;
2810 isl_multi_pw_aff_free(test);
2811 return scop;
2812 error:
2813 isl_multi_pw_aff_free(test);
2814 return pet_scop_free(scop);
2817 /* Add all parameters in "expr" to "dim" and return the result.
2819 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2820 __isl_take isl_space *dim)
2822 int i;
2824 if (!expr)
2825 goto error;
2826 for (i = 0; i < expr->n_arg; ++i)
2828 dim = expr_collect_params(expr->args[i], dim);
2830 if (expr->type == pet_expr_access)
2831 dim = isl_space_align_params(dim,
2832 isl_map_get_space(expr->acc.access));
2834 return dim;
2835 error:
2836 isl_space_free(dim);
2837 return pet_expr_free(expr);
2840 /* Add all parameters in "stmt" to "dim" and return the result.
2842 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2843 __isl_take isl_space *dim)
2845 if (!stmt)
2846 goto error;
2848 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2849 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2850 dim = expr_collect_params(stmt->body, dim);
2852 return dim;
2853 error:
2854 isl_space_free(dim);
2855 return pet_stmt_free(stmt);
2858 /* Add all parameters in "array" to "dim" and return the result.
2860 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2861 __isl_take isl_space *dim)
2863 if (!array)
2864 goto error;
2866 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2867 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2869 return dim;
2870 error:
2871 pet_array_free(array);
2872 return isl_space_free(dim);
2875 /* Add all parameters in "scop" to "dim" and return the result.
2877 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2878 __isl_take isl_space *dim)
2880 int i;
2882 if (!scop)
2883 goto error;
2885 for (i = 0; i < scop->n_array; ++i)
2886 dim = array_collect_params(scop->arrays[i], dim);
2888 for (i = 0; i < scop->n_stmt; ++i)
2889 dim = stmt_collect_params(scop->stmts[i], dim);
2891 return dim;
2892 error:
2893 isl_space_free(dim);
2894 pet_scop_free(scop);
2895 return NULL;
2898 /* Add all parameters in "dim" to all access relations and index expressions
2899 * in "expr".
2901 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2902 __isl_take isl_space *dim)
2904 int i;
2906 if (!expr)
2907 goto error;
2909 for (i = 0; i < expr->n_arg; ++i) {
2910 expr->args[i] =
2911 expr_propagate_params(expr->args[i],
2912 isl_space_copy(dim));
2913 if (!expr->args[i])
2914 goto error;
2917 if (expr->type == pet_expr_access) {
2918 expr->acc.access = isl_map_align_params(expr->acc.access,
2919 isl_space_copy(dim));
2920 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
2921 isl_space_copy(dim));
2922 if (!expr->acc.access || !expr->acc.index)
2923 goto error;
2926 isl_space_free(dim);
2927 return expr;
2928 error:
2929 isl_space_free(dim);
2930 return pet_expr_free(expr);
2933 /* Add all parameters in "dim" to the domain, schedule and
2934 * all access relations in "stmt".
2936 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2937 __isl_take isl_space *dim)
2939 if (!stmt)
2940 goto error;
2942 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2943 stmt->schedule = isl_map_align_params(stmt->schedule,
2944 isl_space_copy(dim));
2945 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2947 if (!stmt->domain || !stmt->schedule || !stmt->body)
2948 goto error;
2950 isl_space_free(dim);
2951 return stmt;
2952 error:
2953 isl_space_free(dim);
2954 return pet_stmt_free(stmt);
2957 /* Add all parameters in "dim" to "array".
2959 static struct pet_array *array_propagate_params(struct pet_array *array,
2960 __isl_take isl_space *dim)
2962 if (!array)
2963 goto error;
2965 array->context = isl_set_align_params(array->context,
2966 isl_space_copy(dim));
2967 array->extent = isl_set_align_params(array->extent,
2968 isl_space_copy(dim));
2969 if (array->value_bounds) {
2970 array->value_bounds = isl_set_align_params(array->value_bounds,
2971 isl_space_copy(dim));
2972 if (!array->value_bounds)
2973 goto error;
2976 if (!array->context || !array->extent)
2977 goto error;
2979 isl_space_free(dim);
2980 return array;
2981 error:
2982 isl_space_free(dim);
2983 return pet_array_free(array);
2986 /* Add all parameters in "dim" to "scop".
2988 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2989 __isl_take isl_space *dim)
2991 int i;
2993 if (!scop)
2994 goto error;
2996 for (i = 0; i < scop->n_array; ++i) {
2997 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2998 isl_space_copy(dim));
2999 if (!scop->arrays[i])
3000 goto error;
3003 for (i = 0; i < scop->n_stmt; ++i) {
3004 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
3005 isl_space_copy(dim));
3006 if (!scop->stmts[i])
3007 goto error;
3010 isl_space_free(dim);
3011 return scop;
3012 error:
3013 isl_space_free(dim);
3014 return pet_scop_free(scop);
3017 /* Update all isl_sets and isl_maps in "scop" such that they all
3018 * have the same parameters.
3020 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
3022 isl_space *dim;
3024 if (!scop)
3025 return NULL;
3027 dim = isl_set_get_space(scop->context);
3028 dim = scop_collect_params(scop, dim);
3030 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
3031 scop = scop_propagate_params(scop, dim);
3033 return scop;
3036 /* Check if the given index expression accesses a (0D) array that corresponds
3037 * to one of the parameters in "dim". If so, replace the array access
3038 * by an access to the set of integers with as index (and value)
3039 * that parameter.
3041 static __isl_give isl_multi_pw_aff *index_detect_parameter(
3042 __isl_take isl_multi_pw_aff *index, __isl_take isl_space *space)
3044 isl_local_space *ls;
3045 isl_id *array_id = NULL;
3046 isl_aff *aff;
3047 int pos = -1;
3049 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out)) {
3050 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
3051 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3053 isl_space_free(space);
3055 if (pos < 0) {
3056 isl_id_free(array_id);
3057 return index;
3060 space = isl_multi_pw_aff_get_domain_space(index);
3061 isl_multi_pw_aff_free(index);
3063 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3064 if (pos < 0) {
3065 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
3066 space = isl_space_set_dim_id(space, isl_dim_param, 0, array_id);
3067 pos = 0;
3068 } else
3069 isl_id_free(array_id);
3071 ls = isl_local_space_from_space(space);
3072 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
3073 index = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
3075 return index;
3078 /* Check if the given access relation accesses a (0D) array that corresponds
3079 * to one of the parameters in "dim". If so, replace the array access
3080 * by an access to the set of integers with as index (and value)
3081 * that parameter.
3083 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
3084 __isl_take isl_space *dim)
3086 isl_id *array_id = NULL;
3087 int pos = -1;
3089 if (isl_map_has_tuple_id(access, isl_dim_out)) {
3090 array_id = isl_map_get_tuple_id(access, isl_dim_out);
3091 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
3093 isl_space_free(dim);
3095 if (pos < 0) {
3096 isl_id_free(array_id);
3097 return access;
3100 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
3101 if (pos < 0) {
3102 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
3103 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
3104 pos = 0;
3105 } else
3106 isl_id_free(array_id);
3108 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
3109 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
3111 return access;
3114 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3115 * in "dim" by a value equal to the corresponding parameter.
3117 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
3118 __isl_take isl_space *dim)
3120 int i;
3122 if (!expr)
3123 goto error;
3125 for (i = 0; i < expr->n_arg; ++i) {
3126 expr->args[i] =
3127 expr_detect_parameter_accesses(expr->args[i],
3128 isl_space_copy(dim));
3129 if (!expr->args[i])
3130 goto error;
3133 if (expr->type == pet_expr_access) {
3134 expr->acc.access = access_detect_parameter(expr->acc.access,
3135 isl_space_copy(dim));
3136 expr->acc.index = index_detect_parameter(expr->acc.index,
3137 isl_space_copy(dim));
3138 if (!expr->acc.access || !expr->acc.index)
3139 goto error;
3142 isl_space_free(dim);
3143 return expr;
3144 error:
3145 isl_space_free(dim);
3146 return pet_expr_free(expr);
3149 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3150 * in "dim" by a value equal to the corresponding parameter.
3152 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
3153 __isl_take isl_space *dim)
3155 if (!stmt)
3156 goto error;
3158 stmt->body = expr_detect_parameter_accesses(stmt->body,
3159 isl_space_copy(dim));
3161 if (!stmt->domain || !stmt->schedule || !stmt->body)
3162 goto error;
3164 isl_space_free(dim);
3165 return stmt;
3166 error:
3167 isl_space_free(dim);
3168 return pet_stmt_free(stmt);
3171 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3172 * in "dim" by a value equal to the corresponding parameter.
3174 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
3175 __isl_take isl_space *dim)
3177 int i;
3179 if (!scop)
3180 goto error;
3182 for (i = 0; i < scop->n_stmt; ++i) {
3183 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
3184 isl_space_copy(dim));
3185 if (!scop->stmts[i])
3186 goto error;
3189 isl_space_free(dim);
3190 return scop;
3191 error:
3192 isl_space_free(dim);
3193 return pet_scop_free(scop);
3196 /* Replace all accesses to (0D) arrays that correspond to any of
3197 * the parameters used in "scop" by a value equal
3198 * to the corresponding parameter.
3200 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
3202 isl_space *dim;
3204 if (!scop)
3205 return NULL;
3207 dim = isl_set_get_space(scop->context);
3208 dim = scop_collect_params(scop, dim);
3210 scop = scop_detect_parameter_accesses(scop, dim);
3212 return scop;
3215 /* Return the relation mapping domain iterations to all possibly
3216 * accessed data elements.
3217 * In particular, take the access relation and project out the values
3218 * of the arguments, if any.
3220 static __isl_give isl_map *expr_access_get_may_access(struct pet_expr *expr)
3222 isl_map *access;
3223 isl_space *space;
3224 isl_map *map;
3226 if (!expr)
3227 return NULL;
3228 if (expr->type != pet_expr_access)
3229 return NULL;
3231 access = isl_map_copy(expr->acc.access);
3232 if (expr->n_arg == 0)
3233 return access;
3235 space = isl_space_domain(isl_map_get_space(access));
3236 map = isl_map_universe(isl_space_unwrap(space));
3237 map = isl_map_domain_map(map);
3238 access = isl_map_apply_domain(access, map);
3240 return access;
3243 /* Add all read access relations (if "read" is set) and/or all write
3244 * access relations (if "write" is set) to "accesses" and return the result.
3246 * If "must" is set, then we only add the accesses that are definitely
3247 * performed. Otherwise, we add all potential accesses.
3248 * In particular, if the access has any arguments, then if "must" is
3249 * set we currently skip the access completely. If "must" is not set,
3250 * we project out the values of the access arguments.
3252 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
3253 int read, int write, int must, __isl_take isl_union_map *accesses)
3255 int i;
3256 isl_id *id;
3257 isl_space *dim;
3259 if (!expr)
3260 return NULL;
3262 for (i = 0; i < expr->n_arg; ++i)
3263 accesses = expr_collect_accesses(expr->args[i],
3264 read, write, must, accesses);
3266 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
3267 ((read && expr->acc.read) || (write && expr->acc.write)) &&
3268 (!must || expr->n_arg == 0)) {
3269 isl_map *access;
3271 access = expr_access_get_may_access(expr);
3272 accesses = isl_union_map_add_map(accesses, access);
3275 return accesses;
3278 /* Collect and return all read access relations (if "read" is set)
3279 * and/or all write access relations (if "write" is set) in "stmt".
3281 * If "must" is set, then we only add the accesses that are definitely
3282 * performed. Otherwise, we add all potential accesses.
3283 * In particular, if the statement has any arguments, then if "must" is
3284 * set we currently skip the statement completely. If "must" is not set,
3285 * we project out the values of the statement arguments.
3287 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
3288 int read, int write, int must, __isl_take isl_space *dim)
3290 isl_union_map *accesses;
3291 isl_set *domain;
3293 if (!stmt)
3294 return NULL;
3296 accesses = isl_union_map_empty(dim);
3298 if (must && stmt->n_arg > 0)
3299 return accesses;
3301 domain = isl_set_copy(stmt->domain);
3302 if (isl_set_is_wrapping(domain))
3303 domain = isl_map_domain(isl_set_unwrap(domain));
3305 accesses = expr_collect_accesses(stmt->body,
3306 read, write, must, accesses);
3307 accesses = isl_union_map_intersect_domain(accesses,
3308 isl_union_set_from_set(domain));
3310 return accesses;
3313 /* Collect and return all read access relations (if "read" is set)
3314 * and/or all write access relations (if "write" is set) in "scop".
3315 * If "must" is set, then we only add the accesses that are definitely
3316 * performed. Otherwise, we add all potential accesses.
3318 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
3319 int read, int write, int must)
3321 int i;
3322 isl_union_map *accesses;
3323 isl_union_set *arrays;
3325 if (!scop)
3326 return NULL;
3328 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
3330 for (i = 0; i < scop->n_stmt; ++i) {
3331 isl_union_map *accesses_i;
3332 isl_space *dim = isl_set_get_space(scop->context);
3333 accesses_i = stmt_collect_accesses(scop->stmts[i],
3334 read, write, must, dim);
3335 accesses = isl_union_map_union(accesses, accesses_i);
3338 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
3339 for (i = 0; i < scop->n_array; ++i) {
3340 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
3341 arrays = isl_union_set_add_set(arrays, extent);
3343 accesses = isl_union_map_intersect_range(accesses, arrays);
3345 return accesses;
3348 /* Collect all potential read access relations.
3350 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
3352 return scop_collect_accesses(scop, 1, 0, 0);
3355 /* Collect all potential write access relations.
3357 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
3359 return scop_collect_accesses(scop, 0, 1, 0);
3362 /* Collect all definite write access relations.
3364 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
3366 return scop_collect_accesses(scop, 0, 1, 1);
3369 /* Collect and return the union of iteration domains in "scop".
3371 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
3373 int i;
3374 isl_set *domain_i;
3375 isl_union_set *domain;
3377 if (!scop)
3378 return NULL;
3380 domain = isl_union_set_empty(isl_set_get_space(scop->context));
3382 for (i = 0; i < scop->n_stmt; ++i) {
3383 domain_i = isl_set_copy(scop->stmts[i]->domain);
3384 domain = isl_union_set_add_set(domain, domain_i);
3387 return domain;
3390 /* Collect and return the schedules of the statements in "scop".
3391 * The range is normalized to the maximal number of scheduling
3392 * dimensions.
3394 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
3396 int i, j;
3397 isl_map *schedule_i;
3398 isl_union_map *schedule;
3399 int depth, max_depth = 0;
3401 if (!scop)
3402 return NULL;
3404 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
3406 for (i = 0; i < scop->n_stmt; ++i) {
3407 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
3408 if (depth > max_depth)
3409 max_depth = depth;
3412 for (i = 0; i < scop->n_stmt; ++i) {
3413 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
3414 depth = isl_map_dim(schedule_i, isl_dim_out);
3415 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
3416 max_depth - depth);
3417 for (j = depth; j < max_depth; ++j)
3418 schedule_i = isl_map_fix_si(schedule_i,
3419 isl_dim_out, j, 0);
3420 schedule = isl_union_map_add_map(schedule, schedule_i);
3423 return schedule;
3426 /* Does expression "expr" write to "id"?
3428 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
3430 int i;
3431 isl_id *write_id;
3433 for (i = 0; i < expr->n_arg; ++i) {
3434 int writes = expr_writes(expr->args[i], id);
3435 if (writes < 0 || writes)
3436 return writes;
3439 if (expr->type != pet_expr_access)
3440 return 0;
3441 if (!expr->acc.write)
3442 return 0;
3443 if (pet_expr_is_affine(expr))
3444 return 0;
3446 write_id = pet_expr_access_get_id(expr);
3447 isl_id_free(write_id);
3449 if (!write_id)
3450 return -1;
3452 return write_id == id;
3455 /* Does statement "stmt" write to "id"?
3457 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
3459 return expr_writes(stmt->body, id);
3462 /* Is there any write access in "scop" that accesses "id"?
3464 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
3466 int i;
3468 if (!scop)
3469 return -1;
3471 for (i = 0; i < scop->n_stmt; ++i) {
3472 int writes = stmt_writes(scop->stmts[i], id);
3473 if (writes < 0 || writes)
3474 return writes;
3477 return 0;
3480 /* Add a reference identifier to access expression "expr".
3481 * "user" points to an integer that contains the sequence number
3482 * of the next reference.
3484 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
3486 isl_ctx *ctx;
3487 char name[50];
3488 int *n_ref = user;
3490 if (!expr)
3491 return expr;
3493 ctx = isl_map_get_ctx(expr->acc.access);
3494 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
3495 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
3496 if (!expr->acc.ref_id)
3497 return pet_expr_free(expr);
3499 return expr;
3502 /* Add a reference identifier to all access expressions in "stmt".
3503 * "n_ref" points to an integer that contains the sequence number
3504 * of the next reference.
3506 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
3508 int i;
3510 if (!stmt)
3511 return NULL;
3513 for (i = 0; i < stmt->n_arg; ++i) {
3514 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3515 &access_add_ref_id, n_ref);
3516 if (!stmt->args[i])
3517 return pet_stmt_free(stmt);
3520 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
3521 if (!stmt->body)
3522 return pet_stmt_free(stmt);
3524 return stmt;
3527 /* Add a reference identifier to all access expressions in "scop".
3529 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
3531 int i;
3532 int n_ref;
3534 if (!scop)
3535 return NULL;
3537 n_ref = 0;
3538 for (i = 0; i < scop->n_stmt; ++i) {
3539 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
3540 if (!scop->stmts[i])
3541 return pet_scop_free(scop);
3544 return scop;
3547 /* Reset the user pointer on the tuple id and all parameter ids in "set".
3549 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
3551 int i, n;
3553 n = isl_set_dim(set, isl_dim_param);
3554 for (i = 0; i < n; ++i) {
3555 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
3556 const char *name = isl_id_get_name(id);
3557 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
3558 isl_id_free(id);
3561 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
3562 isl_id *id = isl_set_get_tuple_id(set);
3563 const char *name = isl_id_get_name(id);
3564 set = isl_set_set_tuple_name(set, name);
3565 isl_id_free(id);
3568 return set;
3571 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
3573 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
3575 int i, n;
3577 n = isl_map_dim(map, isl_dim_param);
3578 for (i = 0; i < n; ++i) {
3579 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
3580 const char *name = isl_id_get_name(id);
3581 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
3582 isl_id_free(id);
3585 if (isl_map_has_tuple_id(map, isl_dim_in)) {
3586 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
3587 const char *name = isl_id_get_name(id);
3588 map = isl_map_set_tuple_name(map, isl_dim_in, name);
3589 isl_id_free(id);
3592 if (isl_map_has_tuple_id(map, isl_dim_out)) {
3593 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
3594 const char *name = isl_id_get_name(id);
3595 map = isl_map_set_tuple_name(map, isl_dim_out, name);
3596 isl_id_free(id);
3599 return map;
3602 /* Reset the user pointer on the tuple ids and all parameter ids in "mpa".
3604 static __isl_give isl_multi_pw_aff *multi_pw_aff_anonymize(
3605 __isl_take isl_multi_pw_aff *mpa)
3607 int i, n;
3609 n = isl_multi_pw_aff_dim(mpa, isl_dim_param);
3610 for (i = 0; i < n; ++i) {
3611 isl_id *id = isl_multi_pw_aff_get_dim_id(mpa, isl_dim_param, i);
3612 const char *name = isl_id_get_name(id);
3613 mpa = isl_multi_pw_aff_set_dim_name(mpa,
3614 isl_dim_param, i, name);
3615 isl_id_free(id);
3618 if (isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_in)) {
3619 isl_id *id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_in);
3620 const char *name = isl_id_get_name(id);
3621 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, name);
3622 isl_id_free(id);
3625 if (isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out)) {
3626 isl_id *id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
3627 const char *name = isl_id_get_name(id);
3628 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_out, name);
3629 isl_id_free(id);
3632 return mpa;
3635 /* Reset the user pointer on all parameter ids in "array".
3637 static struct pet_array *array_anonymize(struct pet_array *array)
3639 if (!array)
3640 return NULL;
3642 array->context = set_anonymize(array->context);
3643 array->extent = set_anonymize(array->extent);
3644 if (!array->context || !array->extent)
3645 return pet_array_free(array);
3647 return array;
3650 /* Reset the user pointer on all parameter and tuple ids in
3651 * the access relation and the index expressions
3652 * of the access expression "expr".
3654 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
3656 expr->acc.access = map_anonymize(expr->acc.access);
3657 expr->acc.index = multi_pw_aff_anonymize(expr->acc.index);
3658 if (!expr->acc.access || !expr->acc.index)
3659 return pet_expr_free(expr);
3661 return expr;
3664 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3666 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3668 int i;
3669 isl_space *space;
3670 isl_set *domain;
3672 if (!stmt)
3673 return NULL;
3675 stmt->domain = set_anonymize(stmt->domain);
3676 stmt->schedule = map_anonymize(stmt->schedule);
3677 if (!stmt->domain || !stmt->schedule)
3678 return pet_stmt_free(stmt);
3680 for (i = 0; i < stmt->n_arg; ++i) {
3681 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3682 &access_anonymize, NULL);
3683 if (!stmt->args[i])
3684 return pet_stmt_free(stmt);
3687 stmt->body = pet_expr_map_access(stmt->body,
3688 &access_anonymize, NULL);
3689 if (!stmt->body)
3690 return pet_stmt_free(stmt);
3692 return stmt;
3695 /* Reset the user pointer on the tuple ids and all parameter ids
3696 * in "implication".
3698 static struct pet_implication *implication_anonymize(
3699 struct pet_implication *implication)
3701 if (!implication)
3702 return NULL;
3704 implication->extension = map_anonymize(implication->extension);
3705 if (!implication->extension)
3706 return pet_implication_free(implication);
3708 return implication;
3711 /* Reset the user pointer on all parameter and tuple ids in "scop".
3713 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
3715 int i;
3717 if (!scop)
3718 return NULL;
3720 scop->context = set_anonymize(scop->context);
3721 scop->context_value = set_anonymize(scop->context_value);
3722 if (!scop->context || !scop->context_value)
3723 return pet_scop_free(scop);
3725 for (i = 0; i < scop->n_array; ++i) {
3726 scop->arrays[i] = array_anonymize(scop->arrays[i]);
3727 if (!scop->arrays[i])
3728 return pet_scop_free(scop);
3731 for (i = 0; i < scop->n_stmt; ++i) {
3732 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
3733 if (!scop->stmts[i])
3734 return pet_scop_free(scop);
3737 for (i = 0; i < scop->n_implication; ++i) {
3738 scop->implications[i] =
3739 implication_anonymize(scop->implications[i]);
3740 if (!scop->implications[i])
3741 return pet_scop_free(scop);
3744 return scop;
3747 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
3748 * then intersect the range of "map" with the valid set of values.
3750 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
3751 struct pet_expr *arg, __isl_keep isl_union_map *value_bounds)
3753 isl_id *id;
3754 isl_map *vb;
3755 isl_space *space;
3756 isl_ctx *ctx = isl_map_get_ctx(map);
3758 id = pet_expr_access_get_id(arg);
3759 space = isl_space_alloc(ctx, 0, 0, 1);
3760 space = isl_space_set_tuple_id(space, isl_dim_in, id);
3761 vb = isl_union_map_extract_map(value_bounds, space);
3762 if (!isl_map_plain_is_empty(vb))
3763 map = isl_map_intersect_range(map, isl_map_range(vb));
3764 else
3765 isl_map_free(vb);
3767 return map;
3770 /* Given a set "domain", return a wrapped relation with the given set
3771 * as domain and a range of dimension "n_arg", where each coordinate
3772 * is either unbounded or, if the corresponding element of args is of
3773 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
3775 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
3776 unsigned n_arg, struct pet_expr **args,
3777 __isl_keep isl_union_map *value_bounds)
3779 int i;
3780 isl_map *map;
3781 isl_space *space;
3783 map = isl_map_from_domain(domain);
3784 space = isl_map_get_space(map);
3785 space = isl_space_add_dims(space, isl_dim_out, 1);
3787 for (i = 0; i < n_arg; ++i) {
3788 isl_map *map_i;
3789 struct pet_expr *arg = args[i];
3791 map_i = isl_map_universe(isl_space_copy(space));
3792 if (arg->type == pet_expr_access)
3793 map_i = access_apply_value_bounds(map_i, arg,
3794 value_bounds);
3795 map = isl_map_flat_range_product(map, map_i);
3797 isl_space_free(space);
3799 return isl_map_wrap(map);
3802 /* Data used in access_gist() callback.
3804 struct pet_access_gist_data {
3805 isl_set *domain;
3806 isl_union_map *value_bounds;
3809 /* Given an expression "expr" of type pet_expr_access, compute
3810 * the gist of the associated access relation and index expression
3811 * with respect to data->domain and the bounds on the values of the arguments
3812 * of the expression.
3814 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
3816 struct pet_access_gist_data *data = user;
3817 isl_set *domain;
3819 domain = isl_set_copy(data->domain);
3820 if (expr->n_arg > 0)
3821 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
3822 data->value_bounds);
3824 expr->acc.access = isl_map_gist_domain(expr->acc.access,
3825 isl_set_copy(domain));
3826 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
3827 if (!expr->acc.access || !expr->acc.index)
3828 return pet_expr_free(expr);
3830 return expr;
3833 /* Compute the gist of the iteration domain and all access relations
3834 * of "stmt" based on the constraints on the parameters specified by "context"
3835 * and the constraints on the values of nested accesses specified
3836 * by "value_bounds".
3838 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3839 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3841 int i;
3842 isl_space *space;
3843 isl_set *domain;
3844 struct pet_access_gist_data data;
3846 if (!stmt)
3847 return NULL;
3849 data.domain = isl_set_copy(stmt->domain);
3850 data.value_bounds = value_bounds;
3851 if (stmt->n_arg > 0)
3852 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3854 data.domain = isl_set_intersect_params(data.domain,
3855 isl_set_copy(context));
3857 for (i = 0; i < stmt->n_arg; ++i) {
3858 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3859 &access_gist, &data);
3860 if (!stmt->args[i])
3861 goto error;
3864 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
3865 if (!stmt->body)
3866 goto error;
3868 isl_set_free(data.domain);
3870 space = isl_set_get_space(stmt->domain);
3871 if (isl_space_is_wrapping(space))
3872 space = isl_space_domain(isl_space_unwrap(space));
3873 domain = isl_set_universe(space);
3874 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3875 if (stmt->n_arg > 0)
3876 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3877 value_bounds);
3878 stmt->domain = isl_set_gist(stmt->domain, domain);
3879 if (!stmt->domain)
3880 return pet_stmt_free(stmt);
3882 return stmt;
3883 error:
3884 isl_set_free(data.domain);
3885 return pet_stmt_free(stmt);
3888 /* Compute the gist of the extent of the array
3889 * based on the constraints on the parameters specified by "context".
3891 static struct pet_array *array_gist(struct pet_array *array,
3892 __isl_keep isl_set *context)
3894 if (!array)
3895 return NULL;
3897 array->extent = isl_set_gist_params(array->extent,
3898 isl_set_copy(context));
3899 if (!array->extent)
3900 return pet_array_free(array);
3902 return array;
3905 /* Compute the gist of all sets and relations in "scop"
3906 * based on the constraints on the parameters specified by "scop->context"
3907 * and the constraints on the values of nested accesses specified
3908 * by "value_bounds".
3910 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3911 __isl_keep isl_union_map *value_bounds)
3913 int i;
3915 if (!scop)
3916 return NULL;
3918 scop->context = isl_set_coalesce(scop->context);
3919 if (!scop->context)
3920 return pet_scop_free(scop);
3922 for (i = 0; i < scop->n_array; ++i) {
3923 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3924 if (!scop->arrays[i])
3925 return pet_scop_free(scop);
3928 for (i = 0; i < scop->n_stmt; ++i) {
3929 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3930 value_bounds);
3931 if (!scop->stmts[i])
3932 return pet_scop_free(scop);
3935 return scop;
3938 /* Intersect the context of "scop" with "context".
3939 * To ensure that we don't introduce any unnamed parameters in
3940 * the context of "scop", we first remove the unnamed parameters
3941 * from "context".
3943 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3944 __isl_take isl_set *context)
3946 if (!scop)
3947 goto error;
3949 context = set_project_out_unnamed_params(context);
3950 scop->context = isl_set_intersect(scop->context, context);
3951 if (!scop->context)
3952 return pet_scop_free(scop);
3954 return scop;
3955 error:
3956 isl_set_free(context);
3957 return pet_scop_free(scop);
3960 /* Drop the current context of "scop". That is, replace the context
3961 * by a universal set.
3963 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3965 isl_space *space;
3967 if (!scop)
3968 return NULL;
3970 space = isl_set_get_space(scop->context);
3971 isl_set_free(scop->context);
3972 scop->context = isl_set_universe(space);
3973 if (!scop->context)
3974 return pet_scop_free(scop);
3976 return scop;
3979 /* Append "array" to the arrays of "scop".
3981 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3982 struct pet_array *array)
3984 isl_ctx *ctx;
3985 struct pet_array **arrays;
3987 if (!array || !scop)
3988 goto error;
3990 ctx = isl_set_get_ctx(scop->context);
3991 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3992 scop->n_array + 1);
3993 if (!arrays)
3994 goto error;
3995 scop->arrays = arrays;
3996 scop->arrays[scop->n_array] = array;
3997 scop->n_array++;
3999 return scop;
4000 error:
4001 pet_array_free(array);
4002 return pet_scop_free(scop);
4005 /* Create and return an implication on filter values equal to "satisfied"
4006 * with extension "map".
4008 static struct pet_implication *new_implication(__isl_take isl_map *map,
4009 int satisfied)
4011 isl_ctx *ctx;
4012 struct pet_implication *implication;
4014 if (!map)
4015 return NULL;
4016 ctx = isl_map_get_ctx(map);
4017 implication = isl_alloc_type(ctx, struct pet_implication);
4018 if (!implication)
4019 goto error;
4021 implication->extension = map;
4022 implication->satisfied = satisfied;
4024 return implication;
4025 error:
4026 isl_map_free(map);
4027 return NULL;
4030 /* Add an implication on filter values equal to "satisfied"
4031 * with extension "map" to "scop".
4033 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
4034 __isl_take isl_map *map, int satisfied)
4036 isl_ctx *ctx;
4037 struct pet_implication *implication;
4038 struct pet_implication **implications;
4040 implication = new_implication(map, satisfied);
4041 if (!scop || !implication)
4042 goto error;
4044 ctx = isl_set_get_ctx(scop->context);
4045 implications = isl_realloc_array(ctx, scop->implications,
4046 struct pet_implication *,
4047 scop->n_implication + 1);
4048 if (!implications)
4049 goto error;
4050 scop->implications = implications;
4051 scop->implications[scop->n_implication] = implication;
4052 scop->n_implication++;
4054 return scop;
4055 error:
4056 pet_implication_free(implication);
4057 return pet_scop_free(scop);
4060 /* Given an access expression, check if it is data dependent.
4061 * If so, set *found and abort the search.
4063 static int is_data_dependent(struct pet_expr *expr, void *user)
4065 int *found = user;
4067 if (expr->n_arg) {
4068 *found = 1;
4069 return -1;
4072 return 0;
4075 /* Does "scop" contain any data dependent accesses?
4077 * Check the body of each statement for such accesses.
4079 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
4081 int i;
4082 int found = 0;
4084 if (!scop)
4085 return -1;
4087 for (i = 0; i < scop->n_stmt; ++i) {
4088 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
4089 &is_data_dependent, &found);
4090 if (r < 0 && !found)
4091 return -1;
4092 if (found)
4093 return found;
4096 return found;
4099 /* Does "scop" contain and data dependent conditions?
4101 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
4103 int i;
4105 if (!scop)
4106 return -1;
4108 for (i = 0; i < scop->n_stmt; ++i)
4109 if (scop->stmts[i]->n_arg > 0)
4110 return 1;
4112 return 0;
4115 /* Keep track of the "input" file inside the (extended) "scop".
4117 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
4119 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4121 if (!scop)
4122 return NULL;
4124 ext->input = input;
4126 return scop;
4129 /* Print the original code corresponding to "scop" to printer "p".
4131 * pet_scop_print_original can only be called from
4132 * a pet_transform_C_source callback. This means that the input
4133 * file is stored in the extended scop and that the printer prints
4134 * to a file.
4136 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
4137 __isl_take isl_printer *p)
4139 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4140 FILE *output;
4142 if (!scop || !p)
4143 return isl_printer_free(p);
4145 if (!ext->input)
4146 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
4147 "no input file stored in scop",
4148 return isl_printer_free(p));
4150 output = isl_printer_get_file(p);
4151 if (!output)
4152 return isl_printer_free(p);
4154 if (copy(ext->input, output, scop->start, scop->end) < 0)
4155 return isl_printer_free(p);
4157 return p;