pet_check_code: better support for schedules that are not single valued
[pet.git] / scop.c
blobbdfa9aef9de8bdbfc10235564e85acb6ba88ef41
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012 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 <isl/constraint.h>
36 #include <isl/union_set.h>
38 #include "scop.h"
40 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
42 static char *type_str[] = {
43 [pet_expr_access] = "access",
44 [pet_expr_call] = "call",
45 [pet_expr_cast] = "cast",
46 [pet_expr_double] = "double",
47 [pet_expr_unary] = "unary",
48 [pet_expr_binary] = "binary",
49 [pet_expr_ternary] = "ternary"
52 static char *op_str[] = {
53 [pet_op_add_assign] = "+=",
54 [pet_op_sub_assign] = "-=",
55 [pet_op_mul_assign] = "*=",
56 [pet_op_div_assign] = "/=",
57 [pet_op_assign] = "=",
58 [pet_op_add] = "+",
59 [pet_op_sub] = "-",
60 [pet_op_mul] = "*",
61 [pet_op_div] = "/",
62 [pet_op_mod] = "%",
63 [pet_op_eq] = "==",
64 [pet_op_le] = "<=",
65 [pet_op_lt] = "<",
66 [pet_op_gt] = ">",
67 [pet_op_minus] = "-",
68 [pet_op_post_inc] = "++",
69 [pet_op_post_dec] = "--",
70 [pet_op_pre_inc] = "++",
71 [pet_op_pre_dec] = "--",
72 [pet_op_address_of] = "&",
73 [pet_op_kill] = "kill"
76 /* pet_scop with extra information that is only used during parsing.
78 * In particular, we keep track of conditions under which we want
79 * to skip the rest of the current loop iteration (skip[pet_skip_now])
80 * and of conditions under which we want to skip subsequent
81 * loop iterations (skip[pet_skip_later]).
83 * The conditions are represented either by a variable, which
84 * is assumed to attain values zero and one, or by a boolean affine
85 * expression. The condition holds if the variable has value one
86 * or if the affine expression has value one (typically for only
87 * part of the parameter space).
89 * A missing condition (skip[type] == NULL) means that we don't want
90 * to skip anything.
92 struct pet_scop_ext {
93 struct pet_scop scop;
95 isl_set *skip[2];
98 const char *pet_op_str(enum pet_op_type op)
100 return op_str[op];
103 int pet_op_is_inc_dec(enum pet_op_type op)
105 return op == pet_op_post_inc || op == pet_op_post_dec ||
106 op == pet_op_pre_inc || op == pet_op_pre_dec;
109 const char *pet_type_str(enum pet_expr_type type)
111 return type_str[type];
114 enum pet_op_type pet_str_op(const char *str)
116 int i;
118 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
119 if (!strcmp(op_str[i], str))
120 return i;
122 return -1;
125 enum pet_expr_type pet_str_type(const char *str)
127 int i;
129 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
130 if (!strcmp(type_str[i], str))
131 return i;
133 return -1;
136 /* Construct a pet_expr from an access relation.
137 * By default, it is considered to be a read access.
139 struct pet_expr *pet_expr_from_access(__isl_take isl_map *access)
141 isl_ctx *ctx = isl_map_get_ctx(access);
142 struct pet_expr *expr;
144 if (!access)
145 return NULL;
146 expr = isl_calloc_type(ctx, struct pet_expr);
147 if (!expr)
148 goto error;
150 expr->type = pet_expr_access;
151 expr->acc.access = access;
152 expr->acc.read = 1;
153 expr->acc.write = 0;
155 return expr;
156 error:
157 isl_map_free(access);
158 return NULL;
161 /* Construct a pet_expr that kills the elements specified by "access".
163 struct pet_expr *pet_expr_kill_from_access(__isl_take isl_map *access)
165 isl_ctx *ctx;
166 struct pet_expr *expr;
168 ctx = isl_map_get_ctx(access);
169 expr = pet_expr_from_access(access);
170 if (!expr)
171 return NULL;
172 expr->acc.read = 0;
173 return pet_expr_new_unary(ctx, pet_op_kill, expr);
176 /* Construct a unary pet_expr that performs "op" on "arg".
178 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
179 struct pet_expr *arg)
181 struct pet_expr *expr;
183 if (!arg)
184 goto error;
185 expr = isl_alloc_type(ctx, struct pet_expr);
186 if (!expr)
187 goto error;
189 expr->type = pet_expr_unary;
190 expr->op = op;
191 expr->n_arg = 1;
192 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
193 if (!expr->args)
194 goto error;
195 expr->args[pet_un_arg] = arg;
197 return expr;
198 error:
199 pet_expr_free(arg);
200 return NULL;
203 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
205 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
206 struct pet_expr *lhs, struct pet_expr *rhs)
208 struct pet_expr *expr;
210 if (!lhs || !rhs)
211 goto error;
212 expr = isl_alloc_type(ctx, struct pet_expr);
213 if (!expr)
214 goto error;
216 expr->type = pet_expr_binary;
217 expr->op = op;
218 expr->n_arg = 2;
219 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
220 if (!expr->args)
221 goto error;
222 expr->args[pet_bin_lhs] = lhs;
223 expr->args[pet_bin_rhs] = rhs;
225 return expr;
226 error:
227 pet_expr_free(lhs);
228 pet_expr_free(rhs);
229 return NULL;
232 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
234 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
235 struct pet_expr *lhs, struct pet_expr *rhs)
237 struct pet_expr *expr;
239 if (!cond || !lhs || !rhs)
240 goto error;
241 expr = isl_alloc_type(ctx, struct pet_expr);
242 if (!expr)
243 goto error;
245 expr->type = pet_expr_ternary;
246 expr->n_arg = 3;
247 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
248 if (!expr->args)
249 goto error;
250 expr->args[pet_ter_cond] = cond;
251 expr->args[pet_ter_true] = lhs;
252 expr->args[pet_ter_false] = rhs;
254 return expr;
255 error:
256 pet_expr_free(cond);
257 pet_expr_free(lhs);
258 pet_expr_free(rhs);
259 return NULL;
262 /* Construct a call pet_expr that calls function "name" with "n_arg"
263 * arguments. The caller is responsible for filling in the arguments.
265 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
266 unsigned n_arg)
268 struct pet_expr *expr;
270 expr = isl_alloc_type(ctx, struct pet_expr);
271 if (!expr)
272 return NULL;
274 expr->type = pet_expr_call;
275 expr->n_arg = n_arg;
276 expr->name = strdup(name);
277 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
278 if (!expr->name || !expr->args)
279 return pet_expr_free(expr);
281 return expr;
284 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
286 struct pet_expr *pet_expr_new_cast(isl_ctx *ctx, const char *type_name,
287 struct pet_expr *arg)
289 struct pet_expr *expr;
291 if (!arg)
292 return NULL;
294 expr = isl_alloc_type(ctx, struct pet_expr);
295 if (!expr)
296 goto error;
298 expr->type = pet_expr_cast;
299 expr->n_arg = 1;
300 expr->type_name = strdup(type_name);
301 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
302 if (!expr->type_name || !expr->args)
303 goto error;
305 expr->args[0] = arg;
307 return expr;
308 error:
309 pet_expr_free(arg);
310 pet_expr_free(expr);
311 return NULL;
314 /* Construct a pet_expr that represents the double "d".
316 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
318 struct pet_expr *expr;
320 expr = isl_calloc_type(ctx, struct pet_expr);
321 if (!expr)
322 return NULL;
324 expr->type = pet_expr_double;
325 expr->d.val = val;
326 expr->d.s = strdup(s);
327 if (!expr->d.s)
328 return pet_expr_free(expr);
330 return expr;
333 void *pet_expr_free(struct pet_expr *expr)
335 int i;
337 if (!expr)
338 return NULL;
340 for (i = 0; i < expr->n_arg; ++i)
341 pet_expr_free(expr->args[i]);
342 free(expr->args);
344 switch (expr->type) {
345 case pet_expr_access:
346 isl_map_free(expr->acc.access);
347 break;
348 case pet_expr_call:
349 free(expr->name);
350 break;
351 case pet_expr_cast:
352 free(expr->type_name);
353 break;
354 case pet_expr_double:
355 free(expr->d.s);
356 break;
357 case pet_expr_unary:
358 case pet_expr_binary:
359 case pet_expr_ternary:
360 break;
363 free(expr);
364 return NULL;
367 static void expr_dump(struct pet_expr *expr, int indent)
369 int i;
371 if (!expr)
372 return;
374 fprintf(stderr, "%*s", indent, "");
376 switch (expr->type) {
377 case pet_expr_double:
378 fprintf(stderr, "%s\n", expr->d.s);
379 break;
380 case pet_expr_access:
381 isl_map_dump(expr->acc.access);
382 fprintf(stderr, "%*sread: %d\n", indent + 2,
383 "", expr->acc.read);
384 fprintf(stderr, "%*swrite: %d\n", indent + 2,
385 "", expr->acc.write);
386 for (i = 0; i < expr->n_arg; ++i)
387 expr_dump(expr->args[i], indent + 2);
388 break;
389 case pet_expr_unary:
390 fprintf(stderr, "%s\n", op_str[expr->op]);
391 expr_dump(expr->args[pet_un_arg], indent + 2);
392 break;
393 case pet_expr_binary:
394 fprintf(stderr, "%s\n", op_str[expr->op]);
395 expr_dump(expr->args[pet_bin_lhs], indent + 2);
396 expr_dump(expr->args[pet_bin_rhs], indent + 2);
397 break;
398 case pet_expr_ternary:
399 fprintf(stderr, "?:\n");
400 expr_dump(expr->args[pet_ter_cond], indent + 2);
401 expr_dump(expr->args[pet_ter_true], indent + 2);
402 expr_dump(expr->args[pet_ter_false], indent + 2);
403 break;
404 case pet_expr_call:
405 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
406 for (i = 0; i < expr->n_arg; ++i)
407 expr_dump(expr->args[i], indent + 2);
408 break;
409 case pet_expr_cast:
410 fprintf(stderr, "(%s)\n", expr->type_name);
411 for (i = 0; i < expr->n_arg; ++i)
412 expr_dump(expr->args[i], indent + 2);
413 break;
417 void pet_expr_dump(struct pet_expr *expr)
419 expr_dump(expr, 0);
422 /* Does "expr" represent an access to an unnamed space, i.e.,
423 * does it represent an affine expression?
425 int pet_expr_is_affine(struct pet_expr *expr)
427 int has_id;
429 if (!expr)
430 return -1;
431 if (expr->type != pet_expr_access)
432 return 0;
434 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
435 if (has_id < 0)
436 return -1;
438 return !has_id;
441 /* Return 1 if the two pet_exprs are equivalent.
443 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
445 int i;
447 if (!expr1 || !expr2)
448 return 0;
450 if (expr1->type != expr2->type)
451 return 0;
452 if (expr1->n_arg != expr2->n_arg)
453 return 0;
454 for (i = 0; i < expr1->n_arg; ++i)
455 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
456 return 0;
457 switch (expr1->type) {
458 case pet_expr_double:
459 if (strcmp(expr1->d.s, expr2->d.s))
460 return 0;
461 if (expr1->d.val != expr2->d.val)
462 return 0;
463 break;
464 case pet_expr_access:
465 if (expr1->acc.read != expr2->acc.read)
466 return 0;
467 if (expr1->acc.write != expr2->acc.write)
468 return 0;
469 if (!expr1->acc.access || !expr2->acc.access)
470 return 0;
471 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
472 return 0;
473 break;
474 case pet_expr_unary:
475 case pet_expr_binary:
476 case pet_expr_ternary:
477 if (expr1->op != expr2->op)
478 return 0;
479 break;
480 case pet_expr_call:
481 if (strcmp(expr1->name, expr2->name))
482 return 0;
483 break;
484 case pet_expr_cast:
485 if (strcmp(expr1->type_name, expr2->type_name))
486 return 0;
487 break;
490 return 1;
493 /* Add extra conditions on the parameters to all access relations in "expr".
495 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
496 __isl_take isl_set *cond)
498 int i;
500 if (!expr)
501 goto error;
503 for (i = 0; i < expr->n_arg; ++i) {
504 expr->args[i] = pet_expr_restrict(expr->args[i],
505 isl_set_copy(cond));
506 if (!expr->args[i])
507 goto error;
510 if (expr->type == pet_expr_access) {
511 expr->acc.access = isl_map_intersect_params(expr->acc.access,
512 isl_set_copy(cond));
513 if (!expr->acc.access)
514 goto error;
517 isl_set_free(cond);
518 return expr;
519 error:
520 isl_set_free(cond);
521 return pet_expr_free(expr);
524 /* Modify all access relations in "expr" by calling "fn" on them.
526 struct pet_expr *pet_expr_foreach_access(struct pet_expr *expr,
527 __isl_give isl_map *(*fn)(__isl_take isl_map *access, void *user),
528 void *user)
530 int i;
532 if (!expr)
533 return NULL;
535 for (i = 0; i < expr->n_arg; ++i) {
536 expr->args[i] = pet_expr_foreach_access(expr->args[i], fn, user);
537 if (!expr->args[i])
538 return pet_expr_free(expr);
541 if (expr->type == pet_expr_access) {
542 expr->acc.access = fn(expr->acc.access, user);
543 if (!expr->acc.access)
544 return pet_expr_free(expr);
547 return expr;
550 /* Modify all expressions of type pet_expr_access in "expr"
551 * by calling "fn" on them.
553 struct pet_expr *pet_expr_foreach_access_expr(struct pet_expr *expr,
554 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
555 void *user)
557 int i;
559 if (!expr)
560 return NULL;
562 for (i = 0; i < expr->n_arg; ++i) {
563 expr->args[i] = pet_expr_foreach_access_expr(expr->args[i],
564 fn, user);
565 if (!expr->args[i])
566 return pet_expr_free(expr);
569 if (expr->type == pet_expr_access)
570 expr = fn(expr, user);
572 return expr;
575 /* Modify the given access relation based on the given iteration space
576 * transformation.
577 * If the access has any arguments then the domain of the access relation
578 * is a wrapped mapping from the iteration space to the space of
579 * argument values. We only need to change the domain of this wrapped
580 * mapping, so we extend the input transformation with an identity mapping
581 * on the space of argument values.
583 static __isl_give isl_map *update_domain(__isl_take isl_map *access,
584 void *user)
586 isl_map *update = user;
587 isl_space *dim;
589 update = isl_map_copy(update);
591 dim = isl_map_get_space(access);
592 dim = isl_space_domain(dim);
593 if (!isl_space_is_wrapping(dim))
594 isl_space_free(dim);
595 else {
596 isl_map *id;
597 dim = isl_space_unwrap(dim);
598 dim = isl_space_range(dim);
599 dim = isl_space_map_from_set(dim);
600 id = isl_map_identity(dim);
601 update = isl_map_product(update, id);
604 return isl_map_apply_domain(access, update);
607 /* Modify all access relations in "expr" based on the given iteration space
608 * transformation.
610 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
611 __isl_take isl_map *update)
613 expr = pet_expr_foreach_access(expr, &update_domain, update);
614 isl_map_free(update);
615 return expr;
618 /* Construct a pet_stmt with given line number and statement
619 * number from a pet_expr.
620 * The initial iteration domain is the zero-dimensional universe.
621 * The name of the domain is given by "label" if it is non-NULL.
622 * Otherwise, the name is constructed as S_<id>.
623 * The domains of all access relations are modified to refer
624 * to the statement iteration domain.
626 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
627 __isl_take isl_id *label, int id, struct pet_expr *expr)
629 struct pet_stmt *stmt;
630 isl_space *dim;
631 isl_set *dom;
632 isl_map *sched;
633 isl_map *add_name;
634 char name[50];
636 if (!expr)
637 goto error;
639 stmt = isl_calloc_type(ctx, struct pet_stmt);
640 if (!stmt)
641 goto error;
643 dim = isl_space_set_alloc(ctx, 0, 0);
644 if (label)
645 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
646 else {
647 snprintf(name, sizeof(name), "S_%d", id);
648 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
650 dom = isl_set_universe(isl_space_copy(dim));
651 sched = isl_map_from_domain(isl_set_copy(dom));
653 dim = isl_space_from_range(dim);
654 add_name = isl_map_universe(dim);
655 expr = expr_update_domain(expr, add_name);
657 stmt->line = line;
658 stmt->domain = dom;
659 stmt->schedule = sched;
660 stmt->body = expr;
662 if (!stmt->domain || !stmt->schedule || !stmt->body)
663 return pet_stmt_free(stmt);
665 return stmt;
666 error:
667 isl_id_free(label);
668 return pet_expr_free(expr);
671 void *pet_stmt_free(struct pet_stmt *stmt)
673 int i;
675 if (!stmt)
676 return NULL;
678 isl_set_free(stmt->domain);
679 isl_map_free(stmt->schedule);
680 pet_expr_free(stmt->body);
682 for (i = 0; i < stmt->n_arg; ++i)
683 pet_expr_free(stmt->args[i]);
684 free(stmt->args);
686 free(stmt);
687 return NULL;
690 static void stmt_dump(struct pet_stmt *stmt, int indent)
692 int i;
694 if (!stmt)
695 return;
697 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
698 fprintf(stderr, "%*s", indent, "");
699 isl_set_dump(stmt->domain);
700 fprintf(stderr, "%*s", indent, "");
701 isl_map_dump(stmt->schedule);
702 expr_dump(stmt->body, indent);
703 for (i = 0; i < stmt->n_arg; ++i)
704 expr_dump(stmt->args[i], indent + 2);
707 void pet_stmt_dump(struct pet_stmt *stmt)
709 stmt_dump(stmt, 0);
712 struct pet_array *pet_array_free(struct pet_array *array)
714 if (!array)
715 return NULL;
717 isl_set_free(array->context);
718 isl_set_free(array->extent);
719 isl_set_free(array->value_bounds);
720 free(array->element_type);
722 free(array);
723 return NULL;
726 void pet_array_dump(struct pet_array *array)
728 if (!array)
729 return;
731 isl_set_dump(array->context);
732 isl_set_dump(array->extent);
733 isl_set_dump(array->value_bounds);
734 fprintf(stderr, "%s %s\n", array->element_type,
735 array->live_out ? "live-out" : "");
738 /* Alloc a pet_scop structure, with extra room for information that
739 * is only used during parsing.
741 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
743 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
746 /* Construct a pet_scop with room for n statements.
748 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
750 isl_space *space;
751 struct pet_scop *scop;
753 scop = pet_scop_alloc(ctx);
754 if (!scop)
755 return NULL;
757 space = isl_space_params_alloc(ctx, 0);
758 scop->context = isl_set_universe(isl_space_copy(space));
759 scop->context_value = isl_set_universe(space);
760 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
761 if (!scop->context || !scop->stmts)
762 return pet_scop_free(scop);
764 scop->n_stmt = n;
766 return scop;
769 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
771 return scop_alloc(ctx, 0);
774 /* Update "context" with respect to the valid parameter values for "access".
776 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
777 __isl_take isl_set *context)
779 context = isl_set_intersect(context,
780 isl_map_params(isl_map_copy(access)));
781 return context;
784 /* Update "context" with respect to the valid parameter values for "expr".
786 * If "expr" represents a ternary operator, then a parameter value
787 * needs to be valid for the condition and for at least one of the
788 * remaining two arguments.
789 * If the condition is an affine expression, then we can be a bit more specific.
790 * The parameter then has to be valid for the second argument for
791 * non-zero accesses and valid for the third argument for zero accesses.
793 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
794 __isl_take isl_set *context)
796 int i;
798 if (expr->type == pet_expr_ternary) {
799 int is_aff;
800 isl_set *context1, *context2;
802 is_aff = pet_expr_is_affine(expr->args[0]);
803 if (is_aff < 0)
804 goto error;
806 context = expr_extract_context(expr->args[0], context);
807 context1 = expr_extract_context(expr->args[1],
808 isl_set_copy(context));
809 context2 = expr_extract_context(expr->args[2], context);
811 if (is_aff) {
812 isl_map *access;
813 isl_set *zero_set;
815 access = isl_map_copy(expr->args[0]->acc.access);
816 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
817 zero_set = isl_map_params(access);
818 context1 = isl_set_subtract(context1,
819 isl_set_copy(zero_set));
820 context2 = isl_set_intersect(context2, zero_set);
823 context = isl_set_union(context1, context2);
824 context = isl_set_coalesce(context);
826 return context;
829 for (i = 0; i < expr->n_arg; ++i)
830 context = expr_extract_context(expr->args[i], context);
832 if (expr->type == pet_expr_access)
833 context = access_extract_context(expr->acc.access, context);
835 return context;
836 error:
837 isl_set_free(context);
838 return NULL;
841 /* Update "context" with respect to the valid parameter values for "stmt".
843 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
844 __isl_take isl_set *context)
846 int i;
848 for (i = 0; i < stmt->n_arg; ++i)
849 context = expr_extract_context(stmt->args[i], context);
851 context = expr_extract_context(stmt->body, context);
853 return context;
856 /* Construct a pet_scop that contains the given pet_stmt.
858 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
860 struct pet_scop *scop;
862 if (!stmt)
863 return NULL;
865 scop = scop_alloc(ctx, 1);
867 scop->context = stmt_extract_context(stmt, scop->context);
868 if (!scop->context)
869 goto error;
871 scop->stmts[0] = stmt;
873 return scop;
874 error:
875 pet_stmt_free(stmt);
876 pet_scop_free(scop);
877 return NULL;
880 /* Does "set" represent an element of an unnamed space, i.e.,
881 * does it represent an affine expression?
883 static int set_is_affine(__isl_keep isl_set *set)
885 int has_id;
887 has_id = isl_set_has_tuple_id(set);
888 if (has_id < 0)
889 return -1;
891 return !has_id;
894 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
895 * ext may be equal to either ext1 or ext2.
897 * The two skips that need to be combined are assumed to be affine expressions.
899 * We need to skip in ext if we need to skip in either ext1 or ext2.
900 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
902 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
903 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
904 enum pet_skip type)
906 isl_set *set, *skip1, *skip2;
908 if (!ext)
909 return NULL;
910 if (!ext1->skip[type] && !ext2->skip[type])
911 return ext;
912 if (!ext1->skip[type]) {
913 if (ext == ext2)
914 return ext;
915 ext->skip[type] = ext2->skip[type];
916 ext2->skip[type] = NULL;
917 return ext;
919 if (!ext2->skip[type]) {
920 if (ext == ext1)
921 return ext;
922 ext->skip[type] = ext1->skip[type];
923 ext1->skip[type] = NULL;
924 return ext;
927 if (!set_is_affine(ext1->skip[type]) ||
928 !set_is_affine(ext2->skip[type]))
929 isl_die(isl_set_get_ctx(ext1->skip[type]), isl_error_internal,
930 "can only combine affine skips",
931 return pet_scop_free(&ext->scop));
933 skip1 = isl_set_copy(ext1->skip[type]);
934 skip2 = isl_set_copy(ext2->skip[type]);
935 set = isl_set_intersect(
936 isl_set_fix_si(isl_set_copy(skip1), isl_dim_set, 0, 0),
937 isl_set_fix_si(isl_set_copy(skip2), isl_dim_set, 0, 0));
938 set = isl_set_union(set, isl_set_fix_si(skip1, isl_dim_set, 0, 1));
939 set = isl_set_union(set, isl_set_fix_si(skip2, isl_dim_set, 0, 1));
940 set = isl_set_coalesce(set);
941 isl_set_free(ext1->skip[type]);
942 ext1->skip[type] = NULL;
943 isl_set_free(ext2->skip[type]);
944 ext2->skip[type] = NULL;
945 ext->skip[type] = set;
946 if (!ext->skip[type])
947 return pet_scop_free(&ext->scop);
949 return ext;
952 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
953 * where type takes on the values pet_skip_now and pet_skip_later.
954 * scop may be equal to either scop1 or scop2.
956 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
957 struct pet_scop *scop1, struct pet_scop *scop2)
959 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
960 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
961 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
963 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
964 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
965 return &ext->scop;
968 /* Update scop->start and scop->end to include the region from "start"
969 * to "end". In particular, if scop->end == 0, then "scop" does not
970 * have any offset information yet and we simply take the information
971 * from "start" and "end". Otherwise, we update the fields if the
972 * region from "start" to "end" is not already included.
974 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
975 unsigned start, unsigned end)
977 if (!scop)
978 return NULL;
979 if (scop->end == 0) {
980 scop->start = start;
981 scop->end = end;
982 } else {
983 if (start < scop->start)
984 scop->start = start;
985 if (end > scop->end)
986 scop->end = end;
989 return scop;
992 /* Combine the offset information of "scop1" and "scop2" into "scop".
994 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
995 struct pet_scop *scop1, struct pet_scop *scop2)
997 if (scop1->end)
998 scop = pet_scop_update_start_end(scop,
999 scop1->start, scop1->end);
1000 if (scop2->end)
1001 scop = pet_scop_update_start_end(scop,
1002 scop2->start, scop2->end);
1003 return scop;
1006 /* Construct a pet_scop that contains the offset information,
1007 * arrays, statements and skip information in "scop1" and "scop2".
1009 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1010 struct pet_scop *scop2)
1012 int i;
1013 struct pet_scop *scop;
1015 if (!scop1 || !scop2)
1016 goto error;
1018 if (scop1->n_stmt == 0) {
1019 scop2 = scop_combine_skips(scop2, scop1, scop2);
1020 pet_scop_free(scop1);
1021 return scop2;
1024 if (scop2->n_stmt == 0) {
1025 scop1 = scop_combine_skips(scop1, scop1, scop2);
1026 pet_scop_free(scop2);
1027 return scop1;
1030 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1031 if (!scop)
1032 goto error;
1034 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1035 scop1->n_array + scop2->n_array);
1036 if (!scop->arrays)
1037 goto error;
1038 scop->n_array = scop1->n_array + scop2->n_array;
1040 for (i = 0; i < scop1->n_stmt; ++i) {
1041 scop->stmts[i] = scop1->stmts[i];
1042 scop1->stmts[i] = NULL;
1045 for (i = 0; i < scop2->n_stmt; ++i) {
1046 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1047 scop2->stmts[i] = NULL;
1050 for (i = 0; i < scop1->n_array; ++i) {
1051 scop->arrays[i] = scop1->arrays[i];
1052 scop1->arrays[i] = NULL;
1055 for (i = 0; i < scop2->n_array; ++i) {
1056 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1057 scop2->arrays[i] = NULL;
1060 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1061 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1062 scop = scop_combine_skips(scop, scop1, scop2);
1063 scop = scop_combine_start_end(scop, scop1, scop2);
1065 pet_scop_free(scop1);
1066 pet_scop_free(scop2);
1067 return scop;
1068 error:
1069 pet_scop_free(scop1);
1070 pet_scop_free(scop2);
1071 return NULL;
1074 /* Apply the skip condition "skip" to "scop".
1075 * That is, make sure "scop" is not executed when the condition holds.
1077 * If "skip" is an affine expression, we add the conditions under
1078 * which the expression is zero to the iteration domains.
1079 * Otherwise, we add a filter on the variable attaining the value zero.
1081 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1082 __isl_take isl_set *skip)
1084 isl_map *skip_map;
1085 int is_aff;
1087 if (!scop || !skip)
1088 goto error;
1090 is_aff = set_is_affine(skip);
1091 if (is_aff < 0)
1092 goto error;
1094 if (!is_aff)
1095 return pet_scop_filter(scop, isl_map_from_range(skip), 0);
1097 skip = isl_set_fix_si(skip, isl_dim_set, 0, 0);
1098 scop = pet_scop_restrict(scop, isl_set_params(skip));
1100 return scop;
1101 error:
1102 isl_set_free(skip);
1103 return pet_scop_free(scop);
1106 /* Construct a pet_scop that contains the arrays, statements and
1107 * skip information in "scop1" and "scop2", where the two scops
1108 * are executed "in sequence". That is, breaks and continues
1109 * in scop1 have an effect on scop2.
1111 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1112 struct pet_scop *scop2)
1114 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1115 scop2 = restrict_skip(scop2,
1116 pet_scop_get_skip(scop1, pet_skip_now));
1117 return pet_scop_add(ctx, scop1, scop2);
1120 /* Construct a pet_scop that contains the arrays, statements and
1121 * skip information in "scop1" and "scop2", where the two scops
1122 * are executed "in parallel". That is, any break or continue
1123 * in scop1 has no effect on scop2.
1125 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1126 struct pet_scop *scop2)
1128 return pet_scop_add(ctx, scop1, scop2);
1131 void *pet_scop_free(struct pet_scop *scop)
1133 int i;
1134 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1136 if (!scop)
1137 return NULL;
1138 isl_set_free(scop->context);
1139 isl_set_free(scop->context_value);
1140 if (scop->arrays)
1141 for (i = 0; i < scop->n_array; ++i)
1142 pet_array_free(scop->arrays[i]);
1143 free(scop->arrays);
1144 if (scop->stmts)
1145 for (i = 0; i < scop->n_stmt; ++i)
1146 pet_stmt_free(scop->stmts[i]);
1147 free(scop->stmts);
1148 isl_set_free(ext->skip[pet_skip_now]);
1149 isl_set_free(ext->skip[pet_skip_later]);
1150 free(scop);
1151 return NULL;
1154 void pet_scop_dump(struct pet_scop *scop)
1156 int i;
1157 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1159 if (!scop)
1160 return;
1162 isl_set_dump(scop->context);
1163 isl_set_dump(scop->context_value);
1164 for (i = 0; i < scop->n_array; ++i)
1165 pet_array_dump(scop->arrays[i]);
1166 for (i = 0; i < scop->n_stmt; ++i)
1167 pet_stmt_dump(scop->stmts[i]);
1169 if (ext->skip[0]) {
1170 fprintf(stderr, "skip\n");
1171 isl_set_dump(ext->skip[0]);
1172 isl_set_dump(ext->skip[1]);
1176 /* Return 1 if the two pet_arrays are equivalent.
1178 * We don't compare element_size as this may be target dependent.
1180 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1182 if (!array1 || !array2)
1183 return 0;
1185 if (!isl_set_is_equal(array1->context, array2->context))
1186 return 0;
1187 if (!isl_set_is_equal(array1->extent, array2->extent))
1188 return 0;
1189 if (!!array1->value_bounds != !!array2->value_bounds)
1190 return 0;
1191 if (array1->value_bounds &&
1192 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1193 return 0;
1194 if (strcmp(array1->element_type, array2->element_type))
1195 return 0;
1196 if (array1->live_out != array2->live_out)
1197 return 0;
1198 if (array1->uniquely_defined != array2->uniquely_defined)
1199 return 0;
1200 if (array1->declared != array2->declared)
1201 return 0;
1202 if (array1->exposed != array2->exposed)
1203 return 0;
1205 return 1;
1208 /* Return 1 if the two pet_stmts are equivalent.
1210 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1212 int i;
1214 if (!stmt1 || !stmt2)
1215 return 0;
1217 if (stmt1->line != stmt2->line)
1218 return 0;
1219 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1220 return 0;
1221 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1222 return 0;
1223 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1224 return 0;
1225 if (stmt1->n_arg != stmt2->n_arg)
1226 return 0;
1227 for (i = 0; i < stmt1->n_arg; ++i) {
1228 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1229 return 0;
1232 return 1;
1235 /* Return 1 if the two pet_scops are equivalent.
1237 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1239 int i;
1241 if (!scop1 || !scop2)
1242 return 0;
1244 if (!isl_set_is_equal(scop1->context, scop2->context))
1245 return 0;
1246 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1247 return 0;
1249 if (scop1->n_array != scop2->n_array)
1250 return 0;
1251 for (i = 0; i < scop1->n_array; ++i)
1252 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1253 return 0;
1255 if (scop1->n_stmt != scop2->n_stmt)
1256 return 0;
1257 for (i = 0; i < scop1->n_stmt; ++i)
1258 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1259 return 0;
1261 return 1;
1264 /* Prefix the schedule of "stmt" with an extra dimension with constant
1265 * value "pos".
1267 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1269 if (!stmt)
1270 return NULL;
1272 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1273 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1274 if (!stmt->schedule)
1275 return pet_stmt_free(stmt);
1277 return stmt;
1280 /* Prefix the schedules of all statements in "scop" with an extra
1281 * dimension with constant value "pos".
1283 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1285 int i;
1287 if (!scop)
1288 return NULL;
1290 for (i = 0; i < scop->n_stmt; ++i) {
1291 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1292 if (!scop->stmts[i])
1293 return pet_scop_free(scop);
1296 return scop;
1299 /* Given a set with a parameter at "param_pos" that refers to the
1300 * iterator, "move" the iterator to the first set dimension.
1301 * That is, essentially equate the parameter to the first set dimension
1302 * and then project it out.
1304 * The first set dimension may however refer to a virtual iterator,
1305 * while the parameter refers to the "real" iterator.
1306 * We therefore need to take into account the mapping "iv_map", which
1307 * maps the virtual iterator to the real iterator.
1308 * In particular, we equate the set dimension to the input of the map
1309 * and the parameter to the output of the map and then project out
1310 * everything we don't need anymore.
1312 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1313 int param_pos, __isl_take isl_map *iv_map)
1315 isl_map *map;
1316 map = isl_map_from_domain(set);
1317 map = isl_map_add_dims(map, isl_dim_out, 1);
1318 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1319 iv_map = isl_map_align_params(iv_map, isl_map_get_space(map));
1320 map = isl_map_apply_range(map, iv_map);
1321 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1322 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1323 return isl_map_domain(map);
1326 /* Data used in embed_access.
1327 * extend adds an iterator to the iteration domain
1328 * iv_map maps the virtual iterator to the real iterator
1329 * var_id represents the induction variable of the corresponding loop
1331 struct pet_embed_access {
1332 isl_map *extend;
1333 isl_map *iv_map;
1334 isl_id *var_id;
1337 /* Embed the access relation in an extra outer loop.
1339 * We first update the iteration domain to insert the extra dimension.
1341 * If the access refers to the induction variable, then it is
1342 * turned into an access to the set of integers with index (and value)
1343 * equal to the induction variable.
1345 * If the induction variable appears in the constraints (as a parameter),
1346 * then the parameter is equated to the newly introduced iteration
1347 * domain dimension and subsequently projected out.
1349 * Similarly, if the accessed array is a virtual array (with user
1350 * pointer equal to NULL), as created by create_test_access,
1351 * then it is extended along with the domain of the access.
1353 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
1354 void *user)
1356 struct pet_embed_access *data = user;
1357 isl_id *array_id = NULL;
1358 int pos;
1360 access = update_domain(access, data->extend);
1362 if (isl_map_has_tuple_id(access, isl_dim_out))
1363 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1364 if (array_id == data->var_id ||
1365 (array_id && !isl_id_get_user(array_id))) {
1366 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1367 access = isl_map_equate(access,
1368 isl_dim_in, 0, isl_dim_out, 0);
1369 if (array_id == data->var_id)
1370 access = isl_map_apply_range(access,
1371 isl_map_copy(data->iv_map));
1372 else
1373 access = isl_map_set_tuple_id(access, isl_dim_out,
1374 isl_id_copy(array_id));
1376 isl_id_free(array_id);
1378 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1379 if (pos >= 0) {
1380 isl_set *set = isl_map_wrap(access);
1381 set = internalize_iv(set, pos, isl_map_copy(data->iv_map));
1382 access = isl_set_unwrap(set);
1384 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1385 isl_id_copy(data->var_id));
1387 return access;
1390 /* Embed all access relations in "expr" in an extra loop.
1391 * "extend" inserts an outer loop iterator in the iteration domains.
1392 * "iv_map" maps the virtual iterator to the real iterator
1393 * "var_id" represents the induction variable.
1395 static struct pet_expr *expr_embed(struct pet_expr *expr,
1396 __isl_take isl_map *extend, __isl_take isl_map *iv_map,
1397 __isl_keep isl_id *var_id)
1399 struct pet_embed_access data =
1400 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1402 expr = pet_expr_foreach_access(expr, &embed_access, &data);
1403 isl_map_free(iv_map);
1404 isl_map_free(extend);
1405 return expr;
1408 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1409 * "dom" and schedule "sched". "var_id" represents the induction variable
1410 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1411 * That is, it maps the iterator used in "dom" and the domain of "sched"
1412 * to the iterator that some of the parameters in "stmt" may refer to.
1414 * The iteration domain and schedule of the statement are updated
1415 * according to the iteration domain and schedule of the new loop.
1416 * If stmt->domain is a wrapped map, then the iteration domain
1417 * is the domain of this map, so we need to be careful to adjust
1418 * this domain.
1420 * If the induction variable appears in the constraints (as a parameter)
1421 * of the current iteration domain or the schedule of the statement,
1422 * then the parameter is equated to the newly introduced iteration
1423 * domain dimension and subsequently projected out.
1425 * Finally, all access relations are updated based on the extra loop.
1427 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1428 __isl_take isl_set *dom, __isl_take isl_map *sched,
1429 __isl_take isl_map *iv_map, __isl_take isl_id *var_id)
1431 int i;
1432 int pos;
1433 isl_id *stmt_id;
1434 isl_space *dim;
1435 isl_map *extend;
1437 if (!stmt)
1438 goto error;
1440 if (isl_set_is_wrapping(stmt->domain)) {
1441 isl_map *map;
1442 isl_map *ext;
1443 isl_space *ran_dim;
1445 map = isl_set_unwrap(stmt->domain);
1446 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1447 ran_dim = isl_space_range(isl_map_get_space(map));
1448 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1449 isl_set_universe(ran_dim));
1450 map = isl_map_flat_domain_product(ext, map);
1451 map = isl_map_set_tuple_id(map, isl_dim_in,
1452 isl_id_copy(stmt_id));
1453 dim = isl_space_domain(isl_map_get_space(map));
1454 stmt->domain = isl_map_wrap(map);
1455 } else {
1456 stmt_id = isl_set_get_tuple_id(stmt->domain);
1457 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1458 stmt->domain);
1459 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1460 isl_id_copy(stmt_id));
1461 dim = isl_set_get_space(stmt->domain);
1464 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1465 if (pos >= 0)
1466 stmt->domain = internalize_iv(stmt->domain, pos,
1467 isl_map_copy(iv_map));
1469 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1470 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1471 isl_dim_in, stmt_id);
1473 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1474 if (pos >= 0) {
1475 isl_set *set = isl_map_wrap(stmt->schedule);
1476 set = internalize_iv(set, pos, isl_map_copy(iv_map));
1477 stmt->schedule = isl_set_unwrap(set);
1480 dim = isl_space_map_from_set(dim);
1481 extend = isl_map_identity(dim);
1482 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1483 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1484 isl_map_get_tuple_id(extend, isl_dim_out));
1485 for (i = 0; i < stmt->n_arg; ++i)
1486 stmt->args[i] = expr_embed(stmt->args[i], isl_map_copy(extend),
1487 isl_map_copy(iv_map), var_id);
1488 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1490 isl_set_free(dom);
1491 isl_id_free(var_id);
1493 for (i = 0; i < stmt->n_arg; ++i)
1494 if (!stmt->args[i])
1495 return pet_stmt_free(stmt);
1496 if (!stmt->domain || !stmt->schedule || !stmt->body)
1497 return pet_stmt_free(stmt);
1498 return stmt;
1499 error:
1500 isl_set_free(dom);
1501 isl_map_free(sched);
1502 isl_map_free(iv_map);
1503 isl_id_free(var_id);
1504 return NULL;
1507 /* Embed the given pet_array in an extra outer loop with iteration domain
1508 * "dom".
1509 * This embedding only has an effect on virtual arrays (those with
1510 * user pointer equal to NULL), which need to be extended along with
1511 * the iteration domain.
1513 static struct pet_array *pet_array_embed(struct pet_array *array,
1514 __isl_take isl_set *dom)
1516 isl_id *array_id = NULL;
1518 if (!array)
1519 goto error;
1521 if (isl_set_has_tuple_id(array->extent))
1522 array_id = isl_set_get_tuple_id(array->extent);
1524 if (array_id && !isl_id_get_user(array_id)) {
1525 array->extent = isl_set_flat_product(dom, array->extent);
1526 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1527 } else {
1528 isl_set_free(dom);
1529 isl_id_free(array_id);
1532 return array;
1533 error:
1534 isl_set_free(dom);
1535 return NULL;
1538 /* Project out all unnamed parameters from "set" and return the result.
1540 static __isl_give isl_set *set_project_out_unnamed_params(
1541 __isl_take isl_set *set)
1543 int i, n;
1545 n = isl_set_dim(set, isl_dim_param);
1546 for (i = n - 1; i >= 0; --i) {
1547 if (isl_set_has_dim_name(set, isl_dim_param, i))
1548 continue;
1549 set = isl_set_project_out(set, isl_dim_param, i, 1);
1552 return set;
1555 /* Update the context with respect to an embedding into a loop
1556 * with iteration domain "dom" and induction variable "id".
1557 * "iv_map" maps a possibly virtual iterator (used in "dom")
1558 * to the real iterator (parameter "id").
1560 * If the current context is independent of "id", we don't need
1561 * to do anything.
1562 * Otherwise, a parameter value is invalid for the embedding if
1563 * any of the corresponding iterator values is invalid.
1564 * That is, a parameter value is valid only if all the corresponding
1565 * iterator values are valid.
1566 * We therefore compute the set of parameters
1568 * forall i in dom : valid (i)
1570 * or
1572 * not exists i in dom : not valid(i)
1574 * i.e.,
1576 * not exists i in dom \ valid(i)
1578 * Before we subtract valid(i) from dom, we first need to map
1579 * the real iterator to the virtual iterator.
1581 * If there are any unnamed parameters in "dom", then we consider
1582 * a parameter value to be valid if it is valid for any value of those
1583 * unnamed parameters. They are therefore projected out at the end.
1585 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1586 __isl_keep isl_set *dom, __isl_keep isl_map *iv_map,
1587 __isl_keep isl_id *id)
1589 int pos;
1591 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1592 if (pos < 0)
1593 return context;
1595 context = isl_set_from_params(context);
1596 context = isl_set_add_dims(context, isl_dim_set, 1);
1597 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1598 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1599 context = isl_set_apply(context, isl_map_reverse(isl_map_copy(iv_map)));
1600 context = isl_set_subtract(isl_set_copy(dom), context);
1601 context = isl_set_params(context);
1602 context = isl_set_complement(context);
1603 context = set_project_out_unnamed_params(context);
1604 return context;
1607 /* Embed all statements and arrays in "scop" in an extra outer loop
1608 * with iteration domain "dom" and schedule "sched".
1609 * "id" represents the induction variable of the loop.
1610 * "iv_map" maps a possibly virtual iterator to the real iterator.
1611 * That is, it maps the iterator used in "dom" and the domain of "sched"
1612 * to the iterator that some of the parameters in "scop" may refer to.
1614 * Any skip conditions within the loop have no effect outside of the loop.
1615 * The caller is responsible for making sure skip[pet_skip_later] has been
1616 * taken into account.
1618 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1619 __isl_take isl_map *sched, __isl_take isl_map *iv_map,
1620 __isl_take isl_id *id)
1622 int i;
1624 if (!scop)
1625 goto error;
1627 pet_scop_reset_skip(scop, pet_skip_now);
1628 pet_scop_reset_skip(scop, pet_skip_later);
1630 scop->context = context_embed(scop->context, dom, iv_map, id);
1631 if (!scop->context)
1632 goto error;
1634 for (i = 0; i < scop->n_stmt; ++i) {
1635 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1636 isl_set_copy(dom), isl_map_copy(sched),
1637 isl_map_copy(iv_map), isl_id_copy(id));
1638 if (!scop->stmts[i])
1639 goto error;
1642 for (i = 0; i < scop->n_array; ++i) {
1643 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1644 isl_set_copy(dom));
1645 if (!scop->arrays[i])
1646 goto error;
1649 isl_set_free(dom);
1650 isl_map_free(sched);
1651 isl_map_free(iv_map);
1652 isl_id_free(id);
1653 return scop;
1654 error:
1655 isl_set_free(dom);
1656 isl_map_free(sched);
1657 isl_map_free(iv_map);
1658 isl_id_free(id);
1659 return pet_scop_free(scop);
1662 /* Add extra conditions on the parameters to iteration domain of "stmt".
1664 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1665 __isl_take isl_set *cond)
1667 if (!stmt)
1668 goto error;
1670 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1672 return stmt;
1673 error:
1674 isl_set_free(cond);
1675 return pet_stmt_free(stmt);
1678 /* Add extra conditions to scop->skip[type].
1680 * The new skip condition only holds if it held before
1681 * and the condition is true. It does not hold if it did not hold
1682 * before or the condition is false.
1684 * The skip condition is assumed to be an affine expression.
1686 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1687 enum pet_skip type, __isl_keep isl_set *cond)
1689 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1690 isl_set *skip;
1691 isl_set *set;
1693 if (!scop)
1694 return NULL;
1695 if (!ext->skip[type])
1696 return scop;
1698 if (!set_is_affine(ext->skip[type]))
1699 isl_die(isl_set_get_ctx(ext->skip[type]), isl_error_internal,
1700 "can only resrict affine skips",
1701 return pet_scop_free(scop));
1703 skip = ext->skip[type];
1704 skip = isl_set_intersect_params(skip, isl_set_copy(cond));
1705 set = isl_set_from_params(isl_set_copy(cond));
1706 set = isl_set_complement(set);
1707 set = isl_set_add_dims(set, isl_dim_set, 1);
1708 set = isl_set_fix_si(set, isl_dim_set, 0, 0);
1709 skip = isl_set_union(skip, set);
1710 ext->skip[type] = skip;
1711 if (!ext->skip[type])
1712 return pet_scop_free(scop);
1714 return scop;
1717 /* Add extra conditions on the parameters to all iteration domains
1718 * and skip conditions.
1720 * A parameter value is valid for the result if it was valid
1721 * for the original scop and satisfies "cond" or if it does
1722 * not satisfy "cond" as in this case the scop is not executed
1723 * and the original constraints on the parameters are irrelevant.
1725 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1726 __isl_take isl_set *cond)
1728 int i;
1730 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1731 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1733 if (!scop)
1734 goto error;
1736 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1737 scop->context = isl_set_union(scop->context,
1738 isl_set_complement(isl_set_copy(cond)));
1739 scop->context = isl_set_coalesce(scop->context);
1740 scop->context = set_project_out_unnamed_params(scop->context);
1741 if (!scop->context)
1742 goto error;
1744 for (i = 0; i < scop->n_stmt; ++i) {
1745 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1746 isl_set_copy(cond));
1747 if (!scop->stmts[i])
1748 goto error;
1751 isl_set_free(cond);
1752 return scop;
1753 error:
1754 isl_set_free(cond);
1755 return pet_scop_free(scop);
1758 /* Construct a map that inserts a filter value with name "id" and value
1759 * "satisfied" in the list of filter values embedded in the set space "space".
1761 * If "space" does not contain any filter values yet, we first create
1762 * a map that inserts 0 filter values, i.e.,
1764 * space -> [space -> []]
1766 * We can now assume that space is of the form [dom -> [filters]]
1767 * We construct an identity mapping on dom and a mapping on filters
1768 * that inserts the new filter
1770 * dom -> dom
1771 * [filters] -> [satisfied, filters]
1773 * and then compute the cross product
1775 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1777 static __isl_give isl_map *insert_filter_map(__isl_take isl_space *space,
1778 __isl_take isl_id *id, int satisfied)
1780 isl_space *space2;
1781 isl_map *map, *map_dom, *map_ran;
1782 isl_set *dom;
1784 if (isl_space_is_wrapping(space)) {
1785 space2 = isl_space_map_from_set(isl_space_copy(space));
1786 map = isl_map_identity(space2);
1787 space = isl_space_unwrap(space);
1788 } else {
1789 space = isl_space_from_domain(space);
1790 map = isl_map_universe(isl_space_copy(space));
1791 map = isl_map_reverse(isl_map_domain_map(map));
1794 space2 = isl_space_domain(isl_space_copy(space));
1795 map_dom = isl_map_identity(isl_space_map_from_set(space2));
1796 space = isl_space_range(space);
1797 map_ran = isl_map_identity(isl_space_map_from_set(space));
1798 map_ran = isl_map_insert_dims(map_ran, isl_dim_out, 0, 1);
1799 map_ran = isl_map_set_dim_id(map_ran, isl_dim_out, 0, id);
1800 map_ran = isl_map_fix_si(map_ran, isl_dim_out, 0, satisfied);
1802 map = isl_map_apply_range(map, isl_map_product(map_dom, map_ran));
1804 return map;
1807 /* Insert an argument expression corresponding to "test" in front
1808 * of the list of arguments described by *n_arg and *args.
1810 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
1811 __isl_keep isl_map *test)
1813 int i;
1814 isl_ctx *ctx = isl_map_get_ctx(test);
1816 if (!test)
1817 return -1;
1819 if (!*args) {
1820 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
1821 if (!*args)
1822 return -1;
1823 } else {
1824 struct pet_expr **ext;
1825 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
1826 if (!ext)
1827 return -1;
1828 for (i = 0; i < *n_arg; ++i)
1829 ext[1 + i] = (*args)[i];
1830 free(*args);
1831 *args = ext;
1833 (*n_arg)++;
1834 (*args)[0] = pet_expr_from_access(isl_map_copy(test));
1835 if (!(*args)[0])
1836 return -1;
1838 return 0;
1841 /* Make the expression "expr" depend on the value of "test"
1842 * being equal to "satisfied".
1844 * If "test" is an affine expression, we simply add the conditions
1845 * on the expression have the value "satisfied" to all access relations.
1847 * Otherwise, we add a filter to "expr" (which is then assumed to be
1848 * an access expression) corresponding to "test" being equal to "satisfied".
1850 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
1851 __isl_take isl_map *test, int satisfied)
1853 isl_id *id;
1854 isl_ctx *ctx;
1855 isl_space *space;
1856 isl_map *map;
1858 if (!expr || !test)
1859 goto error;
1861 if (!isl_map_has_tuple_id(test, isl_dim_out)) {
1862 test = isl_map_fix_si(test, isl_dim_out, 0, satisfied);
1863 return pet_expr_restrict(expr, isl_map_params(test));
1866 ctx = isl_map_get_ctx(test);
1867 if (expr->type != pet_expr_access)
1868 isl_die(ctx, isl_error_invalid,
1869 "can only filter access expressions", goto error);
1871 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1872 id = isl_map_get_tuple_id(test, isl_dim_out);
1873 map = insert_filter_map(space, id, satisfied);
1875 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
1876 if (!expr->acc.access)
1877 goto error;
1879 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
1880 goto error;
1882 isl_map_free(test);
1883 return expr;
1884 error:
1885 isl_map_free(test);
1886 return pet_expr_free(expr);
1889 /* Make the statement "stmt" depend on the value of "test"
1890 * being equal to "satisfied" by adjusting stmt->domain.
1892 * The domain of "test" corresponds to the (zero or more) outer dimensions
1893 * of the iteration domain.
1895 * We insert an argument corresponding to a read to "test"
1896 * from the iteration domain of "stmt" in front of the list of arguments.
1897 * We also insert a corresponding output dimension in the wrapped
1898 * map contained in stmt->domain, with value set to "satisfied".
1900 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1901 __isl_take isl_map *test, int satisfied)
1903 int i;
1904 isl_id *id;
1905 isl_ctx *ctx;
1906 isl_map *map, *add_dom;
1907 isl_space *space;
1908 isl_set *dom;
1909 int n_test_dom;
1911 if (!stmt || !test)
1912 goto error;
1914 id = isl_map_get_tuple_id(test, isl_dim_out);
1915 map = insert_filter_map(isl_set_get_space(stmt->domain), id, satisfied);
1916 stmt->domain = isl_set_apply(stmt->domain, map);
1918 space = isl_space_unwrap(isl_set_get_space(stmt->domain));
1919 dom = isl_set_universe(isl_space_domain(space));
1920 n_test_dom = isl_map_dim(test, isl_dim_in);
1921 add_dom = isl_map_from_range(dom);
1922 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
1923 for (i = 0; i < n_test_dom; ++i)
1924 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
1925 isl_dim_out, i);
1926 test = isl_map_apply_domain(test, add_dom);
1928 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1929 goto error;
1931 isl_map_free(test);
1932 return stmt;
1933 error:
1934 isl_map_free(test);
1935 return pet_stmt_free(stmt);
1938 /* Does "scop" have a skip condition of the given "type"?
1940 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1942 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1944 if (!scop)
1945 return -1;
1946 return ext->skip[type] != NULL;
1949 /* Does "scop" have a skip condition of the given "type" that
1950 * is an affine expression?
1952 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1954 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1956 if (!scop)
1957 return -1;
1958 if (!ext->skip[type])
1959 return 0;
1960 return set_is_affine(ext->skip[type]);
1963 /* Does "scop" have a skip condition of the given "type" that
1964 * is not an affine expression?
1966 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1968 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1969 int aff;
1971 if (!scop)
1972 return -1;
1973 if (!ext->skip[type])
1974 return 0;
1975 aff = set_is_affine(ext->skip[type]);
1976 if (aff < 0)
1977 return -1;
1978 return !aff;
1981 /* Does "scop" have a skip condition of the given "type" that
1982 * is affine and holds on the entire domain?
1984 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1986 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1987 isl_set *set;
1988 int is_aff;
1989 int is_univ;
1991 is_aff = pet_scop_has_affine_skip(scop, type);
1992 if (is_aff < 0 || !is_aff)
1993 return is_aff;
1995 set = isl_set_copy(ext->skip[type]);
1996 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
1997 set = isl_set_params(set);
1998 is_univ = isl_set_plain_is_universe(set);
1999 isl_set_free(set);
2001 return is_univ;
2004 /* Replace scop->skip[type] by "skip".
2006 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2007 enum pet_skip type, __isl_take isl_set *skip)
2009 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2011 if (!scop || !skip)
2012 goto error;
2014 isl_set_free(ext->skip[type]);
2015 ext->skip[type] = skip;
2017 return scop;
2018 error:
2019 isl_set_free(skip);
2020 return pet_scop_free(scop);
2023 /* Return a copy of scop->skip[type].
2025 __isl_give isl_set *pet_scop_get_skip(struct pet_scop *scop,
2026 enum pet_skip type)
2028 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2030 if (!scop)
2031 return NULL;
2033 return isl_set_copy(ext->skip[type]);
2036 /* Return a map to the skip condition of the given type.
2038 __isl_give isl_map *pet_scop_get_skip_map(struct pet_scop *scop,
2039 enum pet_skip type)
2041 return isl_map_from_range(pet_scop_get_skip(scop, type));
2044 /* Return an access pet_expr corresponding to the skip condition
2045 * of the given type.
2047 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2048 enum pet_skip type)
2050 return pet_expr_from_access(pet_scop_get_skip_map(scop, type));
2053 /* Drop the the skip condition scop->skip[type].
2055 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2057 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2059 if (!scop)
2060 return;
2062 isl_set_free(ext->skip[type]);
2063 ext->skip[type] = NULL;
2066 /* Make the skip condition (if any) depend on the value of "test" being
2067 * equal to "satisfied".
2069 * We only support the case where the original skip condition is universal,
2070 * i.e., where skipping is unconditional, and where satisfied == 1.
2071 * In this case, the skip condition is changed to skip only when
2072 * "test" is equal to one.
2074 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2075 enum pet_skip type, __isl_keep isl_map *test, int satisfied)
2077 int is_univ = 0;
2079 if (!scop)
2080 return NULL;
2081 if (!pet_scop_has_skip(scop, type))
2082 return scop;
2084 if (satisfied)
2085 is_univ = pet_scop_has_universal_skip(scop, type);
2086 if (is_univ < 0)
2087 return pet_scop_free(scop);
2088 if (satisfied && is_univ) {
2089 scop = pet_scop_set_skip(scop, type,
2090 isl_map_range(isl_map_copy(test)));
2091 if (!scop)
2092 return NULL;
2093 } else {
2094 isl_die(isl_map_get_ctx(test), isl_error_internal,
2095 "skip expression cannot be filtered",
2096 return pet_scop_free(scop));
2099 return scop;
2102 /* Make all statements in "scop" depend on the value of "test"
2103 * being equal to "satisfied" by adjusting their domains.
2105 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2106 __isl_take isl_map *test, int satisfied)
2108 int i;
2110 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2111 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2113 if (!scop || !test)
2114 goto error;
2116 for (i = 0; i < scop->n_stmt; ++i) {
2117 scop->stmts[i] = stmt_filter(scop->stmts[i],
2118 isl_map_copy(test), satisfied);
2119 if (!scop->stmts[i])
2120 goto error;
2123 isl_map_free(test);
2124 return scop;
2125 error:
2126 isl_map_free(test);
2127 return pet_scop_free(scop);
2130 /* Do the filters "i" and "j" always have the same value?
2132 static int equal_filter_values(__isl_keep isl_set *domain, int i, int j)
2134 isl_map *map, *test;
2135 int equal;
2137 map = isl_set_unwrap(isl_set_copy(domain));
2138 test = isl_map_universe(isl_map_get_space(map));
2139 test = isl_map_equate(test, isl_dim_out, i, isl_dim_out, j);
2140 equal = isl_map_is_subset(map, test);
2141 isl_map_free(map);
2142 isl_map_free(test);
2144 return equal;
2147 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2148 * access relation, the union of the two access relations.
2150 static struct pet_stmt *merge_filter_pair(struct pet_stmt *stmt, int i, int j)
2152 int k;
2153 isl_map *map;
2155 if (!stmt)
2156 return NULL;
2158 stmt->args[i]->acc.access = isl_map_union(stmt->args[i]->acc.access,
2159 isl_map_copy(stmt->args[j]->acc.access));
2160 stmt->args[i]->acc.access = isl_map_coalesce(stmt->args[i]->acc.access);
2162 pet_expr_free(stmt->args[j]);
2163 for (k = j; k < stmt->n_arg - 1; ++k)
2164 stmt->args[k] = stmt->args[k + 1];
2165 stmt->n_arg--;
2167 map = isl_set_unwrap(stmt->domain);
2168 map = isl_map_project_out(map, isl_dim_out, j, 1);
2169 stmt->domain = isl_map_wrap(map);
2171 if (!stmt->domain || !stmt->args[i]->acc.access)
2172 return pet_stmt_free(stmt);
2174 return stmt;
2177 /* Look for any pair of filters that access the same filter variable
2178 * and that have the same filter value and merge them into a single
2179 * filter with as filter access relation the union of the filter access
2180 * relations.
2182 static struct pet_stmt *stmt_merge_filters(struct pet_stmt *stmt)
2184 int i, j;
2185 isl_space *space_i, *space_j;
2187 if (!stmt)
2188 return NULL;
2189 if (stmt->n_arg <= 1)
2190 return stmt;
2192 for (i = 0; i < stmt->n_arg - 1; ++i) {
2193 if (stmt->args[i]->type != pet_expr_access)
2194 continue;
2195 if (pet_expr_is_affine(stmt->args[i]))
2196 continue;
2198 space_i = isl_map_get_space(stmt->args[i]->acc.access);
2200 for (j = stmt->n_arg - 1; j > i; --j) {
2201 int eq;
2203 if (stmt->args[j]->type != pet_expr_access)
2204 continue;
2205 if (pet_expr_is_affine(stmt->args[j]))
2206 continue;
2208 space_j = isl_map_get_space(stmt->args[j]->acc.access);
2210 eq = isl_space_is_equal(space_i, space_j);
2211 if (eq >= 0 && eq)
2212 eq = equal_filter_values(stmt->domain, i, j);
2213 if (eq >= 0 && eq)
2214 stmt = merge_filter_pair(stmt, i, j);
2216 isl_space_free(space_j);
2218 if (eq < 0 || !stmt)
2219 break;
2222 isl_space_free(space_i);
2224 if (j > i || !stmt)
2225 return pet_stmt_free(stmt);
2228 return stmt;
2231 /* Look for any pair of filters that access the same filter variable
2232 * and that have the same filter value and merge them into a single
2233 * filter with as filter access relation the union of the filter access
2234 * relations.
2236 struct pet_scop *pet_scop_merge_filters(struct pet_scop *scop)
2238 int i;
2240 if (!scop)
2241 return NULL;
2243 for (i = 0; i < scop->n_stmt; ++i) {
2244 scop->stmts[i] = stmt_merge_filters(scop->stmts[i]);
2245 if (!scop->stmts[i])
2246 return pet_scop_free(scop);
2249 return scop;
2252 /* Add all parameters in "expr" to "dim" and return the result.
2254 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2255 __isl_take isl_space *dim)
2257 int i;
2259 if (!expr)
2260 goto error;
2261 for (i = 0; i < expr->n_arg; ++i)
2263 dim = expr_collect_params(expr->args[i], dim);
2265 if (expr->type == pet_expr_access)
2266 dim = isl_space_align_params(dim,
2267 isl_map_get_space(expr->acc.access));
2269 return dim;
2270 error:
2271 isl_space_free(dim);
2272 return pet_expr_free(expr);
2275 /* Add all parameters in "stmt" to "dim" and return the result.
2277 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2278 __isl_take isl_space *dim)
2280 if (!stmt)
2281 goto error;
2283 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2284 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2285 dim = expr_collect_params(stmt->body, dim);
2287 return dim;
2288 error:
2289 isl_space_free(dim);
2290 return pet_stmt_free(stmt);
2293 /* Add all parameters in "array" to "dim" and return the result.
2295 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2296 __isl_take isl_space *dim)
2298 if (!array)
2299 goto error;
2301 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2302 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2304 return dim;
2305 error:
2306 pet_array_free(array);
2307 return isl_space_free(dim);
2310 /* Add all parameters in "scop" to "dim" and return the result.
2312 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2313 __isl_take isl_space *dim)
2315 int i;
2317 if (!scop)
2318 goto error;
2320 for (i = 0; i < scop->n_array; ++i)
2321 dim = array_collect_params(scop->arrays[i], dim);
2323 for (i = 0; i < scop->n_stmt; ++i)
2324 dim = stmt_collect_params(scop->stmts[i], dim);
2326 return dim;
2327 error:
2328 isl_space_free(dim);
2329 return pet_scop_free(scop);
2332 /* Add all parameters in "dim" to all access relations in "expr".
2334 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2335 __isl_take isl_space *dim)
2337 int i;
2339 if (!expr)
2340 goto error;
2342 for (i = 0; i < expr->n_arg; ++i) {
2343 expr->args[i] =
2344 expr_propagate_params(expr->args[i],
2345 isl_space_copy(dim));
2346 if (!expr->args[i])
2347 goto error;
2350 if (expr->type == pet_expr_access) {
2351 expr->acc.access = isl_map_align_params(expr->acc.access,
2352 isl_space_copy(dim));
2353 if (!expr->acc.access)
2354 goto error;
2357 isl_space_free(dim);
2358 return expr;
2359 error:
2360 isl_space_free(dim);
2361 return pet_expr_free(expr);
2364 /* Add all parameters in "dim" to the domain, schedule and
2365 * all access relations in "stmt".
2367 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2368 __isl_take isl_space *dim)
2370 if (!stmt)
2371 goto error;
2373 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2374 stmt->schedule = isl_map_align_params(stmt->schedule,
2375 isl_space_copy(dim));
2376 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2378 if (!stmt->domain || !stmt->schedule || !stmt->body)
2379 goto error;
2381 isl_space_free(dim);
2382 return stmt;
2383 error:
2384 isl_space_free(dim);
2385 return pet_stmt_free(stmt);
2388 /* Add all parameters in "dim" to "array".
2390 static struct pet_array *array_propagate_params(struct pet_array *array,
2391 __isl_take isl_space *dim)
2393 if (!array)
2394 goto error;
2396 array->context = isl_set_align_params(array->context,
2397 isl_space_copy(dim));
2398 array->extent = isl_set_align_params(array->extent,
2399 isl_space_copy(dim));
2400 if (array->value_bounds) {
2401 array->value_bounds = isl_set_align_params(array->value_bounds,
2402 isl_space_copy(dim));
2403 if (!array->value_bounds)
2404 goto error;
2407 if (!array->context || !array->extent)
2408 goto error;
2410 isl_space_free(dim);
2411 return array;
2412 error:
2413 isl_space_free(dim);
2414 return pet_array_free(array);
2417 /* Add all parameters in "dim" to "scop".
2419 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2420 __isl_take isl_space *dim)
2422 int i;
2424 if (!scop)
2425 goto error;
2427 for (i = 0; i < scop->n_array; ++i) {
2428 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2429 isl_space_copy(dim));
2430 if (!scop->arrays[i])
2431 goto error;
2434 for (i = 0; i < scop->n_stmt; ++i) {
2435 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2436 isl_space_copy(dim));
2437 if (!scop->stmts[i])
2438 goto error;
2441 isl_space_free(dim);
2442 return scop;
2443 error:
2444 isl_space_free(dim);
2445 return pet_scop_free(scop);
2448 /* Update all isl_sets and isl_maps in "scop" such that they all
2449 * have the same parameters.
2451 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2453 isl_space *dim;
2455 if (!scop)
2456 return NULL;
2458 dim = isl_set_get_space(scop->context);
2459 dim = scop_collect_params(scop, dim);
2461 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
2462 scop = scop_propagate_params(scop, dim);
2464 return scop;
2467 /* Check if the given access relation accesses a (0D) array that corresponds
2468 * to one of the parameters in "dim". If so, replace the array access
2469 * by an access to the set of integers with as index (and value)
2470 * that parameter.
2472 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
2473 __isl_take isl_space *dim)
2475 isl_id *array_id = NULL;
2476 int pos = -1;
2478 if (isl_map_has_tuple_id(access, isl_dim_out)) {
2479 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2480 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
2482 isl_space_free(dim);
2484 if (pos < 0) {
2485 isl_id_free(array_id);
2486 return access;
2489 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
2490 if (pos < 0) {
2491 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
2492 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
2493 pos = 0;
2494 } else
2495 isl_id_free(array_id);
2497 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2498 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
2500 return access;
2503 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2504 * in "dim" by a value equal to the corresponding parameter.
2506 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
2507 __isl_take isl_space *dim)
2509 int i;
2511 if (!expr)
2512 goto error;
2514 for (i = 0; i < expr->n_arg; ++i) {
2515 expr->args[i] =
2516 expr_detect_parameter_accesses(expr->args[i],
2517 isl_space_copy(dim));
2518 if (!expr->args[i])
2519 goto error;
2522 if (expr->type == pet_expr_access) {
2523 expr->acc.access = access_detect_parameter(expr->acc.access,
2524 isl_space_copy(dim));
2525 if (!expr->acc.access)
2526 goto error;
2529 isl_space_free(dim);
2530 return expr;
2531 error:
2532 isl_space_free(dim);
2533 return pet_expr_free(expr);
2536 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2537 * in "dim" by a value equal to the corresponding parameter.
2539 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2540 __isl_take isl_space *dim)
2542 if (!stmt)
2543 goto error;
2545 stmt->body = expr_detect_parameter_accesses(stmt->body,
2546 isl_space_copy(dim));
2548 if (!stmt->domain || !stmt->schedule || !stmt->body)
2549 goto error;
2551 isl_space_free(dim);
2552 return stmt;
2553 error:
2554 isl_space_free(dim);
2555 return pet_stmt_free(stmt);
2558 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2559 * in "dim" by a value equal to the corresponding parameter.
2561 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2562 __isl_take isl_space *dim)
2564 int i;
2566 if (!scop)
2567 goto error;
2569 for (i = 0; i < scop->n_stmt; ++i) {
2570 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2571 isl_space_copy(dim));
2572 if (!scop->stmts[i])
2573 goto error;
2576 isl_space_free(dim);
2577 return scop;
2578 error:
2579 isl_space_free(dim);
2580 return pet_scop_free(scop);
2583 /* Replace all accesses to (0D) arrays that correspond to any of
2584 * the parameters used in "scop" by a value equal
2585 * to the corresponding parameter.
2587 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2589 isl_space *dim;
2591 if (!scop)
2592 return NULL;
2594 dim = isl_set_get_space(scop->context);
2595 dim = scop_collect_params(scop, dim);
2597 scop = scop_detect_parameter_accesses(scop, dim);
2599 return scop;
2602 /* Add all read access relations (if "read" is set) and/or all write
2603 * access relations (if "write" is set) to "accesses" and return the result.
2605 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
2606 int read, int write, __isl_take isl_union_map *accesses)
2608 int i;
2609 isl_id *id;
2610 isl_space *dim;
2612 if (!expr)
2613 return NULL;
2615 for (i = 0; i < expr->n_arg; ++i)
2616 accesses = expr_collect_accesses(expr->args[i],
2617 read, write, accesses);
2619 if (expr->type == pet_expr_access &&
2620 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
2621 ((read && expr->acc.read) || (write && expr->acc.write)))
2622 accesses = isl_union_map_add_map(accesses,
2623 isl_map_copy(expr->acc.access));
2625 return accesses;
2628 /* Collect and return all read access relations (if "read" is set)
2629 * and/or all write access relations (if "write" is set) in "stmt".
2631 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2632 int read, int write, __isl_take isl_space *dim)
2634 isl_union_map *accesses;
2636 if (!stmt)
2637 return NULL;
2639 accesses = isl_union_map_empty(dim);
2640 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
2641 accesses = isl_union_map_intersect_domain(accesses,
2642 isl_union_set_from_set(isl_set_copy(stmt->domain)));
2644 return accesses;
2647 /* Collect and return all read access relations (if "read" is set)
2648 * and/or all write access relations (if "write" is set) in "scop".
2650 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2651 int read, int write)
2653 int i;
2654 isl_union_map *accesses;
2656 if (!scop)
2657 return NULL;
2659 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2661 for (i = 0; i < scop->n_stmt; ++i) {
2662 isl_union_map *accesses_i;
2663 isl_space *dim = isl_set_get_space(scop->context);
2664 accesses_i = stmt_collect_accesses(scop->stmts[i],
2665 read, write, dim);
2666 accesses = isl_union_map_union(accesses, accesses_i);
2669 return accesses;
2672 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
2674 return scop_collect_accesses(scop, 1, 0);
2677 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
2679 return scop_collect_accesses(scop, 0, 1);
2682 /* Collect and return the union of iteration domains in "scop".
2684 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2686 int i;
2687 isl_set *domain_i;
2688 isl_union_set *domain;
2690 if (!scop)
2691 return NULL;
2693 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2695 for (i = 0; i < scop->n_stmt; ++i) {
2696 domain_i = isl_set_copy(scop->stmts[i]->domain);
2697 domain = isl_union_set_add_set(domain, domain_i);
2700 return domain;
2703 /* Collect and return the schedules of the statements in "scop".
2704 * The range is normalized to the maximal number of scheduling
2705 * dimensions.
2707 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2709 int i, j;
2710 isl_map *schedule_i;
2711 isl_union_map *schedule;
2712 int depth, max_depth = 0;
2714 if (!scop)
2715 return NULL;
2717 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2719 for (i = 0; i < scop->n_stmt; ++i) {
2720 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2721 if (depth > max_depth)
2722 max_depth = depth;
2725 for (i = 0; i < scop->n_stmt; ++i) {
2726 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2727 depth = isl_map_dim(schedule_i, isl_dim_out);
2728 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2729 max_depth - depth);
2730 for (j = depth; j < max_depth; ++j)
2731 schedule_i = isl_map_fix_si(schedule_i,
2732 isl_dim_out, j, 0);
2733 schedule = isl_union_map_add_map(schedule, schedule_i);
2736 return schedule;
2739 /* Does expression "expr" write to "id"?
2741 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
2743 int i;
2744 isl_id *write_id;
2746 for (i = 0; i < expr->n_arg; ++i) {
2747 int writes = expr_writes(expr->args[i], id);
2748 if (writes < 0 || writes)
2749 return writes;
2752 if (expr->type != pet_expr_access)
2753 return 0;
2754 if (!expr->acc.write)
2755 return 0;
2756 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
2757 return 0;
2759 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
2760 isl_id_free(write_id);
2762 if (!write_id)
2763 return -1;
2765 return write_id == id;
2768 /* Does statement "stmt" write to "id"?
2770 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
2772 return expr_writes(stmt->body, id);
2775 /* Is there any write access in "scop" that accesses "id"?
2777 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
2779 int i;
2781 if (!scop)
2782 return -1;
2784 for (i = 0; i < scop->n_stmt; ++i) {
2785 int writes = stmt_writes(scop->stmts[i], id);
2786 if (writes < 0 || writes)
2787 return writes;
2790 return 0;
2793 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2795 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
2797 int i, n;
2799 n = isl_set_dim(set, isl_dim_param);
2800 for (i = 0; i < n; ++i) {
2801 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2802 const char *name = isl_id_get_name(id);
2803 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
2804 isl_id_free(id);
2807 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
2808 isl_id *id = isl_set_get_tuple_id(set);
2809 const char *name = isl_id_get_name(id);
2810 set = isl_set_set_tuple_name(set, name);
2811 isl_id_free(id);
2814 return set;
2817 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2819 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
2821 int i, n;
2823 n = isl_map_dim(map, isl_dim_param);
2824 for (i = 0; i < n; ++i) {
2825 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2826 const char *name = isl_id_get_name(id);
2827 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2828 isl_id_free(id);
2831 if (isl_map_has_tuple_id(map, isl_dim_in)) {
2832 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
2833 const char *name = isl_id_get_name(id);
2834 map = isl_map_set_tuple_name(map, isl_dim_in, name);
2835 isl_id_free(id);
2838 if (isl_map_has_tuple_id(map, isl_dim_out)) {
2839 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
2840 const char *name = isl_id_get_name(id);
2841 map = isl_map_set_tuple_name(map, isl_dim_out, name);
2842 isl_id_free(id);
2845 return map;
2848 /* Reset the user pointer on all parameter ids in "array".
2850 static struct pet_array *array_anonymize(struct pet_array *array)
2852 if (!array)
2853 return NULL;
2855 array->context = set_anonymize(array->context);
2856 array->extent = set_anonymize(array->extent);
2857 if (!array->context || !array->extent)
2858 return pet_array_free(array);
2860 return array;
2863 /* Reset the user pointer on all parameter and tuple ids in "access".
2865 static __isl_give isl_map *access_anonymize(__isl_take isl_map *access,
2866 void *user)
2868 access = map_anonymize(access);
2870 return access;
2873 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2875 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2877 int i;
2878 isl_space *space;
2879 isl_set *domain;
2881 if (!stmt)
2882 return NULL;
2884 stmt->domain = set_anonymize(stmt->domain);
2885 stmt->schedule = map_anonymize(stmt->schedule);
2886 if (!stmt->domain || !stmt->schedule)
2887 return pet_stmt_free(stmt);
2889 for (i = 0; i < stmt->n_arg; ++i) {
2890 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2891 &access_anonymize, NULL);
2892 if (!stmt->args[i])
2893 return pet_stmt_free(stmt);
2896 stmt->body = pet_expr_foreach_access(stmt->body,
2897 &access_anonymize, NULL);
2898 if (!stmt->body)
2899 return pet_stmt_free(stmt);
2901 return stmt;
2904 /* Reset the user pointer on all parameter and tuple ids in "scop".
2906 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2908 int i;
2910 if (!scop)
2911 return NULL;
2913 scop->context = set_anonymize(scop->context);
2914 scop->context_value = set_anonymize(scop->context_value);
2915 if (!scop->context || !scop->context_value)
2916 return pet_scop_free(scop);
2918 for (i = 0; i < scop->n_array; ++i) {
2919 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2920 if (!scop->arrays[i])
2921 return pet_scop_free(scop);
2924 for (i = 0; i < scop->n_stmt; ++i) {
2925 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2926 if (!scop->stmts[i])
2927 return pet_scop_free(scop);
2930 return scop;
2933 /* Given a set "domain", return a wrapped relation with the given set
2934 * as domain and a range of dimension "n_arg", where each coordinate
2935 * is either unbounded or, if the corresponding element of args is of
2936 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2938 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2939 unsigned n_arg, struct pet_expr **args,
2940 __isl_keep isl_union_map *value_bounds)
2942 int i;
2943 isl_map *map;
2944 isl_space *space;
2945 isl_ctx *ctx = isl_set_get_ctx(domain);
2947 map = isl_map_from_domain(domain);
2948 space = isl_map_get_space(map);
2949 space = isl_space_add_dims(space, isl_dim_out, 1);
2951 for (i = 0; i < n_arg; ++i) {
2952 isl_map *map_i;
2953 struct pet_expr *arg = args[i];
2954 isl_id *id;
2955 isl_space *space2;
2957 map_i = isl_map_universe(isl_space_copy(space));
2958 if (arg->type == pet_expr_access) {
2959 isl_map *vb;
2960 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2961 space2 = isl_space_alloc(ctx, 0, 0, 1);
2962 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2963 vb = isl_union_map_extract_map(value_bounds, space2);
2964 if (!isl_map_plain_is_empty(vb))
2965 map_i = isl_map_intersect_range(map_i,
2966 isl_map_range(vb));
2967 else
2968 isl_map_free(vb);
2970 map = isl_map_flat_range_product(map, map_i);
2972 isl_space_free(space);
2974 return isl_map_wrap(map);
2977 /* Data used in access_gist() callback.
2979 struct pet_access_gist_data {
2980 isl_set *domain;
2981 isl_union_map *value_bounds;
2984 /* Given an expression "expr" of type pet_expr_access, compute
2985 * the gist of the associated access relation with respect to
2986 * data->domain and the bounds on the values of the arguments
2987 * of the expression.
2989 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
2991 struct pet_access_gist_data *data = user;
2992 isl_set *domain;
2994 domain = isl_set_copy(data->domain);
2995 if (expr->n_arg > 0)
2996 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
2997 data->value_bounds);
2999 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
3000 if (!expr->acc.access)
3001 return pet_expr_free(expr);
3003 return expr;
3006 /* Compute the gist of the iteration domain and all access relations
3007 * of "stmt" based on the constraints on the parameters specified by "context"
3008 * and the constraints on the values of nested accesses specified
3009 * by "value_bounds".
3011 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3012 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3014 int i;
3015 isl_space *space;
3016 isl_set *domain;
3017 struct pet_access_gist_data data;
3019 if (!stmt)
3020 return NULL;
3022 data.domain = isl_set_copy(stmt->domain);
3023 data.value_bounds = value_bounds;
3024 if (stmt->n_arg > 0)
3025 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3027 data.domain = isl_set_intersect_params(data.domain,
3028 isl_set_copy(context));
3030 for (i = 0; i < stmt->n_arg; ++i) {
3031 stmt->args[i] = pet_expr_foreach_access_expr(stmt->args[i],
3032 &access_gist, &data);
3033 if (!stmt->args[i])
3034 goto error;
3037 stmt->body = pet_expr_foreach_access_expr(stmt->body,
3038 &access_gist, &data);
3039 if (!stmt->body)
3040 goto error;
3042 isl_set_free(data.domain);
3044 space = isl_set_get_space(stmt->domain);
3045 if (isl_space_is_wrapping(space))
3046 space = isl_space_domain(isl_space_unwrap(space));
3047 domain = isl_set_universe(space);
3048 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3049 if (stmt->n_arg > 0)
3050 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3051 value_bounds);
3052 stmt->domain = isl_set_gist(stmt->domain, domain);
3053 if (!stmt->domain)
3054 return pet_stmt_free(stmt);
3056 return stmt;
3057 error:
3058 isl_set_free(data.domain);
3059 return pet_stmt_free(stmt);
3062 /* Compute the gist of the extent of the array
3063 * based on the constraints on the parameters specified by "context".
3065 static struct pet_array *array_gist(struct pet_array *array,
3066 __isl_keep isl_set *context)
3068 if (!array)
3069 return NULL;
3071 array->extent = isl_set_gist_params(array->extent,
3072 isl_set_copy(context));
3073 if (!array->extent)
3074 return pet_array_free(array);
3076 return array;
3079 /* Compute the gist of all sets and relations in "scop"
3080 * based on the constraints on the parameters specified by "scop->context"
3081 * and the constraints on the values of nested accesses specified
3082 * by "value_bounds".
3084 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3085 __isl_keep isl_union_map *value_bounds)
3087 int i;
3089 if (!scop)
3090 return NULL;
3092 scop->context = isl_set_coalesce(scop->context);
3093 if (!scop->context)
3094 return pet_scop_free(scop);
3096 for (i = 0; i < scop->n_array; ++i) {
3097 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3098 if (!scop->arrays[i])
3099 return pet_scop_free(scop);
3102 for (i = 0; i < scop->n_stmt; ++i) {
3103 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3104 value_bounds);
3105 if (!scop->stmts[i])
3106 return pet_scop_free(scop);
3109 return scop;
3112 /* Intersect the context of "scop" with "context".
3113 * To ensure that we don't introduce any unnamed parameters in
3114 * the context of "scop", we first remove the unnamed parameters
3115 * from "context".
3117 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3118 __isl_take isl_set *context)
3120 if (!scop)
3121 goto error;
3123 context = set_project_out_unnamed_params(context);
3124 scop->context = isl_set_intersect(scop->context, context);
3125 if (!scop->context)
3126 return pet_scop_free(scop);
3128 return scop;
3129 error:
3130 isl_set_free(context);
3131 return pet_scop_free(scop);
3134 /* Drop the current context of "scop". That is, replace the context
3135 * by a universal set.
3137 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3139 isl_space *space;
3141 if (!scop)
3142 return NULL;
3144 space = isl_set_get_space(scop->context);
3145 isl_set_free(scop->context);
3146 scop->context = isl_set_universe(space);
3147 if (!scop->context)
3148 return pet_scop_free(scop);
3150 return scop;
3153 /* Append "array" to the arrays of "scop".
3155 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3156 struct pet_array *array)
3158 isl_ctx *ctx;
3159 struct pet_array **arrays;
3161 if (!array || !scop)
3162 goto error;
3164 ctx = isl_set_get_ctx(scop->context);
3165 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3166 scop->n_array + 1);
3167 if (!arrays)
3168 goto error;
3169 scop->arrays = arrays;
3170 scop->arrays[scop->n_array] = array;
3171 scop->n_array++;
3173 return scop;
3174 error:
3175 pet_array_free(array);
3176 return pet_scop_free(scop);