scan.cc: scop_add_while: take isl_id instead of isl_map
[pet.git] / scop.c
blobf41ba6887a9a892df597555a8f43b69222dc56a6
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_id_free(expr->acc.ref_id);
348 isl_map_free(expr->acc.access);
349 break;
350 case pet_expr_call:
351 free(expr->name);
352 break;
353 case pet_expr_cast:
354 free(expr->type_name);
355 break;
356 case pet_expr_double:
357 free(expr->d.s);
358 break;
359 case pet_expr_unary:
360 case pet_expr_binary:
361 case pet_expr_ternary:
362 break;
365 free(expr);
366 return NULL;
369 static void expr_dump(struct pet_expr *expr, int indent)
371 int i;
373 if (!expr)
374 return;
376 fprintf(stderr, "%*s", indent, "");
378 switch (expr->type) {
379 case pet_expr_double:
380 fprintf(stderr, "%s\n", expr->d.s);
381 break;
382 case pet_expr_access:
383 isl_id_dump(expr->acc.ref_id);
384 fprintf(stderr, "%*s", indent, "");
385 isl_map_dump(expr->acc.access);
386 fprintf(stderr, "%*sread: %d\n", indent + 2,
387 "", expr->acc.read);
388 fprintf(stderr, "%*swrite: %d\n", indent + 2,
389 "", expr->acc.write);
390 for (i = 0; i < expr->n_arg; ++i)
391 expr_dump(expr->args[i], indent + 2);
392 break;
393 case pet_expr_unary:
394 fprintf(stderr, "%s\n", op_str[expr->op]);
395 expr_dump(expr->args[pet_un_arg], indent + 2);
396 break;
397 case pet_expr_binary:
398 fprintf(stderr, "%s\n", op_str[expr->op]);
399 expr_dump(expr->args[pet_bin_lhs], indent + 2);
400 expr_dump(expr->args[pet_bin_rhs], indent + 2);
401 break;
402 case pet_expr_ternary:
403 fprintf(stderr, "?:\n");
404 expr_dump(expr->args[pet_ter_cond], indent + 2);
405 expr_dump(expr->args[pet_ter_true], indent + 2);
406 expr_dump(expr->args[pet_ter_false], indent + 2);
407 break;
408 case pet_expr_call:
409 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
410 for (i = 0; i < expr->n_arg; ++i)
411 expr_dump(expr->args[i], indent + 2);
412 break;
413 case pet_expr_cast:
414 fprintf(stderr, "(%s)\n", expr->type_name);
415 for (i = 0; i < expr->n_arg; ++i)
416 expr_dump(expr->args[i], indent + 2);
417 break;
421 void pet_expr_dump(struct pet_expr *expr)
423 expr_dump(expr, 0);
426 /* Does "expr" represent an access to an unnamed space, i.e.,
427 * does it represent an affine expression?
429 int pet_expr_is_affine(struct pet_expr *expr)
431 int has_id;
433 if (!expr)
434 return -1;
435 if (expr->type != pet_expr_access)
436 return 0;
438 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
439 if (has_id < 0)
440 return -1;
442 return !has_id;
445 /* Return the identifier of the array accessed by "expr".
447 __isl_give isl_id *pet_expr_access_get_id(struct pet_expr *expr)
449 if (!expr)
450 return NULL;
451 if (expr->type != pet_expr_access)
452 return NULL;
453 return isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
456 /* Does "expr" represent an access to a scalar, i.e., zero-dimensional array?
458 int pet_expr_is_scalar_access(struct pet_expr *expr)
460 if (!expr)
461 return -1;
462 if (expr->type != pet_expr_access)
463 return 0;
465 return isl_map_dim(expr->acc.access, isl_dim_out) == 0;
468 /* Return 1 if the two pet_exprs are equivalent.
470 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
472 int i;
474 if (!expr1 || !expr2)
475 return 0;
477 if (expr1->type != expr2->type)
478 return 0;
479 if (expr1->n_arg != expr2->n_arg)
480 return 0;
481 for (i = 0; i < expr1->n_arg; ++i)
482 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
483 return 0;
484 switch (expr1->type) {
485 case pet_expr_double:
486 if (strcmp(expr1->d.s, expr2->d.s))
487 return 0;
488 if (expr1->d.val != expr2->d.val)
489 return 0;
490 break;
491 case pet_expr_access:
492 if (expr1->acc.read != expr2->acc.read)
493 return 0;
494 if (expr1->acc.write != expr2->acc.write)
495 return 0;
496 if (expr1->acc.ref_id != expr2->acc.ref_id)
497 return 0;
498 if (!expr1->acc.access || !expr2->acc.access)
499 return 0;
500 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
501 return 0;
502 break;
503 case pet_expr_unary:
504 case pet_expr_binary:
505 case pet_expr_ternary:
506 if (expr1->op != expr2->op)
507 return 0;
508 break;
509 case pet_expr_call:
510 if (strcmp(expr1->name, expr2->name))
511 return 0;
512 break;
513 case pet_expr_cast:
514 if (strcmp(expr1->type_name, expr2->type_name))
515 return 0;
516 break;
519 return 1;
522 /* Add extra conditions on the parameters to all access relations in "expr".
524 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
525 __isl_take isl_set *cond)
527 int i;
529 if (!expr)
530 goto error;
532 for (i = 0; i < expr->n_arg; ++i) {
533 expr->args[i] = pet_expr_restrict(expr->args[i],
534 isl_set_copy(cond));
535 if (!expr->args[i])
536 goto error;
539 if (expr->type == pet_expr_access) {
540 expr->acc.access = isl_map_intersect_params(expr->acc.access,
541 isl_set_copy(cond));
542 if (!expr->acc.access)
543 goto error;
546 isl_set_free(cond);
547 return expr;
548 error:
549 isl_set_free(cond);
550 return pet_expr_free(expr);
553 /* Modify all expressions of type pet_expr_access in "expr"
554 * by calling "fn" on them.
556 struct pet_expr *pet_expr_map_access(struct pet_expr *expr,
557 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
558 void *user)
560 int i;
562 if (!expr)
563 return NULL;
565 for (i = 0; i < expr->n_arg; ++i) {
566 expr->args[i] = pet_expr_map_access(expr->args[i], fn, user);
567 if (!expr->args[i])
568 return pet_expr_free(expr);
571 if (expr->type == pet_expr_access)
572 expr = fn(expr, user);
574 return expr;
577 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
579 * Return -1 on error (where fn return a negative value is treated as an error).
580 * Otherwise return 0.
582 int pet_expr_foreach_access_expr(struct pet_expr *expr,
583 int (*fn)(struct pet_expr *expr, void *user), void *user)
585 int i;
587 if (!expr)
588 return -1;
590 for (i = 0; i < expr->n_arg; ++i)
591 if (pet_expr_foreach_access_expr(expr->args[i], fn, user) < 0)
592 return -1;
594 if (expr->type == pet_expr_access)
595 return fn(expr, user);
597 return 0;
600 /* Modify the access relation of the given access expression
601 * based on the given iteration space transformation.
602 * If the access has any arguments then the domain of the access relation
603 * is a wrapped mapping from the iteration space to the space of
604 * argument values. We only need to change the domain of this wrapped
605 * mapping, so we extend the input transformation with an identity mapping
606 * on the space of argument values.
608 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
610 isl_map *update = user;
611 isl_space *dim;
613 update = isl_map_copy(update);
615 dim = isl_map_get_space(expr->acc.access);
616 dim = isl_space_domain(dim);
617 if (!isl_space_is_wrapping(dim))
618 isl_space_free(dim);
619 else {
620 isl_map *id;
621 dim = isl_space_unwrap(dim);
622 dim = isl_space_range(dim);
623 dim = isl_space_map_from_set(dim);
624 id = isl_map_identity(dim);
625 update = isl_map_product(update, id);
628 expr->acc.access = isl_map_apply_domain(expr->acc.access, update);
629 if (!expr->acc.access)
630 return pet_expr_free(expr);
632 return expr;
635 /* Modify all access relations in "expr" based on the given iteration space
636 * transformation.
638 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
639 __isl_take isl_map *update)
641 expr = pet_expr_map_access(expr, &update_domain, update);
642 isl_map_free(update);
643 return expr;
646 /* Construct a pet_stmt with given line number and statement
647 * number from a pet_expr.
648 * The initial iteration domain is the zero-dimensional universe.
649 * The name of the domain is given by "label" if it is non-NULL.
650 * Otherwise, the name is constructed as S_<id>.
651 * The domains of all access relations are modified to refer
652 * to the statement iteration domain.
654 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
655 __isl_take isl_id *label, int id, struct pet_expr *expr)
657 struct pet_stmt *stmt;
658 isl_space *dim;
659 isl_set *dom;
660 isl_map *sched;
661 isl_map *add_name;
662 char name[50];
664 if (!expr)
665 goto error;
667 stmt = isl_calloc_type(ctx, struct pet_stmt);
668 if (!stmt)
669 goto error;
671 dim = isl_space_set_alloc(ctx, 0, 0);
672 if (label)
673 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
674 else {
675 snprintf(name, sizeof(name), "S_%d", id);
676 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
678 dom = isl_set_universe(isl_space_copy(dim));
679 sched = isl_map_from_domain(isl_set_copy(dom));
681 dim = isl_space_from_range(dim);
682 add_name = isl_map_universe(dim);
683 expr = expr_update_domain(expr, add_name);
685 stmt->line = line;
686 stmt->domain = dom;
687 stmt->schedule = sched;
688 stmt->body = expr;
690 if (!stmt->domain || !stmt->schedule || !stmt->body)
691 return pet_stmt_free(stmt);
693 return stmt;
694 error:
695 isl_id_free(label);
696 return pet_expr_free(expr);
699 void *pet_stmt_free(struct pet_stmt *stmt)
701 int i;
703 if (!stmt)
704 return NULL;
706 isl_set_free(stmt->domain);
707 isl_map_free(stmt->schedule);
708 pet_expr_free(stmt->body);
710 for (i = 0; i < stmt->n_arg; ++i)
711 pet_expr_free(stmt->args[i]);
712 free(stmt->args);
714 free(stmt);
715 return NULL;
718 static void stmt_dump(struct pet_stmt *stmt, int indent)
720 int i;
722 if (!stmt)
723 return;
725 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
726 fprintf(stderr, "%*s", indent, "");
727 isl_set_dump(stmt->domain);
728 fprintf(stderr, "%*s", indent, "");
729 isl_map_dump(stmt->schedule);
730 expr_dump(stmt->body, indent);
731 for (i = 0; i < stmt->n_arg; ++i)
732 expr_dump(stmt->args[i], indent + 2);
735 void pet_stmt_dump(struct pet_stmt *stmt)
737 stmt_dump(stmt, 0);
740 struct pet_array *pet_array_free(struct pet_array *array)
742 if (!array)
743 return NULL;
745 isl_set_free(array->context);
746 isl_set_free(array->extent);
747 isl_set_free(array->value_bounds);
748 free(array->element_type);
750 free(array);
751 return NULL;
754 void pet_array_dump(struct pet_array *array)
756 if (!array)
757 return;
759 isl_set_dump(array->context);
760 isl_set_dump(array->extent);
761 isl_set_dump(array->value_bounds);
762 fprintf(stderr, "%s %s\n", array->element_type,
763 array->live_out ? "live-out" : "");
766 /* Alloc a pet_scop structure, with extra room for information that
767 * is only used during parsing.
769 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
771 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
774 /* Construct a pet_scop with room for n statements.
776 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
778 isl_space *space;
779 struct pet_scop *scop;
781 scop = pet_scop_alloc(ctx);
782 if (!scop)
783 return NULL;
785 space = isl_space_params_alloc(ctx, 0);
786 scop->context = isl_set_universe(isl_space_copy(space));
787 scop->context_value = isl_set_universe(space);
788 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
789 if (!scop->context || !scop->stmts)
790 return pet_scop_free(scop);
792 scop->n_stmt = n;
794 return scop;
797 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
799 return scop_alloc(ctx, 0);
802 /* Update "context" with respect to the valid parameter values for "access".
804 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
805 __isl_take isl_set *context)
807 context = isl_set_intersect(context,
808 isl_map_params(isl_map_copy(access)));
809 return context;
812 /* Update "context" with respect to the valid parameter values for "expr".
814 * If "expr" represents a ternary operator, then a parameter value
815 * needs to be valid for the condition and for at least one of the
816 * remaining two arguments.
817 * If the condition is an affine expression, then we can be a bit more specific.
818 * The parameter then has to be valid for the second argument for
819 * non-zero accesses and valid for the third argument for zero accesses.
821 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
822 __isl_take isl_set *context)
824 int i;
826 if (expr->type == pet_expr_ternary) {
827 int is_aff;
828 isl_set *context1, *context2;
830 is_aff = pet_expr_is_affine(expr->args[0]);
831 if (is_aff < 0)
832 goto error;
834 context = expr_extract_context(expr->args[0], context);
835 context1 = expr_extract_context(expr->args[1],
836 isl_set_copy(context));
837 context2 = expr_extract_context(expr->args[2], context);
839 if (is_aff) {
840 isl_map *access;
841 isl_set *zero_set;
843 access = isl_map_copy(expr->args[0]->acc.access);
844 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
845 zero_set = isl_map_params(access);
846 context1 = isl_set_subtract(context1,
847 isl_set_copy(zero_set));
848 context2 = isl_set_intersect(context2, zero_set);
851 context = isl_set_union(context1, context2);
852 context = isl_set_coalesce(context);
854 return context;
857 for (i = 0; i < expr->n_arg; ++i)
858 context = expr_extract_context(expr->args[i], context);
860 if (expr->type == pet_expr_access)
861 context = access_extract_context(expr->acc.access, context);
863 return context;
864 error:
865 isl_set_free(context);
866 return NULL;
869 /* Update "context" with respect to the valid parameter values for "stmt".
871 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
872 __isl_take isl_set *context)
874 int i;
876 for (i = 0; i < stmt->n_arg; ++i)
877 context = expr_extract_context(stmt->args[i], context);
879 context = expr_extract_context(stmt->body, context);
881 return context;
884 /* Construct a pet_scop that contains the given pet_stmt.
886 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
888 struct pet_scop *scop;
890 if (!stmt)
891 return NULL;
893 scop = scop_alloc(ctx, 1);
894 if (!scop)
895 goto error;
897 scop->context = stmt_extract_context(stmt, scop->context);
898 if (!scop->context)
899 goto error;
901 scop->stmts[0] = stmt;
903 return scop;
904 error:
905 pet_stmt_free(stmt);
906 pet_scop_free(scop);
907 return NULL;
910 /* Does "set" represent an element of an unnamed space, i.e.,
911 * does it represent an affine expression?
913 static int set_is_affine(__isl_keep isl_set *set)
915 int has_id;
917 has_id = isl_set_has_tuple_id(set);
918 if (has_id < 0)
919 return -1;
921 return !has_id;
924 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
925 * ext may be equal to either ext1 or ext2.
927 * The two skips that need to be combined are assumed to be affine expressions.
929 * We need to skip in ext if we need to skip in either ext1 or ext2.
930 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
932 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
933 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
934 enum pet_skip type)
936 isl_set *set, *skip1, *skip2;
938 if (!ext)
939 return NULL;
940 if (!ext1->skip[type] && !ext2->skip[type])
941 return ext;
942 if (!ext1->skip[type]) {
943 if (ext == ext2)
944 return ext;
945 ext->skip[type] = ext2->skip[type];
946 ext2->skip[type] = NULL;
947 return ext;
949 if (!ext2->skip[type]) {
950 if (ext == ext1)
951 return ext;
952 ext->skip[type] = ext1->skip[type];
953 ext1->skip[type] = NULL;
954 return ext;
957 if (!set_is_affine(ext1->skip[type]) ||
958 !set_is_affine(ext2->skip[type]))
959 isl_die(isl_set_get_ctx(ext1->skip[type]), isl_error_internal,
960 "can only combine affine skips",
961 return pet_scop_free(&ext->scop));
963 skip1 = isl_set_copy(ext1->skip[type]);
964 skip2 = isl_set_copy(ext2->skip[type]);
965 set = isl_set_intersect(
966 isl_set_fix_si(isl_set_copy(skip1), isl_dim_set, 0, 0),
967 isl_set_fix_si(isl_set_copy(skip2), isl_dim_set, 0, 0));
968 set = isl_set_union(set, isl_set_fix_si(skip1, isl_dim_set, 0, 1));
969 set = isl_set_union(set, isl_set_fix_si(skip2, isl_dim_set, 0, 1));
970 set = isl_set_coalesce(set);
971 isl_set_free(ext1->skip[type]);
972 ext1->skip[type] = NULL;
973 isl_set_free(ext2->skip[type]);
974 ext2->skip[type] = NULL;
975 ext->skip[type] = set;
976 if (!ext->skip[type])
977 return pet_scop_free(&ext->scop);
979 return ext;
982 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
983 * where type takes on the values pet_skip_now and pet_skip_later.
984 * scop may be equal to either scop1 or scop2.
986 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
987 struct pet_scop *scop1, struct pet_scop *scop2)
989 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
990 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
991 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
993 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
994 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
995 return &ext->scop;
998 /* Update scop->start and scop->end to include the region from "start"
999 * to "end". In particular, if scop->end == 0, then "scop" does not
1000 * have any offset information yet and we simply take the information
1001 * from "start" and "end". Otherwise, we update the fields if the
1002 * region from "start" to "end" is not already included.
1004 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
1005 unsigned start, unsigned end)
1007 if (!scop)
1008 return NULL;
1009 if (scop->end == 0) {
1010 scop->start = start;
1011 scop->end = end;
1012 } else {
1013 if (start < scop->start)
1014 scop->start = start;
1015 if (end > scop->end)
1016 scop->end = end;
1019 return scop;
1022 /* Combine the offset information of "scop1" and "scop2" into "scop".
1024 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
1025 struct pet_scop *scop1, struct pet_scop *scop2)
1027 if (scop1->end)
1028 scop = pet_scop_update_start_end(scop,
1029 scop1->start, scop1->end);
1030 if (scop2->end)
1031 scop = pet_scop_update_start_end(scop,
1032 scop2->start, scop2->end);
1033 return scop;
1036 /* Construct a pet_scop that contains the offset information,
1037 * arrays, statements and skip information in "scop1" and "scop2".
1039 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1040 struct pet_scop *scop2)
1042 int i;
1043 struct pet_scop *scop = NULL;
1045 if (!scop1 || !scop2)
1046 goto error;
1048 if (scop1->n_stmt == 0) {
1049 scop2 = scop_combine_skips(scop2, scop1, scop2);
1050 pet_scop_free(scop1);
1051 return scop2;
1054 if (scop2->n_stmt == 0) {
1055 scop1 = scop_combine_skips(scop1, scop1, scop2);
1056 pet_scop_free(scop2);
1057 return scop1;
1060 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1061 if (!scop)
1062 goto error;
1064 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1065 scop1->n_array + scop2->n_array);
1066 if (!scop->arrays)
1067 goto error;
1068 scop->n_array = scop1->n_array + scop2->n_array;
1070 for (i = 0; i < scop1->n_stmt; ++i) {
1071 scop->stmts[i] = scop1->stmts[i];
1072 scop1->stmts[i] = NULL;
1075 for (i = 0; i < scop2->n_stmt; ++i) {
1076 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1077 scop2->stmts[i] = NULL;
1080 for (i = 0; i < scop1->n_array; ++i) {
1081 scop->arrays[i] = scop1->arrays[i];
1082 scop1->arrays[i] = NULL;
1085 for (i = 0; i < scop2->n_array; ++i) {
1086 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1087 scop2->arrays[i] = NULL;
1090 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1091 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1092 scop = scop_combine_skips(scop, scop1, scop2);
1093 scop = scop_combine_start_end(scop, scop1, scop2);
1095 pet_scop_free(scop1);
1096 pet_scop_free(scop2);
1097 return scop;
1098 error:
1099 pet_scop_free(scop1);
1100 pet_scop_free(scop2);
1101 pet_scop_free(scop);
1102 return NULL;
1105 /* Apply the skip condition "skip" to "scop".
1106 * That is, make sure "scop" is not executed when the condition holds.
1108 * If "skip" is an affine expression, we add the conditions under
1109 * which the expression is zero to the iteration domains.
1110 * Otherwise, we add a filter on the variable attaining the value zero.
1112 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1113 __isl_take isl_set *skip)
1115 isl_map *skip_map;
1116 int is_aff;
1118 if (!scop || !skip)
1119 goto error;
1121 is_aff = set_is_affine(skip);
1122 if (is_aff < 0)
1123 goto error;
1125 if (!is_aff)
1126 return pet_scop_filter(scop, isl_map_from_range(skip), 0);
1128 skip = isl_set_fix_si(skip, isl_dim_set, 0, 0);
1129 scop = pet_scop_restrict(scop, isl_set_params(skip));
1131 return scop;
1132 error:
1133 isl_set_free(skip);
1134 return pet_scop_free(scop);
1137 /* Construct a pet_scop that contains the arrays, statements and
1138 * skip information in "scop1" and "scop2", where the two scops
1139 * are executed "in sequence". That is, breaks and continues
1140 * in scop1 have an effect on scop2.
1142 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1143 struct pet_scop *scop2)
1145 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1146 scop2 = restrict_skip(scop2,
1147 pet_scop_get_skip(scop1, pet_skip_now));
1148 return pet_scop_add(ctx, scop1, scop2);
1151 /* Construct a pet_scop that contains the arrays, statements and
1152 * skip information in "scop1" and "scop2", where the two scops
1153 * are executed "in parallel". That is, any break or continue
1154 * in scop1 has no effect on scop2.
1156 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1157 struct pet_scop *scop2)
1159 return pet_scop_add(ctx, scop1, scop2);
1162 void *pet_scop_free(struct pet_scop *scop)
1164 int i;
1165 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1167 if (!scop)
1168 return NULL;
1169 isl_set_free(scop->context);
1170 isl_set_free(scop->context_value);
1171 if (scop->arrays)
1172 for (i = 0; i < scop->n_array; ++i)
1173 pet_array_free(scop->arrays[i]);
1174 free(scop->arrays);
1175 if (scop->stmts)
1176 for (i = 0; i < scop->n_stmt; ++i)
1177 pet_stmt_free(scop->stmts[i]);
1178 free(scop->stmts);
1179 isl_set_free(ext->skip[pet_skip_now]);
1180 isl_set_free(ext->skip[pet_skip_later]);
1181 free(scop);
1182 return NULL;
1185 void pet_scop_dump(struct pet_scop *scop)
1187 int i;
1188 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1190 if (!scop)
1191 return;
1193 isl_set_dump(scop->context);
1194 isl_set_dump(scop->context_value);
1195 for (i = 0; i < scop->n_array; ++i)
1196 pet_array_dump(scop->arrays[i]);
1197 for (i = 0; i < scop->n_stmt; ++i)
1198 pet_stmt_dump(scop->stmts[i]);
1200 if (ext->skip[0]) {
1201 fprintf(stderr, "skip\n");
1202 isl_set_dump(ext->skip[0]);
1203 isl_set_dump(ext->skip[1]);
1207 /* Return 1 if the two pet_arrays are equivalent.
1209 * We don't compare element_size as this may be target dependent.
1211 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1213 if (!array1 || !array2)
1214 return 0;
1216 if (!isl_set_is_equal(array1->context, array2->context))
1217 return 0;
1218 if (!isl_set_is_equal(array1->extent, array2->extent))
1219 return 0;
1220 if (!!array1->value_bounds != !!array2->value_bounds)
1221 return 0;
1222 if (array1->value_bounds &&
1223 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1224 return 0;
1225 if (strcmp(array1->element_type, array2->element_type))
1226 return 0;
1227 if (array1->live_out != array2->live_out)
1228 return 0;
1229 if (array1->uniquely_defined != array2->uniquely_defined)
1230 return 0;
1231 if (array1->declared != array2->declared)
1232 return 0;
1233 if (array1->exposed != array2->exposed)
1234 return 0;
1236 return 1;
1239 /* Return 1 if the two pet_stmts are equivalent.
1241 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1243 int i;
1245 if (!stmt1 || !stmt2)
1246 return 0;
1248 if (stmt1->line != stmt2->line)
1249 return 0;
1250 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1251 return 0;
1252 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1253 return 0;
1254 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1255 return 0;
1256 if (stmt1->n_arg != stmt2->n_arg)
1257 return 0;
1258 for (i = 0; i < stmt1->n_arg; ++i) {
1259 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1260 return 0;
1263 return 1;
1266 /* Return 1 if the two pet_scops are equivalent.
1268 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1270 int i;
1272 if (!scop1 || !scop2)
1273 return 0;
1275 if (!isl_set_is_equal(scop1->context, scop2->context))
1276 return 0;
1277 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1278 return 0;
1280 if (scop1->n_array != scop2->n_array)
1281 return 0;
1282 for (i = 0; i < scop1->n_array; ++i)
1283 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1284 return 0;
1286 if (scop1->n_stmt != scop2->n_stmt)
1287 return 0;
1288 for (i = 0; i < scop1->n_stmt; ++i)
1289 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1290 return 0;
1292 return 1;
1295 /* Prefix the schedule of "stmt" with an extra dimension with constant
1296 * value "pos".
1298 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1300 if (!stmt)
1301 return NULL;
1303 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1304 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1305 if (!stmt->schedule)
1306 return pet_stmt_free(stmt);
1308 return stmt;
1311 /* Prefix the schedules of all statements in "scop" with an extra
1312 * dimension with constant value "pos".
1314 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1316 int i;
1318 if (!scop)
1319 return NULL;
1321 for (i = 0; i < scop->n_stmt; ++i) {
1322 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1323 if (!scop->stmts[i])
1324 return pet_scop_free(scop);
1327 return scop;
1330 /* Given a set with a parameter at "param_pos" that refers to the
1331 * iterator, "move" the iterator to the first set dimension.
1332 * That is, essentially equate the parameter to the first set dimension
1333 * and then project it out.
1335 * The first set dimension may however refer to a virtual iterator,
1336 * while the parameter refers to the "real" iterator.
1337 * We therefore need to take into account the mapping "iv_map", which
1338 * maps the virtual iterator to the real iterator.
1339 * In particular, we equate the set dimension to the input of the map
1340 * and the parameter to the output of the map and then project out
1341 * everything we don't need anymore.
1343 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1344 int param_pos, __isl_take isl_map *iv_map)
1346 isl_map *map;
1347 map = isl_map_from_domain(set);
1348 map = isl_map_add_dims(map, isl_dim_out, 1);
1349 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1350 iv_map = isl_map_align_params(iv_map, isl_map_get_space(map));
1351 map = isl_map_apply_range(map, iv_map);
1352 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1353 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1354 return isl_map_domain(map);
1357 /* Data used in embed_access.
1358 * extend adds an iterator to the iteration domain
1359 * iv_map maps the virtual iterator to the real iterator
1360 * var_id represents the induction variable of the corresponding loop
1362 struct pet_embed_access {
1363 isl_map *extend;
1364 isl_map *iv_map;
1365 isl_id *var_id;
1368 /* Given an access expression, embed the associated access relation
1369 * in an extra outer loop.
1371 * We first update the iteration domain to insert the extra dimension.
1373 * If the access refers to the induction variable, then it is
1374 * turned into an access to the set of integers with index (and value)
1375 * equal to the induction variable.
1377 * If the induction variable appears in the constraints (as a parameter),
1378 * then the parameter is equated to the newly introduced iteration
1379 * domain dimension and subsequently projected out.
1381 * Similarly, if the accessed array is a virtual array (with user
1382 * pointer equal to NULL), as created by create_test_access,
1383 * then it is extended along with the domain of the access.
1385 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
1387 struct pet_embed_access *data = user;
1388 isl_map *access;
1389 isl_id *array_id = NULL;
1390 int pos;
1392 expr = update_domain(expr, data->extend);
1393 if (!expr)
1394 return NULL;
1396 access = expr->acc.access;
1398 if (isl_map_has_tuple_id(access, isl_dim_out))
1399 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1400 if (array_id == data->var_id ||
1401 (array_id && !isl_id_get_user(array_id))) {
1402 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1403 access = isl_map_equate(access,
1404 isl_dim_in, 0, isl_dim_out, 0);
1405 if (array_id == data->var_id)
1406 access = isl_map_apply_range(access,
1407 isl_map_copy(data->iv_map));
1408 else
1409 access = isl_map_set_tuple_id(access, isl_dim_out,
1410 isl_id_copy(array_id));
1412 isl_id_free(array_id);
1414 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1415 if (pos >= 0) {
1416 isl_set *set = isl_map_wrap(access);
1417 set = internalize_iv(set, pos, isl_map_copy(data->iv_map));
1418 access = isl_set_unwrap(set);
1420 expr->acc.access = isl_map_set_dim_id(access, isl_dim_in, 0,
1421 isl_id_copy(data->var_id));
1422 if (!expr->acc.access)
1423 return pet_expr_free(expr);
1425 return expr;
1428 /* Embed all access subexpressions of "expr" in an extra loop.
1429 * "extend" inserts an outer loop iterator in the iteration domains.
1430 * "iv_map" maps the virtual iterator to the real iterator
1431 * "var_id" represents the induction variable.
1433 static struct pet_expr *expr_embed(struct pet_expr *expr,
1434 __isl_take isl_map *extend, __isl_take isl_map *iv_map,
1435 __isl_keep isl_id *var_id)
1437 struct pet_embed_access data =
1438 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1440 expr = pet_expr_map_access(expr, &embed_access, &data);
1441 isl_map_free(iv_map);
1442 isl_map_free(extend);
1443 return expr;
1446 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1447 * "dom" and schedule "sched". "var_id" represents the induction variable
1448 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1449 * That is, it maps the iterator used in "dom" and the domain of "sched"
1450 * to the iterator that some of the parameters in "stmt" may refer to.
1452 * The iteration domain and schedule of the statement are updated
1453 * according to the iteration domain and schedule of the new loop.
1454 * If stmt->domain is a wrapped map, then the iteration domain
1455 * is the domain of this map, so we need to be careful to adjust
1456 * this domain.
1458 * If the induction variable appears in the constraints (as a parameter)
1459 * of the current iteration domain or the schedule of the statement,
1460 * then the parameter is equated to the newly introduced iteration
1461 * domain dimension and subsequently projected out.
1463 * Finally, all access relations are updated based on the extra loop.
1465 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1466 __isl_take isl_set *dom, __isl_take isl_map *sched,
1467 __isl_take isl_map *iv_map, __isl_take isl_id *var_id)
1469 int i;
1470 int pos;
1471 isl_id *stmt_id;
1472 isl_space *dim;
1473 isl_map *extend;
1475 if (!stmt)
1476 goto error;
1478 if (isl_set_is_wrapping(stmt->domain)) {
1479 isl_map *map;
1480 isl_map *ext;
1481 isl_space *ran_dim;
1483 map = isl_set_unwrap(stmt->domain);
1484 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1485 ran_dim = isl_space_range(isl_map_get_space(map));
1486 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1487 isl_set_universe(ran_dim));
1488 map = isl_map_flat_domain_product(ext, map);
1489 map = isl_map_set_tuple_id(map, isl_dim_in,
1490 isl_id_copy(stmt_id));
1491 dim = isl_space_domain(isl_map_get_space(map));
1492 stmt->domain = isl_map_wrap(map);
1493 } else {
1494 stmt_id = isl_set_get_tuple_id(stmt->domain);
1495 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1496 stmt->domain);
1497 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1498 isl_id_copy(stmt_id));
1499 dim = isl_set_get_space(stmt->domain);
1502 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1503 if (pos >= 0)
1504 stmt->domain = internalize_iv(stmt->domain, pos,
1505 isl_map_copy(iv_map));
1507 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1508 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1509 isl_dim_in, stmt_id);
1511 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1512 if (pos >= 0) {
1513 isl_set *set = isl_map_wrap(stmt->schedule);
1514 set = internalize_iv(set, pos, isl_map_copy(iv_map));
1515 stmt->schedule = isl_set_unwrap(set);
1518 dim = isl_space_map_from_set(dim);
1519 extend = isl_map_identity(dim);
1520 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1521 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1522 isl_map_get_tuple_id(extend, isl_dim_out));
1523 for (i = 0; i < stmt->n_arg; ++i)
1524 stmt->args[i] = expr_embed(stmt->args[i], isl_map_copy(extend),
1525 isl_map_copy(iv_map), var_id);
1526 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1528 isl_set_free(dom);
1529 isl_id_free(var_id);
1531 for (i = 0; i < stmt->n_arg; ++i)
1532 if (!stmt->args[i])
1533 return pet_stmt_free(stmt);
1534 if (!stmt->domain || !stmt->schedule || !stmt->body)
1535 return pet_stmt_free(stmt);
1536 return stmt;
1537 error:
1538 isl_set_free(dom);
1539 isl_map_free(sched);
1540 isl_map_free(iv_map);
1541 isl_id_free(var_id);
1542 return NULL;
1545 /* Embed the given pet_array in an extra outer loop with iteration domain
1546 * "dom".
1547 * This embedding only has an effect on virtual arrays (those with
1548 * user pointer equal to NULL), which need to be extended along with
1549 * the iteration domain.
1551 static struct pet_array *pet_array_embed(struct pet_array *array,
1552 __isl_take isl_set *dom)
1554 isl_id *array_id = NULL;
1556 if (!array)
1557 goto error;
1559 if (isl_set_has_tuple_id(array->extent))
1560 array_id = isl_set_get_tuple_id(array->extent);
1562 if (array_id && !isl_id_get_user(array_id)) {
1563 array->extent = isl_set_flat_product(dom, array->extent);
1564 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1565 if (!array->extent)
1566 return pet_array_free(array);
1567 } else {
1568 isl_set_free(dom);
1569 isl_id_free(array_id);
1572 return array;
1573 error:
1574 isl_set_free(dom);
1575 return NULL;
1578 /* Project out all unnamed parameters from "set" and return the result.
1580 static __isl_give isl_set *set_project_out_unnamed_params(
1581 __isl_take isl_set *set)
1583 int i, n;
1585 n = isl_set_dim(set, isl_dim_param);
1586 for (i = n - 1; i >= 0; --i) {
1587 if (isl_set_has_dim_name(set, isl_dim_param, i))
1588 continue;
1589 set = isl_set_project_out(set, isl_dim_param, i, 1);
1592 return set;
1595 /* Update the context with respect to an embedding into a loop
1596 * with iteration domain "dom" and induction variable "id".
1597 * "iv_map" maps a possibly virtual iterator (used in "dom")
1598 * to the real iterator (parameter "id").
1600 * If the current context is independent of "id", we don't need
1601 * to do anything.
1602 * Otherwise, a parameter value is invalid for the embedding if
1603 * any of the corresponding iterator values is invalid.
1604 * That is, a parameter value is valid only if all the corresponding
1605 * iterator values are valid.
1606 * We therefore compute the set of parameters
1608 * forall i in dom : valid (i)
1610 * or
1612 * not exists i in dom : not valid(i)
1614 * i.e.,
1616 * not exists i in dom \ valid(i)
1618 * Before we subtract valid(i) from dom, we first need to map
1619 * the real iterator to the virtual iterator.
1621 * If there are any unnamed parameters in "dom", then we consider
1622 * a parameter value to be valid if it is valid for any value of those
1623 * unnamed parameters. They are therefore projected out at the end.
1625 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1626 __isl_keep isl_set *dom, __isl_keep isl_map *iv_map,
1627 __isl_keep isl_id *id)
1629 int pos;
1631 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1632 if (pos < 0)
1633 return context;
1635 context = isl_set_from_params(context);
1636 context = isl_set_add_dims(context, isl_dim_set, 1);
1637 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1638 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1639 context = isl_set_apply(context, isl_map_reverse(isl_map_copy(iv_map)));
1640 context = isl_set_subtract(isl_set_copy(dom), context);
1641 context = isl_set_params(context);
1642 context = isl_set_complement(context);
1643 context = set_project_out_unnamed_params(context);
1644 return context;
1647 /* Embed all statements and arrays in "scop" in an extra outer loop
1648 * with iteration domain "dom" and schedule "sched".
1649 * "id" represents the induction variable of the loop.
1650 * "iv_map" maps a possibly virtual iterator to the real iterator.
1651 * That is, it maps the iterator used in "dom" and the domain of "sched"
1652 * to the iterator that some of the parameters in "scop" may refer to.
1654 * Any skip conditions within the loop have no effect outside of the loop.
1655 * The caller is responsible for making sure skip[pet_skip_later] has been
1656 * taken into account.
1658 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1659 __isl_take isl_map *sched, __isl_take isl_map *iv_map,
1660 __isl_take isl_id *id)
1662 int i;
1664 if (!scop)
1665 goto error;
1667 pet_scop_reset_skip(scop, pet_skip_now);
1668 pet_scop_reset_skip(scop, pet_skip_later);
1670 scop->context = context_embed(scop->context, dom, iv_map, id);
1671 if (!scop->context)
1672 goto error;
1674 for (i = 0; i < scop->n_stmt; ++i) {
1675 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1676 isl_set_copy(dom), isl_map_copy(sched),
1677 isl_map_copy(iv_map), isl_id_copy(id));
1678 if (!scop->stmts[i])
1679 goto error;
1682 for (i = 0; i < scop->n_array; ++i) {
1683 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1684 isl_set_copy(dom));
1685 if (!scop->arrays[i])
1686 goto error;
1689 isl_set_free(dom);
1690 isl_map_free(sched);
1691 isl_map_free(iv_map);
1692 isl_id_free(id);
1693 return scop;
1694 error:
1695 isl_set_free(dom);
1696 isl_map_free(sched);
1697 isl_map_free(iv_map);
1698 isl_id_free(id);
1699 return pet_scop_free(scop);
1702 /* Add extra conditions on the parameters to iteration domain of "stmt".
1704 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1705 __isl_take isl_set *cond)
1707 if (!stmt)
1708 goto error;
1710 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1712 return stmt;
1713 error:
1714 isl_set_free(cond);
1715 return pet_stmt_free(stmt);
1718 /* Add extra conditions to scop->skip[type].
1720 * The new skip condition only holds if it held before
1721 * and the condition is true. It does not hold if it did not hold
1722 * before or the condition is false.
1724 * The skip condition is assumed to be an affine expression.
1726 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1727 enum pet_skip type, __isl_keep isl_set *cond)
1729 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1730 isl_set *skip;
1731 isl_set *set;
1733 if (!scop)
1734 return NULL;
1735 if (!ext->skip[type])
1736 return scop;
1738 if (!set_is_affine(ext->skip[type]))
1739 isl_die(isl_set_get_ctx(ext->skip[type]), isl_error_internal,
1740 "can only resrict affine skips",
1741 return pet_scop_free(scop));
1743 skip = ext->skip[type];
1744 skip = isl_set_intersect_params(skip, isl_set_copy(cond));
1745 set = isl_set_from_params(isl_set_copy(cond));
1746 set = isl_set_complement(set);
1747 set = isl_set_add_dims(set, isl_dim_set, 1);
1748 set = isl_set_fix_si(set, isl_dim_set, 0, 0);
1749 skip = isl_set_union(skip, set);
1750 ext->skip[type] = skip;
1751 if (!ext->skip[type])
1752 return pet_scop_free(scop);
1754 return scop;
1757 /* Add extra conditions on the parameters to all iteration domains
1758 * and skip conditions.
1760 * A parameter value is valid for the result if it was valid
1761 * for the original scop and satisfies "cond" or if it does
1762 * not satisfy "cond" as in this case the scop is not executed
1763 * and the original constraints on the parameters are irrelevant.
1765 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1766 __isl_take isl_set *cond)
1768 int i;
1770 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1771 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1773 if (!scop)
1774 goto error;
1776 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1777 scop->context = isl_set_union(scop->context,
1778 isl_set_complement(isl_set_copy(cond)));
1779 scop->context = isl_set_coalesce(scop->context);
1780 scop->context = set_project_out_unnamed_params(scop->context);
1781 if (!scop->context)
1782 goto error;
1784 for (i = 0; i < scop->n_stmt; ++i) {
1785 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1786 isl_set_copy(cond));
1787 if (!scop->stmts[i])
1788 goto error;
1791 isl_set_free(cond);
1792 return scop;
1793 error:
1794 isl_set_free(cond);
1795 return pet_scop_free(scop);
1798 /* Construct a map that inserts a filter value with name "id" and value
1799 * "satisfied" in the list of filter values embedded in the set space "space".
1801 * If "space" does not contain any filter values yet, we first create
1802 * a map that inserts 0 filter values, i.e.,
1804 * space -> [space -> []]
1806 * We can now assume that space is of the form [dom -> [filters]]
1807 * We construct an identity mapping on dom and a mapping on filters
1808 * that inserts the new filter
1810 * dom -> dom
1811 * [filters] -> [satisfied, filters]
1813 * and then compute the cross product
1815 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1817 static __isl_give isl_map *insert_filter_map(__isl_take isl_space *space,
1818 __isl_take isl_id *id, int satisfied)
1820 isl_space *space2;
1821 isl_map *map, *map_dom, *map_ran;
1822 isl_set *dom;
1824 if (isl_space_is_wrapping(space)) {
1825 space2 = isl_space_map_from_set(isl_space_copy(space));
1826 map = isl_map_identity(space2);
1827 space = isl_space_unwrap(space);
1828 } else {
1829 space = isl_space_from_domain(space);
1830 map = isl_map_universe(isl_space_copy(space));
1831 map = isl_map_reverse(isl_map_domain_map(map));
1834 space2 = isl_space_domain(isl_space_copy(space));
1835 map_dom = isl_map_identity(isl_space_map_from_set(space2));
1836 space = isl_space_range(space);
1837 map_ran = isl_map_identity(isl_space_map_from_set(space));
1838 map_ran = isl_map_insert_dims(map_ran, isl_dim_out, 0, 1);
1839 map_ran = isl_map_set_dim_id(map_ran, isl_dim_out, 0, id);
1840 map_ran = isl_map_fix_si(map_ran, isl_dim_out, 0, satisfied);
1842 map = isl_map_apply_range(map, isl_map_product(map_dom, map_ran));
1844 return map;
1847 /* Insert an argument expression corresponding to "test" in front
1848 * of the list of arguments described by *n_arg and *args.
1850 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
1851 __isl_keep isl_map *test)
1853 int i;
1854 isl_ctx *ctx = isl_map_get_ctx(test);
1856 if (!test)
1857 return -1;
1859 if (!*args) {
1860 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
1861 if (!*args)
1862 return -1;
1863 } else {
1864 struct pet_expr **ext;
1865 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
1866 if (!ext)
1867 return -1;
1868 for (i = 0; i < *n_arg; ++i)
1869 ext[1 + i] = (*args)[i];
1870 free(*args);
1871 *args = ext;
1873 (*n_arg)++;
1874 (*args)[0] = pet_expr_from_access(isl_map_copy(test));
1875 if (!(*args)[0])
1876 return -1;
1878 return 0;
1881 /* Make the expression "expr" depend on the value of "test"
1882 * being equal to "satisfied".
1884 * If "test" is an affine expression, we simply add the conditions
1885 * on the expression have the value "satisfied" to all access relations.
1887 * Otherwise, we add a filter to "expr" (which is then assumed to be
1888 * an access expression) corresponding to "test" being equal to "satisfied".
1890 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
1891 __isl_take isl_map *test, int satisfied)
1893 isl_id *id;
1894 isl_ctx *ctx;
1895 isl_space *space;
1896 isl_map *map;
1898 if (!expr || !test)
1899 goto error;
1901 if (!isl_map_has_tuple_id(test, isl_dim_out)) {
1902 test = isl_map_fix_si(test, isl_dim_out, 0, satisfied);
1903 return pet_expr_restrict(expr, isl_map_params(test));
1906 ctx = isl_map_get_ctx(test);
1907 if (expr->type != pet_expr_access)
1908 isl_die(ctx, isl_error_invalid,
1909 "can only filter access expressions", goto error);
1911 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1912 id = isl_map_get_tuple_id(test, isl_dim_out);
1913 map = insert_filter_map(space, id, satisfied);
1915 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
1916 if (!expr->acc.access)
1917 goto error;
1919 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
1920 goto error;
1922 isl_map_free(test);
1923 return expr;
1924 error:
1925 isl_map_free(test);
1926 return pet_expr_free(expr);
1929 /* Make the statement "stmt" depend on the value of "test"
1930 * being equal to "satisfied" by adjusting stmt->domain.
1932 * The domain of "test" corresponds to the (zero or more) outer dimensions
1933 * of the iteration domain.
1935 * We insert an argument corresponding to a read to "test"
1936 * from the iteration domain of "stmt" in front of the list of arguments.
1937 * We also insert a corresponding output dimension in the wrapped
1938 * map contained in stmt->domain, with value set to "satisfied".
1940 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1941 __isl_take isl_map *test, int satisfied)
1943 int i;
1944 isl_id *id;
1945 isl_ctx *ctx;
1946 isl_map *map, *add_dom;
1947 isl_space *space;
1948 isl_set *dom;
1949 int n_test_dom;
1951 if (!stmt || !test)
1952 goto error;
1954 id = isl_map_get_tuple_id(test, isl_dim_out);
1955 map = insert_filter_map(isl_set_get_space(stmt->domain), id, satisfied);
1956 stmt->domain = isl_set_apply(stmt->domain, map);
1958 space = isl_space_unwrap(isl_set_get_space(stmt->domain));
1959 dom = isl_set_universe(isl_space_domain(space));
1960 n_test_dom = isl_map_dim(test, isl_dim_in);
1961 add_dom = isl_map_from_range(dom);
1962 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
1963 for (i = 0; i < n_test_dom; ++i)
1964 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
1965 isl_dim_out, i);
1966 test = isl_map_apply_domain(test, add_dom);
1968 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1969 goto error;
1971 isl_map_free(test);
1972 return stmt;
1973 error:
1974 isl_map_free(test);
1975 return pet_stmt_free(stmt);
1978 /* Does "scop" have a skip condition of the given "type"?
1980 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1982 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1984 if (!scop)
1985 return -1;
1986 return ext->skip[type] != NULL;
1989 /* Does "scop" have a skip condition of the given "type" that
1990 * is an affine expression?
1992 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1994 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1996 if (!scop)
1997 return -1;
1998 if (!ext->skip[type])
1999 return 0;
2000 return set_is_affine(ext->skip[type]);
2003 /* Does "scop" have a skip condition of the given "type" that
2004 * is not an affine expression?
2006 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2008 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2009 int aff;
2011 if (!scop)
2012 return -1;
2013 if (!ext->skip[type])
2014 return 0;
2015 aff = set_is_affine(ext->skip[type]);
2016 if (aff < 0)
2017 return -1;
2018 return !aff;
2021 /* Does "scop" have a skip condition of the given "type" that
2022 * is affine and holds on the entire domain?
2024 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2026 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2027 isl_set *set;
2028 int is_aff;
2029 int is_univ;
2031 is_aff = pet_scop_has_affine_skip(scop, type);
2032 if (is_aff < 0 || !is_aff)
2033 return is_aff;
2035 set = isl_set_copy(ext->skip[type]);
2036 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
2037 set = isl_set_params(set);
2038 is_univ = isl_set_plain_is_universe(set);
2039 isl_set_free(set);
2041 return is_univ;
2044 /* Replace scop->skip[type] by "skip".
2046 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2047 enum pet_skip type, __isl_take isl_set *skip)
2049 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2051 if (!scop || !skip)
2052 goto error;
2054 isl_set_free(ext->skip[type]);
2055 ext->skip[type] = skip;
2057 return scop;
2058 error:
2059 isl_set_free(skip);
2060 return pet_scop_free(scop);
2063 /* Return a copy of scop->skip[type].
2065 __isl_give isl_set *pet_scop_get_skip(struct pet_scop *scop,
2066 enum pet_skip type)
2068 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2070 if (!scop)
2071 return NULL;
2073 return isl_set_copy(ext->skip[type]);
2076 /* Assuming scop->skip[type] is an affine expression,
2077 * return the constraints on the parameters for which the skip condition
2078 * holds.
2080 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2081 enum pet_skip type)
2083 isl_set *skip;
2085 skip = pet_scop_get_skip(scop, type);
2086 skip = isl_set_fix_si(skip, isl_dim_set, 0, 1);
2087 skip = isl_set_params(skip);
2089 return skip;
2092 /* Return a map to the skip condition of the given type.
2094 __isl_give isl_map *pet_scop_get_skip_map(struct pet_scop *scop,
2095 enum pet_skip type)
2097 return isl_map_from_range(pet_scop_get_skip(scop, type));
2100 /* Return the identifier of the variable that is accessed by
2101 * the skip condition of the given type.
2103 * The skip condition is assumed not to be an affine condition.
2105 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2106 enum pet_skip type)
2108 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2110 if (!scop)
2111 return NULL;
2113 return isl_set_get_tuple_id(ext->skip[type]);
2116 /* Return an access pet_expr corresponding to the skip condition
2117 * of the given type.
2119 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2120 enum pet_skip type)
2122 return pet_expr_from_access(pet_scop_get_skip_map(scop, type));
2125 /* Drop the the skip condition scop->skip[type].
2127 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2129 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2131 if (!scop)
2132 return;
2134 isl_set_free(ext->skip[type]);
2135 ext->skip[type] = NULL;
2138 /* Make the skip condition (if any) depend on the value of "test" being
2139 * equal to "satisfied".
2141 * We only support the case where the original skip condition is universal,
2142 * i.e., where skipping is unconditional, and where satisfied == 1.
2143 * In this case, the skip condition is changed to skip only when
2144 * "test" is equal to one.
2146 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2147 enum pet_skip type, __isl_keep isl_map *test, int satisfied)
2149 int is_univ = 0;
2151 if (!scop)
2152 return NULL;
2153 if (!pet_scop_has_skip(scop, type))
2154 return scop;
2156 if (satisfied)
2157 is_univ = pet_scop_has_universal_skip(scop, type);
2158 if (is_univ < 0)
2159 return pet_scop_free(scop);
2160 if (satisfied && is_univ) {
2161 scop = pet_scop_set_skip(scop, type,
2162 isl_map_range(isl_map_copy(test)));
2163 if (!scop)
2164 return NULL;
2165 } else {
2166 isl_die(isl_map_get_ctx(test), isl_error_internal,
2167 "skip expression cannot be filtered",
2168 return pet_scop_free(scop));
2171 return scop;
2174 /* Make all statements in "scop" depend on the value of "test"
2175 * being equal to "satisfied" by adjusting their domains.
2177 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2178 __isl_take isl_map *test, int satisfied)
2180 int i;
2182 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2183 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2185 if (!scop || !test)
2186 goto error;
2188 for (i = 0; i < scop->n_stmt; ++i) {
2189 scop->stmts[i] = stmt_filter(scop->stmts[i],
2190 isl_map_copy(test), satisfied);
2191 if (!scop->stmts[i])
2192 goto error;
2195 isl_map_free(test);
2196 return scop;
2197 error:
2198 isl_map_free(test);
2199 return pet_scop_free(scop);
2202 /* Do the filters "i" and "j" always have the same value?
2204 static int equal_filter_values(__isl_keep isl_set *domain, int i, int j)
2206 isl_map *map, *test;
2207 int equal;
2209 map = isl_set_unwrap(isl_set_copy(domain));
2210 test = isl_map_universe(isl_map_get_space(map));
2211 test = isl_map_equate(test, isl_dim_out, i, isl_dim_out, j);
2212 equal = isl_map_is_subset(map, test);
2213 isl_map_free(map);
2214 isl_map_free(test);
2216 return equal;
2219 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2220 * access relation, the union of the two access relations.
2222 static struct pet_stmt *merge_filter_pair(struct pet_stmt *stmt, int i, int j)
2224 int k;
2225 isl_map *map;
2227 if (!stmt)
2228 return NULL;
2230 stmt->args[i]->acc.access = isl_map_union(stmt->args[i]->acc.access,
2231 isl_map_copy(stmt->args[j]->acc.access));
2232 stmt->args[i]->acc.access = isl_map_coalesce(stmt->args[i]->acc.access);
2234 pet_expr_free(stmt->args[j]);
2235 for (k = j; k < stmt->n_arg - 1; ++k)
2236 stmt->args[k] = stmt->args[k + 1];
2237 stmt->n_arg--;
2239 map = isl_set_unwrap(stmt->domain);
2240 map = isl_map_project_out(map, isl_dim_out, j, 1);
2241 stmt->domain = isl_map_wrap(map);
2243 if (!stmt->domain || !stmt->args[i]->acc.access)
2244 return pet_stmt_free(stmt);
2246 return stmt;
2249 /* Look for any pair of filters that access the same filter variable
2250 * and that have the same filter value and merge them into a single
2251 * filter with as filter access relation the union of the filter access
2252 * relations.
2254 static struct pet_stmt *stmt_merge_filters(struct pet_stmt *stmt)
2256 int i, j;
2257 isl_space *space_i, *space_j;
2259 if (!stmt)
2260 return NULL;
2261 if (stmt->n_arg <= 1)
2262 return stmt;
2264 for (i = 0; i < stmt->n_arg - 1; ++i) {
2265 if (stmt->args[i]->type != pet_expr_access)
2266 continue;
2267 if (pet_expr_is_affine(stmt->args[i]))
2268 continue;
2270 space_i = isl_map_get_space(stmt->args[i]->acc.access);
2272 for (j = stmt->n_arg - 1; j > i; --j) {
2273 int eq;
2275 if (stmt->args[j]->type != pet_expr_access)
2276 continue;
2277 if (pet_expr_is_affine(stmt->args[j]))
2278 continue;
2280 space_j = isl_map_get_space(stmt->args[j]->acc.access);
2282 eq = isl_space_is_equal(space_i, space_j);
2283 if (eq >= 0 && eq)
2284 eq = equal_filter_values(stmt->domain, i, j);
2285 if (eq >= 0 && eq)
2286 stmt = merge_filter_pair(stmt, i, j);
2288 isl_space_free(space_j);
2290 if (eq < 0 || !stmt)
2291 break;
2294 isl_space_free(space_i);
2296 if (j > i || !stmt)
2297 return pet_stmt_free(stmt);
2300 return stmt;
2303 /* Look for any pair of filters that access the same filter variable
2304 * and that have the same filter value and merge them into a single
2305 * filter with as filter access relation the union of the filter access
2306 * relations.
2308 struct pet_scop *pet_scop_merge_filters(struct pet_scop *scop)
2310 int i;
2312 if (!scop)
2313 return NULL;
2315 for (i = 0; i < scop->n_stmt; ++i) {
2316 scop->stmts[i] = stmt_merge_filters(scop->stmts[i]);
2317 if (!scop->stmts[i])
2318 return pet_scop_free(scop);
2321 return scop;
2324 /* Add all parameters in "expr" to "dim" and return the result.
2326 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2327 __isl_take isl_space *dim)
2329 int i;
2331 if (!expr)
2332 goto error;
2333 for (i = 0; i < expr->n_arg; ++i)
2335 dim = expr_collect_params(expr->args[i], dim);
2337 if (expr->type == pet_expr_access)
2338 dim = isl_space_align_params(dim,
2339 isl_map_get_space(expr->acc.access));
2341 return dim;
2342 error:
2343 isl_space_free(dim);
2344 return pet_expr_free(expr);
2347 /* Add all parameters in "stmt" to "dim" and return the result.
2349 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2350 __isl_take isl_space *dim)
2352 if (!stmt)
2353 goto error;
2355 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2356 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2357 dim = expr_collect_params(stmt->body, dim);
2359 return dim;
2360 error:
2361 isl_space_free(dim);
2362 return pet_stmt_free(stmt);
2365 /* Add all parameters in "array" to "dim" and return the result.
2367 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2368 __isl_take isl_space *dim)
2370 if (!array)
2371 goto error;
2373 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2374 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2376 return dim;
2377 error:
2378 pet_array_free(array);
2379 return isl_space_free(dim);
2382 /* Add all parameters in "scop" to "dim" and return the result.
2384 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2385 __isl_take isl_space *dim)
2387 int i;
2389 if (!scop)
2390 goto error;
2392 for (i = 0; i < scop->n_array; ++i)
2393 dim = array_collect_params(scop->arrays[i], dim);
2395 for (i = 0; i < scop->n_stmt; ++i)
2396 dim = stmt_collect_params(scop->stmts[i], dim);
2398 return dim;
2399 error:
2400 isl_space_free(dim);
2401 return pet_scop_free(scop);
2404 /* Add all parameters in "dim" to all access relations in "expr".
2406 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2407 __isl_take isl_space *dim)
2409 int i;
2411 if (!expr)
2412 goto error;
2414 for (i = 0; i < expr->n_arg; ++i) {
2415 expr->args[i] =
2416 expr_propagate_params(expr->args[i],
2417 isl_space_copy(dim));
2418 if (!expr->args[i])
2419 goto error;
2422 if (expr->type == pet_expr_access) {
2423 expr->acc.access = isl_map_align_params(expr->acc.access,
2424 isl_space_copy(dim));
2425 if (!expr->acc.access)
2426 goto error;
2429 isl_space_free(dim);
2430 return expr;
2431 error:
2432 isl_space_free(dim);
2433 return pet_expr_free(expr);
2436 /* Add all parameters in "dim" to the domain, schedule and
2437 * all access relations in "stmt".
2439 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2440 __isl_take isl_space *dim)
2442 if (!stmt)
2443 goto error;
2445 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2446 stmt->schedule = isl_map_align_params(stmt->schedule,
2447 isl_space_copy(dim));
2448 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2450 if (!stmt->domain || !stmt->schedule || !stmt->body)
2451 goto error;
2453 isl_space_free(dim);
2454 return stmt;
2455 error:
2456 isl_space_free(dim);
2457 return pet_stmt_free(stmt);
2460 /* Add all parameters in "dim" to "array".
2462 static struct pet_array *array_propagate_params(struct pet_array *array,
2463 __isl_take isl_space *dim)
2465 if (!array)
2466 goto error;
2468 array->context = isl_set_align_params(array->context,
2469 isl_space_copy(dim));
2470 array->extent = isl_set_align_params(array->extent,
2471 isl_space_copy(dim));
2472 if (array->value_bounds) {
2473 array->value_bounds = isl_set_align_params(array->value_bounds,
2474 isl_space_copy(dim));
2475 if (!array->value_bounds)
2476 goto error;
2479 if (!array->context || !array->extent)
2480 goto error;
2482 isl_space_free(dim);
2483 return array;
2484 error:
2485 isl_space_free(dim);
2486 return pet_array_free(array);
2489 /* Add all parameters in "dim" to "scop".
2491 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2492 __isl_take isl_space *dim)
2494 int i;
2496 if (!scop)
2497 goto error;
2499 for (i = 0; i < scop->n_array; ++i) {
2500 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2501 isl_space_copy(dim));
2502 if (!scop->arrays[i])
2503 goto error;
2506 for (i = 0; i < scop->n_stmt; ++i) {
2507 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2508 isl_space_copy(dim));
2509 if (!scop->stmts[i])
2510 goto error;
2513 isl_space_free(dim);
2514 return scop;
2515 error:
2516 isl_space_free(dim);
2517 return pet_scop_free(scop);
2520 /* Update all isl_sets and isl_maps in "scop" such that they all
2521 * have the same parameters.
2523 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2525 isl_space *dim;
2527 if (!scop)
2528 return NULL;
2530 dim = isl_set_get_space(scop->context);
2531 dim = scop_collect_params(scop, dim);
2533 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
2534 scop = scop_propagate_params(scop, dim);
2536 return scop;
2539 /* Check if the given access relation accesses a (0D) array that corresponds
2540 * to one of the parameters in "dim". If so, replace the array access
2541 * by an access to the set of integers with as index (and value)
2542 * that parameter.
2544 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
2545 __isl_take isl_space *dim)
2547 isl_id *array_id = NULL;
2548 int pos = -1;
2550 if (isl_map_has_tuple_id(access, isl_dim_out)) {
2551 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2552 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
2554 isl_space_free(dim);
2556 if (pos < 0) {
2557 isl_id_free(array_id);
2558 return access;
2561 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
2562 if (pos < 0) {
2563 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
2564 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
2565 pos = 0;
2566 } else
2567 isl_id_free(array_id);
2569 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2570 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
2572 return access;
2575 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2576 * in "dim" by a value equal to the corresponding parameter.
2578 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
2579 __isl_take isl_space *dim)
2581 int i;
2583 if (!expr)
2584 goto error;
2586 for (i = 0; i < expr->n_arg; ++i) {
2587 expr->args[i] =
2588 expr_detect_parameter_accesses(expr->args[i],
2589 isl_space_copy(dim));
2590 if (!expr->args[i])
2591 goto error;
2594 if (expr->type == pet_expr_access) {
2595 expr->acc.access = access_detect_parameter(expr->acc.access,
2596 isl_space_copy(dim));
2597 if (!expr->acc.access)
2598 goto error;
2601 isl_space_free(dim);
2602 return expr;
2603 error:
2604 isl_space_free(dim);
2605 return pet_expr_free(expr);
2608 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2609 * in "dim" by a value equal to the corresponding parameter.
2611 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2612 __isl_take isl_space *dim)
2614 if (!stmt)
2615 goto error;
2617 stmt->body = expr_detect_parameter_accesses(stmt->body,
2618 isl_space_copy(dim));
2620 if (!stmt->domain || !stmt->schedule || !stmt->body)
2621 goto error;
2623 isl_space_free(dim);
2624 return stmt;
2625 error:
2626 isl_space_free(dim);
2627 return pet_stmt_free(stmt);
2630 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2631 * in "dim" by a value equal to the corresponding parameter.
2633 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2634 __isl_take isl_space *dim)
2636 int i;
2638 if (!scop)
2639 goto error;
2641 for (i = 0; i < scop->n_stmt; ++i) {
2642 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2643 isl_space_copy(dim));
2644 if (!scop->stmts[i])
2645 goto error;
2648 isl_space_free(dim);
2649 return scop;
2650 error:
2651 isl_space_free(dim);
2652 return pet_scop_free(scop);
2655 /* Replace all accesses to (0D) arrays that correspond to any of
2656 * the parameters used in "scop" by a value equal
2657 * to the corresponding parameter.
2659 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2661 isl_space *dim;
2663 if (!scop)
2664 return NULL;
2666 dim = isl_set_get_space(scop->context);
2667 dim = scop_collect_params(scop, dim);
2669 scop = scop_detect_parameter_accesses(scop, dim);
2671 return scop;
2674 /* Add all read access relations (if "read" is set) and/or all write
2675 * access relations (if "write" is set) to "accesses" and return the result.
2677 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
2678 int read, int write, __isl_take isl_union_map *accesses)
2680 int i;
2681 isl_id *id;
2682 isl_space *dim;
2684 if (!expr)
2685 return NULL;
2687 for (i = 0; i < expr->n_arg; ++i)
2688 accesses = expr_collect_accesses(expr->args[i],
2689 read, write, accesses);
2691 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
2692 ((read && expr->acc.read) || (write && expr->acc.write)))
2693 accesses = isl_union_map_add_map(accesses,
2694 isl_map_copy(expr->acc.access));
2696 return accesses;
2699 /* Collect and return all read access relations (if "read" is set)
2700 * and/or all write access relations (if "write" is set) in "stmt".
2702 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2703 int read, int write, __isl_take isl_space *dim)
2705 isl_union_map *accesses;
2707 if (!stmt)
2708 return NULL;
2710 accesses = isl_union_map_empty(dim);
2711 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
2712 accesses = isl_union_map_intersect_domain(accesses,
2713 isl_union_set_from_set(isl_set_copy(stmt->domain)));
2715 return accesses;
2718 /* Collect and return all read access relations (if "read" is set)
2719 * and/or all write access relations (if "write" is set) in "scop".
2721 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2722 int read, int write)
2724 int i;
2725 isl_union_map *accesses;
2727 if (!scop)
2728 return NULL;
2730 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2732 for (i = 0; i < scop->n_stmt; ++i) {
2733 isl_union_map *accesses_i;
2734 isl_space *dim = isl_set_get_space(scop->context);
2735 accesses_i = stmt_collect_accesses(scop->stmts[i],
2736 read, write, dim);
2737 accesses = isl_union_map_union(accesses, accesses_i);
2740 return accesses;
2743 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
2745 return scop_collect_accesses(scop, 1, 0);
2748 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
2750 return scop_collect_accesses(scop, 0, 1);
2753 /* Collect and return the union of iteration domains in "scop".
2755 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2757 int i;
2758 isl_set *domain_i;
2759 isl_union_set *domain;
2761 if (!scop)
2762 return NULL;
2764 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2766 for (i = 0; i < scop->n_stmt; ++i) {
2767 domain_i = isl_set_copy(scop->stmts[i]->domain);
2768 domain = isl_union_set_add_set(domain, domain_i);
2771 return domain;
2774 /* Collect and return the schedules of the statements in "scop".
2775 * The range is normalized to the maximal number of scheduling
2776 * dimensions.
2778 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2780 int i, j;
2781 isl_map *schedule_i;
2782 isl_union_map *schedule;
2783 int depth, max_depth = 0;
2785 if (!scop)
2786 return NULL;
2788 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2790 for (i = 0; i < scop->n_stmt; ++i) {
2791 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2792 if (depth > max_depth)
2793 max_depth = depth;
2796 for (i = 0; i < scop->n_stmt; ++i) {
2797 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2798 depth = isl_map_dim(schedule_i, isl_dim_out);
2799 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2800 max_depth - depth);
2801 for (j = depth; j < max_depth; ++j)
2802 schedule_i = isl_map_fix_si(schedule_i,
2803 isl_dim_out, j, 0);
2804 schedule = isl_union_map_add_map(schedule, schedule_i);
2807 return schedule;
2810 /* Does expression "expr" write to "id"?
2812 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
2814 int i;
2815 isl_id *write_id;
2817 for (i = 0; i < expr->n_arg; ++i) {
2818 int writes = expr_writes(expr->args[i], id);
2819 if (writes < 0 || writes)
2820 return writes;
2823 if (expr->type != pet_expr_access)
2824 return 0;
2825 if (!expr->acc.write)
2826 return 0;
2827 if (pet_expr_is_affine(expr))
2828 return 0;
2830 write_id = pet_expr_access_get_id(expr);
2831 isl_id_free(write_id);
2833 if (!write_id)
2834 return -1;
2836 return write_id == id;
2839 /* Does statement "stmt" write to "id"?
2841 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
2843 return expr_writes(stmt->body, id);
2846 /* Is there any write access in "scop" that accesses "id"?
2848 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
2850 int i;
2852 if (!scop)
2853 return -1;
2855 for (i = 0; i < scop->n_stmt; ++i) {
2856 int writes = stmt_writes(scop->stmts[i], id);
2857 if (writes < 0 || writes)
2858 return writes;
2861 return 0;
2864 /* Add a reference identifier to access expression "expr".
2865 * "user" points to an integer that contains the sequence number
2866 * of the next reference.
2868 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
2870 isl_ctx *ctx;
2871 char name[50];
2872 int *n_ref = user;
2874 if (!expr)
2875 return expr;
2877 ctx = isl_map_get_ctx(expr->acc.access);
2878 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
2879 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
2880 if (!expr->acc.ref_id)
2881 return pet_expr_free(expr);
2883 return expr;
2886 /* Add a reference identifier to all access expressions in "stmt".
2887 * "n_ref" points to an integer that contains the sequence number
2888 * of the next reference.
2890 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2892 int i;
2894 if (!stmt)
2895 return NULL;
2897 for (i = 0; i < stmt->n_arg; ++i) {
2898 stmt->args[i] = pet_expr_map_access(stmt->args[i],
2899 &access_add_ref_id, n_ref);
2900 if (!stmt->args[i])
2901 return pet_stmt_free(stmt);
2904 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
2905 if (!stmt->body)
2906 return pet_stmt_free(stmt);
2908 return stmt;
2911 /* Add a reference identifier to all access expressions in "scop".
2913 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2915 int i;
2916 int n_ref;
2918 if (!scop)
2919 return NULL;
2921 n_ref = 0;
2922 for (i = 0; i < scop->n_stmt; ++i) {
2923 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2924 if (!scop->stmts[i])
2925 return pet_scop_free(scop);
2928 return scop;
2931 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2933 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
2935 int i, n;
2937 n = isl_set_dim(set, isl_dim_param);
2938 for (i = 0; i < n; ++i) {
2939 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2940 const char *name = isl_id_get_name(id);
2941 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
2942 isl_id_free(id);
2945 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
2946 isl_id *id = isl_set_get_tuple_id(set);
2947 const char *name = isl_id_get_name(id);
2948 set = isl_set_set_tuple_name(set, name);
2949 isl_id_free(id);
2952 return set;
2955 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2957 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
2959 int i, n;
2961 n = isl_map_dim(map, isl_dim_param);
2962 for (i = 0; i < n; ++i) {
2963 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2964 const char *name = isl_id_get_name(id);
2965 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2966 isl_id_free(id);
2969 if (isl_map_has_tuple_id(map, isl_dim_in)) {
2970 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
2971 const char *name = isl_id_get_name(id);
2972 map = isl_map_set_tuple_name(map, isl_dim_in, name);
2973 isl_id_free(id);
2976 if (isl_map_has_tuple_id(map, isl_dim_out)) {
2977 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
2978 const char *name = isl_id_get_name(id);
2979 map = isl_map_set_tuple_name(map, isl_dim_out, name);
2980 isl_id_free(id);
2983 return map;
2986 /* Reset the user pointer on all parameter ids in "array".
2988 static struct pet_array *array_anonymize(struct pet_array *array)
2990 if (!array)
2991 return NULL;
2993 array->context = set_anonymize(array->context);
2994 array->extent = set_anonymize(array->extent);
2995 if (!array->context || !array->extent)
2996 return pet_array_free(array);
2998 return array;
3001 /* Reset the user pointer on all parameter and tuple ids in
3002 * the access relation of the access expression "expr".
3004 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
3006 expr->acc.access = map_anonymize(expr->acc.access);
3007 if (!expr->acc.access)
3008 return pet_expr_free(expr);
3010 return expr;
3013 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3015 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3017 int i;
3018 isl_space *space;
3019 isl_set *domain;
3021 if (!stmt)
3022 return NULL;
3024 stmt->domain = set_anonymize(stmt->domain);
3025 stmt->schedule = map_anonymize(stmt->schedule);
3026 if (!stmt->domain || !stmt->schedule)
3027 return pet_stmt_free(stmt);
3029 for (i = 0; i < stmt->n_arg; ++i) {
3030 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3031 &access_anonymize, NULL);
3032 if (!stmt->args[i])
3033 return pet_stmt_free(stmt);
3036 stmt->body = pet_expr_map_access(stmt->body,
3037 &access_anonymize, NULL);
3038 if (!stmt->body)
3039 return pet_stmt_free(stmt);
3041 return stmt;
3044 /* Reset the user pointer on all parameter and tuple ids in "scop".
3046 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
3048 int i;
3050 if (!scop)
3051 return NULL;
3053 scop->context = set_anonymize(scop->context);
3054 scop->context_value = set_anonymize(scop->context_value);
3055 if (!scop->context || !scop->context_value)
3056 return pet_scop_free(scop);
3058 for (i = 0; i < scop->n_array; ++i) {
3059 scop->arrays[i] = array_anonymize(scop->arrays[i]);
3060 if (!scop->arrays[i])
3061 return pet_scop_free(scop);
3064 for (i = 0; i < scop->n_stmt; ++i) {
3065 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
3066 if (!scop->stmts[i])
3067 return pet_scop_free(scop);
3070 return scop;
3073 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
3074 * then intersect the range of "map" with the valid set of values.
3076 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
3077 struct pet_expr *arg, __isl_keep isl_union_map *value_bounds)
3079 isl_id *id;
3080 isl_map *vb;
3081 isl_space *space;
3082 isl_ctx *ctx = isl_map_get_ctx(map);
3084 id = pet_expr_access_get_id(arg);
3085 space = isl_space_alloc(ctx, 0, 0, 1);
3086 space = isl_space_set_tuple_id(space, isl_dim_in, id);
3087 vb = isl_union_map_extract_map(value_bounds, space);
3088 if (!isl_map_plain_is_empty(vb))
3089 map = isl_map_intersect_range(map, isl_map_range(vb));
3090 else
3091 isl_map_free(vb);
3093 return map;
3096 /* Given a set "domain", return a wrapped relation with the given set
3097 * as domain and a range of dimension "n_arg", where each coordinate
3098 * is either unbounded or, if the corresponding element of args is of
3099 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
3101 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
3102 unsigned n_arg, struct pet_expr **args,
3103 __isl_keep isl_union_map *value_bounds)
3105 int i;
3106 isl_map *map;
3107 isl_space *space;
3109 map = isl_map_from_domain(domain);
3110 space = isl_map_get_space(map);
3111 space = isl_space_add_dims(space, isl_dim_out, 1);
3113 for (i = 0; i < n_arg; ++i) {
3114 isl_map *map_i;
3115 struct pet_expr *arg = args[i];
3117 map_i = isl_map_universe(isl_space_copy(space));
3118 if (arg->type == pet_expr_access)
3119 map_i = access_apply_value_bounds(map_i, arg,
3120 value_bounds);
3121 map = isl_map_flat_range_product(map, map_i);
3123 isl_space_free(space);
3125 return isl_map_wrap(map);
3128 /* Data used in access_gist() callback.
3130 struct pet_access_gist_data {
3131 isl_set *domain;
3132 isl_union_map *value_bounds;
3135 /* Given an expression "expr" of type pet_expr_access, compute
3136 * the gist of the associated access relation with respect to
3137 * data->domain and the bounds on the values of the arguments
3138 * of the expression.
3140 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
3142 struct pet_access_gist_data *data = user;
3143 isl_set *domain;
3145 domain = isl_set_copy(data->domain);
3146 if (expr->n_arg > 0)
3147 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
3148 data->value_bounds);
3150 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
3151 if (!expr->acc.access)
3152 return pet_expr_free(expr);
3154 return expr;
3157 /* Compute the gist of the iteration domain and all access relations
3158 * of "stmt" based on the constraints on the parameters specified by "context"
3159 * and the constraints on the values of nested accesses specified
3160 * by "value_bounds".
3162 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3163 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3165 int i;
3166 isl_space *space;
3167 isl_set *domain;
3168 struct pet_access_gist_data data;
3170 if (!stmt)
3171 return NULL;
3173 data.domain = isl_set_copy(stmt->domain);
3174 data.value_bounds = value_bounds;
3175 if (stmt->n_arg > 0)
3176 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3178 data.domain = isl_set_intersect_params(data.domain,
3179 isl_set_copy(context));
3181 for (i = 0; i < stmt->n_arg; ++i) {
3182 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3183 &access_gist, &data);
3184 if (!stmt->args[i])
3185 goto error;
3188 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
3189 if (!stmt->body)
3190 goto error;
3192 isl_set_free(data.domain);
3194 space = isl_set_get_space(stmt->domain);
3195 if (isl_space_is_wrapping(space))
3196 space = isl_space_domain(isl_space_unwrap(space));
3197 domain = isl_set_universe(space);
3198 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3199 if (stmt->n_arg > 0)
3200 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3201 value_bounds);
3202 stmt->domain = isl_set_gist(stmt->domain, domain);
3203 if (!stmt->domain)
3204 return pet_stmt_free(stmt);
3206 return stmt;
3207 error:
3208 isl_set_free(data.domain);
3209 return pet_stmt_free(stmt);
3212 /* Compute the gist of the extent of the array
3213 * based on the constraints on the parameters specified by "context".
3215 static struct pet_array *array_gist(struct pet_array *array,
3216 __isl_keep isl_set *context)
3218 if (!array)
3219 return NULL;
3221 array->extent = isl_set_gist_params(array->extent,
3222 isl_set_copy(context));
3223 if (!array->extent)
3224 return pet_array_free(array);
3226 return array;
3229 /* Compute the gist of all sets and relations in "scop"
3230 * based on the constraints on the parameters specified by "scop->context"
3231 * and the constraints on the values of nested accesses specified
3232 * by "value_bounds".
3234 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3235 __isl_keep isl_union_map *value_bounds)
3237 int i;
3239 if (!scop)
3240 return NULL;
3242 scop->context = isl_set_coalesce(scop->context);
3243 if (!scop->context)
3244 return pet_scop_free(scop);
3246 for (i = 0; i < scop->n_array; ++i) {
3247 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3248 if (!scop->arrays[i])
3249 return pet_scop_free(scop);
3252 for (i = 0; i < scop->n_stmt; ++i) {
3253 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3254 value_bounds);
3255 if (!scop->stmts[i])
3256 return pet_scop_free(scop);
3259 return scop;
3262 /* Intersect the context of "scop" with "context".
3263 * To ensure that we don't introduce any unnamed parameters in
3264 * the context of "scop", we first remove the unnamed parameters
3265 * from "context".
3267 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3268 __isl_take isl_set *context)
3270 if (!scop)
3271 goto error;
3273 context = set_project_out_unnamed_params(context);
3274 scop->context = isl_set_intersect(scop->context, context);
3275 if (!scop->context)
3276 return pet_scop_free(scop);
3278 return scop;
3279 error:
3280 isl_set_free(context);
3281 return pet_scop_free(scop);
3284 /* Drop the current context of "scop". That is, replace the context
3285 * by a universal set.
3287 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3289 isl_space *space;
3291 if (!scop)
3292 return NULL;
3294 space = isl_set_get_space(scop->context);
3295 isl_set_free(scop->context);
3296 scop->context = isl_set_universe(space);
3297 if (!scop->context)
3298 return pet_scop_free(scop);
3300 return scop;
3303 /* Append "array" to the arrays of "scop".
3305 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3306 struct pet_array *array)
3308 isl_ctx *ctx;
3309 struct pet_array **arrays;
3311 if (!array || !scop)
3312 goto error;
3314 ctx = isl_set_get_ctx(scop->context);
3315 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3316 scop->n_array + 1);
3317 if (!arrays)
3318 goto error;
3319 scop->arrays = arrays;
3320 scop->arrays[scop->n_array] = array;
3321 scop->n_array++;
3323 return scop;
3324 error:
3325 pet_array_free(array);
3326 return pet_scop_free(scop);