update isl to version 0.11.2
[pet.git] / scop.c
blobc334d06417548861a23dd26b8013d6868f64a698
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <string.h>
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
39 #include "scop.h"
41 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
43 static char *type_str[] = {
44 [pet_expr_access] = "access",
45 [pet_expr_call] = "call",
46 [pet_expr_cast] = "cast",
47 [pet_expr_double] = "double",
48 [pet_expr_unary] = "unary",
49 [pet_expr_binary] = "binary",
50 [pet_expr_ternary] = "ternary"
53 static char *op_str[] = {
54 [pet_op_add_assign] = "+=",
55 [pet_op_sub_assign] = "-=",
56 [pet_op_mul_assign] = "*=",
57 [pet_op_div_assign] = "/=",
58 [pet_op_assign] = "=",
59 [pet_op_add] = "+",
60 [pet_op_sub] = "-",
61 [pet_op_mul] = "*",
62 [pet_op_div] = "/",
63 [pet_op_mod] = "%",
64 [pet_op_eq] = "==",
65 [pet_op_le] = "<=",
66 [pet_op_lt] = "<",
67 [pet_op_gt] = ">",
68 [pet_op_minus] = "-",
69 [pet_op_post_inc] = "++",
70 [pet_op_post_dec] = "--",
71 [pet_op_pre_inc] = "++",
72 [pet_op_pre_dec] = "--",
73 [pet_op_address_of] = "&",
74 [pet_op_kill] = "kill"
77 /* pet_scop with extra information that is only used during parsing.
79 * In particular, we keep track of conditions under which we want
80 * to skip the rest of the current loop iteration (skip[pet_skip_now])
81 * and of conditions under which we want to skip subsequent
82 * loop iterations (skip[pet_skip_later]).
84 * The conditions are represented either by a variable, which
85 * is assumed to attain values zero and one, or by a boolean affine
86 * expression. The condition holds if the variable has value one
87 * or if the affine expression has value one (typically for only
88 * part of the parameter space).
90 * A missing condition (skip[type] == NULL) means that we don't want
91 * to skip anything.
93 struct pet_scop_ext {
94 struct pet_scop scop;
96 isl_set *skip[2];
99 const char *pet_op_str(enum pet_op_type op)
101 return op_str[op];
104 int pet_op_is_inc_dec(enum pet_op_type op)
106 return op == pet_op_post_inc || op == pet_op_post_dec ||
107 op == pet_op_pre_inc || op == pet_op_pre_dec;
110 const char *pet_type_str(enum pet_expr_type type)
112 return type_str[type];
115 enum pet_op_type pet_str_op(const char *str)
117 int i;
119 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
120 if (!strcmp(op_str[i], str))
121 return i;
123 return -1;
126 enum pet_expr_type pet_str_type(const char *str)
128 int i;
130 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
131 if (!strcmp(type_str[i], str))
132 return i;
134 return -1;
137 /* Construct a pet_expr from an access relation.
138 * By default, it is considered to be a read access.
140 struct pet_expr *pet_expr_from_access(__isl_take isl_map *access)
142 isl_ctx *ctx = isl_map_get_ctx(access);
143 struct pet_expr *expr;
145 if (!access)
146 return NULL;
147 expr = isl_calloc_type(ctx, struct pet_expr);
148 if (!expr)
149 goto error;
151 expr->type = pet_expr_access;
152 expr->acc.access = access;
153 expr->acc.read = 1;
154 expr->acc.write = 0;
156 return expr;
157 error:
158 isl_map_free(access);
159 return NULL;
162 /* Construct a pet_expr that kills the elements specified by "access".
164 struct pet_expr *pet_expr_kill_from_access(__isl_take isl_map *access)
166 isl_ctx *ctx;
167 struct pet_expr *expr;
169 ctx = isl_map_get_ctx(access);
170 expr = pet_expr_from_access(access);
171 if (!expr)
172 return NULL;
173 expr->acc.read = 0;
174 return pet_expr_new_unary(ctx, pet_op_kill, expr);
177 /* Construct a unary pet_expr that performs "op" on "arg".
179 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
180 struct pet_expr *arg)
182 struct pet_expr *expr;
184 if (!arg)
185 goto error;
186 expr = isl_alloc_type(ctx, struct pet_expr);
187 if (!expr)
188 goto error;
190 expr->type = pet_expr_unary;
191 expr->op = op;
192 expr->n_arg = 1;
193 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
194 if (!expr->args)
195 goto error;
196 expr->args[pet_un_arg] = arg;
198 return expr;
199 error:
200 pet_expr_free(arg);
201 return NULL;
204 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
206 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
207 struct pet_expr *lhs, struct pet_expr *rhs)
209 struct pet_expr *expr;
211 if (!lhs || !rhs)
212 goto error;
213 expr = isl_alloc_type(ctx, struct pet_expr);
214 if (!expr)
215 goto error;
217 expr->type = pet_expr_binary;
218 expr->op = op;
219 expr->n_arg = 2;
220 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
221 if (!expr->args)
222 goto error;
223 expr->args[pet_bin_lhs] = lhs;
224 expr->args[pet_bin_rhs] = rhs;
226 return expr;
227 error:
228 pet_expr_free(lhs);
229 pet_expr_free(rhs);
230 return NULL;
233 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
235 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
236 struct pet_expr *lhs, struct pet_expr *rhs)
238 struct pet_expr *expr;
240 if (!cond || !lhs || !rhs)
241 goto error;
242 expr = isl_alloc_type(ctx, struct pet_expr);
243 if (!expr)
244 goto error;
246 expr->type = pet_expr_ternary;
247 expr->n_arg = 3;
248 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
249 if (!expr->args)
250 goto error;
251 expr->args[pet_ter_cond] = cond;
252 expr->args[pet_ter_true] = lhs;
253 expr->args[pet_ter_false] = rhs;
255 return expr;
256 error:
257 pet_expr_free(cond);
258 pet_expr_free(lhs);
259 pet_expr_free(rhs);
260 return NULL;
263 /* Construct a call pet_expr that calls function "name" with "n_arg"
264 * arguments. The caller is responsible for filling in the arguments.
266 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
267 unsigned n_arg)
269 struct pet_expr *expr;
271 expr = isl_alloc_type(ctx, struct pet_expr);
272 if (!expr)
273 return NULL;
275 expr->type = pet_expr_call;
276 expr->n_arg = n_arg;
277 expr->name = strdup(name);
278 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
279 if (!expr->name || !expr->args)
280 return pet_expr_free(expr);
282 return expr;
285 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
287 struct pet_expr *pet_expr_new_cast(isl_ctx *ctx, const char *type_name,
288 struct pet_expr *arg)
290 struct pet_expr *expr;
292 if (!arg)
293 return NULL;
295 expr = isl_alloc_type(ctx, struct pet_expr);
296 if (!expr)
297 goto error;
299 expr->type = pet_expr_cast;
300 expr->n_arg = 1;
301 expr->type_name = strdup(type_name);
302 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
303 if (!expr->type_name || !expr->args)
304 goto error;
306 expr->args[0] = arg;
308 return expr;
309 error:
310 pet_expr_free(arg);
311 pet_expr_free(expr);
312 return NULL;
315 /* Construct a pet_expr that represents the double "d".
317 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
319 struct pet_expr *expr;
321 expr = isl_calloc_type(ctx, struct pet_expr);
322 if (!expr)
323 return NULL;
325 expr->type = pet_expr_double;
326 expr->d.val = val;
327 expr->d.s = strdup(s);
328 if (!expr->d.s)
329 return pet_expr_free(expr);
331 return expr;
334 void *pet_expr_free(struct pet_expr *expr)
336 int i;
338 if (!expr)
339 return NULL;
341 for (i = 0; i < expr->n_arg; ++i)
342 pet_expr_free(expr->args[i]);
343 free(expr->args);
345 switch (expr->type) {
346 case pet_expr_access:
347 isl_map_free(expr->acc.access);
348 break;
349 case pet_expr_call:
350 free(expr->name);
351 break;
352 case pet_expr_cast:
353 free(expr->type_name);
354 break;
355 case pet_expr_double:
356 free(expr->d.s);
357 break;
358 case pet_expr_unary:
359 case pet_expr_binary:
360 case pet_expr_ternary:
361 break;
364 free(expr);
365 return NULL;
368 static void expr_dump(struct pet_expr *expr, int indent)
370 int i;
372 if (!expr)
373 return;
375 fprintf(stderr, "%*s", indent, "");
377 switch (expr->type) {
378 case pet_expr_double:
379 fprintf(stderr, "%s\n", expr->d.s);
380 break;
381 case pet_expr_access:
382 isl_map_dump(expr->acc.access);
383 fprintf(stderr, "%*sread: %d\n", indent + 2,
384 "", expr->acc.read);
385 fprintf(stderr, "%*swrite: %d\n", indent + 2,
386 "", expr->acc.write);
387 for (i = 0; i < expr->n_arg; ++i)
388 expr_dump(expr->args[i], indent + 2);
389 break;
390 case pet_expr_unary:
391 fprintf(stderr, "%s\n", op_str[expr->op]);
392 expr_dump(expr->args[pet_un_arg], indent + 2);
393 break;
394 case pet_expr_binary:
395 fprintf(stderr, "%s\n", op_str[expr->op]);
396 expr_dump(expr->args[pet_bin_lhs], indent + 2);
397 expr_dump(expr->args[pet_bin_rhs], indent + 2);
398 break;
399 case pet_expr_ternary:
400 fprintf(stderr, "?:\n");
401 expr_dump(expr->args[pet_ter_cond], indent + 2);
402 expr_dump(expr->args[pet_ter_true], indent + 2);
403 expr_dump(expr->args[pet_ter_false], indent + 2);
404 break;
405 case pet_expr_call:
406 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
407 for (i = 0; i < expr->n_arg; ++i)
408 expr_dump(expr->args[i], indent + 2);
409 break;
410 case pet_expr_cast:
411 fprintf(stderr, "(%s)\n", expr->type_name);
412 for (i = 0; i < expr->n_arg; ++i)
413 expr_dump(expr->args[i], indent + 2);
414 break;
418 void pet_expr_dump(struct pet_expr *expr)
420 expr_dump(expr, 0);
423 /* Does "expr" represent an access to an unnamed space, i.e.,
424 * does it represent an affine expression?
426 int pet_expr_is_affine(struct pet_expr *expr)
428 int has_id;
430 if (!expr)
431 return -1;
432 if (expr->type != pet_expr_access)
433 return 0;
435 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
436 if (has_id < 0)
437 return -1;
439 return !has_id;
442 /* Return 1 if the two pet_exprs are equivalent.
444 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
446 int i;
448 if (!expr1 || !expr2)
449 return 0;
451 if (expr1->type != expr2->type)
452 return 0;
453 if (expr1->n_arg != expr2->n_arg)
454 return 0;
455 for (i = 0; i < expr1->n_arg; ++i)
456 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
457 return 0;
458 switch (expr1->type) {
459 case pet_expr_double:
460 if (strcmp(expr1->d.s, expr2->d.s))
461 return 0;
462 if (expr1->d.val != expr2->d.val)
463 return 0;
464 break;
465 case pet_expr_access:
466 if (expr1->acc.read != expr2->acc.read)
467 return 0;
468 if (expr1->acc.write != expr2->acc.write)
469 return 0;
470 if (!expr1->acc.access || !expr2->acc.access)
471 return 0;
472 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
473 return 0;
474 break;
475 case pet_expr_unary:
476 case pet_expr_binary:
477 case pet_expr_ternary:
478 if (expr1->op != expr2->op)
479 return 0;
480 break;
481 case pet_expr_call:
482 if (strcmp(expr1->name, expr2->name))
483 return 0;
484 break;
485 case pet_expr_cast:
486 if (strcmp(expr1->type_name, expr2->type_name))
487 return 0;
488 break;
491 return 1;
494 /* Add extra conditions on the parameters to all access relations in "expr".
496 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
497 __isl_take isl_set *cond)
499 int i;
501 if (!expr)
502 goto error;
504 for (i = 0; i < expr->n_arg; ++i) {
505 expr->args[i] = pet_expr_restrict(expr->args[i],
506 isl_set_copy(cond));
507 if (!expr->args[i])
508 goto error;
511 if (expr->type == pet_expr_access) {
512 expr->acc.access = isl_map_intersect_params(expr->acc.access,
513 isl_set_copy(cond));
514 if (!expr->acc.access)
515 goto error;
518 isl_set_free(cond);
519 return expr;
520 error:
521 isl_set_free(cond);
522 return pet_expr_free(expr);
525 /* Modify all access relations in "expr" by calling "fn" on them.
527 struct pet_expr *pet_expr_foreach_access(struct pet_expr *expr,
528 __isl_give isl_map *(*fn)(__isl_take isl_map *access, void *user),
529 void *user)
531 int i;
533 if (!expr)
534 return NULL;
536 for (i = 0; i < expr->n_arg; ++i) {
537 expr->args[i] = pet_expr_foreach_access(expr->args[i], fn, user);
538 if (!expr->args[i])
539 return pet_expr_free(expr);
542 if (expr->type == pet_expr_access) {
543 expr->acc.access = fn(expr->acc.access, user);
544 if (!expr->acc.access)
545 return pet_expr_free(expr);
548 return expr;
551 /* Modify all expressions of type pet_expr_access in "expr"
552 * by calling "fn" on them.
554 struct pet_expr *pet_expr_foreach_access_expr(struct pet_expr *expr,
555 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
556 void *user)
558 int i;
560 if (!expr)
561 return NULL;
563 for (i = 0; i < expr->n_arg; ++i) {
564 expr->args[i] = pet_expr_foreach_access_expr(expr->args[i],
565 fn, user);
566 if (!expr->args[i])
567 return pet_expr_free(expr);
570 if (expr->type == pet_expr_access)
571 expr = fn(expr, user);
573 return expr;
576 /* Modify the given access relation based on the given iteration space
577 * transformation.
578 * If the access has any arguments then the domain of the access relation
579 * is a wrapped mapping from the iteration space to the space of
580 * argument values. We only need to change the domain of this wrapped
581 * mapping, so we extend the input transformation with an identity mapping
582 * on the space of argument values.
584 static __isl_give isl_map *update_domain(__isl_take isl_map *access,
585 void *user)
587 isl_map *update = user;
588 isl_space *dim;
590 update = isl_map_copy(update);
592 dim = isl_map_get_space(access);
593 dim = isl_space_domain(dim);
594 if (!isl_space_is_wrapping(dim))
595 isl_space_free(dim);
596 else {
597 isl_map *id;
598 dim = isl_space_unwrap(dim);
599 dim = isl_space_range(dim);
600 dim = isl_space_map_from_set(dim);
601 id = isl_map_identity(dim);
602 update = isl_map_product(update, id);
605 return isl_map_apply_domain(access, update);
608 /* Modify all access relations in "expr" based on the given iteration space
609 * transformation.
611 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
612 __isl_take isl_map *update)
614 expr = pet_expr_foreach_access(expr, &update_domain, update);
615 isl_map_free(update);
616 return expr;
619 /* Construct a pet_stmt with given line number and statement
620 * number from a pet_expr.
621 * The initial iteration domain is the zero-dimensional universe.
622 * The name of the domain is given by "label" if it is non-NULL.
623 * Otherwise, the name is constructed as S_<id>.
624 * The domains of all access relations are modified to refer
625 * to the statement iteration domain.
627 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
628 __isl_take isl_id *label, int id, struct pet_expr *expr)
630 struct pet_stmt *stmt;
631 isl_space *dim;
632 isl_set *dom;
633 isl_map *sched;
634 isl_map *add_name;
635 char name[50];
637 if (!expr)
638 goto error;
640 stmt = isl_calloc_type(ctx, struct pet_stmt);
641 if (!stmt)
642 goto error;
644 dim = isl_space_set_alloc(ctx, 0, 0);
645 if (label)
646 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
647 else {
648 snprintf(name, sizeof(name), "S_%d", id);
649 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
651 dom = isl_set_universe(isl_space_copy(dim));
652 sched = isl_map_from_domain(isl_set_copy(dom));
654 dim = isl_space_from_range(dim);
655 add_name = isl_map_universe(dim);
656 expr = expr_update_domain(expr, add_name);
658 stmt->line = line;
659 stmt->domain = dom;
660 stmt->schedule = sched;
661 stmt->body = expr;
663 if (!stmt->domain || !stmt->schedule || !stmt->body)
664 return pet_stmt_free(stmt);
666 return stmt;
667 error:
668 isl_id_free(label);
669 return pet_expr_free(expr);
672 void *pet_stmt_free(struct pet_stmt *stmt)
674 int i;
676 if (!stmt)
677 return NULL;
679 isl_set_free(stmt->domain);
680 isl_map_free(stmt->schedule);
681 pet_expr_free(stmt->body);
683 for (i = 0; i < stmt->n_arg; ++i)
684 pet_expr_free(stmt->args[i]);
685 free(stmt->args);
687 free(stmt);
688 return NULL;
691 static void stmt_dump(struct pet_stmt *stmt, int indent)
693 int i;
695 if (!stmt)
696 return;
698 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
699 fprintf(stderr, "%*s", indent, "");
700 isl_set_dump(stmt->domain);
701 fprintf(stderr, "%*s", indent, "");
702 isl_map_dump(stmt->schedule);
703 expr_dump(stmt->body, indent);
704 for (i = 0; i < stmt->n_arg; ++i)
705 expr_dump(stmt->args[i], indent + 2);
708 void pet_stmt_dump(struct pet_stmt *stmt)
710 stmt_dump(stmt, 0);
713 struct pet_array *pet_array_free(struct pet_array *array)
715 if (!array)
716 return NULL;
718 isl_set_free(array->context);
719 isl_set_free(array->extent);
720 isl_set_free(array->value_bounds);
721 free(array->element_type);
723 free(array);
724 return NULL;
727 void pet_array_dump(struct pet_array *array)
729 if (!array)
730 return;
732 isl_set_dump(array->context);
733 isl_set_dump(array->extent);
734 isl_set_dump(array->value_bounds);
735 fprintf(stderr, "%s %s\n", array->element_type,
736 array->live_out ? "live-out" : "");
739 /* Alloc a pet_scop structure, with extra room for information that
740 * is only used during parsing.
742 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
744 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
747 /* Construct a pet_scop with room for n statements.
749 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
751 isl_space *space;
752 struct pet_scop *scop;
754 scop = pet_scop_alloc(ctx);
755 if (!scop)
756 return NULL;
758 space = isl_space_params_alloc(ctx, 0);
759 scop->context = isl_set_universe(isl_space_copy(space));
760 scop->context_value = isl_set_universe(space);
761 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
762 if (!scop->context || !scop->stmts)
763 return pet_scop_free(scop);
765 scop->n_stmt = n;
767 return scop;
770 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
772 return scop_alloc(ctx, 0);
775 /* Update "context" with respect to the valid parameter values for "access".
777 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
778 __isl_take isl_set *context)
780 context = isl_set_intersect(context,
781 isl_map_params(isl_map_copy(access)));
782 return context;
785 /* Update "context" with respect to the valid parameter values for "expr".
787 * If "expr" represents a ternary operator, then a parameter value
788 * needs to be valid for the condition and for at least one of the
789 * remaining two arguments.
790 * If the condition is an affine expression, then we can be a bit more specific.
791 * The parameter then has to be valid for the second argument for
792 * non-zero accesses and valid for the third argument for zero accesses.
794 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
795 __isl_take isl_set *context)
797 int i;
799 if (expr->type == pet_expr_ternary) {
800 int is_aff;
801 isl_set *context1, *context2;
803 is_aff = pet_expr_is_affine(expr->args[0]);
804 if (is_aff < 0)
805 goto error;
807 context = expr_extract_context(expr->args[0], context);
808 context1 = expr_extract_context(expr->args[1],
809 isl_set_copy(context));
810 context2 = expr_extract_context(expr->args[2], context);
812 if (is_aff) {
813 isl_map *access;
814 isl_set *zero_set;
816 access = isl_map_copy(expr->args[0]->acc.access);
817 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
818 zero_set = isl_map_params(access);
819 context1 = isl_set_subtract(context1,
820 isl_set_copy(zero_set));
821 context2 = isl_set_intersect(context2, zero_set);
824 context = isl_set_union(context1, context2);
825 context = isl_set_coalesce(context);
827 return context;
830 for (i = 0; i < expr->n_arg; ++i)
831 context = expr_extract_context(expr->args[i], context);
833 if (expr->type == pet_expr_access)
834 context = access_extract_context(expr->acc.access, context);
836 return context;
837 error:
838 isl_set_free(context);
839 return NULL;
842 /* Update "context" with respect to the valid parameter values for "stmt".
844 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
845 __isl_take isl_set *context)
847 int i;
849 for (i = 0; i < stmt->n_arg; ++i)
850 context = expr_extract_context(stmt->args[i], context);
852 context = expr_extract_context(stmt->body, context);
854 return context;
857 /* Construct a pet_scop that contains the given pet_stmt.
859 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
861 struct pet_scop *scop;
863 if (!stmt)
864 return NULL;
866 scop = scop_alloc(ctx, 1);
868 scop->context = stmt_extract_context(stmt, scop->context);
869 if (!scop->context)
870 goto error;
872 scop->stmts[0] = stmt;
874 return scop;
875 error:
876 pet_stmt_free(stmt);
877 pet_scop_free(scop);
878 return NULL;
881 /* Does "set" represent an element of an unnamed space, i.e.,
882 * does it represent an affine expression?
884 static int set_is_affine(__isl_keep isl_set *set)
886 int has_id;
888 has_id = isl_set_has_tuple_id(set);
889 if (has_id < 0)
890 return -1;
892 return !has_id;
895 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
896 * ext may be equal to either ext1 or ext2.
898 * The two skips that need to be combined are assumed to be affine expressions.
900 * We need to skip in ext if we need to skip in either ext1 or ext2.
901 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
903 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
904 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
905 enum pet_skip type)
907 isl_set *set, *skip1, *skip2;
909 if (!ext)
910 return NULL;
911 if (!ext1->skip[type] && !ext2->skip[type])
912 return ext;
913 if (!ext1->skip[type]) {
914 if (ext == ext2)
915 return ext;
916 ext->skip[type] = ext2->skip[type];
917 ext2->skip[type] = NULL;
918 return ext;
920 if (!ext2->skip[type]) {
921 if (ext == ext1)
922 return ext;
923 ext->skip[type] = ext1->skip[type];
924 ext1->skip[type] = NULL;
925 return ext;
928 if (!set_is_affine(ext1->skip[type]) ||
929 !set_is_affine(ext2->skip[type]))
930 isl_die(isl_set_get_ctx(ext1->skip[type]), isl_error_internal,
931 "can only combine affine skips",
932 return pet_scop_free(&ext->scop));
934 skip1 = isl_set_copy(ext1->skip[type]);
935 skip2 = isl_set_copy(ext2->skip[type]);
936 set = isl_set_intersect(
937 isl_set_fix_si(isl_set_copy(skip1), isl_dim_set, 0, 0),
938 isl_set_fix_si(isl_set_copy(skip2), isl_dim_set, 0, 0));
939 set = isl_set_union(set, isl_set_fix_si(skip1, isl_dim_set, 0, 1));
940 set = isl_set_union(set, isl_set_fix_si(skip2, isl_dim_set, 0, 1));
941 set = isl_set_coalesce(set);
942 isl_set_free(ext1->skip[type]);
943 ext1->skip[type] = NULL;
944 isl_set_free(ext2->skip[type]);
945 ext2->skip[type] = NULL;
946 ext->skip[type] = set;
947 if (!ext->skip[type])
948 return pet_scop_free(&ext->scop);
950 return ext;
953 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
954 * where type takes on the values pet_skip_now and pet_skip_later.
955 * scop may be equal to either scop1 or scop2.
957 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
958 struct pet_scop *scop1, struct pet_scop *scop2)
960 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
961 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
962 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
964 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
965 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
966 return &ext->scop;
969 /* Update scop->start and scop->end to include the region from "start"
970 * to "end". In particular, if scop->end == 0, then "scop" does not
971 * have any offset information yet and we simply take the information
972 * from "start" and "end". Otherwise, we update the fields if the
973 * region from "start" to "end" is not already included.
975 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
976 unsigned start, unsigned end)
978 if (!scop)
979 return NULL;
980 if (scop->end == 0) {
981 scop->start = start;
982 scop->end = end;
983 } else {
984 if (start < scop->start)
985 scop->start = start;
986 if (end > scop->end)
987 scop->end = end;
990 return scop;
993 /* Combine the offset information of "scop1" and "scop2" into "scop".
995 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
996 struct pet_scop *scop1, struct pet_scop *scop2)
998 if (scop1->end)
999 scop = pet_scop_update_start_end(scop,
1000 scop1->start, scop1->end);
1001 if (scop2->end)
1002 scop = pet_scop_update_start_end(scop,
1003 scop2->start, scop2->end);
1004 return scop;
1007 /* Construct a pet_scop that contains the offset information,
1008 * arrays, statements and skip information in "scop1" and "scop2".
1010 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1011 struct pet_scop *scop2)
1013 int i;
1014 struct pet_scop *scop;
1016 if (!scop1 || !scop2)
1017 goto error;
1019 if (scop1->n_stmt == 0) {
1020 scop2 = scop_combine_skips(scop2, scop1, scop2);
1021 pet_scop_free(scop1);
1022 return scop2;
1025 if (scop2->n_stmt == 0) {
1026 scop1 = scop_combine_skips(scop1, scop1, scop2);
1027 pet_scop_free(scop2);
1028 return scop1;
1031 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1032 if (!scop)
1033 goto error;
1035 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1036 scop1->n_array + scop2->n_array);
1037 if (!scop->arrays)
1038 goto error;
1039 scop->n_array = scop1->n_array + scop2->n_array;
1041 for (i = 0; i < scop1->n_stmt; ++i) {
1042 scop->stmts[i] = scop1->stmts[i];
1043 scop1->stmts[i] = NULL;
1046 for (i = 0; i < scop2->n_stmt; ++i) {
1047 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1048 scop2->stmts[i] = NULL;
1051 for (i = 0; i < scop1->n_array; ++i) {
1052 scop->arrays[i] = scop1->arrays[i];
1053 scop1->arrays[i] = NULL;
1056 for (i = 0; i < scop2->n_array; ++i) {
1057 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1058 scop2->arrays[i] = NULL;
1061 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1062 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1063 scop = scop_combine_skips(scop, scop1, scop2);
1064 scop = scop_combine_start_end(scop, scop1, scop2);
1066 pet_scop_free(scop1);
1067 pet_scop_free(scop2);
1068 return scop;
1069 error:
1070 pet_scop_free(scop1);
1071 pet_scop_free(scop2);
1072 return NULL;
1075 /* Apply the skip condition "skip" to "scop".
1076 * That is, make sure "scop" is not executed when the condition holds.
1078 * If "skip" is an affine expression, we add the conditions under
1079 * which the expression is zero to the iteration domains.
1080 * Otherwise, we add a filter on the variable attaining the value zero.
1082 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1083 __isl_take isl_set *skip)
1085 isl_map *skip_map;
1086 int is_aff;
1088 if (!scop || !skip)
1089 goto error;
1091 is_aff = set_is_affine(skip);
1092 if (is_aff < 0)
1093 goto error;
1095 if (!is_aff)
1096 return pet_scop_filter(scop, isl_map_from_range(skip), 0);
1098 skip = isl_set_fix_si(skip, isl_dim_set, 0, 0);
1099 scop = pet_scop_restrict(scop, isl_set_params(skip));
1101 return scop;
1102 error:
1103 isl_set_free(skip);
1104 return pet_scop_free(scop);
1107 /* Construct a pet_scop that contains the arrays, statements and
1108 * skip information in "scop1" and "scop2", where the two scops
1109 * are executed "in sequence". That is, breaks and continues
1110 * in scop1 have an effect on scop2.
1112 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1113 struct pet_scop *scop2)
1115 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1116 scop2 = restrict_skip(scop2,
1117 pet_scop_get_skip(scop1, pet_skip_now));
1118 return pet_scop_add(ctx, scop1, scop2);
1121 /* Construct a pet_scop that contains the arrays, statements and
1122 * skip information in "scop1" and "scop2", where the two scops
1123 * are executed "in parallel". That is, any break or continue
1124 * in scop1 has no effect on scop2.
1126 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1127 struct pet_scop *scop2)
1129 return pet_scop_add(ctx, scop1, scop2);
1132 void *pet_scop_free(struct pet_scop *scop)
1134 int i;
1135 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1137 if (!scop)
1138 return NULL;
1139 isl_set_free(scop->context);
1140 isl_set_free(scop->context_value);
1141 if (scop->arrays)
1142 for (i = 0; i < scop->n_array; ++i)
1143 pet_array_free(scop->arrays[i]);
1144 free(scop->arrays);
1145 if (scop->stmts)
1146 for (i = 0; i < scop->n_stmt; ++i)
1147 pet_stmt_free(scop->stmts[i]);
1148 free(scop->stmts);
1149 isl_set_free(ext->skip[pet_skip_now]);
1150 isl_set_free(ext->skip[pet_skip_later]);
1151 free(scop);
1152 return NULL;
1155 void pet_scop_dump(struct pet_scop *scop)
1157 int i;
1158 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1160 if (!scop)
1161 return;
1163 isl_set_dump(scop->context);
1164 isl_set_dump(scop->context_value);
1165 for (i = 0; i < scop->n_array; ++i)
1166 pet_array_dump(scop->arrays[i]);
1167 for (i = 0; i < scop->n_stmt; ++i)
1168 pet_stmt_dump(scop->stmts[i]);
1170 if (ext->skip[0]) {
1171 fprintf(stderr, "skip\n");
1172 isl_set_dump(ext->skip[0]);
1173 isl_set_dump(ext->skip[1]);
1177 /* Return 1 if the two pet_arrays are equivalent.
1179 * We don't compare element_size as this may be target dependent.
1181 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1183 if (!array1 || !array2)
1184 return 0;
1186 if (!isl_set_is_equal(array1->context, array2->context))
1187 return 0;
1188 if (!isl_set_is_equal(array1->extent, array2->extent))
1189 return 0;
1190 if (!!array1->value_bounds != !!array2->value_bounds)
1191 return 0;
1192 if (array1->value_bounds &&
1193 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1194 return 0;
1195 if (strcmp(array1->element_type, array2->element_type))
1196 return 0;
1197 if (array1->live_out != array2->live_out)
1198 return 0;
1199 if (array1->uniquely_defined != array2->uniquely_defined)
1200 return 0;
1201 if (array1->declared != array2->declared)
1202 return 0;
1203 if (array1->exposed != array2->exposed)
1204 return 0;
1206 return 1;
1209 /* Return 1 if the two pet_stmts are equivalent.
1211 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1213 int i;
1215 if (!stmt1 || !stmt2)
1216 return 0;
1218 if (stmt1->line != stmt2->line)
1219 return 0;
1220 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1221 return 0;
1222 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1223 return 0;
1224 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1225 return 0;
1226 if (stmt1->n_arg != stmt2->n_arg)
1227 return 0;
1228 for (i = 0; i < stmt1->n_arg; ++i) {
1229 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1230 return 0;
1233 return 1;
1236 /* Return 1 if the two pet_scops are equivalent.
1238 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1240 int i;
1242 if (!scop1 || !scop2)
1243 return 0;
1245 if (!isl_set_is_equal(scop1->context, scop2->context))
1246 return 0;
1247 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1248 return 0;
1250 if (scop1->n_array != scop2->n_array)
1251 return 0;
1252 for (i = 0; i < scop1->n_array; ++i)
1253 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1254 return 0;
1256 if (scop1->n_stmt != scop2->n_stmt)
1257 return 0;
1258 for (i = 0; i < scop1->n_stmt; ++i)
1259 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1260 return 0;
1262 return 1;
1265 /* Prefix the schedule of "stmt" with an extra dimension with constant
1266 * value "pos".
1268 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1270 if (!stmt)
1271 return NULL;
1273 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1274 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1275 if (!stmt->schedule)
1276 return pet_stmt_free(stmt);
1278 return stmt;
1281 /* Prefix the schedules of all statements in "scop" with an extra
1282 * dimension with constant value "pos".
1284 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1286 int i;
1288 if (!scop)
1289 return NULL;
1291 for (i = 0; i < scop->n_stmt; ++i) {
1292 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1293 if (!scop->stmts[i])
1294 return pet_scop_free(scop);
1297 return scop;
1300 /* Given a set with a parameter at "param_pos" that refers to the
1301 * iterator, "move" the iterator to the first set dimension.
1302 * That is, essentially equate the parameter to the first set dimension
1303 * and then project it out.
1305 * The first set dimension may however refer to a virtual iterator,
1306 * while the parameter refers to the "real" iterator.
1307 * We therefore need to take into account the mapping "iv_map", which
1308 * maps the virtual iterator to the real iterator.
1309 * In particular, we equate the set dimension to the input of the map
1310 * and the parameter to the output of the map and then project out
1311 * everything we don't need anymore.
1313 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1314 int param_pos, __isl_take isl_map *iv_map)
1316 isl_map *map;
1317 map = isl_map_from_domain(set);
1318 map = isl_map_add_dims(map, isl_dim_out, 1);
1319 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1320 iv_map = isl_map_align_params(iv_map, isl_map_get_space(map));
1321 map = isl_map_apply_range(map, iv_map);
1322 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1323 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1324 return isl_map_domain(map);
1327 /* Data used in embed_access.
1328 * extend adds an iterator to the iteration domain
1329 * iv_map maps the virtual iterator to the real iterator
1330 * var_id represents the induction variable of the corresponding loop
1332 struct pet_embed_access {
1333 isl_map *extend;
1334 isl_map *iv_map;
1335 isl_id *var_id;
1338 /* Embed the access relation in an extra outer loop.
1340 * We first update the iteration domain to insert the extra dimension.
1342 * If the access refers to the induction variable, then it is
1343 * turned into an access to the set of integers with index (and value)
1344 * equal to the induction variable.
1346 * If the induction variable appears in the constraints (as a parameter),
1347 * then the parameter is equated to the newly introduced iteration
1348 * domain dimension and subsequently projected out.
1350 * Similarly, if the accessed array is a virtual array (with user
1351 * pointer equal to NULL), as created by create_test_access,
1352 * then it is extended along with the domain of the access.
1354 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
1355 void *user)
1357 struct pet_embed_access *data = user;
1358 isl_id *array_id = NULL;
1359 int pos;
1361 access = update_domain(access, data->extend);
1363 if (isl_map_has_tuple_id(access, isl_dim_out))
1364 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1365 if (array_id == data->var_id ||
1366 (array_id && !isl_id_get_user(array_id))) {
1367 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1368 access = isl_map_equate(access,
1369 isl_dim_in, 0, isl_dim_out, 0);
1370 if (array_id == data->var_id)
1371 access = isl_map_apply_range(access,
1372 isl_map_copy(data->iv_map));
1373 else
1374 access = isl_map_set_tuple_id(access, isl_dim_out,
1375 isl_id_copy(array_id));
1377 isl_id_free(array_id);
1379 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1380 if (pos >= 0) {
1381 isl_set *set = isl_map_wrap(access);
1382 set = internalize_iv(set, pos, isl_map_copy(data->iv_map));
1383 access = isl_set_unwrap(set);
1385 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1386 isl_id_copy(data->var_id));
1388 return access;
1391 /* Embed all access relations in "expr" in an extra loop.
1392 * "extend" inserts an outer loop iterator in the iteration domains.
1393 * "iv_map" maps the virtual iterator to the real iterator
1394 * "var_id" represents the induction variable.
1396 static struct pet_expr *expr_embed(struct pet_expr *expr,
1397 __isl_take isl_map *extend, __isl_take isl_map *iv_map,
1398 __isl_keep isl_id *var_id)
1400 struct pet_embed_access data =
1401 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1403 expr = pet_expr_foreach_access(expr, &embed_access, &data);
1404 isl_map_free(iv_map);
1405 isl_map_free(extend);
1406 return expr;
1409 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1410 * "dom" and schedule "sched". "var_id" represents the induction variable
1411 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1412 * That is, it maps the iterator used in "dom" and the domain of "sched"
1413 * to the iterator that some of the parameters in "stmt" may refer to.
1415 * The iteration domain and schedule of the statement are updated
1416 * according to the iteration domain and schedule of the new loop.
1417 * If stmt->domain is a wrapped map, then the iteration domain
1418 * is the domain of this map, so we need to be careful to adjust
1419 * this domain.
1421 * If the induction variable appears in the constraints (as a parameter)
1422 * of the current iteration domain or the schedule of the statement,
1423 * then the parameter is equated to the newly introduced iteration
1424 * domain dimension and subsequently projected out.
1426 * Finally, all access relations are updated based on the extra loop.
1428 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1429 __isl_take isl_set *dom, __isl_take isl_map *sched,
1430 __isl_take isl_map *iv_map, __isl_take isl_id *var_id)
1432 int i;
1433 int pos;
1434 isl_id *stmt_id;
1435 isl_space *dim;
1436 isl_map *extend;
1438 if (!stmt)
1439 goto error;
1441 if (isl_set_is_wrapping(stmt->domain)) {
1442 isl_map *map;
1443 isl_map *ext;
1444 isl_space *ran_dim;
1446 map = isl_set_unwrap(stmt->domain);
1447 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1448 ran_dim = isl_space_range(isl_map_get_space(map));
1449 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1450 isl_set_universe(ran_dim));
1451 map = isl_map_flat_domain_product(ext, map);
1452 map = isl_map_set_tuple_id(map, isl_dim_in,
1453 isl_id_copy(stmt_id));
1454 dim = isl_space_domain(isl_map_get_space(map));
1455 stmt->domain = isl_map_wrap(map);
1456 } else {
1457 stmt_id = isl_set_get_tuple_id(stmt->domain);
1458 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1459 stmt->domain);
1460 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1461 isl_id_copy(stmt_id));
1462 dim = isl_set_get_space(stmt->domain);
1465 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1466 if (pos >= 0)
1467 stmt->domain = internalize_iv(stmt->domain, pos,
1468 isl_map_copy(iv_map));
1470 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1471 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1472 isl_dim_in, stmt_id);
1474 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1475 if (pos >= 0) {
1476 isl_set *set = isl_map_wrap(stmt->schedule);
1477 set = internalize_iv(set, pos, isl_map_copy(iv_map));
1478 stmt->schedule = isl_set_unwrap(set);
1481 dim = isl_space_map_from_set(dim);
1482 extend = isl_map_identity(dim);
1483 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1484 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1485 isl_map_get_tuple_id(extend, isl_dim_out));
1486 for (i = 0; i < stmt->n_arg; ++i)
1487 stmt->args[i] = expr_embed(stmt->args[i], isl_map_copy(extend),
1488 isl_map_copy(iv_map), var_id);
1489 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1491 isl_set_free(dom);
1492 isl_id_free(var_id);
1494 for (i = 0; i < stmt->n_arg; ++i)
1495 if (!stmt->args[i])
1496 return pet_stmt_free(stmt);
1497 if (!stmt->domain || !stmt->schedule || !stmt->body)
1498 return pet_stmt_free(stmt);
1499 return stmt;
1500 error:
1501 isl_set_free(dom);
1502 isl_map_free(sched);
1503 isl_map_free(iv_map);
1504 isl_id_free(var_id);
1505 return NULL;
1508 /* Embed the given pet_array in an extra outer loop with iteration domain
1509 * "dom".
1510 * This embedding only has an effect on virtual arrays (those with
1511 * user pointer equal to NULL), which need to be extended along with
1512 * the iteration domain.
1514 static struct pet_array *pet_array_embed(struct pet_array *array,
1515 __isl_take isl_set *dom)
1517 isl_id *array_id = NULL;
1519 if (!array)
1520 goto error;
1522 if (isl_set_has_tuple_id(array->extent))
1523 array_id = isl_set_get_tuple_id(array->extent);
1525 if (array_id && !isl_id_get_user(array_id)) {
1526 array->extent = isl_set_flat_product(dom, array->extent);
1527 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1528 } else {
1529 isl_set_free(dom);
1530 isl_id_free(array_id);
1533 return array;
1534 error:
1535 isl_set_free(dom);
1536 return NULL;
1539 /* Project out all unnamed parameters from "set" and return the result.
1541 static __isl_give isl_set *set_project_out_unnamed_params(
1542 __isl_take isl_set *set)
1544 int i, n;
1546 n = isl_set_dim(set, isl_dim_param);
1547 for (i = n - 1; i >= 0; --i) {
1548 if (isl_set_has_dim_name(set, isl_dim_param, i))
1549 continue;
1550 set = isl_set_project_out(set, isl_dim_param, i, 1);
1553 return set;
1556 /* Update the context with respect to an embedding into a loop
1557 * with iteration domain "dom" and induction variable "id".
1558 * "iv_map" maps a possibly virtual iterator (used in "dom")
1559 * to the real iterator (parameter "id").
1561 * If the current context is independent of "id", we don't need
1562 * to do anything.
1563 * Otherwise, a parameter value is invalid for the embedding if
1564 * any of the corresponding iterator values is invalid.
1565 * That is, a parameter value is valid only if all the corresponding
1566 * iterator values are valid.
1567 * We therefore compute the set of parameters
1569 * forall i in dom : valid (i)
1571 * or
1573 * not exists i in dom : not valid(i)
1575 * i.e.,
1577 * not exists i in dom \ valid(i)
1579 * Before we subtract valid(i) from dom, we first need to map
1580 * the real iterator to the virtual iterator.
1582 * If there are any unnamed parameters in "dom", then we consider
1583 * a parameter value to be valid if it is valid for any value of those
1584 * unnamed parameters. They are therefore projected out at the end.
1586 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1587 __isl_keep isl_set *dom, __isl_keep isl_map *iv_map,
1588 __isl_keep isl_id *id)
1590 int pos;
1592 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1593 if (pos < 0)
1594 return context;
1596 context = isl_set_from_params(context);
1597 context = isl_set_add_dims(context, isl_dim_set, 1);
1598 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1599 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1600 context = isl_set_apply(context, isl_map_reverse(isl_map_copy(iv_map)));
1601 context = isl_set_subtract(isl_set_copy(dom), context);
1602 context = isl_set_params(context);
1603 context = isl_set_complement(context);
1604 context = set_project_out_unnamed_params(context);
1605 return context;
1608 /* Embed all statements and arrays in "scop" in an extra outer loop
1609 * with iteration domain "dom" and schedule "sched".
1610 * "id" represents the induction variable of the loop.
1611 * "iv_map" maps a possibly virtual iterator to the real iterator.
1612 * That is, it maps the iterator used in "dom" and the domain of "sched"
1613 * to the iterator that some of the parameters in "scop" may refer to.
1615 * Any skip conditions within the loop have no effect outside of the loop.
1616 * The caller is responsible for making sure skip[pet_skip_later] has been
1617 * taken into account.
1619 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1620 __isl_take isl_map *sched, __isl_take isl_map *iv_map,
1621 __isl_take isl_id *id)
1623 int i;
1625 if (!scop)
1626 goto error;
1628 pet_scop_reset_skip(scop, pet_skip_now);
1629 pet_scop_reset_skip(scop, pet_skip_later);
1631 scop->context = context_embed(scop->context, dom, iv_map, id);
1632 if (!scop->context)
1633 goto error;
1635 for (i = 0; i < scop->n_stmt; ++i) {
1636 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1637 isl_set_copy(dom), isl_map_copy(sched),
1638 isl_map_copy(iv_map), isl_id_copy(id));
1639 if (!scop->stmts[i])
1640 goto error;
1643 for (i = 0; i < scop->n_array; ++i) {
1644 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1645 isl_set_copy(dom));
1646 if (!scop->arrays[i])
1647 goto error;
1650 isl_set_free(dom);
1651 isl_map_free(sched);
1652 isl_map_free(iv_map);
1653 isl_id_free(id);
1654 return scop;
1655 error:
1656 isl_set_free(dom);
1657 isl_map_free(sched);
1658 isl_map_free(iv_map);
1659 isl_id_free(id);
1660 return pet_scop_free(scop);
1663 /* Add extra conditions on the parameters to iteration domain of "stmt".
1665 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1666 __isl_take isl_set *cond)
1668 if (!stmt)
1669 goto error;
1671 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1673 return stmt;
1674 error:
1675 isl_set_free(cond);
1676 return pet_stmt_free(stmt);
1679 /* Add extra conditions to scop->skip[type].
1681 * The new skip condition only holds if it held before
1682 * and the condition is true. It does not hold if it did not hold
1683 * before or the condition is false.
1685 * The skip condition is assumed to be an affine expression.
1687 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1688 enum pet_skip type, __isl_keep isl_set *cond)
1690 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1691 isl_set *skip;
1692 isl_set *set;
1694 if (!scop)
1695 return NULL;
1696 if (!ext->skip[type])
1697 return scop;
1699 if (!set_is_affine(ext->skip[type]))
1700 isl_die(isl_set_get_ctx(ext->skip[type]), isl_error_internal,
1701 "can only resrict affine skips",
1702 return pet_scop_free(scop));
1704 skip = ext->skip[type];
1705 skip = isl_set_intersect_params(skip, isl_set_copy(cond));
1706 set = isl_set_from_params(isl_set_copy(cond));
1707 set = isl_set_complement(set);
1708 set = isl_set_add_dims(set, isl_dim_set, 1);
1709 set = isl_set_fix_si(set, isl_dim_set, 0, 0);
1710 skip = isl_set_union(skip, set);
1711 ext->skip[type] = skip;
1712 if (!ext->skip[type])
1713 return pet_scop_free(scop);
1715 return scop;
1718 /* Add extra conditions on the parameters to all iteration domains
1719 * and skip conditions.
1721 * A parameter value is valid for the result if it was valid
1722 * for the original scop and satisfies "cond" or if it does
1723 * not satisfy "cond" as in this case the scop is not executed
1724 * and the original constraints on the parameters are irrelevant.
1726 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1727 __isl_take isl_set *cond)
1729 int i;
1731 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1732 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1734 if (!scop)
1735 goto error;
1737 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1738 scop->context = isl_set_union(scop->context,
1739 isl_set_complement(isl_set_copy(cond)));
1740 scop->context = isl_set_coalesce(scop->context);
1741 scop->context = set_project_out_unnamed_params(scop->context);
1742 if (!scop->context)
1743 goto error;
1745 for (i = 0; i < scop->n_stmt; ++i) {
1746 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1747 isl_set_copy(cond));
1748 if (!scop->stmts[i])
1749 goto error;
1752 isl_set_free(cond);
1753 return scop;
1754 error:
1755 isl_set_free(cond);
1756 return pet_scop_free(scop);
1759 /* Construct a map that inserts a filter value with name "id" and value
1760 * "satisfied" in the list of filter values embedded in the set space "space".
1762 * If "space" does not contain any filter values yet, we first create
1763 * a map that inserts 0 filter values, i.e.,
1765 * space -> [space -> []]
1767 * We can now assume that space is of the form [dom -> [filters]]
1768 * We construct an identity mapping on dom and a mapping on filters
1769 * that inserts the new filter
1771 * dom -> dom
1772 * [filters] -> [satisfied, filters]
1774 * and then compute the cross product
1776 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1778 static __isl_give isl_map *insert_filter_map(__isl_take isl_space *space,
1779 __isl_take isl_id *id, int satisfied)
1781 isl_space *space2;
1782 isl_map *map, *map_dom, *map_ran;
1783 isl_set *dom;
1785 if (isl_space_is_wrapping(space)) {
1786 space2 = isl_space_map_from_set(isl_space_copy(space));
1787 map = isl_map_identity(space2);
1788 space = isl_space_unwrap(space);
1789 } else {
1790 space = isl_space_from_domain(space);
1791 map = isl_map_universe(isl_space_copy(space));
1792 map = isl_map_reverse(isl_map_domain_map(map));
1795 space2 = isl_space_domain(isl_space_copy(space));
1796 map_dom = isl_map_identity(isl_space_map_from_set(space2));
1797 space = isl_space_range(space);
1798 map_ran = isl_map_identity(isl_space_map_from_set(space));
1799 map_ran = isl_map_insert_dims(map_ran, isl_dim_out, 0, 1);
1800 map_ran = isl_map_set_dim_id(map_ran, isl_dim_out, 0, id);
1801 map_ran = isl_map_fix_si(map_ran, isl_dim_out, 0, satisfied);
1803 map = isl_map_apply_range(map, isl_map_product(map_dom, map_ran));
1805 return map;
1808 /* Insert an argument expression corresponding to "test" in front
1809 * of the list of arguments described by *n_arg and *args.
1811 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
1812 __isl_keep isl_map *test)
1814 int i;
1815 isl_ctx *ctx = isl_map_get_ctx(test);
1817 if (!test)
1818 return -1;
1820 if (!*args) {
1821 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
1822 if (!*args)
1823 return -1;
1824 } else {
1825 struct pet_expr **ext;
1826 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
1827 if (!ext)
1828 return -1;
1829 for (i = 0; i < *n_arg; ++i)
1830 ext[1 + i] = (*args)[i];
1831 free(*args);
1832 *args = ext;
1834 (*n_arg)++;
1835 (*args)[0] = pet_expr_from_access(isl_map_copy(test));
1836 if (!(*args)[0])
1837 return -1;
1839 return 0;
1842 /* Make the expression "expr" depend on the value of "test"
1843 * being equal to "satisfied".
1845 * If "test" is an affine expression, we simply add the conditions
1846 * on the expression have the value "satisfied" to all access relations.
1848 * Otherwise, we add a filter to "expr" (which is then assumed to be
1849 * an access expression) corresponding to "test" being equal to "satisfied".
1851 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
1852 __isl_take isl_map *test, int satisfied)
1854 isl_id *id;
1855 isl_ctx *ctx;
1856 isl_space *space;
1857 isl_map *map;
1859 if (!expr || !test)
1860 goto error;
1862 if (!isl_map_has_tuple_id(test, isl_dim_out)) {
1863 test = isl_map_fix_si(test, isl_dim_out, 0, satisfied);
1864 return pet_expr_restrict(expr, isl_map_params(test));
1867 ctx = isl_map_get_ctx(test);
1868 if (expr->type != pet_expr_access)
1869 isl_die(ctx, isl_error_invalid,
1870 "can only filter access expressions", goto error);
1872 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1873 id = isl_map_get_tuple_id(test, isl_dim_out);
1874 map = insert_filter_map(space, id, satisfied);
1876 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
1877 if (!expr->acc.access)
1878 goto error;
1880 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
1881 goto error;
1883 isl_map_free(test);
1884 return expr;
1885 error:
1886 isl_map_free(test);
1887 return pet_expr_free(expr);
1890 /* Make the statement "stmt" depend on the value of "test"
1891 * being equal to "satisfied" by adjusting stmt->domain.
1893 * The domain of "test" corresponds to the (zero or more) outer dimensions
1894 * of the iteration domain.
1896 * We insert an argument corresponding to a read to "test"
1897 * from the iteration domain of "stmt" in front of the list of arguments.
1898 * We also insert a corresponding output dimension in the wrapped
1899 * map contained in stmt->domain, with value set to "satisfied".
1901 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1902 __isl_take isl_map *test, int satisfied)
1904 int i;
1905 isl_id *id;
1906 isl_ctx *ctx;
1907 isl_map *map, *add_dom;
1908 isl_space *space;
1909 isl_set *dom;
1910 int n_test_dom;
1912 if (!stmt || !test)
1913 goto error;
1915 id = isl_map_get_tuple_id(test, isl_dim_out);
1916 map = insert_filter_map(isl_set_get_space(stmt->domain), id, satisfied);
1917 stmt->domain = isl_set_apply(stmt->domain, map);
1919 space = isl_space_unwrap(isl_set_get_space(stmt->domain));
1920 dom = isl_set_universe(isl_space_domain(space));
1921 n_test_dom = isl_map_dim(test, isl_dim_in);
1922 add_dom = isl_map_from_range(dom);
1923 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
1924 for (i = 0; i < n_test_dom; ++i)
1925 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
1926 isl_dim_out, i);
1927 test = isl_map_apply_domain(test, add_dom);
1929 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1930 goto error;
1932 isl_map_free(test);
1933 return stmt;
1934 error:
1935 isl_map_free(test);
1936 return pet_stmt_free(stmt);
1939 /* Does "scop" have a skip condition of the given "type"?
1941 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1943 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1945 if (!scop)
1946 return -1;
1947 return ext->skip[type] != NULL;
1950 /* Does "scop" have a skip condition of the given "type" that
1951 * is an affine expression?
1953 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1955 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1957 if (!scop)
1958 return -1;
1959 if (!ext->skip[type])
1960 return 0;
1961 return set_is_affine(ext->skip[type]);
1964 /* Does "scop" have a skip condition of the given "type" that
1965 * is not an affine expression?
1967 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1969 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1970 int aff;
1972 if (!scop)
1973 return -1;
1974 if (!ext->skip[type])
1975 return 0;
1976 aff = set_is_affine(ext->skip[type]);
1977 if (aff < 0)
1978 return -1;
1979 return !aff;
1982 /* Does "scop" have a skip condition of the given "type" that
1983 * is affine and holds on the entire domain?
1985 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1987 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1988 isl_set *set;
1989 int is_aff;
1990 int is_univ;
1992 is_aff = pet_scop_has_affine_skip(scop, type);
1993 if (is_aff < 0 || !is_aff)
1994 return is_aff;
1996 set = isl_set_copy(ext->skip[type]);
1997 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
1998 set = isl_set_params(set);
1999 is_univ = isl_set_plain_is_universe(set);
2000 isl_set_free(set);
2002 return is_univ;
2005 /* Replace scop->skip[type] by "skip".
2007 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2008 enum pet_skip type, __isl_take isl_set *skip)
2010 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2012 if (!scop || !skip)
2013 goto error;
2015 isl_set_free(ext->skip[type]);
2016 ext->skip[type] = skip;
2018 return scop;
2019 error:
2020 isl_set_free(skip);
2021 return pet_scop_free(scop);
2024 /* Return a copy of scop->skip[type].
2026 __isl_give isl_set *pet_scop_get_skip(struct pet_scop *scop,
2027 enum pet_skip type)
2029 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2031 if (!scop)
2032 return NULL;
2034 return isl_set_copy(ext->skip[type]);
2037 /* Return a map to the skip condition of the given type.
2039 __isl_give isl_map *pet_scop_get_skip_map(struct pet_scop *scop,
2040 enum pet_skip type)
2042 return isl_map_from_range(pet_scop_get_skip(scop, type));
2045 /* Return an access pet_expr corresponding to the skip condition
2046 * of the given type.
2048 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2049 enum pet_skip type)
2051 return pet_expr_from_access(pet_scop_get_skip_map(scop, type));
2054 /* Drop the the skip condition scop->skip[type].
2056 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2058 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2060 if (!scop)
2061 return;
2063 isl_set_free(ext->skip[type]);
2064 ext->skip[type] = NULL;
2067 /* Make the skip condition (if any) depend on the value of "test" being
2068 * equal to "satisfied".
2070 * We only support the case where the original skip condition is universal,
2071 * i.e., where skipping is unconditional, and where satisfied == 1.
2072 * In this case, the skip condition is changed to skip only when
2073 * "test" is equal to one.
2075 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2076 enum pet_skip type, __isl_keep isl_map *test, int satisfied)
2078 int is_univ = 0;
2080 if (!scop)
2081 return NULL;
2082 if (!pet_scop_has_skip(scop, type))
2083 return scop;
2085 if (satisfied)
2086 is_univ = pet_scop_has_universal_skip(scop, type);
2087 if (is_univ < 0)
2088 return pet_scop_free(scop);
2089 if (satisfied && is_univ) {
2090 scop = pet_scop_set_skip(scop, type,
2091 isl_map_range(isl_map_copy(test)));
2092 if (!scop)
2093 return NULL;
2094 } else {
2095 isl_die(isl_map_get_ctx(test), isl_error_internal,
2096 "skip expression cannot be filtered",
2097 return pet_scop_free(scop));
2100 return scop;
2103 /* Make all statements in "scop" depend on the value of "test"
2104 * being equal to "satisfied" by adjusting their domains.
2106 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2107 __isl_take isl_map *test, int satisfied)
2109 int i;
2111 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2112 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2114 if (!scop || !test)
2115 goto error;
2117 for (i = 0; i < scop->n_stmt; ++i) {
2118 scop->stmts[i] = stmt_filter(scop->stmts[i],
2119 isl_map_copy(test), satisfied);
2120 if (!scop->stmts[i])
2121 goto error;
2124 isl_map_free(test);
2125 return scop;
2126 error:
2127 isl_map_free(test);
2128 return pet_scop_free(scop);
2131 /* Do the filters "i" and "j" always have the same value?
2133 static int equal_filter_values(__isl_keep isl_set *domain, int i, int j)
2135 isl_map *map, *test;
2136 int equal;
2138 map = isl_set_unwrap(isl_set_copy(domain));
2139 test = isl_map_universe(isl_map_get_space(map));
2140 test = isl_map_equate(test, isl_dim_out, i, isl_dim_out, j);
2141 equal = isl_map_is_subset(map, test);
2142 isl_map_free(map);
2143 isl_map_free(test);
2145 return equal;
2148 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2149 * access relation, the union of the two access relations.
2151 static struct pet_stmt *merge_filter_pair(struct pet_stmt *stmt, int i, int j)
2153 int k;
2154 isl_map *map;
2156 if (!stmt)
2157 return NULL;
2159 stmt->args[i]->acc.access = isl_map_union(stmt->args[i]->acc.access,
2160 isl_map_copy(stmt->args[j]->acc.access));
2161 stmt->args[i]->acc.access = isl_map_coalesce(stmt->args[i]->acc.access);
2163 pet_expr_free(stmt->args[j]);
2164 for (k = j; k < stmt->n_arg - 1; ++k)
2165 stmt->args[k] = stmt->args[k + 1];
2166 stmt->n_arg--;
2168 map = isl_set_unwrap(stmt->domain);
2169 map = isl_map_project_out(map, isl_dim_out, j, 1);
2170 stmt->domain = isl_map_wrap(map);
2172 if (!stmt->domain || !stmt->args[i]->acc.access)
2173 return pet_stmt_free(stmt);
2175 return stmt;
2178 /* Look for any pair of filters that access the same filter variable
2179 * and that have the same filter value and merge them into a single
2180 * filter with as filter access relation the union of the filter access
2181 * relations.
2183 static struct pet_stmt *stmt_merge_filters(struct pet_stmt *stmt)
2185 int i, j;
2186 isl_space *space_i, *space_j;
2188 if (!stmt)
2189 return NULL;
2190 if (stmt->n_arg <= 1)
2191 return stmt;
2193 for (i = 0; i < stmt->n_arg - 1; ++i) {
2194 if (stmt->args[i]->type != pet_expr_access)
2195 continue;
2196 if (pet_expr_is_affine(stmt->args[i]))
2197 continue;
2199 space_i = isl_map_get_space(stmt->args[i]->acc.access);
2201 for (j = stmt->n_arg - 1; j > i; --j) {
2202 int eq;
2204 if (stmt->args[j]->type != pet_expr_access)
2205 continue;
2206 if (pet_expr_is_affine(stmt->args[j]))
2207 continue;
2209 space_j = isl_map_get_space(stmt->args[j]->acc.access);
2211 eq = isl_space_is_equal(space_i, space_j);
2212 if (eq >= 0 && eq)
2213 eq = equal_filter_values(stmt->domain, i, j);
2214 if (eq >= 0 && eq)
2215 stmt = merge_filter_pair(stmt, i, j);
2217 isl_space_free(space_j);
2219 if (eq < 0 || !stmt)
2220 break;
2223 isl_space_free(space_i);
2225 if (j > i || !stmt)
2226 return pet_stmt_free(stmt);
2229 return stmt;
2232 /* Look for any pair of filters that access the same filter variable
2233 * and that have the same filter value and merge them into a single
2234 * filter with as filter access relation the union of the filter access
2235 * relations.
2237 struct pet_scop *pet_scop_merge_filters(struct pet_scop *scop)
2239 int i;
2241 if (!scop)
2242 return NULL;
2244 for (i = 0; i < scop->n_stmt; ++i) {
2245 scop->stmts[i] = stmt_merge_filters(scop->stmts[i]);
2246 if (!scop->stmts[i])
2247 return pet_scop_free(scop);
2250 return scop;
2253 /* Add all parameters in "expr" to "dim" and return the result.
2255 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2256 __isl_take isl_space *dim)
2258 int i;
2260 if (!expr)
2261 goto error;
2262 for (i = 0; i < expr->n_arg; ++i)
2264 dim = expr_collect_params(expr->args[i], dim);
2266 if (expr->type == pet_expr_access)
2267 dim = isl_space_align_params(dim,
2268 isl_map_get_space(expr->acc.access));
2270 return dim;
2271 error:
2272 isl_space_free(dim);
2273 return pet_expr_free(expr);
2276 /* Add all parameters in "stmt" to "dim" and return the result.
2278 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2279 __isl_take isl_space *dim)
2281 if (!stmt)
2282 goto error;
2284 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2285 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2286 dim = expr_collect_params(stmt->body, dim);
2288 return dim;
2289 error:
2290 isl_space_free(dim);
2291 return pet_stmt_free(stmt);
2294 /* Add all parameters in "array" to "dim" and return the result.
2296 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2297 __isl_take isl_space *dim)
2299 if (!array)
2300 goto error;
2302 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2303 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2305 return dim;
2306 error:
2307 pet_array_free(array);
2308 return isl_space_free(dim);
2311 /* Add all parameters in "scop" to "dim" and return the result.
2313 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2314 __isl_take isl_space *dim)
2316 int i;
2318 if (!scop)
2319 goto error;
2321 for (i = 0; i < scop->n_array; ++i)
2322 dim = array_collect_params(scop->arrays[i], dim);
2324 for (i = 0; i < scop->n_stmt; ++i)
2325 dim = stmt_collect_params(scop->stmts[i], dim);
2327 return dim;
2328 error:
2329 isl_space_free(dim);
2330 return pet_scop_free(scop);
2333 /* Add all parameters in "dim" to all access relations in "expr".
2335 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2336 __isl_take isl_space *dim)
2338 int i;
2340 if (!expr)
2341 goto error;
2343 for (i = 0; i < expr->n_arg; ++i) {
2344 expr->args[i] =
2345 expr_propagate_params(expr->args[i],
2346 isl_space_copy(dim));
2347 if (!expr->args[i])
2348 goto error;
2351 if (expr->type == pet_expr_access) {
2352 expr->acc.access = isl_map_align_params(expr->acc.access,
2353 isl_space_copy(dim));
2354 if (!expr->acc.access)
2355 goto error;
2358 isl_space_free(dim);
2359 return expr;
2360 error:
2361 isl_space_free(dim);
2362 return pet_expr_free(expr);
2365 /* Add all parameters in "dim" to the domain, schedule and
2366 * all access relations in "stmt".
2368 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2369 __isl_take isl_space *dim)
2371 if (!stmt)
2372 goto error;
2374 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2375 stmt->schedule = isl_map_align_params(stmt->schedule,
2376 isl_space_copy(dim));
2377 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2379 if (!stmt->domain || !stmt->schedule || !stmt->body)
2380 goto error;
2382 isl_space_free(dim);
2383 return stmt;
2384 error:
2385 isl_space_free(dim);
2386 return pet_stmt_free(stmt);
2389 /* Add all parameters in "dim" to "array".
2391 static struct pet_array *array_propagate_params(struct pet_array *array,
2392 __isl_take isl_space *dim)
2394 if (!array)
2395 goto error;
2397 array->context = isl_set_align_params(array->context,
2398 isl_space_copy(dim));
2399 array->extent = isl_set_align_params(array->extent,
2400 isl_space_copy(dim));
2401 if (array->value_bounds) {
2402 array->value_bounds = isl_set_align_params(array->value_bounds,
2403 isl_space_copy(dim));
2404 if (!array->value_bounds)
2405 goto error;
2408 if (!array->context || !array->extent)
2409 goto error;
2411 isl_space_free(dim);
2412 return array;
2413 error:
2414 isl_space_free(dim);
2415 return pet_array_free(array);
2418 /* Add all parameters in "dim" to "scop".
2420 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2421 __isl_take isl_space *dim)
2423 int i;
2425 if (!scop)
2426 goto error;
2428 for (i = 0; i < scop->n_array; ++i) {
2429 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2430 isl_space_copy(dim));
2431 if (!scop->arrays[i])
2432 goto error;
2435 for (i = 0; i < scop->n_stmt; ++i) {
2436 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2437 isl_space_copy(dim));
2438 if (!scop->stmts[i])
2439 goto error;
2442 isl_space_free(dim);
2443 return scop;
2444 error:
2445 isl_space_free(dim);
2446 return pet_scop_free(scop);
2449 /* Update all isl_sets and isl_maps in "scop" such that they all
2450 * have the same parameters.
2452 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2454 isl_space *dim;
2456 if (!scop)
2457 return NULL;
2459 dim = isl_set_get_space(scop->context);
2460 dim = scop_collect_params(scop, dim);
2462 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
2463 scop = scop_propagate_params(scop, dim);
2465 return scop;
2468 /* Check if the given access relation accesses a (0D) array that corresponds
2469 * to one of the parameters in "dim". If so, replace the array access
2470 * by an access to the set of integers with as index (and value)
2471 * that parameter.
2473 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
2474 __isl_take isl_space *dim)
2476 isl_id *array_id = NULL;
2477 int pos = -1;
2479 if (isl_map_has_tuple_id(access, isl_dim_out)) {
2480 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2481 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
2483 isl_space_free(dim);
2485 if (pos < 0) {
2486 isl_id_free(array_id);
2487 return access;
2490 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
2491 if (pos < 0) {
2492 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
2493 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
2494 pos = 0;
2495 } else
2496 isl_id_free(array_id);
2498 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2499 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
2501 return access;
2504 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2505 * in "dim" by a value equal to the corresponding parameter.
2507 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
2508 __isl_take isl_space *dim)
2510 int i;
2512 if (!expr)
2513 goto error;
2515 for (i = 0; i < expr->n_arg; ++i) {
2516 expr->args[i] =
2517 expr_detect_parameter_accesses(expr->args[i],
2518 isl_space_copy(dim));
2519 if (!expr->args[i])
2520 goto error;
2523 if (expr->type == pet_expr_access) {
2524 expr->acc.access = access_detect_parameter(expr->acc.access,
2525 isl_space_copy(dim));
2526 if (!expr->acc.access)
2527 goto error;
2530 isl_space_free(dim);
2531 return expr;
2532 error:
2533 isl_space_free(dim);
2534 return pet_expr_free(expr);
2537 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2538 * in "dim" by a value equal to the corresponding parameter.
2540 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2541 __isl_take isl_space *dim)
2543 if (!stmt)
2544 goto error;
2546 stmt->body = expr_detect_parameter_accesses(stmt->body,
2547 isl_space_copy(dim));
2549 if (!stmt->domain || !stmt->schedule || !stmt->body)
2550 goto error;
2552 isl_space_free(dim);
2553 return stmt;
2554 error:
2555 isl_space_free(dim);
2556 return pet_stmt_free(stmt);
2559 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2560 * in "dim" by a value equal to the corresponding parameter.
2562 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2563 __isl_take isl_space *dim)
2565 int i;
2567 if (!scop)
2568 goto error;
2570 for (i = 0; i < scop->n_stmt; ++i) {
2571 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2572 isl_space_copy(dim));
2573 if (!scop->stmts[i])
2574 goto error;
2577 isl_space_free(dim);
2578 return scop;
2579 error:
2580 isl_space_free(dim);
2581 return pet_scop_free(scop);
2584 /* Replace all accesses to (0D) arrays that correspond to any of
2585 * the parameters used in "scop" by a value equal
2586 * to the corresponding parameter.
2588 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2590 isl_space *dim;
2592 if (!scop)
2593 return NULL;
2595 dim = isl_set_get_space(scop->context);
2596 dim = scop_collect_params(scop, dim);
2598 scop = scop_detect_parameter_accesses(scop, dim);
2600 return scop;
2603 /* Add all read access relations (if "read" is set) and/or all write
2604 * access relations (if "write" is set) to "accesses" and return the result.
2606 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
2607 int read, int write, __isl_take isl_union_map *accesses)
2609 int i;
2610 isl_id *id;
2611 isl_space *dim;
2613 if (!expr)
2614 return NULL;
2616 for (i = 0; i < expr->n_arg; ++i)
2617 accesses = expr_collect_accesses(expr->args[i],
2618 read, write, accesses);
2620 if (expr->type == pet_expr_access &&
2621 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
2622 ((read && expr->acc.read) || (write && expr->acc.write)))
2623 accesses = isl_union_map_add_map(accesses,
2624 isl_map_copy(expr->acc.access));
2626 return accesses;
2629 /* Collect and return all read access relations (if "read" is set)
2630 * and/or all write access relations (if "write" is set) in "stmt".
2632 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2633 int read, int write, __isl_take isl_space *dim)
2635 isl_union_map *accesses;
2637 if (!stmt)
2638 return NULL;
2640 accesses = isl_union_map_empty(dim);
2641 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
2642 accesses = isl_union_map_intersect_domain(accesses,
2643 isl_union_set_from_set(isl_set_copy(stmt->domain)));
2645 return accesses;
2648 /* Collect and return all read access relations (if "read" is set)
2649 * and/or all write access relations (if "write" is set) in "scop".
2651 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2652 int read, int write)
2654 int i;
2655 isl_union_map *accesses;
2657 if (!scop)
2658 return NULL;
2660 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2662 for (i = 0; i < scop->n_stmt; ++i) {
2663 isl_union_map *accesses_i;
2664 isl_space *dim = isl_set_get_space(scop->context);
2665 accesses_i = stmt_collect_accesses(scop->stmts[i],
2666 read, write, dim);
2667 accesses = isl_union_map_union(accesses, accesses_i);
2670 return accesses;
2673 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
2675 return scop_collect_accesses(scop, 1, 0);
2678 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
2680 return scop_collect_accesses(scop, 0, 1);
2683 /* Collect and return the union of iteration domains in "scop".
2685 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2687 int i;
2688 isl_set *domain_i;
2689 isl_union_set *domain;
2691 if (!scop)
2692 return NULL;
2694 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2696 for (i = 0; i < scop->n_stmt; ++i) {
2697 domain_i = isl_set_copy(scop->stmts[i]->domain);
2698 domain = isl_union_set_add_set(domain, domain_i);
2701 return domain;
2704 /* Collect and return the schedules of the statements in "scop".
2705 * The range is normalized to the maximal number of scheduling
2706 * dimensions.
2708 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2710 int i, j;
2711 isl_map *schedule_i;
2712 isl_union_map *schedule;
2713 int depth, max_depth = 0;
2715 if (!scop)
2716 return NULL;
2718 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2720 for (i = 0; i < scop->n_stmt; ++i) {
2721 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2722 if (depth > max_depth)
2723 max_depth = depth;
2726 for (i = 0; i < scop->n_stmt; ++i) {
2727 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2728 depth = isl_map_dim(schedule_i, isl_dim_out);
2729 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2730 max_depth - depth);
2731 for (j = depth; j < max_depth; ++j)
2732 schedule_i = isl_map_fix_si(schedule_i,
2733 isl_dim_out, j, 0);
2734 schedule = isl_union_map_add_map(schedule, schedule_i);
2737 return schedule;
2740 /* Does expression "expr" write to "id"?
2742 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
2744 int i;
2745 isl_id *write_id;
2747 for (i = 0; i < expr->n_arg; ++i) {
2748 int writes = expr_writes(expr->args[i], id);
2749 if (writes < 0 || writes)
2750 return writes;
2753 if (expr->type != pet_expr_access)
2754 return 0;
2755 if (!expr->acc.write)
2756 return 0;
2757 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
2758 return 0;
2760 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
2761 isl_id_free(write_id);
2763 if (!write_id)
2764 return -1;
2766 return write_id == id;
2769 /* Does statement "stmt" write to "id"?
2771 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
2773 return expr_writes(stmt->body, id);
2776 /* Is there any write access in "scop" that accesses "id"?
2778 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
2780 int i;
2782 if (!scop)
2783 return -1;
2785 for (i = 0; i < scop->n_stmt; ++i) {
2786 int writes = stmt_writes(scop->stmts[i], id);
2787 if (writes < 0 || writes)
2788 return writes;
2791 return 0;
2794 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2796 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
2798 int i, n;
2800 n = isl_set_dim(set, isl_dim_param);
2801 for (i = 0; i < n; ++i) {
2802 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2803 const char *name = isl_id_get_name(id);
2804 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
2805 isl_id_free(id);
2808 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
2809 isl_id *id = isl_set_get_tuple_id(set);
2810 const char *name = isl_id_get_name(id);
2811 set = isl_set_set_tuple_name(set, name);
2812 isl_id_free(id);
2815 return set;
2818 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2820 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
2822 int i, n;
2824 n = isl_map_dim(map, isl_dim_param);
2825 for (i = 0; i < n; ++i) {
2826 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2827 const char *name = isl_id_get_name(id);
2828 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2829 isl_id_free(id);
2832 if (isl_map_has_tuple_id(map, isl_dim_in)) {
2833 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
2834 const char *name = isl_id_get_name(id);
2835 map = isl_map_set_tuple_name(map, isl_dim_in, name);
2836 isl_id_free(id);
2839 if (isl_map_has_tuple_id(map, isl_dim_out)) {
2840 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
2841 const char *name = isl_id_get_name(id);
2842 map = isl_map_set_tuple_name(map, isl_dim_out, name);
2843 isl_id_free(id);
2846 return map;
2849 /* Reset the user pointer on all parameter ids in "array".
2851 static struct pet_array *array_anonymize(struct pet_array *array)
2853 if (!array)
2854 return NULL;
2856 array->context = set_anonymize(array->context);
2857 array->extent = set_anonymize(array->extent);
2858 if (!array->context || !array->extent)
2859 return pet_array_free(array);
2861 return array;
2864 /* Reset the user pointer on all parameter and tuple ids in "access".
2866 static __isl_give isl_map *access_anonymize(__isl_take isl_map *access,
2867 void *user)
2869 access = map_anonymize(access);
2871 return access;
2874 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2876 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2878 int i;
2879 isl_space *space;
2880 isl_set *domain;
2882 if (!stmt)
2883 return NULL;
2885 stmt->domain = set_anonymize(stmt->domain);
2886 stmt->schedule = map_anonymize(stmt->schedule);
2887 if (!stmt->domain || !stmt->schedule)
2888 return pet_stmt_free(stmt);
2890 for (i = 0; i < stmt->n_arg; ++i) {
2891 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2892 &access_anonymize, NULL);
2893 if (!stmt->args[i])
2894 return pet_stmt_free(stmt);
2897 stmt->body = pet_expr_foreach_access(stmt->body,
2898 &access_anonymize, NULL);
2899 if (!stmt->body)
2900 return pet_stmt_free(stmt);
2902 return stmt;
2905 /* Reset the user pointer on all parameter and tuple ids in "scop".
2907 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2909 int i;
2911 if (!scop)
2912 return NULL;
2914 scop->context = set_anonymize(scop->context);
2915 scop->context_value = set_anonymize(scop->context_value);
2916 if (!scop->context || !scop->context_value)
2917 return pet_scop_free(scop);
2919 for (i = 0; i < scop->n_array; ++i) {
2920 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2921 if (!scop->arrays[i])
2922 return pet_scop_free(scop);
2925 for (i = 0; i < scop->n_stmt; ++i) {
2926 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2927 if (!scop->stmts[i])
2928 return pet_scop_free(scop);
2931 return scop;
2934 /* Given a set "domain", return a wrapped relation with the given set
2935 * as domain and a range of dimension "n_arg", where each coordinate
2936 * is either unbounded or, if the corresponding element of args is of
2937 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2939 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2940 unsigned n_arg, struct pet_expr **args,
2941 __isl_keep isl_union_map *value_bounds)
2943 int i;
2944 isl_map *map;
2945 isl_space *space;
2946 isl_ctx *ctx = isl_set_get_ctx(domain);
2948 map = isl_map_from_domain(domain);
2949 space = isl_map_get_space(map);
2950 space = isl_space_add_dims(space, isl_dim_out, 1);
2952 for (i = 0; i < n_arg; ++i) {
2953 isl_map *map_i;
2954 struct pet_expr *arg = args[i];
2955 isl_id *id;
2956 isl_space *space2;
2958 map_i = isl_map_universe(isl_space_copy(space));
2959 if (arg->type == pet_expr_access) {
2960 isl_map *vb;
2961 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2962 space2 = isl_space_alloc(ctx, 0, 0, 1);
2963 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2964 vb = isl_union_map_extract_map(value_bounds, space2);
2965 if (!isl_map_plain_is_empty(vb))
2966 map_i = isl_map_intersect_range(map_i,
2967 isl_map_range(vb));
2968 else
2969 isl_map_free(vb);
2971 map = isl_map_flat_range_product(map, map_i);
2973 isl_space_free(space);
2975 return isl_map_wrap(map);
2978 /* Data used in access_gist() callback.
2980 struct pet_access_gist_data {
2981 isl_set *domain;
2982 isl_union_map *value_bounds;
2985 /* Given an expression "expr" of type pet_expr_access, compute
2986 * the gist of the associated access relation with respect to
2987 * data->domain and the bounds on the values of the arguments
2988 * of the expression.
2990 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
2992 struct pet_access_gist_data *data = user;
2993 isl_set *domain;
2995 domain = isl_set_copy(data->domain);
2996 if (expr->n_arg > 0)
2997 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
2998 data->value_bounds);
3000 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
3001 if (!expr->acc.access)
3002 return pet_expr_free(expr);
3004 return expr;
3007 /* Compute the gist of the iteration domain and all access relations
3008 * of "stmt" based on the constraints on the parameters specified by "context"
3009 * and the constraints on the values of nested accesses specified
3010 * by "value_bounds".
3012 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3013 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3015 int i;
3016 isl_space *space;
3017 isl_set *domain;
3018 struct pet_access_gist_data data;
3020 if (!stmt)
3021 return NULL;
3023 data.domain = isl_set_copy(stmt->domain);
3024 data.value_bounds = value_bounds;
3025 if (stmt->n_arg > 0)
3026 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3028 data.domain = isl_set_intersect_params(data.domain,
3029 isl_set_copy(context));
3031 for (i = 0; i < stmt->n_arg; ++i) {
3032 stmt->args[i] = pet_expr_foreach_access_expr(stmt->args[i],
3033 &access_gist, &data);
3034 if (!stmt->args[i])
3035 goto error;
3038 stmt->body = pet_expr_foreach_access_expr(stmt->body,
3039 &access_gist, &data);
3040 if (!stmt->body)
3041 goto error;
3043 isl_set_free(data.domain);
3045 space = isl_set_get_space(stmt->domain);
3046 if (isl_space_is_wrapping(space))
3047 space = isl_space_domain(isl_space_unwrap(space));
3048 domain = isl_set_universe(space);
3049 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3050 if (stmt->n_arg > 0)
3051 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3052 value_bounds);
3053 stmt->domain = isl_set_gist(stmt->domain, domain);
3054 if (!stmt->domain)
3055 return pet_stmt_free(stmt);
3057 return stmt;
3058 error:
3059 isl_set_free(data.domain);
3060 return pet_stmt_free(stmt);
3063 /* Compute the gist of the extent of the array
3064 * based on the constraints on the parameters specified by "context".
3066 static struct pet_array *array_gist(struct pet_array *array,
3067 __isl_keep isl_set *context)
3069 if (!array)
3070 return NULL;
3072 array->extent = isl_set_gist_params(array->extent,
3073 isl_set_copy(context));
3074 if (!array->extent)
3075 return pet_array_free(array);
3077 return array;
3080 /* Compute the gist of all sets and relations in "scop"
3081 * based on the constraints on the parameters specified by "scop->context"
3082 * and the constraints on the values of nested accesses specified
3083 * by "value_bounds".
3085 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3086 __isl_keep isl_union_map *value_bounds)
3088 int i;
3090 if (!scop)
3091 return NULL;
3093 scop->context = isl_set_coalesce(scop->context);
3094 if (!scop->context)
3095 return pet_scop_free(scop);
3097 for (i = 0; i < scop->n_array; ++i) {
3098 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3099 if (!scop->arrays[i])
3100 return pet_scop_free(scop);
3103 for (i = 0; i < scop->n_stmt; ++i) {
3104 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3105 value_bounds);
3106 if (!scop->stmts[i])
3107 return pet_scop_free(scop);
3110 return scop;
3113 /* Intersect the context of "scop" with "context".
3114 * To ensure that we don't introduce any unnamed parameters in
3115 * the context of "scop", we first remove the unnamed parameters
3116 * from "context".
3118 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3119 __isl_take isl_set *context)
3121 if (!scop)
3122 goto error;
3124 context = set_project_out_unnamed_params(context);
3125 scop->context = isl_set_intersect(scop->context, context);
3126 if (!scop->context)
3127 return pet_scop_free(scop);
3129 return scop;
3130 error:
3131 isl_set_free(context);
3132 return pet_scop_free(scop);
3135 /* Drop the current context of "scop". That is, replace the context
3136 * by a universal set.
3138 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3140 isl_space *space;
3142 if (!scop)
3143 return NULL;
3145 space = isl_set_get_space(scop->context);
3146 isl_set_free(scop->context);
3147 scop->context = isl_set_universe(space);
3148 if (!scop->context)
3149 return pet_scop_free(scop);
3151 return scop;
3154 /* Append "array" to the arrays of "scop".
3156 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3157 struct pet_array *array)
3159 isl_ctx *ctx;
3160 struct pet_array **arrays;
3162 if (!array || !scop)
3163 goto error;
3165 ctx = isl_set_get_ctx(scop->context);
3166 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3167 scop->n_array + 1);
3168 if (!arrays)
3169 goto error;
3170 scop->arrays = arrays;
3171 scop->arrays[scop->n_array] = array;
3172 scop->n_array++;
3174 return scop;
3175 error:
3176 pet_array_free(array);
3177 return pet_scop_free(scop);