pet_array_embed: improve error handling
[pet.git] / scop.c
blob087deafcc6b21f679f1fddf617ad0dc460a7ddbb
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 access relations in "expr" by calling "fn" on them.
527 struct pet_expr *pet_expr_foreach_access(struct pet_expr *expr,
528 __isl_give isl_map *(*fn)(__isl_take isl_map *access, void *user),
529 void *user)
531 int i;
533 if (!expr)
534 return NULL;
536 for (i = 0; i < expr->n_arg; ++i) {
537 expr->args[i] = pet_expr_foreach_access(expr->args[i], fn, user);
538 if (!expr->args[i])
539 return pet_expr_free(expr);
542 if (expr->type == pet_expr_access) {
543 expr->acc.access = fn(expr->acc.access, user);
544 if (!expr->acc.access)
545 return pet_expr_free(expr);
548 return expr;
551 /* Modify all expressions of type pet_expr_access in "expr"
552 * by calling "fn" on them.
554 struct pet_expr *pet_expr_foreach_access_expr(struct pet_expr *expr,
555 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
556 void *user)
558 int i;
560 if (!expr)
561 return NULL;
563 for (i = 0; i < expr->n_arg; ++i) {
564 expr->args[i] = pet_expr_foreach_access_expr(expr->args[i],
565 fn, user);
566 if (!expr->args[i])
567 return pet_expr_free(expr);
570 if (expr->type == pet_expr_access)
571 expr = fn(expr, user);
573 return expr;
576 /* Modify the given access relation based on the given iteration space
577 * transformation.
578 * If the access has any arguments then the domain of the access relation
579 * is a wrapped mapping from the iteration space to the space of
580 * argument values. We only need to change the domain of this wrapped
581 * mapping, so we extend the input transformation with an identity mapping
582 * on the space of argument values.
584 static __isl_give isl_map *update_domain(__isl_take isl_map *access,
585 void *user)
587 isl_map *update = user;
588 isl_space *dim;
590 update = isl_map_copy(update);
592 dim = isl_map_get_space(access);
593 dim = isl_space_domain(dim);
594 if (!isl_space_is_wrapping(dim))
595 isl_space_free(dim);
596 else {
597 isl_map *id;
598 dim = isl_space_unwrap(dim);
599 dim = isl_space_range(dim);
600 dim = isl_space_map_from_set(dim);
601 id = isl_map_identity(dim);
602 update = isl_map_product(update, id);
605 return isl_map_apply_domain(access, update);
608 /* Modify all access relations in "expr" based on the given iteration space
609 * transformation.
611 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
612 __isl_take isl_map *update)
614 expr = pet_expr_foreach_access(expr, &update_domain, update);
615 isl_map_free(update);
616 return expr;
619 /* Construct a pet_stmt with given line number and statement
620 * number from a pet_expr.
621 * The initial iteration domain is the zero-dimensional universe.
622 * The name of the domain is given by "label" if it is non-NULL.
623 * Otherwise, the name is constructed as S_<id>.
624 * The domains of all access relations are modified to refer
625 * to the statement iteration domain.
627 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
628 __isl_take isl_id *label, int id, struct pet_expr *expr)
630 struct pet_stmt *stmt;
631 isl_space *dim;
632 isl_set *dom;
633 isl_map *sched;
634 isl_map *add_name;
635 char name[50];
637 if (!expr)
638 goto error;
640 stmt = isl_calloc_type(ctx, struct pet_stmt);
641 if (!stmt)
642 goto error;
644 dim = isl_space_set_alloc(ctx, 0, 0);
645 if (label)
646 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
647 else {
648 snprintf(name, sizeof(name), "S_%d", id);
649 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
651 dom = isl_set_universe(isl_space_copy(dim));
652 sched = isl_map_from_domain(isl_set_copy(dom));
654 dim = isl_space_from_range(dim);
655 add_name = isl_map_universe(dim);
656 expr = expr_update_domain(expr, add_name);
658 stmt->line = line;
659 stmt->domain = dom;
660 stmt->schedule = sched;
661 stmt->body = expr;
663 if (!stmt->domain || !stmt->schedule || !stmt->body)
664 return pet_stmt_free(stmt);
666 return stmt;
667 error:
668 isl_id_free(label);
669 return pet_expr_free(expr);
672 void *pet_stmt_free(struct pet_stmt *stmt)
674 int i;
676 if (!stmt)
677 return NULL;
679 isl_set_free(stmt->domain);
680 isl_map_free(stmt->schedule);
681 pet_expr_free(stmt->body);
683 for (i = 0; i < stmt->n_arg; ++i)
684 pet_expr_free(stmt->args[i]);
685 free(stmt->args);
687 free(stmt);
688 return NULL;
691 static void stmt_dump(struct pet_stmt *stmt, int indent)
693 int i;
695 if (!stmt)
696 return;
698 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
699 fprintf(stderr, "%*s", indent, "");
700 isl_set_dump(stmt->domain);
701 fprintf(stderr, "%*s", indent, "");
702 isl_map_dump(stmt->schedule);
703 expr_dump(stmt->body, indent);
704 for (i = 0; i < stmt->n_arg; ++i)
705 expr_dump(stmt->args[i], indent + 2);
708 void pet_stmt_dump(struct pet_stmt *stmt)
710 stmt_dump(stmt, 0);
713 struct pet_array *pet_array_free(struct pet_array *array)
715 if (!array)
716 return NULL;
718 isl_set_free(array->context);
719 isl_set_free(array->extent);
720 isl_set_free(array->value_bounds);
721 free(array->element_type);
723 free(array);
724 return NULL;
727 void pet_array_dump(struct pet_array *array)
729 if (!array)
730 return;
732 isl_set_dump(array->context);
733 isl_set_dump(array->extent);
734 isl_set_dump(array->value_bounds);
735 fprintf(stderr, "%s %s\n", array->element_type,
736 array->live_out ? "live-out" : "");
739 /* Alloc a pet_scop structure, with extra room for information that
740 * is only used during parsing.
742 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
744 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
747 /* Construct a pet_scop with room for n statements.
749 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
751 isl_space *space;
752 struct pet_scop *scop;
754 scop = pet_scop_alloc(ctx);
755 if (!scop)
756 return NULL;
758 space = isl_space_params_alloc(ctx, 0);
759 scop->context = isl_set_universe(isl_space_copy(space));
760 scop->context_value = isl_set_universe(space);
761 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
762 if (!scop->context || !scop->stmts)
763 return pet_scop_free(scop);
765 scop->n_stmt = n;
767 return scop;
770 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
772 return scop_alloc(ctx, 0);
775 /* Update "context" with respect to the valid parameter values for "access".
777 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
778 __isl_take isl_set *context)
780 context = isl_set_intersect(context,
781 isl_map_params(isl_map_copy(access)));
782 return context;
785 /* Update "context" with respect to the valid parameter values for "expr".
787 * If "expr" represents a ternary operator, then a parameter value
788 * needs to be valid for the condition and for at least one of the
789 * remaining two arguments.
790 * If the condition is an affine expression, then we can be a bit more specific.
791 * The parameter then has to be valid for the second argument for
792 * non-zero accesses and valid for the third argument for zero accesses.
794 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
795 __isl_take isl_set *context)
797 int i;
799 if (expr->type == pet_expr_ternary) {
800 int is_aff;
801 isl_set *context1, *context2;
803 is_aff = pet_expr_is_affine(expr->args[0]);
804 if (is_aff < 0)
805 goto error;
807 context = expr_extract_context(expr->args[0], context);
808 context1 = expr_extract_context(expr->args[1],
809 isl_set_copy(context));
810 context2 = expr_extract_context(expr->args[2], context);
812 if (is_aff) {
813 isl_map *access;
814 isl_set *zero_set;
816 access = isl_map_copy(expr->args[0]->acc.access);
817 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
818 zero_set = isl_map_params(access);
819 context1 = isl_set_subtract(context1,
820 isl_set_copy(zero_set));
821 context2 = isl_set_intersect(context2, zero_set);
824 context = isl_set_union(context1, context2);
825 context = isl_set_coalesce(context);
827 return context;
830 for (i = 0; i < expr->n_arg; ++i)
831 context = expr_extract_context(expr->args[i], context);
833 if (expr->type == pet_expr_access)
834 context = access_extract_context(expr->acc.access, context);
836 return context;
837 error:
838 isl_set_free(context);
839 return NULL;
842 /* Update "context" with respect to the valid parameter values for "stmt".
844 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
845 __isl_take isl_set *context)
847 int i;
849 for (i = 0; i < stmt->n_arg; ++i)
850 context = expr_extract_context(stmt->args[i], context);
852 context = expr_extract_context(stmt->body, context);
854 return context;
857 /* Construct a pet_scop that contains the given pet_stmt.
859 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
861 struct pet_scop *scop;
863 if (!stmt)
864 return NULL;
866 scop = scop_alloc(ctx, 1);
867 if (!scop)
868 goto error;
870 scop->context = stmt_extract_context(stmt, scop->context);
871 if (!scop->context)
872 goto error;
874 scop->stmts[0] = stmt;
876 return scop;
877 error:
878 pet_stmt_free(stmt);
879 pet_scop_free(scop);
880 return NULL;
883 /* Does "set" represent an element of an unnamed space, i.e.,
884 * does it represent an affine expression?
886 static int set_is_affine(__isl_keep isl_set *set)
888 int has_id;
890 has_id = isl_set_has_tuple_id(set);
891 if (has_id < 0)
892 return -1;
894 return !has_id;
897 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
898 * ext may be equal to either ext1 or ext2.
900 * The two skips that need to be combined are assumed to be affine expressions.
902 * We need to skip in ext if we need to skip in either ext1 or ext2.
903 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
905 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
906 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
907 enum pet_skip type)
909 isl_set *set, *skip1, *skip2;
911 if (!ext)
912 return NULL;
913 if (!ext1->skip[type] && !ext2->skip[type])
914 return ext;
915 if (!ext1->skip[type]) {
916 if (ext == ext2)
917 return ext;
918 ext->skip[type] = ext2->skip[type];
919 ext2->skip[type] = NULL;
920 return ext;
922 if (!ext2->skip[type]) {
923 if (ext == ext1)
924 return ext;
925 ext->skip[type] = ext1->skip[type];
926 ext1->skip[type] = NULL;
927 return ext;
930 if (!set_is_affine(ext1->skip[type]) ||
931 !set_is_affine(ext2->skip[type]))
932 isl_die(isl_set_get_ctx(ext1->skip[type]), isl_error_internal,
933 "can only combine affine skips",
934 return pet_scop_free(&ext->scop));
936 skip1 = isl_set_copy(ext1->skip[type]);
937 skip2 = isl_set_copy(ext2->skip[type]);
938 set = isl_set_intersect(
939 isl_set_fix_si(isl_set_copy(skip1), isl_dim_set, 0, 0),
940 isl_set_fix_si(isl_set_copy(skip2), isl_dim_set, 0, 0));
941 set = isl_set_union(set, isl_set_fix_si(skip1, isl_dim_set, 0, 1));
942 set = isl_set_union(set, isl_set_fix_si(skip2, isl_dim_set, 0, 1));
943 set = isl_set_coalesce(set);
944 isl_set_free(ext1->skip[type]);
945 ext1->skip[type] = NULL;
946 isl_set_free(ext2->skip[type]);
947 ext2->skip[type] = NULL;
948 ext->skip[type] = set;
949 if (!ext->skip[type])
950 return pet_scop_free(&ext->scop);
952 return ext;
955 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
956 * where type takes on the values pet_skip_now and pet_skip_later.
957 * scop may be equal to either scop1 or scop2.
959 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
960 struct pet_scop *scop1, struct pet_scop *scop2)
962 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
963 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
964 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
966 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
967 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
968 return &ext->scop;
971 /* Update scop->start and scop->end to include the region from "start"
972 * to "end". In particular, if scop->end == 0, then "scop" does not
973 * have any offset information yet and we simply take the information
974 * from "start" and "end". Otherwise, we update the fields if the
975 * region from "start" to "end" is not already included.
977 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
978 unsigned start, unsigned end)
980 if (!scop)
981 return NULL;
982 if (scop->end == 0) {
983 scop->start = start;
984 scop->end = end;
985 } else {
986 if (start < scop->start)
987 scop->start = start;
988 if (end > scop->end)
989 scop->end = end;
992 return scop;
995 /* Combine the offset information of "scop1" and "scop2" into "scop".
997 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
998 struct pet_scop *scop1, struct pet_scop *scop2)
1000 if (scop1->end)
1001 scop = pet_scop_update_start_end(scop,
1002 scop1->start, scop1->end);
1003 if (scop2->end)
1004 scop = pet_scop_update_start_end(scop,
1005 scop2->start, scop2->end);
1006 return scop;
1009 /* Construct a pet_scop that contains the offset information,
1010 * arrays, statements and skip information in "scop1" and "scop2".
1012 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1013 struct pet_scop *scop2)
1015 int i;
1016 struct pet_scop *scop = NULL;
1018 if (!scop1 || !scop2)
1019 goto error;
1021 if (scop1->n_stmt == 0) {
1022 scop2 = scop_combine_skips(scop2, scop1, scop2);
1023 pet_scop_free(scop1);
1024 return scop2;
1027 if (scop2->n_stmt == 0) {
1028 scop1 = scop_combine_skips(scop1, scop1, scop2);
1029 pet_scop_free(scop2);
1030 return scop1;
1033 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1034 if (!scop)
1035 goto error;
1037 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1038 scop1->n_array + scop2->n_array);
1039 if (!scop->arrays)
1040 goto error;
1041 scop->n_array = scop1->n_array + scop2->n_array;
1043 for (i = 0; i < scop1->n_stmt; ++i) {
1044 scop->stmts[i] = scop1->stmts[i];
1045 scop1->stmts[i] = NULL;
1048 for (i = 0; i < scop2->n_stmt; ++i) {
1049 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1050 scop2->stmts[i] = NULL;
1053 for (i = 0; i < scop1->n_array; ++i) {
1054 scop->arrays[i] = scop1->arrays[i];
1055 scop1->arrays[i] = NULL;
1058 for (i = 0; i < scop2->n_array; ++i) {
1059 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1060 scop2->arrays[i] = NULL;
1063 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1064 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1065 scop = scop_combine_skips(scop, scop1, scop2);
1066 scop = scop_combine_start_end(scop, scop1, scop2);
1068 pet_scop_free(scop1);
1069 pet_scop_free(scop2);
1070 return scop;
1071 error:
1072 pet_scop_free(scop1);
1073 pet_scop_free(scop2);
1074 pet_scop_free(scop);
1075 return NULL;
1078 /* Apply the skip condition "skip" to "scop".
1079 * That is, make sure "scop" is not executed when the condition holds.
1081 * If "skip" is an affine expression, we add the conditions under
1082 * which the expression is zero to the iteration domains.
1083 * Otherwise, we add a filter on the variable attaining the value zero.
1085 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1086 __isl_take isl_set *skip)
1088 isl_map *skip_map;
1089 int is_aff;
1091 if (!scop || !skip)
1092 goto error;
1094 is_aff = set_is_affine(skip);
1095 if (is_aff < 0)
1096 goto error;
1098 if (!is_aff)
1099 return pet_scop_filter(scop, isl_map_from_range(skip), 0);
1101 skip = isl_set_fix_si(skip, isl_dim_set, 0, 0);
1102 scop = pet_scop_restrict(scop, isl_set_params(skip));
1104 return scop;
1105 error:
1106 isl_set_free(skip);
1107 return pet_scop_free(scop);
1110 /* Construct a pet_scop that contains the arrays, statements and
1111 * skip information in "scop1" and "scop2", where the two scops
1112 * are executed "in sequence". That is, breaks and continues
1113 * in scop1 have an effect on scop2.
1115 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1116 struct pet_scop *scop2)
1118 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1119 scop2 = restrict_skip(scop2,
1120 pet_scop_get_skip(scop1, pet_skip_now));
1121 return pet_scop_add(ctx, scop1, scop2);
1124 /* Construct a pet_scop that contains the arrays, statements and
1125 * skip information in "scop1" and "scop2", where the two scops
1126 * are executed "in parallel". That is, any break or continue
1127 * in scop1 has no effect on scop2.
1129 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1130 struct pet_scop *scop2)
1132 return pet_scop_add(ctx, scop1, scop2);
1135 void *pet_scop_free(struct pet_scop *scop)
1137 int i;
1138 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1140 if (!scop)
1141 return NULL;
1142 isl_set_free(scop->context);
1143 isl_set_free(scop->context_value);
1144 if (scop->arrays)
1145 for (i = 0; i < scop->n_array; ++i)
1146 pet_array_free(scop->arrays[i]);
1147 free(scop->arrays);
1148 if (scop->stmts)
1149 for (i = 0; i < scop->n_stmt; ++i)
1150 pet_stmt_free(scop->stmts[i]);
1151 free(scop->stmts);
1152 isl_set_free(ext->skip[pet_skip_now]);
1153 isl_set_free(ext->skip[pet_skip_later]);
1154 free(scop);
1155 return NULL;
1158 void pet_scop_dump(struct pet_scop *scop)
1160 int i;
1161 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1163 if (!scop)
1164 return;
1166 isl_set_dump(scop->context);
1167 isl_set_dump(scop->context_value);
1168 for (i = 0; i < scop->n_array; ++i)
1169 pet_array_dump(scop->arrays[i]);
1170 for (i = 0; i < scop->n_stmt; ++i)
1171 pet_stmt_dump(scop->stmts[i]);
1173 if (ext->skip[0]) {
1174 fprintf(stderr, "skip\n");
1175 isl_set_dump(ext->skip[0]);
1176 isl_set_dump(ext->skip[1]);
1180 /* Return 1 if the two pet_arrays are equivalent.
1182 * We don't compare element_size as this may be target dependent.
1184 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1186 if (!array1 || !array2)
1187 return 0;
1189 if (!isl_set_is_equal(array1->context, array2->context))
1190 return 0;
1191 if (!isl_set_is_equal(array1->extent, array2->extent))
1192 return 0;
1193 if (!!array1->value_bounds != !!array2->value_bounds)
1194 return 0;
1195 if (array1->value_bounds &&
1196 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1197 return 0;
1198 if (strcmp(array1->element_type, array2->element_type))
1199 return 0;
1200 if (array1->live_out != array2->live_out)
1201 return 0;
1202 if (array1->uniquely_defined != array2->uniquely_defined)
1203 return 0;
1204 if (array1->declared != array2->declared)
1205 return 0;
1206 if (array1->exposed != array2->exposed)
1207 return 0;
1209 return 1;
1212 /* Return 1 if the two pet_stmts are equivalent.
1214 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1216 int i;
1218 if (!stmt1 || !stmt2)
1219 return 0;
1221 if (stmt1->line != stmt2->line)
1222 return 0;
1223 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1224 return 0;
1225 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1226 return 0;
1227 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1228 return 0;
1229 if (stmt1->n_arg != stmt2->n_arg)
1230 return 0;
1231 for (i = 0; i < stmt1->n_arg; ++i) {
1232 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1233 return 0;
1236 return 1;
1239 /* Return 1 if the two pet_scops are equivalent.
1241 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1243 int i;
1245 if (!scop1 || !scop2)
1246 return 0;
1248 if (!isl_set_is_equal(scop1->context, scop2->context))
1249 return 0;
1250 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1251 return 0;
1253 if (scop1->n_array != scop2->n_array)
1254 return 0;
1255 for (i = 0; i < scop1->n_array; ++i)
1256 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1257 return 0;
1259 if (scop1->n_stmt != scop2->n_stmt)
1260 return 0;
1261 for (i = 0; i < scop1->n_stmt; ++i)
1262 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1263 return 0;
1265 return 1;
1268 /* Prefix the schedule of "stmt" with an extra dimension with constant
1269 * value "pos".
1271 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1273 if (!stmt)
1274 return NULL;
1276 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1277 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1278 if (!stmt->schedule)
1279 return pet_stmt_free(stmt);
1281 return stmt;
1284 /* Prefix the schedules of all statements in "scop" with an extra
1285 * dimension with constant value "pos".
1287 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1289 int i;
1291 if (!scop)
1292 return NULL;
1294 for (i = 0; i < scop->n_stmt; ++i) {
1295 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1296 if (!scop->stmts[i])
1297 return pet_scop_free(scop);
1300 return scop;
1303 /* Given a set with a parameter at "param_pos" that refers to the
1304 * iterator, "move" the iterator to the first set dimension.
1305 * That is, essentially equate the parameter to the first set dimension
1306 * and then project it out.
1308 * The first set dimension may however refer to a virtual iterator,
1309 * while the parameter refers to the "real" iterator.
1310 * We therefore need to take into account the mapping "iv_map", which
1311 * maps the virtual iterator to the real iterator.
1312 * In particular, we equate the set dimension to the input of the map
1313 * and the parameter to the output of the map and then project out
1314 * everything we don't need anymore.
1316 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1317 int param_pos, __isl_take isl_map *iv_map)
1319 isl_map *map;
1320 map = isl_map_from_domain(set);
1321 map = isl_map_add_dims(map, isl_dim_out, 1);
1322 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1323 iv_map = isl_map_align_params(iv_map, isl_map_get_space(map));
1324 map = isl_map_apply_range(map, iv_map);
1325 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1326 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1327 return isl_map_domain(map);
1330 /* Data used in embed_access.
1331 * extend adds an iterator to the iteration domain
1332 * iv_map maps the virtual iterator to the real iterator
1333 * var_id represents the induction variable of the corresponding loop
1335 struct pet_embed_access {
1336 isl_map *extend;
1337 isl_map *iv_map;
1338 isl_id *var_id;
1341 /* Embed the access relation 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 __isl_give isl_map *embed_access(__isl_take isl_map *access,
1358 void *user)
1360 struct pet_embed_access *data = user;
1361 isl_id *array_id = NULL;
1362 int pos;
1364 access = update_domain(access, data->extend);
1366 if (isl_map_has_tuple_id(access, isl_dim_out))
1367 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1368 if (array_id == data->var_id ||
1369 (array_id && !isl_id_get_user(array_id))) {
1370 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1371 access = isl_map_equate(access,
1372 isl_dim_in, 0, isl_dim_out, 0);
1373 if (array_id == data->var_id)
1374 access = isl_map_apply_range(access,
1375 isl_map_copy(data->iv_map));
1376 else
1377 access = isl_map_set_tuple_id(access, isl_dim_out,
1378 isl_id_copy(array_id));
1380 isl_id_free(array_id);
1382 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1383 if (pos >= 0) {
1384 isl_set *set = isl_map_wrap(access);
1385 set = internalize_iv(set, pos, isl_map_copy(data->iv_map));
1386 access = isl_set_unwrap(set);
1388 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1389 isl_id_copy(data->var_id));
1391 return access;
1394 /* Embed all access relations in "expr" in an extra loop.
1395 * "extend" inserts an outer loop iterator in the iteration domains.
1396 * "iv_map" maps the virtual iterator to the real iterator
1397 * "var_id" represents the induction variable.
1399 static struct pet_expr *expr_embed(struct pet_expr *expr,
1400 __isl_take isl_map *extend, __isl_take isl_map *iv_map,
1401 __isl_keep isl_id *var_id)
1403 struct pet_embed_access data =
1404 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1406 expr = pet_expr_foreach_access(expr, &embed_access, &data);
1407 isl_map_free(iv_map);
1408 isl_map_free(extend);
1409 return expr;
1412 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1413 * "dom" and schedule "sched". "var_id" represents the induction variable
1414 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1415 * That is, it maps the iterator used in "dom" and the domain of "sched"
1416 * to the iterator that some of the parameters in "stmt" may refer to.
1418 * The iteration domain and schedule of the statement are updated
1419 * according to the iteration domain and schedule of the new loop.
1420 * If stmt->domain is a wrapped map, then the iteration domain
1421 * is the domain of this map, so we need to be careful to adjust
1422 * this domain.
1424 * If the induction variable appears in the constraints (as a parameter)
1425 * of the current iteration domain or the schedule of the statement,
1426 * then the parameter is equated to the newly introduced iteration
1427 * domain dimension and subsequently projected out.
1429 * Finally, all access relations are updated based on the extra loop.
1431 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1432 __isl_take isl_set *dom, __isl_take isl_map *sched,
1433 __isl_take isl_map *iv_map, __isl_take isl_id *var_id)
1435 int i;
1436 int pos;
1437 isl_id *stmt_id;
1438 isl_space *dim;
1439 isl_map *extend;
1441 if (!stmt)
1442 goto error;
1444 if (isl_set_is_wrapping(stmt->domain)) {
1445 isl_map *map;
1446 isl_map *ext;
1447 isl_space *ran_dim;
1449 map = isl_set_unwrap(stmt->domain);
1450 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1451 ran_dim = isl_space_range(isl_map_get_space(map));
1452 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1453 isl_set_universe(ran_dim));
1454 map = isl_map_flat_domain_product(ext, map);
1455 map = isl_map_set_tuple_id(map, isl_dim_in,
1456 isl_id_copy(stmt_id));
1457 dim = isl_space_domain(isl_map_get_space(map));
1458 stmt->domain = isl_map_wrap(map);
1459 } else {
1460 stmt_id = isl_set_get_tuple_id(stmt->domain);
1461 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1462 stmt->domain);
1463 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1464 isl_id_copy(stmt_id));
1465 dim = isl_set_get_space(stmt->domain);
1468 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1469 if (pos >= 0)
1470 stmt->domain = internalize_iv(stmt->domain, pos,
1471 isl_map_copy(iv_map));
1473 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1474 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1475 isl_dim_in, stmt_id);
1477 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1478 if (pos >= 0) {
1479 isl_set *set = isl_map_wrap(stmt->schedule);
1480 set = internalize_iv(set, pos, isl_map_copy(iv_map));
1481 stmt->schedule = isl_set_unwrap(set);
1484 dim = isl_space_map_from_set(dim);
1485 extend = isl_map_identity(dim);
1486 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1487 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1488 isl_map_get_tuple_id(extend, isl_dim_out));
1489 for (i = 0; i < stmt->n_arg; ++i)
1490 stmt->args[i] = expr_embed(stmt->args[i], isl_map_copy(extend),
1491 isl_map_copy(iv_map), var_id);
1492 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1494 isl_set_free(dom);
1495 isl_id_free(var_id);
1497 for (i = 0; i < stmt->n_arg; ++i)
1498 if (!stmt->args[i])
1499 return pet_stmt_free(stmt);
1500 if (!stmt->domain || !stmt->schedule || !stmt->body)
1501 return pet_stmt_free(stmt);
1502 return stmt;
1503 error:
1504 isl_set_free(dom);
1505 isl_map_free(sched);
1506 isl_map_free(iv_map);
1507 isl_id_free(var_id);
1508 return NULL;
1511 /* Embed the given pet_array in an extra outer loop with iteration domain
1512 * "dom".
1513 * This embedding only has an effect on virtual arrays (those with
1514 * user pointer equal to NULL), which need to be extended along with
1515 * the iteration domain.
1517 static struct pet_array *pet_array_embed(struct pet_array *array,
1518 __isl_take isl_set *dom)
1520 isl_id *array_id = NULL;
1522 if (!array)
1523 goto error;
1525 if (isl_set_has_tuple_id(array->extent))
1526 array_id = isl_set_get_tuple_id(array->extent);
1528 if (array_id && !isl_id_get_user(array_id)) {
1529 array->extent = isl_set_flat_product(dom, array->extent);
1530 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1531 if (!array->extent)
1532 return pet_array_free(array);
1533 } else {
1534 isl_set_free(dom);
1535 isl_id_free(array_id);
1538 return array;
1539 error:
1540 isl_set_free(dom);
1541 return NULL;
1544 /* Project out all unnamed parameters from "set" and return the result.
1546 static __isl_give isl_set *set_project_out_unnamed_params(
1547 __isl_take isl_set *set)
1549 int i, n;
1551 n = isl_set_dim(set, isl_dim_param);
1552 for (i = n - 1; i >= 0; --i) {
1553 if (isl_set_has_dim_name(set, isl_dim_param, i))
1554 continue;
1555 set = isl_set_project_out(set, isl_dim_param, i, 1);
1558 return set;
1561 /* Update the context with respect to an embedding into a loop
1562 * with iteration domain "dom" and induction variable "id".
1563 * "iv_map" maps a possibly virtual iterator (used in "dom")
1564 * to the real iterator (parameter "id").
1566 * If the current context is independent of "id", we don't need
1567 * to do anything.
1568 * Otherwise, a parameter value is invalid for the embedding if
1569 * any of the corresponding iterator values is invalid.
1570 * That is, a parameter value is valid only if all the corresponding
1571 * iterator values are valid.
1572 * We therefore compute the set of parameters
1574 * forall i in dom : valid (i)
1576 * or
1578 * not exists i in dom : not valid(i)
1580 * i.e.,
1582 * not exists i in dom \ valid(i)
1584 * Before we subtract valid(i) from dom, we first need to map
1585 * the real iterator to the virtual iterator.
1587 * If there are any unnamed parameters in "dom", then we consider
1588 * a parameter value to be valid if it is valid for any value of those
1589 * unnamed parameters. They are therefore projected out at the end.
1591 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1592 __isl_keep isl_set *dom, __isl_keep isl_map *iv_map,
1593 __isl_keep isl_id *id)
1595 int pos;
1597 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1598 if (pos < 0)
1599 return context;
1601 context = isl_set_from_params(context);
1602 context = isl_set_add_dims(context, isl_dim_set, 1);
1603 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1604 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1605 context = isl_set_apply(context, isl_map_reverse(isl_map_copy(iv_map)));
1606 context = isl_set_subtract(isl_set_copy(dom), context);
1607 context = isl_set_params(context);
1608 context = isl_set_complement(context);
1609 context = set_project_out_unnamed_params(context);
1610 return context;
1613 /* Embed all statements and arrays in "scop" in an extra outer loop
1614 * with iteration domain "dom" and schedule "sched".
1615 * "id" represents the induction variable of the loop.
1616 * "iv_map" maps a possibly virtual iterator to the real iterator.
1617 * That is, it maps the iterator used in "dom" and the domain of "sched"
1618 * to the iterator that some of the parameters in "scop" may refer to.
1620 * Any skip conditions within the loop have no effect outside of the loop.
1621 * The caller is responsible for making sure skip[pet_skip_later] has been
1622 * taken into account.
1624 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1625 __isl_take isl_map *sched, __isl_take isl_map *iv_map,
1626 __isl_take isl_id *id)
1628 int i;
1630 if (!scop)
1631 goto error;
1633 pet_scop_reset_skip(scop, pet_skip_now);
1634 pet_scop_reset_skip(scop, pet_skip_later);
1636 scop->context = context_embed(scop->context, dom, iv_map, id);
1637 if (!scop->context)
1638 goto error;
1640 for (i = 0; i < scop->n_stmt; ++i) {
1641 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1642 isl_set_copy(dom), isl_map_copy(sched),
1643 isl_map_copy(iv_map), isl_id_copy(id));
1644 if (!scop->stmts[i])
1645 goto error;
1648 for (i = 0; i < scop->n_array; ++i) {
1649 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1650 isl_set_copy(dom));
1651 if (!scop->arrays[i])
1652 goto error;
1655 isl_set_free(dom);
1656 isl_map_free(sched);
1657 isl_map_free(iv_map);
1658 isl_id_free(id);
1659 return scop;
1660 error:
1661 isl_set_free(dom);
1662 isl_map_free(sched);
1663 isl_map_free(iv_map);
1664 isl_id_free(id);
1665 return pet_scop_free(scop);
1668 /* Add extra conditions on the parameters to iteration domain of "stmt".
1670 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1671 __isl_take isl_set *cond)
1673 if (!stmt)
1674 goto error;
1676 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1678 return stmt;
1679 error:
1680 isl_set_free(cond);
1681 return pet_stmt_free(stmt);
1684 /* Add extra conditions to scop->skip[type].
1686 * The new skip condition only holds if it held before
1687 * and the condition is true. It does not hold if it did not hold
1688 * before or the condition is false.
1690 * The skip condition is assumed to be an affine expression.
1692 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1693 enum pet_skip type, __isl_keep isl_set *cond)
1695 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1696 isl_set *skip;
1697 isl_set *set;
1699 if (!scop)
1700 return NULL;
1701 if (!ext->skip[type])
1702 return scop;
1704 if (!set_is_affine(ext->skip[type]))
1705 isl_die(isl_set_get_ctx(ext->skip[type]), isl_error_internal,
1706 "can only resrict affine skips",
1707 return pet_scop_free(scop));
1709 skip = ext->skip[type];
1710 skip = isl_set_intersect_params(skip, isl_set_copy(cond));
1711 set = isl_set_from_params(isl_set_copy(cond));
1712 set = isl_set_complement(set);
1713 set = isl_set_add_dims(set, isl_dim_set, 1);
1714 set = isl_set_fix_si(set, isl_dim_set, 0, 0);
1715 skip = isl_set_union(skip, set);
1716 ext->skip[type] = skip;
1717 if (!ext->skip[type])
1718 return pet_scop_free(scop);
1720 return scop;
1723 /* Add extra conditions on the parameters to all iteration domains
1724 * and skip conditions.
1726 * A parameter value is valid for the result if it was valid
1727 * for the original scop and satisfies "cond" or if it does
1728 * not satisfy "cond" as in this case the scop is not executed
1729 * and the original constraints on the parameters are irrelevant.
1731 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1732 __isl_take isl_set *cond)
1734 int i;
1736 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1737 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1739 if (!scop)
1740 goto error;
1742 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1743 scop->context = isl_set_union(scop->context,
1744 isl_set_complement(isl_set_copy(cond)));
1745 scop->context = isl_set_coalesce(scop->context);
1746 scop->context = set_project_out_unnamed_params(scop->context);
1747 if (!scop->context)
1748 goto error;
1750 for (i = 0; i < scop->n_stmt; ++i) {
1751 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1752 isl_set_copy(cond));
1753 if (!scop->stmts[i])
1754 goto error;
1757 isl_set_free(cond);
1758 return scop;
1759 error:
1760 isl_set_free(cond);
1761 return pet_scop_free(scop);
1764 /* Construct a map that inserts a filter value with name "id" and value
1765 * "satisfied" in the list of filter values embedded in the set space "space".
1767 * If "space" does not contain any filter values yet, we first create
1768 * a map that inserts 0 filter values, i.e.,
1770 * space -> [space -> []]
1772 * We can now assume that space is of the form [dom -> [filters]]
1773 * We construct an identity mapping on dom and a mapping on filters
1774 * that inserts the new filter
1776 * dom -> dom
1777 * [filters] -> [satisfied, filters]
1779 * and then compute the cross product
1781 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1783 static __isl_give isl_map *insert_filter_map(__isl_take isl_space *space,
1784 __isl_take isl_id *id, int satisfied)
1786 isl_space *space2;
1787 isl_map *map, *map_dom, *map_ran;
1788 isl_set *dom;
1790 if (isl_space_is_wrapping(space)) {
1791 space2 = isl_space_map_from_set(isl_space_copy(space));
1792 map = isl_map_identity(space2);
1793 space = isl_space_unwrap(space);
1794 } else {
1795 space = isl_space_from_domain(space);
1796 map = isl_map_universe(isl_space_copy(space));
1797 map = isl_map_reverse(isl_map_domain_map(map));
1800 space2 = isl_space_domain(isl_space_copy(space));
1801 map_dom = isl_map_identity(isl_space_map_from_set(space2));
1802 space = isl_space_range(space);
1803 map_ran = isl_map_identity(isl_space_map_from_set(space));
1804 map_ran = isl_map_insert_dims(map_ran, isl_dim_out, 0, 1);
1805 map_ran = isl_map_set_dim_id(map_ran, isl_dim_out, 0, id);
1806 map_ran = isl_map_fix_si(map_ran, isl_dim_out, 0, satisfied);
1808 map = isl_map_apply_range(map, isl_map_product(map_dom, map_ran));
1810 return map;
1813 /* Insert an argument expression corresponding to "test" in front
1814 * of the list of arguments described by *n_arg and *args.
1816 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
1817 __isl_keep isl_map *test)
1819 int i;
1820 isl_ctx *ctx = isl_map_get_ctx(test);
1822 if (!test)
1823 return -1;
1825 if (!*args) {
1826 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
1827 if (!*args)
1828 return -1;
1829 } else {
1830 struct pet_expr **ext;
1831 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
1832 if (!ext)
1833 return -1;
1834 for (i = 0; i < *n_arg; ++i)
1835 ext[1 + i] = (*args)[i];
1836 free(*args);
1837 *args = ext;
1839 (*n_arg)++;
1840 (*args)[0] = pet_expr_from_access(isl_map_copy(test));
1841 if (!(*args)[0])
1842 return -1;
1844 return 0;
1847 /* Make the expression "expr" depend on the value of "test"
1848 * being equal to "satisfied".
1850 * If "test" is an affine expression, we simply add the conditions
1851 * on the expression have the value "satisfied" to all access relations.
1853 * Otherwise, we add a filter to "expr" (which is then assumed to be
1854 * an access expression) corresponding to "test" being equal to "satisfied".
1856 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
1857 __isl_take isl_map *test, int satisfied)
1859 isl_id *id;
1860 isl_ctx *ctx;
1861 isl_space *space;
1862 isl_map *map;
1864 if (!expr || !test)
1865 goto error;
1867 if (!isl_map_has_tuple_id(test, isl_dim_out)) {
1868 test = isl_map_fix_si(test, isl_dim_out, 0, satisfied);
1869 return pet_expr_restrict(expr, isl_map_params(test));
1872 ctx = isl_map_get_ctx(test);
1873 if (expr->type != pet_expr_access)
1874 isl_die(ctx, isl_error_invalid,
1875 "can only filter access expressions", goto error);
1877 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1878 id = isl_map_get_tuple_id(test, isl_dim_out);
1879 map = insert_filter_map(space, id, satisfied);
1881 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
1882 if (!expr->acc.access)
1883 goto error;
1885 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
1886 goto error;
1888 isl_map_free(test);
1889 return expr;
1890 error:
1891 isl_map_free(test);
1892 return pet_expr_free(expr);
1895 /* Make the statement "stmt" depend on the value of "test"
1896 * being equal to "satisfied" by adjusting stmt->domain.
1898 * The domain of "test" corresponds to the (zero or more) outer dimensions
1899 * of the iteration domain.
1901 * We insert an argument corresponding to a read to "test"
1902 * from the iteration domain of "stmt" in front of the list of arguments.
1903 * We also insert a corresponding output dimension in the wrapped
1904 * map contained in stmt->domain, with value set to "satisfied".
1906 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1907 __isl_take isl_map *test, int satisfied)
1909 int i;
1910 isl_id *id;
1911 isl_ctx *ctx;
1912 isl_map *map, *add_dom;
1913 isl_space *space;
1914 isl_set *dom;
1915 int n_test_dom;
1917 if (!stmt || !test)
1918 goto error;
1920 id = isl_map_get_tuple_id(test, isl_dim_out);
1921 map = insert_filter_map(isl_set_get_space(stmt->domain), id, satisfied);
1922 stmt->domain = isl_set_apply(stmt->domain, map);
1924 space = isl_space_unwrap(isl_set_get_space(stmt->domain));
1925 dom = isl_set_universe(isl_space_domain(space));
1926 n_test_dom = isl_map_dim(test, isl_dim_in);
1927 add_dom = isl_map_from_range(dom);
1928 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
1929 for (i = 0; i < n_test_dom; ++i)
1930 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
1931 isl_dim_out, i);
1932 test = isl_map_apply_domain(test, add_dom);
1934 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1935 goto error;
1937 isl_map_free(test);
1938 return stmt;
1939 error:
1940 isl_map_free(test);
1941 return pet_stmt_free(stmt);
1944 /* Does "scop" have a skip condition of the given "type"?
1946 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1948 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1950 if (!scop)
1951 return -1;
1952 return ext->skip[type] != NULL;
1955 /* Does "scop" have a skip condition of the given "type" that
1956 * is an affine expression?
1958 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1960 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1962 if (!scop)
1963 return -1;
1964 if (!ext->skip[type])
1965 return 0;
1966 return set_is_affine(ext->skip[type]);
1969 /* Does "scop" have a skip condition of the given "type" that
1970 * is not an affine expression?
1972 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1974 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1975 int aff;
1977 if (!scop)
1978 return -1;
1979 if (!ext->skip[type])
1980 return 0;
1981 aff = set_is_affine(ext->skip[type]);
1982 if (aff < 0)
1983 return -1;
1984 return !aff;
1987 /* Does "scop" have a skip condition of the given "type" that
1988 * is affine and holds on the entire domain?
1990 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1992 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1993 isl_set *set;
1994 int is_aff;
1995 int is_univ;
1997 is_aff = pet_scop_has_affine_skip(scop, type);
1998 if (is_aff < 0 || !is_aff)
1999 return is_aff;
2001 set = isl_set_copy(ext->skip[type]);
2002 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
2003 set = isl_set_params(set);
2004 is_univ = isl_set_plain_is_universe(set);
2005 isl_set_free(set);
2007 return is_univ;
2010 /* Replace scop->skip[type] by "skip".
2012 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2013 enum pet_skip type, __isl_take isl_set *skip)
2015 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2017 if (!scop || !skip)
2018 goto error;
2020 isl_set_free(ext->skip[type]);
2021 ext->skip[type] = skip;
2023 return scop;
2024 error:
2025 isl_set_free(skip);
2026 return pet_scop_free(scop);
2029 /* Return a copy of scop->skip[type].
2031 __isl_give isl_set *pet_scop_get_skip(struct pet_scop *scop,
2032 enum pet_skip type)
2034 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2036 if (!scop)
2037 return NULL;
2039 return isl_set_copy(ext->skip[type]);
2042 /* Return a map to the skip condition of the given type.
2044 __isl_give isl_map *pet_scop_get_skip_map(struct pet_scop *scop,
2045 enum pet_skip type)
2047 return isl_map_from_range(pet_scop_get_skip(scop, type));
2050 /* Return an access pet_expr corresponding to the skip condition
2051 * of the given type.
2053 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2054 enum pet_skip type)
2056 return pet_expr_from_access(pet_scop_get_skip_map(scop, type));
2059 /* Drop the the skip condition scop->skip[type].
2061 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2063 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2065 if (!scop)
2066 return;
2068 isl_set_free(ext->skip[type]);
2069 ext->skip[type] = NULL;
2072 /* Make the skip condition (if any) depend on the value of "test" being
2073 * equal to "satisfied".
2075 * We only support the case where the original skip condition is universal,
2076 * i.e., where skipping is unconditional, and where satisfied == 1.
2077 * In this case, the skip condition is changed to skip only when
2078 * "test" is equal to one.
2080 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2081 enum pet_skip type, __isl_keep isl_map *test, int satisfied)
2083 int is_univ = 0;
2085 if (!scop)
2086 return NULL;
2087 if (!pet_scop_has_skip(scop, type))
2088 return scop;
2090 if (satisfied)
2091 is_univ = pet_scop_has_universal_skip(scop, type);
2092 if (is_univ < 0)
2093 return pet_scop_free(scop);
2094 if (satisfied && is_univ) {
2095 scop = pet_scop_set_skip(scop, type,
2096 isl_map_range(isl_map_copy(test)));
2097 if (!scop)
2098 return NULL;
2099 } else {
2100 isl_die(isl_map_get_ctx(test), isl_error_internal,
2101 "skip expression cannot be filtered",
2102 return pet_scop_free(scop));
2105 return scop;
2108 /* Make all statements in "scop" depend on the value of "test"
2109 * being equal to "satisfied" by adjusting their domains.
2111 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2112 __isl_take isl_map *test, int satisfied)
2114 int i;
2116 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2117 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2119 if (!scop || !test)
2120 goto error;
2122 for (i = 0; i < scop->n_stmt; ++i) {
2123 scop->stmts[i] = stmt_filter(scop->stmts[i],
2124 isl_map_copy(test), satisfied);
2125 if (!scop->stmts[i])
2126 goto error;
2129 isl_map_free(test);
2130 return scop;
2131 error:
2132 isl_map_free(test);
2133 return pet_scop_free(scop);
2136 /* Do the filters "i" and "j" always have the same value?
2138 static int equal_filter_values(__isl_keep isl_set *domain, int i, int j)
2140 isl_map *map, *test;
2141 int equal;
2143 map = isl_set_unwrap(isl_set_copy(domain));
2144 test = isl_map_universe(isl_map_get_space(map));
2145 test = isl_map_equate(test, isl_dim_out, i, isl_dim_out, j);
2146 equal = isl_map_is_subset(map, test);
2147 isl_map_free(map);
2148 isl_map_free(test);
2150 return equal;
2153 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2154 * access relation, the union of the two access relations.
2156 static struct pet_stmt *merge_filter_pair(struct pet_stmt *stmt, int i, int j)
2158 int k;
2159 isl_map *map;
2161 if (!stmt)
2162 return NULL;
2164 stmt->args[i]->acc.access = isl_map_union(stmt->args[i]->acc.access,
2165 isl_map_copy(stmt->args[j]->acc.access));
2166 stmt->args[i]->acc.access = isl_map_coalesce(stmt->args[i]->acc.access);
2168 pet_expr_free(stmt->args[j]);
2169 for (k = j; k < stmt->n_arg - 1; ++k)
2170 stmt->args[k] = stmt->args[k + 1];
2171 stmt->n_arg--;
2173 map = isl_set_unwrap(stmt->domain);
2174 map = isl_map_project_out(map, isl_dim_out, j, 1);
2175 stmt->domain = isl_map_wrap(map);
2177 if (!stmt->domain || !stmt->args[i]->acc.access)
2178 return pet_stmt_free(stmt);
2180 return stmt;
2183 /* Look for any pair of filters that access the same filter variable
2184 * and that have the same filter value and merge them into a single
2185 * filter with as filter access relation the union of the filter access
2186 * relations.
2188 static struct pet_stmt *stmt_merge_filters(struct pet_stmt *stmt)
2190 int i, j;
2191 isl_space *space_i, *space_j;
2193 if (!stmt)
2194 return NULL;
2195 if (stmt->n_arg <= 1)
2196 return stmt;
2198 for (i = 0; i < stmt->n_arg - 1; ++i) {
2199 if (stmt->args[i]->type != pet_expr_access)
2200 continue;
2201 if (pet_expr_is_affine(stmt->args[i]))
2202 continue;
2204 space_i = isl_map_get_space(stmt->args[i]->acc.access);
2206 for (j = stmt->n_arg - 1; j > i; --j) {
2207 int eq;
2209 if (stmt->args[j]->type != pet_expr_access)
2210 continue;
2211 if (pet_expr_is_affine(stmt->args[j]))
2212 continue;
2214 space_j = isl_map_get_space(stmt->args[j]->acc.access);
2216 eq = isl_space_is_equal(space_i, space_j);
2217 if (eq >= 0 && eq)
2218 eq = equal_filter_values(stmt->domain, i, j);
2219 if (eq >= 0 && eq)
2220 stmt = merge_filter_pair(stmt, i, j);
2222 isl_space_free(space_j);
2224 if (eq < 0 || !stmt)
2225 break;
2228 isl_space_free(space_i);
2230 if (j > i || !stmt)
2231 return pet_stmt_free(stmt);
2234 return stmt;
2237 /* Look for any pair of filters that access the same filter variable
2238 * and that have the same filter value and merge them into a single
2239 * filter with as filter access relation the union of the filter access
2240 * relations.
2242 struct pet_scop *pet_scop_merge_filters(struct pet_scop *scop)
2244 int i;
2246 if (!scop)
2247 return NULL;
2249 for (i = 0; i < scop->n_stmt; ++i) {
2250 scop->stmts[i] = stmt_merge_filters(scop->stmts[i]);
2251 if (!scop->stmts[i])
2252 return pet_scop_free(scop);
2255 return scop;
2258 /* Add all parameters in "expr" to "dim" and return the result.
2260 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2261 __isl_take isl_space *dim)
2263 int i;
2265 if (!expr)
2266 goto error;
2267 for (i = 0; i < expr->n_arg; ++i)
2269 dim = expr_collect_params(expr->args[i], dim);
2271 if (expr->type == pet_expr_access)
2272 dim = isl_space_align_params(dim,
2273 isl_map_get_space(expr->acc.access));
2275 return dim;
2276 error:
2277 isl_space_free(dim);
2278 return pet_expr_free(expr);
2281 /* Add all parameters in "stmt" to "dim" and return the result.
2283 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2284 __isl_take isl_space *dim)
2286 if (!stmt)
2287 goto error;
2289 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2290 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2291 dim = expr_collect_params(stmt->body, dim);
2293 return dim;
2294 error:
2295 isl_space_free(dim);
2296 return pet_stmt_free(stmt);
2299 /* Add all parameters in "array" to "dim" and return the result.
2301 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2302 __isl_take isl_space *dim)
2304 if (!array)
2305 goto error;
2307 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2308 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2310 return dim;
2311 error:
2312 pet_array_free(array);
2313 return isl_space_free(dim);
2316 /* Add all parameters in "scop" to "dim" and return the result.
2318 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2319 __isl_take isl_space *dim)
2321 int i;
2323 if (!scop)
2324 goto error;
2326 for (i = 0; i < scop->n_array; ++i)
2327 dim = array_collect_params(scop->arrays[i], dim);
2329 for (i = 0; i < scop->n_stmt; ++i)
2330 dim = stmt_collect_params(scop->stmts[i], dim);
2332 return dim;
2333 error:
2334 isl_space_free(dim);
2335 return pet_scop_free(scop);
2338 /* Add all parameters in "dim" to all access relations in "expr".
2340 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2341 __isl_take isl_space *dim)
2343 int i;
2345 if (!expr)
2346 goto error;
2348 for (i = 0; i < expr->n_arg; ++i) {
2349 expr->args[i] =
2350 expr_propagate_params(expr->args[i],
2351 isl_space_copy(dim));
2352 if (!expr->args[i])
2353 goto error;
2356 if (expr->type == pet_expr_access) {
2357 expr->acc.access = isl_map_align_params(expr->acc.access,
2358 isl_space_copy(dim));
2359 if (!expr->acc.access)
2360 goto error;
2363 isl_space_free(dim);
2364 return expr;
2365 error:
2366 isl_space_free(dim);
2367 return pet_expr_free(expr);
2370 /* Add all parameters in "dim" to the domain, schedule and
2371 * all access relations in "stmt".
2373 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2374 __isl_take isl_space *dim)
2376 if (!stmt)
2377 goto error;
2379 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2380 stmt->schedule = isl_map_align_params(stmt->schedule,
2381 isl_space_copy(dim));
2382 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2384 if (!stmt->domain || !stmt->schedule || !stmt->body)
2385 goto error;
2387 isl_space_free(dim);
2388 return stmt;
2389 error:
2390 isl_space_free(dim);
2391 return pet_stmt_free(stmt);
2394 /* Add all parameters in "dim" to "array".
2396 static struct pet_array *array_propagate_params(struct pet_array *array,
2397 __isl_take isl_space *dim)
2399 if (!array)
2400 goto error;
2402 array->context = isl_set_align_params(array->context,
2403 isl_space_copy(dim));
2404 array->extent = isl_set_align_params(array->extent,
2405 isl_space_copy(dim));
2406 if (array->value_bounds) {
2407 array->value_bounds = isl_set_align_params(array->value_bounds,
2408 isl_space_copy(dim));
2409 if (!array->value_bounds)
2410 goto error;
2413 if (!array->context || !array->extent)
2414 goto error;
2416 isl_space_free(dim);
2417 return array;
2418 error:
2419 isl_space_free(dim);
2420 return pet_array_free(array);
2423 /* Add all parameters in "dim" to "scop".
2425 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2426 __isl_take isl_space *dim)
2428 int i;
2430 if (!scop)
2431 goto error;
2433 for (i = 0; i < scop->n_array; ++i) {
2434 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2435 isl_space_copy(dim));
2436 if (!scop->arrays[i])
2437 goto error;
2440 for (i = 0; i < scop->n_stmt; ++i) {
2441 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2442 isl_space_copy(dim));
2443 if (!scop->stmts[i])
2444 goto error;
2447 isl_space_free(dim);
2448 return scop;
2449 error:
2450 isl_space_free(dim);
2451 return pet_scop_free(scop);
2454 /* Update all isl_sets and isl_maps in "scop" such that they all
2455 * have the same parameters.
2457 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2459 isl_space *dim;
2461 if (!scop)
2462 return NULL;
2464 dim = isl_set_get_space(scop->context);
2465 dim = scop_collect_params(scop, dim);
2467 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
2468 scop = scop_propagate_params(scop, dim);
2470 return scop;
2473 /* Check if the given access relation accesses a (0D) array that corresponds
2474 * to one of the parameters in "dim". If so, replace the array access
2475 * by an access to the set of integers with as index (and value)
2476 * that parameter.
2478 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
2479 __isl_take isl_space *dim)
2481 isl_id *array_id = NULL;
2482 int pos = -1;
2484 if (isl_map_has_tuple_id(access, isl_dim_out)) {
2485 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2486 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
2488 isl_space_free(dim);
2490 if (pos < 0) {
2491 isl_id_free(array_id);
2492 return access;
2495 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
2496 if (pos < 0) {
2497 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
2498 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
2499 pos = 0;
2500 } else
2501 isl_id_free(array_id);
2503 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2504 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
2506 return access;
2509 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2510 * in "dim" by a value equal to the corresponding parameter.
2512 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
2513 __isl_take isl_space *dim)
2515 int i;
2517 if (!expr)
2518 goto error;
2520 for (i = 0; i < expr->n_arg; ++i) {
2521 expr->args[i] =
2522 expr_detect_parameter_accesses(expr->args[i],
2523 isl_space_copy(dim));
2524 if (!expr->args[i])
2525 goto error;
2528 if (expr->type == pet_expr_access) {
2529 expr->acc.access = access_detect_parameter(expr->acc.access,
2530 isl_space_copy(dim));
2531 if (!expr->acc.access)
2532 goto error;
2535 isl_space_free(dim);
2536 return expr;
2537 error:
2538 isl_space_free(dim);
2539 return pet_expr_free(expr);
2542 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2543 * in "dim" by a value equal to the corresponding parameter.
2545 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2546 __isl_take isl_space *dim)
2548 if (!stmt)
2549 goto error;
2551 stmt->body = expr_detect_parameter_accesses(stmt->body,
2552 isl_space_copy(dim));
2554 if (!stmt->domain || !stmt->schedule || !stmt->body)
2555 goto error;
2557 isl_space_free(dim);
2558 return stmt;
2559 error:
2560 isl_space_free(dim);
2561 return pet_stmt_free(stmt);
2564 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2565 * in "dim" by a value equal to the corresponding parameter.
2567 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2568 __isl_take isl_space *dim)
2570 int i;
2572 if (!scop)
2573 goto error;
2575 for (i = 0; i < scop->n_stmt; ++i) {
2576 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2577 isl_space_copy(dim));
2578 if (!scop->stmts[i])
2579 goto error;
2582 isl_space_free(dim);
2583 return scop;
2584 error:
2585 isl_space_free(dim);
2586 return pet_scop_free(scop);
2589 /* Replace all accesses to (0D) arrays that correspond to any of
2590 * the parameters used in "scop" by a value equal
2591 * to the corresponding parameter.
2593 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2595 isl_space *dim;
2597 if (!scop)
2598 return NULL;
2600 dim = isl_set_get_space(scop->context);
2601 dim = scop_collect_params(scop, dim);
2603 scop = scop_detect_parameter_accesses(scop, dim);
2605 return scop;
2608 /* Add all read access relations (if "read" is set) and/or all write
2609 * access relations (if "write" is set) to "accesses" and return the result.
2611 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
2612 int read, int write, __isl_take isl_union_map *accesses)
2614 int i;
2615 isl_id *id;
2616 isl_space *dim;
2618 if (!expr)
2619 return NULL;
2621 for (i = 0; i < expr->n_arg; ++i)
2622 accesses = expr_collect_accesses(expr->args[i],
2623 read, write, accesses);
2625 if (expr->type == pet_expr_access &&
2626 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
2627 ((read && expr->acc.read) || (write && expr->acc.write)))
2628 accesses = isl_union_map_add_map(accesses,
2629 isl_map_copy(expr->acc.access));
2631 return accesses;
2634 /* Collect and return all read access relations (if "read" is set)
2635 * and/or all write access relations (if "write" is set) in "stmt".
2637 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2638 int read, int write, __isl_take isl_space *dim)
2640 isl_union_map *accesses;
2642 if (!stmt)
2643 return NULL;
2645 accesses = isl_union_map_empty(dim);
2646 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
2647 accesses = isl_union_map_intersect_domain(accesses,
2648 isl_union_set_from_set(isl_set_copy(stmt->domain)));
2650 return accesses;
2653 /* Collect and return all read access relations (if "read" is set)
2654 * and/or all write access relations (if "write" is set) in "scop".
2656 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2657 int read, int write)
2659 int i;
2660 isl_union_map *accesses;
2662 if (!scop)
2663 return NULL;
2665 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2667 for (i = 0; i < scop->n_stmt; ++i) {
2668 isl_union_map *accesses_i;
2669 isl_space *dim = isl_set_get_space(scop->context);
2670 accesses_i = stmt_collect_accesses(scop->stmts[i],
2671 read, write, dim);
2672 accesses = isl_union_map_union(accesses, accesses_i);
2675 return accesses;
2678 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
2680 return scop_collect_accesses(scop, 1, 0);
2683 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
2685 return scop_collect_accesses(scop, 0, 1);
2688 /* Collect and return the union of iteration domains in "scop".
2690 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2692 int i;
2693 isl_set *domain_i;
2694 isl_union_set *domain;
2696 if (!scop)
2697 return NULL;
2699 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2701 for (i = 0; i < scop->n_stmt; ++i) {
2702 domain_i = isl_set_copy(scop->stmts[i]->domain);
2703 domain = isl_union_set_add_set(domain, domain_i);
2706 return domain;
2709 /* Collect and return the schedules of the statements in "scop".
2710 * The range is normalized to the maximal number of scheduling
2711 * dimensions.
2713 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2715 int i, j;
2716 isl_map *schedule_i;
2717 isl_union_map *schedule;
2718 int depth, max_depth = 0;
2720 if (!scop)
2721 return NULL;
2723 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2725 for (i = 0; i < scop->n_stmt; ++i) {
2726 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2727 if (depth > max_depth)
2728 max_depth = depth;
2731 for (i = 0; i < scop->n_stmt; ++i) {
2732 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2733 depth = isl_map_dim(schedule_i, isl_dim_out);
2734 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2735 max_depth - depth);
2736 for (j = depth; j < max_depth; ++j)
2737 schedule_i = isl_map_fix_si(schedule_i,
2738 isl_dim_out, j, 0);
2739 schedule = isl_union_map_add_map(schedule, schedule_i);
2742 return schedule;
2745 /* Does expression "expr" write to "id"?
2747 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
2749 int i;
2750 isl_id *write_id;
2752 for (i = 0; i < expr->n_arg; ++i) {
2753 int writes = expr_writes(expr->args[i], id);
2754 if (writes < 0 || writes)
2755 return writes;
2758 if (expr->type != pet_expr_access)
2759 return 0;
2760 if (!expr->acc.write)
2761 return 0;
2762 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
2763 return 0;
2765 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
2766 isl_id_free(write_id);
2768 if (!write_id)
2769 return -1;
2771 return write_id == id;
2774 /* Does statement "stmt" write to "id"?
2776 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
2778 return expr_writes(stmt->body, id);
2781 /* Is there any write access in "scop" that accesses "id"?
2783 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
2785 int i;
2787 if (!scop)
2788 return -1;
2790 for (i = 0; i < scop->n_stmt; ++i) {
2791 int writes = stmt_writes(scop->stmts[i], id);
2792 if (writes < 0 || writes)
2793 return writes;
2796 return 0;
2799 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2801 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
2803 int i, n;
2805 n = isl_set_dim(set, isl_dim_param);
2806 for (i = 0; i < n; ++i) {
2807 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2808 const char *name = isl_id_get_name(id);
2809 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
2810 isl_id_free(id);
2813 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
2814 isl_id *id = isl_set_get_tuple_id(set);
2815 const char *name = isl_id_get_name(id);
2816 set = isl_set_set_tuple_name(set, name);
2817 isl_id_free(id);
2820 return set;
2823 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2825 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
2827 int i, n;
2829 n = isl_map_dim(map, isl_dim_param);
2830 for (i = 0; i < n; ++i) {
2831 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2832 const char *name = isl_id_get_name(id);
2833 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2834 isl_id_free(id);
2837 if (isl_map_has_tuple_id(map, isl_dim_in)) {
2838 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
2839 const char *name = isl_id_get_name(id);
2840 map = isl_map_set_tuple_name(map, isl_dim_in, name);
2841 isl_id_free(id);
2844 if (isl_map_has_tuple_id(map, isl_dim_out)) {
2845 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
2846 const char *name = isl_id_get_name(id);
2847 map = isl_map_set_tuple_name(map, isl_dim_out, name);
2848 isl_id_free(id);
2851 return map;
2854 /* Reset the user pointer on all parameter ids in "array".
2856 static struct pet_array *array_anonymize(struct pet_array *array)
2858 if (!array)
2859 return NULL;
2861 array->context = set_anonymize(array->context);
2862 array->extent = set_anonymize(array->extent);
2863 if (!array->context || !array->extent)
2864 return pet_array_free(array);
2866 return array;
2869 /* Reset the user pointer on all parameter and tuple ids in "access".
2871 static __isl_give isl_map *access_anonymize(__isl_take isl_map *access,
2872 void *user)
2874 access = map_anonymize(access);
2876 return access;
2879 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2881 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2883 int i;
2884 isl_space *space;
2885 isl_set *domain;
2887 if (!stmt)
2888 return NULL;
2890 stmt->domain = set_anonymize(stmt->domain);
2891 stmt->schedule = map_anonymize(stmt->schedule);
2892 if (!stmt->domain || !stmt->schedule)
2893 return pet_stmt_free(stmt);
2895 for (i = 0; i < stmt->n_arg; ++i) {
2896 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2897 &access_anonymize, NULL);
2898 if (!stmt->args[i])
2899 return pet_stmt_free(stmt);
2902 stmt->body = pet_expr_foreach_access(stmt->body,
2903 &access_anonymize, NULL);
2904 if (!stmt->body)
2905 return pet_stmt_free(stmt);
2907 return stmt;
2910 /* Reset the user pointer on all parameter and tuple ids in "scop".
2912 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2914 int i;
2916 if (!scop)
2917 return NULL;
2919 scop->context = set_anonymize(scop->context);
2920 scop->context_value = set_anonymize(scop->context_value);
2921 if (!scop->context || !scop->context_value)
2922 return pet_scop_free(scop);
2924 for (i = 0; i < scop->n_array; ++i) {
2925 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2926 if (!scop->arrays[i])
2927 return pet_scop_free(scop);
2930 for (i = 0; i < scop->n_stmt; ++i) {
2931 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2932 if (!scop->stmts[i])
2933 return pet_scop_free(scop);
2936 return scop;
2939 /* Given a set "domain", return a wrapped relation with the given set
2940 * as domain and a range of dimension "n_arg", where each coordinate
2941 * is either unbounded or, if the corresponding element of args is of
2942 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2944 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2945 unsigned n_arg, struct pet_expr **args,
2946 __isl_keep isl_union_map *value_bounds)
2948 int i;
2949 isl_map *map;
2950 isl_space *space;
2951 isl_ctx *ctx = isl_set_get_ctx(domain);
2953 map = isl_map_from_domain(domain);
2954 space = isl_map_get_space(map);
2955 space = isl_space_add_dims(space, isl_dim_out, 1);
2957 for (i = 0; i < n_arg; ++i) {
2958 isl_map *map_i;
2959 struct pet_expr *arg = args[i];
2960 isl_id *id;
2961 isl_space *space2;
2963 map_i = isl_map_universe(isl_space_copy(space));
2964 if (arg->type == pet_expr_access) {
2965 isl_map *vb;
2966 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2967 space2 = isl_space_alloc(ctx, 0, 0, 1);
2968 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2969 vb = isl_union_map_extract_map(value_bounds, space2);
2970 if (!isl_map_plain_is_empty(vb))
2971 map_i = isl_map_intersect_range(map_i,
2972 isl_map_range(vb));
2973 else
2974 isl_map_free(vb);
2976 map = isl_map_flat_range_product(map, map_i);
2978 isl_space_free(space);
2980 return isl_map_wrap(map);
2983 /* Data used in access_gist() callback.
2985 struct pet_access_gist_data {
2986 isl_set *domain;
2987 isl_union_map *value_bounds;
2990 /* Given an expression "expr" of type pet_expr_access, compute
2991 * the gist of the associated access relation with respect to
2992 * data->domain and the bounds on the values of the arguments
2993 * of the expression.
2995 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
2997 struct pet_access_gist_data *data = user;
2998 isl_set *domain;
3000 domain = isl_set_copy(data->domain);
3001 if (expr->n_arg > 0)
3002 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
3003 data->value_bounds);
3005 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
3006 if (!expr->acc.access)
3007 return pet_expr_free(expr);
3009 return expr;
3012 /* Compute the gist of the iteration domain and all access relations
3013 * of "stmt" based on the constraints on the parameters specified by "context"
3014 * and the constraints on the values of nested accesses specified
3015 * by "value_bounds".
3017 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3018 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3020 int i;
3021 isl_space *space;
3022 isl_set *domain;
3023 struct pet_access_gist_data data;
3025 if (!stmt)
3026 return NULL;
3028 data.domain = isl_set_copy(stmt->domain);
3029 data.value_bounds = value_bounds;
3030 if (stmt->n_arg > 0)
3031 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3033 data.domain = isl_set_intersect_params(data.domain,
3034 isl_set_copy(context));
3036 for (i = 0; i < stmt->n_arg; ++i) {
3037 stmt->args[i] = pet_expr_foreach_access_expr(stmt->args[i],
3038 &access_gist, &data);
3039 if (!stmt->args[i])
3040 goto error;
3043 stmt->body = pet_expr_foreach_access_expr(stmt->body,
3044 &access_gist, &data);
3045 if (!stmt->body)
3046 goto error;
3048 isl_set_free(data.domain);
3050 space = isl_set_get_space(stmt->domain);
3051 if (isl_space_is_wrapping(space))
3052 space = isl_space_domain(isl_space_unwrap(space));
3053 domain = isl_set_universe(space);
3054 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3055 if (stmt->n_arg > 0)
3056 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3057 value_bounds);
3058 stmt->domain = isl_set_gist(stmt->domain, domain);
3059 if (!stmt->domain)
3060 return pet_stmt_free(stmt);
3062 return stmt;
3063 error:
3064 isl_set_free(data.domain);
3065 return pet_stmt_free(stmt);
3068 /* Compute the gist of the extent of the array
3069 * based on the constraints on the parameters specified by "context".
3071 static struct pet_array *array_gist(struct pet_array *array,
3072 __isl_keep isl_set *context)
3074 if (!array)
3075 return NULL;
3077 array->extent = isl_set_gist_params(array->extent,
3078 isl_set_copy(context));
3079 if (!array->extent)
3080 return pet_array_free(array);
3082 return array;
3085 /* Compute the gist of all sets and relations in "scop"
3086 * based on the constraints on the parameters specified by "scop->context"
3087 * and the constraints on the values of nested accesses specified
3088 * by "value_bounds".
3090 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3091 __isl_keep isl_union_map *value_bounds)
3093 int i;
3095 if (!scop)
3096 return NULL;
3098 scop->context = isl_set_coalesce(scop->context);
3099 if (!scop->context)
3100 return pet_scop_free(scop);
3102 for (i = 0; i < scop->n_array; ++i) {
3103 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3104 if (!scop->arrays[i])
3105 return pet_scop_free(scop);
3108 for (i = 0; i < scop->n_stmt; ++i) {
3109 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3110 value_bounds);
3111 if (!scop->stmts[i])
3112 return pet_scop_free(scop);
3115 return scop;
3118 /* Intersect the context of "scop" with "context".
3119 * To ensure that we don't introduce any unnamed parameters in
3120 * the context of "scop", we first remove the unnamed parameters
3121 * from "context".
3123 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3124 __isl_take isl_set *context)
3126 if (!scop)
3127 goto error;
3129 context = set_project_out_unnamed_params(context);
3130 scop->context = isl_set_intersect(scop->context, context);
3131 if (!scop->context)
3132 return pet_scop_free(scop);
3134 return scop;
3135 error:
3136 isl_set_free(context);
3137 return pet_scop_free(scop);
3140 /* Drop the current context of "scop". That is, replace the context
3141 * by a universal set.
3143 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3145 isl_space *space;
3147 if (!scop)
3148 return NULL;
3150 space = isl_set_get_space(scop->context);
3151 isl_set_free(scop->context);
3152 scop->context = isl_set_universe(space);
3153 if (!scop->context)
3154 return pet_scop_free(scop);
3156 return scop;
3159 /* Append "array" to the arrays of "scop".
3161 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3162 struct pet_array *array)
3164 isl_ctx *ctx;
3165 struct pet_array **arrays;
3167 if (!array || !scop)
3168 goto error;
3170 ctx = isl_set_get_ctx(scop->context);
3171 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3172 scop->n_array + 1);
3173 if (!arrays)
3174 goto error;
3175 scop->arrays = arrays;
3176 scop->arrays[scop->n_array] = array;
3177 scop->n_array++;
3179 return scop;
3180 error:
3181 pet_array_free(array);
3182 return pet_scop_free(scop);