update isl for missing include in interface/python.cc
[pet.git] / scop.c
blobdc873822a5dd74265a5747a845d38c94b37a28d8
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 <isl/constraint.h>
36 #include <isl/union_set.h>
38 #include "scop.h"
40 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
42 static char *type_str[] = {
43 [pet_expr_access] = "access",
44 [pet_expr_call] = "call",
45 [pet_expr_double] = "double",
46 [pet_expr_unary] = "unary",
47 [pet_expr_binary] = "binary",
48 [pet_expr_ternary] = "ternary"
51 static char *op_str[] = {
52 [pet_op_add_assign] = "+=",
53 [pet_op_sub_assign] = "-=",
54 [pet_op_mul_assign] = "*=",
55 [pet_op_div_assign] = "/=",
56 [pet_op_assign] = "=",
57 [pet_op_add] = "+",
58 [pet_op_sub] = "-",
59 [pet_op_mul] = "*",
60 [pet_op_div] = "/",
61 [pet_op_mod] = "%",
62 [pet_op_eq] = "==",
63 [pet_op_le] = "<=",
64 [pet_op_lt] = "<",
65 [pet_op_gt] = ">",
66 [pet_op_minus] = "-",
67 [pet_op_post_inc] = "++",
68 [pet_op_post_dec] = "--",
69 [pet_op_pre_inc] = "++",
70 [pet_op_pre_dec] = "--",
71 [pet_op_address_of] = "&",
72 [pet_op_kill] = "kill"
75 /* pet_scop with extra information that is only used during parsing.
77 * In particular, we keep track of conditions under which we want
78 * to skip the rest of the current loop iteration (skip[pet_skip_now])
79 * and of conditions under which we want to skip subsequent
80 * loop iterations (skip[pet_skip_later]).
82 * The conditions are represented either by a variable, which
83 * is assumed to attain values zero and one, or by a boolean affine
84 * expression. The condition holds if the variable has value one
85 * or if the affine expression has value one (typically for only
86 * part of the parameter space).
88 * A missing condition (skip[type] == NULL) means that we don't want
89 * to skip anything.
91 struct pet_scop_ext {
92 struct pet_scop scop;
94 isl_set *skip[2];
97 const char *pet_op_str(enum pet_op_type op)
99 return op_str[op];
102 int pet_op_is_inc_dec(enum pet_op_type op)
104 return op == pet_op_post_inc || op == pet_op_post_dec ||
105 op == pet_op_pre_inc || op == pet_op_pre_dec;
108 const char *pet_type_str(enum pet_expr_type type)
110 return type_str[type];
113 enum pet_op_type pet_str_op(const char *str)
115 int i;
117 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
118 if (!strcmp(op_str[i], str))
119 return i;
121 return -1;
124 enum pet_expr_type pet_str_type(const char *str)
126 int i;
128 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
129 if (!strcmp(type_str[i], str))
130 return i;
132 return -1;
135 /* Construct a pet_expr from an access relation.
136 * By default, it is considered to be a read access.
138 struct pet_expr *pet_expr_from_access(__isl_take isl_map *access)
140 isl_ctx *ctx = isl_map_get_ctx(access);
141 struct pet_expr *expr;
143 if (!access)
144 return NULL;
145 expr = isl_calloc_type(ctx, struct pet_expr);
146 if (!expr)
147 goto error;
149 expr->type = pet_expr_access;
150 expr->acc.access = access;
151 expr->acc.read = 1;
152 expr->acc.write = 0;
154 return expr;
155 error:
156 isl_map_free(access);
157 return NULL;
160 /* Construct a pet_expr that kills the elements specified by "access".
162 struct pet_expr *pet_expr_kill_from_access(__isl_take isl_map *access)
164 isl_ctx *ctx;
165 struct pet_expr *expr;
167 ctx = isl_map_get_ctx(access);
168 expr = pet_expr_from_access(access);
169 if (!expr)
170 return NULL;
171 expr->acc.read = 0;
172 return pet_expr_new_unary(ctx, pet_op_kill, expr);
175 /* Construct a unary pet_expr that performs "op" on "arg".
177 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
178 struct pet_expr *arg)
180 struct pet_expr *expr;
182 if (!arg)
183 goto error;
184 expr = isl_alloc_type(ctx, struct pet_expr);
185 if (!expr)
186 goto error;
188 expr->type = pet_expr_unary;
189 expr->op = op;
190 expr->n_arg = 1;
191 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
192 if (!expr->args)
193 goto error;
194 expr->args[pet_un_arg] = arg;
196 return expr;
197 error:
198 pet_expr_free(arg);
199 return NULL;
202 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
204 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
205 struct pet_expr *lhs, struct pet_expr *rhs)
207 struct pet_expr *expr;
209 if (!lhs || !rhs)
210 goto error;
211 expr = isl_alloc_type(ctx, struct pet_expr);
212 if (!expr)
213 goto error;
215 expr->type = pet_expr_binary;
216 expr->op = op;
217 expr->n_arg = 2;
218 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
219 if (!expr->args)
220 goto error;
221 expr->args[pet_bin_lhs] = lhs;
222 expr->args[pet_bin_rhs] = rhs;
224 return expr;
225 error:
226 pet_expr_free(lhs);
227 pet_expr_free(rhs);
228 return NULL;
231 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
233 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
234 struct pet_expr *lhs, struct pet_expr *rhs)
236 struct pet_expr *expr;
238 if (!cond || !lhs || !rhs)
239 goto error;
240 expr = isl_alloc_type(ctx, struct pet_expr);
241 if (!expr)
242 goto error;
244 expr->type = pet_expr_ternary;
245 expr->n_arg = 3;
246 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
247 if (!expr->args)
248 goto error;
249 expr->args[pet_ter_cond] = cond;
250 expr->args[pet_ter_true] = lhs;
251 expr->args[pet_ter_false] = rhs;
253 return expr;
254 error:
255 pet_expr_free(cond);
256 pet_expr_free(lhs);
257 pet_expr_free(rhs);
258 return NULL;
261 /* Construct a call pet_expr that calls function "name" with "n_arg"
262 * arguments. The caller is responsible for filling in the arguments.
264 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
265 unsigned n_arg)
267 struct pet_expr *expr;
269 expr = isl_alloc_type(ctx, struct pet_expr);
270 if (!expr)
271 return NULL;
273 expr->type = pet_expr_call;
274 expr->n_arg = n_arg;
275 expr->name = strdup(name);
276 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
277 if (!expr->name || !expr->args)
278 return pet_expr_free(expr);
280 return expr;
283 /* Construct a pet_expr that represents the double "d".
285 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
287 struct pet_expr *expr;
289 expr = isl_calloc_type(ctx, struct pet_expr);
290 if (!expr)
291 return NULL;
293 expr->type = pet_expr_double;
294 expr->d.val = val;
295 expr->d.s = strdup(s);
296 if (!expr->d.s)
297 return pet_expr_free(expr);
299 return expr;
302 void *pet_expr_free(struct pet_expr *expr)
304 int i;
306 if (!expr)
307 return NULL;
309 for (i = 0; i < expr->n_arg; ++i)
310 pet_expr_free(expr->args[i]);
311 free(expr->args);
313 switch (expr->type) {
314 case pet_expr_access:
315 isl_map_free(expr->acc.access);
316 break;
317 case pet_expr_call:
318 free(expr->name);
319 break;
320 case pet_expr_double:
321 free(expr->d.s);
322 break;
323 case pet_expr_unary:
324 case pet_expr_binary:
325 case pet_expr_ternary:
326 break;
329 free(expr);
330 return NULL;
333 static void expr_dump(struct pet_expr *expr, int indent)
335 int i;
337 if (!expr)
338 return;
340 fprintf(stderr, "%*s", indent, "");
342 switch (expr->type) {
343 case pet_expr_double:
344 fprintf(stderr, "%s\n", expr->d.s);
345 break;
346 case pet_expr_access:
347 isl_map_dump(expr->acc.access);
348 fprintf(stderr, "%*sread: %d\n", indent + 2,
349 "", expr->acc.read);
350 fprintf(stderr, "%*swrite: %d\n", indent + 2,
351 "", expr->acc.write);
352 for (i = 0; i < expr->n_arg; ++i)
353 expr_dump(expr->args[i], indent + 2);
354 break;
355 case pet_expr_unary:
356 fprintf(stderr, "%s\n", op_str[expr->op]);
357 expr_dump(expr->args[pet_un_arg], indent + 2);
358 break;
359 case pet_expr_binary:
360 fprintf(stderr, "%s\n", op_str[expr->op]);
361 expr_dump(expr->args[pet_bin_lhs], indent + 2);
362 expr_dump(expr->args[pet_bin_rhs], indent + 2);
363 break;
364 case pet_expr_ternary:
365 fprintf(stderr, "?:\n");
366 expr_dump(expr->args[pet_ter_cond], indent + 2);
367 expr_dump(expr->args[pet_ter_true], indent + 2);
368 expr_dump(expr->args[pet_ter_false], indent + 2);
369 break;
370 case pet_expr_call:
371 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
372 for (i = 0; i < expr->n_arg; ++i)
373 expr_dump(expr->args[i], indent + 2);
374 break;
378 void pet_expr_dump(struct pet_expr *expr)
380 expr_dump(expr, 0);
383 /* Does "expr" represent an access to an unnamed space, i.e.,
384 * does it represent an affine expression?
386 int pet_expr_is_affine(struct pet_expr *expr)
388 int has_id;
390 if (!expr)
391 return -1;
392 if (expr->type != pet_expr_access)
393 return 0;
395 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
396 if (has_id < 0)
397 return -1;
399 return !has_id;
402 /* Return 1 if the two pet_exprs are equivalent.
404 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
406 int i;
408 if (!expr1 || !expr2)
409 return 0;
411 if (expr1->type != expr2->type)
412 return 0;
413 if (expr1->n_arg != expr2->n_arg)
414 return 0;
415 for (i = 0; i < expr1->n_arg; ++i)
416 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
417 return 0;
418 switch (expr1->type) {
419 case pet_expr_double:
420 if (strcmp(expr1->d.s, expr2->d.s))
421 return 0;
422 if (expr1->d.val != expr2->d.val)
423 return 0;
424 break;
425 case pet_expr_access:
426 if (expr1->acc.read != expr2->acc.read)
427 return 0;
428 if (expr1->acc.write != expr2->acc.write)
429 return 0;
430 if (!expr1->acc.access || !expr2->acc.access)
431 return 0;
432 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
433 return 0;
434 break;
435 case pet_expr_unary:
436 case pet_expr_binary:
437 case pet_expr_ternary:
438 if (expr1->op != expr2->op)
439 return 0;
440 break;
441 case pet_expr_call:
442 if (strcmp(expr1->name, expr2->name))
443 return 0;
444 break;
447 return 1;
450 /* Add extra conditions on the parameters to all access relations in "expr".
452 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
453 __isl_take isl_set *cond)
455 int i;
457 if (!expr)
458 goto error;
460 for (i = 0; i < expr->n_arg; ++i) {
461 expr->args[i] = pet_expr_restrict(expr->args[i],
462 isl_set_copy(cond));
463 if (!expr->args[i])
464 goto error;
467 if (expr->type == pet_expr_access) {
468 expr->acc.access = isl_map_intersect_params(expr->acc.access,
469 isl_set_copy(cond));
470 if (!expr->acc.access)
471 goto error;
474 isl_set_free(cond);
475 return expr;
476 error:
477 isl_set_free(cond);
478 return pet_expr_free(expr);
481 /* Modify all access relations in "expr" by calling "fn" on them.
483 struct pet_expr *pet_expr_foreach_access(struct pet_expr *expr,
484 __isl_give isl_map *(*fn)(__isl_take isl_map *access, void *user),
485 void *user)
487 int i;
489 if (!expr)
490 return NULL;
492 for (i = 0; i < expr->n_arg; ++i) {
493 expr->args[i] = pet_expr_foreach_access(expr->args[i], fn, user);
494 if (!expr->args[i])
495 return pet_expr_free(expr);
498 if (expr->type == pet_expr_access) {
499 expr->acc.access = fn(expr->acc.access, user);
500 if (!expr->acc.access)
501 return pet_expr_free(expr);
504 return expr;
507 /* Modify all expressions of type pet_expr_access in "expr"
508 * by calling "fn" on them.
510 struct pet_expr *pet_expr_foreach_access_expr(struct pet_expr *expr,
511 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
512 void *user)
514 int i;
516 if (!expr)
517 return NULL;
519 for (i = 0; i < expr->n_arg; ++i) {
520 expr->args[i] = pet_expr_foreach_access_expr(expr->args[i],
521 fn, user);
522 if (!expr->args[i])
523 return pet_expr_free(expr);
526 if (expr->type == pet_expr_access)
527 expr = fn(expr, user);
529 return expr;
532 /* Modify the given access relation based on the given iteration space
533 * transformation.
534 * If the access has any arguments then the domain of the access relation
535 * is a wrapped mapping from the iteration space to the space of
536 * argument values. We only need to change the domain of this wrapped
537 * mapping, so we extend the input transformation with an identity mapping
538 * on the space of argument values.
540 static __isl_give isl_map *update_domain(__isl_take isl_map *access,
541 void *user)
543 isl_map *update = user;
544 isl_space *dim;
546 update = isl_map_copy(update);
548 dim = isl_map_get_space(access);
549 dim = isl_space_domain(dim);
550 if (!isl_space_is_wrapping(dim))
551 isl_space_free(dim);
552 else {
553 isl_map *id;
554 dim = isl_space_unwrap(dim);
555 dim = isl_space_range(dim);
556 dim = isl_space_map_from_set(dim);
557 id = isl_map_identity(dim);
558 update = isl_map_product(update, id);
561 return isl_map_apply_domain(access, update);
564 /* Modify all access relations in "expr" based on the given iteration space
565 * transformation.
567 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
568 __isl_take isl_map *update)
570 expr = pet_expr_foreach_access(expr, &update_domain, update);
571 isl_map_free(update);
572 return expr;
575 /* Construct a pet_stmt with given line number and statement
576 * number from a pet_expr.
577 * The initial iteration domain is the zero-dimensional universe.
578 * The name of the domain is given by "label" if it is non-NULL.
579 * Otherwise, the name is constructed as S_<id>.
580 * The domains of all access relations are modified to refer
581 * to the statement iteration domain.
583 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
584 __isl_take isl_id *label, int id, struct pet_expr *expr)
586 struct pet_stmt *stmt;
587 isl_space *dim;
588 isl_set *dom;
589 isl_map *sched;
590 isl_map *add_name;
591 char name[50];
593 if (!expr)
594 goto error;
596 stmt = isl_calloc_type(ctx, struct pet_stmt);
597 if (!stmt)
598 goto error;
600 dim = isl_space_set_alloc(ctx, 0, 0);
601 if (label)
602 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
603 else {
604 snprintf(name, sizeof(name), "S_%d", id);
605 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
607 dom = isl_set_universe(isl_space_copy(dim));
608 sched = isl_map_from_domain(isl_set_copy(dom));
610 dim = isl_space_from_range(dim);
611 add_name = isl_map_universe(dim);
612 expr = expr_update_domain(expr, add_name);
614 stmt->line = line;
615 stmt->domain = dom;
616 stmt->schedule = sched;
617 stmt->body = expr;
619 if (!stmt->domain || !stmt->schedule || !stmt->body)
620 return pet_stmt_free(stmt);
622 return stmt;
623 error:
624 isl_id_free(label);
625 return pet_expr_free(expr);
628 void *pet_stmt_free(struct pet_stmt *stmt)
630 int i;
632 if (!stmt)
633 return NULL;
635 isl_set_free(stmt->domain);
636 isl_map_free(stmt->schedule);
637 pet_expr_free(stmt->body);
639 for (i = 0; i < stmt->n_arg; ++i)
640 pet_expr_free(stmt->args[i]);
641 free(stmt->args);
643 free(stmt);
644 return NULL;
647 static void stmt_dump(struct pet_stmt *stmt, int indent)
649 int i;
651 if (!stmt)
652 return;
654 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
655 fprintf(stderr, "%*s", indent, "");
656 isl_set_dump(stmt->domain);
657 fprintf(stderr, "%*s", indent, "");
658 isl_map_dump(stmt->schedule);
659 expr_dump(stmt->body, indent);
660 for (i = 0; i < stmt->n_arg; ++i)
661 expr_dump(stmt->args[i], indent + 2);
664 void pet_stmt_dump(struct pet_stmt *stmt)
666 stmt_dump(stmt, 0);
669 struct pet_array *pet_array_free(struct pet_array *array)
671 if (!array)
672 return NULL;
674 isl_set_free(array->context);
675 isl_set_free(array->extent);
676 isl_set_free(array->value_bounds);
677 free(array->element_type);
679 free(array);
680 return NULL;
683 void pet_array_dump(struct pet_array *array)
685 if (!array)
686 return;
688 isl_set_dump(array->context);
689 isl_set_dump(array->extent);
690 isl_set_dump(array->value_bounds);
691 fprintf(stderr, "%s %s\n", array->element_type,
692 array->live_out ? "live-out" : "");
695 /* Alloc a pet_scop structure, with extra room for information that
696 * is only used during parsing.
698 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
700 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
703 /* Construct a pet_scop with room for n statements.
705 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
707 isl_space *space;
708 struct pet_scop *scop;
710 scop = pet_scop_alloc(ctx);
711 if (!scop)
712 return NULL;
714 space = isl_space_params_alloc(ctx, 0);
715 scop->context = isl_set_universe(isl_space_copy(space));
716 scop->context_value = isl_set_universe(space);
717 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
718 if (!scop->context || !scop->stmts)
719 return pet_scop_free(scop);
721 scop->n_stmt = n;
723 return scop;
726 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
728 return scop_alloc(ctx, 0);
731 /* Update "context" with respect to the valid parameter values for "access".
733 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
734 __isl_take isl_set *context)
736 context = isl_set_intersect(context,
737 isl_map_params(isl_map_copy(access)));
738 return context;
741 /* Update "context" with respect to the valid parameter values for "expr".
743 * If "expr" represents a ternary operator, then a parameter value
744 * needs to be valid for the condition and for at least one of the
745 * remaining two arguments.
746 * If the condition is an affine expression, then we can be a bit more specific.
747 * The parameter then has to be valid for the second argument for
748 * non-zero accesses and valid for the third argument for zero accesses.
750 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
751 __isl_take isl_set *context)
753 int i;
755 if (expr->type == pet_expr_ternary) {
756 int is_aff;
757 isl_set *context1, *context2;
759 is_aff = pet_expr_is_affine(expr->args[0]);
760 if (is_aff < 0)
761 goto error;
763 context = expr_extract_context(expr->args[0], context);
764 context1 = expr_extract_context(expr->args[1],
765 isl_set_copy(context));
766 context2 = expr_extract_context(expr->args[2], context);
768 if (is_aff) {
769 isl_map *access;
770 isl_set *zero_set;
772 access = isl_map_copy(expr->args[0]->acc.access);
773 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
774 zero_set = isl_map_params(access);
775 context1 = isl_set_subtract(context1,
776 isl_set_copy(zero_set));
777 context2 = isl_set_intersect(context2, zero_set);
780 context = isl_set_union(context1, context2);
781 context = isl_set_coalesce(context);
783 return context;
786 for (i = 0; i < expr->n_arg; ++i)
787 context = expr_extract_context(expr->args[i], context);
789 if (expr->type == pet_expr_access)
790 context = access_extract_context(expr->acc.access, context);
792 return context;
793 error:
794 isl_set_free(context);
795 return NULL;
798 /* Update "context" with respect to the valid parameter values for "stmt".
800 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
801 __isl_take isl_set *context)
803 int i;
805 for (i = 0; i < stmt->n_arg; ++i)
806 context = expr_extract_context(stmt->args[i], context);
808 context = expr_extract_context(stmt->body, context);
810 return context;
813 /* Construct a pet_scop that contains the given pet_stmt.
815 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
817 struct pet_scop *scop;
819 if (!stmt)
820 return NULL;
822 scop = scop_alloc(ctx, 1);
824 scop->context = stmt_extract_context(stmt, scop->context);
825 if (!scop->context)
826 goto error;
828 scop->stmts[0] = stmt;
830 return scop;
831 error:
832 pet_stmt_free(stmt);
833 pet_scop_free(scop);
834 return NULL;
837 /* Does "set" represent an element of an unnamed space, i.e.,
838 * does it represent an affine expression?
840 static int set_is_affine(__isl_keep isl_set *set)
842 int has_id;
844 has_id = isl_set_has_tuple_id(set);
845 if (has_id < 0)
846 return -1;
848 return !has_id;
851 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
852 * ext may be equal to either ext1 or ext2.
854 * The two skips that need to be combined are assumed to be affine expressions.
856 * We need to skip in ext if we need to skip in either ext1 or ext2.
857 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
859 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
860 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
861 enum pet_skip type)
863 isl_set *set, *skip1, *skip2;
865 if (!ext)
866 return NULL;
867 if (!ext1->skip[type] && !ext2->skip[type])
868 return ext;
869 if (!ext1->skip[type]) {
870 if (ext == ext2)
871 return ext;
872 ext->skip[type] = ext2->skip[type];
873 ext2->skip[type] = NULL;
874 return ext;
876 if (!ext2->skip[type]) {
877 if (ext == ext1)
878 return ext;
879 ext->skip[type] = ext1->skip[type];
880 ext1->skip[type] = NULL;
881 return ext;
884 if (!set_is_affine(ext1->skip[type]) ||
885 !set_is_affine(ext2->skip[type]))
886 isl_die(isl_set_get_ctx(ext1->skip[type]), isl_error_internal,
887 "can only combine affine skips",
888 return pet_scop_free(&ext->scop));
890 skip1 = isl_set_copy(ext1->skip[type]);
891 skip2 = isl_set_copy(ext2->skip[type]);
892 set = isl_set_intersect(
893 isl_set_fix_si(isl_set_copy(skip1), isl_dim_set, 0, 0),
894 isl_set_fix_si(isl_set_copy(skip2), isl_dim_set, 0, 0));
895 set = isl_set_union(set, isl_set_fix_si(skip1, isl_dim_set, 0, 1));
896 set = isl_set_union(set, isl_set_fix_si(skip2, isl_dim_set, 0, 1));
897 set = isl_set_coalesce(set);
898 isl_set_free(ext1->skip[type]);
899 ext1->skip[type] = NULL;
900 isl_set_free(ext2->skip[type]);
901 ext2->skip[type] = NULL;
902 ext->skip[type] = set;
903 if (!ext->skip[type])
904 return pet_scop_free(&ext->scop);
906 return ext;
909 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
910 * where type takes on the values pet_skip_now and pet_skip_later.
911 * scop may be equal to either scop1 or scop2.
913 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
914 struct pet_scop *scop1, struct pet_scop *scop2)
916 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
917 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
918 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
920 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
921 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
922 return &ext->scop;
925 /* Construct a pet_scop that contains the arrays, statements and
926 * skip information in "scop1" and "scop2".
928 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
929 struct pet_scop *scop2)
931 int i;
932 struct pet_scop *scop;
934 if (!scop1 || !scop2)
935 goto error;
937 if (scop1->n_stmt == 0) {
938 scop2 = scop_combine_skips(scop2, scop1, scop2);
939 pet_scop_free(scop1);
940 return scop2;
943 if (scop2->n_stmt == 0) {
944 scop1 = scop_combine_skips(scop1, scop1, scop2);
945 pet_scop_free(scop2);
946 return scop1;
949 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
950 if (!scop)
951 goto error;
953 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
954 scop1->n_array + scop2->n_array);
955 if (!scop->arrays)
956 goto error;
957 scop->n_array = scop1->n_array + scop2->n_array;
959 for (i = 0; i < scop1->n_stmt; ++i) {
960 scop->stmts[i] = scop1->stmts[i];
961 scop1->stmts[i] = NULL;
964 for (i = 0; i < scop2->n_stmt; ++i) {
965 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
966 scop2->stmts[i] = NULL;
969 for (i = 0; i < scop1->n_array; ++i) {
970 scop->arrays[i] = scop1->arrays[i];
971 scop1->arrays[i] = NULL;
974 for (i = 0; i < scop2->n_array; ++i) {
975 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
976 scop2->arrays[i] = NULL;
979 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
980 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
981 scop = scop_combine_skips(scop, scop1, scop2);
983 pet_scop_free(scop1);
984 pet_scop_free(scop2);
985 return scop;
986 error:
987 pet_scop_free(scop1);
988 pet_scop_free(scop2);
989 return NULL;
992 /* Apply the skip condition "skip" to "scop".
993 * That is, make sure "scop" is not executed when the condition holds.
995 * If "skip" is an affine expression, we add the conditions under
996 * which the expression is zero to the iteration domains.
997 * Otherwise, we add a filter on the variable attaining the value zero.
999 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1000 __isl_take isl_set *skip)
1002 isl_map *skip_map;
1003 int is_aff;
1005 if (!scop || !skip)
1006 goto error;
1008 is_aff = set_is_affine(skip);
1009 if (is_aff < 0)
1010 goto error;
1012 if (!is_aff)
1013 return pet_scop_filter(scop, isl_map_from_range(skip), 0);
1015 skip = isl_set_fix_si(skip, isl_dim_set, 0, 0);
1016 scop = pet_scop_restrict(scop, isl_set_params(skip));
1018 return scop;
1019 error:
1020 isl_set_free(skip);
1021 return pet_scop_free(scop);
1024 /* Construct a pet_scop that contains the arrays, statements and
1025 * skip information in "scop1" and "scop2", where the two scops
1026 * are executed "in sequence". That is, breaks and continues
1027 * in scop1 have an effect on scop2.
1029 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1030 struct pet_scop *scop2)
1032 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1033 scop2 = restrict_skip(scop2,
1034 pet_scop_get_skip(scop1, pet_skip_now));
1035 return pet_scop_add(ctx, scop1, scop2);
1038 /* Construct a pet_scop that contains the arrays, statements and
1039 * skip information in "scop1" and "scop2", where the two scops
1040 * are executed "in parallel". That is, any break or continue
1041 * in scop1 has no effect on scop2.
1043 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1044 struct pet_scop *scop2)
1046 return pet_scop_add(ctx, scop1, scop2);
1049 void *pet_scop_free(struct pet_scop *scop)
1051 int i;
1052 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1054 if (!scop)
1055 return NULL;
1056 isl_set_free(scop->context);
1057 isl_set_free(scop->context_value);
1058 if (scop->arrays)
1059 for (i = 0; i < scop->n_array; ++i)
1060 pet_array_free(scop->arrays[i]);
1061 free(scop->arrays);
1062 if (scop->stmts)
1063 for (i = 0; i < scop->n_stmt; ++i)
1064 pet_stmt_free(scop->stmts[i]);
1065 free(scop->stmts);
1066 isl_set_free(ext->skip[pet_skip_now]);
1067 isl_set_free(ext->skip[pet_skip_later]);
1068 free(scop);
1069 return NULL;
1072 void pet_scop_dump(struct pet_scop *scop)
1074 int i;
1075 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1077 if (!scop)
1078 return;
1080 isl_set_dump(scop->context);
1081 isl_set_dump(scop->context_value);
1082 for (i = 0; i < scop->n_array; ++i)
1083 pet_array_dump(scop->arrays[i]);
1084 for (i = 0; i < scop->n_stmt; ++i)
1085 pet_stmt_dump(scop->stmts[i]);
1087 if (ext->skip[0]) {
1088 fprintf(stderr, "skip\n");
1089 isl_set_dump(ext->skip[0]);
1090 isl_set_dump(ext->skip[1]);
1094 /* Return 1 if the two pet_arrays are equivalent.
1096 * We don't compare element_size as this may be target dependent.
1098 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1100 if (!array1 || !array2)
1101 return 0;
1103 if (!isl_set_is_equal(array1->context, array2->context))
1104 return 0;
1105 if (!isl_set_is_equal(array1->extent, array2->extent))
1106 return 0;
1107 if (!!array1->value_bounds != !!array2->value_bounds)
1108 return 0;
1109 if (array1->value_bounds &&
1110 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1111 return 0;
1112 if (strcmp(array1->element_type, array2->element_type))
1113 return 0;
1114 if (array1->live_out != array2->live_out)
1115 return 0;
1116 if (array1->uniquely_defined != array2->uniquely_defined)
1117 return 0;
1118 if (array1->declared != array2->declared)
1119 return 0;
1120 if (array1->exposed != array2->exposed)
1121 return 0;
1123 return 1;
1126 /* Return 1 if the two pet_stmts are equivalent.
1128 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1130 int i;
1132 if (!stmt1 || !stmt2)
1133 return 0;
1135 if (stmt1->line != stmt2->line)
1136 return 0;
1137 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1138 return 0;
1139 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1140 return 0;
1141 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1142 return 0;
1143 if (stmt1->n_arg != stmt2->n_arg)
1144 return 0;
1145 for (i = 0; i < stmt1->n_arg; ++i) {
1146 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1147 return 0;
1150 return 1;
1153 /* Return 1 if the two pet_scops are equivalent.
1155 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1157 int i;
1159 if (!scop1 || !scop2)
1160 return 0;
1162 if (!isl_set_is_equal(scop1->context, scop2->context))
1163 return 0;
1164 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1165 return 0;
1167 if (scop1->n_array != scop2->n_array)
1168 return 0;
1169 for (i = 0; i < scop1->n_array; ++i)
1170 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1171 return 0;
1173 if (scop1->n_stmt != scop2->n_stmt)
1174 return 0;
1175 for (i = 0; i < scop1->n_stmt; ++i)
1176 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1177 return 0;
1179 return 1;
1182 /* Prefix the schedule of "stmt" with an extra dimension with constant
1183 * value "pos".
1185 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1187 if (!stmt)
1188 return NULL;
1190 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1191 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1192 if (!stmt->schedule)
1193 return pet_stmt_free(stmt);
1195 return stmt;
1198 /* Prefix the schedules of all statements in "scop" with an extra
1199 * dimension with constant value "pos".
1201 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1203 int i;
1205 if (!scop)
1206 return NULL;
1208 for (i = 0; i < scop->n_stmt; ++i) {
1209 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1210 if (!scop->stmts[i])
1211 return pet_scop_free(scop);
1214 return scop;
1217 /* Given a set with a parameter at "param_pos" that refers to the
1218 * iterator, "move" the iterator to the first set dimension.
1219 * That is, essentially equate the parameter to the first set dimension
1220 * and then project it out.
1222 * The first set dimension may however refer to a virtual iterator,
1223 * while the parameter refers to the "real" iterator.
1224 * We therefore need to take into account the mapping "iv_map", which
1225 * maps the virtual iterator to the real iterator.
1226 * In particular, we equate the set dimension to the input of the map
1227 * and the parameter to the output of the map and then project out
1228 * everything we don't need anymore.
1230 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1231 int param_pos, __isl_take isl_map *iv_map)
1233 isl_map *map;
1234 map = isl_map_from_domain(set);
1235 map = isl_map_add_dims(map, isl_dim_out, 1);
1236 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1237 iv_map = isl_map_align_params(iv_map, isl_map_get_space(map));
1238 map = isl_map_apply_range(map, iv_map);
1239 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1240 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1241 return isl_map_domain(map);
1244 /* Data used in embed_access.
1245 * extend adds an iterator to the iteration domain
1246 * iv_map maps the virtual iterator to the real iterator
1247 * var_id represents the induction variable of the corresponding loop
1249 struct pet_embed_access {
1250 isl_map *extend;
1251 isl_map *iv_map;
1252 isl_id *var_id;
1255 /* Embed the access relation in an extra outer loop.
1257 * We first update the iteration domain to insert the extra dimension.
1259 * If the access refers to the induction variable, then it is
1260 * turned into an access to the set of integers with index (and value)
1261 * equal to the induction variable.
1263 * If the induction variable appears in the constraints (as a parameter),
1264 * then the parameter is equated to the newly introduced iteration
1265 * domain dimension and subsequently projected out.
1267 * Similarly, if the accessed array is a virtual array (with user
1268 * pointer equal to NULL), as created by create_test_access,
1269 * then it is extended along with the domain of the access.
1271 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
1272 void *user)
1274 struct pet_embed_access *data = user;
1275 isl_id *array_id = NULL;
1276 int pos;
1278 access = update_domain(access, data->extend);
1280 if (isl_map_has_tuple_id(access, isl_dim_out))
1281 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1282 if (array_id == data->var_id ||
1283 (array_id && !isl_id_get_user(array_id))) {
1284 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1285 access = isl_map_equate(access,
1286 isl_dim_in, 0, isl_dim_out, 0);
1287 if (array_id == data->var_id)
1288 access = isl_map_apply_range(access,
1289 isl_map_copy(data->iv_map));
1290 else
1291 access = isl_map_set_tuple_id(access, isl_dim_out,
1292 isl_id_copy(array_id));
1294 isl_id_free(array_id);
1296 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1297 if (pos >= 0) {
1298 isl_set *set = isl_map_wrap(access);
1299 set = internalize_iv(set, pos, isl_map_copy(data->iv_map));
1300 access = isl_set_unwrap(set);
1302 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1303 isl_id_copy(data->var_id));
1305 return access;
1308 /* Embed all access relations in "expr" in an extra loop.
1309 * "extend" inserts an outer loop iterator in the iteration domains.
1310 * "iv_map" maps the virtual iterator to the real iterator
1311 * "var_id" represents the induction variable.
1313 static struct pet_expr *expr_embed(struct pet_expr *expr,
1314 __isl_take isl_map *extend, __isl_take isl_map *iv_map,
1315 __isl_keep isl_id *var_id)
1317 struct pet_embed_access data =
1318 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1320 expr = pet_expr_foreach_access(expr, &embed_access, &data);
1321 isl_map_free(iv_map);
1322 isl_map_free(extend);
1323 return expr;
1326 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1327 * "dom" and schedule "sched". "var_id" represents the induction variable
1328 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1329 * That is, it maps the iterator used in "dom" and the domain of "sched"
1330 * to the iterator that some of the parameters in "stmt" may refer to.
1332 * The iteration domain and schedule of the statement are updated
1333 * according to the iteration domain and schedule of the new loop.
1334 * If stmt->domain is a wrapped map, then the iteration domain
1335 * is the domain of this map, so we need to be careful to adjust
1336 * this domain.
1338 * If the induction variable appears in the constraints (as a parameter)
1339 * of the current iteration domain or the schedule of the statement,
1340 * then the parameter is equated to the newly introduced iteration
1341 * domain dimension and subsequently projected out.
1343 * Finally, all access relations are updated based on the extra loop.
1345 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1346 __isl_take isl_set *dom, __isl_take isl_map *sched,
1347 __isl_take isl_map *iv_map, __isl_take isl_id *var_id)
1349 int i;
1350 int pos;
1351 isl_id *stmt_id;
1352 isl_space *dim;
1353 isl_map *extend;
1355 if (!stmt)
1356 goto error;
1358 if (isl_set_is_wrapping(stmt->domain)) {
1359 isl_map *map;
1360 isl_map *ext;
1361 isl_space *ran_dim;
1363 map = isl_set_unwrap(stmt->domain);
1364 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1365 ran_dim = isl_space_range(isl_map_get_space(map));
1366 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1367 isl_set_universe(ran_dim));
1368 map = isl_map_flat_domain_product(ext, map);
1369 map = isl_map_set_tuple_id(map, isl_dim_in,
1370 isl_id_copy(stmt_id));
1371 dim = isl_space_domain(isl_map_get_space(map));
1372 stmt->domain = isl_map_wrap(map);
1373 } else {
1374 stmt_id = isl_set_get_tuple_id(stmt->domain);
1375 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1376 stmt->domain);
1377 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1378 isl_id_copy(stmt_id));
1379 dim = isl_set_get_space(stmt->domain);
1382 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1383 if (pos >= 0)
1384 stmt->domain = internalize_iv(stmt->domain, pos,
1385 isl_map_copy(iv_map));
1387 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1388 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1389 isl_dim_in, stmt_id);
1391 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1392 if (pos >= 0) {
1393 isl_set *set = isl_map_wrap(stmt->schedule);
1394 set = internalize_iv(set, pos, isl_map_copy(iv_map));
1395 stmt->schedule = isl_set_unwrap(set);
1398 dim = isl_space_map_from_set(dim);
1399 extend = isl_map_identity(dim);
1400 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1401 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1402 isl_map_get_tuple_id(extend, isl_dim_out));
1403 for (i = 0; i < stmt->n_arg; ++i)
1404 stmt->args[i] = expr_embed(stmt->args[i], isl_map_copy(extend),
1405 isl_map_copy(iv_map), var_id);
1406 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1408 isl_set_free(dom);
1409 isl_id_free(var_id);
1411 for (i = 0; i < stmt->n_arg; ++i)
1412 if (!stmt->args[i])
1413 return pet_stmt_free(stmt);
1414 if (!stmt->domain || !stmt->schedule || !stmt->body)
1415 return pet_stmt_free(stmt);
1416 return stmt;
1417 error:
1418 isl_set_free(dom);
1419 isl_map_free(sched);
1420 isl_map_free(iv_map);
1421 isl_id_free(var_id);
1422 return NULL;
1425 /* Embed the given pet_array in an extra outer loop with iteration domain
1426 * "dom".
1427 * This embedding only has an effect on virtual arrays (those with
1428 * user pointer equal to NULL), which need to be extended along with
1429 * the iteration domain.
1431 static struct pet_array *pet_array_embed(struct pet_array *array,
1432 __isl_take isl_set *dom)
1434 isl_id *array_id = NULL;
1436 if (!array)
1437 goto error;
1439 if (isl_set_has_tuple_id(array->extent))
1440 array_id = isl_set_get_tuple_id(array->extent);
1442 if (array_id && !isl_id_get_user(array_id)) {
1443 array->extent = isl_set_flat_product(dom, array->extent);
1444 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1445 } else {
1446 isl_set_free(dom);
1447 isl_id_free(array_id);
1450 return array;
1451 error:
1452 isl_set_free(dom);
1453 return NULL;
1456 /* Project out all unnamed parameters from "set" and return the result.
1458 static __isl_give isl_set *set_project_out_unnamed_params(
1459 __isl_take isl_set *set)
1461 int i, n;
1463 n = isl_set_dim(set, isl_dim_param);
1464 for (i = n - 1; i >= 0; --i) {
1465 if (isl_set_has_dim_name(set, isl_dim_param, i))
1466 continue;
1467 set = isl_set_project_out(set, isl_dim_param, i, 1);
1470 return set;
1473 /* Update the context with respect to an embedding into a loop
1474 * with iteration domain "dom" and induction variable "id".
1475 * "iv_map" maps a possibly virtual iterator (used in "dom")
1476 * to the real iterator (parameter "id").
1478 * If the current context is independent of "id", we don't need
1479 * to do anything.
1480 * Otherwise, a parameter value is invalid for the embedding if
1481 * any of the corresponding iterator values is invalid.
1482 * That is, a parameter value is valid only if all the corresponding
1483 * iterator values are valid.
1484 * We therefore compute the set of parameters
1486 * forall i in dom : valid (i)
1488 * or
1490 * not exists i in dom : not valid(i)
1492 * i.e.,
1494 * not exists i in dom \ valid(i)
1496 * Before we subtract valid(i) from dom, we first need to map
1497 * the real iterator to the virtual iterator.
1499 * If there are any unnamed parameters in "dom", then we consider
1500 * a parameter value to be valid if it is valid for any value of those
1501 * unnamed parameters. They are therefore projected out at the end.
1503 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1504 __isl_keep isl_set *dom, __isl_keep isl_map *iv_map,
1505 __isl_keep isl_id *id)
1507 int pos;
1509 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1510 if (pos < 0)
1511 return context;
1513 context = isl_set_from_params(context);
1514 context = isl_set_add_dims(context, isl_dim_set, 1);
1515 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1516 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1517 context = isl_set_apply(context, isl_map_reverse(isl_map_copy(iv_map)));
1518 context = isl_set_subtract(isl_set_copy(dom), context);
1519 context = isl_set_params(context);
1520 context = isl_set_complement(context);
1521 context = set_project_out_unnamed_params(context);
1522 return context;
1525 /* Embed all statements and arrays in "scop" in an extra outer loop
1526 * with iteration domain "dom" and schedule "sched".
1527 * "id" represents the induction variable of the loop.
1528 * "iv_map" maps a possibly virtual iterator to the real iterator.
1529 * That is, it maps the iterator used in "dom" and the domain of "sched"
1530 * to the iterator that some of the parameters in "scop" may refer to.
1532 * Any skip conditions within the loop have no effect outside of the loop.
1533 * The caller is responsible for making sure skip[pet_skip_later] has been
1534 * taken into account.
1536 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1537 __isl_take isl_map *sched, __isl_take isl_map *iv_map,
1538 __isl_take isl_id *id)
1540 int i;
1542 if (!scop)
1543 goto error;
1545 pet_scop_reset_skip(scop, pet_skip_now);
1546 pet_scop_reset_skip(scop, pet_skip_later);
1548 scop->context = context_embed(scop->context, dom, iv_map, id);
1549 if (!scop->context)
1550 goto error;
1552 for (i = 0; i < scop->n_stmt; ++i) {
1553 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1554 isl_set_copy(dom), isl_map_copy(sched),
1555 isl_map_copy(iv_map), isl_id_copy(id));
1556 if (!scop->stmts[i])
1557 goto error;
1560 for (i = 0; i < scop->n_array; ++i) {
1561 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1562 isl_set_copy(dom));
1563 if (!scop->arrays[i])
1564 goto error;
1567 isl_set_free(dom);
1568 isl_map_free(sched);
1569 isl_map_free(iv_map);
1570 isl_id_free(id);
1571 return scop;
1572 error:
1573 isl_set_free(dom);
1574 isl_map_free(sched);
1575 isl_map_free(iv_map);
1576 isl_id_free(id);
1577 return pet_scop_free(scop);
1580 /* Add extra conditions on the parameters to iteration domain of "stmt".
1582 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1583 __isl_take isl_set *cond)
1585 if (!stmt)
1586 goto error;
1588 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1590 return stmt;
1591 error:
1592 isl_set_free(cond);
1593 return pet_stmt_free(stmt);
1596 /* Add extra conditions to scop->skip[type].
1598 * The new skip condition only holds if it held before
1599 * and the condition is true. It does not hold if it did not hold
1600 * before or the condition is false.
1602 * The skip condition is assumed to be an affine expression.
1604 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1605 enum pet_skip type, __isl_keep isl_set *cond)
1607 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1608 isl_set *skip;
1609 isl_set *set;
1611 if (!scop)
1612 return NULL;
1613 if (!ext->skip[type])
1614 return scop;
1616 if (!set_is_affine(ext->skip[type]))
1617 isl_die(isl_set_get_ctx(ext->skip[type]), isl_error_internal,
1618 "can only resrict affine skips",
1619 return pet_scop_free(scop));
1621 skip = ext->skip[type];
1622 skip = isl_set_intersect_params(skip, isl_set_copy(cond));
1623 set = isl_set_from_params(isl_set_copy(cond));
1624 set = isl_set_complement(set);
1625 set = isl_set_add_dims(set, isl_dim_set, 1);
1626 set = isl_set_fix_si(set, isl_dim_set, 0, 0);
1627 skip = isl_set_union(skip, set);
1628 ext->skip[type] = skip;
1629 if (!ext->skip[type])
1630 return pet_scop_free(scop);
1632 return scop;
1635 /* Add extra conditions on the parameters to all iteration domains
1636 * and skip conditions.
1638 * A parameter value is valid for the result if it was valid
1639 * for the original scop and satisfies "cond" or if it does
1640 * not satisfy "cond" as in this case the scop is not executed
1641 * and the original constraints on the parameters are irrelevant.
1643 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1644 __isl_take isl_set *cond)
1646 int i;
1648 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1649 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1651 if (!scop)
1652 goto error;
1654 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1655 scop->context = isl_set_union(scop->context,
1656 isl_set_complement(isl_set_copy(cond)));
1657 scop->context = isl_set_coalesce(scop->context);
1658 scop->context = set_project_out_unnamed_params(scop->context);
1659 if (!scop->context)
1660 goto error;
1662 for (i = 0; i < scop->n_stmt; ++i) {
1663 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1664 isl_set_copy(cond));
1665 if (!scop->stmts[i])
1666 goto error;
1669 isl_set_free(cond);
1670 return scop;
1671 error:
1672 isl_set_free(cond);
1673 return pet_scop_free(scop);
1676 /* Construct a map that inserts a filter value with name "id" and value
1677 * "satisfied" in the list of filter values embedded in the set space "space".
1679 * If "space" does not contain any filter values yet, we first create
1680 * a map that inserts 0 filter values, i.e.,
1682 * space -> [space -> []]
1684 * We can now assume that space is of the form [dom -> [filters]]
1685 * We construct an identity mapping on dom and a mapping on filters
1686 * that inserts the new filter
1688 * dom -> dom
1689 * [filters] -> [satisfied, filters]
1691 * and then compute the cross product
1693 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1695 static __isl_give isl_map *insert_filter_map(__isl_take isl_space *space,
1696 __isl_take isl_id *id, int satisfied)
1698 isl_space *space2;
1699 isl_map *map, *map_dom, *map_ran;
1700 isl_set *dom;
1702 if (isl_space_is_wrapping(space)) {
1703 space2 = isl_space_map_from_set(isl_space_copy(space));
1704 map = isl_map_identity(space2);
1705 space = isl_space_unwrap(space);
1706 } else {
1707 space = isl_space_from_domain(space);
1708 map = isl_map_universe(isl_space_copy(space));
1709 map = isl_map_reverse(isl_map_domain_map(map));
1712 space2 = isl_space_domain(isl_space_copy(space));
1713 map_dom = isl_map_identity(isl_space_map_from_set(space2));
1714 space = isl_space_range(space);
1715 map_ran = isl_map_identity(isl_space_map_from_set(space));
1716 map_ran = isl_map_insert_dims(map_ran, isl_dim_out, 0, 1);
1717 map_ran = isl_map_set_dim_id(map_ran, isl_dim_out, 0, id);
1718 map_ran = isl_map_fix_si(map_ran, isl_dim_out, 0, satisfied);
1720 map = isl_map_apply_range(map, isl_map_product(map_dom, map_ran));
1722 return map;
1725 /* Insert an argument expression corresponding to "test" in front
1726 * of the list of arguments described by *n_arg and *args.
1728 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
1729 __isl_keep isl_map *test)
1731 int i;
1732 isl_ctx *ctx = isl_map_get_ctx(test);
1734 if (!test)
1735 return -1;
1737 if (!*args) {
1738 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
1739 if (!*args)
1740 return -1;
1741 } else {
1742 struct pet_expr **ext;
1743 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
1744 if (!ext)
1745 return -1;
1746 for (i = 0; i < *n_arg; ++i)
1747 ext[1 + i] = (*args)[i];
1748 free(*args);
1749 *args = ext;
1751 (*n_arg)++;
1752 (*args)[0] = pet_expr_from_access(isl_map_copy(test));
1753 if (!(*args)[0])
1754 return -1;
1756 return 0;
1759 /* Make the expression "expr" depend on the value of "test"
1760 * being equal to "satisfied".
1762 * If "test" is an affine expression, we simply add the conditions
1763 * on the expression have the value "satisfied" to all access relations.
1765 * Otherwise, we add a filter to "expr" (which is then assumed to be
1766 * an access expression) corresponding to "test" being equal to "satisfied".
1768 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
1769 __isl_take isl_map *test, int satisfied)
1771 isl_id *id;
1772 isl_ctx *ctx;
1773 isl_space *space;
1774 isl_map *map;
1776 if (!expr || !test)
1777 goto error;
1779 if (!isl_map_has_tuple_id(test, isl_dim_out)) {
1780 test = isl_map_fix_si(test, isl_dim_out, 0, satisfied);
1781 return pet_expr_restrict(expr, isl_map_params(test));
1784 ctx = isl_map_get_ctx(test);
1785 if (expr->type != pet_expr_access)
1786 isl_die(ctx, isl_error_invalid,
1787 "can only filter access expressions", goto error);
1789 space = isl_space_domain(isl_map_get_space(expr->acc.access));
1790 id = isl_map_get_tuple_id(test, isl_dim_out);
1791 map = insert_filter_map(space, id, satisfied);
1793 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
1794 if (!expr->acc.access)
1795 goto error;
1797 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
1798 goto error;
1800 isl_map_free(test);
1801 return expr;
1802 error:
1803 isl_map_free(test);
1804 return pet_expr_free(expr);
1807 /* Make the statement "stmt" depend on the value of "test"
1808 * being equal to "satisfied" by adjusting stmt->domain.
1810 * The domain of "test" corresponds to the (zero or more) outer dimensions
1811 * of the iteration domain.
1813 * We insert an argument corresponding to a read to "test"
1814 * from the iteration domain of "stmt" in front of the list of arguments.
1815 * We also insert a corresponding output dimension in the wrapped
1816 * map contained in stmt->domain, with value set to "satisfied".
1818 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1819 __isl_take isl_map *test, int satisfied)
1821 int i;
1822 isl_id *id;
1823 isl_ctx *ctx;
1824 isl_map *map, *add_dom;
1825 isl_space *space;
1826 isl_set *dom;
1827 int n_test_dom;
1829 if (!stmt || !test)
1830 goto error;
1832 id = isl_map_get_tuple_id(test, isl_dim_out);
1833 map = insert_filter_map(isl_set_get_space(stmt->domain), id, satisfied);
1834 stmt->domain = isl_set_apply(stmt->domain, map);
1836 space = isl_space_unwrap(isl_set_get_space(stmt->domain));
1837 dom = isl_set_universe(isl_space_domain(space));
1838 n_test_dom = isl_map_dim(test, isl_dim_in);
1839 add_dom = isl_map_from_range(dom);
1840 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
1841 for (i = 0; i < n_test_dom; ++i)
1842 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
1843 isl_dim_out, i);
1844 test = isl_map_apply_domain(test, add_dom);
1846 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1847 goto error;
1849 isl_map_free(test);
1850 return stmt;
1851 error:
1852 isl_map_free(test);
1853 return pet_stmt_free(stmt);
1856 /* Does "scop" have a skip condition of the given "type"?
1858 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1860 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1862 if (!scop)
1863 return -1;
1864 return ext->skip[type] != NULL;
1867 /* Does "scop" have a skip condition of the given "type" that
1868 * is an affine expression?
1870 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1872 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1874 if (!scop)
1875 return -1;
1876 if (!ext->skip[type])
1877 return 0;
1878 return set_is_affine(ext->skip[type]);
1881 /* Does "scop" have a skip condition of the given "type" that
1882 * is not an affine expression?
1884 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1886 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1887 int aff;
1889 if (!scop)
1890 return -1;
1891 if (!ext->skip[type])
1892 return 0;
1893 aff = set_is_affine(ext->skip[type]);
1894 if (aff < 0)
1895 return -1;
1896 return !aff;
1899 /* Does "scop" have a skip condition of the given "type" that
1900 * is affine and holds on the entire domain?
1902 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1904 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1905 isl_set *set;
1906 int is_aff;
1907 int is_univ;
1909 is_aff = pet_scop_has_affine_skip(scop, type);
1910 if (is_aff < 0 || !is_aff)
1911 return is_aff;
1913 set = isl_set_copy(ext->skip[type]);
1914 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
1915 set = isl_set_params(set);
1916 is_univ = isl_set_plain_is_universe(set);
1917 isl_set_free(set);
1919 return is_univ;
1922 /* Replace scop->skip[type] by "skip".
1924 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
1925 enum pet_skip type, __isl_take isl_set *skip)
1927 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1929 if (!scop || !skip)
1930 goto error;
1932 isl_set_free(ext->skip[type]);
1933 ext->skip[type] = skip;
1935 return scop;
1936 error:
1937 isl_set_free(skip);
1938 return pet_scop_free(scop);
1941 /* Return a copy of scop->skip[type].
1943 __isl_give isl_set *pet_scop_get_skip(struct pet_scop *scop,
1944 enum pet_skip type)
1946 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1948 if (!scop)
1949 return NULL;
1951 return isl_set_copy(ext->skip[type]);
1954 /* Return a map to the skip condition of the given type.
1956 __isl_give isl_map *pet_scop_get_skip_map(struct pet_scop *scop,
1957 enum pet_skip type)
1959 return isl_map_from_range(pet_scop_get_skip(scop, type));
1962 /* Return an access pet_expr corresponding to the skip condition
1963 * of the given type.
1965 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
1966 enum pet_skip type)
1968 return pet_expr_from_access(pet_scop_get_skip_map(scop, type));
1971 /* Drop the the skip condition scop->skip[type].
1973 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
1975 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1977 if (!scop)
1978 return;
1980 isl_set_free(ext->skip[type]);
1981 ext->skip[type] = NULL;
1984 /* Make the skip condition (if any) depend on the value of "test" being
1985 * equal to "satisfied".
1987 * We only support the case where the original skip condition is universal,
1988 * i.e., where skipping is unconditional, and where satisfied == 1.
1989 * In this case, the skip condition is changed to skip only when
1990 * "test" is equal to one.
1992 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
1993 enum pet_skip type, __isl_keep isl_map *test, int satisfied)
1995 int is_univ = 0;
1997 if (!scop)
1998 return NULL;
1999 if (!pet_scop_has_skip(scop, type))
2000 return scop;
2002 if (satisfied)
2003 is_univ = pet_scop_has_universal_skip(scop, type);
2004 if (is_univ < 0)
2005 return pet_scop_free(scop);
2006 if (satisfied && is_univ) {
2007 scop = pet_scop_set_skip(scop, type,
2008 isl_map_range(isl_map_copy(test)));
2009 if (!scop)
2010 return NULL;
2011 } else {
2012 isl_die(isl_map_get_ctx(test), isl_error_internal,
2013 "skip expression cannot be filtered",
2014 return pet_scop_free(scop));
2017 return scop;
2020 /* Make all statements in "scop" depend on the value of "test"
2021 * being equal to "satisfied" by adjusting their domains.
2023 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2024 __isl_take isl_map *test, int satisfied)
2026 int i;
2028 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2029 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2031 if (!scop || !test)
2032 goto error;
2034 for (i = 0; i < scop->n_stmt; ++i) {
2035 scop->stmts[i] = stmt_filter(scop->stmts[i],
2036 isl_map_copy(test), satisfied);
2037 if (!scop->stmts[i])
2038 goto error;
2041 isl_map_free(test);
2042 return scop;
2043 error:
2044 isl_map_free(test);
2045 return pet_scop_free(scop);
2048 /* Do the filters "i" and "j" always have the same value?
2050 static int equal_filter_values(__isl_keep isl_set *domain, int i, int j)
2052 isl_map *map, *test;
2053 int equal;
2055 map = isl_set_unwrap(isl_set_copy(domain));
2056 test = isl_map_universe(isl_map_get_space(map));
2057 test = isl_map_equate(test, isl_dim_out, i, isl_dim_out, j);
2058 equal = isl_map_is_subset(map, test);
2059 isl_map_free(map);
2060 isl_map_free(test);
2062 return equal;
2065 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2066 * access relation, the union of the two access relations.
2068 static struct pet_stmt *merge_filter_pair(struct pet_stmt *stmt, int i, int j)
2070 int k;
2071 isl_map *map;
2073 if (!stmt)
2074 return NULL;
2076 stmt->args[i]->acc.access = isl_map_union(stmt->args[i]->acc.access,
2077 isl_map_copy(stmt->args[j]->acc.access));
2078 stmt->args[i]->acc.access = isl_map_coalesce(stmt->args[i]->acc.access);
2080 pet_expr_free(stmt->args[j]);
2081 for (k = j; k < stmt->n_arg - 1; ++k)
2082 stmt->args[k] = stmt->args[k + 1];
2083 stmt->n_arg--;
2085 map = isl_set_unwrap(stmt->domain);
2086 map = isl_map_project_out(map, isl_dim_out, j, 1);
2087 stmt->domain = isl_map_wrap(map);
2089 if (!stmt->domain || !stmt->args[i]->acc.access)
2090 return pet_stmt_free(stmt);
2092 return stmt;
2095 /* Look for any pair of filters that access the same filter variable
2096 * and that have the same filter value and merge them into a single
2097 * filter with as filter access relation the union of the filter access
2098 * relations.
2100 static struct pet_stmt *stmt_merge_filters(struct pet_stmt *stmt)
2102 int i, j;
2103 isl_space *space_i, *space_j;
2105 if (!stmt)
2106 return NULL;
2107 if (stmt->n_arg <= 1)
2108 return stmt;
2110 for (i = 0; i < stmt->n_arg - 1; ++i) {
2111 if (stmt->args[i]->type != pet_expr_access)
2112 continue;
2113 if (pet_expr_is_affine(stmt->args[i]))
2114 continue;
2116 space_i = isl_map_get_space(stmt->args[i]->acc.access);
2118 for (j = stmt->n_arg - 1; j > i; --j) {
2119 int eq;
2121 if (stmt->args[j]->type != pet_expr_access)
2122 continue;
2123 if (pet_expr_is_affine(stmt->args[j]))
2124 continue;
2126 space_j = isl_map_get_space(stmt->args[j]->acc.access);
2128 eq = isl_space_is_equal(space_i, space_j);
2129 if (eq >= 0 && eq)
2130 eq = equal_filter_values(stmt->domain, i, j);
2131 if (eq >= 0 && eq)
2132 stmt = merge_filter_pair(stmt, i, j);
2134 isl_space_free(space_j);
2136 if (eq < 0 || !stmt)
2137 break;
2140 isl_space_free(space_i);
2142 if (j > i || !stmt)
2143 return pet_stmt_free(stmt);
2146 return stmt;
2149 /* Look for any pair of filters that access the same filter variable
2150 * and that have the same filter value and merge them into a single
2151 * filter with as filter access relation the union of the filter access
2152 * relations.
2154 struct pet_scop *pet_scop_merge_filters(struct pet_scop *scop)
2156 int i;
2158 if (!scop)
2159 return NULL;
2161 for (i = 0; i < scop->n_stmt; ++i) {
2162 scop->stmts[i] = stmt_merge_filters(scop->stmts[i]);
2163 if (!scop->stmts[i])
2164 return pet_scop_free(scop);
2167 return scop;
2170 /* Add all parameters in "expr" to "dim" and return the result.
2172 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2173 __isl_take isl_space *dim)
2175 int i;
2177 if (!expr)
2178 goto error;
2179 for (i = 0; i < expr->n_arg; ++i)
2181 dim = expr_collect_params(expr->args[i], dim);
2183 if (expr->type == pet_expr_access)
2184 dim = isl_space_align_params(dim,
2185 isl_map_get_space(expr->acc.access));
2187 return dim;
2188 error:
2189 isl_space_free(dim);
2190 return pet_expr_free(expr);
2193 /* Add all parameters in "stmt" to "dim" and return the result.
2195 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2196 __isl_take isl_space *dim)
2198 if (!stmt)
2199 goto error;
2201 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2202 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2203 dim = expr_collect_params(stmt->body, dim);
2205 return dim;
2206 error:
2207 isl_space_free(dim);
2208 return pet_stmt_free(stmt);
2211 /* Add all parameters in "array" to "dim" and return the result.
2213 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2214 __isl_take isl_space *dim)
2216 if (!array)
2217 goto error;
2219 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2220 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2222 return dim;
2223 error:
2224 pet_array_free(array);
2225 return isl_space_free(dim);
2228 /* Add all parameters in "scop" to "dim" and return the result.
2230 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2231 __isl_take isl_space *dim)
2233 int i;
2235 if (!scop)
2236 goto error;
2238 for (i = 0; i < scop->n_array; ++i)
2239 dim = array_collect_params(scop->arrays[i], dim);
2241 for (i = 0; i < scop->n_stmt; ++i)
2242 dim = stmt_collect_params(scop->stmts[i], dim);
2244 return dim;
2245 error:
2246 isl_space_free(dim);
2247 return pet_scop_free(scop);
2250 /* Add all parameters in "dim" to all access relations in "expr".
2252 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2253 __isl_take isl_space *dim)
2255 int i;
2257 if (!expr)
2258 goto error;
2260 for (i = 0; i < expr->n_arg; ++i) {
2261 expr->args[i] =
2262 expr_propagate_params(expr->args[i],
2263 isl_space_copy(dim));
2264 if (!expr->args[i])
2265 goto error;
2268 if (expr->type == pet_expr_access) {
2269 expr->acc.access = isl_map_align_params(expr->acc.access,
2270 isl_space_copy(dim));
2271 if (!expr->acc.access)
2272 goto error;
2275 isl_space_free(dim);
2276 return expr;
2277 error:
2278 isl_space_free(dim);
2279 return pet_expr_free(expr);
2282 /* Add all parameters in "dim" to the domain, schedule and
2283 * all access relations in "stmt".
2285 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2286 __isl_take isl_space *dim)
2288 if (!stmt)
2289 goto error;
2291 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2292 stmt->schedule = isl_map_align_params(stmt->schedule,
2293 isl_space_copy(dim));
2294 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2296 if (!stmt->domain || !stmt->schedule || !stmt->body)
2297 goto error;
2299 isl_space_free(dim);
2300 return stmt;
2301 error:
2302 isl_space_free(dim);
2303 return pet_stmt_free(stmt);
2306 /* Add all parameters in "dim" to "array".
2308 static struct pet_array *array_propagate_params(struct pet_array *array,
2309 __isl_take isl_space *dim)
2311 if (!array)
2312 goto error;
2314 array->context = isl_set_align_params(array->context,
2315 isl_space_copy(dim));
2316 array->extent = isl_set_align_params(array->extent,
2317 isl_space_copy(dim));
2318 if (array->value_bounds) {
2319 array->value_bounds = isl_set_align_params(array->value_bounds,
2320 isl_space_copy(dim));
2321 if (!array->value_bounds)
2322 goto error;
2325 if (!array->context || !array->extent)
2326 goto error;
2328 isl_space_free(dim);
2329 return array;
2330 error:
2331 isl_space_free(dim);
2332 return pet_array_free(array);
2335 /* Add all parameters in "dim" to "scop".
2337 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2338 __isl_take isl_space *dim)
2340 int i;
2342 if (!scop)
2343 goto error;
2345 for (i = 0; i < scop->n_array; ++i) {
2346 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2347 isl_space_copy(dim));
2348 if (!scop->arrays[i])
2349 goto error;
2352 for (i = 0; i < scop->n_stmt; ++i) {
2353 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2354 isl_space_copy(dim));
2355 if (!scop->stmts[i])
2356 goto error;
2359 isl_space_free(dim);
2360 return scop;
2361 error:
2362 isl_space_free(dim);
2363 return pet_scop_free(scop);
2366 /* Update all isl_sets and isl_maps in "scop" such that they all
2367 * have the same parameters.
2369 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2371 isl_space *dim;
2373 if (!scop)
2374 return NULL;
2376 dim = isl_set_get_space(scop->context);
2377 dim = scop_collect_params(scop, dim);
2379 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
2380 scop = scop_propagate_params(scop, dim);
2382 return scop;
2385 /* Check if the given access relation accesses a (0D) array that corresponds
2386 * to one of the parameters in "dim". If so, replace the array access
2387 * by an access to the set of integers with as index (and value)
2388 * that parameter.
2390 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
2391 __isl_take isl_space *dim)
2393 isl_id *array_id = NULL;
2394 int pos = -1;
2396 if (isl_map_has_tuple_id(access, isl_dim_out)) {
2397 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2398 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
2400 isl_space_free(dim);
2402 if (pos < 0) {
2403 isl_id_free(array_id);
2404 return access;
2407 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
2408 if (pos < 0) {
2409 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
2410 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
2411 pos = 0;
2412 } else
2413 isl_id_free(array_id);
2415 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2416 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
2418 return access;
2421 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2422 * in "dim" by a value equal to the corresponding parameter.
2424 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
2425 __isl_take isl_space *dim)
2427 int i;
2429 if (!expr)
2430 goto error;
2432 for (i = 0; i < expr->n_arg; ++i) {
2433 expr->args[i] =
2434 expr_detect_parameter_accesses(expr->args[i],
2435 isl_space_copy(dim));
2436 if (!expr->args[i])
2437 goto error;
2440 if (expr->type == pet_expr_access) {
2441 expr->acc.access = access_detect_parameter(expr->acc.access,
2442 isl_space_copy(dim));
2443 if (!expr->acc.access)
2444 goto error;
2447 isl_space_free(dim);
2448 return expr;
2449 error:
2450 isl_space_free(dim);
2451 return pet_expr_free(expr);
2454 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2455 * in "dim" by a value equal to the corresponding parameter.
2457 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2458 __isl_take isl_space *dim)
2460 if (!stmt)
2461 goto error;
2463 stmt->body = expr_detect_parameter_accesses(stmt->body,
2464 isl_space_copy(dim));
2466 if (!stmt->domain || !stmt->schedule || !stmt->body)
2467 goto error;
2469 isl_space_free(dim);
2470 return stmt;
2471 error:
2472 isl_space_free(dim);
2473 return pet_stmt_free(stmt);
2476 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2477 * in "dim" by a value equal to the corresponding parameter.
2479 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2480 __isl_take isl_space *dim)
2482 int i;
2484 if (!scop)
2485 goto error;
2487 for (i = 0; i < scop->n_stmt; ++i) {
2488 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2489 isl_space_copy(dim));
2490 if (!scop->stmts[i])
2491 goto error;
2494 isl_space_free(dim);
2495 return scop;
2496 error:
2497 isl_space_free(dim);
2498 return pet_scop_free(scop);
2501 /* Replace all accesses to (0D) arrays that correspond to any of
2502 * the parameters used in "scop" by a value equal
2503 * to the corresponding parameter.
2505 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2507 isl_space *dim;
2509 if (!scop)
2510 return NULL;
2512 dim = isl_set_get_space(scop->context);
2513 dim = scop_collect_params(scop, dim);
2515 scop = scop_detect_parameter_accesses(scop, dim);
2517 return scop;
2520 /* Add all read access relations (if "read" is set) and/or all write
2521 * access relations (if "write" is set) to "accesses" and return the result.
2523 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
2524 int read, int write, __isl_take isl_union_map *accesses)
2526 int i;
2527 isl_id *id;
2528 isl_space *dim;
2530 if (!expr)
2531 return NULL;
2533 for (i = 0; i < expr->n_arg; ++i)
2534 accesses = expr_collect_accesses(expr->args[i],
2535 read, write, accesses);
2537 if (expr->type == pet_expr_access &&
2538 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
2539 ((read && expr->acc.read) || (write && expr->acc.write)))
2540 accesses = isl_union_map_add_map(accesses,
2541 isl_map_copy(expr->acc.access));
2543 return accesses;
2546 /* Collect and return all read access relations (if "read" is set)
2547 * and/or all write * access relations (if "write" is set) in "stmt".
2549 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2550 int read, int write, __isl_take isl_space *dim)
2552 isl_union_map *accesses;
2554 if (!stmt)
2555 return NULL;
2557 accesses = isl_union_map_empty(dim);
2558 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
2559 accesses = isl_union_map_intersect_domain(accesses,
2560 isl_union_set_from_set(isl_set_copy(stmt->domain)));
2562 return accesses;
2565 /* Collect and return all read access relations (if "read" is set)
2566 * and/or all write * access relations (if "write" is set) in "scop".
2568 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2569 int read, int write)
2571 int i;
2572 isl_union_map *accesses;
2574 if (!scop)
2575 return NULL;
2577 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2579 for (i = 0; i < scop->n_stmt; ++i) {
2580 isl_union_map *accesses_i;
2581 isl_space *dim = isl_set_get_space(scop->context);
2582 accesses_i = stmt_collect_accesses(scop->stmts[i],
2583 read, write, dim);
2584 accesses = isl_union_map_union(accesses, accesses_i);
2587 return accesses;
2590 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
2592 return scop_collect_accesses(scop, 1, 0);
2595 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
2597 return scop_collect_accesses(scop, 0, 1);
2600 /* Collect and return the union of iteration domains in "scop".
2602 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2604 int i;
2605 isl_set *domain_i;
2606 isl_union_set *domain;
2608 if (!scop)
2609 return NULL;
2611 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2613 for (i = 0; i < scop->n_stmt; ++i) {
2614 domain_i = isl_set_copy(scop->stmts[i]->domain);
2615 domain = isl_union_set_add_set(domain, domain_i);
2618 return domain;
2621 /* Collect and return the schedules of the statements in "scop".
2622 * The range is normalized to the maximal number of scheduling
2623 * dimensions.
2625 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2627 int i, j;
2628 isl_map *schedule_i;
2629 isl_union_map *schedule;
2630 int depth, max_depth = 0;
2632 if (!scop)
2633 return NULL;
2635 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2637 for (i = 0; i < scop->n_stmt; ++i) {
2638 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2639 if (depth > max_depth)
2640 max_depth = depth;
2643 for (i = 0; i < scop->n_stmt; ++i) {
2644 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2645 depth = isl_map_dim(schedule_i, isl_dim_out);
2646 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2647 max_depth - depth);
2648 for (j = depth; j < max_depth; ++j)
2649 schedule_i = isl_map_fix_si(schedule_i,
2650 isl_dim_out, j, 0);
2651 schedule = isl_union_map_add_map(schedule, schedule_i);
2654 return schedule;
2657 /* Does expression "expr" write to "id"?
2659 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
2661 int i;
2662 isl_id *write_id;
2664 for (i = 0; i < expr->n_arg; ++i) {
2665 int writes = expr_writes(expr->args[i], id);
2666 if (writes < 0 || writes)
2667 return writes;
2670 if (expr->type != pet_expr_access)
2671 return 0;
2672 if (!expr->acc.write)
2673 return 0;
2674 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
2675 return 0;
2677 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
2678 isl_id_free(write_id);
2680 if (!write_id)
2681 return -1;
2683 return write_id == id;
2686 /* Does statement "stmt" write to "id"?
2688 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
2690 return expr_writes(stmt->body, id);
2693 /* Is there any write access in "scop" that accesses "id"?
2695 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
2697 int i;
2699 if (!scop)
2700 return -1;
2702 for (i = 0; i < scop->n_stmt; ++i) {
2703 int writes = stmt_writes(scop->stmts[i], id);
2704 if (writes < 0 || writes)
2705 return writes;
2708 return 0;
2711 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2713 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
2715 int i, n;
2717 n = isl_set_dim(set, isl_dim_param);
2718 for (i = 0; i < n; ++i) {
2719 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2720 const char *name = isl_id_get_name(id);
2721 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
2722 isl_id_free(id);
2725 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
2726 isl_id *id = isl_set_get_tuple_id(set);
2727 const char *name = isl_id_get_name(id);
2728 set = isl_set_set_tuple_name(set, name);
2729 isl_id_free(id);
2732 return set;
2735 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2737 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
2739 int i, n;
2741 n = isl_map_dim(map, isl_dim_param);
2742 for (i = 0; i < n; ++i) {
2743 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2744 const char *name = isl_id_get_name(id);
2745 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2746 isl_id_free(id);
2749 if (isl_map_has_tuple_id(map, isl_dim_in)) {
2750 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
2751 const char *name = isl_id_get_name(id);
2752 map = isl_map_set_tuple_name(map, isl_dim_in, name);
2753 isl_id_free(id);
2756 if (isl_map_has_tuple_id(map, isl_dim_out)) {
2757 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
2758 const char *name = isl_id_get_name(id);
2759 map = isl_map_set_tuple_name(map, isl_dim_out, name);
2760 isl_id_free(id);
2763 return map;
2766 /* Reset the user pointer on all parameter ids in "array".
2768 static struct pet_array *array_anonymize(struct pet_array *array)
2770 if (!array)
2771 return NULL;
2773 array->context = set_anonymize(array->context);
2774 array->extent = set_anonymize(array->extent);
2775 if (!array->context || !array->extent)
2776 return pet_array_free(array);
2778 return array;
2781 /* Reset the user pointer on all parameter and tuple ids in "access".
2783 static __isl_give isl_map *access_anonymize(__isl_take isl_map *access,
2784 void *user)
2786 access = map_anonymize(access);
2788 return access;
2791 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2793 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2795 int i;
2796 isl_space *space;
2797 isl_set *domain;
2799 if (!stmt)
2800 return NULL;
2802 stmt->domain = set_anonymize(stmt->domain);
2803 stmt->schedule = map_anonymize(stmt->schedule);
2804 if (!stmt->domain || !stmt->schedule)
2805 return pet_stmt_free(stmt);
2807 for (i = 0; i < stmt->n_arg; ++i) {
2808 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2809 &access_anonymize, NULL);
2810 if (!stmt->args[i])
2811 return pet_stmt_free(stmt);
2814 stmt->body = pet_expr_foreach_access(stmt->body,
2815 &access_anonymize, NULL);
2816 if (!stmt->body)
2817 return pet_stmt_free(stmt);
2819 return stmt;
2822 /* Reset the user pointer on all parameter and tuple ids in "scop".
2824 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2826 int i;
2828 if (!scop)
2829 return NULL;
2831 scop->context = set_anonymize(scop->context);
2832 scop->context_value = set_anonymize(scop->context_value);
2833 if (!scop->context || !scop->context_value)
2834 return pet_scop_free(scop);
2836 for (i = 0; i < scop->n_array; ++i) {
2837 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2838 if (!scop->arrays[i])
2839 return pet_scop_free(scop);
2842 for (i = 0; i < scop->n_stmt; ++i) {
2843 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2844 if (!scop->stmts[i])
2845 return pet_scop_free(scop);
2848 return scop;
2851 /* Given a set "domain", return a wrapped relation with the given set
2852 * as domain and a range of dimension "n_arg", where each coordinate
2853 * is either unbounded or, if the corresponding element of args is of
2854 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2856 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2857 unsigned n_arg, struct pet_expr **args,
2858 __isl_keep isl_union_map *value_bounds)
2860 int i;
2861 isl_map *map;
2862 isl_space *space;
2863 isl_ctx *ctx = isl_set_get_ctx(domain);
2865 map = isl_map_from_domain(domain);
2866 space = isl_map_get_space(map);
2867 space = isl_space_add_dims(space, isl_dim_out, 1);
2869 for (i = 0; i < n_arg; ++i) {
2870 isl_map *map_i;
2871 struct pet_expr *arg = args[i];
2872 isl_id *id;
2873 isl_space *space2;
2875 map_i = isl_map_universe(isl_space_copy(space));
2876 if (arg->type == pet_expr_access) {
2877 isl_map *vb;
2878 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2879 space2 = isl_space_alloc(ctx, 0, 0, 1);
2880 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2881 vb = isl_union_map_extract_map(value_bounds, space2);
2882 if (!isl_map_plain_is_empty(vb))
2883 map_i = isl_map_intersect_range(map_i,
2884 isl_map_range(vb));
2885 else
2886 isl_map_free(vb);
2888 map = isl_map_flat_range_product(map, map_i);
2890 isl_space_free(space);
2892 return isl_map_wrap(map);
2895 /* Data used in access_gist() callback.
2897 struct pet_access_gist_data {
2898 isl_set *domain;
2899 isl_union_map *value_bounds;
2902 /* Given an expression "expr" of type pet_expr_access, compute
2903 * the gist of the associated access relation with respect to
2904 * data->domain and the bounds on the values of the arguments
2905 * of the expression.
2907 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
2909 struct pet_access_gist_data *data = user;
2910 isl_set *domain;
2912 domain = isl_set_copy(data->domain);
2913 if (expr->n_arg > 0)
2914 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
2915 data->value_bounds);
2917 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
2918 if (!expr->acc.access)
2919 return pet_expr_free(expr);
2921 return expr;
2924 /* Compute the gist of the iteration domain and all access relations
2925 * of "stmt" based on the constraints on the parameters specified by "context"
2926 * and the constraints on the values of nested accesses specified
2927 * by "value_bounds".
2929 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2930 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2932 int i;
2933 isl_space *space;
2934 isl_set *domain;
2935 struct pet_access_gist_data data;
2937 if (!stmt)
2938 return NULL;
2940 data.domain = isl_set_copy(stmt->domain);
2941 data.value_bounds = value_bounds;
2942 if (stmt->n_arg > 0)
2943 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
2945 data.domain = isl_set_intersect_params(data.domain,
2946 isl_set_copy(context));
2948 for (i = 0; i < stmt->n_arg; ++i) {
2949 stmt->args[i] = pet_expr_foreach_access_expr(stmt->args[i],
2950 &access_gist, &data);
2951 if (!stmt->args[i])
2952 goto error;
2955 stmt->body = pet_expr_foreach_access_expr(stmt->body,
2956 &access_gist, &data);
2957 if (!stmt->body)
2958 goto error;
2960 isl_set_free(data.domain);
2962 space = isl_set_get_space(stmt->domain);
2963 if (isl_space_is_wrapping(space))
2964 space = isl_space_domain(isl_space_unwrap(space));
2965 domain = isl_set_universe(space);
2966 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2967 if (stmt->n_arg > 0)
2968 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
2969 value_bounds);
2970 stmt->domain = isl_set_gist(stmt->domain, domain);
2971 if (!stmt->domain)
2972 return pet_stmt_free(stmt);
2974 return stmt;
2975 error:
2976 isl_set_free(data.domain);
2977 return pet_stmt_free(stmt);
2980 /* Compute the gist of the extent of the array
2981 * based on the constraints on the parameters specified by "context".
2983 static struct pet_array *array_gist(struct pet_array *array,
2984 __isl_keep isl_set *context)
2986 if (!array)
2987 return NULL;
2989 array->extent = isl_set_gist_params(array->extent,
2990 isl_set_copy(context));
2991 if (!array->extent)
2992 return pet_array_free(array);
2994 return array;
2997 /* Compute the gist of all sets and relations in "scop"
2998 * based on the constraints on the parameters specified by "scop->context"
2999 * and the constraints on the values of nested accesses specified
3000 * by "value_bounds".
3002 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3003 __isl_keep isl_union_map *value_bounds)
3005 int i;
3007 if (!scop)
3008 return NULL;
3010 scop->context = isl_set_coalesce(scop->context);
3011 if (!scop->context)
3012 return pet_scop_free(scop);
3014 for (i = 0; i < scop->n_array; ++i) {
3015 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3016 if (!scop->arrays[i])
3017 return pet_scop_free(scop);
3020 for (i = 0; i < scop->n_stmt; ++i) {
3021 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3022 value_bounds);
3023 if (!scop->stmts[i])
3024 return pet_scop_free(scop);
3027 return scop;
3030 /* Intersect the context of "scop" with "context".
3031 * To ensure that we don't introduce any unnamed parameters in
3032 * the context of "scop", we first remove the unnamed parameters
3033 * from "context".
3035 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3036 __isl_take isl_set *context)
3038 if (!scop)
3039 goto error;
3041 context = set_project_out_unnamed_params(context);
3042 scop->context = isl_set_intersect(scop->context, context);
3043 if (!scop->context)
3044 return pet_scop_free(scop);
3046 return scop;
3047 error:
3048 isl_set_free(context);
3049 return pet_scop_free(scop);
3052 /* Drop the current context of "scop". That is, replace the context
3053 * by a universal set.
3055 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3057 isl_space *space;
3059 if (!scop)
3060 return NULL;
3062 space = isl_set_get_space(scop->context);
3063 isl_set_free(scop->context);
3064 scop->context = isl_set_universe(space);
3065 if (!scop->context)
3066 return pet_scop_free(scop);
3068 return scop;
3071 /* Append "array" to the arrays of "scop".
3073 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3074 struct pet_array *array)
3076 isl_ctx *ctx;
3077 struct pet_array **arrays;
3079 if (!array || !scop)
3080 goto error;
3082 ctx = isl_set_get_ctx(scop->context);
3083 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3084 scop->n_array + 1);
3085 if (!arrays)
3086 goto error;
3087 scop->arrays = arrays;
3088 scop->arrays[scop->n_array] = array;
3089 scop->n_array++;
3091 return scop;
3092 error:
3093 pet_array_free(array);
3094 return pet_scop_free(scop);