scop_plus.cc: access_collect_arrays: use pet_expr_is_affine
[pet.git] / scop.c
blob1b150ddade92b02e9db81240f3eda341bcdc41a0
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 1 if the two pet_exprs are equivalent.
447 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
449 int i;
451 if (!expr1 || !expr2)
452 return 0;
454 if (expr1->type != expr2->type)
455 return 0;
456 if (expr1->n_arg != expr2->n_arg)
457 return 0;
458 for (i = 0; i < expr1->n_arg; ++i)
459 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
460 return 0;
461 switch (expr1->type) {
462 case pet_expr_double:
463 if (strcmp(expr1->d.s, expr2->d.s))
464 return 0;
465 if (expr1->d.val != expr2->d.val)
466 return 0;
467 break;
468 case pet_expr_access:
469 if (expr1->acc.read != expr2->acc.read)
470 return 0;
471 if (expr1->acc.write != expr2->acc.write)
472 return 0;
473 if (expr1->acc.ref_id != expr2->acc.ref_id)
474 return 0;
475 if (!expr1->acc.access || !expr2->acc.access)
476 return 0;
477 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
478 return 0;
479 break;
480 case pet_expr_unary:
481 case pet_expr_binary:
482 case pet_expr_ternary:
483 if (expr1->op != expr2->op)
484 return 0;
485 break;
486 case pet_expr_call:
487 if (strcmp(expr1->name, expr2->name))
488 return 0;
489 break;
490 case pet_expr_cast:
491 if (strcmp(expr1->type_name, expr2->type_name))
492 return 0;
493 break;
496 return 1;
499 /* Add extra conditions on the parameters to all access relations in "expr".
501 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
502 __isl_take isl_set *cond)
504 int i;
506 if (!expr)
507 goto error;
509 for (i = 0; i < expr->n_arg; ++i) {
510 expr->args[i] = pet_expr_restrict(expr->args[i],
511 isl_set_copy(cond));
512 if (!expr->args[i])
513 goto error;
516 if (expr->type == pet_expr_access) {
517 expr->acc.access = isl_map_intersect_params(expr->acc.access,
518 isl_set_copy(cond));
519 if (!expr->acc.access)
520 goto error;
523 isl_set_free(cond);
524 return expr;
525 error:
526 isl_set_free(cond);
527 return pet_expr_free(expr);
530 /* Modify all expressions of type pet_expr_access in "expr"
531 * by calling "fn" on them.
533 struct pet_expr *pet_expr_map_access(struct pet_expr *expr,
534 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
535 void *user)
537 int i;
539 if (!expr)
540 return NULL;
542 for (i = 0; i < expr->n_arg; ++i) {
543 expr->args[i] = pet_expr_map_access(expr->args[i], fn, user);
544 if (!expr->args[i])
545 return pet_expr_free(expr);
548 if (expr->type == pet_expr_access)
549 expr = fn(expr, user);
551 return expr;
554 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
556 * Return -1 on error (where fn return a negative value is treated as an error).
557 * Otherwise return 0.
559 int pet_expr_foreach_access_expr(struct pet_expr *expr,
560 int (*fn)(struct pet_expr *expr, void *user), void *user)
562 int i;
564 if (!expr)
565 return -1;
567 for (i = 0; i < expr->n_arg; ++i)
568 if (pet_expr_foreach_access_expr(expr->args[i], fn, user) < 0)
569 return -1;
571 if (expr->type == pet_expr_access)
572 return fn(expr, user);
574 return 0;
577 /* Modify the access relation of the given access expression
578 * based on the given iteration space transformation.
579 * If the access has any arguments then the domain of the access relation
580 * is a wrapped mapping from the iteration space to the space of
581 * argument values. We only need to change the domain of this wrapped
582 * mapping, so we extend the input transformation with an identity mapping
583 * on the space of argument values.
585 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
587 isl_map *update = user;
588 isl_space *dim;
590 update = isl_map_copy(update);
592 dim = isl_map_get_space(expr->acc.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 expr->acc.access = isl_map_apply_domain(expr->acc.access, update);
606 if (!expr->acc.access)
607 return pet_expr_free(expr);
609 return expr;
612 /* Modify all access relations in "expr" based on the given iteration space
613 * transformation.
615 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
616 __isl_take isl_map *update)
618 expr = pet_expr_map_access(expr, &update_domain, update);
619 isl_map_free(update);
620 return expr;
623 /* Construct a pet_stmt with given line number and statement
624 * number from a pet_expr.
625 * The initial iteration domain is the zero-dimensional universe.
626 * The name of the domain is given by "label" if it is non-NULL.
627 * Otherwise, the name is constructed as S_<id>.
628 * The domains of all access relations are modified to refer
629 * to the statement iteration domain.
631 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
632 __isl_take isl_id *label, int id, struct pet_expr *expr)
634 struct pet_stmt *stmt;
635 isl_space *dim;
636 isl_set *dom;
637 isl_map *sched;
638 isl_map *add_name;
639 char name[50];
641 if (!expr)
642 goto error;
644 stmt = isl_calloc_type(ctx, struct pet_stmt);
645 if (!stmt)
646 goto error;
648 dim = isl_space_set_alloc(ctx, 0, 0);
649 if (label)
650 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
651 else {
652 snprintf(name, sizeof(name), "S_%d", id);
653 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
655 dom = isl_set_universe(isl_space_copy(dim));
656 sched = isl_map_from_domain(isl_set_copy(dom));
658 dim = isl_space_from_range(dim);
659 add_name = isl_map_universe(dim);
660 expr = expr_update_domain(expr, add_name);
662 stmt->line = line;
663 stmt->domain = dom;
664 stmt->schedule = sched;
665 stmt->body = expr;
667 if (!stmt->domain || !stmt->schedule || !stmt->body)
668 return pet_stmt_free(stmt);
670 return stmt;
671 error:
672 isl_id_free(label);
673 return pet_expr_free(expr);
676 void *pet_stmt_free(struct pet_stmt *stmt)
678 int i;
680 if (!stmt)
681 return NULL;
683 isl_set_free(stmt->domain);
684 isl_map_free(stmt->schedule);
685 pet_expr_free(stmt->body);
687 for (i = 0; i < stmt->n_arg; ++i)
688 pet_expr_free(stmt->args[i]);
689 free(stmt->args);
691 free(stmt);
692 return NULL;
695 static void stmt_dump(struct pet_stmt *stmt, int indent)
697 int i;
699 if (!stmt)
700 return;
702 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
703 fprintf(stderr, "%*s", indent, "");
704 isl_set_dump(stmt->domain);
705 fprintf(stderr, "%*s", indent, "");
706 isl_map_dump(stmt->schedule);
707 expr_dump(stmt->body, indent);
708 for (i = 0; i < stmt->n_arg; ++i)
709 expr_dump(stmt->args[i], indent + 2);
712 void pet_stmt_dump(struct pet_stmt *stmt)
714 stmt_dump(stmt, 0);
717 struct pet_array *pet_array_free(struct pet_array *array)
719 if (!array)
720 return NULL;
722 isl_set_free(array->context);
723 isl_set_free(array->extent);
724 isl_set_free(array->value_bounds);
725 free(array->element_type);
727 free(array);
728 return NULL;
731 void pet_array_dump(struct pet_array *array)
733 if (!array)
734 return;
736 isl_set_dump(array->context);
737 isl_set_dump(array->extent);
738 isl_set_dump(array->value_bounds);
739 fprintf(stderr, "%s %s\n", array->element_type,
740 array->live_out ? "live-out" : "");
743 /* Alloc a pet_scop structure, with extra room for information that
744 * is only used during parsing.
746 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
748 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
751 /* Construct a pet_scop with room for n statements.
753 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
755 isl_space *space;
756 struct pet_scop *scop;
758 scop = pet_scop_alloc(ctx);
759 if (!scop)
760 return NULL;
762 space = isl_space_params_alloc(ctx, 0);
763 scop->context = isl_set_universe(isl_space_copy(space));
764 scop->context_value = isl_set_universe(space);
765 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
766 if (!scop->context || !scop->stmts)
767 return pet_scop_free(scop);
769 scop->n_stmt = n;
771 return scop;
774 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
776 return scop_alloc(ctx, 0);
779 /* Update "context" with respect to the valid parameter values for "access".
781 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
782 __isl_take isl_set *context)
784 context = isl_set_intersect(context,
785 isl_map_params(isl_map_copy(access)));
786 return context;
789 /* Update "context" with respect to the valid parameter values for "expr".
791 * If "expr" represents a ternary operator, then a parameter value
792 * needs to be valid for the condition and for at least one of the
793 * remaining two arguments.
794 * If the condition is an affine expression, then we can be a bit more specific.
795 * The parameter then has to be valid for the second argument for
796 * non-zero accesses and valid for the third argument for zero accesses.
798 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
799 __isl_take isl_set *context)
801 int i;
803 if (expr->type == pet_expr_ternary) {
804 int is_aff;
805 isl_set *context1, *context2;
807 is_aff = pet_expr_is_affine(expr->args[0]);
808 if (is_aff < 0)
809 goto error;
811 context = expr_extract_context(expr->args[0], context);
812 context1 = expr_extract_context(expr->args[1],
813 isl_set_copy(context));
814 context2 = expr_extract_context(expr->args[2], context);
816 if (is_aff) {
817 isl_map *access;
818 isl_set *zero_set;
820 access = isl_map_copy(expr->args[0]->acc.access);
821 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
822 zero_set = isl_map_params(access);
823 context1 = isl_set_subtract(context1,
824 isl_set_copy(zero_set));
825 context2 = isl_set_intersect(context2, zero_set);
828 context = isl_set_union(context1, context2);
829 context = isl_set_coalesce(context);
831 return context;
834 for (i = 0; i < expr->n_arg; ++i)
835 context = expr_extract_context(expr->args[i], context);
837 if (expr->type == pet_expr_access)
838 context = access_extract_context(expr->acc.access, context);
840 return context;
841 error:
842 isl_set_free(context);
843 return NULL;
846 /* Update "context" with respect to the valid parameter values for "stmt".
848 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
849 __isl_take isl_set *context)
851 int i;
853 for (i = 0; i < stmt->n_arg; ++i)
854 context = expr_extract_context(stmt->args[i], context);
856 context = expr_extract_context(stmt->body, context);
858 return context;
861 /* Construct a pet_scop that contains the given pet_stmt.
863 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
865 struct pet_scop *scop;
867 if (!stmt)
868 return NULL;
870 scop = scop_alloc(ctx, 1);
871 if (!scop)
872 goto error;
874 scop->context = stmt_extract_context(stmt, scop->context);
875 if (!scop->context)
876 goto error;
878 scop->stmts[0] = stmt;
880 return scop;
881 error:
882 pet_stmt_free(stmt);
883 pet_scop_free(scop);
884 return NULL;
887 /* Does "set" represent an element of an unnamed space, i.e.,
888 * does it represent an affine expression?
890 static int set_is_affine(__isl_keep isl_set *set)
892 int has_id;
894 has_id = isl_set_has_tuple_id(set);
895 if (has_id < 0)
896 return -1;
898 return !has_id;
901 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
902 * ext may be equal to either ext1 or ext2.
904 * The two skips that need to be combined are assumed to be affine expressions.
906 * We need to skip in ext if we need to skip in either ext1 or ext2.
907 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
909 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
910 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
911 enum pet_skip type)
913 isl_set *set, *skip1, *skip2;
915 if (!ext)
916 return NULL;
917 if (!ext1->skip[type] && !ext2->skip[type])
918 return ext;
919 if (!ext1->skip[type]) {
920 if (ext == ext2)
921 return ext;
922 ext->skip[type] = ext2->skip[type];
923 ext2->skip[type] = NULL;
924 return ext;
926 if (!ext2->skip[type]) {
927 if (ext == ext1)
928 return ext;
929 ext->skip[type] = ext1->skip[type];
930 ext1->skip[type] = NULL;
931 return ext;
934 if (!set_is_affine(ext1->skip[type]) ||
935 !set_is_affine(ext2->skip[type]))
936 isl_die(isl_set_get_ctx(ext1->skip[type]), isl_error_internal,
937 "can only combine affine skips",
938 return pet_scop_free(&ext->scop));
940 skip1 = isl_set_copy(ext1->skip[type]);
941 skip2 = isl_set_copy(ext2->skip[type]);
942 set = isl_set_intersect(
943 isl_set_fix_si(isl_set_copy(skip1), isl_dim_set, 0, 0),
944 isl_set_fix_si(isl_set_copy(skip2), isl_dim_set, 0, 0));
945 set = isl_set_union(set, isl_set_fix_si(skip1, isl_dim_set, 0, 1));
946 set = isl_set_union(set, isl_set_fix_si(skip2, isl_dim_set, 0, 1));
947 set = isl_set_coalesce(set);
948 isl_set_free(ext1->skip[type]);
949 ext1->skip[type] = NULL;
950 isl_set_free(ext2->skip[type]);
951 ext2->skip[type] = NULL;
952 ext->skip[type] = set;
953 if (!ext->skip[type])
954 return pet_scop_free(&ext->scop);
956 return ext;
959 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
960 * where type takes on the values pet_skip_now and pet_skip_later.
961 * scop may be equal to either scop1 or scop2.
963 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
964 struct pet_scop *scop1, struct pet_scop *scop2)
966 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
967 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
968 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
970 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
971 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
972 return &ext->scop;
975 /* Update scop->start and scop->end to include the region from "start"
976 * to "end". In particular, if scop->end == 0, then "scop" does not
977 * have any offset information yet and we simply take the information
978 * from "start" and "end". Otherwise, we update the fields if the
979 * region from "start" to "end" is not already included.
981 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
982 unsigned start, unsigned end)
984 if (!scop)
985 return NULL;
986 if (scop->end == 0) {
987 scop->start = start;
988 scop->end = end;
989 } else {
990 if (start < scop->start)
991 scop->start = start;
992 if (end > scop->end)
993 scop->end = end;
996 return scop;
999 /* Combine the offset information of "scop1" and "scop2" into "scop".
1001 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
1002 struct pet_scop *scop1, struct pet_scop *scop2)
1004 if (scop1->end)
1005 scop = pet_scop_update_start_end(scop,
1006 scop1->start, scop1->end);
1007 if (scop2->end)
1008 scop = pet_scop_update_start_end(scop,
1009 scop2->start, scop2->end);
1010 return scop;
1013 /* Construct a pet_scop that contains the offset information,
1014 * arrays, statements and skip information in "scop1" and "scop2".
1016 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1017 struct pet_scop *scop2)
1019 int i;
1020 struct pet_scop *scop = NULL;
1022 if (!scop1 || !scop2)
1023 goto error;
1025 if (scop1->n_stmt == 0) {
1026 scop2 = scop_combine_skips(scop2, scop1, scop2);
1027 pet_scop_free(scop1);
1028 return scop2;
1031 if (scop2->n_stmt == 0) {
1032 scop1 = scop_combine_skips(scop1, scop1, scop2);
1033 pet_scop_free(scop2);
1034 return scop1;
1037 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1038 if (!scop)
1039 goto error;
1041 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1042 scop1->n_array + scop2->n_array);
1043 if (!scop->arrays)
1044 goto error;
1045 scop->n_array = scop1->n_array + scop2->n_array;
1047 for (i = 0; i < scop1->n_stmt; ++i) {
1048 scop->stmts[i] = scop1->stmts[i];
1049 scop1->stmts[i] = NULL;
1052 for (i = 0; i < scop2->n_stmt; ++i) {
1053 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1054 scop2->stmts[i] = NULL;
1057 for (i = 0; i < scop1->n_array; ++i) {
1058 scop->arrays[i] = scop1->arrays[i];
1059 scop1->arrays[i] = NULL;
1062 for (i = 0; i < scop2->n_array; ++i) {
1063 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1064 scop2->arrays[i] = NULL;
1067 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1068 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1069 scop = scop_combine_skips(scop, scop1, scop2);
1070 scop = scop_combine_start_end(scop, scop1, scop2);
1072 pet_scop_free(scop1);
1073 pet_scop_free(scop2);
1074 return scop;
1075 error:
1076 pet_scop_free(scop1);
1077 pet_scop_free(scop2);
1078 pet_scop_free(scop);
1079 return NULL;
1082 /* Apply the skip condition "skip" to "scop".
1083 * That is, make sure "scop" is not executed when the condition holds.
1085 * If "skip" is an affine expression, we add the conditions under
1086 * which the expression is zero to the iteration domains.
1087 * Otherwise, we add a filter on the variable attaining the value zero.
1089 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1090 __isl_take isl_set *skip)
1092 isl_map *skip_map;
1093 int is_aff;
1095 if (!scop || !skip)
1096 goto error;
1098 is_aff = set_is_affine(skip);
1099 if (is_aff < 0)
1100 goto error;
1102 if (!is_aff)
1103 return pet_scop_filter(scop, isl_map_from_range(skip), 0);
1105 skip = isl_set_fix_si(skip, isl_dim_set, 0, 0);
1106 scop = pet_scop_restrict(scop, isl_set_params(skip));
1108 return scop;
1109 error:
1110 isl_set_free(skip);
1111 return pet_scop_free(scop);
1114 /* Construct a pet_scop that contains the arrays, statements and
1115 * skip information in "scop1" and "scop2", where the two scops
1116 * are executed "in sequence". That is, breaks and continues
1117 * in scop1 have an effect on scop2.
1119 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1120 struct pet_scop *scop2)
1122 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1123 scop2 = restrict_skip(scop2,
1124 pet_scop_get_skip(scop1, pet_skip_now));
1125 return pet_scop_add(ctx, scop1, scop2);
1128 /* Construct a pet_scop that contains the arrays, statements and
1129 * skip information in "scop1" and "scop2", where the two scops
1130 * are executed "in parallel". That is, any break or continue
1131 * in scop1 has no effect on scop2.
1133 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1134 struct pet_scop *scop2)
1136 return pet_scop_add(ctx, scop1, scop2);
1139 void *pet_scop_free(struct pet_scop *scop)
1141 int i;
1142 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1144 if (!scop)
1145 return NULL;
1146 isl_set_free(scop->context);
1147 isl_set_free(scop->context_value);
1148 if (scop->arrays)
1149 for (i = 0; i < scop->n_array; ++i)
1150 pet_array_free(scop->arrays[i]);
1151 free(scop->arrays);
1152 if (scop->stmts)
1153 for (i = 0; i < scop->n_stmt; ++i)
1154 pet_stmt_free(scop->stmts[i]);
1155 free(scop->stmts);
1156 isl_set_free(ext->skip[pet_skip_now]);
1157 isl_set_free(ext->skip[pet_skip_later]);
1158 free(scop);
1159 return NULL;
1162 void pet_scop_dump(struct pet_scop *scop)
1164 int i;
1165 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1167 if (!scop)
1168 return;
1170 isl_set_dump(scop->context);
1171 isl_set_dump(scop->context_value);
1172 for (i = 0; i < scop->n_array; ++i)
1173 pet_array_dump(scop->arrays[i]);
1174 for (i = 0; i < scop->n_stmt; ++i)
1175 pet_stmt_dump(scop->stmts[i]);
1177 if (ext->skip[0]) {
1178 fprintf(stderr, "skip\n");
1179 isl_set_dump(ext->skip[0]);
1180 isl_set_dump(ext->skip[1]);
1184 /* Return 1 if the two pet_arrays are equivalent.
1186 * We don't compare element_size as this may be target dependent.
1188 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1190 if (!array1 || !array2)
1191 return 0;
1193 if (!isl_set_is_equal(array1->context, array2->context))
1194 return 0;
1195 if (!isl_set_is_equal(array1->extent, array2->extent))
1196 return 0;
1197 if (!!array1->value_bounds != !!array2->value_bounds)
1198 return 0;
1199 if (array1->value_bounds &&
1200 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1201 return 0;
1202 if (strcmp(array1->element_type, array2->element_type))
1203 return 0;
1204 if (array1->live_out != array2->live_out)
1205 return 0;
1206 if (array1->uniquely_defined != array2->uniquely_defined)
1207 return 0;
1208 if (array1->declared != array2->declared)
1209 return 0;
1210 if (array1->exposed != array2->exposed)
1211 return 0;
1213 return 1;
1216 /* Return 1 if the two pet_stmts are equivalent.
1218 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1220 int i;
1222 if (!stmt1 || !stmt2)
1223 return 0;
1225 if (stmt1->line != stmt2->line)
1226 return 0;
1227 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1228 return 0;
1229 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1230 return 0;
1231 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1232 return 0;
1233 if (stmt1->n_arg != stmt2->n_arg)
1234 return 0;
1235 for (i = 0; i < stmt1->n_arg; ++i) {
1236 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1237 return 0;
1240 return 1;
1243 /* Return 1 if the two pet_scops are equivalent.
1245 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1247 int i;
1249 if (!scop1 || !scop2)
1250 return 0;
1252 if (!isl_set_is_equal(scop1->context, scop2->context))
1253 return 0;
1254 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1255 return 0;
1257 if (scop1->n_array != scop2->n_array)
1258 return 0;
1259 for (i = 0; i < scop1->n_array; ++i)
1260 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1261 return 0;
1263 if (scop1->n_stmt != scop2->n_stmt)
1264 return 0;
1265 for (i = 0; i < scop1->n_stmt; ++i)
1266 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1267 return 0;
1269 return 1;
1272 /* Prefix the schedule of "stmt" with an extra dimension with constant
1273 * value "pos".
1275 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1277 if (!stmt)
1278 return NULL;
1280 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1281 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1282 if (!stmt->schedule)
1283 return pet_stmt_free(stmt);
1285 return stmt;
1288 /* Prefix the schedules of all statements in "scop" with an extra
1289 * dimension with constant value "pos".
1291 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1293 int i;
1295 if (!scop)
1296 return NULL;
1298 for (i = 0; i < scop->n_stmt; ++i) {
1299 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1300 if (!scop->stmts[i])
1301 return pet_scop_free(scop);
1304 return scop;
1307 /* Given a set with a parameter at "param_pos" that refers to the
1308 * iterator, "move" the iterator to the first set dimension.
1309 * That is, essentially equate the parameter to the first set dimension
1310 * and then project it out.
1312 * The first set dimension may however refer to a virtual iterator,
1313 * while the parameter refers to the "real" iterator.
1314 * We therefore need to take into account the mapping "iv_map", which
1315 * maps the virtual iterator to the real iterator.
1316 * In particular, we equate the set dimension to the input of the map
1317 * and the parameter to the output of the map and then project out
1318 * everything we don't need anymore.
1320 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1321 int param_pos, __isl_take isl_map *iv_map)
1323 isl_map *map;
1324 map = isl_map_from_domain(set);
1325 map = isl_map_add_dims(map, isl_dim_out, 1);
1326 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1327 iv_map = isl_map_align_params(iv_map, isl_map_get_space(map));
1328 map = isl_map_apply_range(map, iv_map);
1329 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1330 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1331 return isl_map_domain(map);
1334 /* Data used in embed_access.
1335 * extend adds an iterator to the iteration domain
1336 * iv_map maps the virtual iterator to the real iterator
1337 * var_id represents the induction variable of the corresponding loop
1339 struct pet_embed_access {
1340 isl_map *extend;
1341 isl_map *iv_map;
1342 isl_id *var_id;
1345 /* Given an access expression, embed the associated access relation
1346 * in an extra outer loop.
1348 * We first update the iteration domain to insert the extra dimension.
1350 * If the access refers to the induction variable, then it is
1351 * turned into an access to the set of integers with index (and value)
1352 * equal to the induction variable.
1354 * If the induction variable appears in the constraints (as a parameter),
1355 * then the parameter is equated to the newly introduced iteration
1356 * domain dimension and subsequently projected out.
1358 * Similarly, if the accessed array is a virtual array (with user
1359 * pointer equal to NULL), as created by create_test_access,
1360 * then it is extended along with the domain of the access.
1362 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
1364 struct pet_embed_access *data = user;
1365 isl_map *access;
1366 isl_id *array_id = NULL;
1367 int pos;
1369 expr = update_domain(expr, data->extend);
1370 if (!expr)
1371 return NULL;
1373 access = expr->acc.access;
1375 if (isl_map_has_tuple_id(access, isl_dim_out))
1376 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1377 if (array_id == data->var_id ||
1378 (array_id && !isl_id_get_user(array_id))) {
1379 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1380 access = isl_map_equate(access,
1381 isl_dim_in, 0, isl_dim_out, 0);
1382 if (array_id == data->var_id)
1383 access = isl_map_apply_range(access,
1384 isl_map_copy(data->iv_map));
1385 else
1386 access = isl_map_set_tuple_id(access, isl_dim_out,
1387 isl_id_copy(array_id));
1389 isl_id_free(array_id);
1391 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1392 if (pos >= 0) {
1393 isl_set *set = isl_map_wrap(access);
1394 set = internalize_iv(set, pos, isl_map_copy(data->iv_map));
1395 access = isl_set_unwrap(set);
1397 expr->acc.access = isl_map_set_dim_id(access, isl_dim_in, 0,
1398 isl_id_copy(data->var_id));
1399 if (!expr->acc.access)
1400 return pet_expr_free(expr);
1402 return expr;
1405 /* Embed all access subexpressions of "expr" in an extra loop.
1406 * "extend" inserts an outer loop iterator in the iteration domains.
1407 * "iv_map" maps the virtual iterator to the real iterator
1408 * "var_id" represents the induction variable.
1410 static struct pet_expr *expr_embed(struct pet_expr *expr,
1411 __isl_take isl_map *extend, __isl_take isl_map *iv_map,
1412 __isl_keep isl_id *var_id)
1414 struct pet_embed_access data =
1415 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1417 expr = pet_expr_map_access(expr, &embed_access, &data);
1418 isl_map_free(iv_map);
1419 isl_map_free(extend);
1420 return expr;
1423 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1424 * "dom" and schedule "sched". "var_id" represents the induction variable
1425 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1426 * That is, it maps the iterator used in "dom" and the domain of "sched"
1427 * to the iterator that some of the parameters in "stmt" may refer to.
1429 * The iteration domain and schedule of the statement are updated
1430 * according to the iteration domain and schedule of the new loop.
1431 * If stmt->domain is a wrapped map, then the iteration domain
1432 * is the domain of this map, so we need to be careful to adjust
1433 * this domain.
1435 * If the induction variable appears in the constraints (as a parameter)
1436 * of the current iteration domain or the schedule of the statement,
1437 * then the parameter is equated to the newly introduced iteration
1438 * domain dimension and subsequently projected out.
1440 * Finally, all access relations are updated based on the extra loop.
1442 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1443 __isl_take isl_set *dom, __isl_take isl_map *sched,
1444 __isl_take isl_map *iv_map, __isl_take isl_id *var_id)
1446 int i;
1447 int pos;
1448 isl_id *stmt_id;
1449 isl_space *dim;
1450 isl_map *extend;
1452 if (!stmt)
1453 goto error;
1455 if (isl_set_is_wrapping(stmt->domain)) {
1456 isl_map *map;
1457 isl_map *ext;
1458 isl_space *ran_dim;
1460 map = isl_set_unwrap(stmt->domain);
1461 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1462 ran_dim = isl_space_range(isl_map_get_space(map));
1463 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1464 isl_set_universe(ran_dim));
1465 map = isl_map_flat_domain_product(ext, map);
1466 map = isl_map_set_tuple_id(map, isl_dim_in,
1467 isl_id_copy(stmt_id));
1468 dim = isl_space_domain(isl_map_get_space(map));
1469 stmt->domain = isl_map_wrap(map);
1470 } else {
1471 stmt_id = isl_set_get_tuple_id(stmt->domain);
1472 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1473 stmt->domain);
1474 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1475 isl_id_copy(stmt_id));
1476 dim = isl_set_get_space(stmt->domain);
1479 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1480 if (pos >= 0)
1481 stmt->domain = internalize_iv(stmt->domain, pos,
1482 isl_map_copy(iv_map));
1484 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1485 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1486 isl_dim_in, stmt_id);
1488 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1489 if (pos >= 0) {
1490 isl_set *set = isl_map_wrap(stmt->schedule);
1491 set = internalize_iv(set, pos, isl_map_copy(iv_map));
1492 stmt->schedule = isl_set_unwrap(set);
1495 dim = isl_space_map_from_set(dim);
1496 extend = isl_map_identity(dim);
1497 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1498 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1499 isl_map_get_tuple_id(extend, isl_dim_out));
1500 for (i = 0; i < stmt->n_arg; ++i)
1501 stmt->args[i] = expr_embed(stmt->args[i], isl_map_copy(extend),
1502 isl_map_copy(iv_map), var_id);
1503 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1505 isl_set_free(dom);
1506 isl_id_free(var_id);
1508 for (i = 0; i < stmt->n_arg; ++i)
1509 if (!stmt->args[i])
1510 return pet_stmt_free(stmt);
1511 if (!stmt->domain || !stmt->schedule || !stmt->body)
1512 return pet_stmt_free(stmt);
1513 return stmt;
1514 error:
1515 isl_set_free(dom);
1516 isl_map_free(sched);
1517 isl_map_free(iv_map);
1518 isl_id_free(var_id);
1519 return NULL;
1522 /* Embed the given pet_array in an extra outer loop with iteration domain
1523 * "dom".
1524 * This embedding only has an effect on virtual arrays (those with
1525 * user pointer equal to NULL), which need to be extended along with
1526 * the iteration domain.
1528 static struct pet_array *pet_array_embed(struct pet_array *array,
1529 __isl_take isl_set *dom)
1531 isl_id *array_id = NULL;
1533 if (!array)
1534 goto error;
1536 if (isl_set_has_tuple_id(array->extent))
1537 array_id = isl_set_get_tuple_id(array->extent);
1539 if (array_id && !isl_id_get_user(array_id)) {
1540 array->extent = isl_set_flat_product(dom, array->extent);
1541 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1542 if (!array->extent)
1543 return pet_array_free(array);
1544 } else {
1545 isl_set_free(dom);
1546 isl_id_free(array_id);
1549 return array;
1550 error:
1551 isl_set_free(dom);
1552 return NULL;
1555 /* Project out all unnamed parameters from "set" and return the result.
1557 static __isl_give isl_set *set_project_out_unnamed_params(
1558 __isl_take isl_set *set)
1560 int i, n;
1562 n = isl_set_dim(set, isl_dim_param);
1563 for (i = n - 1; i >= 0; --i) {
1564 if (isl_set_has_dim_name(set, isl_dim_param, i))
1565 continue;
1566 set = isl_set_project_out(set, isl_dim_param, i, 1);
1569 return set;
1572 /* Update the context with respect to an embedding into a loop
1573 * with iteration domain "dom" and induction variable "id".
1574 * "iv_map" maps a possibly virtual iterator (used in "dom")
1575 * to the real iterator (parameter "id").
1577 * If the current context is independent of "id", we don't need
1578 * to do anything.
1579 * Otherwise, a parameter value is invalid for the embedding if
1580 * any of the corresponding iterator values is invalid.
1581 * That is, a parameter value is valid only if all the corresponding
1582 * iterator values are valid.
1583 * We therefore compute the set of parameters
1585 * forall i in dom : valid (i)
1587 * or
1589 * not exists i in dom : not valid(i)
1591 * i.e.,
1593 * not exists i in dom \ valid(i)
1595 * Before we subtract valid(i) from dom, we first need to map
1596 * the real iterator to the virtual iterator.
1598 * If there are any unnamed parameters in "dom", then we consider
1599 * a parameter value to be valid if it is valid for any value of those
1600 * unnamed parameters. They are therefore projected out at the end.
1602 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1603 __isl_keep isl_set *dom, __isl_keep isl_map *iv_map,
1604 __isl_keep isl_id *id)
1606 int pos;
1608 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1609 if (pos < 0)
1610 return context;
1612 context = isl_set_from_params(context);
1613 context = isl_set_add_dims(context, isl_dim_set, 1);
1614 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1615 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1616 context = isl_set_apply(context, isl_map_reverse(isl_map_copy(iv_map)));
1617 context = isl_set_subtract(isl_set_copy(dom), context);
1618 context = isl_set_params(context);
1619 context = isl_set_complement(context);
1620 context = set_project_out_unnamed_params(context);
1621 return context;
1624 /* Embed all statements and arrays in "scop" in an extra outer loop
1625 * with iteration domain "dom" and schedule "sched".
1626 * "id" represents the induction variable of the loop.
1627 * "iv_map" maps a possibly virtual iterator to the real iterator.
1628 * That is, it maps the iterator used in "dom" and the domain of "sched"
1629 * to the iterator that some of the parameters in "scop" may refer to.
1631 * Any skip conditions within the loop have no effect outside of the loop.
1632 * The caller is responsible for making sure skip[pet_skip_later] has been
1633 * taken into account.
1635 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1636 __isl_take isl_map *sched, __isl_take isl_map *iv_map,
1637 __isl_take isl_id *id)
1639 int i;
1641 if (!scop)
1642 goto error;
1644 pet_scop_reset_skip(scop, pet_skip_now);
1645 pet_scop_reset_skip(scop, pet_skip_later);
1647 scop->context = context_embed(scop->context, dom, iv_map, id);
1648 if (!scop->context)
1649 goto error;
1651 for (i = 0; i < scop->n_stmt; ++i) {
1652 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1653 isl_set_copy(dom), isl_map_copy(sched),
1654 isl_map_copy(iv_map), isl_id_copy(id));
1655 if (!scop->stmts[i])
1656 goto error;
1659 for (i = 0; i < scop->n_array; ++i) {
1660 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1661 isl_set_copy(dom));
1662 if (!scop->arrays[i])
1663 goto error;
1666 isl_set_free(dom);
1667 isl_map_free(sched);
1668 isl_map_free(iv_map);
1669 isl_id_free(id);
1670 return scop;
1671 error:
1672 isl_set_free(dom);
1673 isl_map_free(sched);
1674 isl_map_free(iv_map);
1675 isl_id_free(id);
1676 return pet_scop_free(scop);
1679 /* Add extra conditions on the parameters to iteration domain of "stmt".
1681 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1682 __isl_take isl_set *cond)
1684 if (!stmt)
1685 goto error;
1687 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1689 return stmt;
1690 error:
1691 isl_set_free(cond);
1692 return pet_stmt_free(stmt);
1695 /* Add extra conditions to scop->skip[type].
1697 * The new skip condition only holds if it held before
1698 * and the condition is true. It does not hold if it did not hold
1699 * before or the condition is false.
1701 * The skip condition is assumed to be an affine expression.
1703 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1704 enum pet_skip type, __isl_keep isl_set *cond)
1706 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1707 isl_set *skip;
1708 isl_set *set;
1710 if (!scop)
1711 return NULL;
1712 if (!ext->skip[type])
1713 return scop;
1715 if (!set_is_affine(ext->skip[type]))
1716 isl_die(isl_set_get_ctx(ext->skip[type]), isl_error_internal,
1717 "can only resrict affine skips",
1718 return pet_scop_free(scop));
1720 skip = ext->skip[type];
1721 skip = isl_set_intersect_params(skip, isl_set_copy(cond));
1722 set = isl_set_from_params(isl_set_copy(cond));
1723 set = isl_set_complement(set);
1724 set = isl_set_add_dims(set, isl_dim_set, 1);
1725 set = isl_set_fix_si(set, isl_dim_set, 0, 0);
1726 skip = isl_set_union(skip, set);
1727 ext->skip[type] = skip;
1728 if (!ext->skip[type])
1729 return pet_scop_free(scop);
1731 return scop;
1734 /* Add extra conditions on the parameters to all iteration domains
1735 * and skip conditions.
1737 * A parameter value is valid for the result if it was valid
1738 * for the original scop and satisfies "cond" or if it does
1739 * not satisfy "cond" as in this case the scop is not executed
1740 * and the original constraints on the parameters are irrelevant.
1742 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1743 __isl_take isl_set *cond)
1745 int i;
1747 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1748 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1750 if (!scop)
1751 goto error;
1753 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1754 scop->context = isl_set_union(scop->context,
1755 isl_set_complement(isl_set_copy(cond)));
1756 scop->context = isl_set_coalesce(scop->context);
1757 scop->context = set_project_out_unnamed_params(scop->context);
1758 if (!scop->context)
1759 goto error;
1761 for (i = 0; i < scop->n_stmt; ++i) {
1762 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1763 isl_set_copy(cond));
1764 if (!scop->stmts[i])
1765 goto error;
1768 isl_set_free(cond);
1769 return scop;
1770 error:
1771 isl_set_free(cond);
1772 return pet_scop_free(scop);
1775 /* Construct a map that inserts a filter value with name "id" and value
1776 * "satisfied" in the list of filter values embedded in the set space "space".
1778 * If "space" does not contain any filter values yet, we first create
1779 * a map that inserts 0 filter values, i.e.,
1781 * space -> [space -> []]
1783 * We can now assume that space is of the form [dom -> [filters]]
1784 * We construct an identity mapping on dom and a mapping on filters
1785 * that inserts the new filter
1787 * dom -> dom
1788 * [filters] -> [satisfied, filters]
1790 * and then compute the cross product
1792 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1794 static __isl_give isl_map *insert_filter_map(__isl_take isl_space *space,
1795 __isl_take isl_id *id, int satisfied)
1797 isl_space *space2;
1798 isl_map *map, *map_dom, *map_ran;
1799 isl_set *dom;
1801 if (isl_space_is_wrapping(space)) {
1802 space2 = isl_space_map_from_set(isl_space_copy(space));
1803 map = isl_map_identity(space2);
1804 space = isl_space_unwrap(space);
1805 } else {
1806 space = isl_space_from_domain(space);
1807 map = isl_map_universe(isl_space_copy(space));
1808 map = isl_map_reverse(isl_map_domain_map(map));
1811 space2 = isl_space_domain(isl_space_copy(space));
1812 map_dom = isl_map_identity(isl_space_map_from_set(space2));
1813 space = isl_space_range(space);
1814 map_ran = isl_map_identity(isl_space_map_from_set(space));
1815 map_ran = isl_map_insert_dims(map_ran, isl_dim_out, 0, 1);
1816 map_ran = isl_map_set_dim_id(map_ran, isl_dim_out, 0, id);
1817 map_ran = isl_map_fix_si(map_ran, isl_dim_out, 0, satisfied);
1819 map = isl_map_apply_range(map, isl_map_product(map_dom, map_ran));
1821 return map;
1824 /* Insert an argument expression corresponding to "test" in front
1825 * of the list of arguments described by *n_arg and *args.
1827 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
1828 __isl_keep isl_map *test)
1830 int i;
1831 isl_ctx *ctx = isl_map_get_ctx(test);
1833 if (!test)
1834 return -1;
1836 if (!*args) {
1837 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
1838 if (!*args)
1839 return -1;
1840 } else {
1841 struct pet_expr **ext;
1842 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
1843 if (!ext)
1844 return -1;
1845 for (i = 0; i < *n_arg; ++i)
1846 ext[1 + i] = (*args)[i];
1847 free(*args);
1848 *args = ext;
1850 (*n_arg)++;
1851 (*args)[0] = pet_expr_from_access(isl_map_copy(test));
1852 if (!(*args)[0])
1853 return -1;
1855 return 0;
1858 /* Make the expression "expr" depend on the value of "test"
1859 * being equal to "satisfied".
1861 * If "test" is an affine expression, we simply add the conditions
1862 * on the expression have the value "satisfied" to all access relations.
1864 * Otherwise, we add a filter to "expr" (which is then assumed to be
1865 * an access expression) corresponding to "test" being equal to "satisfied".
1867 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
1868 __isl_take isl_map *test, int satisfied)
1870 isl_id *id;
1871 isl_ctx *ctx;
1872 isl_space *space;
1873 isl_map *map;
1875 if (!expr || !test)
1876 goto error;
1878 if (!isl_map_has_tuple_id(test, isl_dim_out)) {
1879 test = isl_map_fix_si(test, isl_dim_out, 0, satisfied);
1880 return pet_expr_restrict(expr, isl_map_params(test));
1883 ctx = isl_map_get_ctx(test);
1884 if (expr->type != pet_expr_access)
1885 isl_die(ctx, isl_error_invalid,
1886 "can only filter access expressions", goto error);
1888 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1889 id = isl_map_get_tuple_id(test, isl_dim_out);
1890 map = insert_filter_map(space, id, satisfied);
1892 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
1893 if (!expr->acc.access)
1894 goto error;
1896 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
1897 goto error;
1899 isl_map_free(test);
1900 return expr;
1901 error:
1902 isl_map_free(test);
1903 return pet_expr_free(expr);
1906 /* Make the statement "stmt" depend on the value of "test"
1907 * being equal to "satisfied" by adjusting stmt->domain.
1909 * The domain of "test" corresponds to the (zero or more) outer dimensions
1910 * of the iteration domain.
1912 * We insert an argument corresponding to a read to "test"
1913 * from the iteration domain of "stmt" in front of the list of arguments.
1914 * We also insert a corresponding output dimension in the wrapped
1915 * map contained in stmt->domain, with value set to "satisfied".
1917 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1918 __isl_take isl_map *test, int satisfied)
1920 int i;
1921 isl_id *id;
1922 isl_ctx *ctx;
1923 isl_map *map, *add_dom;
1924 isl_space *space;
1925 isl_set *dom;
1926 int n_test_dom;
1928 if (!stmt || !test)
1929 goto error;
1931 id = isl_map_get_tuple_id(test, isl_dim_out);
1932 map = insert_filter_map(isl_set_get_space(stmt->domain), id, satisfied);
1933 stmt->domain = isl_set_apply(stmt->domain, map);
1935 space = isl_space_unwrap(isl_set_get_space(stmt->domain));
1936 dom = isl_set_universe(isl_space_domain(space));
1937 n_test_dom = isl_map_dim(test, isl_dim_in);
1938 add_dom = isl_map_from_range(dom);
1939 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
1940 for (i = 0; i < n_test_dom; ++i)
1941 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
1942 isl_dim_out, i);
1943 test = isl_map_apply_domain(test, add_dom);
1945 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1946 goto error;
1948 isl_map_free(test);
1949 return stmt;
1950 error:
1951 isl_map_free(test);
1952 return pet_stmt_free(stmt);
1955 /* Does "scop" have a skip condition of the given "type"?
1957 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1959 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1961 if (!scop)
1962 return -1;
1963 return ext->skip[type] != NULL;
1966 /* Does "scop" have a skip condition of the given "type" that
1967 * is an affine expression?
1969 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1971 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1973 if (!scop)
1974 return -1;
1975 if (!ext->skip[type])
1976 return 0;
1977 return set_is_affine(ext->skip[type]);
1980 /* Does "scop" have a skip condition of the given "type" that
1981 * is not an affine expression?
1983 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1985 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1986 int aff;
1988 if (!scop)
1989 return -1;
1990 if (!ext->skip[type])
1991 return 0;
1992 aff = set_is_affine(ext->skip[type]);
1993 if (aff < 0)
1994 return -1;
1995 return !aff;
1998 /* Does "scop" have a skip condition of the given "type" that
1999 * is affine and holds on the entire domain?
2001 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2003 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2004 isl_set *set;
2005 int is_aff;
2006 int is_univ;
2008 is_aff = pet_scop_has_affine_skip(scop, type);
2009 if (is_aff < 0 || !is_aff)
2010 return is_aff;
2012 set = isl_set_copy(ext->skip[type]);
2013 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
2014 set = isl_set_params(set);
2015 is_univ = isl_set_plain_is_universe(set);
2016 isl_set_free(set);
2018 return is_univ;
2021 /* Replace scop->skip[type] by "skip".
2023 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2024 enum pet_skip type, __isl_take isl_set *skip)
2026 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2028 if (!scop || !skip)
2029 goto error;
2031 isl_set_free(ext->skip[type]);
2032 ext->skip[type] = skip;
2034 return scop;
2035 error:
2036 isl_set_free(skip);
2037 return pet_scop_free(scop);
2040 /* Return a copy of scop->skip[type].
2042 __isl_give isl_set *pet_scop_get_skip(struct pet_scop *scop,
2043 enum pet_skip type)
2045 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2047 if (!scop)
2048 return NULL;
2050 return isl_set_copy(ext->skip[type]);
2053 /* Return a map to the skip condition of the given type.
2055 __isl_give isl_map *pet_scop_get_skip_map(struct pet_scop *scop,
2056 enum pet_skip type)
2058 return isl_map_from_range(pet_scop_get_skip(scop, type));
2061 /* Return an access pet_expr corresponding to the skip condition
2062 * of the given type.
2064 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2065 enum pet_skip type)
2067 return pet_expr_from_access(pet_scop_get_skip_map(scop, type));
2070 /* Drop the the skip condition scop->skip[type].
2072 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2074 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2076 if (!scop)
2077 return;
2079 isl_set_free(ext->skip[type]);
2080 ext->skip[type] = NULL;
2083 /* Make the skip condition (if any) depend on the value of "test" being
2084 * equal to "satisfied".
2086 * We only support the case where the original skip condition is universal,
2087 * i.e., where skipping is unconditional, and where satisfied == 1.
2088 * In this case, the skip condition is changed to skip only when
2089 * "test" is equal to one.
2091 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2092 enum pet_skip type, __isl_keep isl_map *test, int satisfied)
2094 int is_univ = 0;
2096 if (!scop)
2097 return NULL;
2098 if (!pet_scop_has_skip(scop, type))
2099 return scop;
2101 if (satisfied)
2102 is_univ = pet_scop_has_universal_skip(scop, type);
2103 if (is_univ < 0)
2104 return pet_scop_free(scop);
2105 if (satisfied && is_univ) {
2106 scop = pet_scop_set_skip(scop, type,
2107 isl_map_range(isl_map_copy(test)));
2108 if (!scop)
2109 return NULL;
2110 } else {
2111 isl_die(isl_map_get_ctx(test), isl_error_internal,
2112 "skip expression cannot be filtered",
2113 return pet_scop_free(scop));
2116 return scop;
2119 /* Make all statements in "scop" depend on the value of "test"
2120 * being equal to "satisfied" by adjusting their domains.
2122 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2123 __isl_take isl_map *test, int satisfied)
2125 int i;
2127 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2128 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2130 if (!scop || !test)
2131 goto error;
2133 for (i = 0; i < scop->n_stmt; ++i) {
2134 scop->stmts[i] = stmt_filter(scop->stmts[i],
2135 isl_map_copy(test), satisfied);
2136 if (!scop->stmts[i])
2137 goto error;
2140 isl_map_free(test);
2141 return scop;
2142 error:
2143 isl_map_free(test);
2144 return pet_scop_free(scop);
2147 /* Do the filters "i" and "j" always have the same value?
2149 static int equal_filter_values(__isl_keep isl_set *domain, int i, int j)
2151 isl_map *map, *test;
2152 int equal;
2154 map = isl_set_unwrap(isl_set_copy(domain));
2155 test = isl_map_universe(isl_map_get_space(map));
2156 test = isl_map_equate(test, isl_dim_out, i, isl_dim_out, j);
2157 equal = isl_map_is_subset(map, test);
2158 isl_map_free(map);
2159 isl_map_free(test);
2161 return equal;
2164 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2165 * access relation, the union of the two access relations.
2167 static struct pet_stmt *merge_filter_pair(struct pet_stmt *stmt, int i, int j)
2169 int k;
2170 isl_map *map;
2172 if (!stmt)
2173 return NULL;
2175 stmt->args[i]->acc.access = isl_map_union(stmt->args[i]->acc.access,
2176 isl_map_copy(stmt->args[j]->acc.access));
2177 stmt->args[i]->acc.access = isl_map_coalesce(stmt->args[i]->acc.access);
2179 pet_expr_free(stmt->args[j]);
2180 for (k = j; k < stmt->n_arg - 1; ++k)
2181 stmt->args[k] = stmt->args[k + 1];
2182 stmt->n_arg--;
2184 map = isl_set_unwrap(stmt->domain);
2185 map = isl_map_project_out(map, isl_dim_out, j, 1);
2186 stmt->domain = isl_map_wrap(map);
2188 if (!stmt->domain || !stmt->args[i]->acc.access)
2189 return pet_stmt_free(stmt);
2191 return stmt;
2194 /* Look for any pair of filters that access the same filter variable
2195 * and that have the same filter value and merge them into a single
2196 * filter with as filter access relation the union of the filter access
2197 * relations.
2199 static struct pet_stmt *stmt_merge_filters(struct pet_stmt *stmt)
2201 int i, j;
2202 isl_space *space_i, *space_j;
2204 if (!stmt)
2205 return NULL;
2206 if (stmt->n_arg <= 1)
2207 return stmt;
2209 for (i = 0; i < stmt->n_arg - 1; ++i) {
2210 if (stmt->args[i]->type != pet_expr_access)
2211 continue;
2212 if (pet_expr_is_affine(stmt->args[i]))
2213 continue;
2215 space_i = isl_map_get_space(stmt->args[i]->acc.access);
2217 for (j = stmt->n_arg - 1; j > i; --j) {
2218 int eq;
2220 if (stmt->args[j]->type != pet_expr_access)
2221 continue;
2222 if (pet_expr_is_affine(stmt->args[j]))
2223 continue;
2225 space_j = isl_map_get_space(stmt->args[j]->acc.access);
2227 eq = isl_space_is_equal(space_i, space_j);
2228 if (eq >= 0 && eq)
2229 eq = equal_filter_values(stmt->domain, i, j);
2230 if (eq >= 0 && eq)
2231 stmt = merge_filter_pair(stmt, i, j);
2233 isl_space_free(space_j);
2235 if (eq < 0 || !stmt)
2236 break;
2239 isl_space_free(space_i);
2241 if (j > i || !stmt)
2242 return pet_stmt_free(stmt);
2245 return stmt;
2248 /* Look for any pair of filters that access the same filter variable
2249 * and that have the same filter value and merge them into a single
2250 * filter with as filter access relation the union of the filter access
2251 * relations.
2253 struct pet_scop *pet_scop_merge_filters(struct pet_scop *scop)
2255 int i;
2257 if (!scop)
2258 return NULL;
2260 for (i = 0; i < scop->n_stmt; ++i) {
2261 scop->stmts[i] = stmt_merge_filters(scop->stmts[i]);
2262 if (!scop->stmts[i])
2263 return pet_scop_free(scop);
2266 return scop;
2269 /* Add all parameters in "expr" to "dim" and return the result.
2271 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2272 __isl_take isl_space *dim)
2274 int i;
2276 if (!expr)
2277 goto error;
2278 for (i = 0; i < expr->n_arg; ++i)
2280 dim = expr_collect_params(expr->args[i], dim);
2282 if (expr->type == pet_expr_access)
2283 dim = isl_space_align_params(dim,
2284 isl_map_get_space(expr->acc.access));
2286 return dim;
2287 error:
2288 isl_space_free(dim);
2289 return pet_expr_free(expr);
2292 /* Add all parameters in "stmt" to "dim" and return the result.
2294 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2295 __isl_take isl_space *dim)
2297 if (!stmt)
2298 goto error;
2300 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2301 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2302 dim = expr_collect_params(stmt->body, dim);
2304 return dim;
2305 error:
2306 isl_space_free(dim);
2307 return pet_stmt_free(stmt);
2310 /* Add all parameters in "array" to "dim" and return the result.
2312 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2313 __isl_take isl_space *dim)
2315 if (!array)
2316 goto error;
2318 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2319 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2321 return dim;
2322 error:
2323 pet_array_free(array);
2324 return isl_space_free(dim);
2327 /* Add all parameters in "scop" to "dim" and return the result.
2329 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2330 __isl_take isl_space *dim)
2332 int i;
2334 if (!scop)
2335 goto error;
2337 for (i = 0; i < scop->n_array; ++i)
2338 dim = array_collect_params(scop->arrays[i], dim);
2340 for (i = 0; i < scop->n_stmt; ++i)
2341 dim = stmt_collect_params(scop->stmts[i], dim);
2343 return dim;
2344 error:
2345 isl_space_free(dim);
2346 return pet_scop_free(scop);
2349 /* Add all parameters in "dim" to all access relations in "expr".
2351 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2352 __isl_take isl_space *dim)
2354 int i;
2356 if (!expr)
2357 goto error;
2359 for (i = 0; i < expr->n_arg; ++i) {
2360 expr->args[i] =
2361 expr_propagate_params(expr->args[i],
2362 isl_space_copy(dim));
2363 if (!expr->args[i])
2364 goto error;
2367 if (expr->type == pet_expr_access) {
2368 expr->acc.access = isl_map_align_params(expr->acc.access,
2369 isl_space_copy(dim));
2370 if (!expr->acc.access)
2371 goto error;
2374 isl_space_free(dim);
2375 return expr;
2376 error:
2377 isl_space_free(dim);
2378 return pet_expr_free(expr);
2381 /* Add all parameters in "dim" to the domain, schedule and
2382 * all access relations in "stmt".
2384 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2385 __isl_take isl_space *dim)
2387 if (!stmt)
2388 goto error;
2390 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2391 stmt->schedule = isl_map_align_params(stmt->schedule,
2392 isl_space_copy(dim));
2393 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2395 if (!stmt->domain || !stmt->schedule || !stmt->body)
2396 goto error;
2398 isl_space_free(dim);
2399 return stmt;
2400 error:
2401 isl_space_free(dim);
2402 return pet_stmt_free(stmt);
2405 /* Add all parameters in "dim" to "array".
2407 static struct pet_array *array_propagate_params(struct pet_array *array,
2408 __isl_take isl_space *dim)
2410 if (!array)
2411 goto error;
2413 array->context = isl_set_align_params(array->context,
2414 isl_space_copy(dim));
2415 array->extent = isl_set_align_params(array->extent,
2416 isl_space_copy(dim));
2417 if (array->value_bounds) {
2418 array->value_bounds = isl_set_align_params(array->value_bounds,
2419 isl_space_copy(dim));
2420 if (!array->value_bounds)
2421 goto error;
2424 if (!array->context || !array->extent)
2425 goto error;
2427 isl_space_free(dim);
2428 return array;
2429 error:
2430 isl_space_free(dim);
2431 return pet_array_free(array);
2434 /* Add all parameters in "dim" to "scop".
2436 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2437 __isl_take isl_space *dim)
2439 int i;
2441 if (!scop)
2442 goto error;
2444 for (i = 0; i < scop->n_array; ++i) {
2445 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2446 isl_space_copy(dim));
2447 if (!scop->arrays[i])
2448 goto error;
2451 for (i = 0; i < scop->n_stmt; ++i) {
2452 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2453 isl_space_copy(dim));
2454 if (!scop->stmts[i])
2455 goto error;
2458 isl_space_free(dim);
2459 return scop;
2460 error:
2461 isl_space_free(dim);
2462 return pet_scop_free(scop);
2465 /* Update all isl_sets and isl_maps in "scop" such that they all
2466 * have the same parameters.
2468 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2470 isl_space *dim;
2472 if (!scop)
2473 return NULL;
2475 dim = isl_set_get_space(scop->context);
2476 dim = scop_collect_params(scop, dim);
2478 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
2479 scop = scop_propagate_params(scop, dim);
2481 return scop;
2484 /* Check if the given access relation accesses a (0D) array that corresponds
2485 * to one of the parameters in "dim". If so, replace the array access
2486 * by an access to the set of integers with as index (and value)
2487 * that parameter.
2489 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
2490 __isl_take isl_space *dim)
2492 isl_id *array_id = NULL;
2493 int pos = -1;
2495 if (isl_map_has_tuple_id(access, isl_dim_out)) {
2496 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2497 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
2499 isl_space_free(dim);
2501 if (pos < 0) {
2502 isl_id_free(array_id);
2503 return access;
2506 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
2507 if (pos < 0) {
2508 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
2509 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
2510 pos = 0;
2511 } else
2512 isl_id_free(array_id);
2514 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2515 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
2517 return access;
2520 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2521 * in "dim" by a value equal to the corresponding parameter.
2523 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
2524 __isl_take isl_space *dim)
2526 int i;
2528 if (!expr)
2529 goto error;
2531 for (i = 0; i < expr->n_arg; ++i) {
2532 expr->args[i] =
2533 expr_detect_parameter_accesses(expr->args[i],
2534 isl_space_copy(dim));
2535 if (!expr->args[i])
2536 goto error;
2539 if (expr->type == pet_expr_access) {
2540 expr->acc.access = access_detect_parameter(expr->acc.access,
2541 isl_space_copy(dim));
2542 if (!expr->acc.access)
2543 goto error;
2546 isl_space_free(dim);
2547 return expr;
2548 error:
2549 isl_space_free(dim);
2550 return pet_expr_free(expr);
2553 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2554 * in "dim" by a value equal to the corresponding parameter.
2556 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2557 __isl_take isl_space *dim)
2559 if (!stmt)
2560 goto error;
2562 stmt->body = expr_detect_parameter_accesses(stmt->body,
2563 isl_space_copy(dim));
2565 if (!stmt->domain || !stmt->schedule || !stmt->body)
2566 goto error;
2568 isl_space_free(dim);
2569 return stmt;
2570 error:
2571 isl_space_free(dim);
2572 return pet_stmt_free(stmt);
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_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2579 __isl_take isl_space *dim)
2581 int i;
2583 if (!scop)
2584 goto error;
2586 for (i = 0; i < scop->n_stmt; ++i) {
2587 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2588 isl_space_copy(dim));
2589 if (!scop->stmts[i])
2590 goto error;
2593 isl_space_free(dim);
2594 return scop;
2595 error:
2596 isl_space_free(dim);
2597 return pet_scop_free(scop);
2600 /* Replace all accesses to (0D) arrays that correspond to any of
2601 * the parameters used in "scop" by a value equal
2602 * to the corresponding parameter.
2604 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2606 isl_space *dim;
2608 if (!scop)
2609 return NULL;
2611 dim = isl_set_get_space(scop->context);
2612 dim = scop_collect_params(scop, dim);
2614 scop = scop_detect_parameter_accesses(scop, dim);
2616 return scop;
2619 /* Add all read access relations (if "read" is set) and/or all write
2620 * access relations (if "write" is set) to "accesses" and return the result.
2622 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
2623 int read, int write, __isl_take isl_union_map *accesses)
2625 int i;
2626 isl_id *id;
2627 isl_space *dim;
2629 if (!expr)
2630 return NULL;
2632 for (i = 0; i < expr->n_arg; ++i)
2633 accesses = expr_collect_accesses(expr->args[i],
2634 read, write, accesses);
2636 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
2637 ((read && expr->acc.read) || (write && expr->acc.write)))
2638 accesses = isl_union_map_add_map(accesses,
2639 isl_map_copy(expr->acc.access));
2641 return accesses;
2644 /* Collect and return all read access relations (if "read" is set)
2645 * and/or all write access relations (if "write" is set) in "stmt".
2647 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2648 int read, int write, __isl_take isl_space *dim)
2650 isl_union_map *accesses;
2652 if (!stmt)
2653 return NULL;
2655 accesses = isl_union_map_empty(dim);
2656 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
2657 accesses = isl_union_map_intersect_domain(accesses,
2658 isl_union_set_from_set(isl_set_copy(stmt->domain)));
2660 return accesses;
2663 /* Collect and return all read access relations (if "read" is set)
2664 * and/or all write access relations (if "write" is set) in "scop".
2666 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2667 int read, int write)
2669 int i;
2670 isl_union_map *accesses;
2672 if (!scop)
2673 return NULL;
2675 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2677 for (i = 0; i < scop->n_stmt; ++i) {
2678 isl_union_map *accesses_i;
2679 isl_space *dim = isl_set_get_space(scop->context);
2680 accesses_i = stmt_collect_accesses(scop->stmts[i],
2681 read, write, dim);
2682 accesses = isl_union_map_union(accesses, accesses_i);
2685 return accesses;
2688 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
2690 return scop_collect_accesses(scop, 1, 0);
2693 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
2695 return scop_collect_accesses(scop, 0, 1);
2698 /* Collect and return the union of iteration domains in "scop".
2700 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2702 int i;
2703 isl_set *domain_i;
2704 isl_union_set *domain;
2706 if (!scop)
2707 return NULL;
2709 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2711 for (i = 0; i < scop->n_stmt; ++i) {
2712 domain_i = isl_set_copy(scop->stmts[i]->domain);
2713 domain = isl_union_set_add_set(domain, domain_i);
2716 return domain;
2719 /* Collect and return the schedules of the statements in "scop".
2720 * The range is normalized to the maximal number of scheduling
2721 * dimensions.
2723 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2725 int i, j;
2726 isl_map *schedule_i;
2727 isl_union_map *schedule;
2728 int depth, max_depth = 0;
2730 if (!scop)
2731 return NULL;
2733 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2735 for (i = 0; i < scop->n_stmt; ++i) {
2736 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2737 if (depth > max_depth)
2738 max_depth = depth;
2741 for (i = 0; i < scop->n_stmt; ++i) {
2742 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2743 depth = isl_map_dim(schedule_i, isl_dim_out);
2744 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2745 max_depth - depth);
2746 for (j = depth; j < max_depth; ++j)
2747 schedule_i = isl_map_fix_si(schedule_i,
2748 isl_dim_out, j, 0);
2749 schedule = isl_union_map_add_map(schedule, schedule_i);
2752 return schedule;
2755 /* Does expression "expr" write to "id"?
2757 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
2759 int i;
2760 isl_id *write_id;
2762 for (i = 0; i < expr->n_arg; ++i) {
2763 int writes = expr_writes(expr->args[i], id);
2764 if (writes < 0 || writes)
2765 return writes;
2768 if (expr->type != pet_expr_access)
2769 return 0;
2770 if (!expr->acc.write)
2771 return 0;
2772 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
2773 return 0;
2775 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
2776 isl_id_free(write_id);
2778 if (!write_id)
2779 return -1;
2781 return write_id == id;
2784 /* Does statement "stmt" write to "id"?
2786 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
2788 return expr_writes(stmt->body, id);
2791 /* Is there any write access in "scop" that accesses "id"?
2793 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
2795 int i;
2797 if (!scop)
2798 return -1;
2800 for (i = 0; i < scop->n_stmt; ++i) {
2801 int writes = stmt_writes(scop->stmts[i], id);
2802 if (writes < 0 || writes)
2803 return writes;
2806 return 0;
2809 /* Add a reference identifier to access expression "expr".
2810 * "user" points to an integer that contains the sequence number
2811 * of the next reference.
2813 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
2815 isl_ctx *ctx;
2816 char name[50];
2817 int *n_ref = user;
2819 if (!expr)
2820 return expr;
2822 ctx = isl_map_get_ctx(expr->acc.access);
2823 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
2824 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
2825 if (!expr->acc.ref_id)
2826 return pet_expr_free(expr);
2828 return expr;
2831 /* Add a reference identifier to all access expressions in "stmt".
2832 * "n_ref" points to an integer that contains the sequence number
2833 * of the next reference.
2835 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2837 int i;
2839 if (!stmt)
2840 return NULL;
2842 for (i = 0; i < stmt->n_arg; ++i) {
2843 stmt->args[i] = pet_expr_map_access(stmt->args[i],
2844 &access_add_ref_id, n_ref);
2845 if (!stmt->args[i])
2846 return pet_stmt_free(stmt);
2849 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
2850 if (!stmt->body)
2851 return pet_stmt_free(stmt);
2853 return stmt;
2856 /* Add a reference identifier to all access expressions in "scop".
2858 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2860 int i;
2861 int n_ref;
2863 if (!scop)
2864 return NULL;
2866 n_ref = 0;
2867 for (i = 0; i < scop->n_stmt; ++i) {
2868 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2869 if (!scop->stmts[i])
2870 return pet_scop_free(scop);
2873 return scop;
2876 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2878 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
2880 int i, n;
2882 n = isl_set_dim(set, isl_dim_param);
2883 for (i = 0; i < n; ++i) {
2884 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2885 const char *name = isl_id_get_name(id);
2886 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
2887 isl_id_free(id);
2890 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
2891 isl_id *id = isl_set_get_tuple_id(set);
2892 const char *name = isl_id_get_name(id);
2893 set = isl_set_set_tuple_name(set, name);
2894 isl_id_free(id);
2897 return set;
2900 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2902 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
2904 int i, n;
2906 n = isl_map_dim(map, isl_dim_param);
2907 for (i = 0; i < n; ++i) {
2908 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2909 const char *name = isl_id_get_name(id);
2910 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2911 isl_id_free(id);
2914 if (isl_map_has_tuple_id(map, isl_dim_in)) {
2915 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
2916 const char *name = isl_id_get_name(id);
2917 map = isl_map_set_tuple_name(map, isl_dim_in, name);
2918 isl_id_free(id);
2921 if (isl_map_has_tuple_id(map, isl_dim_out)) {
2922 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
2923 const char *name = isl_id_get_name(id);
2924 map = isl_map_set_tuple_name(map, isl_dim_out, name);
2925 isl_id_free(id);
2928 return map;
2931 /* Reset the user pointer on all parameter ids in "array".
2933 static struct pet_array *array_anonymize(struct pet_array *array)
2935 if (!array)
2936 return NULL;
2938 array->context = set_anonymize(array->context);
2939 array->extent = set_anonymize(array->extent);
2940 if (!array->context || !array->extent)
2941 return pet_array_free(array);
2943 return array;
2946 /* Reset the user pointer on all parameter and tuple ids in
2947 * the access relation of the access expression "expr".
2949 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
2951 expr->acc.access = map_anonymize(expr->acc.access);
2952 if (!expr->acc.access)
2953 return pet_expr_free(expr);
2955 return expr;
2958 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2960 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2962 int i;
2963 isl_space *space;
2964 isl_set *domain;
2966 if (!stmt)
2967 return NULL;
2969 stmt->domain = set_anonymize(stmt->domain);
2970 stmt->schedule = map_anonymize(stmt->schedule);
2971 if (!stmt->domain || !stmt->schedule)
2972 return pet_stmt_free(stmt);
2974 for (i = 0; i < stmt->n_arg; ++i) {
2975 stmt->args[i] = pet_expr_map_access(stmt->args[i],
2976 &access_anonymize, NULL);
2977 if (!stmt->args[i])
2978 return pet_stmt_free(stmt);
2981 stmt->body = pet_expr_map_access(stmt->body,
2982 &access_anonymize, NULL);
2983 if (!stmt->body)
2984 return pet_stmt_free(stmt);
2986 return stmt;
2989 /* Reset the user pointer on all parameter and tuple ids in "scop".
2991 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2993 int i;
2995 if (!scop)
2996 return NULL;
2998 scop->context = set_anonymize(scop->context);
2999 scop->context_value = set_anonymize(scop->context_value);
3000 if (!scop->context || !scop->context_value)
3001 return pet_scop_free(scop);
3003 for (i = 0; i < scop->n_array; ++i) {
3004 scop->arrays[i] = array_anonymize(scop->arrays[i]);
3005 if (!scop->arrays[i])
3006 return pet_scop_free(scop);
3009 for (i = 0; i < scop->n_stmt; ++i) {
3010 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
3011 if (!scop->stmts[i])
3012 return pet_scop_free(scop);
3015 return scop;
3018 /* Given a set "domain", return a wrapped relation with the given set
3019 * as domain and a range of dimension "n_arg", where each coordinate
3020 * is either unbounded or, if the corresponding element of args is of
3021 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
3023 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
3024 unsigned n_arg, struct pet_expr **args,
3025 __isl_keep isl_union_map *value_bounds)
3027 int i;
3028 isl_map *map;
3029 isl_space *space;
3030 isl_ctx *ctx = isl_set_get_ctx(domain);
3032 map = isl_map_from_domain(domain);
3033 space = isl_map_get_space(map);
3034 space = isl_space_add_dims(space, isl_dim_out, 1);
3036 for (i = 0; i < n_arg; ++i) {
3037 isl_map *map_i;
3038 struct pet_expr *arg = args[i];
3039 isl_id *id;
3040 isl_space *space2;
3042 map_i = isl_map_universe(isl_space_copy(space));
3043 if (arg->type == pet_expr_access) {
3044 isl_map *vb;
3045 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
3046 space2 = isl_space_alloc(ctx, 0, 0, 1);
3047 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
3048 vb = isl_union_map_extract_map(value_bounds, space2);
3049 if (!isl_map_plain_is_empty(vb))
3050 map_i = isl_map_intersect_range(map_i,
3051 isl_map_range(vb));
3052 else
3053 isl_map_free(vb);
3055 map = isl_map_flat_range_product(map, map_i);
3057 isl_space_free(space);
3059 return isl_map_wrap(map);
3062 /* Data used in access_gist() callback.
3064 struct pet_access_gist_data {
3065 isl_set *domain;
3066 isl_union_map *value_bounds;
3069 /* Given an expression "expr" of type pet_expr_access, compute
3070 * the gist of the associated access relation with respect to
3071 * data->domain and the bounds on the values of the arguments
3072 * of the expression.
3074 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
3076 struct pet_access_gist_data *data = user;
3077 isl_set *domain;
3079 domain = isl_set_copy(data->domain);
3080 if (expr->n_arg > 0)
3081 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
3082 data->value_bounds);
3084 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
3085 if (!expr->acc.access)
3086 return pet_expr_free(expr);
3088 return expr;
3091 /* Compute the gist of the iteration domain and all access relations
3092 * of "stmt" based on the constraints on the parameters specified by "context"
3093 * and the constraints on the values of nested accesses specified
3094 * by "value_bounds".
3096 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3097 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3099 int i;
3100 isl_space *space;
3101 isl_set *domain;
3102 struct pet_access_gist_data data;
3104 if (!stmt)
3105 return NULL;
3107 data.domain = isl_set_copy(stmt->domain);
3108 data.value_bounds = value_bounds;
3109 if (stmt->n_arg > 0)
3110 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3112 data.domain = isl_set_intersect_params(data.domain,
3113 isl_set_copy(context));
3115 for (i = 0; i < stmt->n_arg; ++i) {
3116 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3117 &access_gist, &data);
3118 if (!stmt->args[i])
3119 goto error;
3122 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
3123 if (!stmt->body)
3124 goto error;
3126 isl_set_free(data.domain);
3128 space = isl_set_get_space(stmt->domain);
3129 if (isl_space_is_wrapping(space))
3130 space = isl_space_domain(isl_space_unwrap(space));
3131 domain = isl_set_universe(space);
3132 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3133 if (stmt->n_arg > 0)
3134 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3135 value_bounds);
3136 stmt->domain = isl_set_gist(stmt->domain, domain);
3137 if (!stmt->domain)
3138 return pet_stmt_free(stmt);
3140 return stmt;
3141 error:
3142 isl_set_free(data.domain);
3143 return pet_stmt_free(stmt);
3146 /* Compute the gist of the extent of the array
3147 * based on the constraints on the parameters specified by "context".
3149 static struct pet_array *array_gist(struct pet_array *array,
3150 __isl_keep isl_set *context)
3152 if (!array)
3153 return NULL;
3155 array->extent = isl_set_gist_params(array->extent,
3156 isl_set_copy(context));
3157 if (!array->extent)
3158 return pet_array_free(array);
3160 return array;
3163 /* Compute the gist of all sets and relations in "scop"
3164 * based on the constraints on the parameters specified by "scop->context"
3165 * and the constraints on the values of nested accesses specified
3166 * by "value_bounds".
3168 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3169 __isl_keep isl_union_map *value_bounds)
3171 int i;
3173 if (!scop)
3174 return NULL;
3176 scop->context = isl_set_coalesce(scop->context);
3177 if (!scop->context)
3178 return pet_scop_free(scop);
3180 for (i = 0; i < scop->n_array; ++i) {
3181 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3182 if (!scop->arrays[i])
3183 return pet_scop_free(scop);
3186 for (i = 0; i < scop->n_stmt; ++i) {
3187 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3188 value_bounds);
3189 if (!scop->stmts[i])
3190 return pet_scop_free(scop);
3193 return scop;
3196 /* Intersect the context of "scop" with "context".
3197 * To ensure that we don't introduce any unnamed parameters in
3198 * the context of "scop", we first remove the unnamed parameters
3199 * from "context".
3201 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3202 __isl_take isl_set *context)
3204 if (!scop)
3205 goto error;
3207 context = set_project_out_unnamed_params(context);
3208 scop->context = isl_set_intersect(scop->context, context);
3209 if (!scop->context)
3210 return pet_scop_free(scop);
3212 return scop;
3213 error:
3214 isl_set_free(context);
3215 return pet_scop_free(scop);
3218 /* Drop the current context of "scop". That is, replace the context
3219 * by a universal set.
3221 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3223 isl_space *space;
3225 if (!scop)
3226 return NULL;
3228 space = isl_set_get_space(scop->context);
3229 isl_set_free(scop->context);
3230 scop->context = isl_set_universe(space);
3231 if (!scop->context)
3232 return pet_scop_free(scop);
3234 return scop;
3237 /* Append "array" to the arrays of "scop".
3239 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3240 struct pet_array *array)
3242 isl_ctx *ctx;
3243 struct pet_array **arrays;
3245 if (!array || !scop)
3246 goto error;
3248 ctx = isl_set_get_ctx(scop->context);
3249 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3250 scop->n_array + 1);
3251 if (!arrays)
3252 goto error;
3253 scop->arrays = arrays;
3254 scop->arrays[scop->n_array] = array;
3255 scop->n_array++;
3257 return scop;
3258 error:
3259 pet_array_free(array);
3260 return pet_scop_free(scop);