add pet_expr_foreach_access_expr
[pet.git] / scop.c
blob4e281b6c8a6f327ab1dfca5c724d296cb513efdb
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 <string.h>
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
39 #include "scop.h"
41 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
43 static char *type_str[] = {
44 [pet_expr_access] = "access",
45 [pet_expr_call] = "call",
46 [pet_expr_cast] = "cast",
47 [pet_expr_double] = "double",
48 [pet_expr_unary] = "unary",
49 [pet_expr_binary] = "binary",
50 [pet_expr_ternary] = "ternary"
53 static char *op_str[] = {
54 [pet_op_add_assign] = "+=",
55 [pet_op_sub_assign] = "-=",
56 [pet_op_mul_assign] = "*=",
57 [pet_op_div_assign] = "/=",
58 [pet_op_assign] = "=",
59 [pet_op_add] = "+",
60 [pet_op_sub] = "-",
61 [pet_op_mul] = "*",
62 [pet_op_div] = "/",
63 [pet_op_mod] = "%",
64 [pet_op_eq] = "==",
65 [pet_op_le] = "<=",
66 [pet_op_lt] = "<",
67 [pet_op_gt] = ">",
68 [pet_op_minus] = "-",
69 [pet_op_post_inc] = "++",
70 [pet_op_post_dec] = "--",
71 [pet_op_pre_inc] = "++",
72 [pet_op_pre_dec] = "--",
73 [pet_op_address_of] = "&",
74 [pet_op_kill] = "kill"
77 /* pet_scop with extra information that is only used during parsing.
79 * In particular, we keep track of conditions under which we want
80 * to skip the rest of the current loop iteration (skip[pet_skip_now])
81 * and of conditions under which we want to skip subsequent
82 * loop iterations (skip[pet_skip_later]).
84 * The conditions are represented either by a variable, which
85 * is assumed to attain values zero and one, or by a boolean affine
86 * expression. The condition holds if the variable has value one
87 * or if the affine expression has value one (typically for only
88 * part of the parameter space).
90 * A missing condition (skip[type] == NULL) means that we don't want
91 * to skip anything.
93 struct pet_scop_ext {
94 struct pet_scop scop;
96 isl_set *skip[2];
99 const char *pet_op_str(enum pet_op_type op)
101 return op_str[op];
104 int pet_op_is_inc_dec(enum pet_op_type op)
106 return op == pet_op_post_inc || op == pet_op_post_dec ||
107 op == pet_op_pre_inc || op == pet_op_pre_dec;
110 const char *pet_type_str(enum pet_expr_type type)
112 return type_str[type];
115 enum pet_op_type pet_str_op(const char *str)
117 int i;
119 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
120 if (!strcmp(op_str[i], str))
121 return i;
123 return -1;
126 enum pet_expr_type pet_str_type(const char *str)
128 int i;
130 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
131 if (!strcmp(type_str[i], str))
132 return i;
134 return -1;
137 /* Construct a pet_expr from an access relation.
138 * By default, it is considered to be a read access.
140 struct pet_expr *pet_expr_from_access(__isl_take isl_map *access)
142 isl_ctx *ctx = isl_map_get_ctx(access);
143 struct pet_expr *expr;
145 if (!access)
146 return NULL;
147 expr = isl_calloc_type(ctx, struct pet_expr);
148 if (!expr)
149 goto error;
151 expr->type = pet_expr_access;
152 expr->acc.access = access;
153 expr->acc.read = 1;
154 expr->acc.write = 0;
156 return expr;
157 error:
158 isl_map_free(access);
159 return NULL;
162 /* Construct a pet_expr that kills the elements specified by "access".
164 struct pet_expr *pet_expr_kill_from_access(__isl_take isl_map *access)
166 isl_ctx *ctx;
167 struct pet_expr *expr;
169 ctx = isl_map_get_ctx(access);
170 expr = pet_expr_from_access(access);
171 if (!expr)
172 return NULL;
173 expr->acc.read = 0;
174 return pet_expr_new_unary(ctx, pet_op_kill, expr);
177 /* Construct a unary pet_expr that performs "op" on "arg".
179 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
180 struct pet_expr *arg)
182 struct pet_expr *expr;
184 if (!arg)
185 goto error;
186 expr = isl_alloc_type(ctx, struct pet_expr);
187 if (!expr)
188 goto error;
190 expr->type = pet_expr_unary;
191 expr->op = op;
192 expr->n_arg = 1;
193 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
194 if (!expr->args)
195 goto error;
196 expr->args[pet_un_arg] = arg;
198 return expr;
199 error:
200 pet_expr_free(arg);
201 return NULL;
204 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
206 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
207 struct pet_expr *lhs, struct pet_expr *rhs)
209 struct pet_expr *expr;
211 if (!lhs || !rhs)
212 goto error;
213 expr = isl_alloc_type(ctx, struct pet_expr);
214 if (!expr)
215 goto error;
217 expr->type = pet_expr_binary;
218 expr->op = op;
219 expr->n_arg = 2;
220 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
221 if (!expr->args)
222 goto error;
223 expr->args[pet_bin_lhs] = lhs;
224 expr->args[pet_bin_rhs] = rhs;
226 return expr;
227 error:
228 pet_expr_free(lhs);
229 pet_expr_free(rhs);
230 return NULL;
233 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
235 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
236 struct pet_expr *lhs, struct pet_expr *rhs)
238 struct pet_expr *expr;
240 if (!cond || !lhs || !rhs)
241 goto error;
242 expr = isl_alloc_type(ctx, struct pet_expr);
243 if (!expr)
244 goto error;
246 expr->type = pet_expr_ternary;
247 expr->n_arg = 3;
248 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
249 if (!expr->args)
250 goto error;
251 expr->args[pet_ter_cond] = cond;
252 expr->args[pet_ter_true] = lhs;
253 expr->args[pet_ter_false] = rhs;
255 return expr;
256 error:
257 pet_expr_free(cond);
258 pet_expr_free(lhs);
259 pet_expr_free(rhs);
260 return NULL;
263 /* Construct a call pet_expr that calls function "name" with "n_arg"
264 * arguments. The caller is responsible for filling in the arguments.
266 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
267 unsigned n_arg)
269 struct pet_expr *expr;
271 expr = isl_alloc_type(ctx, struct pet_expr);
272 if (!expr)
273 return NULL;
275 expr->type = pet_expr_call;
276 expr->n_arg = n_arg;
277 expr->name = strdup(name);
278 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
279 if (!expr->name || !expr->args)
280 return pet_expr_free(expr);
282 return expr;
285 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
287 struct pet_expr *pet_expr_new_cast(isl_ctx *ctx, const char *type_name,
288 struct pet_expr *arg)
290 struct pet_expr *expr;
292 if (!arg)
293 return NULL;
295 expr = isl_alloc_type(ctx, struct pet_expr);
296 if (!expr)
297 goto error;
299 expr->type = pet_expr_cast;
300 expr->n_arg = 1;
301 expr->type_name = strdup(type_name);
302 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
303 if (!expr->type_name || !expr->args)
304 goto error;
306 expr->args[0] = arg;
308 return expr;
309 error:
310 pet_expr_free(arg);
311 pet_expr_free(expr);
312 return NULL;
315 /* Construct a pet_expr that represents the double "d".
317 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
319 struct pet_expr *expr;
321 expr = isl_calloc_type(ctx, struct pet_expr);
322 if (!expr)
323 return NULL;
325 expr->type = pet_expr_double;
326 expr->d.val = val;
327 expr->d.s = strdup(s);
328 if (!expr->d.s)
329 return pet_expr_free(expr);
331 return expr;
334 void *pet_expr_free(struct pet_expr *expr)
336 int i;
338 if (!expr)
339 return NULL;
341 for (i = 0; i < expr->n_arg; ++i)
342 pet_expr_free(expr->args[i]);
343 free(expr->args);
345 switch (expr->type) {
346 case pet_expr_access:
347 isl_map_free(expr->acc.access);
348 break;
349 case pet_expr_call:
350 free(expr->name);
351 break;
352 case pet_expr_cast:
353 free(expr->type_name);
354 break;
355 case pet_expr_double:
356 free(expr->d.s);
357 break;
358 case pet_expr_unary:
359 case pet_expr_binary:
360 case pet_expr_ternary:
361 break;
364 free(expr);
365 return NULL;
368 static void expr_dump(struct pet_expr *expr, int indent)
370 int i;
372 if (!expr)
373 return;
375 fprintf(stderr, "%*s", indent, "");
377 switch (expr->type) {
378 case pet_expr_double:
379 fprintf(stderr, "%s\n", expr->d.s);
380 break;
381 case pet_expr_access:
382 isl_map_dump(expr->acc.access);
383 fprintf(stderr, "%*sread: %d\n", indent + 2,
384 "", expr->acc.read);
385 fprintf(stderr, "%*swrite: %d\n", indent + 2,
386 "", expr->acc.write);
387 for (i = 0; i < expr->n_arg; ++i)
388 expr_dump(expr->args[i], indent + 2);
389 break;
390 case pet_expr_unary:
391 fprintf(stderr, "%s\n", op_str[expr->op]);
392 expr_dump(expr->args[pet_un_arg], indent + 2);
393 break;
394 case pet_expr_binary:
395 fprintf(stderr, "%s\n", op_str[expr->op]);
396 expr_dump(expr->args[pet_bin_lhs], indent + 2);
397 expr_dump(expr->args[pet_bin_rhs], indent + 2);
398 break;
399 case pet_expr_ternary:
400 fprintf(stderr, "?:\n");
401 expr_dump(expr->args[pet_ter_cond], indent + 2);
402 expr_dump(expr->args[pet_ter_true], indent + 2);
403 expr_dump(expr->args[pet_ter_false], indent + 2);
404 break;
405 case pet_expr_call:
406 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
407 for (i = 0; i < expr->n_arg; ++i)
408 expr_dump(expr->args[i], indent + 2);
409 break;
410 case pet_expr_cast:
411 fprintf(stderr, "(%s)\n", expr->type_name);
412 for (i = 0; i < expr->n_arg; ++i)
413 expr_dump(expr->args[i], indent + 2);
414 break;
418 void pet_expr_dump(struct pet_expr *expr)
420 expr_dump(expr, 0);
423 /* Does "expr" represent an access to an unnamed space, i.e.,
424 * does it represent an affine expression?
426 int pet_expr_is_affine(struct pet_expr *expr)
428 int has_id;
430 if (!expr)
431 return -1;
432 if (expr->type != pet_expr_access)
433 return 0;
435 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
436 if (has_id < 0)
437 return -1;
439 return !has_id;
442 /* Return 1 if the two pet_exprs are equivalent.
444 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
446 int i;
448 if (!expr1 || !expr2)
449 return 0;
451 if (expr1->type != expr2->type)
452 return 0;
453 if (expr1->n_arg != expr2->n_arg)
454 return 0;
455 for (i = 0; i < expr1->n_arg; ++i)
456 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
457 return 0;
458 switch (expr1->type) {
459 case pet_expr_double:
460 if (strcmp(expr1->d.s, expr2->d.s))
461 return 0;
462 if (expr1->d.val != expr2->d.val)
463 return 0;
464 break;
465 case pet_expr_access:
466 if (expr1->acc.read != expr2->acc.read)
467 return 0;
468 if (expr1->acc.write != expr2->acc.write)
469 return 0;
470 if (!expr1->acc.access || !expr2->acc.access)
471 return 0;
472 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
473 return 0;
474 break;
475 case pet_expr_unary:
476 case pet_expr_binary:
477 case pet_expr_ternary:
478 if (expr1->op != expr2->op)
479 return 0;
480 break;
481 case pet_expr_call:
482 if (strcmp(expr1->name, expr2->name))
483 return 0;
484 break;
485 case pet_expr_cast:
486 if (strcmp(expr1->type_name, expr2->type_name))
487 return 0;
488 break;
491 return 1;
494 /* Add extra conditions on the parameters to all access relations in "expr".
496 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
497 __isl_take isl_set *cond)
499 int i;
501 if (!expr)
502 goto error;
504 for (i = 0; i < expr->n_arg; ++i) {
505 expr->args[i] = pet_expr_restrict(expr->args[i],
506 isl_set_copy(cond));
507 if (!expr->args[i])
508 goto error;
511 if (expr->type == pet_expr_access) {
512 expr->acc.access = isl_map_intersect_params(expr->acc.access,
513 isl_set_copy(cond));
514 if (!expr->acc.access)
515 goto error;
518 isl_set_free(cond);
519 return expr;
520 error:
521 isl_set_free(cond);
522 return pet_expr_free(expr);
525 /* Modify all expressions of type pet_expr_access in "expr"
526 * by calling "fn" on them.
528 struct pet_expr *pet_expr_map_access(struct pet_expr *expr,
529 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
530 void *user)
532 int i;
534 if (!expr)
535 return NULL;
537 for (i = 0; i < expr->n_arg; ++i) {
538 expr->args[i] = pet_expr_map_access(expr->args[i], fn, user);
539 if (!expr->args[i])
540 return pet_expr_free(expr);
543 if (expr->type == pet_expr_access)
544 expr = fn(expr, user);
546 return expr;
549 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
551 * Return -1 on error (where fn return a negative value is treated as an error).
552 * Otherwise return 0.
554 int pet_expr_foreach_access_expr(struct pet_expr *expr,
555 int (*fn)(struct pet_expr *expr, void *user), void *user)
557 int i;
559 if (!expr)
560 return -1;
562 for (i = 0; i < expr->n_arg; ++i)
563 if (pet_expr_foreach_access_expr(expr->args[i], fn, user) < 0)
564 return -1;
566 if (expr->type == pet_expr_access)
567 return fn(expr, user);
569 return 0;
572 /* Modify the access relation of the given access expression
573 * based on the given iteration space transformation.
574 * If the access has any arguments then the domain of the access relation
575 * is a wrapped mapping from the iteration space to the space of
576 * argument values. We only need to change the domain of this wrapped
577 * mapping, so we extend the input transformation with an identity mapping
578 * on the space of argument values.
580 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
582 isl_map *update = user;
583 isl_space *dim;
585 update = isl_map_copy(update);
587 dim = isl_map_get_space(expr->acc.access);
588 dim = isl_space_domain(dim);
589 if (!isl_space_is_wrapping(dim))
590 isl_space_free(dim);
591 else {
592 isl_map *id;
593 dim = isl_space_unwrap(dim);
594 dim = isl_space_range(dim);
595 dim = isl_space_map_from_set(dim);
596 id = isl_map_identity(dim);
597 update = isl_map_product(update, id);
600 expr->acc.access = isl_map_apply_domain(expr->acc.access, update);
601 if (!expr->acc.access)
602 return pet_expr_free(expr);
604 return expr;
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_map_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);
866 if (!scop)
867 goto error;
869 scop->context = stmt_extract_context(stmt, scop->context);
870 if (!scop->context)
871 goto error;
873 scop->stmts[0] = stmt;
875 return scop;
876 error:
877 pet_stmt_free(stmt);
878 pet_scop_free(scop);
879 return NULL;
882 /* Does "set" represent an element of an unnamed space, i.e.,
883 * does it represent an affine expression?
885 static int set_is_affine(__isl_keep isl_set *set)
887 int has_id;
889 has_id = isl_set_has_tuple_id(set);
890 if (has_id < 0)
891 return -1;
893 return !has_id;
896 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
897 * ext may be equal to either ext1 or ext2.
899 * The two skips that need to be combined are assumed to be affine expressions.
901 * We need to skip in ext if we need to skip in either ext1 or ext2.
902 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
904 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
905 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
906 enum pet_skip type)
908 isl_set *set, *skip1, *skip2;
910 if (!ext)
911 return NULL;
912 if (!ext1->skip[type] && !ext2->skip[type])
913 return ext;
914 if (!ext1->skip[type]) {
915 if (ext == ext2)
916 return ext;
917 ext->skip[type] = ext2->skip[type];
918 ext2->skip[type] = NULL;
919 return ext;
921 if (!ext2->skip[type]) {
922 if (ext == ext1)
923 return ext;
924 ext->skip[type] = ext1->skip[type];
925 ext1->skip[type] = NULL;
926 return ext;
929 if (!set_is_affine(ext1->skip[type]) ||
930 !set_is_affine(ext2->skip[type]))
931 isl_die(isl_set_get_ctx(ext1->skip[type]), isl_error_internal,
932 "can only combine affine skips",
933 return pet_scop_free(&ext->scop));
935 skip1 = isl_set_copy(ext1->skip[type]);
936 skip2 = isl_set_copy(ext2->skip[type]);
937 set = isl_set_intersect(
938 isl_set_fix_si(isl_set_copy(skip1), isl_dim_set, 0, 0),
939 isl_set_fix_si(isl_set_copy(skip2), isl_dim_set, 0, 0));
940 set = isl_set_union(set, isl_set_fix_si(skip1, isl_dim_set, 0, 1));
941 set = isl_set_union(set, isl_set_fix_si(skip2, isl_dim_set, 0, 1));
942 set = isl_set_coalesce(set);
943 isl_set_free(ext1->skip[type]);
944 ext1->skip[type] = NULL;
945 isl_set_free(ext2->skip[type]);
946 ext2->skip[type] = NULL;
947 ext->skip[type] = set;
948 if (!ext->skip[type])
949 return pet_scop_free(&ext->scop);
951 return ext;
954 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
955 * where type takes on the values pet_skip_now and pet_skip_later.
956 * scop may be equal to either scop1 or scop2.
958 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
959 struct pet_scop *scop1, struct pet_scop *scop2)
961 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
962 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
963 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
965 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
966 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
967 return &ext->scop;
970 /* Update scop->start and scop->end to include the region from "start"
971 * to "end". In particular, if scop->end == 0, then "scop" does not
972 * have any offset information yet and we simply take the information
973 * from "start" and "end". Otherwise, we update the fields if the
974 * region from "start" to "end" is not already included.
976 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
977 unsigned start, unsigned end)
979 if (!scop)
980 return NULL;
981 if (scop->end == 0) {
982 scop->start = start;
983 scop->end = end;
984 } else {
985 if (start < scop->start)
986 scop->start = start;
987 if (end > scop->end)
988 scop->end = end;
991 return scop;
994 /* Combine the offset information of "scop1" and "scop2" into "scop".
996 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
997 struct pet_scop *scop1, struct pet_scop *scop2)
999 if (scop1->end)
1000 scop = pet_scop_update_start_end(scop,
1001 scop1->start, scop1->end);
1002 if (scop2->end)
1003 scop = pet_scop_update_start_end(scop,
1004 scop2->start, scop2->end);
1005 return scop;
1008 /* Construct a pet_scop that contains the offset information,
1009 * arrays, statements and skip information in "scop1" and "scop2".
1011 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1012 struct pet_scop *scop2)
1014 int i;
1015 struct pet_scop *scop = NULL;
1017 if (!scop1 || !scop2)
1018 goto error;
1020 if (scop1->n_stmt == 0) {
1021 scop2 = scop_combine_skips(scop2, scop1, scop2);
1022 pet_scop_free(scop1);
1023 return scop2;
1026 if (scop2->n_stmt == 0) {
1027 scop1 = scop_combine_skips(scop1, scop1, scop2);
1028 pet_scop_free(scop2);
1029 return scop1;
1032 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1033 if (!scop)
1034 goto error;
1036 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1037 scop1->n_array + scop2->n_array);
1038 if (!scop->arrays)
1039 goto error;
1040 scop->n_array = scop1->n_array + scop2->n_array;
1042 for (i = 0; i < scop1->n_stmt; ++i) {
1043 scop->stmts[i] = scop1->stmts[i];
1044 scop1->stmts[i] = NULL;
1047 for (i = 0; i < scop2->n_stmt; ++i) {
1048 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1049 scop2->stmts[i] = NULL;
1052 for (i = 0; i < scop1->n_array; ++i) {
1053 scop->arrays[i] = scop1->arrays[i];
1054 scop1->arrays[i] = NULL;
1057 for (i = 0; i < scop2->n_array; ++i) {
1058 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1059 scop2->arrays[i] = NULL;
1062 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1063 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1064 scop = scop_combine_skips(scop, scop1, scop2);
1065 scop = scop_combine_start_end(scop, scop1, scop2);
1067 pet_scop_free(scop1);
1068 pet_scop_free(scop2);
1069 return scop;
1070 error:
1071 pet_scop_free(scop1);
1072 pet_scop_free(scop2);
1073 pet_scop_free(scop);
1074 return NULL;
1077 /* Apply the skip condition "skip" to "scop".
1078 * That is, make sure "scop" is not executed when the condition holds.
1080 * If "skip" is an affine expression, we add the conditions under
1081 * which the expression is zero to the iteration domains.
1082 * Otherwise, we add a filter on the variable attaining the value zero.
1084 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1085 __isl_take isl_set *skip)
1087 isl_map *skip_map;
1088 int is_aff;
1090 if (!scop || !skip)
1091 goto error;
1093 is_aff = set_is_affine(skip);
1094 if (is_aff < 0)
1095 goto error;
1097 if (!is_aff)
1098 return pet_scop_filter(scop, isl_map_from_range(skip), 0);
1100 skip = isl_set_fix_si(skip, isl_dim_set, 0, 0);
1101 scop = pet_scop_restrict(scop, isl_set_params(skip));
1103 return scop;
1104 error:
1105 isl_set_free(skip);
1106 return pet_scop_free(scop);
1109 /* Construct a pet_scop that contains the arrays, statements and
1110 * skip information in "scop1" and "scop2", where the two scops
1111 * are executed "in sequence". That is, breaks and continues
1112 * in scop1 have an effect on scop2.
1114 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1115 struct pet_scop *scop2)
1117 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1118 scop2 = restrict_skip(scop2,
1119 pet_scop_get_skip(scop1, pet_skip_now));
1120 return pet_scop_add(ctx, scop1, scop2);
1123 /* Construct a pet_scop that contains the arrays, statements and
1124 * skip information in "scop1" and "scop2", where the two scops
1125 * are executed "in parallel". That is, any break or continue
1126 * in scop1 has no effect on scop2.
1128 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1129 struct pet_scop *scop2)
1131 return pet_scop_add(ctx, scop1, scop2);
1134 void *pet_scop_free(struct pet_scop *scop)
1136 int i;
1137 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1139 if (!scop)
1140 return NULL;
1141 isl_set_free(scop->context);
1142 isl_set_free(scop->context_value);
1143 if (scop->arrays)
1144 for (i = 0; i < scop->n_array; ++i)
1145 pet_array_free(scop->arrays[i]);
1146 free(scop->arrays);
1147 if (scop->stmts)
1148 for (i = 0; i < scop->n_stmt; ++i)
1149 pet_stmt_free(scop->stmts[i]);
1150 free(scop->stmts);
1151 isl_set_free(ext->skip[pet_skip_now]);
1152 isl_set_free(ext->skip[pet_skip_later]);
1153 free(scop);
1154 return NULL;
1157 void pet_scop_dump(struct pet_scop *scop)
1159 int i;
1160 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1162 if (!scop)
1163 return;
1165 isl_set_dump(scop->context);
1166 isl_set_dump(scop->context_value);
1167 for (i = 0; i < scop->n_array; ++i)
1168 pet_array_dump(scop->arrays[i]);
1169 for (i = 0; i < scop->n_stmt; ++i)
1170 pet_stmt_dump(scop->stmts[i]);
1172 if (ext->skip[0]) {
1173 fprintf(stderr, "skip\n");
1174 isl_set_dump(ext->skip[0]);
1175 isl_set_dump(ext->skip[1]);
1179 /* Return 1 if the two pet_arrays are equivalent.
1181 * We don't compare element_size as this may be target dependent.
1183 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1185 if (!array1 || !array2)
1186 return 0;
1188 if (!isl_set_is_equal(array1->context, array2->context))
1189 return 0;
1190 if (!isl_set_is_equal(array1->extent, array2->extent))
1191 return 0;
1192 if (!!array1->value_bounds != !!array2->value_bounds)
1193 return 0;
1194 if (array1->value_bounds &&
1195 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1196 return 0;
1197 if (strcmp(array1->element_type, array2->element_type))
1198 return 0;
1199 if (array1->live_out != array2->live_out)
1200 return 0;
1201 if (array1->uniquely_defined != array2->uniquely_defined)
1202 return 0;
1203 if (array1->declared != array2->declared)
1204 return 0;
1205 if (array1->exposed != array2->exposed)
1206 return 0;
1208 return 1;
1211 /* Return 1 if the two pet_stmts are equivalent.
1213 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1215 int i;
1217 if (!stmt1 || !stmt2)
1218 return 0;
1220 if (stmt1->line != stmt2->line)
1221 return 0;
1222 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1223 return 0;
1224 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1225 return 0;
1226 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1227 return 0;
1228 if (stmt1->n_arg != stmt2->n_arg)
1229 return 0;
1230 for (i = 0; i < stmt1->n_arg; ++i) {
1231 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1232 return 0;
1235 return 1;
1238 /* Return 1 if the two pet_scops are equivalent.
1240 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1242 int i;
1244 if (!scop1 || !scop2)
1245 return 0;
1247 if (!isl_set_is_equal(scop1->context, scop2->context))
1248 return 0;
1249 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1250 return 0;
1252 if (scop1->n_array != scop2->n_array)
1253 return 0;
1254 for (i = 0; i < scop1->n_array; ++i)
1255 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1256 return 0;
1258 if (scop1->n_stmt != scop2->n_stmt)
1259 return 0;
1260 for (i = 0; i < scop1->n_stmt; ++i)
1261 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1262 return 0;
1264 return 1;
1267 /* Prefix the schedule of "stmt" with an extra dimension with constant
1268 * value "pos".
1270 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1272 if (!stmt)
1273 return NULL;
1275 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1276 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1277 if (!stmt->schedule)
1278 return pet_stmt_free(stmt);
1280 return stmt;
1283 /* Prefix the schedules of all statements in "scop" with an extra
1284 * dimension with constant value "pos".
1286 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1288 int i;
1290 if (!scop)
1291 return NULL;
1293 for (i = 0; i < scop->n_stmt; ++i) {
1294 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1295 if (!scop->stmts[i])
1296 return pet_scop_free(scop);
1299 return scop;
1302 /* Given a set with a parameter at "param_pos" that refers to the
1303 * iterator, "move" the iterator to the first set dimension.
1304 * That is, essentially equate the parameter to the first set dimension
1305 * and then project it out.
1307 * The first set dimension may however refer to a virtual iterator,
1308 * while the parameter refers to the "real" iterator.
1309 * We therefore need to take into account the mapping "iv_map", which
1310 * maps the virtual iterator to the real iterator.
1311 * In particular, we equate the set dimension to the input of the map
1312 * and the parameter to the output of the map and then project out
1313 * everything we don't need anymore.
1315 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1316 int param_pos, __isl_take isl_map *iv_map)
1318 isl_map *map;
1319 map = isl_map_from_domain(set);
1320 map = isl_map_add_dims(map, isl_dim_out, 1);
1321 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1322 iv_map = isl_map_align_params(iv_map, isl_map_get_space(map));
1323 map = isl_map_apply_range(map, iv_map);
1324 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1325 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1326 return isl_map_domain(map);
1329 /* Data used in embed_access.
1330 * extend adds an iterator to the iteration domain
1331 * iv_map maps the virtual iterator to the real iterator
1332 * var_id represents the induction variable of the corresponding loop
1334 struct pet_embed_access {
1335 isl_map *extend;
1336 isl_map *iv_map;
1337 isl_id *var_id;
1340 /* Given an access expression, embed the associated access relation
1341 * in an extra outer loop.
1343 * We first update the iteration domain to insert the extra dimension.
1345 * If the access refers to the induction variable, then it is
1346 * turned into an access to the set of integers with index (and value)
1347 * equal to the induction variable.
1349 * If the induction variable appears in the constraints (as a parameter),
1350 * then the parameter is equated to the newly introduced iteration
1351 * domain dimension and subsequently projected out.
1353 * Similarly, if the accessed array is a virtual array (with user
1354 * pointer equal to NULL), as created by create_test_access,
1355 * then it is extended along with the domain of the access.
1357 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
1359 struct pet_embed_access *data = user;
1360 isl_map *access;
1361 isl_id *array_id = NULL;
1362 int pos;
1364 expr = update_domain(expr, data->extend);
1365 if (!expr)
1366 return NULL;
1368 access = expr->acc.access;
1370 if (isl_map_has_tuple_id(access, isl_dim_out))
1371 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1372 if (array_id == data->var_id ||
1373 (array_id && !isl_id_get_user(array_id))) {
1374 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1375 access = isl_map_equate(access,
1376 isl_dim_in, 0, isl_dim_out, 0);
1377 if (array_id == data->var_id)
1378 access = isl_map_apply_range(access,
1379 isl_map_copy(data->iv_map));
1380 else
1381 access = isl_map_set_tuple_id(access, isl_dim_out,
1382 isl_id_copy(array_id));
1384 isl_id_free(array_id);
1386 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1387 if (pos >= 0) {
1388 isl_set *set = isl_map_wrap(access);
1389 set = internalize_iv(set, pos, isl_map_copy(data->iv_map));
1390 access = isl_set_unwrap(set);
1392 expr->acc.access = isl_map_set_dim_id(access, isl_dim_in, 0,
1393 isl_id_copy(data->var_id));
1394 if (!expr->acc.access)
1395 return pet_expr_free(expr);
1397 return expr;
1400 /* Embed all access subexpressions of "expr" in an extra loop.
1401 * "extend" inserts an outer loop iterator in the iteration domains.
1402 * "iv_map" maps the virtual iterator to the real iterator
1403 * "var_id" represents the induction variable.
1405 static struct pet_expr *expr_embed(struct pet_expr *expr,
1406 __isl_take isl_map *extend, __isl_take isl_map *iv_map,
1407 __isl_keep isl_id *var_id)
1409 struct pet_embed_access data =
1410 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1412 expr = pet_expr_map_access(expr, &embed_access, &data);
1413 isl_map_free(iv_map);
1414 isl_map_free(extend);
1415 return expr;
1418 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1419 * "dom" and schedule "sched". "var_id" represents the induction variable
1420 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1421 * That is, it maps the iterator used in "dom" and the domain of "sched"
1422 * to the iterator that some of the parameters in "stmt" may refer to.
1424 * The iteration domain and schedule of the statement are updated
1425 * according to the iteration domain and schedule of the new loop.
1426 * If stmt->domain is a wrapped map, then the iteration domain
1427 * is the domain of this map, so we need to be careful to adjust
1428 * this domain.
1430 * If the induction variable appears in the constraints (as a parameter)
1431 * of the current iteration domain or the schedule of the statement,
1432 * then the parameter is equated to the newly introduced iteration
1433 * domain dimension and subsequently projected out.
1435 * Finally, all access relations are updated based on the extra loop.
1437 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1438 __isl_take isl_set *dom, __isl_take isl_map *sched,
1439 __isl_take isl_map *iv_map, __isl_take isl_id *var_id)
1441 int i;
1442 int pos;
1443 isl_id *stmt_id;
1444 isl_space *dim;
1445 isl_map *extend;
1447 if (!stmt)
1448 goto error;
1450 if (isl_set_is_wrapping(stmt->domain)) {
1451 isl_map *map;
1452 isl_map *ext;
1453 isl_space *ran_dim;
1455 map = isl_set_unwrap(stmt->domain);
1456 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1457 ran_dim = isl_space_range(isl_map_get_space(map));
1458 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1459 isl_set_universe(ran_dim));
1460 map = isl_map_flat_domain_product(ext, map);
1461 map = isl_map_set_tuple_id(map, isl_dim_in,
1462 isl_id_copy(stmt_id));
1463 dim = isl_space_domain(isl_map_get_space(map));
1464 stmt->domain = isl_map_wrap(map);
1465 } else {
1466 stmt_id = isl_set_get_tuple_id(stmt->domain);
1467 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1468 stmt->domain);
1469 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1470 isl_id_copy(stmt_id));
1471 dim = isl_set_get_space(stmt->domain);
1474 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1475 if (pos >= 0)
1476 stmt->domain = internalize_iv(stmt->domain, pos,
1477 isl_map_copy(iv_map));
1479 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1480 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1481 isl_dim_in, stmt_id);
1483 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1484 if (pos >= 0) {
1485 isl_set *set = isl_map_wrap(stmt->schedule);
1486 set = internalize_iv(set, pos, isl_map_copy(iv_map));
1487 stmt->schedule = isl_set_unwrap(set);
1490 dim = isl_space_map_from_set(dim);
1491 extend = isl_map_identity(dim);
1492 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1493 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1494 isl_map_get_tuple_id(extend, isl_dim_out));
1495 for (i = 0; i < stmt->n_arg; ++i)
1496 stmt->args[i] = expr_embed(stmt->args[i], isl_map_copy(extend),
1497 isl_map_copy(iv_map), var_id);
1498 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1500 isl_set_free(dom);
1501 isl_id_free(var_id);
1503 for (i = 0; i < stmt->n_arg; ++i)
1504 if (!stmt->args[i])
1505 return pet_stmt_free(stmt);
1506 if (!stmt->domain || !stmt->schedule || !stmt->body)
1507 return pet_stmt_free(stmt);
1508 return stmt;
1509 error:
1510 isl_set_free(dom);
1511 isl_map_free(sched);
1512 isl_map_free(iv_map);
1513 isl_id_free(var_id);
1514 return NULL;
1517 /* Embed the given pet_array in an extra outer loop with iteration domain
1518 * "dom".
1519 * This embedding only has an effect on virtual arrays (those with
1520 * user pointer equal to NULL), which need to be extended along with
1521 * the iteration domain.
1523 static struct pet_array *pet_array_embed(struct pet_array *array,
1524 __isl_take isl_set *dom)
1526 isl_id *array_id = NULL;
1528 if (!array)
1529 goto error;
1531 if (isl_set_has_tuple_id(array->extent))
1532 array_id = isl_set_get_tuple_id(array->extent);
1534 if (array_id && !isl_id_get_user(array_id)) {
1535 array->extent = isl_set_flat_product(dom, array->extent);
1536 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1537 if (!array->extent)
1538 return pet_array_free(array);
1539 } else {
1540 isl_set_free(dom);
1541 isl_id_free(array_id);
1544 return array;
1545 error:
1546 isl_set_free(dom);
1547 return NULL;
1550 /* Project out all unnamed parameters from "set" and return the result.
1552 static __isl_give isl_set *set_project_out_unnamed_params(
1553 __isl_take isl_set *set)
1555 int i, n;
1557 n = isl_set_dim(set, isl_dim_param);
1558 for (i = n - 1; i >= 0; --i) {
1559 if (isl_set_has_dim_name(set, isl_dim_param, i))
1560 continue;
1561 set = isl_set_project_out(set, isl_dim_param, i, 1);
1564 return set;
1567 /* Update the context with respect to an embedding into a loop
1568 * with iteration domain "dom" and induction variable "id".
1569 * "iv_map" maps a possibly virtual iterator (used in "dom")
1570 * to the real iterator (parameter "id").
1572 * If the current context is independent of "id", we don't need
1573 * to do anything.
1574 * Otherwise, a parameter value is invalid for the embedding if
1575 * any of the corresponding iterator values is invalid.
1576 * That is, a parameter value is valid only if all the corresponding
1577 * iterator values are valid.
1578 * We therefore compute the set of parameters
1580 * forall i in dom : valid (i)
1582 * or
1584 * not exists i in dom : not valid(i)
1586 * i.e.,
1588 * not exists i in dom \ valid(i)
1590 * Before we subtract valid(i) from dom, we first need to map
1591 * the real iterator to the virtual iterator.
1593 * If there are any unnamed parameters in "dom", then we consider
1594 * a parameter value to be valid if it is valid for any value of those
1595 * unnamed parameters. They are therefore projected out at the end.
1597 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1598 __isl_keep isl_set *dom, __isl_keep isl_map *iv_map,
1599 __isl_keep isl_id *id)
1601 int pos;
1603 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1604 if (pos < 0)
1605 return context;
1607 context = isl_set_from_params(context);
1608 context = isl_set_add_dims(context, isl_dim_set, 1);
1609 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1610 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1611 context = isl_set_apply(context, isl_map_reverse(isl_map_copy(iv_map)));
1612 context = isl_set_subtract(isl_set_copy(dom), context);
1613 context = isl_set_params(context);
1614 context = isl_set_complement(context);
1615 context = set_project_out_unnamed_params(context);
1616 return context;
1619 /* Embed all statements and arrays in "scop" in an extra outer loop
1620 * with iteration domain "dom" and schedule "sched".
1621 * "id" represents the induction variable of the loop.
1622 * "iv_map" maps a possibly virtual iterator to the real iterator.
1623 * That is, it maps the iterator used in "dom" and the domain of "sched"
1624 * to the iterator that some of the parameters in "scop" may refer to.
1626 * Any skip conditions within the loop have no effect outside of the loop.
1627 * The caller is responsible for making sure skip[pet_skip_later] has been
1628 * taken into account.
1630 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1631 __isl_take isl_map *sched, __isl_take isl_map *iv_map,
1632 __isl_take isl_id *id)
1634 int i;
1636 if (!scop)
1637 goto error;
1639 pet_scop_reset_skip(scop, pet_skip_now);
1640 pet_scop_reset_skip(scop, pet_skip_later);
1642 scop->context = context_embed(scop->context, dom, iv_map, id);
1643 if (!scop->context)
1644 goto error;
1646 for (i = 0; i < scop->n_stmt; ++i) {
1647 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1648 isl_set_copy(dom), isl_map_copy(sched),
1649 isl_map_copy(iv_map), isl_id_copy(id));
1650 if (!scop->stmts[i])
1651 goto error;
1654 for (i = 0; i < scop->n_array; ++i) {
1655 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1656 isl_set_copy(dom));
1657 if (!scop->arrays[i])
1658 goto error;
1661 isl_set_free(dom);
1662 isl_map_free(sched);
1663 isl_map_free(iv_map);
1664 isl_id_free(id);
1665 return scop;
1666 error:
1667 isl_set_free(dom);
1668 isl_map_free(sched);
1669 isl_map_free(iv_map);
1670 isl_id_free(id);
1671 return pet_scop_free(scop);
1674 /* Add extra conditions on the parameters to iteration domain of "stmt".
1676 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1677 __isl_take isl_set *cond)
1679 if (!stmt)
1680 goto error;
1682 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1684 return stmt;
1685 error:
1686 isl_set_free(cond);
1687 return pet_stmt_free(stmt);
1690 /* Add extra conditions to scop->skip[type].
1692 * The new skip condition only holds if it held before
1693 * and the condition is true. It does not hold if it did not hold
1694 * before or the condition is false.
1696 * The skip condition is assumed to be an affine expression.
1698 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1699 enum pet_skip type, __isl_keep isl_set *cond)
1701 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1702 isl_set *skip;
1703 isl_set *set;
1705 if (!scop)
1706 return NULL;
1707 if (!ext->skip[type])
1708 return scop;
1710 if (!set_is_affine(ext->skip[type]))
1711 isl_die(isl_set_get_ctx(ext->skip[type]), isl_error_internal,
1712 "can only resrict affine skips",
1713 return pet_scop_free(scop));
1715 skip = ext->skip[type];
1716 skip = isl_set_intersect_params(skip, isl_set_copy(cond));
1717 set = isl_set_from_params(isl_set_copy(cond));
1718 set = isl_set_complement(set);
1719 set = isl_set_add_dims(set, isl_dim_set, 1);
1720 set = isl_set_fix_si(set, isl_dim_set, 0, 0);
1721 skip = isl_set_union(skip, set);
1722 ext->skip[type] = skip;
1723 if (!ext->skip[type])
1724 return pet_scop_free(scop);
1726 return scop;
1729 /* Add extra conditions on the parameters to all iteration domains
1730 * and skip conditions.
1732 * A parameter value is valid for the result if it was valid
1733 * for the original scop and satisfies "cond" or if it does
1734 * not satisfy "cond" as in this case the scop is not executed
1735 * and the original constraints on the parameters are irrelevant.
1737 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1738 __isl_take isl_set *cond)
1740 int i;
1742 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1743 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1745 if (!scop)
1746 goto error;
1748 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1749 scop->context = isl_set_union(scop->context,
1750 isl_set_complement(isl_set_copy(cond)));
1751 scop->context = isl_set_coalesce(scop->context);
1752 scop->context = set_project_out_unnamed_params(scop->context);
1753 if (!scop->context)
1754 goto error;
1756 for (i = 0; i < scop->n_stmt; ++i) {
1757 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1758 isl_set_copy(cond));
1759 if (!scop->stmts[i])
1760 goto error;
1763 isl_set_free(cond);
1764 return scop;
1765 error:
1766 isl_set_free(cond);
1767 return pet_scop_free(scop);
1770 /* Construct a map that inserts a filter value with name "id" and value
1771 * "satisfied" in the list of filter values embedded in the set space "space".
1773 * If "space" does not contain any filter values yet, we first create
1774 * a map that inserts 0 filter values, i.e.,
1776 * space -> [space -> []]
1778 * We can now assume that space is of the form [dom -> [filters]]
1779 * We construct an identity mapping on dom and a mapping on filters
1780 * that inserts the new filter
1782 * dom -> dom
1783 * [filters] -> [satisfied, filters]
1785 * and then compute the cross product
1787 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1789 static __isl_give isl_map *insert_filter_map(__isl_take isl_space *space,
1790 __isl_take isl_id *id, int satisfied)
1792 isl_space *space2;
1793 isl_map *map, *map_dom, *map_ran;
1794 isl_set *dom;
1796 if (isl_space_is_wrapping(space)) {
1797 space2 = isl_space_map_from_set(isl_space_copy(space));
1798 map = isl_map_identity(space2);
1799 space = isl_space_unwrap(space);
1800 } else {
1801 space = isl_space_from_domain(space);
1802 map = isl_map_universe(isl_space_copy(space));
1803 map = isl_map_reverse(isl_map_domain_map(map));
1806 space2 = isl_space_domain(isl_space_copy(space));
1807 map_dom = isl_map_identity(isl_space_map_from_set(space2));
1808 space = isl_space_range(space);
1809 map_ran = isl_map_identity(isl_space_map_from_set(space));
1810 map_ran = isl_map_insert_dims(map_ran, isl_dim_out, 0, 1);
1811 map_ran = isl_map_set_dim_id(map_ran, isl_dim_out, 0, id);
1812 map_ran = isl_map_fix_si(map_ran, isl_dim_out, 0, satisfied);
1814 map = isl_map_apply_range(map, isl_map_product(map_dom, map_ran));
1816 return map;
1819 /* Insert an argument expression corresponding to "test" in front
1820 * of the list of arguments described by *n_arg and *args.
1822 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
1823 __isl_keep isl_map *test)
1825 int i;
1826 isl_ctx *ctx = isl_map_get_ctx(test);
1828 if (!test)
1829 return -1;
1831 if (!*args) {
1832 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
1833 if (!*args)
1834 return -1;
1835 } else {
1836 struct pet_expr **ext;
1837 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
1838 if (!ext)
1839 return -1;
1840 for (i = 0; i < *n_arg; ++i)
1841 ext[1 + i] = (*args)[i];
1842 free(*args);
1843 *args = ext;
1845 (*n_arg)++;
1846 (*args)[0] = pet_expr_from_access(isl_map_copy(test));
1847 if (!(*args)[0])
1848 return -1;
1850 return 0;
1853 /* Make the expression "expr" depend on the value of "test"
1854 * being equal to "satisfied".
1856 * If "test" is an affine expression, we simply add the conditions
1857 * on the expression have the value "satisfied" to all access relations.
1859 * Otherwise, we add a filter to "expr" (which is then assumed to be
1860 * an access expression) corresponding to "test" being equal to "satisfied".
1862 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
1863 __isl_take isl_map *test, int satisfied)
1865 isl_id *id;
1866 isl_ctx *ctx;
1867 isl_space *space;
1868 isl_map *map;
1870 if (!expr || !test)
1871 goto error;
1873 if (!isl_map_has_tuple_id(test, isl_dim_out)) {
1874 test = isl_map_fix_si(test, isl_dim_out, 0, satisfied);
1875 return pet_expr_restrict(expr, isl_map_params(test));
1878 ctx = isl_map_get_ctx(test);
1879 if (expr->type != pet_expr_access)
1880 isl_die(ctx, isl_error_invalid,
1881 "can only filter access expressions", goto error);
1883 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1884 id = isl_map_get_tuple_id(test, isl_dim_out);
1885 map = insert_filter_map(space, id, satisfied);
1887 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
1888 if (!expr->acc.access)
1889 goto error;
1891 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
1892 goto error;
1894 isl_map_free(test);
1895 return expr;
1896 error:
1897 isl_map_free(test);
1898 return pet_expr_free(expr);
1901 /* Make the statement "stmt" depend on the value of "test"
1902 * being equal to "satisfied" by adjusting stmt->domain.
1904 * The domain of "test" corresponds to the (zero or more) outer dimensions
1905 * of the iteration domain.
1907 * We insert an argument corresponding to a read to "test"
1908 * from the iteration domain of "stmt" in front of the list of arguments.
1909 * We also insert a corresponding output dimension in the wrapped
1910 * map contained in stmt->domain, with value set to "satisfied".
1912 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1913 __isl_take isl_map *test, int satisfied)
1915 int i;
1916 isl_id *id;
1917 isl_ctx *ctx;
1918 isl_map *map, *add_dom;
1919 isl_space *space;
1920 isl_set *dom;
1921 int n_test_dom;
1923 if (!stmt || !test)
1924 goto error;
1926 id = isl_map_get_tuple_id(test, isl_dim_out);
1927 map = insert_filter_map(isl_set_get_space(stmt->domain), id, satisfied);
1928 stmt->domain = isl_set_apply(stmt->domain, map);
1930 space = isl_space_unwrap(isl_set_get_space(stmt->domain));
1931 dom = isl_set_universe(isl_space_domain(space));
1932 n_test_dom = isl_map_dim(test, isl_dim_in);
1933 add_dom = isl_map_from_range(dom);
1934 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
1935 for (i = 0; i < n_test_dom; ++i)
1936 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
1937 isl_dim_out, i);
1938 test = isl_map_apply_domain(test, add_dom);
1940 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1941 goto error;
1943 isl_map_free(test);
1944 return stmt;
1945 error:
1946 isl_map_free(test);
1947 return pet_stmt_free(stmt);
1950 /* Does "scop" have a skip condition of the given "type"?
1952 int pet_scop_has_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 return ext->skip[type] != NULL;
1961 /* Does "scop" have a skip condition of the given "type" that
1962 * is an affine expression?
1964 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1966 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1968 if (!scop)
1969 return -1;
1970 if (!ext->skip[type])
1971 return 0;
1972 return set_is_affine(ext->skip[type]);
1975 /* Does "scop" have a skip condition of the given "type" that
1976 * is not an affine expression?
1978 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1980 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1981 int aff;
1983 if (!scop)
1984 return -1;
1985 if (!ext->skip[type])
1986 return 0;
1987 aff = set_is_affine(ext->skip[type]);
1988 if (aff < 0)
1989 return -1;
1990 return !aff;
1993 /* Does "scop" have a skip condition of the given "type" that
1994 * is affine and holds on the entire domain?
1996 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1998 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1999 isl_set *set;
2000 int is_aff;
2001 int is_univ;
2003 is_aff = pet_scop_has_affine_skip(scop, type);
2004 if (is_aff < 0 || !is_aff)
2005 return is_aff;
2007 set = isl_set_copy(ext->skip[type]);
2008 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
2009 set = isl_set_params(set);
2010 is_univ = isl_set_plain_is_universe(set);
2011 isl_set_free(set);
2013 return is_univ;
2016 /* Replace scop->skip[type] by "skip".
2018 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2019 enum pet_skip type, __isl_take isl_set *skip)
2021 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2023 if (!scop || !skip)
2024 goto error;
2026 isl_set_free(ext->skip[type]);
2027 ext->skip[type] = skip;
2029 return scop;
2030 error:
2031 isl_set_free(skip);
2032 return pet_scop_free(scop);
2035 /* Return a copy of scop->skip[type].
2037 __isl_give isl_set *pet_scop_get_skip(struct pet_scop *scop,
2038 enum pet_skip type)
2040 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2042 if (!scop)
2043 return NULL;
2045 return isl_set_copy(ext->skip[type]);
2048 /* Return a map to the skip condition of the given type.
2050 __isl_give isl_map *pet_scop_get_skip_map(struct pet_scop *scop,
2051 enum pet_skip type)
2053 return isl_map_from_range(pet_scop_get_skip(scop, type));
2056 /* Return an access pet_expr corresponding to the skip condition
2057 * of the given type.
2059 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2060 enum pet_skip type)
2062 return pet_expr_from_access(pet_scop_get_skip_map(scop, type));
2065 /* Drop the the skip condition scop->skip[type].
2067 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2069 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2071 if (!scop)
2072 return;
2074 isl_set_free(ext->skip[type]);
2075 ext->skip[type] = NULL;
2078 /* Make the skip condition (if any) depend on the value of "test" being
2079 * equal to "satisfied".
2081 * We only support the case where the original skip condition is universal,
2082 * i.e., where skipping is unconditional, and where satisfied == 1.
2083 * In this case, the skip condition is changed to skip only when
2084 * "test" is equal to one.
2086 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2087 enum pet_skip type, __isl_keep isl_map *test, int satisfied)
2089 int is_univ = 0;
2091 if (!scop)
2092 return NULL;
2093 if (!pet_scop_has_skip(scop, type))
2094 return scop;
2096 if (satisfied)
2097 is_univ = pet_scop_has_universal_skip(scop, type);
2098 if (is_univ < 0)
2099 return pet_scop_free(scop);
2100 if (satisfied && is_univ) {
2101 scop = pet_scop_set_skip(scop, type,
2102 isl_map_range(isl_map_copy(test)));
2103 if (!scop)
2104 return NULL;
2105 } else {
2106 isl_die(isl_map_get_ctx(test), isl_error_internal,
2107 "skip expression cannot be filtered",
2108 return pet_scop_free(scop));
2111 return scop;
2114 /* Make all statements in "scop" depend on the value of "test"
2115 * being equal to "satisfied" by adjusting their domains.
2117 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2118 __isl_take isl_map *test, int satisfied)
2120 int i;
2122 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2123 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2125 if (!scop || !test)
2126 goto error;
2128 for (i = 0; i < scop->n_stmt; ++i) {
2129 scop->stmts[i] = stmt_filter(scop->stmts[i],
2130 isl_map_copy(test), satisfied);
2131 if (!scop->stmts[i])
2132 goto error;
2135 isl_map_free(test);
2136 return scop;
2137 error:
2138 isl_map_free(test);
2139 return pet_scop_free(scop);
2142 /* Do the filters "i" and "j" always have the same value?
2144 static int equal_filter_values(__isl_keep isl_set *domain, int i, int j)
2146 isl_map *map, *test;
2147 int equal;
2149 map = isl_set_unwrap(isl_set_copy(domain));
2150 test = isl_map_universe(isl_map_get_space(map));
2151 test = isl_map_equate(test, isl_dim_out, i, isl_dim_out, j);
2152 equal = isl_map_is_subset(map, test);
2153 isl_map_free(map);
2154 isl_map_free(test);
2156 return equal;
2159 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2160 * access relation, the union of the two access relations.
2162 static struct pet_stmt *merge_filter_pair(struct pet_stmt *stmt, int i, int j)
2164 int k;
2165 isl_map *map;
2167 if (!stmt)
2168 return NULL;
2170 stmt->args[i]->acc.access = isl_map_union(stmt->args[i]->acc.access,
2171 isl_map_copy(stmt->args[j]->acc.access));
2172 stmt->args[i]->acc.access = isl_map_coalesce(stmt->args[i]->acc.access);
2174 pet_expr_free(stmt->args[j]);
2175 for (k = j; k < stmt->n_arg - 1; ++k)
2176 stmt->args[k] = stmt->args[k + 1];
2177 stmt->n_arg--;
2179 map = isl_set_unwrap(stmt->domain);
2180 map = isl_map_project_out(map, isl_dim_out, j, 1);
2181 stmt->domain = isl_map_wrap(map);
2183 if (!stmt->domain || !stmt->args[i]->acc.access)
2184 return pet_stmt_free(stmt);
2186 return stmt;
2189 /* Look for any pair of filters that access the same filter variable
2190 * and that have the same filter value and merge them into a single
2191 * filter with as filter access relation the union of the filter access
2192 * relations.
2194 static struct pet_stmt *stmt_merge_filters(struct pet_stmt *stmt)
2196 int i, j;
2197 isl_space *space_i, *space_j;
2199 if (!stmt)
2200 return NULL;
2201 if (stmt->n_arg <= 1)
2202 return stmt;
2204 for (i = 0; i < stmt->n_arg - 1; ++i) {
2205 if (stmt->args[i]->type != pet_expr_access)
2206 continue;
2207 if (pet_expr_is_affine(stmt->args[i]))
2208 continue;
2210 space_i = isl_map_get_space(stmt->args[i]->acc.access);
2212 for (j = stmt->n_arg - 1; j > i; --j) {
2213 int eq;
2215 if (stmt->args[j]->type != pet_expr_access)
2216 continue;
2217 if (pet_expr_is_affine(stmt->args[j]))
2218 continue;
2220 space_j = isl_map_get_space(stmt->args[j]->acc.access);
2222 eq = isl_space_is_equal(space_i, space_j);
2223 if (eq >= 0 && eq)
2224 eq = equal_filter_values(stmt->domain, i, j);
2225 if (eq >= 0 && eq)
2226 stmt = merge_filter_pair(stmt, i, j);
2228 isl_space_free(space_j);
2230 if (eq < 0 || !stmt)
2231 break;
2234 isl_space_free(space_i);
2236 if (j > i || !stmt)
2237 return pet_stmt_free(stmt);
2240 return stmt;
2243 /* Look for any pair of filters that access the same filter variable
2244 * and that have the same filter value and merge them into a single
2245 * filter with as filter access relation the union of the filter access
2246 * relations.
2248 struct pet_scop *pet_scop_merge_filters(struct pet_scop *scop)
2250 int i;
2252 if (!scop)
2253 return NULL;
2255 for (i = 0; i < scop->n_stmt; ++i) {
2256 scop->stmts[i] = stmt_merge_filters(scop->stmts[i]);
2257 if (!scop->stmts[i])
2258 return pet_scop_free(scop);
2261 return scop;
2264 /* Add all parameters in "expr" to "dim" and return the result.
2266 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2267 __isl_take isl_space *dim)
2269 int i;
2271 if (!expr)
2272 goto error;
2273 for (i = 0; i < expr->n_arg; ++i)
2275 dim = expr_collect_params(expr->args[i], dim);
2277 if (expr->type == pet_expr_access)
2278 dim = isl_space_align_params(dim,
2279 isl_map_get_space(expr->acc.access));
2281 return dim;
2282 error:
2283 isl_space_free(dim);
2284 return pet_expr_free(expr);
2287 /* Add all parameters in "stmt" to "dim" and return the result.
2289 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2290 __isl_take isl_space *dim)
2292 if (!stmt)
2293 goto error;
2295 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2296 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2297 dim = expr_collect_params(stmt->body, dim);
2299 return dim;
2300 error:
2301 isl_space_free(dim);
2302 return pet_stmt_free(stmt);
2305 /* Add all parameters in "array" to "dim" and return the result.
2307 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2308 __isl_take isl_space *dim)
2310 if (!array)
2311 goto error;
2313 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2314 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2316 return dim;
2317 error:
2318 pet_array_free(array);
2319 return isl_space_free(dim);
2322 /* Add all parameters in "scop" to "dim" and return the result.
2324 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2325 __isl_take isl_space *dim)
2327 int i;
2329 if (!scop)
2330 goto error;
2332 for (i = 0; i < scop->n_array; ++i)
2333 dim = array_collect_params(scop->arrays[i], dim);
2335 for (i = 0; i < scop->n_stmt; ++i)
2336 dim = stmt_collect_params(scop->stmts[i], dim);
2338 return dim;
2339 error:
2340 isl_space_free(dim);
2341 return pet_scop_free(scop);
2344 /* Add all parameters in "dim" to all access relations in "expr".
2346 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2347 __isl_take isl_space *dim)
2349 int i;
2351 if (!expr)
2352 goto error;
2354 for (i = 0; i < expr->n_arg; ++i) {
2355 expr->args[i] =
2356 expr_propagate_params(expr->args[i],
2357 isl_space_copy(dim));
2358 if (!expr->args[i])
2359 goto error;
2362 if (expr->type == pet_expr_access) {
2363 expr->acc.access = isl_map_align_params(expr->acc.access,
2364 isl_space_copy(dim));
2365 if (!expr->acc.access)
2366 goto error;
2369 isl_space_free(dim);
2370 return expr;
2371 error:
2372 isl_space_free(dim);
2373 return pet_expr_free(expr);
2376 /* Add all parameters in "dim" to the domain, schedule and
2377 * all access relations in "stmt".
2379 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2380 __isl_take isl_space *dim)
2382 if (!stmt)
2383 goto error;
2385 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2386 stmt->schedule = isl_map_align_params(stmt->schedule,
2387 isl_space_copy(dim));
2388 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2390 if (!stmt->domain || !stmt->schedule || !stmt->body)
2391 goto error;
2393 isl_space_free(dim);
2394 return stmt;
2395 error:
2396 isl_space_free(dim);
2397 return pet_stmt_free(stmt);
2400 /* Add all parameters in "dim" to "array".
2402 static struct pet_array *array_propagate_params(struct pet_array *array,
2403 __isl_take isl_space *dim)
2405 if (!array)
2406 goto error;
2408 array->context = isl_set_align_params(array->context,
2409 isl_space_copy(dim));
2410 array->extent = isl_set_align_params(array->extent,
2411 isl_space_copy(dim));
2412 if (array->value_bounds) {
2413 array->value_bounds = isl_set_align_params(array->value_bounds,
2414 isl_space_copy(dim));
2415 if (!array->value_bounds)
2416 goto error;
2419 if (!array->context || !array->extent)
2420 goto error;
2422 isl_space_free(dim);
2423 return array;
2424 error:
2425 isl_space_free(dim);
2426 return pet_array_free(array);
2429 /* Add all parameters in "dim" to "scop".
2431 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2432 __isl_take isl_space *dim)
2434 int i;
2436 if (!scop)
2437 goto error;
2439 for (i = 0; i < scop->n_array; ++i) {
2440 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2441 isl_space_copy(dim));
2442 if (!scop->arrays[i])
2443 goto error;
2446 for (i = 0; i < scop->n_stmt; ++i) {
2447 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2448 isl_space_copy(dim));
2449 if (!scop->stmts[i])
2450 goto error;
2453 isl_space_free(dim);
2454 return scop;
2455 error:
2456 isl_space_free(dim);
2457 return pet_scop_free(scop);
2460 /* Update all isl_sets and isl_maps in "scop" such that they all
2461 * have the same parameters.
2463 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2465 isl_space *dim;
2467 if (!scop)
2468 return NULL;
2470 dim = isl_set_get_space(scop->context);
2471 dim = scop_collect_params(scop, dim);
2473 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
2474 scop = scop_propagate_params(scop, dim);
2476 return scop;
2479 /* Check if the given access relation accesses a (0D) array that corresponds
2480 * to one of the parameters in "dim". If so, replace the array access
2481 * by an access to the set of integers with as index (and value)
2482 * that parameter.
2484 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
2485 __isl_take isl_space *dim)
2487 isl_id *array_id = NULL;
2488 int pos = -1;
2490 if (isl_map_has_tuple_id(access, isl_dim_out)) {
2491 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2492 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
2494 isl_space_free(dim);
2496 if (pos < 0) {
2497 isl_id_free(array_id);
2498 return access;
2501 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
2502 if (pos < 0) {
2503 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
2504 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
2505 pos = 0;
2506 } else
2507 isl_id_free(array_id);
2509 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2510 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
2512 return access;
2515 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2516 * in "dim" by a value equal to the corresponding parameter.
2518 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
2519 __isl_take isl_space *dim)
2521 int i;
2523 if (!expr)
2524 goto error;
2526 for (i = 0; i < expr->n_arg; ++i) {
2527 expr->args[i] =
2528 expr_detect_parameter_accesses(expr->args[i],
2529 isl_space_copy(dim));
2530 if (!expr->args[i])
2531 goto error;
2534 if (expr->type == pet_expr_access) {
2535 expr->acc.access = access_detect_parameter(expr->acc.access,
2536 isl_space_copy(dim));
2537 if (!expr->acc.access)
2538 goto error;
2541 isl_space_free(dim);
2542 return expr;
2543 error:
2544 isl_space_free(dim);
2545 return pet_expr_free(expr);
2548 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2549 * in "dim" by a value equal to the corresponding parameter.
2551 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2552 __isl_take isl_space *dim)
2554 if (!stmt)
2555 goto error;
2557 stmt->body = expr_detect_parameter_accesses(stmt->body,
2558 isl_space_copy(dim));
2560 if (!stmt->domain || !stmt->schedule || !stmt->body)
2561 goto error;
2563 isl_space_free(dim);
2564 return stmt;
2565 error:
2566 isl_space_free(dim);
2567 return pet_stmt_free(stmt);
2570 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2571 * in "dim" by a value equal to the corresponding parameter.
2573 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2574 __isl_take isl_space *dim)
2576 int i;
2578 if (!scop)
2579 goto error;
2581 for (i = 0; i < scop->n_stmt; ++i) {
2582 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2583 isl_space_copy(dim));
2584 if (!scop->stmts[i])
2585 goto error;
2588 isl_space_free(dim);
2589 return scop;
2590 error:
2591 isl_space_free(dim);
2592 return pet_scop_free(scop);
2595 /* Replace all accesses to (0D) arrays that correspond to any of
2596 * the parameters used in "scop" by a value equal
2597 * to the corresponding parameter.
2599 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2601 isl_space *dim;
2603 if (!scop)
2604 return NULL;
2606 dim = isl_set_get_space(scop->context);
2607 dim = scop_collect_params(scop, dim);
2609 scop = scop_detect_parameter_accesses(scop, dim);
2611 return scop;
2614 /* Add all read access relations (if "read" is set) and/or all write
2615 * access relations (if "write" is set) to "accesses" and return the result.
2617 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
2618 int read, int write, __isl_take isl_union_map *accesses)
2620 int i;
2621 isl_id *id;
2622 isl_space *dim;
2624 if (!expr)
2625 return NULL;
2627 for (i = 0; i < expr->n_arg; ++i)
2628 accesses = expr_collect_accesses(expr->args[i],
2629 read, write, accesses);
2631 if (expr->type == pet_expr_access &&
2632 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
2633 ((read && expr->acc.read) || (write && expr->acc.write)))
2634 accesses = isl_union_map_add_map(accesses,
2635 isl_map_copy(expr->acc.access));
2637 return accesses;
2640 /* Collect and return all read access relations (if "read" is set)
2641 * and/or all write access relations (if "write" is set) in "stmt".
2643 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2644 int read, int write, __isl_take isl_space *dim)
2646 isl_union_map *accesses;
2648 if (!stmt)
2649 return NULL;
2651 accesses = isl_union_map_empty(dim);
2652 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
2653 accesses = isl_union_map_intersect_domain(accesses,
2654 isl_union_set_from_set(isl_set_copy(stmt->domain)));
2656 return accesses;
2659 /* Collect and return all read access relations (if "read" is set)
2660 * and/or all write access relations (if "write" is set) in "scop".
2662 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2663 int read, int write)
2665 int i;
2666 isl_union_map *accesses;
2668 if (!scop)
2669 return NULL;
2671 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2673 for (i = 0; i < scop->n_stmt; ++i) {
2674 isl_union_map *accesses_i;
2675 isl_space *dim = isl_set_get_space(scop->context);
2676 accesses_i = stmt_collect_accesses(scop->stmts[i],
2677 read, write, dim);
2678 accesses = isl_union_map_union(accesses, accesses_i);
2681 return accesses;
2684 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
2686 return scop_collect_accesses(scop, 1, 0);
2689 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
2691 return scop_collect_accesses(scop, 0, 1);
2694 /* Collect and return the union of iteration domains in "scop".
2696 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2698 int i;
2699 isl_set *domain_i;
2700 isl_union_set *domain;
2702 if (!scop)
2703 return NULL;
2705 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2707 for (i = 0; i < scop->n_stmt; ++i) {
2708 domain_i = isl_set_copy(scop->stmts[i]->domain);
2709 domain = isl_union_set_add_set(domain, domain_i);
2712 return domain;
2715 /* Collect and return the schedules of the statements in "scop".
2716 * The range is normalized to the maximal number of scheduling
2717 * dimensions.
2719 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2721 int i, j;
2722 isl_map *schedule_i;
2723 isl_union_map *schedule;
2724 int depth, max_depth = 0;
2726 if (!scop)
2727 return NULL;
2729 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2731 for (i = 0; i < scop->n_stmt; ++i) {
2732 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2733 if (depth > max_depth)
2734 max_depth = depth;
2737 for (i = 0; i < scop->n_stmt; ++i) {
2738 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2739 depth = isl_map_dim(schedule_i, isl_dim_out);
2740 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2741 max_depth - depth);
2742 for (j = depth; j < max_depth; ++j)
2743 schedule_i = isl_map_fix_si(schedule_i,
2744 isl_dim_out, j, 0);
2745 schedule = isl_union_map_add_map(schedule, schedule_i);
2748 return schedule;
2751 /* Does expression "expr" write to "id"?
2753 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
2755 int i;
2756 isl_id *write_id;
2758 for (i = 0; i < expr->n_arg; ++i) {
2759 int writes = expr_writes(expr->args[i], id);
2760 if (writes < 0 || writes)
2761 return writes;
2764 if (expr->type != pet_expr_access)
2765 return 0;
2766 if (!expr->acc.write)
2767 return 0;
2768 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
2769 return 0;
2771 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
2772 isl_id_free(write_id);
2774 if (!write_id)
2775 return -1;
2777 return write_id == id;
2780 /* Does statement "stmt" write to "id"?
2782 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
2784 return expr_writes(stmt->body, id);
2787 /* Is there any write access in "scop" that accesses "id"?
2789 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
2791 int i;
2793 if (!scop)
2794 return -1;
2796 for (i = 0; i < scop->n_stmt; ++i) {
2797 int writes = stmt_writes(scop->stmts[i], id);
2798 if (writes < 0 || writes)
2799 return writes;
2802 return 0;
2805 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2807 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
2809 int i, n;
2811 n = isl_set_dim(set, isl_dim_param);
2812 for (i = 0; i < n; ++i) {
2813 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2814 const char *name = isl_id_get_name(id);
2815 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
2816 isl_id_free(id);
2819 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
2820 isl_id *id = isl_set_get_tuple_id(set);
2821 const char *name = isl_id_get_name(id);
2822 set = isl_set_set_tuple_name(set, name);
2823 isl_id_free(id);
2826 return set;
2829 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2831 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
2833 int i, n;
2835 n = isl_map_dim(map, isl_dim_param);
2836 for (i = 0; i < n; ++i) {
2837 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2838 const char *name = isl_id_get_name(id);
2839 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2840 isl_id_free(id);
2843 if (isl_map_has_tuple_id(map, isl_dim_in)) {
2844 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
2845 const char *name = isl_id_get_name(id);
2846 map = isl_map_set_tuple_name(map, isl_dim_in, name);
2847 isl_id_free(id);
2850 if (isl_map_has_tuple_id(map, isl_dim_out)) {
2851 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
2852 const char *name = isl_id_get_name(id);
2853 map = isl_map_set_tuple_name(map, isl_dim_out, name);
2854 isl_id_free(id);
2857 return map;
2860 /* Reset the user pointer on all parameter ids in "array".
2862 static struct pet_array *array_anonymize(struct pet_array *array)
2864 if (!array)
2865 return NULL;
2867 array->context = set_anonymize(array->context);
2868 array->extent = set_anonymize(array->extent);
2869 if (!array->context || !array->extent)
2870 return pet_array_free(array);
2872 return array;
2875 /* Reset the user pointer on all parameter and tuple ids in
2876 * the access relation of the access expression "expr".
2878 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
2880 expr->acc.access = map_anonymize(expr->acc.access);
2881 if (!expr->acc.access)
2882 return pet_expr_free(expr);
2884 return expr;
2887 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2889 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2891 int i;
2892 isl_space *space;
2893 isl_set *domain;
2895 if (!stmt)
2896 return NULL;
2898 stmt->domain = set_anonymize(stmt->domain);
2899 stmt->schedule = map_anonymize(stmt->schedule);
2900 if (!stmt->domain || !stmt->schedule)
2901 return pet_stmt_free(stmt);
2903 for (i = 0; i < stmt->n_arg; ++i) {
2904 stmt->args[i] = pet_expr_map_access(stmt->args[i],
2905 &access_anonymize, NULL);
2906 if (!stmt->args[i])
2907 return pet_stmt_free(stmt);
2910 stmt->body = pet_expr_map_access(stmt->body,
2911 &access_anonymize, NULL);
2912 if (!stmt->body)
2913 return pet_stmt_free(stmt);
2915 return stmt;
2918 /* Reset the user pointer on all parameter and tuple ids in "scop".
2920 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2922 int i;
2924 if (!scop)
2925 return NULL;
2927 scop->context = set_anonymize(scop->context);
2928 scop->context_value = set_anonymize(scop->context_value);
2929 if (!scop->context || !scop->context_value)
2930 return pet_scop_free(scop);
2932 for (i = 0; i < scop->n_array; ++i) {
2933 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2934 if (!scop->arrays[i])
2935 return pet_scop_free(scop);
2938 for (i = 0; i < scop->n_stmt; ++i) {
2939 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2940 if (!scop->stmts[i])
2941 return pet_scop_free(scop);
2944 return scop;
2947 /* Given a set "domain", return a wrapped relation with the given set
2948 * as domain and a range of dimension "n_arg", where each coordinate
2949 * is either unbounded or, if the corresponding element of args is of
2950 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2952 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2953 unsigned n_arg, struct pet_expr **args,
2954 __isl_keep isl_union_map *value_bounds)
2956 int i;
2957 isl_map *map;
2958 isl_space *space;
2959 isl_ctx *ctx = isl_set_get_ctx(domain);
2961 map = isl_map_from_domain(domain);
2962 space = isl_map_get_space(map);
2963 space = isl_space_add_dims(space, isl_dim_out, 1);
2965 for (i = 0; i < n_arg; ++i) {
2966 isl_map *map_i;
2967 struct pet_expr *arg = args[i];
2968 isl_id *id;
2969 isl_space *space2;
2971 map_i = isl_map_universe(isl_space_copy(space));
2972 if (arg->type == pet_expr_access) {
2973 isl_map *vb;
2974 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2975 space2 = isl_space_alloc(ctx, 0, 0, 1);
2976 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2977 vb = isl_union_map_extract_map(value_bounds, space2);
2978 if (!isl_map_plain_is_empty(vb))
2979 map_i = isl_map_intersect_range(map_i,
2980 isl_map_range(vb));
2981 else
2982 isl_map_free(vb);
2984 map = isl_map_flat_range_product(map, map_i);
2986 isl_space_free(space);
2988 return isl_map_wrap(map);
2991 /* Data used in access_gist() callback.
2993 struct pet_access_gist_data {
2994 isl_set *domain;
2995 isl_union_map *value_bounds;
2998 /* Given an expression "expr" of type pet_expr_access, compute
2999 * the gist of the associated access relation with respect to
3000 * data->domain and the bounds on the values of the arguments
3001 * of the expression.
3003 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
3005 struct pet_access_gist_data *data = user;
3006 isl_set *domain;
3008 domain = isl_set_copy(data->domain);
3009 if (expr->n_arg > 0)
3010 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
3011 data->value_bounds);
3013 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
3014 if (!expr->acc.access)
3015 return pet_expr_free(expr);
3017 return expr;
3020 /* Compute the gist of the iteration domain and all access relations
3021 * of "stmt" based on the constraints on the parameters specified by "context"
3022 * and the constraints on the values of nested accesses specified
3023 * by "value_bounds".
3025 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3026 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3028 int i;
3029 isl_space *space;
3030 isl_set *domain;
3031 struct pet_access_gist_data data;
3033 if (!stmt)
3034 return NULL;
3036 data.domain = isl_set_copy(stmt->domain);
3037 data.value_bounds = value_bounds;
3038 if (stmt->n_arg > 0)
3039 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3041 data.domain = isl_set_intersect_params(data.domain,
3042 isl_set_copy(context));
3044 for (i = 0; i < stmt->n_arg; ++i) {
3045 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3046 &access_gist, &data);
3047 if (!stmt->args[i])
3048 goto error;
3051 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
3052 if (!stmt->body)
3053 goto error;
3055 isl_set_free(data.domain);
3057 space = isl_set_get_space(stmt->domain);
3058 if (isl_space_is_wrapping(space))
3059 space = isl_space_domain(isl_space_unwrap(space));
3060 domain = isl_set_universe(space);
3061 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3062 if (stmt->n_arg > 0)
3063 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3064 value_bounds);
3065 stmt->domain = isl_set_gist(stmt->domain, domain);
3066 if (!stmt->domain)
3067 return pet_stmt_free(stmt);
3069 return stmt;
3070 error:
3071 isl_set_free(data.domain);
3072 return pet_stmt_free(stmt);
3075 /* Compute the gist of the extent of the array
3076 * based on the constraints on the parameters specified by "context".
3078 static struct pet_array *array_gist(struct pet_array *array,
3079 __isl_keep isl_set *context)
3081 if (!array)
3082 return NULL;
3084 array->extent = isl_set_gist_params(array->extent,
3085 isl_set_copy(context));
3086 if (!array->extent)
3087 return pet_array_free(array);
3089 return array;
3092 /* Compute the gist of all sets and relations in "scop"
3093 * based on the constraints on the parameters specified by "scop->context"
3094 * and the constraints on the values of nested accesses specified
3095 * by "value_bounds".
3097 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3098 __isl_keep isl_union_map *value_bounds)
3100 int i;
3102 if (!scop)
3103 return NULL;
3105 scop->context = isl_set_coalesce(scop->context);
3106 if (!scop->context)
3107 return pet_scop_free(scop);
3109 for (i = 0; i < scop->n_array; ++i) {
3110 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3111 if (!scop->arrays[i])
3112 return pet_scop_free(scop);
3115 for (i = 0; i < scop->n_stmt; ++i) {
3116 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3117 value_bounds);
3118 if (!scop->stmts[i])
3119 return pet_scop_free(scop);
3122 return scop;
3125 /* Intersect the context of "scop" with "context".
3126 * To ensure that we don't introduce any unnamed parameters in
3127 * the context of "scop", we first remove the unnamed parameters
3128 * from "context".
3130 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3131 __isl_take isl_set *context)
3133 if (!scop)
3134 goto error;
3136 context = set_project_out_unnamed_params(context);
3137 scop->context = isl_set_intersect(scop->context, context);
3138 if (!scop->context)
3139 return pet_scop_free(scop);
3141 return scop;
3142 error:
3143 isl_set_free(context);
3144 return pet_scop_free(scop);
3147 /* Drop the current context of "scop". That is, replace the context
3148 * by a universal set.
3150 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3152 isl_space *space;
3154 if (!scop)
3155 return NULL;
3157 space = isl_set_get_space(scop->context);
3158 isl_set_free(scop->context);
3159 scop->context = isl_set_universe(space);
3160 if (!scop->context)
3161 return pet_scop_free(scop);
3163 return scop;
3166 /* Append "array" to the arrays of "scop".
3168 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3169 struct pet_array *array)
3171 isl_ctx *ctx;
3172 struct pet_array **arrays;
3174 if (!array || !scop)
3175 goto error;
3177 ctx = isl_set_get_ctx(scop->context);
3178 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3179 scop->n_array + 1);
3180 if (!arrays)
3181 goto error;
3182 scop->arrays = arrays;
3183 scop->arrays[scop->n_array] = array;
3184 scop->n_array++;
3186 return scop;
3187 error:
3188 pet_array_free(array);
3189 return pet_scop_free(scop);