scop.c: update_domain: rename variable "dim" to "space"
[pet.git] / scop.c
blob0edfd90eec627327dc2bc02b8809833d2fafacca
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2013 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 as index expressions defined
85 * over a zero-dimensiona domain. The index expression is either
86 * a boolean affine expression or an access to a variable, which
87 * is assumed to attain values zero and one. The condition holds
88 * if the variable has value one or if the affine expression
89 * has value one (typically for only part of the parameter space).
91 * A missing condition (skip[type] == NULL) means that we don't want
92 * to skip anything.
94 struct pet_scop_ext {
95 struct pet_scop scop;
97 isl_multi_pw_aff *skip[2];
100 const char *pet_op_str(enum pet_op_type op)
102 return op_str[op];
105 int pet_op_is_inc_dec(enum pet_op_type op)
107 return op == pet_op_post_inc || op == pet_op_post_dec ||
108 op == pet_op_pre_inc || op == pet_op_pre_dec;
111 const char *pet_type_str(enum pet_expr_type type)
113 return type_str[type];
116 enum pet_op_type pet_str_op(const char *str)
118 int i;
120 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
121 if (!strcmp(op_str[i], str))
122 return i;
124 return -1;
127 enum pet_expr_type pet_str_type(const char *str)
129 int i;
131 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
132 if (!strcmp(type_str[i], str))
133 return i;
135 return -1;
138 /* Construct a pet_expr from an access relation.
139 * By default, it is considered to be a read access.
141 struct pet_expr *pet_expr_from_access(__isl_take isl_map *access)
143 isl_ctx *ctx = isl_map_get_ctx(access);
144 struct pet_expr *expr;
146 if (!access)
147 return NULL;
148 expr = isl_calloc_type(ctx, struct pet_expr);
149 if (!expr)
150 goto error;
152 expr->type = pet_expr_access;
153 expr->acc.access = access;
154 expr->acc.read = 1;
155 expr->acc.write = 0;
157 return expr;
158 error:
159 isl_map_free(access);
160 return NULL;
163 /* Construct an access pet_expr from an index expression.
164 * By default, the access is considered to be a read access.
166 struct pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
168 isl_map *access;
170 access = isl_map_from_multi_pw_aff(index);
171 return pet_expr_from_access(access);
174 /* Construct an access pet_expr from an index expression and
175 * the depth of the accessed array.
176 * By default, the access is considered to be a read access.
178 * If the number of indices is smaller than the depth of the array,
179 * then we assume that all elements of the remaining dimensions
180 * are accessed.
182 struct pet_expr *pet_expr_from_index_and_depth(
183 __isl_take isl_multi_pw_aff *index, int depth)
185 isl_id *id;
186 isl_map *access;
187 int dim;
189 access = isl_map_from_multi_pw_aff(index);
190 if (!access)
191 return NULL;
192 dim = isl_map_dim(access, isl_dim_out);
193 if (dim > depth)
194 isl_die(isl_map_get_ctx(access), isl_error_internal,
195 "number of indices greater than depth",
196 access = isl_map_free(access));
197 if (dim == depth)
198 return pet_expr_from_access(access);
200 id = isl_map_get_tuple_id(access, isl_dim_out);
201 access = isl_map_add_dims(access, isl_dim_out, depth - dim);
202 access = isl_map_set_tuple_id(access, isl_dim_out, id);
204 return pet_expr_from_access(access);
207 /* Construct a pet_expr that kills the elements specified by "access".
209 struct pet_expr *pet_expr_kill_from_access(__isl_take isl_map *access)
211 isl_ctx *ctx;
212 struct pet_expr *expr;
214 ctx = isl_map_get_ctx(access);
215 expr = pet_expr_from_access(access);
216 if (!expr)
217 return NULL;
218 expr->acc.read = 0;
219 return pet_expr_new_unary(ctx, pet_op_kill, expr);
222 /* Construct a pet_expr that kills the elements specified by
223 * the index expression "index" and the access relation "access".
225 * We currently ignore "index".
227 struct pet_expr *pet_expr_kill_from_access_and_index(__isl_take isl_map *access,
228 __isl_take isl_multi_pw_aff *index)
230 if (!access || !index)
231 goto error;
232 isl_multi_pw_aff_free(index);
233 return pet_expr_kill_from_access(access);
234 error:
235 isl_map_free(access);
236 isl_multi_pw_aff_free(index);
237 return NULL;
240 /* Construct a unary pet_expr that performs "op" on "arg".
242 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
243 struct pet_expr *arg)
245 struct pet_expr *expr;
247 if (!arg)
248 goto error;
249 expr = isl_alloc_type(ctx, struct pet_expr);
250 if (!expr)
251 goto error;
253 expr->type = pet_expr_unary;
254 expr->op = op;
255 expr->n_arg = 1;
256 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
257 if (!expr->args)
258 goto error;
259 expr->args[pet_un_arg] = arg;
261 return expr;
262 error:
263 pet_expr_free(arg);
264 return NULL;
267 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
269 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
270 struct pet_expr *lhs, struct pet_expr *rhs)
272 struct pet_expr *expr;
274 if (!lhs || !rhs)
275 goto error;
276 expr = isl_alloc_type(ctx, struct pet_expr);
277 if (!expr)
278 goto error;
280 expr->type = pet_expr_binary;
281 expr->op = op;
282 expr->n_arg = 2;
283 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
284 if (!expr->args)
285 goto error;
286 expr->args[pet_bin_lhs] = lhs;
287 expr->args[pet_bin_rhs] = rhs;
289 return expr;
290 error:
291 pet_expr_free(lhs);
292 pet_expr_free(rhs);
293 return NULL;
296 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
298 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
299 struct pet_expr *lhs, struct pet_expr *rhs)
301 struct pet_expr *expr;
303 if (!cond || !lhs || !rhs)
304 goto error;
305 expr = isl_alloc_type(ctx, struct pet_expr);
306 if (!expr)
307 goto error;
309 expr->type = pet_expr_ternary;
310 expr->n_arg = 3;
311 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
312 if (!expr->args)
313 goto error;
314 expr->args[pet_ter_cond] = cond;
315 expr->args[pet_ter_true] = lhs;
316 expr->args[pet_ter_false] = rhs;
318 return expr;
319 error:
320 pet_expr_free(cond);
321 pet_expr_free(lhs);
322 pet_expr_free(rhs);
323 return NULL;
326 /* Construct a call pet_expr that calls function "name" with "n_arg"
327 * arguments. The caller is responsible for filling in the arguments.
329 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
330 unsigned n_arg)
332 struct pet_expr *expr;
334 expr = isl_alloc_type(ctx, struct pet_expr);
335 if (!expr)
336 return NULL;
338 expr->type = pet_expr_call;
339 expr->n_arg = n_arg;
340 expr->name = strdup(name);
341 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
342 if (!expr->name || !expr->args)
343 return pet_expr_free(expr);
345 return expr;
348 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
350 struct pet_expr *pet_expr_new_cast(isl_ctx *ctx, const char *type_name,
351 struct pet_expr *arg)
353 struct pet_expr *expr;
355 if (!arg)
356 return NULL;
358 expr = isl_alloc_type(ctx, struct pet_expr);
359 if (!expr)
360 goto error;
362 expr->type = pet_expr_cast;
363 expr->n_arg = 1;
364 expr->type_name = strdup(type_name);
365 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
366 if (!expr->type_name || !expr->args)
367 goto error;
369 expr->args[0] = arg;
371 return expr;
372 error:
373 pet_expr_free(arg);
374 pet_expr_free(expr);
375 return NULL;
378 /* Construct a pet_expr that represents the double "d".
380 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
382 struct pet_expr *expr;
384 expr = isl_calloc_type(ctx, struct pet_expr);
385 if (!expr)
386 return NULL;
388 expr->type = pet_expr_double;
389 expr->d.val = val;
390 expr->d.s = strdup(s);
391 if (!expr->d.s)
392 return pet_expr_free(expr);
394 return expr;
397 void *pet_expr_free(struct pet_expr *expr)
399 int i;
401 if (!expr)
402 return NULL;
404 for (i = 0; i < expr->n_arg; ++i)
405 pet_expr_free(expr->args[i]);
406 free(expr->args);
408 switch (expr->type) {
409 case pet_expr_access:
410 isl_id_free(expr->acc.ref_id);
411 isl_map_free(expr->acc.access);
412 break;
413 case pet_expr_call:
414 free(expr->name);
415 break;
416 case pet_expr_cast:
417 free(expr->type_name);
418 break;
419 case pet_expr_double:
420 free(expr->d.s);
421 break;
422 case pet_expr_unary:
423 case pet_expr_binary:
424 case pet_expr_ternary:
425 break;
428 free(expr);
429 return NULL;
432 static void expr_dump(struct pet_expr *expr, int indent)
434 int i;
436 if (!expr)
437 return;
439 fprintf(stderr, "%*s", indent, "");
441 switch (expr->type) {
442 case pet_expr_double:
443 fprintf(stderr, "%s\n", expr->d.s);
444 break;
445 case pet_expr_access:
446 isl_id_dump(expr->acc.ref_id);
447 fprintf(stderr, "%*s", indent, "");
448 isl_map_dump(expr->acc.access);
449 fprintf(stderr, "%*sread: %d\n", indent + 2,
450 "", expr->acc.read);
451 fprintf(stderr, "%*swrite: %d\n", indent + 2,
452 "", expr->acc.write);
453 for (i = 0; i < expr->n_arg; ++i)
454 expr_dump(expr->args[i], indent + 2);
455 break;
456 case pet_expr_unary:
457 fprintf(stderr, "%s\n", op_str[expr->op]);
458 expr_dump(expr->args[pet_un_arg], indent + 2);
459 break;
460 case pet_expr_binary:
461 fprintf(stderr, "%s\n", op_str[expr->op]);
462 expr_dump(expr->args[pet_bin_lhs], indent + 2);
463 expr_dump(expr->args[pet_bin_rhs], indent + 2);
464 break;
465 case pet_expr_ternary:
466 fprintf(stderr, "?:\n");
467 expr_dump(expr->args[pet_ter_cond], indent + 2);
468 expr_dump(expr->args[pet_ter_true], indent + 2);
469 expr_dump(expr->args[pet_ter_false], indent + 2);
470 break;
471 case pet_expr_call:
472 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
473 for (i = 0; i < expr->n_arg; ++i)
474 expr_dump(expr->args[i], indent + 2);
475 break;
476 case pet_expr_cast:
477 fprintf(stderr, "(%s)\n", expr->type_name);
478 for (i = 0; i < expr->n_arg; ++i)
479 expr_dump(expr->args[i], indent + 2);
480 break;
484 void pet_expr_dump(struct pet_expr *expr)
486 expr_dump(expr, 0);
489 /* Does "expr" represent an access to an unnamed space, i.e.,
490 * does it represent an affine expression?
492 int pet_expr_is_affine(struct pet_expr *expr)
494 int has_id;
496 if (!expr)
497 return -1;
498 if (expr->type != pet_expr_access)
499 return 0;
501 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
502 if (has_id < 0)
503 return -1;
505 return !has_id;
508 /* Return the identifier of the array accessed by "expr".
510 __isl_give isl_id *pet_expr_access_get_id(struct pet_expr *expr)
512 if (!expr)
513 return NULL;
514 if (expr->type != pet_expr_access)
515 return NULL;
516 return isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
519 /* Does "expr" represent an access to a scalar, i.e., zero-dimensional array?
521 int pet_expr_is_scalar_access(struct pet_expr *expr)
523 if (!expr)
524 return -1;
525 if (expr->type != pet_expr_access)
526 return 0;
528 return isl_map_dim(expr->acc.access, isl_dim_out) == 0;
531 /* Return 1 if the two pet_exprs are equivalent.
533 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
535 int i;
537 if (!expr1 || !expr2)
538 return 0;
540 if (expr1->type != expr2->type)
541 return 0;
542 if (expr1->n_arg != expr2->n_arg)
543 return 0;
544 for (i = 0; i < expr1->n_arg; ++i)
545 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
546 return 0;
547 switch (expr1->type) {
548 case pet_expr_double:
549 if (strcmp(expr1->d.s, expr2->d.s))
550 return 0;
551 if (expr1->d.val != expr2->d.val)
552 return 0;
553 break;
554 case pet_expr_access:
555 if (expr1->acc.read != expr2->acc.read)
556 return 0;
557 if (expr1->acc.write != expr2->acc.write)
558 return 0;
559 if (expr1->acc.ref_id != expr2->acc.ref_id)
560 return 0;
561 if (!expr1->acc.access || !expr2->acc.access)
562 return 0;
563 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
564 return 0;
565 break;
566 case pet_expr_unary:
567 case pet_expr_binary:
568 case pet_expr_ternary:
569 if (expr1->op != expr2->op)
570 return 0;
571 break;
572 case pet_expr_call:
573 if (strcmp(expr1->name, expr2->name))
574 return 0;
575 break;
576 case pet_expr_cast:
577 if (strcmp(expr1->type_name, expr2->type_name))
578 return 0;
579 break;
582 return 1;
585 /* Add extra conditions on the parameters to all access relations in "expr".
587 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
588 __isl_take isl_set *cond)
590 int i;
592 if (!expr)
593 goto error;
595 for (i = 0; i < expr->n_arg; ++i) {
596 expr->args[i] = pet_expr_restrict(expr->args[i],
597 isl_set_copy(cond));
598 if (!expr->args[i])
599 goto error;
602 if (expr->type == pet_expr_access) {
603 expr->acc.access = isl_map_intersect_params(expr->acc.access,
604 isl_set_copy(cond));
605 if (!expr->acc.access)
606 goto error;
609 isl_set_free(cond);
610 return expr;
611 error:
612 isl_set_free(cond);
613 return pet_expr_free(expr);
616 /* Modify all expressions of type pet_expr_access in "expr"
617 * by calling "fn" on them.
619 struct pet_expr *pet_expr_map_access(struct pet_expr *expr,
620 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
621 void *user)
623 int i;
625 if (!expr)
626 return NULL;
628 for (i = 0; i < expr->n_arg; ++i) {
629 expr->args[i] = pet_expr_map_access(expr->args[i], fn, user);
630 if (!expr->args[i])
631 return pet_expr_free(expr);
634 if (expr->type == pet_expr_access)
635 expr = fn(expr, user);
637 return expr;
640 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
642 * Return -1 on error (where fn return a negative value is treated as an error).
643 * Otherwise return 0.
645 int pet_expr_foreach_access_expr(struct pet_expr *expr,
646 int (*fn)(struct pet_expr *expr, void *user), void *user)
648 int i;
650 if (!expr)
651 return -1;
653 for (i = 0; i < expr->n_arg; ++i)
654 if (pet_expr_foreach_access_expr(expr->args[i], fn, user) < 0)
655 return -1;
657 if (expr->type == pet_expr_access)
658 return fn(expr, user);
660 return 0;
663 /* Modify the access relation of the given access expression
664 * based on the given iteration space transformation.
665 * If the access has any arguments then the domain of the access relation
666 * is a wrapped mapping from the iteration space to the space of
667 * argument values. We only need to change the domain of this wrapped
668 * mapping, so we extend the input transformation with an identity mapping
669 * on the space of argument values.
671 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
673 isl_map *update = user;
674 isl_space *space;
676 update = isl_map_copy(update);
678 space = isl_map_get_space(expr->acc.access);
679 space = isl_space_domain(space);
680 if (!isl_space_is_wrapping(space))
681 isl_space_free(space);
682 else {
683 isl_map *id;
684 space = isl_space_unwrap(space);
685 space = isl_space_range(space);
686 space = isl_space_map_from_set(space);
687 id = isl_map_identity(space);
688 update = isl_map_product(update, id);
691 expr->acc.access = isl_map_apply_domain(expr->acc.access, update);
692 if (!expr->acc.access)
693 return pet_expr_free(expr);
695 return expr;
698 /* Modify all access relations in "expr" based on the given iteration space
699 * transformation.
701 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
702 __isl_take isl_map *update)
704 expr = pet_expr_map_access(expr, &update_domain, update);
705 isl_map_free(update);
706 return expr;
709 /* Construct a pet_stmt with given line number and statement
710 * number from a pet_expr.
711 * The initial iteration domain is the zero-dimensional universe.
712 * The name of the domain is given by "label" if it is non-NULL.
713 * Otherwise, the name is constructed as S_<id>.
714 * The domains of all access relations are modified to refer
715 * to the statement iteration domain.
717 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
718 __isl_take isl_id *label, int id, struct pet_expr *expr)
720 struct pet_stmt *stmt;
721 isl_space *dim;
722 isl_set *dom;
723 isl_map *sched;
724 isl_map *add_name;
725 char name[50];
727 if (!expr)
728 goto error;
730 stmt = isl_calloc_type(ctx, struct pet_stmt);
731 if (!stmt)
732 goto error;
734 dim = isl_space_set_alloc(ctx, 0, 0);
735 if (label)
736 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
737 else {
738 snprintf(name, sizeof(name), "S_%d", id);
739 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
741 dom = isl_set_universe(isl_space_copy(dim));
742 sched = isl_map_from_domain(isl_set_copy(dom));
744 dim = isl_space_from_range(dim);
745 add_name = isl_map_universe(dim);
746 expr = expr_update_domain(expr, add_name);
748 stmt->line = line;
749 stmt->domain = dom;
750 stmt->schedule = sched;
751 stmt->body = expr;
753 if (!stmt->domain || !stmt->schedule || !stmt->body)
754 return pet_stmt_free(stmt);
756 return stmt;
757 error:
758 isl_id_free(label);
759 return pet_expr_free(expr);
762 void *pet_stmt_free(struct pet_stmt *stmt)
764 int i;
766 if (!stmt)
767 return NULL;
769 isl_set_free(stmt->domain);
770 isl_map_free(stmt->schedule);
771 pet_expr_free(stmt->body);
773 for (i = 0; i < stmt->n_arg; ++i)
774 pet_expr_free(stmt->args[i]);
775 free(stmt->args);
777 free(stmt);
778 return NULL;
781 static void stmt_dump(struct pet_stmt *stmt, int indent)
783 int i;
785 if (!stmt)
786 return;
788 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
789 fprintf(stderr, "%*s", indent, "");
790 isl_set_dump(stmt->domain);
791 fprintf(stderr, "%*s", indent, "");
792 isl_map_dump(stmt->schedule);
793 expr_dump(stmt->body, indent);
794 for (i = 0; i < stmt->n_arg; ++i)
795 expr_dump(stmt->args[i], indent + 2);
798 void pet_stmt_dump(struct pet_stmt *stmt)
800 stmt_dump(stmt, 0);
803 struct pet_array *pet_array_free(struct pet_array *array)
805 if (!array)
806 return NULL;
808 isl_set_free(array->context);
809 isl_set_free(array->extent);
810 isl_set_free(array->value_bounds);
811 free(array->element_type);
813 free(array);
814 return NULL;
817 void pet_array_dump(struct pet_array *array)
819 if (!array)
820 return;
822 isl_set_dump(array->context);
823 isl_set_dump(array->extent);
824 isl_set_dump(array->value_bounds);
825 fprintf(stderr, "%s %s\n", array->element_type,
826 array->live_out ? "live-out" : "");
829 /* Alloc a pet_scop structure, with extra room for information that
830 * is only used during parsing.
832 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
834 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
837 /* Construct a pet_scop with room for n statements.
839 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
841 isl_space *space;
842 struct pet_scop *scop;
844 scop = pet_scop_alloc(ctx);
845 if (!scop)
846 return NULL;
848 space = isl_space_params_alloc(ctx, 0);
849 scop->context = isl_set_universe(isl_space_copy(space));
850 scop->context_value = isl_set_universe(space);
851 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
852 if (!scop->context || !scop->stmts)
853 return pet_scop_free(scop);
855 scop->n_stmt = n;
857 return scop;
860 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
862 return scop_alloc(ctx, 0);
865 /* Update "context" with respect to the valid parameter values for "access".
867 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
868 __isl_take isl_set *context)
870 context = isl_set_intersect(context,
871 isl_map_params(isl_map_copy(access)));
872 return context;
875 /* Update "context" with respect to the valid parameter values for "expr".
877 * If "expr" represents a ternary operator, then a parameter value
878 * needs to be valid for the condition and for at least one of the
879 * remaining two arguments.
880 * If the condition is an affine expression, then we can be a bit more specific.
881 * The parameter then has to be valid for the second argument for
882 * non-zero accesses and valid for the third argument for zero accesses.
884 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
885 __isl_take isl_set *context)
887 int i;
889 if (expr->type == pet_expr_ternary) {
890 int is_aff;
891 isl_set *context1, *context2;
893 is_aff = pet_expr_is_affine(expr->args[0]);
894 if (is_aff < 0)
895 goto error;
897 context = expr_extract_context(expr->args[0], context);
898 context1 = expr_extract_context(expr->args[1],
899 isl_set_copy(context));
900 context2 = expr_extract_context(expr->args[2], context);
902 if (is_aff) {
903 isl_map *access;
904 isl_set *zero_set;
906 access = isl_map_copy(expr->args[0]->acc.access);
907 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
908 zero_set = isl_map_params(access);
909 context1 = isl_set_subtract(context1,
910 isl_set_copy(zero_set));
911 context2 = isl_set_intersect(context2, zero_set);
914 context = isl_set_union(context1, context2);
915 context = isl_set_coalesce(context);
917 return context;
920 for (i = 0; i < expr->n_arg; ++i)
921 context = expr_extract_context(expr->args[i], context);
923 if (expr->type == pet_expr_access)
924 context = access_extract_context(expr->acc.access, context);
926 return context;
927 error:
928 isl_set_free(context);
929 return NULL;
932 /* Update "context" with respect to the valid parameter values for "stmt".
934 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
935 __isl_take isl_set *context)
937 int i;
939 for (i = 0; i < stmt->n_arg; ++i)
940 context = expr_extract_context(stmt->args[i], context);
942 context = expr_extract_context(stmt->body, context);
944 return context;
947 /* Construct a pet_scop that contains the given pet_stmt.
949 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
951 struct pet_scop *scop;
953 if (!stmt)
954 return NULL;
956 scop = scop_alloc(ctx, 1);
957 if (!scop)
958 goto error;
960 scop->context = stmt_extract_context(stmt, scop->context);
961 if (!scop->context)
962 goto error;
964 scop->stmts[0] = stmt;
966 return scop;
967 error:
968 pet_stmt_free(stmt);
969 pet_scop_free(scop);
970 return NULL;
973 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
974 * does it represent an affine expression?
976 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
978 int has_id;
980 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
981 if (has_id < 0)
982 return -1;
984 return !has_id;
987 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
989 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
990 __isl_take isl_set *dom)
992 isl_pw_aff *pa;
993 pa = isl_set_indicator_function(set);
994 pa = isl_pw_aff_intersect_domain(pa, dom);
995 return pa;
998 /* Return "lhs || rhs", defined on the shared definition domain.
1000 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
1001 __isl_take isl_pw_aff *rhs)
1003 isl_set *cond;
1004 isl_set *dom;
1006 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
1007 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
1008 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
1009 isl_pw_aff_non_zero_set(rhs));
1010 cond = isl_set_coalesce(cond);
1011 return indicator_function(cond, dom);
1014 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
1015 * ext may be equal to either ext1 or ext2.
1017 * The two skips that need to be combined are assumed to be affine expressions.
1019 * We need to skip in ext if we need to skip in either ext1 or ext2.
1020 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
1022 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
1023 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
1024 enum pet_skip type)
1026 isl_pw_aff *skip, *skip1, *skip2;
1028 if (!ext)
1029 return NULL;
1030 if (!ext1->skip[type] && !ext2->skip[type])
1031 return ext;
1032 if (!ext1->skip[type]) {
1033 if (ext == ext2)
1034 return ext;
1035 ext->skip[type] = ext2->skip[type];
1036 ext2->skip[type] = NULL;
1037 return ext;
1039 if (!ext2->skip[type]) {
1040 if (ext == ext1)
1041 return ext;
1042 ext->skip[type] = ext1->skip[type];
1043 ext1->skip[type] = NULL;
1044 return ext;
1047 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
1048 !multi_pw_aff_is_affine(ext2->skip[type]))
1049 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
1050 isl_error_internal, "can only combine affine skips",
1051 return pet_scop_free(&ext->scop));
1053 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
1054 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
1055 skip = pw_aff_or(skip1, skip2);
1056 isl_multi_pw_aff_free(ext1->skip[type]);
1057 ext1->skip[type] = NULL;
1058 isl_multi_pw_aff_free(ext2->skip[type]);
1059 ext2->skip[type] = NULL;
1060 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1061 if (!ext->skip[type])
1062 return pet_scop_free(&ext->scop);
1064 return ext;
1067 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
1068 * where type takes on the values pet_skip_now and pet_skip_later.
1069 * scop may be equal to either scop1 or scop2.
1071 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
1072 struct pet_scop *scop1, struct pet_scop *scop2)
1074 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1075 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
1076 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
1078 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
1079 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
1080 return &ext->scop;
1083 /* Update scop->start and scop->end to include the region from "start"
1084 * to "end". In particular, if scop->end == 0, then "scop" does not
1085 * have any offset information yet and we simply take the information
1086 * from "start" and "end". Otherwise, we update the fields if the
1087 * region from "start" to "end" is not already included.
1089 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
1090 unsigned start, unsigned end)
1092 if (!scop)
1093 return NULL;
1094 if (scop->end == 0) {
1095 scop->start = start;
1096 scop->end = end;
1097 } else {
1098 if (start < scop->start)
1099 scop->start = start;
1100 if (end > scop->end)
1101 scop->end = end;
1104 return scop;
1107 /* Does "implication" appear in the list of implications of "scop"?
1109 static int is_known_implication(struct pet_scop *scop,
1110 struct pet_implication *implication)
1112 int i;
1114 for (i = 0; i < scop->n_implication; ++i) {
1115 struct pet_implication *pi = scop->implications[i];
1116 int equal;
1118 if (pi->satisfied != implication->satisfied)
1119 continue;
1120 equal = isl_map_is_equal(pi->extension, implication->extension);
1121 if (equal < 0)
1122 return -1;
1123 if (equal)
1124 return 1;
1127 return 0;
1130 /* Store the concatenation of the impliciations of "scop1" and "scop2"
1131 * in "scop", removing duplicates (i.e., implications in "scop2" that
1132 * already appear in "scop1").
1134 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
1135 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
1137 int i, j;
1139 if (!scop)
1140 return NULL;
1142 if (scop2->n_implication == 0) {
1143 scop->n_implication = scop1->n_implication;
1144 scop->implications = scop1->implications;
1145 scop1->n_implication = 0;
1146 scop1->implications = NULL;
1147 return scop;
1150 if (scop1->n_implication == 0) {
1151 scop->n_implication = scop2->n_implication;
1152 scop->implications = scop2->implications;
1153 scop2->n_implication = 0;
1154 scop2->implications = NULL;
1155 return scop;
1158 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1159 scop1->n_implication + scop2->n_implication);
1160 if (!scop->implications)
1161 return pet_scop_free(scop);
1163 for (i = 0; i < scop1->n_implication; ++i) {
1164 scop->implications[i] = scop1->implications[i];
1165 scop1->implications[i] = NULL;
1168 scop->n_implication = scop1->n_implication;
1169 j = scop1->n_implication;
1170 for (i = 0; i < scop2->n_implication; ++i) {
1171 int known;
1173 known = is_known_implication(scop, scop2->implications[i]);
1174 if (known < 0)
1175 return pet_scop_free(scop);
1176 if (known)
1177 continue;
1178 scop->implications[j++] = scop2->implications[i];
1179 scop2->implications[i] = NULL;
1181 scop->n_implication = j;
1183 return scop;
1186 /* Combine the offset information of "scop1" and "scop2" into "scop".
1188 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
1189 struct pet_scop *scop1, struct pet_scop *scop2)
1191 if (scop1->end)
1192 scop = pet_scop_update_start_end(scop,
1193 scop1->start, scop1->end);
1194 if (scop2->end)
1195 scop = pet_scop_update_start_end(scop,
1196 scop2->start, scop2->end);
1197 return scop;
1200 /* Construct a pet_scop that contains the offset information,
1201 * arrays, statements and skip information in "scop1" and "scop2".
1203 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1204 struct pet_scop *scop2)
1206 int i;
1207 struct pet_scop *scop = NULL;
1209 if (!scop1 || !scop2)
1210 goto error;
1212 if (scop1->n_stmt == 0) {
1213 scop2 = scop_combine_skips(scop2, scop1, scop2);
1214 pet_scop_free(scop1);
1215 return scop2;
1218 if (scop2->n_stmt == 0) {
1219 scop1 = scop_combine_skips(scop1, scop1, scop2);
1220 pet_scop_free(scop2);
1221 return scop1;
1224 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1225 if (!scop)
1226 goto error;
1228 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1229 scop1->n_array + scop2->n_array);
1230 if (!scop->arrays)
1231 goto error;
1232 scop->n_array = scop1->n_array + scop2->n_array;
1234 for (i = 0; i < scop1->n_stmt; ++i) {
1235 scop->stmts[i] = scop1->stmts[i];
1236 scop1->stmts[i] = NULL;
1239 for (i = 0; i < scop2->n_stmt; ++i) {
1240 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1241 scop2->stmts[i] = NULL;
1244 for (i = 0; i < scop1->n_array; ++i) {
1245 scop->arrays[i] = scop1->arrays[i];
1246 scop1->arrays[i] = NULL;
1249 for (i = 0; i < scop2->n_array; ++i) {
1250 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1251 scop2->arrays[i] = NULL;
1254 scop = scop_collect_implications(ctx, scop, scop1, scop2);
1255 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1256 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1257 scop = scop_combine_skips(scop, scop1, scop2);
1258 scop = scop_combine_start_end(scop, scop1, scop2);
1260 pet_scop_free(scop1);
1261 pet_scop_free(scop2);
1262 return scop;
1263 error:
1264 pet_scop_free(scop1);
1265 pet_scop_free(scop2);
1266 pet_scop_free(scop);
1267 return NULL;
1270 /* Apply the skip condition "skip" to "scop".
1271 * That is, make sure "scop" is not executed when the condition holds.
1273 * If "skip" is an affine expression, we add the conditions under
1274 * which the expression is zero to the iteration domains.
1275 * Otherwise, we add a filter on the variable attaining the value zero.
1277 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1278 __isl_take isl_multi_pw_aff *skip)
1280 isl_set *zero;
1281 isl_pw_aff *pa;
1282 int is_aff;
1284 if (!scop || !skip)
1285 goto error;
1287 is_aff = multi_pw_aff_is_affine(skip);
1288 if (is_aff < 0)
1289 goto error;
1291 if (!is_aff)
1292 return pet_scop_filter(scop, skip, 0);
1294 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1295 isl_multi_pw_aff_free(skip);
1296 zero = isl_set_params(isl_pw_aff_zero_set(pa));
1297 scop = pet_scop_restrict(scop, zero);
1299 return scop;
1300 error:
1301 isl_multi_pw_aff_free(skip);
1302 return pet_scop_free(scop);
1305 /* Construct a pet_scop that contains the arrays, statements and
1306 * skip information in "scop1" and "scop2", where the two scops
1307 * are executed "in sequence". That is, breaks and continues
1308 * in scop1 have an effect on scop2.
1310 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1311 struct pet_scop *scop2)
1313 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1314 scop2 = restrict_skip(scop2,
1315 pet_scop_get_skip(scop1, pet_skip_now));
1316 return pet_scop_add(ctx, scop1, scop2);
1319 /* Construct a pet_scop that contains the arrays, statements and
1320 * skip information in "scop1" and "scop2", where the two scops
1321 * are executed "in parallel". That is, any break or continue
1322 * in scop1 has no effect on scop2.
1324 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1325 struct pet_scop *scop2)
1327 return pet_scop_add(ctx, scop1, scop2);
1330 void *pet_implication_free(struct pet_implication *implication)
1332 int i;
1334 if (!implication)
1335 return NULL;
1337 isl_map_free(implication->extension);
1339 free(implication);
1340 return NULL;
1343 void *pet_scop_free(struct pet_scop *scop)
1345 int i;
1346 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1348 if (!scop)
1349 return NULL;
1350 isl_set_free(scop->context);
1351 isl_set_free(scop->context_value);
1352 if (scop->arrays)
1353 for (i = 0; i < scop->n_array; ++i)
1354 pet_array_free(scop->arrays[i]);
1355 free(scop->arrays);
1356 if (scop->stmts)
1357 for (i = 0; i < scop->n_stmt; ++i)
1358 pet_stmt_free(scop->stmts[i]);
1359 free(scop->stmts);
1360 if (scop->implications)
1361 for (i = 0; i < scop->n_implication; ++i)
1362 pet_implication_free(scop->implications[i]);
1363 free(scop->implications);
1364 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1365 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1366 free(scop);
1367 return NULL;
1370 void pet_implication_dump(struct pet_implication *implication)
1372 if (!implication)
1373 return;
1375 fprintf(stderr, "%d\n", implication->satisfied);
1376 isl_map_dump(implication->extension);
1379 void pet_scop_dump(struct pet_scop *scop)
1381 int i;
1382 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1384 if (!scop)
1385 return;
1387 isl_set_dump(scop->context);
1388 isl_set_dump(scop->context_value);
1389 for (i = 0; i < scop->n_array; ++i)
1390 pet_array_dump(scop->arrays[i]);
1391 for (i = 0; i < scop->n_stmt; ++i)
1392 pet_stmt_dump(scop->stmts[i]);
1393 for (i = 0; i < scop->n_implication; ++i)
1394 pet_implication_dump(scop->implications[i]);
1396 if (ext->skip[0]) {
1397 fprintf(stderr, "skip\n");
1398 isl_multi_pw_aff_dump(ext->skip[0]);
1399 isl_multi_pw_aff_dump(ext->skip[1]);
1403 /* Return 1 if the two pet_arrays are equivalent.
1405 * We don't compare element_size as this may be target dependent.
1407 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1409 if (!array1 || !array2)
1410 return 0;
1412 if (!isl_set_is_equal(array1->context, array2->context))
1413 return 0;
1414 if (!isl_set_is_equal(array1->extent, array2->extent))
1415 return 0;
1416 if (!!array1->value_bounds != !!array2->value_bounds)
1417 return 0;
1418 if (array1->value_bounds &&
1419 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1420 return 0;
1421 if (strcmp(array1->element_type, array2->element_type))
1422 return 0;
1423 if (array1->live_out != array2->live_out)
1424 return 0;
1425 if (array1->uniquely_defined != array2->uniquely_defined)
1426 return 0;
1427 if (array1->declared != array2->declared)
1428 return 0;
1429 if (array1->exposed != array2->exposed)
1430 return 0;
1432 return 1;
1435 /* Return 1 if the two pet_stmts are equivalent.
1437 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1439 int i;
1441 if (!stmt1 || !stmt2)
1442 return 0;
1444 if (stmt1->line != stmt2->line)
1445 return 0;
1446 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1447 return 0;
1448 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1449 return 0;
1450 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1451 return 0;
1452 if (stmt1->n_arg != stmt2->n_arg)
1453 return 0;
1454 for (i = 0; i < stmt1->n_arg; ++i) {
1455 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1456 return 0;
1459 return 1;
1462 /* Return 1 if the two pet_implications are equivalent.
1464 int pet_implication_is_equal(struct pet_implication *implication1,
1465 struct pet_implication *implication2)
1467 if (!implication1 || !implication2)
1468 return 0;
1470 if (implication1->satisfied != implication2->satisfied)
1471 return 0;
1472 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1473 return 0;
1475 return 1;
1478 /* Return 1 if the two pet_scops are equivalent.
1480 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1482 int i;
1484 if (!scop1 || !scop2)
1485 return 0;
1487 if (!isl_set_is_equal(scop1->context, scop2->context))
1488 return 0;
1489 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1490 return 0;
1492 if (scop1->n_array != scop2->n_array)
1493 return 0;
1494 for (i = 0; i < scop1->n_array; ++i)
1495 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1496 return 0;
1498 if (scop1->n_stmt != scop2->n_stmt)
1499 return 0;
1500 for (i = 0; i < scop1->n_stmt; ++i)
1501 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1502 return 0;
1504 if (scop1->n_implication != scop2->n_implication)
1505 return 0;
1506 for (i = 0; i < scop1->n_implication; ++i)
1507 if (!pet_implication_is_equal(scop1->implications[i],
1508 scop2->implications[i]))
1509 return 0;
1511 return 1;
1514 /* Prefix the schedule of "stmt" with an extra dimension with constant
1515 * value "pos".
1517 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1519 if (!stmt)
1520 return NULL;
1522 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1523 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1524 if (!stmt->schedule)
1525 return pet_stmt_free(stmt);
1527 return stmt;
1530 /* Prefix the schedules of all statements in "scop" with an extra
1531 * dimension with constant value "pos".
1533 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1535 int i;
1537 if (!scop)
1538 return NULL;
1540 for (i = 0; i < scop->n_stmt; ++i) {
1541 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1542 if (!scop->stmts[i])
1543 return pet_scop_free(scop);
1546 return scop;
1549 /* Given a set with a parameter at "param_pos" that refers to the
1550 * iterator, "move" the iterator to the first set dimension.
1551 * That is, essentially equate the parameter to the first set dimension
1552 * and then project it out.
1554 * The first set dimension may however refer to a virtual iterator,
1555 * while the parameter refers to the "real" iterator.
1556 * We therefore need to take into account the affine expression "iv_map", which
1557 * expresses the real iterator in terms of the virtual iterator.
1558 * In particular, we equate the set dimension to the input of the map
1559 * and the parameter to the output of the map and then project out
1560 * everything we don't need anymore.
1562 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1563 int param_pos, __isl_take isl_aff *iv_map)
1565 isl_map *map, *map2;
1566 map = isl_map_from_domain(set);
1567 map = isl_map_add_dims(map, isl_dim_out, 1);
1568 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1569 map2 = isl_map_from_aff(iv_map);
1570 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1571 map = isl_map_apply_range(map, map2);
1572 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1573 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1574 return isl_map_domain(map);
1577 /* Data used in embed_access.
1578 * extend adds an iterator to the iteration domain
1579 * iv_map expresses the real iterator in terms of the virtual iterator
1580 * var_id represents the induction variable of the corresponding loop
1582 struct pet_embed_access {
1583 isl_map *extend;
1584 isl_aff *iv_map;
1585 isl_id *var_id;
1588 /* Given an access expression, embed the associated access relation
1589 * in an extra outer loop.
1591 * We first update the iteration domain to insert the extra dimension.
1593 * If the access refers to the induction variable, then it is
1594 * turned into an access to the set of integers with index (and value)
1595 * equal to the induction variable.
1597 * If the induction variable appears in the constraints (as a parameter),
1598 * then the parameter is equated to the newly introduced iteration
1599 * domain dimension and subsequently projected out.
1601 * Similarly, if the accessed array is a virtual array (with user
1602 * pointer equal to NULL), as created by create_test_index,
1603 * then it is extended along with the domain of the access.
1605 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
1607 struct pet_embed_access *data = user;
1608 isl_map *access;
1609 isl_id *array_id = NULL;
1610 int pos;
1612 expr = update_domain(expr, data->extend);
1613 if (!expr)
1614 return NULL;
1616 access = expr->acc.access;
1618 if (isl_map_has_tuple_id(access, isl_dim_out))
1619 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1620 if (array_id == data->var_id ||
1621 (array_id && !isl_id_get_user(array_id))) {
1622 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1623 access = isl_map_equate(access,
1624 isl_dim_in, 0, isl_dim_out, 0);
1625 if (array_id == data->var_id)
1626 access = isl_map_apply_range(access,
1627 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1628 else
1629 access = isl_map_set_tuple_id(access, isl_dim_out,
1630 isl_id_copy(array_id));
1632 isl_id_free(array_id);
1634 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1635 if (pos >= 0) {
1636 isl_set *set = isl_map_wrap(access);
1637 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1638 access = isl_set_unwrap(set);
1640 expr->acc.access = isl_map_set_dim_id(access, isl_dim_in, 0,
1641 isl_id_copy(data->var_id));
1642 if (!expr->acc.access)
1643 return pet_expr_free(expr);
1645 return expr;
1648 /* Embed all access subexpressions of "expr" in an extra loop.
1649 * "extend" inserts an outer loop iterator in the iteration domains.
1650 * "iv_map" expresses the real iterator in terms of the virtual iterator
1651 * "var_id" represents the induction variable.
1653 static struct pet_expr *expr_embed(struct pet_expr *expr,
1654 __isl_take isl_map *extend, __isl_take isl_aff *iv_map,
1655 __isl_keep isl_id *var_id)
1657 struct pet_embed_access data =
1658 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1660 expr = pet_expr_map_access(expr, &embed_access, &data);
1661 isl_aff_free(iv_map);
1662 isl_map_free(extend);
1663 return expr;
1666 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1667 * "dom" and schedule "sched". "var_id" represents the induction variable
1668 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1669 * That is, it expresses the iterator that some of the parameters in "stmt"
1670 * may refer to in terms of the iterator used in "dom" and
1671 * the domain of "sched".
1673 * The iteration domain and schedule of the statement are updated
1674 * according to the iteration domain and schedule of the new loop.
1675 * If stmt->domain is a wrapped map, then the iteration domain
1676 * is the domain of this map, so we need to be careful to adjust
1677 * this domain.
1679 * If the induction variable appears in the constraints (as a parameter)
1680 * of the current iteration domain or the schedule of the statement,
1681 * then the parameter is equated to the newly introduced iteration
1682 * domain dimension and subsequently projected out.
1684 * Finally, all access relations are updated based on the extra loop.
1686 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1687 __isl_take isl_set *dom, __isl_take isl_map *sched,
1688 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1690 int i;
1691 int pos;
1692 isl_id *stmt_id;
1693 isl_space *dim;
1694 isl_map *extend;
1696 if (!stmt)
1697 goto error;
1699 if (isl_set_is_wrapping(stmt->domain)) {
1700 isl_map *map;
1701 isl_map *ext;
1702 isl_space *ran_dim;
1704 map = isl_set_unwrap(stmt->domain);
1705 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1706 ran_dim = isl_space_range(isl_map_get_space(map));
1707 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1708 isl_set_universe(ran_dim));
1709 map = isl_map_flat_domain_product(ext, map);
1710 map = isl_map_set_tuple_id(map, isl_dim_in,
1711 isl_id_copy(stmt_id));
1712 dim = isl_space_domain(isl_map_get_space(map));
1713 stmt->domain = isl_map_wrap(map);
1714 } else {
1715 stmt_id = isl_set_get_tuple_id(stmt->domain);
1716 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1717 stmt->domain);
1718 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1719 isl_id_copy(stmt_id));
1720 dim = isl_set_get_space(stmt->domain);
1723 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1724 if (pos >= 0)
1725 stmt->domain = internalize_iv(stmt->domain, pos,
1726 isl_aff_copy(iv_map));
1728 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1729 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1730 isl_dim_in, stmt_id);
1732 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1733 if (pos >= 0) {
1734 isl_set *set = isl_map_wrap(stmt->schedule);
1735 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
1736 stmt->schedule = isl_set_unwrap(set);
1739 dim = isl_space_map_from_set(dim);
1740 extend = isl_map_identity(dim);
1741 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1742 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1743 isl_map_get_tuple_id(extend, isl_dim_out));
1744 for (i = 0; i < stmt->n_arg; ++i)
1745 stmt->args[i] = expr_embed(stmt->args[i], isl_map_copy(extend),
1746 isl_aff_copy(iv_map), var_id);
1747 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1749 isl_set_free(dom);
1750 isl_id_free(var_id);
1752 for (i = 0; i < stmt->n_arg; ++i)
1753 if (!stmt->args[i])
1754 return pet_stmt_free(stmt);
1755 if (!stmt->domain || !stmt->schedule || !stmt->body)
1756 return pet_stmt_free(stmt);
1757 return stmt;
1758 error:
1759 isl_set_free(dom);
1760 isl_map_free(sched);
1761 isl_aff_free(iv_map);
1762 isl_id_free(var_id);
1763 return NULL;
1766 /* Embed the given pet_array in an extra outer loop with iteration domain
1767 * "dom".
1768 * This embedding only has an effect on virtual arrays (those with
1769 * user pointer equal to NULL), which need to be extended along with
1770 * the iteration domain.
1772 static struct pet_array *pet_array_embed(struct pet_array *array,
1773 __isl_take isl_set *dom)
1775 isl_id *array_id = NULL;
1777 if (!array)
1778 goto error;
1780 if (isl_set_has_tuple_id(array->extent))
1781 array_id = isl_set_get_tuple_id(array->extent);
1783 if (array_id && !isl_id_get_user(array_id)) {
1784 array->extent = isl_set_flat_product(dom, array->extent);
1785 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1786 if (!array->extent)
1787 return pet_array_free(array);
1788 } else {
1789 isl_set_free(dom);
1790 isl_id_free(array_id);
1793 return array;
1794 error:
1795 isl_set_free(dom);
1796 return NULL;
1799 /* Project out all unnamed parameters from "set" and return the result.
1801 static __isl_give isl_set *set_project_out_unnamed_params(
1802 __isl_take isl_set *set)
1804 int i, n;
1806 n = isl_set_dim(set, isl_dim_param);
1807 for (i = n - 1; i >= 0; --i) {
1808 if (isl_set_has_dim_name(set, isl_dim_param, i))
1809 continue;
1810 set = isl_set_project_out(set, isl_dim_param, i, 1);
1813 return set;
1816 /* Update the context with respect to an embedding into a loop
1817 * with iteration domain "dom" and induction variable "id".
1818 * "iv_map" expresses the real iterator (parameter "id") in terms
1819 * of a possibly virtual iterator (used in "dom").
1821 * If the current context is independent of "id", we don't need
1822 * to do anything.
1823 * Otherwise, a parameter value is invalid for the embedding if
1824 * any of the corresponding iterator values is invalid.
1825 * That is, a parameter value is valid only if all the corresponding
1826 * iterator values are valid.
1827 * We therefore compute the set of parameters
1829 * forall i in dom : valid (i)
1831 * or
1833 * not exists i in dom : not valid(i)
1835 * i.e.,
1837 * not exists i in dom \ valid(i)
1839 * Before we subtract valid(i) from dom, we first need to substitute
1840 * the real iterator for the virtual iterator.
1842 * If there are any unnamed parameters in "dom", then we consider
1843 * a parameter value to be valid if it is valid for any value of those
1844 * unnamed parameters. They are therefore projected out at the end.
1846 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1847 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
1848 __isl_keep isl_id *id)
1850 int pos;
1851 isl_multi_aff *ma;
1853 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1854 if (pos < 0)
1855 return context;
1857 context = isl_set_from_params(context);
1858 context = isl_set_add_dims(context, isl_dim_set, 1);
1859 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1860 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1861 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
1862 context = isl_set_preimage_multi_aff(context, ma);
1863 context = isl_set_subtract(isl_set_copy(dom), context);
1864 context = isl_set_params(context);
1865 context = isl_set_complement(context);
1866 context = set_project_out_unnamed_params(context);
1867 return context;
1870 /* Update the implication with respect to an embedding into a loop
1871 * with iteration domain "dom".
1873 * Since embed_access extends virtual arrays along with the domain
1874 * of the access, we need to do the same with domain and range
1875 * of the implication. Since the original implication is only valid
1876 * within a given iteration of the loop, the extended implication
1877 * maps the extra array dimension corresponding to the extra loop
1878 * to itself.
1880 static struct pet_implication *pet_implication_embed(
1881 struct pet_implication *implication, __isl_take isl_set *dom)
1883 isl_id *id;
1884 isl_map *map;
1886 if (!implication)
1887 goto error;
1889 map = isl_set_identity(dom);
1890 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1891 map = isl_map_flat_product(map, implication->extension);
1892 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1893 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1894 implication->extension = map;
1895 if (!implication->extension)
1896 return pet_implication_free(implication);
1898 return implication;
1899 error:
1900 isl_set_free(dom);
1901 return NULL;
1904 /* Embed all statements and arrays in "scop" in an extra outer loop
1905 * with iteration domain "dom" and schedule "sched".
1906 * "id" represents the induction variable of the loop.
1907 * "iv_map" maps a possibly virtual iterator to the real iterator.
1908 * That is, it expresses the iterator that some of the parameters in "scop"
1909 * may refer to in terms of the iterator used in "dom" and
1910 * the domain of "sched".
1912 * Any skip conditions within the loop have no effect outside of the loop.
1913 * The caller is responsible for making sure skip[pet_skip_later] has been
1914 * taken into account.
1916 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1917 __isl_take isl_map *sched, __isl_take isl_aff *iv_map,
1918 __isl_take isl_id *id)
1920 int i;
1922 if (!scop)
1923 goto error;
1925 pet_scop_reset_skip(scop, pet_skip_now);
1926 pet_scop_reset_skip(scop, pet_skip_later);
1928 scop->context = context_embed(scop->context, dom, iv_map, id);
1929 if (!scop->context)
1930 goto error;
1932 for (i = 0; i < scop->n_stmt; ++i) {
1933 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1934 isl_set_copy(dom), isl_map_copy(sched),
1935 isl_aff_copy(iv_map), isl_id_copy(id));
1936 if (!scop->stmts[i])
1937 goto error;
1940 for (i = 0; i < scop->n_array; ++i) {
1941 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1942 isl_set_copy(dom));
1943 if (!scop->arrays[i])
1944 goto error;
1947 for (i = 0; i < scop->n_implication; ++i) {
1948 scop->implications[i] =
1949 pet_implication_embed(scop->implications[i],
1950 isl_set_copy(dom));
1951 if (!scop->implications[i])
1952 goto error;
1955 isl_set_free(dom);
1956 isl_map_free(sched);
1957 isl_aff_free(iv_map);
1958 isl_id_free(id);
1959 return scop;
1960 error:
1961 isl_set_free(dom);
1962 isl_map_free(sched);
1963 isl_aff_free(iv_map);
1964 isl_id_free(id);
1965 return pet_scop_free(scop);
1968 /* Add extra conditions on the parameters to iteration domain of "stmt".
1970 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1971 __isl_take isl_set *cond)
1973 if (!stmt)
1974 goto error;
1976 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1978 return stmt;
1979 error:
1980 isl_set_free(cond);
1981 return pet_stmt_free(stmt);
1984 /* Add extra conditions to scop->skip[type].
1986 * The new skip condition only holds if it held before
1987 * and the condition is true. It does not hold if it did not hold
1988 * before or the condition is false.
1990 * The skip condition is assumed to be an affine expression.
1992 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1993 enum pet_skip type, __isl_keep isl_set *cond)
1995 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1996 isl_pw_aff *skip;
1997 isl_set *dom;
1999 if (!scop)
2000 return NULL;
2001 if (!ext->skip[type])
2002 return scop;
2004 if (!multi_pw_aff_is_affine(ext->skip[type]))
2005 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
2006 isl_error_internal, "can only resrict affine skips",
2007 return pet_scop_free(scop));
2009 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2010 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
2011 cond = isl_set_copy(cond);
2012 cond = isl_set_from_params(cond);
2013 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
2014 skip = indicator_function(cond, dom);
2015 isl_multi_pw_aff_free(ext->skip[type]);
2016 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
2017 if (!ext->skip[type])
2018 return pet_scop_free(scop);
2020 return scop;
2023 /* Add extra conditions on the parameters to all iteration domains
2024 * and skip conditions.
2026 * A parameter value is valid for the result if it was valid
2027 * for the original scop and satisfies "cond" or if it does
2028 * not satisfy "cond" as in this case the scop is not executed
2029 * and the original constraints on the parameters are irrelevant.
2031 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
2032 __isl_take isl_set *cond)
2034 int i;
2036 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
2037 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
2039 if (!scop)
2040 goto error;
2042 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
2043 scop->context = isl_set_union(scop->context,
2044 isl_set_complement(isl_set_copy(cond)));
2045 scop->context = isl_set_coalesce(scop->context);
2046 scop->context = set_project_out_unnamed_params(scop->context);
2047 if (!scop->context)
2048 goto error;
2050 for (i = 0; i < scop->n_stmt; ++i) {
2051 scop->stmts[i] = stmt_restrict(scop->stmts[i],
2052 isl_set_copy(cond));
2053 if (!scop->stmts[i])
2054 goto error;
2057 isl_set_free(cond);
2058 return scop;
2059 error:
2060 isl_set_free(cond);
2061 return pet_scop_free(scop);
2064 /* Construct a map that inserts a filter value with name "id" and value
2065 * "satisfied" in the list of filter values embedded in the set space "space".
2067 * If "space" does not contain any filter values yet, we first create
2068 * a map that inserts 0 filter values, i.e.,
2070 * space -> [space -> []]
2072 * We can now assume that space is of the form [dom -> [filters]]
2073 * We construct an identity mapping on dom and a mapping on filters
2074 * that inserts the new filter
2076 * dom -> dom
2077 * [filters] -> [satisfied, filters]
2079 * and then compute the cross product
2081 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
2083 static __isl_give isl_map *insert_filter_map(__isl_take isl_space *space,
2084 __isl_take isl_id *id, int satisfied)
2086 isl_space *space2;
2087 isl_map *map, *map_dom, *map_ran;
2088 isl_set *dom;
2090 if (isl_space_is_wrapping(space)) {
2091 space2 = isl_space_map_from_set(isl_space_copy(space));
2092 map = isl_map_identity(space2);
2093 space = isl_space_unwrap(space);
2094 } else {
2095 space = isl_space_from_domain(space);
2096 map = isl_map_universe(isl_space_copy(space));
2097 map = isl_map_reverse(isl_map_domain_map(map));
2100 space2 = isl_space_domain(isl_space_copy(space));
2101 map_dom = isl_map_identity(isl_space_map_from_set(space2));
2102 space = isl_space_range(space);
2103 map_ran = isl_map_identity(isl_space_map_from_set(space));
2104 map_ran = isl_map_insert_dims(map_ran, isl_dim_out, 0, 1);
2105 map_ran = isl_map_set_dim_id(map_ran, isl_dim_out, 0, id);
2106 map_ran = isl_map_fix_si(map_ran, isl_dim_out, 0, satisfied);
2108 map = isl_map_apply_range(map, isl_map_product(map_dom, map_ran));
2110 return map;
2113 /* Insert an argument expression corresponding to "test" in front
2114 * of the list of arguments described by *n_arg and *args.
2116 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
2117 __isl_keep isl_map *test)
2119 int i;
2120 isl_ctx *ctx = isl_map_get_ctx(test);
2122 if (!test)
2123 return -1;
2125 if (!*args) {
2126 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
2127 if (!*args)
2128 return -1;
2129 } else {
2130 struct pet_expr **ext;
2131 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
2132 if (!ext)
2133 return -1;
2134 for (i = 0; i < *n_arg; ++i)
2135 ext[1 + i] = (*args)[i];
2136 free(*args);
2137 *args = ext;
2139 (*n_arg)++;
2140 (*args)[0] = pet_expr_from_access(isl_map_copy(test));
2141 if (!(*args)[0])
2142 return -1;
2144 return 0;
2147 /* Make the expression "expr" depend on the value of "test"
2148 * being equal to "satisfied".
2150 * If "test" is an affine expression, we simply add the conditions
2151 * on the expression have the value "satisfied" to all access relations.
2153 * Otherwise, we add a filter to "expr" (which is then assumed to be
2154 * an access expression) corresponding to "test" being equal to "satisfied".
2156 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
2157 __isl_take isl_map *test, int satisfied)
2159 isl_id *id;
2160 isl_ctx *ctx;
2161 isl_space *space;
2162 isl_map *map;
2164 if (!expr || !test)
2165 goto error;
2167 if (!isl_map_has_tuple_id(test, isl_dim_out)) {
2168 test = isl_map_fix_si(test, isl_dim_out, 0, satisfied);
2169 return pet_expr_restrict(expr, isl_map_params(test));
2172 ctx = isl_map_get_ctx(test);
2173 if (expr->type != pet_expr_access)
2174 isl_die(ctx, isl_error_invalid,
2175 "can only filter access expressions", goto error);
2177 space = isl_space_domain(isl_map_get_space(expr->acc.access));
2178 id = isl_map_get_tuple_id(test, isl_dim_out);
2179 map = insert_filter_map(space, id, satisfied);
2181 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
2182 if (!expr->acc.access)
2183 goto error;
2185 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
2186 goto error;
2188 isl_map_free(test);
2189 return expr;
2190 error:
2191 isl_map_free(test);
2192 return pet_expr_free(expr);
2195 /* Look through the applications in "scop" for any that can be
2196 * applied to the filter expressed by "map" and "satisified".
2197 * If there is any, then apply it to "map" and return the result.
2198 * Otherwise, return "map".
2199 * "id" is the identifier of the virtual array.
2201 * We only introduce at most one implication for any given virtual array,
2202 * so we can apply the implication and return as soon as we find one.
2204 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
2205 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
2207 int i;
2209 for (i = 0; i < scop->n_implication; ++i) {
2210 struct pet_implication *pi = scop->implications[i];
2211 isl_id *pi_id;
2213 if (pi->satisfied != satisfied)
2214 continue;
2215 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
2216 isl_id_free(pi_id);
2217 if (pi_id != id)
2218 continue;
2220 return isl_map_apply_range(map, isl_map_copy(pi->extension));
2223 return map;
2226 /* Is the filter expressed by "test" and "satisfied" implied
2227 * by filter "pos" on "domain", with filter "expr", taking into
2228 * account the implications of "scop"?
2230 * For filter on domain implying that expressed by "test" and "satisfied",
2231 * the filter needs to be an access to the same (virtual) array as "test" and
2232 * the filter value needs to be equal to "satisfied".
2233 * Moreover, the filter access relation, possibly extended by
2234 * the implications in "scop" needs to contain "test".
2236 static int implies_filter(struct pet_scop *scop,
2237 __isl_keep isl_map *domain, int pos, struct pet_expr *expr,
2238 __isl_keep isl_map *test, int satisfied)
2240 isl_id *test_id, *arg_id;
2241 isl_val *val;
2242 int is_int;
2243 int s;
2244 int is_subset;
2245 isl_map *implied;
2247 if (expr->type != pet_expr_access)
2248 return 0;
2249 test_id = isl_map_get_tuple_id(test, isl_dim_out);
2250 arg_id = pet_expr_access_get_id(expr);
2251 isl_id_free(arg_id);
2252 isl_id_free(test_id);
2253 if (test_id != arg_id)
2254 return 0;
2255 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
2256 is_int = isl_val_is_int(val);
2257 if (is_int)
2258 s = isl_val_get_num_si(val);
2259 isl_val_free(val);
2260 if (!val)
2261 return -1;
2262 if (!is_int)
2263 return 0;
2264 if (s != satisfied)
2265 return 0;
2267 implied = isl_map_copy(expr->acc.access);
2268 implied = apply_implications(scop, implied, test_id, satisfied);
2269 is_subset = isl_map_is_subset(test, implied);
2270 isl_map_free(implied);
2272 return is_subset;
2275 /* Is the filter expressed by "test" and "satisfied" implied
2276 * by any of the filters on the domain of "stmt", taking into
2277 * account the implications of "scop"?
2279 static int filter_implied(struct pet_scop *scop,
2280 struct pet_stmt *stmt, __isl_keep isl_map *test, int satisfied)
2282 int i;
2283 int implied;
2284 isl_id *test_id;
2285 isl_map *domain;
2287 if (!scop || !stmt || !test)
2288 return -1;
2289 if (scop->n_implication == 0)
2290 return 0;
2291 if (stmt->n_arg == 0)
2292 return 0;
2294 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
2296 implied = 0;
2297 for (i = 0; i < stmt->n_arg; ++i) {
2298 implied = implies_filter(scop, domain, i, stmt->args[i],
2299 test, satisfied);
2300 if (implied < 0 || implied)
2301 break;
2304 isl_map_free(domain);
2305 return implied;
2308 /* Make the statement "stmt" depend on the value of "test"
2309 * being equal to "satisfied" by adjusting stmt->domain.
2311 * The domain of "test" corresponds to the (zero or more) outer dimensions
2312 * of the iteration domain.
2314 * We first extend "test" to apply to the entire iteration domain and
2315 * then check if the filter that we are about to add is implied
2316 * by any of the current filters, possibly taking into account
2317 * the implications in "scop". If so, we leave "stmt" untouched and return.
2319 * Otherwise, we insert an argument corresponding to a read to "test"
2320 * from the iteration domain of "stmt" in front of the list of arguments.
2321 * We also insert a corresponding output dimension in the wrapped
2322 * map contained in stmt->domain, with value set to "satisfied".
2324 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
2325 struct pet_stmt *stmt, __isl_take isl_map *test, int satisfied)
2327 int i;
2328 int implied;
2329 isl_id *id;
2330 isl_ctx *ctx;
2331 isl_map *map, *add_dom;
2332 isl_space *space;
2333 isl_set *dom;
2334 int n_test_dom;
2336 if (!stmt || !test)
2337 goto error;
2339 space = isl_set_get_space(stmt->domain);
2340 if (isl_space_is_wrapping(space))
2341 space = isl_space_domain(isl_space_unwrap(space));
2342 dom = isl_set_universe(space);
2343 n_test_dom = isl_map_dim(test, isl_dim_in);
2344 add_dom = isl_map_from_range(dom);
2345 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
2346 for (i = 0; i < n_test_dom; ++i)
2347 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
2348 isl_dim_out, i);
2349 test = isl_map_apply_domain(test, add_dom);
2351 implied = filter_implied(scop, stmt, test, satisfied);
2352 if (implied < 0)
2353 goto error;
2354 if (implied) {
2355 isl_map_free(test);
2356 return stmt;
2359 id = isl_map_get_tuple_id(test, isl_dim_out);
2360 map = insert_filter_map(isl_set_get_space(stmt->domain), id, satisfied);
2361 stmt->domain = isl_set_apply(stmt->domain, map);
2363 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
2364 goto error;
2366 isl_map_free(test);
2367 return stmt;
2368 error:
2369 isl_map_free(test);
2370 return pet_stmt_free(stmt);
2373 /* Does "scop" have a skip condition of the given "type"?
2375 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2377 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2379 if (!scop)
2380 return -1;
2381 return ext->skip[type] != NULL;
2384 /* Does "scop" have a skip condition of the given "type" that
2385 * is an affine expression?
2387 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2389 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2391 if (!scop)
2392 return -1;
2393 if (!ext->skip[type])
2394 return 0;
2395 return multi_pw_aff_is_affine(ext->skip[type]);
2398 /* Does "scop" have a skip condition of the given "type" that
2399 * is not an affine expression?
2401 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2403 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2404 int aff;
2406 if (!scop)
2407 return -1;
2408 if (!ext->skip[type])
2409 return 0;
2410 aff = multi_pw_aff_is_affine(ext->skip[type]);
2411 if (aff < 0)
2412 return -1;
2413 return !aff;
2416 /* Does "scop" have a skip condition of the given "type" that
2417 * is affine and holds on the entire domain?
2419 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2421 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2422 isl_pw_aff *pa;
2423 isl_set *set;
2424 int is_aff;
2425 int is_univ;
2427 is_aff = pet_scop_has_affine_skip(scop, type);
2428 if (is_aff < 0 || !is_aff)
2429 return is_aff;
2431 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2432 set = isl_pw_aff_non_zero_set(pa);
2433 is_univ = isl_set_plain_is_universe(set);
2434 isl_set_free(set);
2436 return is_univ;
2439 /* Replace scop->skip[type] by "skip".
2441 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2442 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2444 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2446 if (!scop || !skip)
2447 goto error;
2449 isl_multi_pw_aff_free(ext->skip[type]);
2450 ext->skip[type] = skip;
2452 return scop;
2453 error:
2454 isl_multi_pw_aff_free(skip);
2455 return pet_scop_free(scop);
2458 /* Return a copy of scop->skip[type].
2460 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2461 enum pet_skip type)
2463 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2465 if (!scop)
2466 return NULL;
2468 return isl_multi_pw_aff_copy(ext->skip[type]);
2471 /* Assuming scop->skip[type] is an affine expression,
2472 * return the constraints on the parameters for which the skip condition
2473 * holds.
2475 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2476 enum pet_skip type)
2478 isl_multi_pw_aff *skip;
2479 isl_pw_aff *pa;
2481 skip = pet_scop_get_skip(scop, type);
2482 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2483 isl_multi_pw_aff_free(skip);
2484 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2487 /* Return a map to the skip condition of the given type.
2489 __isl_give isl_map *pet_scop_get_skip_map(struct pet_scop *scop,
2490 enum pet_skip type)
2492 return isl_map_from_multi_pw_aff(pet_scop_get_skip(scop, type));
2495 /* Return the identifier of the variable that is accessed by
2496 * the skip condition of the given type.
2498 * The skip condition is assumed not to be an affine condition.
2500 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2501 enum pet_skip type)
2503 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2505 if (!scop)
2506 return NULL;
2508 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2511 /* Return an access pet_expr corresponding to the skip condition
2512 * of the given type.
2514 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2515 enum pet_skip type)
2517 return pet_expr_from_access(pet_scop_get_skip_map(scop, type));
2520 /* Drop the the skip condition scop->skip[type].
2522 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2524 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2526 if (!scop)
2527 return;
2529 isl_multi_pw_aff_free(ext->skip[type]);
2530 ext->skip[type] = NULL;
2533 /* Make the skip condition (if any) depend on the value of "test" being
2534 * equal to "satisfied".
2536 * We only support the case where the original skip condition is universal,
2537 * i.e., where skipping is unconditional, and where satisfied == 1.
2538 * In this case, the skip condition is changed to skip only when
2539 * "test" is equal to one.
2541 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2542 enum pet_skip type, __isl_keep isl_map *test, int satisfied)
2544 int is_univ = 0;
2546 if (!scop)
2547 return NULL;
2548 if (!pet_scop_has_skip(scop, type))
2549 return scop;
2551 if (satisfied)
2552 is_univ = pet_scop_has_universal_skip(scop, type);
2553 if (is_univ < 0)
2554 return pet_scop_free(scop);
2555 if (satisfied && is_univ) {
2556 isl_space *space = isl_map_get_space(test);
2557 isl_multi_pw_aff *skip;
2558 skip = isl_multi_pw_aff_zero(space);
2559 scop = pet_scop_set_skip(scop, type, skip);
2560 if (!scop)
2561 return NULL;
2562 } else {
2563 isl_die(isl_map_get_ctx(test), isl_error_internal,
2564 "skip expression cannot be filtered",
2565 return pet_scop_free(scop));
2568 return scop;
2571 /* Make all statements in "scop" depend on the value of "test"
2572 * being equal to "satisfied" by adjusting their domains.
2574 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2575 __isl_take isl_multi_pw_aff *test, int satisfied)
2577 int i;
2578 isl_map *map = isl_map_from_multi_pw_aff(test);
2580 scop = pet_scop_filter_skip(scop, pet_skip_now, map, satisfied);
2581 scop = pet_scop_filter_skip(scop, pet_skip_later, map, satisfied);
2583 if (!scop || !map)
2584 goto error;
2586 for (i = 0; i < scop->n_stmt; ++i) {
2587 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2588 isl_map_copy(map), satisfied);
2589 if (!scop->stmts[i])
2590 goto error;
2593 isl_map_free(map);
2594 return scop;
2595 error:
2596 isl_map_free(map);
2597 return pet_scop_free(scop);
2600 /* Add all parameters in "expr" to "dim" and return the result.
2602 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2603 __isl_take isl_space *dim)
2605 int i;
2607 if (!expr)
2608 goto error;
2609 for (i = 0; i < expr->n_arg; ++i)
2611 dim = expr_collect_params(expr->args[i], dim);
2613 if (expr->type == pet_expr_access)
2614 dim = isl_space_align_params(dim,
2615 isl_map_get_space(expr->acc.access));
2617 return dim;
2618 error:
2619 isl_space_free(dim);
2620 return pet_expr_free(expr);
2623 /* Add all parameters in "stmt" to "dim" and return the result.
2625 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2626 __isl_take isl_space *dim)
2628 if (!stmt)
2629 goto error;
2631 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2632 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2633 dim = expr_collect_params(stmt->body, dim);
2635 return dim;
2636 error:
2637 isl_space_free(dim);
2638 return pet_stmt_free(stmt);
2641 /* Add all parameters in "array" to "dim" and return the result.
2643 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2644 __isl_take isl_space *dim)
2646 if (!array)
2647 goto error;
2649 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2650 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2652 return dim;
2653 error:
2654 pet_array_free(array);
2655 return isl_space_free(dim);
2658 /* Add all parameters in "scop" to "dim" and return the result.
2660 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2661 __isl_take isl_space *dim)
2663 int i;
2665 if (!scop)
2666 goto error;
2668 for (i = 0; i < scop->n_array; ++i)
2669 dim = array_collect_params(scop->arrays[i], dim);
2671 for (i = 0; i < scop->n_stmt; ++i)
2672 dim = stmt_collect_params(scop->stmts[i], dim);
2674 return dim;
2675 error:
2676 isl_space_free(dim);
2677 return pet_scop_free(scop);
2680 /* Add all parameters in "dim" to all access relations in "expr".
2682 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2683 __isl_take isl_space *dim)
2685 int i;
2687 if (!expr)
2688 goto error;
2690 for (i = 0; i < expr->n_arg; ++i) {
2691 expr->args[i] =
2692 expr_propagate_params(expr->args[i],
2693 isl_space_copy(dim));
2694 if (!expr->args[i])
2695 goto error;
2698 if (expr->type == pet_expr_access) {
2699 expr->acc.access = isl_map_align_params(expr->acc.access,
2700 isl_space_copy(dim));
2701 if (!expr->acc.access)
2702 goto error;
2705 isl_space_free(dim);
2706 return expr;
2707 error:
2708 isl_space_free(dim);
2709 return pet_expr_free(expr);
2712 /* Add all parameters in "dim" to the domain, schedule and
2713 * all access relations in "stmt".
2715 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2716 __isl_take isl_space *dim)
2718 if (!stmt)
2719 goto error;
2721 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
2722 stmt->schedule = isl_map_align_params(stmt->schedule,
2723 isl_space_copy(dim));
2724 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
2726 if (!stmt->domain || !stmt->schedule || !stmt->body)
2727 goto error;
2729 isl_space_free(dim);
2730 return stmt;
2731 error:
2732 isl_space_free(dim);
2733 return pet_stmt_free(stmt);
2736 /* Add all parameters in "dim" to "array".
2738 static struct pet_array *array_propagate_params(struct pet_array *array,
2739 __isl_take isl_space *dim)
2741 if (!array)
2742 goto error;
2744 array->context = isl_set_align_params(array->context,
2745 isl_space_copy(dim));
2746 array->extent = isl_set_align_params(array->extent,
2747 isl_space_copy(dim));
2748 if (array->value_bounds) {
2749 array->value_bounds = isl_set_align_params(array->value_bounds,
2750 isl_space_copy(dim));
2751 if (!array->value_bounds)
2752 goto error;
2755 if (!array->context || !array->extent)
2756 goto error;
2758 isl_space_free(dim);
2759 return array;
2760 error:
2761 isl_space_free(dim);
2762 return pet_array_free(array);
2765 /* Add all parameters in "dim" to "scop".
2767 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2768 __isl_take isl_space *dim)
2770 int i;
2772 if (!scop)
2773 goto error;
2775 for (i = 0; i < scop->n_array; ++i) {
2776 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2777 isl_space_copy(dim));
2778 if (!scop->arrays[i])
2779 goto error;
2782 for (i = 0; i < scop->n_stmt; ++i) {
2783 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2784 isl_space_copy(dim));
2785 if (!scop->stmts[i])
2786 goto error;
2789 isl_space_free(dim);
2790 return scop;
2791 error:
2792 isl_space_free(dim);
2793 return pet_scop_free(scop);
2796 /* Update all isl_sets and isl_maps in "scop" such that they all
2797 * have the same parameters.
2799 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2801 isl_space *dim;
2803 if (!scop)
2804 return NULL;
2806 dim = isl_set_get_space(scop->context);
2807 dim = scop_collect_params(scop, dim);
2809 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
2810 scop = scop_propagate_params(scop, dim);
2812 return scop;
2815 /* Check if the given access relation accesses a (0D) array that corresponds
2816 * to one of the parameters in "dim". If so, replace the array access
2817 * by an access to the set of integers with as index (and value)
2818 * that parameter.
2820 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
2821 __isl_take isl_space *dim)
2823 isl_id *array_id = NULL;
2824 int pos = -1;
2826 if (isl_map_has_tuple_id(access, isl_dim_out)) {
2827 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2828 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
2830 isl_space_free(dim);
2832 if (pos < 0) {
2833 isl_id_free(array_id);
2834 return access;
2837 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
2838 if (pos < 0) {
2839 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
2840 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
2841 pos = 0;
2842 } else
2843 isl_id_free(array_id);
2845 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2846 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
2848 return access;
2851 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2852 * in "dim" by a value equal to the corresponding parameter.
2854 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
2855 __isl_take isl_space *dim)
2857 int i;
2859 if (!expr)
2860 goto error;
2862 for (i = 0; i < expr->n_arg; ++i) {
2863 expr->args[i] =
2864 expr_detect_parameter_accesses(expr->args[i],
2865 isl_space_copy(dim));
2866 if (!expr->args[i])
2867 goto error;
2870 if (expr->type == pet_expr_access) {
2871 expr->acc.access = access_detect_parameter(expr->acc.access,
2872 isl_space_copy(dim));
2873 if (!expr->acc.access)
2874 goto error;
2877 isl_space_free(dim);
2878 return expr;
2879 error:
2880 isl_space_free(dim);
2881 return pet_expr_free(expr);
2884 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2885 * in "dim" by a value equal to the corresponding parameter.
2887 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2888 __isl_take isl_space *dim)
2890 if (!stmt)
2891 goto error;
2893 stmt->body = expr_detect_parameter_accesses(stmt->body,
2894 isl_space_copy(dim));
2896 if (!stmt->domain || !stmt->schedule || !stmt->body)
2897 goto error;
2899 isl_space_free(dim);
2900 return stmt;
2901 error:
2902 isl_space_free(dim);
2903 return pet_stmt_free(stmt);
2906 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2907 * in "dim" by a value equal to the corresponding parameter.
2909 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2910 __isl_take isl_space *dim)
2912 int i;
2914 if (!scop)
2915 goto error;
2917 for (i = 0; i < scop->n_stmt; ++i) {
2918 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2919 isl_space_copy(dim));
2920 if (!scop->stmts[i])
2921 goto error;
2924 isl_space_free(dim);
2925 return scop;
2926 error:
2927 isl_space_free(dim);
2928 return pet_scop_free(scop);
2931 /* Replace all accesses to (0D) arrays that correspond to any of
2932 * the parameters used in "scop" by a value equal
2933 * to the corresponding parameter.
2935 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2937 isl_space *dim;
2939 if (!scop)
2940 return NULL;
2942 dim = isl_set_get_space(scop->context);
2943 dim = scop_collect_params(scop, dim);
2945 scop = scop_detect_parameter_accesses(scop, dim);
2947 return scop;
2950 /* Add all read access relations (if "read" is set) and/or all write
2951 * access relations (if "write" is set) to "accesses" and return the result.
2953 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
2954 int read, int write, __isl_take isl_union_map *accesses)
2956 int i;
2957 isl_id *id;
2958 isl_space *dim;
2960 if (!expr)
2961 return NULL;
2963 for (i = 0; i < expr->n_arg; ++i)
2964 accesses = expr_collect_accesses(expr->args[i],
2965 read, write, accesses);
2967 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
2968 ((read && expr->acc.read) || (write && expr->acc.write)))
2969 accesses = isl_union_map_add_map(accesses,
2970 isl_map_copy(expr->acc.access));
2972 return accesses;
2975 /* Collect and return all read access relations (if "read" is set)
2976 * and/or all write access relations (if "write" is set) in "stmt".
2978 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2979 int read, int write, __isl_take isl_space *dim)
2981 isl_union_map *accesses;
2983 if (!stmt)
2984 return NULL;
2986 accesses = isl_union_map_empty(dim);
2987 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
2988 accesses = isl_union_map_intersect_domain(accesses,
2989 isl_union_set_from_set(isl_set_copy(stmt->domain)));
2991 return accesses;
2994 /* Collect and return all read access relations (if "read" is set)
2995 * and/or all write access relations (if "write" is set) in "scop".
2997 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2998 int read, int write)
3000 int i;
3001 isl_union_map *accesses;
3003 if (!scop)
3004 return NULL;
3006 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
3008 for (i = 0; i < scop->n_stmt; ++i) {
3009 isl_union_map *accesses_i;
3010 isl_space *dim = isl_set_get_space(scop->context);
3011 accesses_i = stmt_collect_accesses(scop->stmts[i],
3012 read, write, dim);
3013 accesses = isl_union_map_union(accesses, accesses_i);
3016 return accesses;
3019 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
3021 return scop_collect_accesses(scop, 1, 0);
3024 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
3026 return scop_collect_accesses(scop, 0, 1);
3029 /* Collect and return the union of iteration domains in "scop".
3031 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
3033 int i;
3034 isl_set *domain_i;
3035 isl_union_set *domain;
3037 if (!scop)
3038 return NULL;
3040 domain = isl_union_set_empty(isl_set_get_space(scop->context));
3042 for (i = 0; i < scop->n_stmt; ++i) {
3043 domain_i = isl_set_copy(scop->stmts[i]->domain);
3044 domain = isl_union_set_add_set(domain, domain_i);
3047 return domain;
3050 /* Collect and return the schedules of the statements in "scop".
3051 * The range is normalized to the maximal number of scheduling
3052 * dimensions.
3054 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
3056 int i, j;
3057 isl_map *schedule_i;
3058 isl_union_map *schedule;
3059 int depth, max_depth = 0;
3061 if (!scop)
3062 return NULL;
3064 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
3066 for (i = 0; i < scop->n_stmt; ++i) {
3067 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
3068 if (depth > max_depth)
3069 max_depth = depth;
3072 for (i = 0; i < scop->n_stmt; ++i) {
3073 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
3074 depth = isl_map_dim(schedule_i, isl_dim_out);
3075 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
3076 max_depth - depth);
3077 for (j = depth; j < max_depth; ++j)
3078 schedule_i = isl_map_fix_si(schedule_i,
3079 isl_dim_out, j, 0);
3080 schedule = isl_union_map_add_map(schedule, schedule_i);
3083 return schedule;
3086 /* Does expression "expr" write to "id"?
3088 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
3090 int i;
3091 isl_id *write_id;
3093 for (i = 0; i < expr->n_arg; ++i) {
3094 int writes = expr_writes(expr->args[i], id);
3095 if (writes < 0 || writes)
3096 return writes;
3099 if (expr->type != pet_expr_access)
3100 return 0;
3101 if (!expr->acc.write)
3102 return 0;
3103 if (pet_expr_is_affine(expr))
3104 return 0;
3106 write_id = pet_expr_access_get_id(expr);
3107 isl_id_free(write_id);
3109 if (!write_id)
3110 return -1;
3112 return write_id == id;
3115 /* Does statement "stmt" write to "id"?
3117 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
3119 return expr_writes(stmt->body, id);
3122 /* Is there any write access in "scop" that accesses "id"?
3124 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
3126 int i;
3128 if (!scop)
3129 return -1;
3131 for (i = 0; i < scop->n_stmt; ++i) {
3132 int writes = stmt_writes(scop->stmts[i], id);
3133 if (writes < 0 || writes)
3134 return writes;
3137 return 0;
3140 /* Add a reference identifier to access expression "expr".
3141 * "user" points to an integer that contains the sequence number
3142 * of the next reference.
3144 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
3146 isl_ctx *ctx;
3147 char name[50];
3148 int *n_ref = user;
3150 if (!expr)
3151 return expr;
3153 ctx = isl_map_get_ctx(expr->acc.access);
3154 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
3155 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
3156 if (!expr->acc.ref_id)
3157 return pet_expr_free(expr);
3159 return expr;
3162 /* Add a reference identifier to all access expressions in "stmt".
3163 * "n_ref" points to an integer that contains the sequence number
3164 * of the next reference.
3166 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
3168 int i;
3170 if (!stmt)
3171 return NULL;
3173 for (i = 0; i < stmt->n_arg; ++i) {
3174 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3175 &access_add_ref_id, n_ref);
3176 if (!stmt->args[i])
3177 return pet_stmt_free(stmt);
3180 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
3181 if (!stmt->body)
3182 return pet_stmt_free(stmt);
3184 return stmt;
3187 /* Add a reference identifier to all access expressions in "scop".
3189 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
3191 int i;
3192 int n_ref;
3194 if (!scop)
3195 return NULL;
3197 n_ref = 0;
3198 for (i = 0; i < scop->n_stmt; ++i) {
3199 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
3200 if (!scop->stmts[i])
3201 return pet_scop_free(scop);
3204 return scop;
3207 /* Reset the user pointer on the tuple id and all parameter ids in "set".
3209 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
3211 int i, n;
3213 n = isl_set_dim(set, isl_dim_param);
3214 for (i = 0; i < n; ++i) {
3215 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
3216 const char *name = isl_id_get_name(id);
3217 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
3218 isl_id_free(id);
3221 if (!isl_set_is_params(set) && isl_set_has_tuple_id(set)) {
3222 isl_id *id = isl_set_get_tuple_id(set);
3223 const char *name = isl_id_get_name(id);
3224 set = isl_set_set_tuple_name(set, name);
3225 isl_id_free(id);
3228 return set;
3231 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
3233 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
3235 int i, n;
3237 n = isl_map_dim(map, isl_dim_param);
3238 for (i = 0; i < n; ++i) {
3239 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
3240 const char *name = isl_id_get_name(id);
3241 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
3242 isl_id_free(id);
3245 if (isl_map_has_tuple_id(map, isl_dim_in)) {
3246 isl_id *id = isl_map_get_tuple_id(map, isl_dim_in);
3247 const char *name = isl_id_get_name(id);
3248 map = isl_map_set_tuple_name(map, isl_dim_in, name);
3249 isl_id_free(id);
3252 if (isl_map_has_tuple_id(map, isl_dim_out)) {
3253 isl_id *id = isl_map_get_tuple_id(map, isl_dim_out);
3254 const char *name = isl_id_get_name(id);
3255 map = isl_map_set_tuple_name(map, isl_dim_out, name);
3256 isl_id_free(id);
3259 return map;
3262 /* Reset the user pointer on all parameter ids in "array".
3264 static struct pet_array *array_anonymize(struct pet_array *array)
3266 if (!array)
3267 return NULL;
3269 array->context = set_anonymize(array->context);
3270 array->extent = set_anonymize(array->extent);
3271 if (!array->context || !array->extent)
3272 return pet_array_free(array);
3274 return array;
3277 /* Reset the user pointer on all parameter and tuple ids in
3278 * the access relation of the access expression "expr".
3280 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
3282 expr->acc.access = map_anonymize(expr->acc.access);
3283 if (!expr->acc.access)
3284 return pet_expr_free(expr);
3286 return expr;
3289 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3291 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3293 int i;
3294 isl_space *space;
3295 isl_set *domain;
3297 if (!stmt)
3298 return NULL;
3300 stmt->domain = set_anonymize(stmt->domain);
3301 stmt->schedule = map_anonymize(stmt->schedule);
3302 if (!stmt->domain || !stmt->schedule)
3303 return pet_stmt_free(stmt);
3305 for (i = 0; i < stmt->n_arg; ++i) {
3306 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3307 &access_anonymize, NULL);
3308 if (!stmt->args[i])
3309 return pet_stmt_free(stmt);
3312 stmt->body = pet_expr_map_access(stmt->body,
3313 &access_anonymize, NULL);
3314 if (!stmt->body)
3315 return pet_stmt_free(stmt);
3317 return stmt;
3320 /* Reset the user pointer on the tuple ids and all parameter ids
3321 * in "implication".
3323 static struct pet_implication *implication_anonymize(
3324 struct pet_implication *implication)
3326 if (!implication)
3327 return NULL;
3329 implication->extension = map_anonymize(implication->extension);
3330 if (!implication->extension)
3331 return pet_implication_free(implication);
3333 return implication;
3336 /* Reset the user pointer on all parameter and tuple ids in "scop".
3338 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
3340 int i;
3342 if (!scop)
3343 return NULL;
3345 scop->context = set_anonymize(scop->context);
3346 scop->context_value = set_anonymize(scop->context_value);
3347 if (!scop->context || !scop->context_value)
3348 return pet_scop_free(scop);
3350 for (i = 0; i < scop->n_array; ++i) {
3351 scop->arrays[i] = array_anonymize(scop->arrays[i]);
3352 if (!scop->arrays[i])
3353 return pet_scop_free(scop);
3356 for (i = 0; i < scop->n_stmt; ++i) {
3357 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
3358 if (!scop->stmts[i])
3359 return pet_scop_free(scop);
3362 for (i = 0; i < scop->n_implication; ++i) {
3363 scop->implications[i] =
3364 implication_anonymize(scop->implications[i]);
3365 if (!scop->implications[i])
3366 return pet_scop_free(scop);
3369 return scop;
3372 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
3373 * then intersect the range of "map" with the valid set of values.
3375 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
3376 struct pet_expr *arg, __isl_keep isl_union_map *value_bounds)
3378 isl_id *id;
3379 isl_map *vb;
3380 isl_space *space;
3381 isl_ctx *ctx = isl_map_get_ctx(map);
3383 id = pet_expr_access_get_id(arg);
3384 space = isl_space_alloc(ctx, 0, 0, 1);
3385 space = isl_space_set_tuple_id(space, isl_dim_in, id);
3386 vb = isl_union_map_extract_map(value_bounds, space);
3387 if (!isl_map_plain_is_empty(vb))
3388 map = isl_map_intersect_range(map, isl_map_range(vb));
3389 else
3390 isl_map_free(vb);
3392 return map;
3395 /* Given a set "domain", return a wrapped relation with the given set
3396 * as domain and a range of dimension "n_arg", where each coordinate
3397 * is either unbounded or, if the corresponding element of args is of
3398 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
3400 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
3401 unsigned n_arg, struct pet_expr **args,
3402 __isl_keep isl_union_map *value_bounds)
3404 int i;
3405 isl_map *map;
3406 isl_space *space;
3408 map = isl_map_from_domain(domain);
3409 space = isl_map_get_space(map);
3410 space = isl_space_add_dims(space, isl_dim_out, 1);
3412 for (i = 0; i < n_arg; ++i) {
3413 isl_map *map_i;
3414 struct pet_expr *arg = args[i];
3416 map_i = isl_map_universe(isl_space_copy(space));
3417 if (arg->type == pet_expr_access)
3418 map_i = access_apply_value_bounds(map_i, arg,
3419 value_bounds);
3420 map = isl_map_flat_range_product(map, map_i);
3422 isl_space_free(space);
3424 return isl_map_wrap(map);
3427 /* Data used in access_gist() callback.
3429 struct pet_access_gist_data {
3430 isl_set *domain;
3431 isl_union_map *value_bounds;
3434 /* Given an expression "expr" of type pet_expr_access, compute
3435 * the gist of the associated access relation with respect to
3436 * data->domain and the bounds on the values of the arguments
3437 * of the expression.
3439 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
3441 struct pet_access_gist_data *data = user;
3442 isl_set *domain;
3444 domain = isl_set_copy(data->domain);
3445 if (expr->n_arg > 0)
3446 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
3447 data->value_bounds);
3449 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
3450 if (!expr->acc.access)
3451 return pet_expr_free(expr);
3453 return expr;
3456 /* Compute the gist of the iteration domain and all access relations
3457 * of "stmt" based on the constraints on the parameters specified by "context"
3458 * and the constraints on the values of nested accesses specified
3459 * by "value_bounds".
3461 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3462 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3464 int i;
3465 isl_space *space;
3466 isl_set *domain;
3467 struct pet_access_gist_data data;
3469 if (!stmt)
3470 return NULL;
3472 data.domain = isl_set_copy(stmt->domain);
3473 data.value_bounds = value_bounds;
3474 if (stmt->n_arg > 0)
3475 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3477 data.domain = isl_set_intersect_params(data.domain,
3478 isl_set_copy(context));
3480 for (i = 0; i < stmt->n_arg; ++i) {
3481 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3482 &access_gist, &data);
3483 if (!stmt->args[i])
3484 goto error;
3487 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
3488 if (!stmt->body)
3489 goto error;
3491 isl_set_free(data.domain);
3493 space = isl_set_get_space(stmt->domain);
3494 if (isl_space_is_wrapping(space))
3495 space = isl_space_domain(isl_space_unwrap(space));
3496 domain = isl_set_universe(space);
3497 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3498 if (stmt->n_arg > 0)
3499 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3500 value_bounds);
3501 stmt->domain = isl_set_gist(stmt->domain, domain);
3502 if (!stmt->domain)
3503 return pet_stmt_free(stmt);
3505 return stmt;
3506 error:
3507 isl_set_free(data.domain);
3508 return pet_stmt_free(stmt);
3511 /* Compute the gist of the extent of the array
3512 * based on the constraints on the parameters specified by "context".
3514 static struct pet_array *array_gist(struct pet_array *array,
3515 __isl_keep isl_set *context)
3517 if (!array)
3518 return NULL;
3520 array->extent = isl_set_gist_params(array->extent,
3521 isl_set_copy(context));
3522 if (!array->extent)
3523 return pet_array_free(array);
3525 return array;
3528 /* Compute the gist of all sets and relations in "scop"
3529 * based on the constraints on the parameters specified by "scop->context"
3530 * and the constraints on the values of nested accesses specified
3531 * by "value_bounds".
3533 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3534 __isl_keep isl_union_map *value_bounds)
3536 int i;
3538 if (!scop)
3539 return NULL;
3541 scop->context = isl_set_coalesce(scop->context);
3542 if (!scop->context)
3543 return pet_scop_free(scop);
3545 for (i = 0; i < scop->n_array; ++i) {
3546 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3547 if (!scop->arrays[i])
3548 return pet_scop_free(scop);
3551 for (i = 0; i < scop->n_stmt; ++i) {
3552 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3553 value_bounds);
3554 if (!scop->stmts[i])
3555 return pet_scop_free(scop);
3558 return scop;
3561 /* Intersect the context of "scop" with "context".
3562 * To ensure that we don't introduce any unnamed parameters in
3563 * the context of "scop", we first remove the unnamed parameters
3564 * from "context".
3566 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3567 __isl_take isl_set *context)
3569 if (!scop)
3570 goto error;
3572 context = set_project_out_unnamed_params(context);
3573 scop->context = isl_set_intersect(scop->context, context);
3574 if (!scop->context)
3575 return pet_scop_free(scop);
3577 return scop;
3578 error:
3579 isl_set_free(context);
3580 return pet_scop_free(scop);
3583 /* Drop the current context of "scop". That is, replace the context
3584 * by a universal set.
3586 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3588 isl_space *space;
3590 if (!scop)
3591 return NULL;
3593 space = isl_set_get_space(scop->context);
3594 isl_set_free(scop->context);
3595 scop->context = isl_set_universe(space);
3596 if (!scop->context)
3597 return pet_scop_free(scop);
3599 return scop;
3602 /* Append "array" to the arrays of "scop".
3604 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3605 struct pet_array *array)
3607 isl_ctx *ctx;
3608 struct pet_array **arrays;
3610 if (!array || !scop)
3611 goto error;
3613 ctx = isl_set_get_ctx(scop->context);
3614 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3615 scop->n_array + 1);
3616 if (!arrays)
3617 goto error;
3618 scop->arrays = arrays;
3619 scop->arrays[scop->n_array] = array;
3620 scop->n_array++;
3622 return scop;
3623 error:
3624 pet_array_free(array);
3625 return pet_scop_free(scop);
3628 /* Create and return an implication on filter values equal to "satisfied"
3629 * with extension "map".
3631 static struct pet_implication *new_implication(__isl_take isl_map *map,
3632 int satisfied)
3634 isl_ctx *ctx;
3635 struct pet_implication *implication;
3637 if (!map)
3638 return NULL;
3639 ctx = isl_map_get_ctx(map);
3640 implication = isl_alloc_type(ctx, struct pet_implication);
3641 if (!implication)
3642 goto error;
3644 implication->extension = map;
3645 implication->satisfied = satisfied;
3647 return implication;
3648 error:
3649 isl_map_free(map);
3650 return NULL;
3653 /* Add an implication on filter values equal to "satisfied"
3654 * with extension "map" to "scop".
3656 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3657 __isl_take isl_map *map, int satisfied)
3659 isl_ctx *ctx;
3660 struct pet_implication *implication;
3661 struct pet_implication **implications;
3663 implication = new_implication(map, satisfied);
3664 if (!scop || !implication)
3665 goto error;
3667 ctx = isl_set_get_ctx(scop->context);
3668 implications = isl_realloc_array(ctx, scop->implications,
3669 struct pet_implication *,
3670 scop->n_implication + 1);
3671 if (!implications)
3672 goto error;
3673 scop->implications = implications;
3674 scop->implications[scop->n_implication] = implication;
3675 scop->n_implication++;
3677 return scop;
3678 error:
3679 pet_implication_free(implication);
3680 return pet_scop_free(scop);