replace pet_expr_foreach_access by pet_expr_foreach_access_expr
[pet.git] / scop.c
blobf6337ccfac0512166a8db7f6da655027bdc7e115
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_foreach_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_foreach_access(expr->args[i],
539 fn, user);
540 if (!expr->args[i])
541 return pet_expr_free(expr);
544 if (expr->type == pet_expr_access)
545 expr = fn(expr, user);
547 return expr;
550 /* Modify the access relation of the given access expression
551 * based on the given iteration space transformation.
552 * If the access has any arguments then the domain of the access relation
553 * is a wrapped mapping from the iteration space to the space of
554 * argument values. We only need to change the domain of this wrapped
555 * mapping, so we extend the input transformation with an identity mapping
556 * on the space of argument values.
558 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
560 isl_map *update = user;
561 isl_space *dim;
563 update = isl_map_copy(update);
565 dim = isl_map_get_space(expr->acc.access);
566 dim = isl_space_domain(dim);
567 if (!isl_space_is_wrapping(dim))
568 isl_space_free(dim);
569 else {
570 isl_map *id;
571 dim = isl_space_unwrap(dim);
572 dim = isl_space_range(dim);
573 dim = isl_space_map_from_set(dim);
574 id = isl_map_identity(dim);
575 update = isl_map_product(update, id);
578 expr->acc.access = isl_map_apply_domain(expr->acc.access, update);
579 if (!expr->acc.access)
580 return pet_expr_free(expr);
582 return expr;
585 /* Modify all access relations in "expr" based on the given iteration space
586 * transformation.
588 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
589 __isl_take isl_map *update)
591 expr = pet_expr_foreach_access(expr, &update_domain, update);
592 isl_map_free(update);
593 return expr;
596 /* Construct a pet_stmt with given line number and statement
597 * number from a pet_expr.
598 * The initial iteration domain is the zero-dimensional universe.
599 * The name of the domain is given by "label" if it is non-NULL.
600 * Otherwise, the name is constructed as S_<id>.
601 * The domains of all access relations are modified to refer
602 * to the statement iteration domain.
604 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
605 __isl_take isl_id *label, int id, struct pet_expr *expr)
607 struct pet_stmt *stmt;
608 isl_space *dim;
609 isl_set *dom;
610 isl_map *sched;
611 isl_map *add_name;
612 char name[50];
614 if (!expr)
615 goto error;
617 stmt = isl_calloc_type(ctx, struct pet_stmt);
618 if (!stmt)
619 goto error;
621 dim = isl_space_set_alloc(ctx, 0, 0);
622 if (label)
623 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
624 else {
625 snprintf(name, sizeof(name), "S_%d", id);
626 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
628 dom = isl_set_universe(isl_space_copy(dim));
629 sched = isl_map_from_domain(isl_set_copy(dom));
631 dim = isl_space_from_range(dim);
632 add_name = isl_map_universe(dim);
633 expr = expr_update_domain(expr, add_name);
635 stmt->line = line;
636 stmt->domain = dom;
637 stmt->schedule = sched;
638 stmt->body = expr;
640 if (!stmt->domain || !stmt->schedule || !stmt->body)
641 return pet_stmt_free(stmt);
643 return stmt;
644 error:
645 isl_id_free(label);
646 return pet_expr_free(expr);
649 void *pet_stmt_free(struct pet_stmt *stmt)
651 int i;
653 if (!stmt)
654 return NULL;
656 isl_set_free(stmt->domain);
657 isl_map_free(stmt->schedule);
658 pet_expr_free(stmt->body);
660 for (i = 0; i < stmt->n_arg; ++i)
661 pet_expr_free(stmt->args[i]);
662 free(stmt->args);
664 free(stmt);
665 return NULL;
668 static void stmt_dump(struct pet_stmt *stmt, int indent)
670 int i;
672 if (!stmt)
673 return;
675 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
676 fprintf(stderr, "%*s", indent, "");
677 isl_set_dump(stmt->domain);
678 fprintf(stderr, "%*s", indent, "");
679 isl_map_dump(stmt->schedule);
680 expr_dump(stmt->body, indent);
681 for (i = 0; i < stmt->n_arg; ++i)
682 expr_dump(stmt->args[i], indent + 2);
685 void pet_stmt_dump(struct pet_stmt *stmt)
687 stmt_dump(stmt, 0);
690 struct pet_array *pet_array_free(struct pet_array *array)
692 if (!array)
693 return NULL;
695 isl_set_free(array->context);
696 isl_set_free(array->extent);
697 isl_set_free(array->value_bounds);
698 free(array->element_type);
700 free(array);
701 return NULL;
704 void pet_array_dump(struct pet_array *array)
706 if (!array)
707 return;
709 isl_set_dump(array->context);
710 isl_set_dump(array->extent);
711 isl_set_dump(array->value_bounds);
712 fprintf(stderr, "%s %s\n", array->element_type,
713 array->live_out ? "live-out" : "");
716 /* Alloc a pet_scop structure, with extra room for information that
717 * is only used during parsing.
719 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
721 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
724 /* Construct a pet_scop with room for n statements.
726 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
728 isl_space *space;
729 struct pet_scop *scop;
731 scop = pet_scop_alloc(ctx);
732 if (!scop)
733 return NULL;
735 space = isl_space_params_alloc(ctx, 0);
736 scop->context = isl_set_universe(isl_space_copy(space));
737 scop->context_value = isl_set_universe(space);
738 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
739 if (!scop->context || !scop->stmts)
740 return pet_scop_free(scop);
742 scop->n_stmt = n;
744 return scop;
747 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
749 return scop_alloc(ctx, 0);
752 /* Update "context" with respect to the valid parameter values for "access".
754 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
755 __isl_take isl_set *context)
757 context = isl_set_intersect(context,
758 isl_map_params(isl_map_copy(access)));
759 return context;
762 /* Update "context" with respect to the valid parameter values for "expr".
764 * If "expr" represents a ternary operator, then a parameter value
765 * needs to be valid for the condition and for at least one of the
766 * remaining two arguments.
767 * If the condition is an affine expression, then we can be a bit more specific.
768 * The parameter then has to be valid for the second argument for
769 * non-zero accesses and valid for the third argument for zero accesses.
771 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
772 __isl_take isl_set *context)
774 int i;
776 if (expr->type == pet_expr_ternary) {
777 int is_aff;
778 isl_set *context1, *context2;
780 is_aff = pet_expr_is_affine(expr->args[0]);
781 if (is_aff < 0)
782 goto error;
784 context = expr_extract_context(expr->args[0], context);
785 context1 = expr_extract_context(expr->args[1],
786 isl_set_copy(context));
787 context2 = expr_extract_context(expr->args[2], context);
789 if (is_aff) {
790 isl_map *access;
791 isl_set *zero_set;
793 access = isl_map_copy(expr->args[0]->acc.access);
794 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
795 zero_set = isl_map_params(access);
796 context1 = isl_set_subtract(context1,
797 isl_set_copy(zero_set));
798 context2 = isl_set_intersect(context2, zero_set);
801 context = isl_set_union(context1, context2);
802 context = isl_set_coalesce(context);
804 return context;
807 for (i = 0; i < expr->n_arg; ++i)
808 context = expr_extract_context(expr->args[i], context);
810 if (expr->type == pet_expr_access)
811 context = access_extract_context(expr->acc.access, context);
813 return context;
814 error:
815 isl_set_free(context);
816 return NULL;
819 /* Update "context" with respect to the valid parameter values for "stmt".
821 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
822 __isl_take isl_set *context)
824 int i;
826 for (i = 0; i < stmt->n_arg; ++i)
827 context = expr_extract_context(stmt->args[i], context);
829 context = expr_extract_context(stmt->body, context);
831 return context;
834 /* Construct a pet_scop that contains the given pet_stmt.
836 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
838 struct pet_scop *scop;
840 if (!stmt)
841 return NULL;
843 scop = scop_alloc(ctx, 1);
844 if (!scop)
845 goto error;
847 scop->context = stmt_extract_context(stmt, scop->context);
848 if (!scop->context)
849 goto error;
851 scop->stmts[0] = stmt;
853 return scop;
854 error:
855 pet_stmt_free(stmt);
856 pet_scop_free(scop);
857 return NULL;
860 /* Does "set" represent an element of an unnamed space, i.e.,
861 * does it represent an affine expression?
863 static int set_is_affine(__isl_keep isl_set *set)
865 int has_id;
867 has_id = isl_set_has_tuple_id(set);
868 if (has_id < 0)
869 return -1;
871 return !has_id;
874 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
875 * ext may be equal to either ext1 or ext2.
877 * The two skips that need to be combined are assumed to be affine expressions.
879 * We need to skip in ext if we need to skip in either ext1 or ext2.
880 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
882 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
883 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
884 enum pet_skip type)
886 isl_set *set, *skip1, *skip2;
888 if (!ext)
889 return NULL;
890 if (!ext1->skip[type] && !ext2->skip[type])
891 return ext;
892 if (!ext1->skip[type]) {
893 if (ext == ext2)
894 return ext;
895 ext->skip[type] = ext2->skip[type];
896 ext2->skip[type] = NULL;
897 return ext;
899 if (!ext2->skip[type]) {
900 if (ext == ext1)
901 return ext;
902 ext->skip[type] = ext1->skip[type];
903 ext1->skip[type] = NULL;
904 return ext;
907 if (!set_is_affine(ext1->skip[type]) ||
908 !set_is_affine(ext2->skip[type]))
909 isl_die(isl_set_get_ctx(ext1->skip[type]), isl_error_internal,
910 "can only combine affine skips",
911 return pet_scop_free(&ext->scop));
913 skip1 = isl_set_copy(ext1->skip[type]);
914 skip2 = isl_set_copy(ext2->skip[type]);
915 set = isl_set_intersect(
916 isl_set_fix_si(isl_set_copy(skip1), isl_dim_set, 0, 0),
917 isl_set_fix_si(isl_set_copy(skip2), isl_dim_set, 0, 0));
918 set = isl_set_union(set, isl_set_fix_si(skip1, isl_dim_set, 0, 1));
919 set = isl_set_union(set, isl_set_fix_si(skip2, isl_dim_set, 0, 1));
920 set = isl_set_coalesce(set);
921 isl_set_free(ext1->skip[type]);
922 ext1->skip[type] = NULL;
923 isl_set_free(ext2->skip[type]);
924 ext2->skip[type] = NULL;
925 ext->skip[type] = set;
926 if (!ext->skip[type])
927 return pet_scop_free(&ext->scop);
929 return ext;
932 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
933 * where type takes on the values pet_skip_now and pet_skip_later.
934 * scop may be equal to either scop1 or scop2.
936 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
937 struct pet_scop *scop1, struct pet_scop *scop2)
939 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
940 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
941 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
943 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
944 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
945 return &ext->scop;
948 /* Update scop->start and scop->end to include the region from "start"
949 * to "end". In particular, if scop->end == 0, then "scop" does not
950 * have any offset information yet and we simply take the information
951 * from "start" and "end". Otherwise, we update the fields if the
952 * region from "start" to "end" is not already included.
954 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
955 unsigned start, unsigned end)
957 if (!scop)
958 return NULL;
959 if (scop->end == 0) {
960 scop->start = start;
961 scop->end = end;
962 } else {
963 if (start < scop->start)
964 scop->start = start;
965 if (end > scop->end)
966 scop->end = end;
969 return scop;
972 /* Combine the offset information of "scop1" and "scop2" into "scop".
974 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
975 struct pet_scop *scop1, struct pet_scop *scop2)
977 if (scop1->end)
978 scop = pet_scop_update_start_end(scop,
979 scop1->start, scop1->end);
980 if (scop2->end)
981 scop = pet_scop_update_start_end(scop,
982 scop2->start, scop2->end);
983 return scop;
986 /* Construct a pet_scop that contains the offset information,
987 * arrays, statements and skip information in "scop1" and "scop2".
989 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
990 struct pet_scop *scop2)
992 int i;
993 struct pet_scop *scop = NULL;
995 if (!scop1 || !scop2)
996 goto error;
998 if (scop1->n_stmt == 0) {
999 scop2 = scop_combine_skips(scop2, scop1, scop2);
1000 pet_scop_free(scop1);
1001 return scop2;
1004 if (scop2->n_stmt == 0) {
1005 scop1 = scop_combine_skips(scop1, scop1, scop2);
1006 pet_scop_free(scop2);
1007 return scop1;
1010 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1011 if (!scop)
1012 goto error;
1014 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1015 scop1->n_array + scop2->n_array);
1016 if (!scop->arrays)
1017 goto error;
1018 scop->n_array = scop1->n_array + scop2->n_array;
1020 for (i = 0; i < scop1->n_stmt; ++i) {
1021 scop->stmts[i] = scop1->stmts[i];
1022 scop1->stmts[i] = NULL;
1025 for (i = 0; i < scop2->n_stmt; ++i) {
1026 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1027 scop2->stmts[i] = NULL;
1030 for (i = 0; i < scop1->n_array; ++i) {
1031 scop->arrays[i] = scop1->arrays[i];
1032 scop1->arrays[i] = NULL;
1035 for (i = 0; i < scop2->n_array; ++i) {
1036 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1037 scop2->arrays[i] = NULL;
1040 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1041 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1042 scop = scop_combine_skips(scop, scop1, scop2);
1043 scop = scop_combine_start_end(scop, scop1, scop2);
1045 pet_scop_free(scop1);
1046 pet_scop_free(scop2);
1047 return scop;
1048 error:
1049 pet_scop_free(scop1);
1050 pet_scop_free(scop2);
1051 pet_scop_free(scop);
1052 return NULL;
1055 /* Apply the skip condition "skip" to "scop".
1056 * That is, make sure "scop" is not executed when the condition holds.
1058 * If "skip" is an affine expression, we add the conditions under
1059 * which the expression is zero to the iteration domains.
1060 * Otherwise, we add a filter on the variable attaining the value zero.
1062 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1063 __isl_take isl_set *skip)
1065 isl_map *skip_map;
1066 int is_aff;
1068 if (!scop || !skip)
1069 goto error;
1071 is_aff = set_is_affine(skip);
1072 if (is_aff < 0)
1073 goto error;
1075 if (!is_aff)
1076 return pet_scop_filter(scop, isl_map_from_range(skip), 0);
1078 skip = isl_set_fix_si(skip, isl_dim_set, 0, 0);
1079 scop = pet_scop_restrict(scop, isl_set_params(skip));
1081 return scop;
1082 error:
1083 isl_set_free(skip);
1084 return pet_scop_free(scop);
1087 /* Construct a pet_scop that contains the arrays, statements and
1088 * skip information in "scop1" and "scop2", where the two scops
1089 * are executed "in sequence". That is, breaks and continues
1090 * in scop1 have an effect on scop2.
1092 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1093 struct pet_scop *scop2)
1095 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1096 scop2 = restrict_skip(scop2,
1097 pet_scop_get_skip(scop1, pet_skip_now));
1098 return pet_scop_add(ctx, scop1, scop2);
1101 /* Construct a pet_scop that contains the arrays, statements and
1102 * skip information in "scop1" and "scop2", where the two scops
1103 * are executed "in parallel". That is, any break or continue
1104 * in scop1 has no effect on scop2.
1106 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1107 struct pet_scop *scop2)
1109 return pet_scop_add(ctx, scop1, scop2);
1112 void *pet_scop_free(struct pet_scop *scop)
1114 int i;
1115 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1117 if (!scop)
1118 return NULL;
1119 isl_set_free(scop->context);
1120 isl_set_free(scop->context_value);
1121 if (scop->arrays)
1122 for (i = 0; i < scop->n_array; ++i)
1123 pet_array_free(scop->arrays[i]);
1124 free(scop->arrays);
1125 if (scop->stmts)
1126 for (i = 0; i < scop->n_stmt; ++i)
1127 pet_stmt_free(scop->stmts[i]);
1128 free(scop->stmts);
1129 isl_set_free(ext->skip[pet_skip_now]);
1130 isl_set_free(ext->skip[pet_skip_later]);
1131 free(scop);
1132 return NULL;
1135 void pet_scop_dump(struct pet_scop *scop)
1137 int i;
1138 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1140 if (!scop)
1141 return;
1143 isl_set_dump(scop->context);
1144 isl_set_dump(scop->context_value);
1145 for (i = 0; i < scop->n_array; ++i)
1146 pet_array_dump(scop->arrays[i]);
1147 for (i = 0; i < scop->n_stmt; ++i)
1148 pet_stmt_dump(scop->stmts[i]);
1150 if (ext->skip[0]) {
1151 fprintf(stderr, "skip\n");
1152 isl_set_dump(ext->skip[0]);
1153 isl_set_dump(ext->skip[1]);
1157 /* Return 1 if the two pet_arrays are equivalent.
1159 * We don't compare element_size as this may be target dependent.
1161 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1163 if (!array1 || !array2)
1164 return 0;
1166 if (!isl_set_is_equal(array1->context, array2->context))
1167 return 0;
1168 if (!isl_set_is_equal(array1->extent, array2->extent))
1169 return 0;
1170 if (!!array1->value_bounds != !!array2->value_bounds)
1171 return 0;
1172 if (array1->value_bounds &&
1173 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1174 return 0;
1175 if (strcmp(array1->element_type, array2->element_type))
1176 return 0;
1177 if (array1->live_out != array2->live_out)
1178 return 0;
1179 if (array1->uniquely_defined != array2->uniquely_defined)
1180 return 0;
1181 if (array1->declared != array2->declared)
1182 return 0;
1183 if (array1->exposed != array2->exposed)
1184 return 0;
1186 return 1;
1189 /* Return 1 if the two pet_stmts are equivalent.
1191 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1193 int i;
1195 if (!stmt1 || !stmt2)
1196 return 0;
1198 if (stmt1->line != stmt2->line)
1199 return 0;
1200 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1201 return 0;
1202 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1203 return 0;
1204 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1205 return 0;
1206 if (stmt1->n_arg != stmt2->n_arg)
1207 return 0;
1208 for (i = 0; i < stmt1->n_arg; ++i) {
1209 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1210 return 0;
1213 return 1;
1216 /* Return 1 if the two pet_scops are equivalent.
1218 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1220 int i;
1222 if (!scop1 || !scop2)
1223 return 0;
1225 if (!isl_set_is_equal(scop1->context, scop2->context))
1226 return 0;
1227 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1228 return 0;
1230 if (scop1->n_array != scop2->n_array)
1231 return 0;
1232 for (i = 0; i < scop1->n_array; ++i)
1233 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1234 return 0;
1236 if (scop1->n_stmt != scop2->n_stmt)
1237 return 0;
1238 for (i = 0; i < scop1->n_stmt; ++i)
1239 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1240 return 0;
1242 return 1;
1245 /* Prefix the schedule of "stmt" with an extra dimension with constant
1246 * value "pos".
1248 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1250 if (!stmt)
1251 return NULL;
1253 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1254 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1255 if (!stmt->schedule)
1256 return pet_stmt_free(stmt);
1258 return stmt;
1261 /* Prefix the schedules of all statements in "scop" with an extra
1262 * dimension with constant value "pos".
1264 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1266 int i;
1268 if (!scop)
1269 return NULL;
1271 for (i = 0; i < scop->n_stmt; ++i) {
1272 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1273 if (!scop->stmts[i])
1274 return pet_scop_free(scop);
1277 return scop;
1280 /* Given a set with a parameter at "param_pos" that refers to the
1281 * iterator, "move" the iterator to the first set dimension.
1282 * That is, essentially equate the parameter to the first set dimension
1283 * and then project it out.
1285 * The first set dimension may however refer to a virtual iterator,
1286 * while the parameter refers to the "real" iterator.
1287 * We therefore need to take into account the mapping "iv_map", which
1288 * maps the virtual iterator to the real iterator.
1289 * In particular, we equate the set dimension to the input of the map
1290 * and the parameter to the output of the map and then project out
1291 * everything we don't need anymore.
1293 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1294 int param_pos, __isl_take isl_map *iv_map)
1296 isl_map *map;
1297 map = isl_map_from_domain(set);
1298 map = isl_map_add_dims(map, isl_dim_out, 1);
1299 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1300 iv_map = isl_map_align_params(iv_map, isl_map_get_space(map));
1301 map = isl_map_apply_range(map, iv_map);
1302 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1303 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1304 return isl_map_domain(map);
1307 /* Data used in embed_access.
1308 * extend adds an iterator to the iteration domain
1309 * iv_map maps the virtual iterator to the real iterator
1310 * var_id represents the induction variable of the corresponding loop
1312 struct pet_embed_access {
1313 isl_map *extend;
1314 isl_map *iv_map;
1315 isl_id *var_id;
1318 /* Given an access expression, embed the associated access relation
1319 * in an extra outer loop.
1321 * We first update the iteration domain to insert the extra dimension.
1323 * If the access refers to the induction variable, then it is
1324 * turned into an access to the set of integers with index (and value)
1325 * equal to the induction variable.
1327 * If the induction variable appears in the constraints (as a parameter),
1328 * then the parameter is equated to the newly introduced iteration
1329 * domain dimension and subsequently projected out.
1331 * Similarly, if the accessed array is a virtual array (with user
1332 * pointer equal to NULL), as created by create_test_access,
1333 * then it is extended along with the domain of the access.
1335 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
1337 struct pet_embed_access *data = user;
1338 isl_map *access;
1339 isl_id *array_id = NULL;
1340 int pos;
1342 expr = update_domain(expr, data->extend);
1343 if (!expr)
1344 return NULL;
1346 access = expr->acc.access;
1348 if (isl_map_has_tuple_id(access, isl_dim_out))
1349 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1350 if (array_id == data->var_id ||
1351 (array_id && !isl_id_get_user(array_id))) {
1352 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1353 access = isl_map_equate(access,
1354 isl_dim_in, 0, isl_dim_out, 0);
1355 if (array_id == data->var_id)
1356 access = isl_map_apply_range(access,
1357 isl_map_copy(data->iv_map));
1358 else
1359 access = isl_map_set_tuple_id(access, isl_dim_out,
1360 isl_id_copy(array_id));
1362 isl_id_free(array_id);
1364 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1365 if (pos >= 0) {
1366 isl_set *set = isl_map_wrap(access);
1367 set = internalize_iv(set, pos, isl_map_copy(data->iv_map));
1368 access = isl_set_unwrap(set);
1370 expr->acc.access = isl_map_set_dim_id(access, isl_dim_in, 0,
1371 isl_id_copy(data->var_id));
1372 if (!expr->acc.access)
1373 return pet_expr_free(expr);
1375 return expr;
1378 /* Embed all access subexpressions of "expr" in an extra loop.
1379 * "extend" inserts an outer loop iterator in the iteration domains.
1380 * "iv_map" maps the virtual iterator to the real iterator
1381 * "var_id" represents the induction variable.
1383 static struct pet_expr *expr_embed(struct pet_expr *expr,
1384 __isl_take isl_map *extend, __isl_take isl_map *iv_map,
1385 __isl_keep isl_id *var_id)
1387 struct pet_embed_access data =
1388 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1390 expr = pet_expr_foreach_access(expr, &embed_access, &data);
1391 isl_map_free(iv_map);
1392 isl_map_free(extend);
1393 return expr;
1396 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1397 * "dom" and schedule "sched". "var_id" represents the induction variable
1398 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1399 * That is, it maps the iterator used in "dom" and the domain of "sched"
1400 * to the iterator that some of the parameters in "stmt" may refer to.
1402 * The iteration domain and schedule of the statement are updated
1403 * according to the iteration domain and schedule of the new loop.
1404 * If stmt->domain is a wrapped map, then the iteration domain
1405 * is the domain of this map, so we need to be careful to adjust
1406 * this domain.
1408 * If the induction variable appears in the constraints (as a parameter)
1409 * of the current iteration domain or the schedule of the statement,
1410 * then the parameter is equated to the newly introduced iteration
1411 * domain dimension and subsequently projected out.
1413 * Finally, all access relations are updated based on the extra loop.
1415 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1416 __isl_take isl_set *dom, __isl_take isl_map *sched,
1417 __isl_take isl_map *iv_map, __isl_take isl_id *var_id)
1419 int i;
1420 int pos;
1421 isl_id *stmt_id;
1422 isl_space *dim;
1423 isl_map *extend;
1425 if (!stmt)
1426 goto error;
1428 if (isl_set_is_wrapping(stmt->domain)) {
1429 isl_map *map;
1430 isl_map *ext;
1431 isl_space *ran_dim;
1433 map = isl_set_unwrap(stmt->domain);
1434 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1435 ran_dim = isl_space_range(isl_map_get_space(map));
1436 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1437 isl_set_universe(ran_dim));
1438 map = isl_map_flat_domain_product(ext, map);
1439 map = isl_map_set_tuple_id(map, isl_dim_in,
1440 isl_id_copy(stmt_id));
1441 dim = isl_space_domain(isl_map_get_space(map));
1442 stmt->domain = isl_map_wrap(map);
1443 } else {
1444 stmt_id = isl_set_get_tuple_id(stmt->domain);
1445 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1446 stmt->domain);
1447 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1448 isl_id_copy(stmt_id));
1449 dim = isl_set_get_space(stmt->domain);
1452 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1453 if (pos >= 0)
1454 stmt->domain = internalize_iv(stmt->domain, pos,
1455 isl_map_copy(iv_map));
1457 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1458 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1459 isl_dim_in, stmt_id);
1461 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1462 if (pos >= 0) {
1463 isl_set *set = isl_map_wrap(stmt->schedule);
1464 set = internalize_iv(set, pos, isl_map_copy(iv_map));
1465 stmt->schedule = isl_set_unwrap(set);
1468 dim = isl_space_map_from_set(dim);
1469 extend = isl_map_identity(dim);
1470 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1471 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1472 isl_map_get_tuple_id(extend, isl_dim_out));
1473 for (i = 0; i < stmt->n_arg; ++i)
1474 stmt->args[i] = expr_embed(stmt->args[i], isl_map_copy(extend),
1475 isl_map_copy(iv_map), var_id);
1476 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1478 isl_set_free(dom);
1479 isl_id_free(var_id);
1481 for (i = 0; i < stmt->n_arg; ++i)
1482 if (!stmt->args[i])
1483 return pet_stmt_free(stmt);
1484 if (!stmt->domain || !stmt->schedule || !stmt->body)
1485 return pet_stmt_free(stmt);
1486 return stmt;
1487 error:
1488 isl_set_free(dom);
1489 isl_map_free(sched);
1490 isl_map_free(iv_map);
1491 isl_id_free(var_id);
1492 return NULL;
1495 /* Embed the given pet_array in an extra outer loop with iteration domain
1496 * "dom".
1497 * This embedding only has an effect on virtual arrays (those with
1498 * user pointer equal to NULL), which need to be extended along with
1499 * the iteration domain.
1501 static struct pet_array *pet_array_embed(struct pet_array *array,
1502 __isl_take isl_set *dom)
1504 isl_id *array_id = NULL;
1506 if (!array)
1507 goto error;
1509 if (isl_set_has_tuple_id(array->extent))
1510 array_id = isl_set_get_tuple_id(array->extent);
1512 if (array_id && !isl_id_get_user(array_id)) {
1513 array->extent = isl_set_flat_product(dom, array->extent);
1514 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1515 if (!array->extent)
1516 return pet_array_free(array);
1517 } else {
1518 isl_set_free(dom);
1519 isl_id_free(array_id);
1522 return array;
1523 error:
1524 isl_set_free(dom);
1525 return NULL;
1528 /* Project out all unnamed parameters from "set" and return the result.
1530 static __isl_give isl_set *set_project_out_unnamed_params(
1531 __isl_take isl_set *set)
1533 int i, n;
1535 n = isl_set_dim(set, isl_dim_param);
1536 for (i = n - 1; i >= 0; --i) {
1537 if (isl_set_has_dim_name(set, isl_dim_param, i))
1538 continue;
1539 set = isl_set_project_out(set, isl_dim_param, i, 1);
1542 return set;
1545 /* Update the context with respect to an embedding into a loop
1546 * with iteration domain "dom" and induction variable "id".
1547 * "iv_map" maps a possibly virtual iterator (used in "dom")
1548 * to the real iterator (parameter "id").
1550 * If the current context is independent of "id", we don't need
1551 * to do anything.
1552 * Otherwise, a parameter value is invalid for the embedding if
1553 * any of the corresponding iterator values is invalid.
1554 * That is, a parameter value is valid only if all the corresponding
1555 * iterator values are valid.
1556 * We therefore compute the set of parameters
1558 * forall i in dom : valid (i)
1560 * or
1562 * not exists i in dom : not valid(i)
1564 * i.e.,
1566 * not exists i in dom \ valid(i)
1568 * Before we subtract valid(i) from dom, we first need to map
1569 * the real iterator to the virtual iterator.
1571 * If there are any unnamed parameters in "dom", then we consider
1572 * a parameter value to be valid if it is valid for any value of those
1573 * unnamed parameters. They are therefore projected out at the end.
1575 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1576 __isl_keep isl_set *dom, __isl_keep isl_map *iv_map,
1577 __isl_keep isl_id *id)
1579 int pos;
1581 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1582 if (pos < 0)
1583 return context;
1585 context = isl_set_from_params(context);
1586 context = isl_set_add_dims(context, isl_dim_set, 1);
1587 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1588 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1589 context = isl_set_apply(context, isl_map_reverse(isl_map_copy(iv_map)));
1590 context = isl_set_subtract(isl_set_copy(dom), context);
1591 context = isl_set_params(context);
1592 context = isl_set_complement(context);
1593 context = set_project_out_unnamed_params(context);
1594 return context;
1597 /* Embed all statements and arrays in "scop" in an extra outer loop
1598 * with iteration domain "dom" and schedule "sched".
1599 * "id" represents the induction variable of the loop.
1600 * "iv_map" maps a possibly virtual iterator to the real iterator.
1601 * That is, it maps the iterator used in "dom" and the domain of "sched"
1602 * to the iterator that some of the parameters in "scop" may refer to.
1604 * Any skip conditions within the loop have no effect outside of the loop.
1605 * The caller is responsible for making sure skip[pet_skip_later] has been
1606 * taken into account.
1608 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1609 __isl_take isl_map *sched, __isl_take isl_map *iv_map,
1610 __isl_take isl_id *id)
1612 int i;
1614 if (!scop)
1615 goto error;
1617 pet_scop_reset_skip(scop, pet_skip_now);
1618 pet_scop_reset_skip(scop, pet_skip_later);
1620 scop->context = context_embed(scop->context, dom, iv_map, id);
1621 if (!scop->context)
1622 goto error;
1624 for (i = 0; i < scop->n_stmt; ++i) {
1625 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1626 isl_set_copy(dom), isl_map_copy(sched),
1627 isl_map_copy(iv_map), isl_id_copy(id));
1628 if (!scop->stmts[i])
1629 goto error;
1632 for (i = 0; i < scop->n_array; ++i) {
1633 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1634 isl_set_copy(dom));
1635 if (!scop->arrays[i])
1636 goto error;
1639 isl_set_free(dom);
1640 isl_map_free(sched);
1641 isl_map_free(iv_map);
1642 isl_id_free(id);
1643 return scop;
1644 error:
1645 isl_set_free(dom);
1646 isl_map_free(sched);
1647 isl_map_free(iv_map);
1648 isl_id_free(id);
1649 return pet_scop_free(scop);
1652 /* Add extra conditions on the parameters to iteration domain of "stmt".
1654 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1655 __isl_take isl_set *cond)
1657 if (!stmt)
1658 goto error;
1660 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1662 return stmt;
1663 error:
1664 isl_set_free(cond);
1665 return pet_stmt_free(stmt);
1668 /* Add extra conditions to scop->skip[type].
1670 * The new skip condition only holds if it held before
1671 * and the condition is true. It does not hold if it did not hold
1672 * before or the condition is false.
1674 * The skip condition is assumed to be an affine expression.
1676 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1677 enum pet_skip type, __isl_keep isl_set *cond)
1679 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1680 isl_set *skip;
1681 isl_set *set;
1683 if (!scop)
1684 return NULL;
1685 if (!ext->skip[type])
1686 return scop;
1688 if (!set_is_affine(ext->skip[type]))
1689 isl_die(isl_set_get_ctx(ext->skip[type]), isl_error_internal,
1690 "can only resrict affine skips",
1691 return pet_scop_free(scop));
1693 skip = ext->skip[type];
1694 skip = isl_set_intersect_params(skip, isl_set_copy(cond));
1695 set = isl_set_from_params(isl_set_copy(cond));
1696 set = isl_set_complement(set);
1697 set = isl_set_add_dims(set, isl_dim_set, 1);
1698 set = isl_set_fix_si(set, isl_dim_set, 0, 0);
1699 skip = isl_set_union(skip, set);
1700 ext->skip[type] = skip;
1701 if (!ext->skip[type])
1702 return pet_scop_free(scop);
1704 return scop;
1707 /* Add extra conditions on the parameters to all iteration domains
1708 * and skip conditions.
1710 * A parameter value is valid for the result if it was valid
1711 * for the original scop and satisfies "cond" or if it does
1712 * not satisfy "cond" as in this case the scop is not executed
1713 * and the original constraints on the parameters are irrelevant.
1715 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1716 __isl_take isl_set *cond)
1718 int i;
1720 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1721 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1723 if (!scop)
1724 goto error;
1726 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1727 scop->context = isl_set_union(scop->context,
1728 isl_set_complement(isl_set_copy(cond)));
1729 scop->context = isl_set_coalesce(scop->context);
1730 scop->context = set_project_out_unnamed_params(scop->context);
1731 if (!scop->context)
1732 goto error;
1734 for (i = 0; i < scop->n_stmt; ++i) {
1735 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1736 isl_set_copy(cond));
1737 if (!scop->stmts[i])
1738 goto error;
1741 isl_set_free(cond);
1742 return scop;
1743 error:
1744 isl_set_free(cond);
1745 return pet_scop_free(scop);
1748 /* Construct a map that inserts a filter value with name "id" and value
1749 * "satisfied" in the list of filter values embedded in the set space "space".
1751 * If "space" does not contain any filter values yet, we first create
1752 * a map that inserts 0 filter values, i.e.,
1754 * space -> [space -> []]
1756 * We can now assume that space is of the form [dom -> [filters]]
1757 * We construct an identity mapping on dom and a mapping on filters
1758 * that inserts the new filter
1760 * dom -> dom
1761 * [filters] -> [satisfied, filters]
1763 * and then compute the cross product
1765 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1767 static __isl_give isl_map *insert_filter_map(__isl_take isl_space *space,
1768 __isl_take isl_id *id, int satisfied)
1770 isl_space *space2;
1771 isl_map *map, *map_dom, *map_ran;
1772 isl_set *dom;
1774 if (isl_space_is_wrapping(space)) {
1775 space2 = isl_space_map_from_set(isl_space_copy(space));
1776 map = isl_map_identity(space2);
1777 space = isl_space_unwrap(space);
1778 } else {
1779 space = isl_space_from_domain(space);
1780 map = isl_map_universe(isl_space_copy(space));
1781 map = isl_map_reverse(isl_map_domain_map(map));
1784 space2 = isl_space_domain(isl_space_copy(space));
1785 map_dom = isl_map_identity(isl_space_map_from_set(space2));
1786 space = isl_space_range(space);
1787 map_ran = isl_map_identity(isl_space_map_from_set(space));
1788 map_ran = isl_map_insert_dims(map_ran, isl_dim_out, 0, 1);
1789 map_ran = isl_map_set_dim_id(map_ran, isl_dim_out, 0, id);
1790 map_ran = isl_map_fix_si(map_ran, isl_dim_out, 0, satisfied);
1792 map = isl_map_apply_range(map, isl_map_product(map_dom, map_ran));
1794 return map;
1797 /* Insert an argument expression corresponding to "test" in front
1798 * of the list of arguments described by *n_arg and *args.
1800 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
1801 __isl_keep isl_map *test)
1803 int i;
1804 isl_ctx *ctx = isl_map_get_ctx(test);
1806 if (!test)
1807 return -1;
1809 if (!*args) {
1810 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
1811 if (!*args)
1812 return -1;
1813 } else {
1814 struct pet_expr **ext;
1815 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
1816 if (!ext)
1817 return -1;
1818 for (i = 0; i < *n_arg; ++i)
1819 ext[1 + i] = (*args)[i];
1820 free(*args);
1821 *args = ext;
1823 (*n_arg)++;
1824 (*args)[0] = pet_expr_from_access(isl_map_copy(test));
1825 if (!(*args)[0])
1826 return -1;
1828 return 0;
1831 /* Make the expression "expr" depend on the value of "test"
1832 * being equal to "satisfied".
1834 * If "test" is an affine expression, we simply add the conditions
1835 * on the expression have the value "satisfied" to all access relations.
1837 * Otherwise, we add a filter to "expr" (which is then assumed to be
1838 * an access expression) corresponding to "test" being equal to "satisfied".
1840 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
1841 __isl_take isl_map *test, int satisfied)
1843 isl_id *id;
1844 isl_ctx *ctx;
1845 isl_space *space;
1846 isl_map *map;
1848 if (!expr || !test)
1849 goto error;
1851 if (!isl_map_has_tuple_id(test, isl_dim_out)) {
1852 test = isl_map_fix_si(test, isl_dim_out, 0, satisfied);
1853 return pet_expr_restrict(expr, isl_map_params(test));
1856 ctx = isl_map_get_ctx(test);
1857 if (expr->type != pet_expr_access)
1858 isl_die(ctx, isl_error_invalid,
1859 "can only filter access expressions", goto error);
1861 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1862 id = isl_map_get_tuple_id(test, isl_dim_out);
1863 map = insert_filter_map(space, id, satisfied);
1865 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
1866 if (!expr->acc.access)
1867 goto error;
1869 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
1870 goto error;
1872 isl_map_free(test);
1873 return expr;
1874 error:
1875 isl_map_free(test);
1876 return pet_expr_free(expr);
1879 /* Make the statement "stmt" depend on the value of "test"
1880 * being equal to "satisfied" by adjusting stmt->domain.
1882 * The domain of "test" corresponds to the (zero or more) outer dimensions
1883 * of the iteration domain.
1885 * We insert an argument corresponding to a read to "test"
1886 * from the iteration domain of "stmt" in front of the list of arguments.
1887 * We also insert a corresponding output dimension in the wrapped
1888 * map contained in stmt->domain, with value set to "satisfied".
1890 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1891 __isl_take isl_map *test, int satisfied)
1893 int i;
1894 isl_id *id;
1895 isl_ctx *ctx;
1896 isl_map *map, *add_dom;
1897 isl_space *space;
1898 isl_set *dom;
1899 int n_test_dom;
1901 if (!stmt || !test)
1902 goto error;
1904 id = isl_map_get_tuple_id(test, isl_dim_out);
1905 map = insert_filter_map(isl_set_get_space(stmt->domain), id, satisfied);
1906 stmt->domain = isl_set_apply(stmt->domain, map);
1908 space = isl_space_unwrap(isl_set_get_space(stmt->domain));
1909 dom = isl_set_universe(isl_space_domain(space));
1910 n_test_dom = isl_map_dim(test, isl_dim_in);
1911 add_dom = isl_map_from_range(dom);
1912 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
1913 for (i = 0; i < n_test_dom; ++i)
1914 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
1915 isl_dim_out, i);
1916 test = isl_map_apply_domain(test, add_dom);
1918 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1919 goto error;
1921 isl_map_free(test);
1922 return stmt;
1923 error:
1924 isl_map_free(test);
1925 return pet_stmt_free(stmt);
1928 /* Does "scop" have a skip condition of the given "type"?
1930 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1932 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1934 if (!scop)
1935 return -1;
1936 return ext->skip[type] != NULL;
1939 /* Does "scop" have a skip condition of the given "type" that
1940 * is an affine expression?
1942 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1944 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1946 if (!scop)
1947 return -1;
1948 if (!ext->skip[type])
1949 return 0;
1950 return set_is_affine(ext->skip[type]);
1953 /* Does "scop" have a skip condition of the given "type" that
1954 * is not an affine expression?
1956 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1958 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1959 int aff;
1961 if (!scop)
1962 return -1;
1963 if (!ext->skip[type])
1964 return 0;
1965 aff = set_is_affine(ext->skip[type]);
1966 if (aff < 0)
1967 return -1;
1968 return !aff;
1971 /* Does "scop" have a skip condition of the given "type" that
1972 * is affine and holds on the entire domain?
1974 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1976 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1977 isl_set *set;
1978 int is_aff;
1979 int is_univ;
1981 is_aff = pet_scop_has_affine_skip(scop, type);
1982 if (is_aff < 0 || !is_aff)
1983 return is_aff;
1985 set = isl_set_copy(ext->skip[type]);
1986 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
1987 set = isl_set_params(set);
1988 is_univ = isl_set_plain_is_universe(set);
1989 isl_set_free(set);
1991 return is_univ;
1994 /* Replace scop->skip[type] by "skip".
1996 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
1997 enum pet_skip type, __isl_take isl_set *skip)
1999 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2001 if (!scop || !skip)
2002 goto error;
2004 isl_set_free(ext->skip[type]);
2005 ext->skip[type] = skip;
2007 return scop;
2008 error:
2009 isl_set_free(skip);
2010 return pet_scop_free(scop);
2013 /* Return a copy of scop->skip[type].
2015 __isl_give isl_set *pet_scop_get_skip(struct pet_scop *scop,
2016 enum pet_skip type)
2018 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2020 if (!scop)
2021 return NULL;
2023 return isl_set_copy(ext->skip[type]);
2026 /* Return a map to the skip condition of the given type.
2028 __isl_give isl_map *pet_scop_get_skip_map(struct pet_scop *scop,
2029 enum pet_skip type)
2031 return isl_map_from_range(pet_scop_get_skip(scop, type));
2034 /* Return an access pet_expr corresponding to the skip condition
2035 * of the given type.
2037 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2038 enum pet_skip type)
2040 return pet_expr_from_access(pet_scop_get_skip_map(scop, type));
2043 /* Drop the the skip condition scop->skip[type].
2045 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2047 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2049 if (!scop)
2050 return;
2052 isl_set_free(ext->skip[type]);
2053 ext->skip[type] = NULL;
2056 /* Make the skip condition (if any) depend on the value of "test" being
2057 * equal to "satisfied".
2059 * We only support the case where the original skip condition is universal,
2060 * i.e., where skipping is unconditional, and where satisfied == 1.
2061 * In this case, the skip condition is changed to skip only when
2062 * "test" is equal to one.
2064 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2065 enum pet_skip type, __isl_keep isl_map *test, int satisfied)
2067 int is_univ = 0;
2069 if (!scop)
2070 return NULL;
2071 if (!pet_scop_has_skip(scop, type))
2072 return scop;
2074 if (satisfied)
2075 is_univ = pet_scop_has_universal_skip(scop, type);
2076 if (is_univ < 0)
2077 return pet_scop_free(scop);
2078 if (satisfied && is_univ) {
2079 scop = pet_scop_set_skip(scop, type,
2080 isl_map_range(isl_map_copy(test)));
2081 if (!scop)
2082 return NULL;
2083 } else {
2084 isl_die(isl_map_get_ctx(test), isl_error_internal,
2085 "skip expression cannot be filtered",
2086 return pet_scop_free(scop));
2089 return scop;
2092 /* Make all statements in "scop" depend on the value of "test"
2093 * being equal to "satisfied" by adjusting their domains.
2095 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2096 __isl_take isl_map *test, int satisfied)
2098 int i;
2100 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2101 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2103 if (!scop || !test)
2104 goto error;
2106 for (i = 0; i < scop->n_stmt; ++i) {
2107 scop->stmts[i] = stmt_filter(scop->stmts[i],
2108 isl_map_copy(test), satisfied);
2109 if (!scop->stmts[i])
2110 goto error;
2113 isl_map_free(test);
2114 return scop;
2115 error:
2116 isl_map_free(test);
2117 return pet_scop_free(scop);
2120 /* Do the filters "i" and "j" always have the same value?
2122 static int equal_filter_values(__isl_keep isl_set *domain, int i, int j)
2124 isl_map *map, *test;
2125 int equal;
2127 map = isl_set_unwrap(isl_set_copy(domain));
2128 test = isl_map_universe(isl_map_get_space(map));
2129 test = isl_map_equate(test, isl_dim_out, i, isl_dim_out, j);
2130 equal = isl_map_is_subset(map, test);
2131 isl_map_free(map);
2132 isl_map_free(test);
2134 return equal;
2137 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2138 * access relation, the union of the two access relations.
2140 static struct pet_stmt *merge_filter_pair(struct pet_stmt *stmt, int i, int j)
2142 int k;
2143 isl_map *map;
2145 if (!stmt)
2146 return NULL;
2148 stmt->args[i]->acc.access = isl_map_union(stmt->args[i]->acc.access,
2149 isl_map_copy(stmt->args[j]->acc.access));
2150 stmt->args[i]->acc.access = isl_map_coalesce(stmt->args[i]->acc.access);
2152 pet_expr_free(stmt->args[j]);
2153 for (k = j; k < stmt->n_arg - 1; ++k)
2154 stmt->args[k] = stmt->args[k + 1];
2155 stmt->n_arg--;
2157 map = isl_set_unwrap(stmt->domain);
2158 map = isl_map_project_out(map, isl_dim_out, j, 1);
2159 stmt->domain = isl_map_wrap(map);
2161 if (!stmt->domain || !stmt->args[i]->acc.access)
2162 return pet_stmt_free(stmt);
2164 return stmt;
2167 /* Look for any pair of filters that access the same filter variable
2168 * and that have the same filter value and merge them into a single
2169 * filter with as filter access relation the union of the filter access
2170 * relations.
2172 static struct pet_stmt *stmt_merge_filters(struct pet_stmt *stmt)
2174 int i, j;
2175 isl_space *space_i, *space_j;
2177 if (!stmt)
2178 return NULL;
2179 if (stmt->n_arg <= 1)
2180 return stmt;
2182 for (i = 0; i < stmt->n_arg - 1; ++i) {
2183 if (stmt->args[i]->type != pet_expr_access)
2184 continue;
2185 if (pet_expr_is_affine(stmt->args[i]))
2186 continue;
2188 space_i = isl_map_get_space(stmt->args[i]->acc.access);
2190 for (j = stmt->n_arg - 1; j > i; --j) {
2191 int eq;
2193 if (stmt->args[j]->type != pet_expr_access)
2194 continue;
2195 if (pet_expr_is_affine(stmt->args[j]))
2196 continue;
2198 space_j = isl_map_get_space(stmt->args[j]->acc.access);
2200 eq = isl_space_is_equal(space_i, space_j);
2201 if (eq >= 0 && eq)
2202 eq = equal_filter_values(stmt->domain, i, j);
2203 if (eq >= 0 && eq)
2204 stmt = merge_filter_pair(stmt, i, j);
2206 isl_space_free(space_j);
2208 if (eq < 0 || !stmt)
2209 break;
2212 isl_space_free(space_i);
2214 if (j > i || !stmt)
2215 return pet_stmt_free(stmt);
2218 return stmt;
2221 /* Look for any pair of filters that access the same filter variable
2222 * and that have the same filter value and merge them into a single
2223 * filter with as filter access relation the union of the filter access
2224 * relations.
2226 struct pet_scop *pet_scop_merge_filters(struct pet_scop *scop)
2228 int i;
2230 if (!scop)
2231 return NULL;
2233 for (i = 0; i < scop->n_stmt; ++i) {
2234 scop->stmts[i] = stmt_merge_filters(scop->stmts[i]);
2235 if (!scop->stmts[i])
2236 return pet_scop_free(scop);
2239 return scop;
2242 /* Add all parameters in "expr" to "dim" and return the result.
2244 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2245 __isl_take isl_space *dim)
2247 int i;
2249 if (!expr)
2250 goto error;
2251 for (i = 0; i < expr->n_arg; ++i)
2253 dim = expr_collect_params(expr->args[i], dim);
2255 if (expr->type == pet_expr_access)
2256 dim = isl_space_align_params(dim,
2257 isl_map_get_space(expr->acc.access));
2259 return dim;
2260 error:
2261 isl_space_free(dim);
2262 return pet_expr_free(expr);
2265 /* Add all parameters in "stmt" to "dim" and return the result.
2267 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2268 __isl_take isl_space *dim)
2270 if (!stmt)
2271 goto error;
2273 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2274 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2275 dim = expr_collect_params(stmt->body, dim);
2277 return dim;
2278 error:
2279 isl_space_free(dim);
2280 return pet_stmt_free(stmt);
2283 /* Add all parameters in "array" to "dim" and return the result.
2285 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2286 __isl_take isl_space *dim)
2288 if (!array)
2289 goto error;
2291 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2292 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2294 return dim;
2295 error:
2296 pet_array_free(array);
2297 return isl_space_free(dim);
2300 /* Add all parameters in "scop" to "dim" and return the result.
2302 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2303 __isl_take isl_space *dim)
2305 int i;
2307 if (!scop)
2308 goto error;
2310 for (i = 0; i < scop->n_array; ++i)
2311 dim = array_collect_params(scop->arrays[i], dim);
2313 for (i = 0; i < scop->n_stmt; ++i)
2314 dim = stmt_collect_params(scop->stmts[i], dim);
2316 return dim;
2317 error:
2318 isl_space_free(dim);
2319 return pet_scop_free(scop);
2322 /* Add all parameters in "dim" to all access relations in "expr".
2324 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2325 __isl_take isl_space *dim)
2327 int i;
2329 if (!expr)
2330 goto error;
2332 for (i = 0; i < expr->n_arg; ++i) {
2333 expr->args[i] =
2334 expr_propagate_params(expr->args[i],
2335 isl_space_copy(dim));
2336 if (!expr->args[i])
2337 goto error;
2340 if (expr->type == pet_expr_access) {
2341 expr->acc.access = isl_map_align_params(expr->acc.access,
2342 isl_space_copy(dim));
2343 if (!expr->acc.access)
2344 goto error;
2347 isl_space_free(dim);
2348 return expr;
2349 error:
2350 isl_space_free(dim);
2351 return pet_expr_free(expr);
2354 /* Add all parameters in "dim" to the domain, schedule and
2355 * all access relations in "stmt".
2357 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2358 __isl_take isl_space *dim)
2360 if (!stmt)
2361 goto error;
2363 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2364 stmt->schedule = isl_map_align_params(stmt->schedule,
2365 isl_space_copy(dim));
2366 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2368 if (!stmt->domain || !stmt->schedule || !stmt->body)
2369 goto error;
2371 isl_space_free(dim);
2372 return stmt;
2373 error:
2374 isl_space_free(dim);
2375 return pet_stmt_free(stmt);
2378 /* Add all parameters in "dim" to "array".
2380 static struct pet_array *array_propagate_params(struct pet_array *array,
2381 __isl_take isl_space *dim)
2383 if (!array)
2384 goto error;
2386 array->context = isl_set_align_params(array->context,
2387 isl_space_copy(dim));
2388 array->extent = isl_set_align_params(array->extent,
2389 isl_space_copy(dim));
2390 if (array->value_bounds) {
2391 array->value_bounds = isl_set_align_params(array->value_bounds,
2392 isl_space_copy(dim));
2393 if (!array->value_bounds)
2394 goto error;
2397 if (!array->context || !array->extent)
2398 goto error;
2400 isl_space_free(dim);
2401 return array;
2402 error:
2403 isl_space_free(dim);
2404 return pet_array_free(array);
2407 /* Add all parameters in "dim" to "scop".
2409 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2410 __isl_take isl_space *dim)
2412 int i;
2414 if (!scop)
2415 goto error;
2417 for (i = 0; i < scop->n_array; ++i) {
2418 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2419 isl_space_copy(dim));
2420 if (!scop->arrays[i])
2421 goto error;
2424 for (i = 0; i < scop->n_stmt; ++i) {
2425 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2426 isl_space_copy(dim));
2427 if (!scop->stmts[i])
2428 goto error;
2431 isl_space_free(dim);
2432 return scop;
2433 error:
2434 isl_space_free(dim);
2435 return pet_scop_free(scop);
2438 /* Update all isl_sets and isl_maps in "scop" such that they all
2439 * have the same parameters.
2441 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2443 isl_space *dim;
2445 if (!scop)
2446 return NULL;
2448 dim = isl_set_get_space(scop->context);
2449 dim = scop_collect_params(scop, dim);
2451 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
2452 scop = scop_propagate_params(scop, dim);
2454 return scop;
2457 /* Check if the given access relation accesses a (0D) array that corresponds
2458 * to one of the parameters in "dim". If so, replace the array access
2459 * by an access to the set of integers with as index (and value)
2460 * that parameter.
2462 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
2463 __isl_take isl_space *dim)
2465 isl_id *array_id = NULL;
2466 int pos = -1;
2468 if (isl_map_has_tuple_id(access, isl_dim_out)) {
2469 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2470 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
2472 isl_space_free(dim);
2474 if (pos < 0) {
2475 isl_id_free(array_id);
2476 return access;
2479 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
2480 if (pos < 0) {
2481 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
2482 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
2483 pos = 0;
2484 } else
2485 isl_id_free(array_id);
2487 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2488 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
2490 return access;
2493 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2494 * in "dim" by a value equal to the corresponding parameter.
2496 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
2497 __isl_take isl_space *dim)
2499 int i;
2501 if (!expr)
2502 goto error;
2504 for (i = 0; i < expr->n_arg; ++i) {
2505 expr->args[i] =
2506 expr_detect_parameter_accesses(expr->args[i],
2507 isl_space_copy(dim));
2508 if (!expr->args[i])
2509 goto error;
2512 if (expr->type == pet_expr_access) {
2513 expr->acc.access = access_detect_parameter(expr->acc.access,
2514 isl_space_copy(dim));
2515 if (!expr->acc.access)
2516 goto error;
2519 isl_space_free(dim);
2520 return expr;
2521 error:
2522 isl_space_free(dim);
2523 return pet_expr_free(expr);
2526 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2527 * in "dim" by a value equal to the corresponding parameter.
2529 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2530 __isl_take isl_space *dim)
2532 if (!stmt)
2533 goto error;
2535 stmt->body = expr_detect_parameter_accesses(stmt->body,
2536 isl_space_copy(dim));
2538 if (!stmt->domain || !stmt->schedule || !stmt->body)
2539 goto error;
2541 isl_space_free(dim);
2542 return stmt;
2543 error:
2544 isl_space_free(dim);
2545 return pet_stmt_free(stmt);
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_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2552 __isl_take isl_space *dim)
2554 int i;
2556 if (!scop)
2557 goto error;
2559 for (i = 0; i < scop->n_stmt; ++i) {
2560 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2561 isl_space_copy(dim));
2562 if (!scop->stmts[i])
2563 goto error;
2566 isl_space_free(dim);
2567 return scop;
2568 error:
2569 isl_space_free(dim);
2570 return pet_scop_free(scop);
2573 /* Replace all accesses to (0D) arrays that correspond to any of
2574 * the parameters used in "scop" by a value equal
2575 * to the corresponding parameter.
2577 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2579 isl_space *dim;
2581 if (!scop)
2582 return NULL;
2584 dim = isl_set_get_space(scop->context);
2585 dim = scop_collect_params(scop, dim);
2587 scop = scop_detect_parameter_accesses(scop, dim);
2589 return scop;
2592 /* Add all read access relations (if "read" is set) and/or all write
2593 * access relations (if "write" is set) to "accesses" and return the result.
2595 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
2596 int read, int write, __isl_take isl_union_map *accesses)
2598 int i;
2599 isl_id *id;
2600 isl_space *dim;
2602 if (!expr)
2603 return NULL;
2605 for (i = 0; i < expr->n_arg; ++i)
2606 accesses = expr_collect_accesses(expr->args[i],
2607 read, write, accesses);
2609 if (expr->type == pet_expr_access &&
2610 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
2611 ((read && expr->acc.read) || (write && expr->acc.write)))
2612 accesses = isl_union_map_add_map(accesses,
2613 isl_map_copy(expr->acc.access));
2615 return accesses;
2618 /* Collect and return all read access relations (if "read" is set)
2619 * and/or all write access relations (if "write" is set) in "stmt".
2621 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2622 int read, int write, __isl_take isl_space *dim)
2624 isl_union_map *accesses;
2626 if (!stmt)
2627 return NULL;
2629 accesses = isl_union_map_empty(dim);
2630 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
2631 accesses = isl_union_map_intersect_domain(accesses,
2632 isl_union_set_from_set(isl_set_copy(stmt->domain)));
2634 return accesses;
2637 /* Collect and return all read access relations (if "read" is set)
2638 * and/or all write access relations (if "write" is set) in "scop".
2640 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2641 int read, int write)
2643 int i;
2644 isl_union_map *accesses;
2646 if (!scop)
2647 return NULL;
2649 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2651 for (i = 0; i < scop->n_stmt; ++i) {
2652 isl_union_map *accesses_i;
2653 isl_space *dim = isl_set_get_space(scop->context);
2654 accesses_i = stmt_collect_accesses(scop->stmts[i],
2655 read, write, dim);
2656 accesses = isl_union_map_union(accesses, accesses_i);
2659 return accesses;
2662 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
2664 return scop_collect_accesses(scop, 1, 0);
2667 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
2669 return scop_collect_accesses(scop, 0, 1);
2672 /* Collect and return the union of iteration domains in "scop".
2674 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2676 int i;
2677 isl_set *domain_i;
2678 isl_union_set *domain;
2680 if (!scop)
2681 return NULL;
2683 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2685 for (i = 0; i < scop->n_stmt; ++i) {
2686 domain_i = isl_set_copy(scop->stmts[i]->domain);
2687 domain = isl_union_set_add_set(domain, domain_i);
2690 return domain;
2693 /* Collect and return the schedules of the statements in "scop".
2694 * The range is normalized to the maximal number of scheduling
2695 * dimensions.
2697 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2699 int i, j;
2700 isl_map *schedule_i;
2701 isl_union_map *schedule;
2702 int depth, max_depth = 0;
2704 if (!scop)
2705 return NULL;
2707 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2709 for (i = 0; i < scop->n_stmt; ++i) {
2710 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2711 if (depth > max_depth)
2712 max_depth = depth;
2715 for (i = 0; i < scop->n_stmt; ++i) {
2716 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2717 depth = isl_map_dim(schedule_i, isl_dim_out);
2718 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2719 max_depth - depth);
2720 for (j = depth; j < max_depth; ++j)
2721 schedule_i = isl_map_fix_si(schedule_i,
2722 isl_dim_out, j, 0);
2723 schedule = isl_union_map_add_map(schedule, schedule_i);
2726 return schedule;
2729 /* Does expression "expr" write to "id"?
2731 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
2733 int i;
2734 isl_id *write_id;
2736 for (i = 0; i < expr->n_arg; ++i) {
2737 int writes = expr_writes(expr->args[i], id);
2738 if (writes < 0 || writes)
2739 return writes;
2742 if (expr->type != pet_expr_access)
2743 return 0;
2744 if (!expr->acc.write)
2745 return 0;
2746 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
2747 return 0;
2749 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
2750 isl_id_free(write_id);
2752 if (!write_id)
2753 return -1;
2755 return write_id == id;
2758 /* Does statement "stmt" write to "id"?
2760 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
2762 return expr_writes(stmt->body, id);
2765 /* Is there any write access in "scop" that accesses "id"?
2767 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
2769 int i;
2771 if (!scop)
2772 return -1;
2774 for (i = 0; i < scop->n_stmt; ++i) {
2775 int writes = stmt_writes(scop->stmts[i], id);
2776 if (writes < 0 || writes)
2777 return writes;
2780 return 0;
2783 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2785 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
2787 int i, n;
2789 n = isl_set_dim(set, isl_dim_param);
2790 for (i = 0; i < n; ++i) {
2791 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2792 const char *name = isl_id_get_name(id);
2793 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
2794 isl_id_free(id);
2797 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
2798 isl_id *id = isl_set_get_tuple_id(set);
2799 const char *name = isl_id_get_name(id);
2800 set = isl_set_set_tuple_name(set, name);
2801 isl_id_free(id);
2804 return set;
2807 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2809 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
2811 int i, n;
2813 n = isl_map_dim(map, isl_dim_param);
2814 for (i = 0; i < n; ++i) {
2815 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2816 const char *name = isl_id_get_name(id);
2817 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2818 isl_id_free(id);
2821 if (isl_map_has_tuple_id(map, isl_dim_in)) {
2822 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
2823 const char *name = isl_id_get_name(id);
2824 map = isl_map_set_tuple_name(map, isl_dim_in, name);
2825 isl_id_free(id);
2828 if (isl_map_has_tuple_id(map, isl_dim_out)) {
2829 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
2830 const char *name = isl_id_get_name(id);
2831 map = isl_map_set_tuple_name(map, isl_dim_out, name);
2832 isl_id_free(id);
2835 return map;
2838 /* Reset the user pointer on all parameter ids in "array".
2840 static struct pet_array *array_anonymize(struct pet_array *array)
2842 if (!array)
2843 return NULL;
2845 array->context = set_anonymize(array->context);
2846 array->extent = set_anonymize(array->extent);
2847 if (!array->context || !array->extent)
2848 return pet_array_free(array);
2850 return array;
2853 /* Reset the user pointer on all parameter and tuple ids in
2854 * the access relation of the access expression "expr".
2856 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
2858 expr->acc.access = map_anonymize(expr->acc.access);
2859 if (!expr->acc.access)
2860 return pet_expr_free(expr);
2862 return expr;
2865 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2867 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2869 int i;
2870 isl_space *space;
2871 isl_set *domain;
2873 if (!stmt)
2874 return NULL;
2876 stmt->domain = set_anonymize(stmt->domain);
2877 stmt->schedule = map_anonymize(stmt->schedule);
2878 if (!stmt->domain || !stmt->schedule)
2879 return pet_stmt_free(stmt);
2881 for (i = 0; i < stmt->n_arg; ++i) {
2882 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2883 &access_anonymize, NULL);
2884 if (!stmt->args[i])
2885 return pet_stmt_free(stmt);
2888 stmt->body = pet_expr_foreach_access(stmt->body,
2889 &access_anonymize, NULL);
2890 if (!stmt->body)
2891 return pet_stmt_free(stmt);
2893 return stmt;
2896 /* Reset the user pointer on all parameter and tuple ids in "scop".
2898 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2900 int i;
2902 if (!scop)
2903 return NULL;
2905 scop->context = set_anonymize(scop->context);
2906 scop->context_value = set_anonymize(scop->context_value);
2907 if (!scop->context || !scop->context_value)
2908 return pet_scop_free(scop);
2910 for (i = 0; i < scop->n_array; ++i) {
2911 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2912 if (!scop->arrays[i])
2913 return pet_scop_free(scop);
2916 for (i = 0; i < scop->n_stmt; ++i) {
2917 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2918 if (!scop->stmts[i])
2919 return pet_scop_free(scop);
2922 return scop;
2925 /* Given a set "domain", return a wrapped relation with the given set
2926 * as domain and a range of dimension "n_arg", where each coordinate
2927 * is either unbounded or, if the corresponding element of args is of
2928 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2930 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2931 unsigned n_arg, struct pet_expr **args,
2932 __isl_keep isl_union_map *value_bounds)
2934 int i;
2935 isl_map *map;
2936 isl_space *space;
2937 isl_ctx *ctx = isl_set_get_ctx(domain);
2939 map = isl_map_from_domain(domain);
2940 space = isl_map_get_space(map);
2941 space = isl_space_add_dims(space, isl_dim_out, 1);
2943 for (i = 0; i < n_arg; ++i) {
2944 isl_map *map_i;
2945 struct pet_expr *arg = args[i];
2946 isl_id *id;
2947 isl_space *space2;
2949 map_i = isl_map_universe(isl_space_copy(space));
2950 if (arg->type == pet_expr_access) {
2951 isl_map *vb;
2952 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2953 space2 = isl_space_alloc(ctx, 0, 0, 1);
2954 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2955 vb = isl_union_map_extract_map(value_bounds, space2);
2956 if (!isl_map_plain_is_empty(vb))
2957 map_i = isl_map_intersect_range(map_i,
2958 isl_map_range(vb));
2959 else
2960 isl_map_free(vb);
2962 map = isl_map_flat_range_product(map, map_i);
2964 isl_space_free(space);
2966 return isl_map_wrap(map);
2969 /* Data used in access_gist() callback.
2971 struct pet_access_gist_data {
2972 isl_set *domain;
2973 isl_union_map *value_bounds;
2976 /* Given an expression "expr" of type pet_expr_access, compute
2977 * the gist of the associated access relation with respect to
2978 * data->domain and the bounds on the values of the arguments
2979 * of the expression.
2981 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
2983 struct pet_access_gist_data *data = user;
2984 isl_set *domain;
2986 domain = isl_set_copy(data->domain);
2987 if (expr->n_arg > 0)
2988 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
2989 data->value_bounds);
2991 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
2992 if (!expr->acc.access)
2993 return pet_expr_free(expr);
2995 return expr;
2998 /* Compute the gist of the iteration domain and all access relations
2999 * of "stmt" based on the constraints on the parameters specified by "context"
3000 * and the constraints on the values of nested accesses specified
3001 * by "value_bounds".
3003 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3004 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3006 int i;
3007 isl_space *space;
3008 isl_set *domain;
3009 struct pet_access_gist_data data;
3011 if (!stmt)
3012 return NULL;
3014 data.domain = isl_set_copy(stmt->domain);
3015 data.value_bounds = value_bounds;
3016 if (stmt->n_arg > 0)
3017 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3019 data.domain = isl_set_intersect_params(data.domain,
3020 isl_set_copy(context));
3022 for (i = 0; i < stmt->n_arg; ++i) {
3023 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
3024 &access_gist, &data);
3025 if (!stmt->args[i])
3026 goto error;
3029 stmt->body = pet_expr_foreach_access(stmt->body, &access_gist, &data);
3030 if (!stmt->body)
3031 goto error;
3033 isl_set_free(data.domain);
3035 space = isl_set_get_space(stmt->domain);
3036 if (isl_space_is_wrapping(space))
3037 space = isl_space_domain(isl_space_unwrap(space));
3038 domain = isl_set_universe(space);
3039 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3040 if (stmt->n_arg > 0)
3041 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3042 value_bounds);
3043 stmt->domain = isl_set_gist(stmt->domain, domain);
3044 if (!stmt->domain)
3045 return pet_stmt_free(stmt);
3047 return stmt;
3048 error:
3049 isl_set_free(data.domain);
3050 return pet_stmt_free(stmt);
3053 /* Compute the gist of the extent of the array
3054 * based on the constraints on the parameters specified by "context".
3056 static struct pet_array *array_gist(struct pet_array *array,
3057 __isl_keep isl_set *context)
3059 if (!array)
3060 return NULL;
3062 array->extent = isl_set_gist_params(array->extent,
3063 isl_set_copy(context));
3064 if (!array->extent)
3065 return pet_array_free(array);
3067 return array;
3070 /* Compute the gist of all sets and relations in "scop"
3071 * based on the constraints on the parameters specified by "scop->context"
3072 * and the constraints on the values of nested accesses specified
3073 * by "value_bounds".
3075 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3076 __isl_keep isl_union_map *value_bounds)
3078 int i;
3080 if (!scop)
3081 return NULL;
3083 scop->context = isl_set_coalesce(scop->context);
3084 if (!scop->context)
3085 return pet_scop_free(scop);
3087 for (i = 0; i < scop->n_array; ++i) {
3088 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3089 if (!scop->arrays[i])
3090 return pet_scop_free(scop);
3093 for (i = 0; i < scop->n_stmt; ++i) {
3094 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3095 value_bounds);
3096 if (!scop->stmts[i])
3097 return pet_scop_free(scop);
3100 return scop;
3103 /* Intersect the context of "scop" with "context".
3104 * To ensure that we don't introduce any unnamed parameters in
3105 * the context of "scop", we first remove the unnamed parameters
3106 * from "context".
3108 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3109 __isl_take isl_set *context)
3111 if (!scop)
3112 goto error;
3114 context = set_project_out_unnamed_params(context);
3115 scop->context = isl_set_intersect(scop->context, context);
3116 if (!scop->context)
3117 return pet_scop_free(scop);
3119 return scop;
3120 error:
3121 isl_set_free(context);
3122 return pet_scop_free(scop);
3125 /* Drop the current context of "scop". That is, replace the context
3126 * by a universal set.
3128 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3130 isl_space *space;
3132 if (!scop)
3133 return NULL;
3135 space = isl_set_get_space(scop->context);
3136 isl_set_free(scop->context);
3137 scop->context = isl_set_universe(space);
3138 if (!scop->context)
3139 return pet_scop_free(scop);
3141 return scop;
3144 /* Append "array" to the arrays of "scop".
3146 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3147 struct pet_array *array)
3149 isl_ctx *ctx;
3150 struct pet_array **arrays;
3152 if (!array || !scop)
3153 goto error;
3155 ctx = isl_set_get_ctx(scop->context);
3156 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3157 scop->n_array + 1);
3158 if (!arrays)
3159 goto error;
3160 scop->arrays = arrays;
3161 scop->arrays[scop->n_array] = array;
3162 scop->n_array++;
3164 return scop;
3165 error:
3166 pet_array_free(array);
3167 return pet_scop_free(scop);