introduce pet_type
[pet.git] / scop.c
blob88a2fa56ab4e19506b34d5136b5c86d5bed15264
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"
40 #include "print.h"
42 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
44 static char *type_str[] = {
45 [pet_expr_access] = "access",
46 [pet_expr_call] = "call",
47 [pet_expr_cast] = "cast",
48 [pet_expr_double] = "double",
49 [pet_expr_unary] = "unary",
50 [pet_expr_binary] = "binary",
51 [pet_expr_ternary] = "ternary"
54 static char *op_str[] = {
55 [pet_op_add_assign] = "+=",
56 [pet_op_sub_assign] = "-=",
57 [pet_op_mul_assign] = "*=",
58 [pet_op_div_assign] = "/=",
59 [pet_op_assign] = "=",
60 [pet_op_add] = "+",
61 [pet_op_sub] = "-",
62 [pet_op_mul] = "*",
63 [pet_op_div] = "/",
64 [pet_op_mod] = "%",
65 [pet_op_eq] = "==",
66 [pet_op_le] = "<=",
67 [pet_op_lt] = "<",
68 [pet_op_gt] = ">",
69 [pet_op_minus] = "-",
70 [pet_op_post_inc] = "++",
71 [pet_op_post_dec] = "--",
72 [pet_op_pre_inc] = "++",
73 [pet_op_pre_dec] = "--",
74 [pet_op_address_of] = "&",
75 [pet_op_kill] = "kill"
78 /* pet_scop with extra information that is used during parsing and printing.
80 * In particular, we keep track of conditions under which we want
81 * to skip the rest of the current loop iteration (skip[pet_skip_now])
82 * and of conditions under which we want to skip subsequent
83 * loop iterations (skip[pet_skip_later]).
85 * The conditions are represented as index expressions defined
86 * over a zero-dimensiona domain. The index expression is either
87 * a boolean affine expression or an access to a variable, which
88 * is assumed to attain values zero and one. The condition holds
89 * if the variable has value one or if the affine expression
90 * has value one (typically for only part of the parameter space).
92 * A missing condition (skip[type] == NULL) means that we don't want
93 * to skip anything.
95 * Additionally, we keep track of the original input file
96 * inside pet_transform_C_source.
98 struct pet_scop_ext {
99 struct pet_scop scop;
101 isl_multi_pw_aff *skip[2];
102 FILE *input;
105 const char *pet_op_str(enum pet_op_type op)
107 return op_str[op];
110 int pet_op_is_inc_dec(enum pet_op_type op)
112 return op == pet_op_post_inc || op == pet_op_post_dec ||
113 op == pet_op_pre_inc || op == pet_op_pre_dec;
116 const char *pet_type_str(enum pet_expr_type type)
118 return type_str[type];
121 enum pet_op_type pet_str_op(const char *str)
123 int i;
125 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
126 if (!strcmp(op_str[i], str))
127 return i;
129 return -1;
132 enum pet_expr_type pet_str_type(const char *str)
134 int i;
136 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
137 if (!strcmp(type_str[i], str))
138 return i;
140 return -1;
143 /* Construct an access pet_expr from an access relation and an index expression.
144 * By default, it is considered to be a read access.
146 struct pet_expr *pet_expr_from_access_and_index( __isl_take isl_map *access,
147 __isl_take isl_multi_pw_aff *index)
149 isl_ctx *ctx = isl_map_get_ctx(access);
150 struct pet_expr *expr;
152 if (!index || !access)
153 goto error;
154 expr = isl_calloc_type(ctx, struct pet_expr);
155 if (!expr)
156 goto error;
158 expr->type = pet_expr_access;
159 expr->acc.access = access;
160 expr->acc.index = index;
161 expr->acc.read = 1;
162 expr->acc.write = 0;
164 return expr;
165 error:
166 isl_map_free(access);
167 isl_multi_pw_aff_free(index);
168 return NULL;
171 /* Construct an access pet_expr from an index expression.
172 * By default, the access is considered to be a read access.
174 struct pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
176 isl_map *access;
178 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
179 return pet_expr_from_access_and_index(access, index);
182 /* Construct an access pet_expr from an index expression and
183 * the depth of the accessed array.
184 * By default, the access is considered to be a read access.
186 * If the number of indices is smaller than the depth of the array,
187 * then we assume that all elements of the remaining dimensions
188 * are accessed.
190 struct pet_expr *pet_expr_from_index_and_depth(
191 __isl_take isl_multi_pw_aff *index, int depth)
193 isl_id *id;
194 isl_map *access;
195 int dim;
197 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
198 if (!access)
199 goto error;
200 dim = isl_map_dim(access, isl_dim_out);
201 if (dim > depth)
202 isl_die(isl_map_get_ctx(access), isl_error_internal,
203 "number of indices greater than depth",
204 access = isl_map_free(access));
205 if (dim == depth)
206 return pet_expr_from_access_and_index(access, index);
208 id = isl_map_get_tuple_id(access, isl_dim_out);
209 access = isl_map_add_dims(access, isl_dim_out, depth - dim);
210 access = isl_map_set_tuple_id(access, isl_dim_out, id);
212 return pet_expr_from_access_and_index(access, index);
213 error:
214 isl_multi_pw_aff_free(index);
215 return NULL;
218 /* Construct a pet_expr that kills the elements specified by
219 * the index expression "index" and the access relation "access".
221 struct pet_expr *pet_expr_kill_from_access_and_index(__isl_take isl_map *access,
222 __isl_take isl_multi_pw_aff *index)
224 isl_ctx *ctx;
225 struct pet_expr *expr;
227 if (!access || !index)
228 goto error;
230 ctx = isl_multi_pw_aff_get_ctx(index);
231 expr = pet_expr_from_access_and_index(access, index);
232 if (!expr)
233 return NULL;
234 expr->acc.read = 0;
235 return pet_expr_new_unary(ctx, pet_op_kill, expr);
236 error:
237 isl_map_free(access);
238 isl_multi_pw_aff_free(index);
239 return NULL;
242 /* Construct a unary pet_expr that performs "op" on "arg".
244 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
245 struct pet_expr *arg)
247 struct pet_expr *expr;
249 if (!arg)
250 goto error;
251 expr = isl_alloc_type(ctx, struct pet_expr);
252 if (!expr)
253 goto error;
255 expr->type = pet_expr_unary;
256 expr->op = op;
257 expr->n_arg = 1;
258 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
259 if (!expr->args)
260 goto error;
261 expr->args[pet_un_arg] = arg;
263 return expr;
264 error:
265 pet_expr_free(arg);
266 return NULL;
269 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
271 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
272 struct pet_expr *lhs, struct pet_expr *rhs)
274 struct pet_expr *expr;
276 if (!lhs || !rhs)
277 goto error;
278 expr = isl_alloc_type(ctx, struct pet_expr);
279 if (!expr)
280 goto error;
282 expr->type = pet_expr_binary;
283 expr->op = op;
284 expr->n_arg = 2;
285 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
286 if (!expr->args)
287 goto error;
288 expr->args[pet_bin_lhs] = lhs;
289 expr->args[pet_bin_rhs] = rhs;
291 return expr;
292 error:
293 pet_expr_free(lhs);
294 pet_expr_free(rhs);
295 return NULL;
298 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
300 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
301 struct pet_expr *lhs, struct pet_expr *rhs)
303 struct pet_expr *expr;
305 if (!cond || !lhs || !rhs)
306 goto error;
307 expr = isl_alloc_type(ctx, struct pet_expr);
308 if (!expr)
309 goto error;
311 expr->type = pet_expr_ternary;
312 expr->n_arg = 3;
313 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
314 if (!expr->args)
315 goto error;
316 expr->args[pet_ter_cond] = cond;
317 expr->args[pet_ter_true] = lhs;
318 expr->args[pet_ter_false] = rhs;
320 return expr;
321 error:
322 pet_expr_free(cond);
323 pet_expr_free(lhs);
324 pet_expr_free(rhs);
325 return NULL;
328 /* Construct a call pet_expr that calls function "name" with "n_arg"
329 * arguments. The caller is responsible for filling in the arguments.
331 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
332 unsigned n_arg)
334 struct pet_expr *expr;
336 expr = isl_alloc_type(ctx, struct pet_expr);
337 if (!expr)
338 return NULL;
340 expr->type = pet_expr_call;
341 expr->n_arg = n_arg;
342 expr->name = strdup(name);
343 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
344 if (!expr->name || !expr->args)
345 return pet_expr_free(expr);
347 return expr;
350 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
352 struct pet_expr *pet_expr_new_cast(isl_ctx *ctx, const char *type_name,
353 struct pet_expr *arg)
355 struct pet_expr *expr;
357 if (!arg)
358 return NULL;
360 expr = isl_alloc_type(ctx, struct pet_expr);
361 if (!expr)
362 goto error;
364 expr->type = pet_expr_cast;
365 expr->n_arg = 1;
366 expr->type_name = strdup(type_name);
367 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
368 if (!expr->type_name || !expr->args)
369 goto error;
371 expr->args[0] = arg;
373 return expr;
374 error:
375 pet_expr_free(arg);
376 pet_expr_free(expr);
377 return NULL;
380 /* Construct a pet_expr that represents the double "d".
382 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
384 struct pet_expr *expr;
386 expr = isl_calloc_type(ctx, struct pet_expr);
387 if (!expr)
388 return NULL;
390 expr->type = pet_expr_double;
391 expr->d.val = val;
392 expr->d.s = strdup(s);
393 if (!expr->d.s)
394 return pet_expr_free(expr);
396 return expr;
399 struct pet_expr *pet_expr_free(struct pet_expr *expr)
401 int i;
403 if (!expr)
404 return NULL;
406 for (i = 0; i < expr->n_arg; ++i)
407 pet_expr_free(expr->args[i]);
408 free(expr->args);
410 switch (expr->type) {
411 case pet_expr_access:
412 isl_id_free(expr->acc.ref_id);
413 isl_map_free(expr->acc.access);
414 isl_multi_pw_aff_free(expr->acc.index);
415 break;
416 case pet_expr_call:
417 free(expr->name);
418 break;
419 case pet_expr_cast:
420 free(expr->type_name);
421 break;
422 case pet_expr_double:
423 free(expr->d.s);
424 break;
425 case pet_expr_unary:
426 case pet_expr_binary:
427 case pet_expr_ternary:
428 break;
431 free(expr);
432 return NULL;
435 static void expr_dump(struct pet_expr *expr, int indent)
437 int i;
439 if (!expr)
440 return;
442 fprintf(stderr, "%*s", indent, "");
444 switch (expr->type) {
445 case pet_expr_double:
446 fprintf(stderr, "%s\n", expr->d.s);
447 break;
448 case pet_expr_access:
449 isl_id_dump(expr->acc.ref_id);
450 fprintf(stderr, "%*s", indent, "");
451 isl_map_dump(expr->acc.access);
452 fprintf(stderr, "%*s", indent, "");
453 isl_multi_pw_aff_dump(expr->acc.index);
454 fprintf(stderr, "%*sread: %d\n", indent + 2,
455 "", expr->acc.read);
456 fprintf(stderr, "%*swrite: %d\n", indent + 2,
457 "", expr->acc.write);
458 for (i = 0; i < expr->n_arg; ++i)
459 expr_dump(expr->args[i], indent + 2);
460 break;
461 case pet_expr_unary:
462 fprintf(stderr, "%s\n", op_str[expr->op]);
463 expr_dump(expr->args[pet_un_arg], indent + 2);
464 break;
465 case pet_expr_binary:
466 fprintf(stderr, "%s\n", op_str[expr->op]);
467 expr_dump(expr->args[pet_bin_lhs], indent + 2);
468 expr_dump(expr->args[pet_bin_rhs], indent + 2);
469 break;
470 case pet_expr_ternary:
471 fprintf(stderr, "?:\n");
472 expr_dump(expr->args[pet_ter_cond], indent + 2);
473 expr_dump(expr->args[pet_ter_true], indent + 2);
474 expr_dump(expr->args[pet_ter_false], indent + 2);
475 break;
476 case pet_expr_call:
477 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
478 for (i = 0; i < expr->n_arg; ++i)
479 expr_dump(expr->args[i], indent + 2);
480 break;
481 case pet_expr_cast:
482 fprintf(stderr, "(%s)\n", expr->type_name);
483 for (i = 0; i < expr->n_arg; ++i)
484 expr_dump(expr->args[i], indent + 2);
485 break;
489 void pet_expr_dump(struct pet_expr *expr)
491 expr_dump(expr, 0);
494 /* Does "expr" represent an access to an unnamed space, i.e.,
495 * does it represent an affine expression?
497 int pet_expr_is_affine(struct pet_expr *expr)
499 int has_id;
501 if (!expr)
502 return -1;
503 if (expr->type != pet_expr_access)
504 return 0;
506 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
507 if (has_id < 0)
508 return -1;
510 return !has_id;
513 /* Return the identifier of the array accessed by "expr".
515 __isl_give isl_id *pet_expr_access_get_id(struct pet_expr *expr)
517 if (!expr)
518 return NULL;
519 if (expr->type != pet_expr_access)
520 return NULL;
521 return isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
524 /* Align the parameters of expr->acc.index and expr->acc.access.
526 struct pet_expr *pet_expr_access_align_params(struct pet_expr *expr)
528 if (!expr)
529 return NULL;
530 if (expr->type != pet_expr_access)
531 return pet_expr_free(expr);
533 expr->acc.access = isl_map_align_params(expr->acc.access,
534 isl_multi_pw_aff_get_space(expr->acc.index));
535 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
536 isl_map_get_space(expr->acc.access));
537 if (!expr->acc.access || !expr->acc.index)
538 return pet_expr_free(expr);
540 return expr;
543 /* Does "expr" represent an access to a scalar, i.e., zero-dimensional array?
545 int pet_expr_is_scalar_access(struct pet_expr *expr)
547 if (!expr)
548 return -1;
549 if (expr->type != pet_expr_access)
550 return 0;
552 return isl_map_dim(expr->acc.access, isl_dim_out) == 0;
555 /* Return 1 if the two pet_exprs are equivalent.
557 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
559 int i;
561 if (!expr1 || !expr2)
562 return 0;
564 if (expr1->type != expr2->type)
565 return 0;
566 if (expr1->n_arg != expr2->n_arg)
567 return 0;
568 for (i = 0; i < expr1->n_arg; ++i)
569 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
570 return 0;
571 switch (expr1->type) {
572 case pet_expr_double:
573 if (strcmp(expr1->d.s, expr2->d.s))
574 return 0;
575 if (expr1->d.val != expr2->d.val)
576 return 0;
577 break;
578 case pet_expr_access:
579 if (expr1->acc.read != expr2->acc.read)
580 return 0;
581 if (expr1->acc.write != expr2->acc.write)
582 return 0;
583 if (expr1->acc.ref_id != expr2->acc.ref_id)
584 return 0;
585 if (!expr1->acc.access || !expr2->acc.access)
586 return 0;
587 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
588 return 0;
589 if (!expr1->acc.index || !expr2->acc.index)
590 return 0;
591 if (!isl_multi_pw_aff_plain_is_equal(expr1->acc.index,
592 expr2->acc.index))
593 return 0;
594 break;
595 case pet_expr_unary:
596 case pet_expr_binary:
597 case pet_expr_ternary:
598 if (expr1->op != expr2->op)
599 return 0;
600 break;
601 case pet_expr_call:
602 if (strcmp(expr1->name, expr2->name))
603 return 0;
604 break;
605 case pet_expr_cast:
606 if (strcmp(expr1->type_name, expr2->type_name))
607 return 0;
608 break;
611 return 1;
614 /* Add extra conditions on the parameters to all access relations in "expr".
616 * The conditions are not added to the index expression. Instead, they
617 * are used to try and simplifty the index expression.
619 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
620 __isl_take isl_set *cond)
622 int i;
624 if (!expr)
625 goto error;
627 for (i = 0; i < expr->n_arg; ++i) {
628 expr->args[i] = pet_expr_restrict(expr->args[i],
629 isl_set_copy(cond));
630 if (!expr->args[i])
631 goto error;
634 if (expr->type == pet_expr_access) {
635 expr->acc.access = isl_map_intersect_params(expr->acc.access,
636 isl_set_copy(cond));
637 expr->acc.index = isl_multi_pw_aff_gist_params(
638 expr->acc.index, isl_set_copy(cond));
639 if (!expr->acc.access || !expr->acc.index)
640 goto error;
643 isl_set_free(cond);
644 return expr;
645 error:
646 isl_set_free(cond);
647 return pet_expr_free(expr);
650 /* Modify all expressions of type pet_expr_access in "expr"
651 * by calling "fn" on them.
653 struct pet_expr *pet_expr_map_access(struct pet_expr *expr,
654 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
655 void *user)
657 int i;
659 if (!expr)
660 return NULL;
662 for (i = 0; i < expr->n_arg; ++i) {
663 expr->args[i] = pet_expr_map_access(expr->args[i], fn, user);
664 if (!expr->args[i])
665 return pet_expr_free(expr);
668 if (expr->type == pet_expr_access)
669 expr = fn(expr, user);
671 return expr;
674 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
676 * Return -1 on error (where fn return a negative value is treated as an error).
677 * Otherwise return 0.
679 int pet_expr_foreach_access_expr(struct pet_expr *expr,
680 int (*fn)(struct pet_expr *expr, void *user), void *user)
682 int i;
684 if (!expr)
685 return -1;
687 for (i = 0; i < expr->n_arg; ++i)
688 if (pet_expr_foreach_access_expr(expr->args[i], fn, user) < 0)
689 return -1;
691 if (expr->type == pet_expr_access)
692 return fn(expr, user);
694 return 0;
697 /* Modify the access relation and index expression
698 * of the given access expression
699 * based on the given iteration space transformation.
700 * In particular, precompose the access relation and index expression
701 * with the update function.
703 * If the access has any arguments then the domain of the access relation
704 * is a wrapped mapping from the iteration space to the space of
705 * argument values. We only need to change the domain of this wrapped
706 * mapping, so we extend the input transformation with an identity mapping
707 * on the space of argument values.
709 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
711 isl_multi_pw_aff *update = user;
712 isl_space *space;
714 update = isl_multi_pw_aff_copy(update);
716 space = isl_map_get_space(expr->acc.access);
717 space = isl_space_domain(space);
718 if (!isl_space_is_wrapping(space))
719 isl_space_free(space);
720 else {
721 isl_multi_pw_aff *id;
722 space = isl_space_unwrap(space);
723 space = isl_space_range(space);
724 space = isl_space_map_from_set(space);
725 id = isl_multi_pw_aff_identity(space);
726 update = isl_multi_pw_aff_product(update, id);
729 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
730 expr->acc.access,
731 isl_multi_pw_aff_copy(update));
732 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
733 expr->acc.index, update);
734 if (!expr->acc.access || !expr->acc.index)
735 return pet_expr_free(expr);
737 return expr;
740 /* Modify all access relations in "expr" by precomposing them with
741 * the given iteration space transformation.
743 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
744 __isl_take isl_multi_pw_aff *update)
746 expr = pet_expr_map_access(expr, &update_domain, update);
747 isl_multi_pw_aff_free(update);
748 return expr;
751 /* Construct a pet_stmt with given line number and statement
752 * number from a pet_expr.
753 * The initial iteration domain is the zero-dimensional universe.
754 * The name of the domain is given by "label" if it is non-NULL.
755 * Otherwise, the name is constructed as S_<id>.
756 * The domains of all access relations are modified to refer
757 * to the statement iteration domain.
759 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
760 __isl_take isl_id *label, int id, struct pet_expr *expr)
762 struct pet_stmt *stmt;
763 isl_space *dim;
764 isl_set *dom;
765 isl_map *sched;
766 isl_multi_pw_aff *add_name;
767 char name[50];
769 if (!expr)
770 goto error;
772 stmt = isl_calloc_type(ctx, struct pet_stmt);
773 if (!stmt)
774 goto error;
776 dim = isl_space_set_alloc(ctx, 0, 0);
777 if (label)
778 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
779 else {
780 snprintf(name, sizeof(name), "S_%d", id);
781 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
783 dom = isl_set_universe(isl_space_copy(dim));
784 sched = isl_map_from_domain(isl_set_copy(dom));
786 dim = isl_space_from_domain(dim);
787 add_name = isl_multi_pw_aff_zero(dim);
788 expr = expr_update_domain(expr, add_name);
790 stmt->line = line;
791 stmt->domain = dom;
792 stmt->schedule = sched;
793 stmt->body = expr;
795 if (!stmt->domain || !stmt->schedule || !stmt->body)
796 return pet_stmt_free(stmt);
798 return stmt;
799 error:
800 isl_id_free(label);
801 pet_expr_free(expr);
802 return NULL;
805 void *pet_stmt_free(struct pet_stmt *stmt)
807 int i;
809 if (!stmt)
810 return NULL;
812 isl_set_free(stmt->domain);
813 isl_map_free(stmt->schedule);
814 pet_expr_free(stmt->body);
816 for (i = 0; i < stmt->n_arg; ++i)
817 pet_expr_free(stmt->args[i]);
818 free(stmt->args);
820 free(stmt);
821 return NULL;
824 static void stmt_dump(struct pet_stmt *stmt, int indent)
826 int i;
828 if (!stmt)
829 return;
831 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
832 fprintf(stderr, "%*s", indent, "");
833 isl_set_dump(stmt->domain);
834 fprintf(stderr, "%*s", indent, "");
835 isl_map_dump(stmt->schedule);
836 expr_dump(stmt->body, indent);
837 for (i = 0; i < stmt->n_arg; ++i)
838 expr_dump(stmt->args[i], indent + 2);
841 void pet_stmt_dump(struct pet_stmt *stmt)
843 stmt_dump(stmt, 0);
846 /* Allocate a new pet_type with the given "name" and "definition".
848 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
849 const char *definition)
851 struct pet_type *type;
853 type = isl_alloc_type(ctx, struct pet_type);
854 if (!type)
855 return NULL;
857 type->name = strdup(name);
858 type->definition = strdup(definition);
860 if (!type->name || !type->definition)
861 return pet_type_free(type);
863 return type;
866 /* Free "type" and return NULL.
868 struct pet_type *pet_type_free(struct pet_type *type)
870 if (!type)
871 return NULL;
873 free(type->name);
874 free(type->definition);
876 free(type);
877 return NULL;
880 struct pet_array *pet_array_free(struct pet_array *array)
882 if (!array)
883 return NULL;
885 isl_set_free(array->context);
886 isl_set_free(array->extent);
887 isl_set_free(array->value_bounds);
888 free(array->element_type);
890 free(array);
891 return NULL;
894 void pet_array_dump(struct pet_array *array)
896 if (!array)
897 return;
899 isl_set_dump(array->context);
900 isl_set_dump(array->extent);
901 isl_set_dump(array->value_bounds);
902 fprintf(stderr, "%s %s\n", array->element_type,
903 array->live_out ? "live-out" : "");
906 /* Alloc a pet_scop structure, with extra room for information that
907 * is only used during parsing.
909 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
911 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
914 /* Construct a pet_scop with room for n statements.
916 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
918 isl_space *space;
919 struct pet_scop *scop;
921 scop = pet_scop_alloc(ctx);
922 if (!scop)
923 return NULL;
925 space = isl_space_params_alloc(ctx, 0);
926 scop->context = isl_set_universe(isl_space_copy(space));
927 scop->context_value = isl_set_universe(space);
928 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
929 if (!scop->context || !scop->stmts)
930 return pet_scop_free(scop);
932 scop->n_stmt = n;
934 return scop;
937 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
939 return scop_alloc(ctx, 0);
942 /* Update "context" with respect to the valid parameter values for "access".
944 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
945 __isl_take isl_set *context)
947 context = isl_set_intersect(context,
948 isl_map_params(isl_map_copy(access)));
949 return context;
952 /* Update "context" with respect to the valid parameter values for "expr".
954 * If "expr" represents a ternary operator, then a parameter value
955 * needs to be valid for the condition and for at least one of the
956 * remaining two arguments.
957 * If the condition is an affine expression, then we can be a bit more specific.
958 * The parameter then has to be valid for the second argument for
959 * non-zero accesses and valid for the third argument for zero accesses.
961 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
962 __isl_take isl_set *context)
964 int i;
966 if (expr->type == pet_expr_ternary) {
967 int is_aff;
968 isl_set *context1, *context2;
970 is_aff = pet_expr_is_affine(expr->args[0]);
971 if (is_aff < 0)
972 goto error;
974 context = expr_extract_context(expr->args[0], context);
975 context1 = expr_extract_context(expr->args[1],
976 isl_set_copy(context));
977 context2 = expr_extract_context(expr->args[2], context);
979 if (is_aff) {
980 isl_map *access;
981 isl_set *zero_set;
983 access = isl_map_copy(expr->args[0]->acc.access);
984 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
985 zero_set = isl_map_params(access);
986 context1 = isl_set_subtract(context1,
987 isl_set_copy(zero_set));
988 context2 = isl_set_intersect(context2, zero_set);
991 context = isl_set_union(context1, context2);
992 context = isl_set_coalesce(context);
994 return context;
997 for (i = 0; i < expr->n_arg; ++i)
998 context = expr_extract_context(expr->args[i], context);
1000 if (expr->type == pet_expr_access)
1001 context = access_extract_context(expr->acc.access, context);
1003 return context;
1004 error:
1005 isl_set_free(context);
1006 return NULL;
1009 /* Update "context" with respect to the valid parameter values for "stmt".
1011 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
1012 __isl_take isl_set *context)
1014 int i;
1016 for (i = 0; i < stmt->n_arg; ++i)
1017 context = expr_extract_context(stmt->args[i], context);
1019 context = expr_extract_context(stmt->body, context);
1021 return context;
1024 /* Construct a pet_scop that contains the given pet_stmt.
1026 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
1028 struct pet_scop *scop;
1030 if (!stmt)
1031 return NULL;
1033 scop = scop_alloc(ctx, 1);
1034 if (!scop)
1035 goto error;
1037 scop->context = stmt_extract_context(stmt, scop->context);
1038 if (!scop->context)
1039 goto error;
1041 scop->stmts[0] = stmt;
1043 return scop;
1044 error:
1045 pet_stmt_free(stmt);
1046 pet_scop_free(scop);
1047 return NULL;
1050 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
1051 * does it represent an affine expression?
1053 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
1055 int has_id;
1057 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
1058 if (has_id < 0)
1059 return -1;
1061 return !has_id;
1064 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
1066 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
1067 __isl_take isl_set *dom)
1069 isl_pw_aff *pa;
1070 pa = isl_set_indicator_function(set);
1071 pa = isl_pw_aff_intersect_domain(pa, dom);
1072 return pa;
1075 /* Return "lhs || rhs", defined on the shared definition domain.
1077 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
1078 __isl_take isl_pw_aff *rhs)
1080 isl_set *cond;
1081 isl_set *dom;
1083 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
1084 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
1085 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
1086 isl_pw_aff_non_zero_set(rhs));
1087 cond = isl_set_coalesce(cond);
1088 return indicator_function(cond, dom);
1091 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
1092 * ext may be equal to either ext1 or ext2.
1094 * The two skips that need to be combined are assumed to be affine expressions.
1096 * We need to skip in ext if we need to skip in either ext1 or ext2.
1097 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
1099 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
1100 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
1101 enum pet_skip type)
1103 isl_pw_aff *skip, *skip1, *skip2;
1105 if (!ext)
1106 return NULL;
1107 if (!ext1->skip[type] && !ext2->skip[type])
1108 return ext;
1109 if (!ext1->skip[type]) {
1110 if (ext == ext2)
1111 return ext;
1112 ext->skip[type] = ext2->skip[type];
1113 ext2->skip[type] = NULL;
1114 return ext;
1116 if (!ext2->skip[type]) {
1117 if (ext == ext1)
1118 return ext;
1119 ext->skip[type] = ext1->skip[type];
1120 ext1->skip[type] = NULL;
1121 return ext;
1124 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
1125 !multi_pw_aff_is_affine(ext2->skip[type]))
1126 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
1127 isl_error_internal, "can only combine affine skips",
1128 goto error);
1130 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
1131 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
1132 skip = pw_aff_or(skip1, skip2);
1133 isl_multi_pw_aff_free(ext1->skip[type]);
1134 ext1->skip[type] = NULL;
1135 isl_multi_pw_aff_free(ext2->skip[type]);
1136 ext2->skip[type] = NULL;
1137 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1138 if (!ext->skip[type])
1139 goto error;
1141 return ext;
1142 error:
1143 pet_scop_free(&ext->scop);
1144 return NULL;
1147 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
1148 * where type takes on the values pet_skip_now and pet_skip_later.
1149 * scop may be equal to either scop1 or scop2.
1151 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
1152 struct pet_scop *scop1, struct pet_scop *scop2)
1154 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1155 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
1156 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
1158 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
1159 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
1160 return &ext->scop;
1163 /* Update scop->start and scop->end to include the region from "start"
1164 * to "end". In particular, if scop->end == 0, then "scop" does not
1165 * have any offset information yet and we simply take the information
1166 * from "start" and "end". Otherwise, we update the fields if the
1167 * region from "start" to "end" is not already included.
1169 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
1170 unsigned start, unsigned end)
1172 if (!scop)
1173 return NULL;
1174 if (scop->end == 0) {
1175 scop->start = start;
1176 scop->end = end;
1177 } else {
1178 if (start < scop->start)
1179 scop->start = start;
1180 if (end > scop->end)
1181 scop->end = end;
1184 return scop;
1187 /* Does "implication" appear in the list of implications of "scop"?
1189 static int is_known_implication(struct pet_scop *scop,
1190 struct pet_implication *implication)
1192 int i;
1194 for (i = 0; i < scop->n_implication; ++i) {
1195 struct pet_implication *pi = scop->implications[i];
1196 int equal;
1198 if (pi->satisfied != implication->satisfied)
1199 continue;
1200 equal = isl_map_is_equal(pi->extension, implication->extension);
1201 if (equal < 0)
1202 return -1;
1203 if (equal)
1204 return 1;
1207 return 0;
1210 /* Store the concatenation of the impliciations of "scop1" and "scop2"
1211 * in "scop", removing duplicates (i.e., implications in "scop2" that
1212 * already appear in "scop1").
1214 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
1215 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
1217 int i, j;
1219 if (!scop)
1220 return NULL;
1222 if (scop2->n_implication == 0) {
1223 scop->n_implication = scop1->n_implication;
1224 scop->implications = scop1->implications;
1225 scop1->n_implication = 0;
1226 scop1->implications = NULL;
1227 return scop;
1230 if (scop1->n_implication == 0) {
1231 scop->n_implication = scop2->n_implication;
1232 scop->implications = scop2->implications;
1233 scop2->n_implication = 0;
1234 scop2->implications = NULL;
1235 return scop;
1238 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1239 scop1->n_implication + scop2->n_implication);
1240 if (!scop->implications)
1241 return pet_scop_free(scop);
1243 for (i = 0; i < scop1->n_implication; ++i) {
1244 scop->implications[i] = scop1->implications[i];
1245 scop1->implications[i] = NULL;
1248 scop->n_implication = scop1->n_implication;
1249 j = scop1->n_implication;
1250 for (i = 0; i < scop2->n_implication; ++i) {
1251 int known;
1253 known = is_known_implication(scop, scop2->implications[i]);
1254 if (known < 0)
1255 return pet_scop_free(scop);
1256 if (known)
1257 continue;
1258 scop->implications[j++] = scop2->implications[i];
1259 scop2->implications[i] = NULL;
1261 scop->n_implication = j;
1263 return scop;
1266 /* Combine the offset information of "scop1" and "scop2" into "scop".
1268 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
1269 struct pet_scop *scop1, struct pet_scop *scop2)
1271 if (scop1->end)
1272 scop = pet_scop_update_start_end(scop,
1273 scop1->start, scop1->end);
1274 if (scop2->end)
1275 scop = pet_scop_update_start_end(scop,
1276 scop2->start, scop2->end);
1277 return scop;
1280 /* Construct a pet_scop that contains the offset information,
1281 * arrays, statements and skip information in "scop1" and "scop2".
1283 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1284 struct pet_scop *scop2)
1286 int i;
1287 struct pet_scop *scop = NULL;
1289 if (!scop1 || !scop2)
1290 goto error;
1292 if (scop1->n_stmt == 0) {
1293 scop2 = scop_combine_skips(scop2, scop1, scop2);
1294 pet_scop_free(scop1);
1295 return scop2;
1298 if (scop2->n_stmt == 0) {
1299 scop1 = scop_combine_skips(scop1, scop1, scop2);
1300 pet_scop_free(scop2);
1301 return scop1;
1304 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1305 if (!scop)
1306 goto error;
1308 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1309 scop1->n_array + scop2->n_array);
1310 if (!scop->arrays)
1311 goto error;
1312 scop->n_array = scop1->n_array + scop2->n_array;
1314 for (i = 0; i < scop1->n_stmt; ++i) {
1315 scop->stmts[i] = scop1->stmts[i];
1316 scop1->stmts[i] = NULL;
1319 for (i = 0; i < scop2->n_stmt; ++i) {
1320 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1321 scop2->stmts[i] = NULL;
1324 for (i = 0; i < scop1->n_array; ++i) {
1325 scop->arrays[i] = scop1->arrays[i];
1326 scop1->arrays[i] = NULL;
1329 for (i = 0; i < scop2->n_array; ++i) {
1330 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1331 scop2->arrays[i] = NULL;
1334 scop = scop_collect_implications(ctx, scop, scop1, scop2);
1335 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1336 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1337 scop = scop_combine_skips(scop, scop1, scop2);
1338 scop = scop_combine_start_end(scop, scop1, scop2);
1340 pet_scop_free(scop1);
1341 pet_scop_free(scop2);
1342 return scop;
1343 error:
1344 pet_scop_free(scop1);
1345 pet_scop_free(scop2);
1346 pet_scop_free(scop);
1347 return NULL;
1350 /* Apply the skip condition "skip" to "scop".
1351 * That is, make sure "scop" is not executed when the condition holds.
1353 * If "skip" is an affine expression, we add the conditions under
1354 * which the expression is zero to the iteration domains.
1355 * Otherwise, we add a filter on the variable attaining the value zero.
1357 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1358 __isl_take isl_multi_pw_aff *skip)
1360 isl_set *zero;
1361 isl_pw_aff *pa;
1362 int is_aff;
1364 if (!scop || !skip)
1365 goto error;
1367 is_aff = multi_pw_aff_is_affine(skip);
1368 if (is_aff < 0)
1369 goto error;
1371 if (!is_aff)
1372 return pet_scop_filter(scop, skip, 0);
1374 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1375 isl_multi_pw_aff_free(skip);
1376 zero = isl_set_params(isl_pw_aff_zero_set(pa));
1377 scop = pet_scop_restrict(scop, zero);
1379 return scop;
1380 error:
1381 isl_multi_pw_aff_free(skip);
1382 return pet_scop_free(scop);
1385 /* Construct a pet_scop that contains the arrays, statements and
1386 * skip information in "scop1" and "scop2", where the two scops
1387 * are executed "in sequence". That is, breaks and continues
1388 * in scop1 have an effect on scop2.
1390 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1391 struct pet_scop *scop2)
1393 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1394 scop2 = restrict_skip(scop2,
1395 pet_scop_get_skip(scop1, pet_skip_now));
1396 return pet_scop_add(ctx, scop1, scop2);
1399 /* Construct a pet_scop that contains the arrays, statements and
1400 * skip information in "scop1" and "scop2", where the two scops
1401 * are executed "in parallel". That is, any break or continue
1402 * in scop1 has no effect on scop2.
1404 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1405 struct pet_scop *scop2)
1407 return pet_scop_add(ctx, scop1, scop2);
1410 void *pet_implication_free(struct pet_implication *implication)
1412 int i;
1414 if (!implication)
1415 return NULL;
1417 isl_map_free(implication->extension);
1419 free(implication);
1420 return NULL;
1423 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1425 int i;
1426 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1428 if (!scop)
1429 return NULL;
1430 isl_set_free(scop->context);
1431 isl_set_free(scop->context_value);
1432 if (scop->types)
1433 for (i = 0; i < scop->n_type; ++i)
1434 pet_type_free(scop->types[i]);
1435 free(scop->types);
1436 if (scop->arrays)
1437 for (i = 0; i < scop->n_array; ++i)
1438 pet_array_free(scop->arrays[i]);
1439 free(scop->arrays);
1440 if (scop->stmts)
1441 for (i = 0; i < scop->n_stmt; ++i)
1442 pet_stmt_free(scop->stmts[i]);
1443 free(scop->stmts);
1444 if (scop->implications)
1445 for (i = 0; i < scop->n_implication; ++i)
1446 pet_implication_free(scop->implications[i]);
1447 free(scop->implications);
1448 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1449 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1450 free(scop);
1451 return NULL;
1454 void pet_type_dump(struct pet_type *type)
1456 if (!type)
1457 return;
1459 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1462 void pet_implication_dump(struct pet_implication *implication)
1464 if (!implication)
1465 return;
1467 fprintf(stderr, "%d\n", implication->satisfied);
1468 isl_map_dump(implication->extension);
1471 void pet_scop_dump(struct pet_scop *scop)
1473 int i;
1474 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1476 if (!scop)
1477 return;
1479 isl_set_dump(scop->context);
1480 isl_set_dump(scop->context_value);
1481 for (i = 0; i < scop->n_type; ++i)
1482 pet_type_dump(scop->types[i]);
1483 for (i = 0; i < scop->n_array; ++i)
1484 pet_array_dump(scop->arrays[i]);
1485 for (i = 0; i < scop->n_stmt; ++i)
1486 pet_stmt_dump(scop->stmts[i]);
1487 for (i = 0; i < scop->n_implication; ++i)
1488 pet_implication_dump(scop->implications[i]);
1490 if (ext->skip[0]) {
1491 fprintf(stderr, "skip\n");
1492 isl_multi_pw_aff_dump(ext->skip[0]);
1493 isl_multi_pw_aff_dump(ext->skip[1]);
1497 /* Return 1 if the two pet_arrays are equivalent.
1499 * We don't compare element_size as this may be target dependent.
1501 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1503 if (!array1 || !array2)
1504 return 0;
1506 if (!isl_set_is_equal(array1->context, array2->context))
1507 return 0;
1508 if (!isl_set_is_equal(array1->extent, array2->extent))
1509 return 0;
1510 if (!!array1->value_bounds != !!array2->value_bounds)
1511 return 0;
1512 if (array1->value_bounds &&
1513 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1514 return 0;
1515 if (strcmp(array1->element_type, array2->element_type))
1516 return 0;
1517 if (array1->live_out != array2->live_out)
1518 return 0;
1519 if (array1->uniquely_defined != array2->uniquely_defined)
1520 return 0;
1521 if (array1->declared != array2->declared)
1522 return 0;
1523 if (array1->exposed != array2->exposed)
1524 return 0;
1526 return 1;
1529 /* Return 1 if the two pet_stmts are equivalent.
1531 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1533 int i;
1535 if (!stmt1 || !stmt2)
1536 return 0;
1538 if (stmt1->line != stmt2->line)
1539 return 0;
1540 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1541 return 0;
1542 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1543 return 0;
1544 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1545 return 0;
1546 if (stmt1->n_arg != stmt2->n_arg)
1547 return 0;
1548 for (i = 0; i < stmt1->n_arg; ++i) {
1549 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1550 return 0;
1553 return 1;
1556 /* Return 1 if the two pet_types are equivalent.
1558 * We only compare the names of the types since the exact representation
1559 * of the definition may depend on the version of clang being used.
1561 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1563 if (!type1 || !type2)
1564 return 0;
1566 if (strcmp(type1->name, type2->name))
1567 return 0;
1569 return 1;
1572 /* Return 1 if the two pet_implications are equivalent.
1574 int pet_implication_is_equal(struct pet_implication *implication1,
1575 struct pet_implication *implication2)
1577 if (!implication1 || !implication2)
1578 return 0;
1580 if (implication1->satisfied != implication2->satisfied)
1581 return 0;
1582 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1583 return 0;
1585 return 1;
1588 /* Return 1 if the two pet_scops are equivalent.
1590 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1592 int i;
1594 if (!scop1 || !scop2)
1595 return 0;
1597 if (!isl_set_is_equal(scop1->context, scop2->context))
1598 return 0;
1599 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1600 return 0;
1602 if (scop1->n_type != scop2->n_type)
1603 return 0;
1604 for (i = 0; i < scop1->n_type; ++i)
1605 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1606 return 0;
1608 if (scop1->n_array != scop2->n_array)
1609 return 0;
1610 for (i = 0; i < scop1->n_array; ++i)
1611 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1612 return 0;
1614 if (scop1->n_stmt != scop2->n_stmt)
1615 return 0;
1616 for (i = 0; i < scop1->n_stmt; ++i)
1617 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1618 return 0;
1620 if (scop1->n_implication != scop2->n_implication)
1621 return 0;
1622 for (i = 0; i < scop1->n_implication; ++i)
1623 if (!pet_implication_is_equal(scop1->implications[i],
1624 scop2->implications[i]))
1625 return 0;
1627 return 1;
1630 /* Prefix the schedule of "stmt" with an extra dimension with constant
1631 * value "pos".
1633 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1635 if (!stmt)
1636 return NULL;
1638 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1639 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1640 if (!stmt->schedule)
1641 return pet_stmt_free(stmt);
1643 return stmt;
1646 /* Prefix the schedules of all statements in "scop" with an extra
1647 * dimension with constant value "pos".
1649 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1651 int i;
1653 if (!scop)
1654 return NULL;
1656 for (i = 0; i < scop->n_stmt; ++i) {
1657 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1658 if (!scop->stmts[i])
1659 return pet_scop_free(scop);
1662 return scop;
1665 /* Given a set with a parameter at "param_pos" that refers to the
1666 * iterator, "move" the iterator to the first set dimension.
1667 * That is, essentially equate the parameter to the first set dimension
1668 * and then project it out.
1670 * The first set dimension may however refer to a virtual iterator,
1671 * while the parameter refers to the "real" iterator.
1672 * We therefore need to take into account the affine expression "iv_map", which
1673 * expresses the real iterator in terms of the virtual iterator.
1674 * In particular, we equate the set dimension to the input of the map
1675 * and the parameter to the output of the map and then project out
1676 * everything we don't need anymore.
1678 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1679 int param_pos, __isl_take isl_aff *iv_map)
1681 isl_map *map, *map2;
1682 map = isl_map_from_domain(set);
1683 map = isl_map_add_dims(map, isl_dim_out, 1);
1684 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1685 map2 = isl_map_from_aff(iv_map);
1686 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1687 map = isl_map_apply_range(map, map2);
1688 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1689 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1690 return isl_map_domain(map);
1693 /* Data used in embed_access.
1694 * extend adds an iterator to the iteration domain (through precomposition).
1695 * iv_map expresses the real iterator in terms of the virtual iterator
1696 * var_id represents the induction variable of the corresponding loop
1698 struct pet_embed_access {
1699 isl_multi_pw_aff *extend;
1700 isl_aff *iv_map;
1701 isl_id *var_id;
1704 /* Given an index expression, return an expression for the outer iterator.
1706 static __isl_give isl_aff *index_outer_iterator(
1707 __isl_take isl_multi_pw_aff *index)
1709 isl_space *space;
1710 isl_local_space *ls;
1712 space = isl_multi_pw_aff_get_domain_space(index);
1713 isl_multi_pw_aff_free(index);
1715 ls = isl_local_space_from_space(space);
1716 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1719 /* Replace an index expression that references the new (outer) iterator variable
1720 * by one that references the corresponding (real) iterator.
1722 * The input index expression is of the form
1724 * { S[i',...] -> i[] }
1726 * where i' refers to the virtual iterator.
1728 * iv_map is of the form
1730 * { [i'] -> [i] }
1732 * Return the index expression
1734 * { S[i',...] -> [i] }
1736 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1737 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1739 isl_space *space;
1740 isl_aff *aff;
1742 aff = index_outer_iterator(index);
1743 space = isl_aff_get_space(aff);
1744 iv_map = isl_aff_align_params(iv_map, space);
1745 aff = isl_aff_pullback_aff(iv_map, aff);
1747 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1750 /* Given an index expression "index" that refers to the (real) iterator
1751 * through the parameter at position "pos", plug in "iv_map", expressing
1752 * the real iterator in terms of the virtual (outer) iterator.
1754 * In particular, the index expression is of the form
1756 * [..., i, ...] -> { S[i',...] -> ... i ... }
1758 * where i refers to the real iterator and i' refers to the virtual iterator.
1760 * iv_map is of the form
1762 * { [i'] -> [i] }
1764 * Return the index expression
1766 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1769 * We first move the parameter to the input
1771 * [..., ...] -> { [i, i',...] -> ... i ... }
1773 * and construct
1775 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1777 * and then combine the two to obtain the desired result.
1779 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1780 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1782 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1783 isl_multi_aff *ma;
1785 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1786 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1787 isl_dim_param, pos, 1);
1789 space = isl_space_map_from_set(space);
1790 ma = isl_multi_aff_identity(isl_space_copy(space));
1791 iv_map = isl_aff_align_params(iv_map, space);
1792 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1793 ma = isl_multi_aff_flat_range_product(
1794 isl_multi_aff_from_aff(iv_map), ma);
1795 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1797 return index;
1800 /* Embed the given index expression in an extra outer loop.
1801 * The domain of the index expression has already been updated.
1803 * If the access refers to the induction variable, then it is
1804 * turned into an access to the set of integers with index (and value)
1805 * equal to the induction variable.
1807 * If the accessed array is a virtual array (with user
1808 * pointer equal to NULL), as created by create_test_index,
1809 * then it is extended along with the domain of the index expression.
1811 static __isl_give isl_multi_pw_aff *embed_index_expression(
1812 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1814 isl_id *array_id = NULL;
1815 int pos;
1817 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1818 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1819 if (array_id == data->var_id) {
1820 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1821 } else if (array_id && !isl_id_get_user(array_id)) {
1822 isl_aff *aff;
1823 isl_multi_pw_aff *mpa;
1825 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1826 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1827 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1828 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1829 isl_id_copy(array_id));
1831 isl_id_free(array_id);
1833 pos = isl_multi_pw_aff_find_dim_by_id(index,
1834 isl_dim_param, data->var_id);
1835 if (pos >= 0)
1836 index = index_internalize_iv(index, pos,
1837 isl_aff_copy(data->iv_map));
1838 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1839 isl_id_copy(data->var_id));
1841 return index;
1844 /* Embed the given access relation in an extra outer loop.
1845 * The domain of the access relation has already been updated.
1847 * If the access refers to the induction variable, then it is
1848 * turned into an access to the set of integers with index (and value)
1849 * equal to the induction variable.
1851 * If the induction variable appears in the constraints (as a parameter),
1852 * then the parameter is equated to the newly introduced iteration
1853 * domain dimension and subsequently projected out.
1855 * Similarly, if the accessed array is a virtual array (with user
1856 * pointer equal to NULL), as created by create_test_index,
1857 * then it is extended along with the domain of the access.
1859 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
1860 struct pet_embed_access *data)
1862 isl_id *array_id = NULL;
1863 int pos;
1865 if (isl_map_has_tuple_id(access, isl_dim_out))
1866 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1867 if (array_id == data->var_id ||
1868 (array_id && !isl_id_get_user(array_id))) {
1869 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1870 access = isl_map_equate(access,
1871 isl_dim_in, 0, isl_dim_out, 0);
1872 if (array_id == data->var_id)
1873 access = isl_map_apply_range(access,
1874 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1875 else
1876 access = isl_map_set_tuple_id(access, isl_dim_out,
1877 isl_id_copy(array_id));
1879 isl_id_free(array_id);
1881 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1882 if (pos >= 0) {
1883 isl_set *set = isl_map_wrap(access);
1884 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1885 access = isl_set_unwrap(set);
1887 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1888 isl_id_copy(data->var_id));
1890 return access;
1893 /* Given an access expression, embed the associated access relation and
1894 * index expression in an extra outer loop.
1896 * We first update the domains to insert the extra dimension and
1897 * then update the access relation and index expression to take
1898 * into account the mapping "iv_map" from virtual iterator
1899 * to real iterator.
1901 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
1903 int dim;
1904 struct pet_embed_access *data = user;
1906 expr = update_domain(expr, data->extend);
1907 if (!expr)
1908 return NULL;
1910 expr->acc.access = embed_access_relation(expr->acc.access, data);
1911 expr->acc.index = embed_index_expression(expr->acc.index, data);
1912 if (!expr->acc.access || !expr->acc.index)
1913 return pet_expr_free(expr);
1915 return expr;
1918 /* Embed all access subexpressions of "expr" in an extra loop.
1919 * "extend" inserts an outer loop iterator in the iteration domains
1920 * (through precomposition).
1921 * "iv_map" expresses the real iterator in terms of the virtual iterator
1922 * "var_id" represents the induction variable.
1924 static struct pet_expr *expr_embed(struct pet_expr *expr,
1925 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
1926 __isl_keep isl_id *var_id)
1928 struct pet_embed_access data =
1929 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1931 expr = pet_expr_map_access(expr, &embed_access, &data);
1932 isl_aff_free(iv_map);
1933 isl_multi_pw_aff_free(extend);
1934 return expr;
1937 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1938 * "dom" and schedule "sched". "var_id" represents the induction variable
1939 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1940 * That is, it expresses the iterator that some of the parameters in "stmt"
1941 * may refer to in terms of the iterator used in "dom" and
1942 * the domain of "sched".
1944 * The iteration domain and schedule of the statement are updated
1945 * according to the iteration domain and schedule of the new loop.
1946 * If stmt->domain is a wrapped map, then the iteration domain
1947 * is the domain of this map, so we need to be careful to adjust
1948 * this domain.
1950 * If the induction variable appears in the constraints (as a parameter)
1951 * of the current iteration domain or the schedule of the statement,
1952 * then the parameter is equated to the newly introduced iteration
1953 * domain dimension and subsequently projected out.
1955 * Finally, all access relations are updated based on the extra loop.
1957 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1958 __isl_take isl_set *dom, __isl_take isl_map *sched,
1959 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1961 int i;
1962 int pos;
1963 isl_id *stmt_id;
1964 isl_space *dim;
1965 isl_multi_pw_aff *extend;
1967 if (!stmt)
1968 goto error;
1970 if (isl_set_is_wrapping(stmt->domain)) {
1971 isl_map *map;
1972 isl_map *ext;
1973 isl_space *ran_dim;
1975 map = isl_set_unwrap(stmt->domain);
1976 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1977 ran_dim = isl_space_range(isl_map_get_space(map));
1978 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1979 isl_set_universe(ran_dim));
1980 map = isl_map_flat_domain_product(ext, map);
1981 map = isl_map_set_tuple_id(map, isl_dim_in,
1982 isl_id_copy(stmt_id));
1983 dim = isl_space_domain(isl_map_get_space(map));
1984 stmt->domain = isl_map_wrap(map);
1985 } else {
1986 stmt_id = isl_set_get_tuple_id(stmt->domain);
1987 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1988 stmt->domain);
1989 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1990 isl_id_copy(stmt_id));
1991 dim = isl_set_get_space(stmt->domain);
1994 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1995 if (pos >= 0)
1996 stmt->domain = internalize_iv(stmt->domain, pos,
1997 isl_aff_copy(iv_map));
1999 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
2000 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
2001 isl_dim_in, stmt_id);
2003 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
2004 if (pos >= 0) {
2005 isl_set *set = isl_map_wrap(stmt->schedule);
2006 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
2007 stmt->schedule = isl_set_unwrap(set);
2010 dim = isl_space_map_from_set(dim);
2011 extend = isl_multi_pw_aff_identity(dim);
2012 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
2013 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
2014 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
2015 for (i = 0; i < stmt->n_arg; ++i)
2016 stmt->args[i] = expr_embed(stmt->args[i],
2017 isl_multi_pw_aff_copy(extend),
2018 isl_aff_copy(iv_map), var_id);
2019 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
2021 isl_set_free(dom);
2022 isl_id_free(var_id);
2024 for (i = 0; i < stmt->n_arg; ++i)
2025 if (!stmt->args[i])
2026 return pet_stmt_free(stmt);
2027 if (!stmt->domain || !stmt->schedule || !stmt->body)
2028 return pet_stmt_free(stmt);
2029 return stmt;
2030 error:
2031 isl_set_free(dom);
2032 isl_map_free(sched);
2033 isl_aff_free(iv_map);
2034 isl_id_free(var_id);
2035 return NULL;
2038 /* Embed the given pet_array in an extra outer loop with iteration domain
2039 * "dom".
2040 * This embedding only has an effect on virtual arrays (those with
2041 * user pointer equal to NULL), which need to be extended along with
2042 * the iteration domain.
2044 static struct pet_array *pet_array_embed(struct pet_array *array,
2045 __isl_take isl_set *dom)
2047 isl_id *array_id = NULL;
2049 if (!array)
2050 goto error;
2052 if (isl_set_has_tuple_id(array->extent))
2053 array_id = isl_set_get_tuple_id(array->extent);
2055 if (array_id && !isl_id_get_user(array_id)) {
2056 array->extent = isl_set_flat_product(dom, array->extent);
2057 array->extent = isl_set_set_tuple_id(array->extent, array_id);
2058 if (!array->extent)
2059 return pet_array_free(array);
2060 } else {
2061 isl_set_free(dom);
2062 isl_id_free(array_id);
2065 return array;
2066 error:
2067 isl_set_free(dom);
2068 return NULL;
2071 /* Project out all unnamed parameters from "set" and return the result.
2073 static __isl_give isl_set *set_project_out_unnamed_params(
2074 __isl_take isl_set *set)
2076 int i, n;
2078 n = isl_set_dim(set, isl_dim_param);
2079 for (i = n - 1; i >= 0; --i) {
2080 if (isl_set_has_dim_name(set, isl_dim_param, i))
2081 continue;
2082 set = isl_set_project_out(set, isl_dim_param, i, 1);
2085 return set;
2088 /* Update the context with respect to an embedding into a loop
2089 * with iteration domain "dom" and induction variable "id".
2090 * "iv_map" expresses the real iterator (parameter "id") in terms
2091 * of a possibly virtual iterator (used in "dom").
2093 * If the current context is independent of "id", we don't need
2094 * to do anything.
2095 * Otherwise, a parameter value is invalid for the embedding if
2096 * any of the corresponding iterator values is invalid.
2097 * That is, a parameter value is valid only if all the corresponding
2098 * iterator values are valid.
2099 * We therefore compute the set of parameters
2101 * forall i in dom : valid (i)
2103 * or
2105 * not exists i in dom : not valid(i)
2107 * i.e.,
2109 * not exists i in dom \ valid(i)
2111 * Before we subtract valid(i) from dom, we first need to substitute
2112 * the real iterator for the virtual iterator.
2114 * If there are any unnamed parameters in "dom", then we consider
2115 * a parameter value to be valid if it is valid for any value of those
2116 * unnamed parameters. They are therefore projected out at the end.
2118 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
2119 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
2120 __isl_keep isl_id *id)
2122 int pos;
2123 isl_multi_aff *ma;
2125 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
2126 if (pos < 0)
2127 return context;
2129 context = isl_set_from_params(context);
2130 context = isl_set_add_dims(context, isl_dim_set, 1);
2131 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
2132 context = isl_set_project_out(context, isl_dim_param, pos, 1);
2133 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
2134 context = isl_set_preimage_multi_aff(context, ma);
2135 context = isl_set_subtract(isl_set_copy(dom), context);
2136 context = isl_set_params(context);
2137 context = isl_set_complement(context);
2138 context = set_project_out_unnamed_params(context);
2139 return context;
2142 /* Update the implication with respect to an embedding into a loop
2143 * with iteration domain "dom".
2145 * Since embed_access extends virtual arrays along with the domain
2146 * of the access, we need to do the same with domain and range
2147 * of the implication. Since the original implication is only valid
2148 * within a given iteration of the loop, the extended implication
2149 * maps the extra array dimension corresponding to the extra loop
2150 * to itself.
2152 static struct pet_implication *pet_implication_embed(
2153 struct pet_implication *implication, __isl_take isl_set *dom)
2155 isl_id *id;
2156 isl_map *map;
2158 if (!implication)
2159 goto error;
2161 map = isl_set_identity(dom);
2162 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
2163 map = isl_map_flat_product(map, implication->extension);
2164 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
2165 map = isl_map_set_tuple_id(map, isl_dim_out, id);
2166 implication->extension = map;
2167 if (!implication->extension)
2168 return pet_implication_free(implication);
2170 return implication;
2171 error:
2172 isl_set_free(dom);
2173 return NULL;
2176 /* Embed all statements and arrays in "scop" in an extra outer loop
2177 * with iteration domain "dom" and schedule "sched".
2178 * "id" represents the induction variable of the loop.
2179 * "iv_map" maps a possibly virtual iterator to the real iterator.
2180 * That is, it expresses the iterator that some of the parameters in "scop"
2181 * may refer to in terms of the iterator used in "dom" and
2182 * the domain of "sched".
2184 * Any skip conditions within the loop have no effect outside of the loop.
2185 * The caller is responsible for making sure skip[pet_skip_later] has been
2186 * taken into account.
2188 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
2189 __isl_take isl_map *sched, __isl_take isl_aff *iv_map,
2190 __isl_take isl_id *id)
2192 int i;
2194 if (!scop)
2195 goto error;
2197 pet_scop_reset_skip(scop, pet_skip_now);
2198 pet_scop_reset_skip(scop, pet_skip_later);
2200 scop->context = context_embed(scop->context, dom, iv_map, id);
2201 if (!scop->context)
2202 goto error;
2204 for (i = 0; i < scop->n_stmt; ++i) {
2205 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
2206 isl_set_copy(dom), isl_map_copy(sched),
2207 isl_aff_copy(iv_map), isl_id_copy(id));
2208 if (!scop->stmts[i])
2209 goto error;
2212 for (i = 0; i < scop->n_array; ++i) {
2213 scop->arrays[i] = pet_array_embed(scop->arrays[i],
2214 isl_set_copy(dom));
2215 if (!scop->arrays[i])
2216 goto error;
2219 for (i = 0; i < scop->n_implication; ++i) {
2220 scop->implications[i] =
2221 pet_implication_embed(scop->implications[i],
2222 isl_set_copy(dom));
2223 if (!scop->implications[i])
2224 goto error;
2227 isl_set_free(dom);
2228 isl_map_free(sched);
2229 isl_aff_free(iv_map);
2230 isl_id_free(id);
2231 return scop;
2232 error:
2233 isl_set_free(dom);
2234 isl_map_free(sched);
2235 isl_aff_free(iv_map);
2236 isl_id_free(id);
2237 return pet_scop_free(scop);
2240 /* Add extra conditions on the parameters to iteration domain of "stmt".
2242 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
2243 __isl_take isl_set *cond)
2245 if (!stmt)
2246 goto error;
2248 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
2250 return stmt;
2251 error:
2252 isl_set_free(cond);
2253 return pet_stmt_free(stmt);
2256 /* Add extra conditions to scop->skip[type].
2258 * The new skip condition only holds if it held before
2259 * and the condition is true. It does not hold if it did not hold
2260 * before or the condition is false.
2262 * The skip condition is assumed to be an affine expression.
2264 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
2265 enum pet_skip type, __isl_keep isl_set *cond)
2267 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2268 isl_pw_aff *skip;
2269 isl_set *dom;
2271 if (!scop)
2272 return NULL;
2273 if (!ext->skip[type])
2274 return scop;
2276 if (!multi_pw_aff_is_affine(ext->skip[type]))
2277 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
2278 isl_error_internal, "can only resrict affine skips",
2279 return pet_scop_free(scop));
2281 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2282 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
2283 cond = isl_set_copy(cond);
2284 cond = isl_set_from_params(cond);
2285 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
2286 skip = indicator_function(cond, dom);
2287 isl_multi_pw_aff_free(ext->skip[type]);
2288 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
2289 if (!ext->skip[type])
2290 return pet_scop_free(scop);
2292 return scop;
2295 /* Add extra conditions on the parameters to all iteration domains
2296 * and skip conditions.
2298 * A parameter value is valid for the result if it was valid
2299 * for the original scop and satisfies "cond" or if it does
2300 * not satisfy "cond" as in this case the scop is not executed
2301 * and the original constraints on the parameters are irrelevant.
2303 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
2304 __isl_take isl_set *cond)
2306 int i;
2308 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
2309 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
2311 if (!scop)
2312 goto error;
2314 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
2315 scop->context = isl_set_union(scop->context,
2316 isl_set_complement(isl_set_copy(cond)));
2317 scop->context = isl_set_coalesce(scop->context);
2318 scop->context = set_project_out_unnamed_params(scop->context);
2319 if (!scop->context)
2320 goto error;
2322 for (i = 0; i < scop->n_stmt; ++i) {
2323 scop->stmts[i] = stmt_restrict(scop->stmts[i],
2324 isl_set_copy(cond));
2325 if (!scop->stmts[i])
2326 goto error;
2329 isl_set_free(cond);
2330 return scop;
2331 error:
2332 isl_set_free(cond);
2333 return pet_scop_free(scop);
2336 /* Construct a function that (upon precomposition) inserts
2337 * a filter value with name "id" and value "satisfied"
2338 * in the list of filter values embedded in the set space "space".
2340 * If "space" does not contain any filter values yet, we first create
2341 * a function that inserts 0 filter values, i.e.,
2343 * [space -> []] -> space
2345 * We can now assume that space is of the form [dom -> [filters]]
2346 * We construct an identity mapping on dom and a mapping on filters
2347 * that (upon precomposition) inserts the new filter
2349 * dom -> dom
2350 * [satisfied, filters] -> [filters]
2352 * and then compute the cross product
2354 * [dom -> [satisfied, filters]] -> [dom -> [filters]]
2356 static __isl_give isl_pw_multi_aff *insert_filter_pma(
2357 __isl_take isl_space *space, __isl_take isl_id *id, int satisfied)
2359 isl_space *space2;
2360 isl_multi_aff *ma;
2361 isl_pw_multi_aff *pma0, *pma, *pma_dom, *pma_ran;
2362 isl_set *dom;
2364 if (isl_space_is_wrapping(space)) {
2365 space2 = isl_space_map_from_set(isl_space_copy(space));
2366 ma = isl_multi_aff_identity(space2);
2367 space = isl_space_unwrap(space);
2368 } else {
2369 space = isl_space_from_domain(space);
2370 ma = isl_multi_aff_domain_map(isl_space_copy(space));
2373 space2 = isl_space_domain(isl_space_copy(space));
2374 pma_dom = isl_pw_multi_aff_identity(isl_space_map_from_set(space2));
2375 space = isl_space_range(space);
2376 space = isl_space_insert_dims(space, isl_dim_set, 0, 1);
2377 pma_ran = isl_pw_multi_aff_project_out_map(space, isl_dim_set, 0, 1);
2378 pma_ran = isl_pw_multi_aff_set_dim_id(pma_ran, isl_dim_in, 0, id);
2379 pma_ran = isl_pw_multi_aff_fix_si(pma_ran, isl_dim_in, 0, satisfied);
2380 pma = isl_pw_multi_aff_product(pma_dom, pma_ran);
2382 pma0 = isl_pw_multi_aff_from_multi_aff(ma);
2383 pma = isl_pw_multi_aff_pullback_pw_multi_aff(pma0, pma);
2385 return pma;
2388 /* Insert an argument expression corresponding to "test" in front
2389 * of the list of arguments described by *n_arg and *args.
2391 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
2392 __isl_keep isl_multi_pw_aff *test)
2394 int i;
2395 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
2397 if (!test)
2398 return -1;
2400 if (!*args) {
2401 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
2402 if (!*args)
2403 return -1;
2404 } else {
2405 struct pet_expr **ext;
2406 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
2407 if (!ext)
2408 return -1;
2409 for (i = 0; i < *n_arg; ++i)
2410 ext[1 + i] = (*args)[i];
2411 free(*args);
2412 *args = ext;
2414 (*n_arg)++;
2415 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
2416 if (!(*args)[0])
2417 return -1;
2419 return 0;
2422 /* Make the expression "expr" depend on the value of "test"
2423 * being equal to "satisfied".
2425 * If "test" is an affine expression, we simply add the conditions
2426 * on the expression having the value "satisfied" to all access relations
2427 * and index expressions.
2429 * Otherwise, we add a filter to "expr" (which is then assumed to be
2430 * an access expression) corresponding to "test" being equal to "satisfied".
2432 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
2433 __isl_take isl_multi_pw_aff *test, int satisfied)
2435 isl_id *id;
2436 isl_ctx *ctx;
2437 isl_space *space;
2438 isl_pw_multi_aff *pma;
2440 if (!expr || !test)
2441 goto error;
2443 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
2444 isl_pw_aff *pa;
2445 isl_set *cond;
2447 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
2448 isl_multi_pw_aff_free(test);
2449 if (satisfied)
2450 cond = isl_pw_aff_non_zero_set(pa);
2451 else
2452 cond = isl_pw_aff_zero_set(pa);
2453 return pet_expr_restrict(expr, isl_set_params(cond));
2456 ctx = isl_multi_pw_aff_get_ctx(test);
2457 if (expr->type != pet_expr_access)
2458 isl_die(ctx, isl_error_invalid,
2459 "can only filter access expressions", goto error);
2461 space = isl_space_domain(isl_map_get_space(expr->acc.access));
2462 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2463 pma = insert_filter_pma(space, id, satisfied);
2465 expr->acc.access = isl_map_preimage_domain_pw_multi_aff(
2466 expr->acc.access,
2467 isl_pw_multi_aff_copy(pma));
2468 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
2469 expr->acc.index, pma);
2470 if (!expr->acc.access || !expr->acc.index)
2471 goto error;
2473 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
2474 goto error;
2476 isl_multi_pw_aff_free(test);
2477 return expr;
2478 error:
2479 isl_multi_pw_aff_free(test);
2480 return pet_expr_free(expr);
2483 /* Look through the applications in "scop" for any that can be
2484 * applied to the filter expressed by "map" and "satisified".
2485 * If there is any, then apply it to "map" and return the result.
2486 * Otherwise, return "map".
2487 * "id" is the identifier of the virtual array.
2489 * We only introduce at most one implication for any given virtual array,
2490 * so we can apply the implication and return as soon as we find one.
2492 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
2493 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
2495 int i;
2497 for (i = 0; i < scop->n_implication; ++i) {
2498 struct pet_implication *pi = scop->implications[i];
2499 isl_id *pi_id;
2501 if (pi->satisfied != satisfied)
2502 continue;
2503 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
2504 isl_id_free(pi_id);
2505 if (pi_id != id)
2506 continue;
2508 return isl_map_apply_range(map, isl_map_copy(pi->extension));
2511 return map;
2514 /* Is the filter expressed by "test" and "satisfied" implied
2515 * by filter "pos" on "domain", with filter "expr", taking into
2516 * account the implications of "scop"?
2518 * For filter on domain implying that expressed by "test" and "satisfied",
2519 * the filter needs to be an access to the same (virtual) array as "test" and
2520 * the filter value needs to be equal to "satisfied".
2521 * Moreover, the filter access relation, possibly extended by
2522 * the implications in "scop" needs to contain "test".
2524 static int implies_filter(struct pet_scop *scop,
2525 __isl_keep isl_map *domain, int pos, struct pet_expr *expr,
2526 __isl_keep isl_map *test, int satisfied)
2528 isl_id *test_id, *arg_id;
2529 isl_val *val;
2530 int is_int;
2531 int s;
2532 int is_subset;
2533 isl_map *implied;
2535 if (expr->type != pet_expr_access)
2536 return 0;
2537 test_id = isl_map_get_tuple_id(test, isl_dim_out);
2538 arg_id = pet_expr_access_get_id(expr);
2539 isl_id_free(arg_id);
2540 isl_id_free(test_id);
2541 if (test_id != arg_id)
2542 return 0;
2543 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
2544 is_int = isl_val_is_int(val);
2545 if (is_int)
2546 s = isl_val_get_num_si(val);
2547 isl_val_free(val);
2548 if (!val)
2549 return -1;
2550 if (!is_int)
2551 return 0;
2552 if (s != satisfied)
2553 return 0;
2555 implied = isl_map_copy(expr->acc.access);
2556 implied = apply_implications(scop, implied, test_id, satisfied);
2557 is_subset = isl_map_is_subset(test, implied);
2558 isl_map_free(implied);
2560 return is_subset;
2563 /* Is the filter expressed by "test" and "satisfied" implied
2564 * by any of the filters on the domain of "stmt", taking into
2565 * account the implications of "scop"?
2567 static int filter_implied(struct pet_scop *scop,
2568 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
2570 int i;
2571 int implied;
2572 isl_id *test_id;
2573 isl_map *domain;
2574 isl_map *test_map;
2576 if (!scop || !stmt || !test)
2577 return -1;
2578 if (scop->n_implication == 0)
2579 return 0;
2580 if (stmt->n_arg == 0)
2581 return 0;
2583 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
2584 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
2586 implied = 0;
2587 for (i = 0; i < stmt->n_arg; ++i) {
2588 implied = implies_filter(scop, domain, i, stmt->args[i],
2589 test_map, satisfied);
2590 if (implied < 0 || implied)
2591 break;
2594 isl_map_free(test_map);
2595 isl_map_free(domain);
2596 return implied;
2599 /* Make the statement "stmt" depend on the value of "test"
2600 * being equal to "satisfied" by adjusting stmt->domain.
2602 * The domain of "test" corresponds to the (zero or more) outer dimensions
2603 * of the iteration domain.
2605 * We first extend "test" to apply to the entire iteration domain and
2606 * then check if the filter that we are about to add is implied
2607 * by any of the current filters, possibly taking into account
2608 * the implications in "scop". If so, we leave "stmt" untouched and return.
2610 * Otherwise, we insert an argument corresponding to a read to "test"
2611 * from the iteration domain of "stmt" in front of the list of arguments.
2612 * We also insert a corresponding output dimension in the wrapped
2613 * map contained in stmt->domain, with value set to "satisfied".
2615 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
2616 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
2618 int i;
2619 int implied;
2620 isl_id *id;
2621 isl_ctx *ctx;
2622 isl_pw_multi_aff *pma;
2623 isl_multi_aff *add_dom;
2624 isl_space *space;
2625 isl_local_space *ls;
2626 int n_test_dom;
2628 if (!stmt || !test)
2629 goto error;
2631 space = isl_set_get_space(stmt->domain);
2632 if (isl_space_is_wrapping(space))
2633 space = isl_space_domain(isl_space_unwrap(space));
2634 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
2635 space = isl_space_from_domain(space);
2636 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
2637 add_dom = isl_multi_aff_zero(isl_space_copy(space));
2638 ls = isl_local_space_from_space(isl_space_domain(space));
2639 for (i = 0; i < n_test_dom; ++i) {
2640 isl_aff *aff;
2641 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2642 isl_dim_set, i);
2643 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
2645 isl_local_space_free(ls);
2646 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
2648 implied = filter_implied(scop, stmt, test, satisfied);
2649 if (implied < 0)
2650 goto error;
2651 if (implied) {
2652 isl_multi_pw_aff_free(test);
2653 return stmt;
2656 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2657 pma = insert_filter_pma(isl_set_get_space(stmt->domain), id, satisfied);
2658 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
2660 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
2661 goto error;
2663 isl_multi_pw_aff_free(test);
2664 return stmt;
2665 error:
2666 isl_multi_pw_aff_free(test);
2667 return pet_stmt_free(stmt);
2670 /* Does "scop" have a skip condition of the given "type"?
2672 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2674 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2676 if (!scop)
2677 return -1;
2678 return ext->skip[type] != NULL;
2681 /* Does "scop" have a skip condition of the given "type" that
2682 * is an affine expression?
2684 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2686 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2688 if (!scop)
2689 return -1;
2690 if (!ext->skip[type])
2691 return 0;
2692 return multi_pw_aff_is_affine(ext->skip[type]);
2695 /* Does "scop" have a skip condition of the given "type" that
2696 * is not an affine expression?
2698 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2700 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2701 int aff;
2703 if (!scop)
2704 return -1;
2705 if (!ext->skip[type])
2706 return 0;
2707 aff = multi_pw_aff_is_affine(ext->skip[type]);
2708 if (aff < 0)
2709 return -1;
2710 return !aff;
2713 /* Does "scop" have a skip condition of the given "type" that
2714 * is affine and holds on the entire domain?
2716 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2718 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2719 isl_pw_aff *pa;
2720 isl_set *set;
2721 int is_aff;
2722 int is_univ;
2724 is_aff = pet_scop_has_affine_skip(scop, type);
2725 if (is_aff < 0 || !is_aff)
2726 return is_aff;
2728 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2729 set = isl_pw_aff_non_zero_set(pa);
2730 is_univ = isl_set_plain_is_universe(set);
2731 isl_set_free(set);
2733 return is_univ;
2736 /* Replace scop->skip[type] by "skip".
2738 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2739 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2741 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2743 if (!scop || !skip)
2744 goto error;
2746 isl_multi_pw_aff_free(ext->skip[type]);
2747 ext->skip[type] = skip;
2749 return scop;
2750 error:
2751 isl_multi_pw_aff_free(skip);
2752 return pet_scop_free(scop);
2755 /* Return a copy of scop->skip[type].
2757 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2758 enum pet_skip type)
2760 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2762 if (!scop)
2763 return NULL;
2765 return isl_multi_pw_aff_copy(ext->skip[type]);
2768 /* Assuming scop->skip[type] is an affine expression,
2769 * return the constraints on the parameters for which the skip condition
2770 * holds.
2772 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2773 enum pet_skip type)
2775 isl_multi_pw_aff *skip;
2776 isl_pw_aff *pa;
2778 skip = pet_scop_get_skip(scop, type);
2779 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2780 isl_multi_pw_aff_free(skip);
2781 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2784 /* Return the identifier of the variable that is accessed by
2785 * the skip condition of the given type.
2787 * The skip condition is assumed not to be an affine condition.
2789 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2790 enum pet_skip type)
2792 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2794 if (!scop)
2795 return NULL;
2797 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2800 /* Return an access pet_expr corresponding to the skip condition
2801 * of the given type.
2803 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2804 enum pet_skip type)
2806 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2809 /* Drop the the skip condition scop->skip[type].
2811 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2813 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2815 if (!scop)
2816 return;
2818 isl_multi_pw_aff_free(ext->skip[type]);
2819 ext->skip[type] = NULL;
2822 /* Make the skip condition (if any) depend on the value of "test" being
2823 * equal to "satisfied".
2825 * We only support the case where the original skip condition is universal,
2826 * i.e., where skipping is unconditional, and where satisfied == 1.
2827 * In this case, the skip condition is changed to skip only when
2828 * "test" is equal to one.
2830 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2831 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2833 int is_univ = 0;
2835 if (!scop)
2836 return NULL;
2837 if (!pet_scop_has_skip(scop, type))
2838 return scop;
2840 if (satisfied)
2841 is_univ = pet_scop_has_universal_skip(scop, type);
2842 if (is_univ < 0)
2843 return pet_scop_free(scop);
2844 if (satisfied && is_univ) {
2845 isl_space *space = isl_multi_pw_aff_get_space(test);
2846 isl_multi_pw_aff *skip;
2847 skip = isl_multi_pw_aff_zero(space);
2848 scop = pet_scop_set_skip(scop, type, skip);
2849 if (!scop)
2850 return NULL;
2851 } else {
2852 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2853 "skip expression cannot be filtered",
2854 return pet_scop_free(scop));
2857 return scop;
2860 /* Make all statements in "scop" depend on the value of "test"
2861 * being equal to "satisfied" by adjusting their domains.
2863 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2864 __isl_take isl_multi_pw_aff *test, int satisfied)
2866 int i;
2868 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2869 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2871 if (!scop || !test)
2872 goto error;
2874 for (i = 0; i < scop->n_stmt; ++i) {
2875 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2876 isl_multi_pw_aff_copy(test), satisfied);
2877 if (!scop->stmts[i])
2878 goto error;
2881 isl_multi_pw_aff_free(test);
2882 return scop;
2883 error:
2884 isl_multi_pw_aff_free(test);
2885 return pet_scop_free(scop);
2888 /* Add all parameters in "expr" to "dim" and return the result.
2890 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2891 __isl_take isl_space *dim)
2893 int i;
2895 if (!expr)
2896 goto error;
2897 for (i = 0; i < expr->n_arg; ++i)
2899 dim = expr_collect_params(expr->args[i], dim);
2901 if (expr->type == pet_expr_access)
2902 dim = isl_space_align_params(dim,
2903 isl_map_get_space(expr->acc.access));
2905 return dim;
2906 error:
2907 pet_expr_free(expr);
2908 return isl_space_free(dim);
2911 /* Add all parameters in "stmt" to "dim" and return the result.
2913 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2914 __isl_take isl_space *dim)
2916 if (!stmt)
2917 goto error;
2919 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2920 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2921 dim = expr_collect_params(stmt->body, dim);
2923 return dim;
2924 error:
2925 isl_space_free(dim);
2926 return pet_stmt_free(stmt);
2929 /* Add all parameters in "array" to "dim" and return the result.
2931 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2932 __isl_take isl_space *dim)
2934 if (!array)
2935 goto error;
2937 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2938 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2940 return dim;
2941 error:
2942 pet_array_free(array);
2943 return isl_space_free(dim);
2946 /* Add all parameters in "scop" to "dim" and return the result.
2948 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2949 __isl_take isl_space *dim)
2951 int i;
2953 if (!scop)
2954 goto error;
2956 for (i = 0; i < scop->n_array; ++i)
2957 dim = array_collect_params(scop->arrays[i], dim);
2959 for (i = 0; i < scop->n_stmt; ++i)
2960 dim = stmt_collect_params(scop->stmts[i], dim);
2962 return dim;
2963 error:
2964 isl_space_free(dim);
2965 pet_scop_free(scop);
2966 return NULL;
2969 /* Add all parameters in "dim" to all access relations and index expressions
2970 * in "expr".
2972 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2973 __isl_take isl_space *dim)
2975 int i;
2977 if (!expr)
2978 goto error;
2980 for (i = 0; i < expr->n_arg; ++i) {
2981 expr->args[i] =
2982 expr_propagate_params(expr->args[i],
2983 isl_space_copy(dim));
2984 if (!expr->args[i])
2985 goto error;
2988 if (expr->type == pet_expr_access) {
2989 expr->acc.access = isl_map_align_params(expr->acc.access,
2990 isl_space_copy(dim));
2991 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
2992 isl_space_copy(dim));
2993 if (!expr->acc.access || !expr->acc.index)
2994 goto error;
2997 isl_space_free(dim);
2998 return expr;
2999 error:
3000 isl_space_free(dim);
3001 return pet_expr_free(expr);
3004 /* Add all parameters in "dim" to the domain, schedule and
3005 * all access relations in "stmt".
3007 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
3008 __isl_take isl_space *dim)
3010 if (!stmt)
3011 goto error;
3013 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
3014 stmt->schedule = isl_map_align_params(stmt->schedule,
3015 isl_space_copy(dim));
3016 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
3018 if (!stmt->domain || !stmt->schedule || !stmt->body)
3019 goto error;
3021 isl_space_free(dim);
3022 return stmt;
3023 error:
3024 isl_space_free(dim);
3025 return pet_stmt_free(stmt);
3028 /* Add all parameters in "dim" to "array".
3030 static struct pet_array *array_propagate_params(struct pet_array *array,
3031 __isl_take isl_space *dim)
3033 if (!array)
3034 goto error;
3036 array->context = isl_set_align_params(array->context,
3037 isl_space_copy(dim));
3038 array->extent = isl_set_align_params(array->extent,
3039 isl_space_copy(dim));
3040 if (array->value_bounds) {
3041 array->value_bounds = isl_set_align_params(array->value_bounds,
3042 isl_space_copy(dim));
3043 if (!array->value_bounds)
3044 goto error;
3047 if (!array->context || !array->extent)
3048 goto error;
3050 isl_space_free(dim);
3051 return array;
3052 error:
3053 isl_space_free(dim);
3054 return pet_array_free(array);
3057 /* Add all parameters in "dim" to "scop".
3059 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
3060 __isl_take isl_space *dim)
3062 int i;
3064 if (!scop)
3065 goto error;
3067 for (i = 0; i < scop->n_array; ++i) {
3068 scop->arrays[i] = array_propagate_params(scop->arrays[i],
3069 isl_space_copy(dim));
3070 if (!scop->arrays[i])
3071 goto error;
3074 for (i = 0; i < scop->n_stmt; ++i) {
3075 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
3076 isl_space_copy(dim));
3077 if (!scop->stmts[i])
3078 goto error;
3081 isl_space_free(dim);
3082 return scop;
3083 error:
3084 isl_space_free(dim);
3085 return pet_scop_free(scop);
3088 /* Update all isl_sets and isl_maps in "scop" such that they all
3089 * have the same parameters.
3091 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
3093 isl_space *dim;
3095 if (!scop)
3096 return NULL;
3098 dim = isl_set_get_space(scop->context);
3099 dim = scop_collect_params(scop, dim);
3101 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
3102 scop = scop_propagate_params(scop, dim);
3104 return scop;
3107 /* Check if the given index expression accesses a (0D) array that corresponds
3108 * to one of the parameters in "dim". If so, replace the array access
3109 * by an access to the set of integers with as index (and value)
3110 * that parameter.
3112 static __isl_give isl_multi_pw_aff *index_detect_parameter(
3113 __isl_take isl_multi_pw_aff *index, __isl_take isl_space *space)
3115 isl_local_space *ls;
3116 isl_id *array_id = NULL;
3117 isl_aff *aff;
3118 int pos = -1;
3120 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out)) {
3121 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
3122 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3124 isl_space_free(space);
3126 if (pos < 0) {
3127 isl_id_free(array_id);
3128 return index;
3131 space = isl_multi_pw_aff_get_domain_space(index);
3132 isl_multi_pw_aff_free(index);
3134 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3135 if (pos < 0) {
3136 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
3137 space = isl_space_set_dim_id(space, isl_dim_param, 0, array_id);
3138 pos = 0;
3139 } else
3140 isl_id_free(array_id);
3142 ls = isl_local_space_from_space(space);
3143 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
3144 index = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
3146 return index;
3149 /* Check if the given access relation accesses a (0D) array that corresponds
3150 * to one of the parameters in "dim". If so, replace the array access
3151 * by an access to the set of integers with as index (and value)
3152 * that parameter.
3154 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
3155 __isl_take isl_space *dim)
3157 isl_id *array_id = NULL;
3158 int pos = -1;
3160 if (isl_map_has_tuple_id(access, isl_dim_out)) {
3161 array_id = isl_map_get_tuple_id(access, isl_dim_out);
3162 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
3164 isl_space_free(dim);
3166 if (pos < 0) {
3167 isl_id_free(array_id);
3168 return access;
3171 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
3172 if (pos < 0) {
3173 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
3174 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
3175 pos = 0;
3176 } else
3177 isl_id_free(array_id);
3179 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
3180 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
3182 return access;
3185 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3186 * in "dim" by a value equal to the corresponding parameter.
3188 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
3189 __isl_take isl_space *dim)
3191 int i;
3193 if (!expr)
3194 goto error;
3196 for (i = 0; i < expr->n_arg; ++i) {
3197 expr->args[i] =
3198 expr_detect_parameter_accesses(expr->args[i],
3199 isl_space_copy(dim));
3200 if (!expr->args[i])
3201 goto error;
3204 if (expr->type == pet_expr_access) {
3205 expr->acc.access = access_detect_parameter(expr->acc.access,
3206 isl_space_copy(dim));
3207 expr->acc.index = index_detect_parameter(expr->acc.index,
3208 isl_space_copy(dim));
3209 if (!expr->acc.access || !expr->acc.index)
3210 goto error;
3213 isl_space_free(dim);
3214 return expr;
3215 error:
3216 isl_space_free(dim);
3217 return pet_expr_free(expr);
3220 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3221 * in "dim" by a value equal to the corresponding parameter.
3223 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
3224 __isl_take isl_space *dim)
3226 if (!stmt)
3227 goto error;
3229 stmt->body = expr_detect_parameter_accesses(stmt->body,
3230 isl_space_copy(dim));
3232 if (!stmt->domain || !stmt->schedule || !stmt->body)
3233 goto error;
3235 isl_space_free(dim);
3236 return stmt;
3237 error:
3238 isl_space_free(dim);
3239 return pet_stmt_free(stmt);
3242 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3243 * in "dim" by a value equal to the corresponding parameter.
3245 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
3246 __isl_take isl_space *dim)
3248 int i;
3250 if (!scop)
3251 goto error;
3253 for (i = 0; i < scop->n_stmt; ++i) {
3254 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
3255 isl_space_copy(dim));
3256 if (!scop->stmts[i])
3257 goto error;
3260 isl_space_free(dim);
3261 return scop;
3262 error:
3263 isl_space_free(dim);
3264 return pet_scop_free(scop);
3267 /* Replace all accesses to (0D) arrays that correspond to any of
3268 * the parameters used in "scop" by a value equal
3269 * to the corresponding parameter.
3271 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
3273 isl_space *dim;
3275 if (!scop)
3276 return NULL;
3278 dim = isl_set_get_space(scop->context);
3279 dim = scop_collect_params(scop, dim);
3281 scop = scop_detect_parameter_accesses(scop, dim);
3283 return scop;
3286 /* Return the relation mapping domain iterations to all possibly
3287 * accessed data elements.
3288 * In particular, take the access relation and project out the values
3289 * of the arguments, if any.
3291 static __isl_give isl_map *expr_access_get_may_access(struct pet_expr *expr)
3293 isl_map *access;
3294 isl_space *space;
3295 isl_map *map;
3297 if (!expr)
3298 return NULL;
3299 if (expr->type != pet_expr_access)
3300 return NULL;
3302 access = isl_map_copy(expr->acc.access);
3303 if (expr->n_arg == 0)
3304 return access;
3306 space = isl_space_domain(isl_map_get_space(access));
3307 map = isl_map_universe(isl_space_unwrap(space));
3308 map = isl_map_domain_map(map);
3309 access = isl_map_apply_domain(access, map);
3311 return access;
3314 /* Add all read access relations (if "read" is set) and/or all write
3315 * access relations (if "write" is set) to "accesses" and return the result.
3317 * If "must" is set, then we only add the accesses that are definitely
3318 * performed. Otherwise, we add all potential accesses.
3319 * In particular, if the access has any arguments, then if "must" is
3320 * set we currently skip the access completely. If "must" is not set,
3321 * we project out the values of the access arguments.
3323 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
3324 int read, int write, int must, __isl_take isl_union_map *accesses)
3326 int i;
3327 isl_id *id;
3328 isl_space *dim;
3330 if (!expr)
3331 return NULL;
3333 for (i = 0; i < expr->n_arg; ++i)
3334 accesses = expr_collect_accesses(expr->args[i],
3335 read, write, must, accesses);
3337 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
3338 ((read && expr->acc.read) || (write && expr->acc.write)) &&
3339 (!must || expr->n_arg == 0)) {
3340 isl_map *access;
3342 access = expr_access_get_may_access(expr);
3343 accesses = isl_union_map_add_map(accesses, access);
3346 return accesses;
3349 /* Collect and return all read access relations (if "read" is set)
3350 * and/or all write access relations (if "write" is set) in "stmt".
3352 * If "must" is set, then we only add the accesses that are definitely
3353 * performed. Otherwise, we add all potential accesses.
3354 * In particular, if the statement has any arguments, then if "must" is
3355 * set we currently skip the statement completely. If "must" is not set,
3356 * we project out the values of the statement arguments.
3358 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
3359 int read, int write, int must, __isl_take isl_space *dim)
3361 isl_union_map *accesses;
3362 isl_set *domain;
3364 if (!stmt)
3365 return NULL;
3367 accesses = isl_union_map_empty(dim);
3369 if (must && stmt->n_arg > 0)
3370 return accesses;
3372 domain = isl_set_copy(stmt->domain);
3373 if (isl_set_is_wrapping(domain))
3374 domain = isl_map_domain(isl_set_unwrap(domain));
3376 accesses = expr_collect_accesses(stmt->body,
3377 read, write, must, accesses);
3378 accesses = isl_union_map_intersect_domain(accesses,
3379 isl_union_set_from_set(domain));
3381 return accesses;
3384 /* Collect and return all read access relations (if "read" is set)
3385 * and/or all write access relations (if "write" is set) in "scop".
3386 * If "must" is set, then we only add the accesses that are definitely
3387 * performed. Otherwise, we add all potential accesses.
3389 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
3390 int read, int write, int must)
3392 int i;
3393 isl_union_map *accesses;
3394 isl_union_set *arrays;
3396 if (!scop)
3397 return NULL;
3399 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
3401 for (i = 0; i < scop->n_stmt; ++i) {
3402 isl_union_map *accesses_i;
3403 isl_space *dim = isl_set_get_space(scop->context);
3404 accesses_i = stmt_collect_accesses(scop->stmts[i],
3405 read, write, must, dim);
3406 accesses = isl_union_map_union(accesses, accesses_i);
3409 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
3410 for (i = 0; i < scop->n_array; ++i) {
3411 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
3412 arrays = isl_union_set_add_set(arrays, extent);
3414 accesses = isl_union_map_intersect_range(accesses, arrays);
3416 return accesses;
3419 /* Collect all potential read access relations.
3421 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
3423 return scop_collect_accesses(scop, 1, 0, 0);
3426 /* Collect all potential write access relations.
3428 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
3430 return scop_collect_accesses(scop, 0, 1, 0);
3433 /* Collect all definite write access relations.
3435 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
3437 return scop_collect_accesses(scop, 0, 1, 1);
3440 /* Collect and return the union of iteration domains in "scop".
3442 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
3444 int i;
3445 isl_set *domain_i;
3446 isl_union_set *domain;
3448 if (!scop)
3449 return NULL;
3451 domain = isl_union_set_empty(isl_set_get_space(scop->context));
3453 for (i = 0; i < scop->n_stmt; ++i) {
3454 domain_i = isl_set_copy(scop->stmts[i]->domain);
3455 domain = isl_union_set_add_set(domain, domain_i);
3458 return domain;
3461 /* Collect and return the schedules of the statements in "scop".
3462 * The range is normalized to the maximal number of scheduling
3463 * dimensions.
3465 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
3467 int i, j;
3468 isl_map *schedule_i;
3469 isl_union_map *schedule;
3470 int depth, max_depth = 0;
3472 if (!scop)
3473 return NULL;
3475 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
3477 for (i = 0; i < scop->n_stmt; ++i) {
3478 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
3479 if (depth > max_depth)
3480 max_depth = depth;
3483 for (i = 0; i < scop->n_stmt; ++i) {
3484 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
3485 depth = isl_map_dim(schedule_i, isl_dim_out);
3486 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
3487 max_depth - depth);
3488 for (j = depth; j < max_depth; ++j)
3489 schedule_i = isl_map_fix_si(schedule_i,
3490 isl_dim_out, j, 0);
3491 schedule = isl_union_map_add_map(schedule, schedule_i);
3494 return schedule;
3497 /* Does expression "expr" write to "id"?
3499 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
3501 int i;
3502 isl_id *write_id;
3504 for (i = 0; i < expr->n_arg; ++i) {
3505 int writes = expr_writes(expr->args[i], id);
3506 if (writes < 0 || writes)
3507 return writes;
3510 if (expr->type != pet_expr_access)
3511 return 0;
3512 if (!expr->acc.write)
3513 return 0;
3514 if (pet_expr_is_affine(expr))
3515 return 0;
3517 write_id = pet_expr_access_get_id(expr);
3518 isl_id_free(write_id);
3520 if (!write_id)
3521 return -1;
3523 return write_id == id;
3526 /* Does statement "stmt" write to "id"?
3528 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
3530 return expr_writes(stmt->body, id);
3533 /* Is there any write access in "scop" that accesses "id"?
3535 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
3537 int i;
3539 if (!scop)
3540 return -1;
3542 for (i = 0; i < scop->n_stmt; ++i) {
3543 int writes = stmt_writes(scop->stmts[i], id);
3544 if (writes < 0 || writes)
3545 return writes;
3548 return 0;
3551 /* Add a reference identifier to access expression "expr".
3552 * "user" points to an integer that contains the sequence number
3553 * of the next reference.
3555 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
3557 isl_ctx *ctx;
3558 char name[50];
3559 int *n_ref = user;
3561 if (!expr)
3562 return expr;
3564 ctx = isl_map_get_ctx(expr->acc.access);
3565 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
3566 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
3567 if (!expr->acc.ref_id)
3568 return pet_expr_free(expr);
3570 return expr;
3573 /* Add a reference identifier to all access expressions in "stmt".
3574 * "n_ref" points to an integer that contains the sequence number
3575 * of the next reference.
3577 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
3579 int i;
3581 if (!stmt)
3582 return NULL;
3584 for (i = 0; i < stmt->n_arg; ++i) {
3585 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3586 &access_add_ref_id, n_ref);
3587 if (!stmt->args[i])
3588 return pet_stmt_free(stmt);
3591 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
3592 if (!stmt->body)
3593 return pet_stmt_free(stmt);
3595 return stmt;
3598 /* Add a reference identifier to all access expressions in "scop".
3600 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
3602 int i;
3603 int n_ref;
3605 if (!scop)
3606 return NULL;
3608 n_ref = 0;
3609 for (i = 0; i < scop->n_stmt; ++i) {
3610 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
3611 if (!scop->stmts[i])
3612 return pet_scop_free(scop);
3615 return scop;
3618 /* Reset the user pointer on all parameter ids in "array".
3620 static struct pet_array *array_anonymize(struct pet_array *array)
3622 if (!array)
3623 return NULL;
3625 array->context = isl_set_reset_user(array->context);
3626 array->extent = isl_set_reset_user(array->extent);
3627 if (!array->context || !array->extent)
3628 return pet_array_free(array);
3630 return array;
3633 /* Reset the user pointer on all parameter and tuple ids in
3634 * the access relation and the index expressions
3635 * of the access expression "expr".
3637 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
3639 expr->acc.access = isl_map_reset_user(expr->acc.access);
3640 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
3641 if (!expr->acc.access || !expr->acc.index)
3642 return pet_expr_free(expr);
3644 return expr;
3647 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3649 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3651 int i;
3652 isl_space *space;
3653 isl_set *domain;
3655 if (!stmt)
3656 return NULL;
3658 stmt->domain = isl_set_reset_user(stmt->domain);
3659 stmt->schedule = isl_map_reset_user(stmt->schedule);
3660 if (!stmt->domain || !stmt->schedule)
3661 return pet_stmt_free(stmt);
3663 for (i = 0; i < stmt->n_arg; ++i) {
3664 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3665 &access_anonymize, NULL);
3666 if (!stmt->args[i])
3667 return pet_stmt_free(stmt);
3670 stmt->body = pet_expr_map_access(stmt->body,
3671 &access_anonymize, NULL);
3672 if (!stmt->body)
3673 return pet_stmt_free(stmt);
3675 return stmt;
3678 /* Reset the user pointer on the tuple ids and all parameter ids
3679 * in "implication".
3681 static struct pet_implication *implication_anonymize(
3682 struct pet_implication *implication)
3684 if (!implication)
3685 return NULL;
3687 implication->extension = isl_map_reset_user(implication->extension);
3688 if (!implication->extension)
3689 return pet_implication_free(implication);
3691 return implication;
3694 /* Reset the user pointer on all parameter and tuple ids in "scop".
3696 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
3698 int i;
3700 if (!scop)
3701 return NULL;
3703 scop->context = isl_set_reset_user(scop->context);
3704 scop->context_value = isl_set_reset_user(scop->context_value);
3705 if (!scop->context || !scop->context_value)
3706 return pet_scop_free(scop);
3708 for (i = 0; i < scop->n_array; ++i) {
3709 scop->arrays[i] = array_anonymize(scop->arrays[i]);
3710 if (!scop->arrays[i])
3711 return pet_scop_free(scop);
3714 for (i = 0; i < scop->n_stmt; ++i) {
3715 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
3716 if (!scop->stmts[i])
3717 return pet_scop_free(scop);
3720 for (i = 0; i < scop->n_implication; ++i) {
3721 scop->implications[i] =
3722 implication_anonymize(scop->implications[i]);
3723 if (!scop->implications[i])
3724 return pet_scop_free(scop);
3727 return scop;
3730 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
3731 * then intersect the range of "map" with the valid set of values.
3733 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
3734 struct pet_expr *arg, __isl_keep isl_union_map *value_bounds)
3736 isl_id *id;
3737 isl_map *vb;
3738 isl_space *space;
3739 isl_ctx *ctx = isl_map_get_ctx(map);
3741 id = pet_expr_access_get_id(arg);
3742 space = isl_space_alloc(ctx, 0, 0, 1);
3743 space = isl_space_set_tuple_id(space, isl_dim_in, id);
3744 vb = isl_union_map_extract_map(value_bounds, space);
3745 if (!isl_map_plain_is_empty(vb))
3746 map = isl_map_intersect_range(map, isl_map_range(vb));
3747 else
3748 isl_map_free(vb);
3750 return map;
3753 /* Given a set "domain", return a wrapped relation with the given set
3754 * as domain and a range of dimension "n_arg", where each coordinate
3755 * is either unbounded or, if the corresponding element of args is of
3756 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
3758 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
3759 unsigned n_arg, struct pet_expr **args,
3760 __isl_keep isl_union_map *value_bounds)
3762 int i;
3763 isl_map *map;
3764 isl_space *space;
3766 map = isl_map_from_domain(domain);
3767 space = isl_map_get_space(map);
3768 space = isl_space_add_dims(space, isl_dim_out, 1);
3770 for (i = 0; i < n_arg; ++i) {
3771 isl_map *map_i;
3772 struct pet_expr *arg = args[i];
3774 map_i = isl_map_universe(isl_space_copy(space));
3775 if (arg->type == pet_expr_access)
3776 map_i = access_apply_value_bounds(map_i, arg,
3777 value_bounds);
3778 map = isl_map_flat_range_product(map, map_i);
3780 isl_space_free(space);
3782 return isl_map_wrap(map);
3785 /* Data used in access_gist() callback.
3787 struct pet_access_gist_data {
3788 isl_set *domain;
3789 isl_union_map *value_bounds;
3792 /* Given an expression "expr" of type pet_expr_access, compute
3793 * the gist of the associated access relation and index expression
3794 * with respect to data->domain and the bounds on the values of the arguments
3795 * of the expression.
3797 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
3799 struct pet_access_gist_data *data = user;
3800 isl_set *domain;
3802 domain = isl_set_copy(data->domain);
3803 if (expr->n_arg > 0)
3804 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
3805 data->value_bounds);
3807 expr->acc.access = isl_map_gist_domain(expr->acc.access,
3808 isl_set_copy(domain));
3809 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
3810 if (!expr->acc.access || !expr->acc.index)
3811 return pet_expr_free(expr);
3813 return expr;
3816 /* Compute the gist of the iteration domain and all access relations
3817 * of "stmt" based on the constraints on the parameters specified by "context"
3818 * and the constraints on the values of nested accesses specified
3819 * by "value_bounds".
3821 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3822 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3824 int i;
3825 isl_space *space;
3826 isl_set *domain;
3827 struct pet_access_gist_data data;
3829 if (!stmt)
3830 return NULL;
3832 data.domain = isl_set_copy(stmt->domain);
3833 data.value_bounds = value_bounds;
3834 if (stmt->n_arg > 0)
3835 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3837 data.domain = isl_set_intersect_params(data.domain,
3838 isl_set_copy(context));
3840 for (i = 0; i < stmt->n_arg; ++i) {
3841 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3842 &access_gist, &data);
3843 if (!stmt->args[i])
3844 goto error;
3847 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
3848 if (!stmt->body)
3849 goto error;
3851 isl_set_free(data.domain);
3853 space = isl_set_get_space(stmt->domain);
3854 if (isl_space_is_wrapping(space))
3855 space = isl_space_domain(isl_space_unwrap(space));
3856 domain = isl_set_universe(space);
3857 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3858 if (stmt->n_arg > 0)
3859 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3860 value_bounds);
3861 stmt->domain = isl_set_gist(stmt->domain, domain);
3862 if (!stmt->domain)
3863 return pet_stmt_free(stmt);
3865 return stmt;
3866 error:
3867 isl_set_free(data.domain);
3868 return pet_stmt_free(stmt);
3871 /* Compute the gist of the extent of the array
3872 * based on the constraints on the parameters specified by "context".
3874 static struct pet_array *array_gist(struct pet_array *array,
3875 __isl_keep isl_set *context)
3877 if (!array)
3878 return NULL;
3880 array->extent = isl_set_gist_params(array->extent,
3881 isl_set_copy(context));
3882 if (!array->extent)
3883 return pet_array_free(array);
3885 return array;
3888 /* Compute the gist of all sets and relations in "scop"
3889 * based on the constraints on the parameters specified by "scop->context"
3890 * and the constraints on the values of nested accesses specified
3891 * by "value_bounds".
3893 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3894 __isl_keep isl_union_map *value_bounds)
3896 int i;
3898 if (!scop)
3899 return NULL;
3901 scop->context = isl_set_coalesce(scop->context);
3902 if (!scop->context)
3903 return pet_scop_free(scop);
3905 for (i = 0; i < scop->n_array; ++i) {
3906 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3907 if (!scop->arrays[i])
3908 return pet_scop_free(scop);
3911 for (i = 0; i < scop->n_stmt; ++i) {
3912 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3913 value_bounds);
3914 if (!scop->stmts[i])
3915 return pet_scop_free(scop);
3918 return scop;
3921 /* Intersect the context of "scop" with "context".
3922 * To ensure that we don't introduce any unnamed parameters in
3923 * the context of "scop", we first remove the unnamed parameters
3924 * from "context".
3926 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3927 __isl_take isl_set *context)
3929 if (!scop)
3930 goto error;
3932 context = set_project_out_unnamed_params(context);
3933 scop->context = isl_set_intersect(scop->context, context);
3934 if (!scop->context)
3935 return pet_scop_free(scop);
3937 return scop;
3938 error:
3939 isl_set_free(context);
3940 return pet_scop_free(scop);
3943 /* Drop the current context of "scop". That is, replace the context
3944 * by a universal set.
3946 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3948 isl_space *space;
3950 if (!scop)
3951 return NULL;
3953 space = isl_set_get_space(scop->context);
3954 isl_set_free(scop->context);
3955 scop->context = isl_set_universe(space);
3956 if (!scop->context)
3957 return pet_scop_free(scop);
3959 return scop;
3962 /* Append "array" to the arrays of "scop".
3964 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3965 struct pet_array *array)
3967 isl_ctx *ctx;
3968 struct pet_array **arrays;
3970 if (!array || !scop)
3971 goto error;
3973 ctx = isl_set_get_ctx(scop->context);
3974 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3975 scop->n_array + 1);
3976 if (!arrays)
3977 goto error;
3978 scop->arrays = arrays;
3979 scop->arrays[scop->n_array] = array;
3980 scop->n_array++;
3982 return scop;
3983 error:
3984 pet_array_free(array);
3985 return pet_scop_free(scop);
3988 /* Create and return an implication on filter values equal to "satisfied"
3989 * with extension "map".
3991 static struct pet_implication *new_implication(__isl_take isl_map *map,
3992 int satisfied)
3994 isl_ctx *ctx;
3995 struct pet_implication *implication;
3997 if (!map)
3998 return NULL;
3999 ctx = isl_map_get_ctx(map);
4000 implication = isl_alloc_type(ctx, struct pet_implication);
4001 if (!implication)
4002 goto error;
4004 implication->extension = map;
4005 implication->satisfied = satisfied;
4007 return implication;
4008 error:
4009 isl_map_free(map);
4010 return NULL;
4013 /* Add an implication on filter values equal to "satisfied"
4014 * with extension "map" to "scop".
4016 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
4017 __isl_take isl_map *map, int satisfied)
4019 isl_ctx *ctx;
4020 struct pet_implication *implication;
4021 struct pet_implication **implications;
4023 implication = new_implication(map, satisfied);
4024 if (!scop || !implication)
4025 goto error;
4027 ctx = isl_set_get_ctx(scop->context);
4028 implications = isl_realloc_array(ctx, scop->implications,
4029 struct pet_implication *,
4030 scop->n_implication + 1);
4031 if (!implications)
4032 goto error;
4033 scop->implications = implications;
4034 scop->implications[scop->n_implication] = implication;
4035 scop->n_implication++;
4037 return scop;
4038 error:
4039 pet_implication_free(implication);
4040 return pet_scop_free(scop);
4043 /* Given an access expression, check if it is data dependent.
4044 * If so, set *found and abort the search.
4046 static int is_data_dependent(struct pet_expr *expr, void *user)
4048 int *found = user;
4050 if (expr->n_arg) {
4051 *found = 1;
4052 return -1;
4055 return 0;
4058 /* Does "scop" contain any data dependent accesses?
4060 * Check the body of each statement for such accesses.
4062 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
4064 int i;
4065 int found = 0;
4067 if (!scop)
4068 return -1;
4070 for (i = 0; i < scop->n_stmt; ++i) {
4071 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
4072 &is_data_dependent, &found);
4073 if (r < 0 && !found)
4074 return -1;
4075 if (found)
4076 return found;
4079 return found;
4082 /* Does "scop" contain and data dependent conditions?
4084 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
4086 int i;
4088 if (!scop)
4089 return -1;
4091 for (i = 0; i < scop->n_stmt; ++i)
4092 if (scop->stmts[i]->n_arg > 0)
4093 return 1;
4095 return 0;
4098 /* Keep track of the "input" file inside the (extended) "scop".
4100 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
4102 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4104 if (!scop)
4105 return NULL;
4107 ext->input = input;
4109 return scop;
4112 /* Print the original code corresponding to "scop" to printer "p".
4114 * pet_scop_print_original can only be called from
4115 * a pet_transform_C_source callback. This means that the input
4116 * file is stored in the extended scop and that the printer prints
4117 * to a file.
4119 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
4120 __isl_take isl_printer *p)
4122 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4123 FILE *output;
4125 if (!scop || !p)
4126 return isl_printer_free(p);
4128 if (!ext->input)
4129 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
4130 "no input file stored in scop",
4131 return isl_printer_free(p));
4133 output = isl_printer_get_file(p);
4134 if (!output)
4135 return isl_printer_free(p);
4137 if (copy(ext->input, output, scop->start, scop->end) < 0)
4138 return isl_printer_free(p);
4140 return p;