pet_expr_from_index_and_depth: extract out extend_range
[pet.git] / scop.c
blobcbcaa518f3f93bbc606fcfcd3fe52756ab1fb633
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 /* Extend the range of "access" with "n" dimensions, retaining
183 * the tuple identifier on this range.
185 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
187 isl_id *id;
189 id = isl_map_get_tuple_id(access, isl_dim_out);
190 access = isl_map_add_dims(access, isl_dim_out, n);
191 access = isl_map_set_tuple_id(access, isl_dim_out, id);
193 return access;
196 /* Construct an access pet_expr from an index expression and
197 * the depth of the accessed array.
198 * By default, the access is considered to be a read access.
200 * If the number of indices is smaller than the depth of the array,
201 * then we assume that all elements of the remaining dimensions
202 * are accessed.
204 struct pet_expr *pet_expr_from_index_and_depth(
205 __isl_take isl_multi_pw_aff *index, int depth)
207 isl_map *access;
208 int dim;
210 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
211 if (!access)
212 goto error;
213 dim = isl_map_dim(access, isl_dim_out);
214 if (dim > depth)
215 isl_die(isl_map_get_ctx(access), isl_error_internal,
216 "number of indices greater than depth",
217 access = isl_map_free(access));
218 if (dim == depth)
219 return pet_expr_from_access_and_index(access, index);
221 access = extend_range(access, depth - dim);
223 return pet_expr_from_access_and_index(access, index);
224 error:
225 isl_multi_pw_aff_free(index);
226 return NULL;
229 /* Construct a pet_expr that kills the elements specified by
230 * the index expression "index" and the access relation "access".
232 struct pet_expr *pet_expr_kill_from_access_and_index(__isl_take isl_map *access,
233 __isl_take isl_multi_pw_aff *index)
235 isl_ctx *ctx;
236 struct pet_expr *expr;
238 if (!access || !index)
239 goto error;
241 ctx = isl_multi_pw_aff_get_ctx(index);
242 expr = pet_expr_from_access_and_index(access, index);
243 if (!expr)
244 return NULL;
245 expr->acc.read = 0;
246 return pet_expr_new_unary(ctx, pet_op_kill, expr);
247 error:
248 isl_map_free(access);
249 isl_multi_pw_aff_free(index);
250 return NULL;
253 /* Construct a unary pet_expr that performs "op" on "arg".
255 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
256 struct pet_expr *arg)
258 struct pet_expr *expr;
260 if (!arg)
261 goto error;
262 expr = isl_alloc_type(ctx, struct pet_expr);
263 if (!expr)
264 goto error;
266 expr->type = pet_expr_unary;
267 expr->op = op;
268 expr->n_arg = 1;
269 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
270 if (!expr->args)
271 goto error;
272 expr->args[pet_un_arg] = arg;
274 return expr;
275 error:
276 pet_expr_free(arg);
277 return NULL;
280 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
282 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
283 struct pet_expr *lhs, struct pet_expr *rhs)
285 struct pet_expr *expr;
287 if (!lhs || !rhs)
288 goto error;
289 expr = isl_alloc_type(ctx, struct pet_expr);
290 if (!expr)
291 goto error;
293 expr->type = pet_expr_binary;
294 expr->op = op;
295 expr->n_arg = 2;
296 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
297 if (!expr->args)
298 goto error;
299 expr->args[pet_bin_lhs] = lhs;
300 expr->args[pet_bin_rhs] = rhs;
302 return expr;
303 error:
304 pet_expr_free(lhs);
305 pet_expr_free(rhs);
306 return NULL;
309 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
311 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
312 struct pet_expr *lhs, struct pet_expr *rhs)
314 struct pet_expr *expr;
316 if (!cond || !lhs || !rhs)
317 goto error;
318 expr = isl_alloc_type(ctx, struct pet_expr);
319 if (!expr)
320 goto error;
322 expr->type = pet_expr_ternary;
323 expr->n_arg = 3;
324 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
325 if (!expr->args)
326 goto error;
327 expr->args[pet_ter_cond] = cond;
328 expr->args[pet_ter_true] = lhs;
329 expr->args[pet_ter_false] = rhs;
331 return expr;
332 error:
333 pet_expr_free(cond);
334 pet_expr_free(lhs);
335 pet_expr_free(rhs);
336 return NULL;
339 /* Construct a call pet_expr that calls function "name" with "n_arg"
340 * arguments. The caller is responsible for filling in the arguments.
342 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
343 unsigned n_arg)
345 struct pet_expr *expr;
347 expr = isl_alloc_type(ctx, struct pet_expr);
348 if (!expr)
349 return NULL;
351 expr->type = pet_expr_call;
352 expr->n_arg = n_arg;
353 expr->name = strdup(name);
354 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
355 if (!expr->name || !expr->args)
356 return pet_expr_free(expr);
358 return expr;
361 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
363 struct pet_expr *pet_expr_new_cast(isl_ctx *ctx, const char *type_name,
364 struct pet_expr *arg)
366 struct pet_expr *expr;
368 if (!arg)
369 return NULL;
371 expr = isl_alloc_type(ctx, struct pet_expr);
372 if (!expr)
373 goto error;
375 expr->type = pet_expr_cast;
376 expr->n_arg = 1;
377 expr->type_name = strdup(type_name);
378 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
379 if (!expr->type_name || !expr->args)
380 goto error;
382 expr->args[0] = arg;
384 return expr;
385 error:
386 pet_expr_free(arg);
387 pet_expr_free(expr);
388 return NULL;
391 /* Construct a pet_expr that represents the double "d".
393 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
395 struct pet_expr *expr;
397 expr = isl_calloc_type(ctx, struct pet_expr);
398 if (!expr)
399 return NULL;
401 expr->type = pet_expr_double;
402 expr->d.val = val;
403 expr->d.s = strdup(s);
404 if (!expr->d.s)
405 return pet_expr_free(expr);
407 return expr;
410 struct pet_expr *pet_expr_free(struct pet_expr *expr)
412 int i;
414 if (!expr)
415 return NULL;
417 for (i = 0; i < expr->n_arg; ++i)
418 pet_expr_free(expr->args[i]);
419 free(expr->args);
421 switch (expr->type) {
422 case pet_expr_access:
423 isl_id_free(expr->acc.ref_id);
424 isl_map_free(expr->acc.access);
425 isl_multi_pw_aff_free(expr->acc.index);
426 break;
427 case pet_expr_call:
428 free(expr->name);
429 break;
430 case pet_expr_cast:
431 free(expr->type_name);
432 break;
433 case pet_expr_double:
434 free(expr->d.s);
435 break;
436 case pet_expr_unary:
437 case pet_expr_binary:
438 case pet_expr_ternary:
439 break;
442 free(expr);
443 return NULL;
446 static void expr_dump(struct pet_expr *expr, int indent)
448 int i;
450 if (!expr)
451 return;
453 fprintf(stderr, "%*s", indent, "");
455 switch (expr->type) {
456 case pet_expr_double:
457 fprintf(stderr, "%s\n", expr->d.s);
458 break;
459 case pet_expr_access:
460 isl_id_dump(expr->acc.ref_id);
461 fprintf(stderr, "%*s", indent, "");
462 isl_map_dump(expr->acc.access);
463 fprintf(stderr, "%*s", indent, "");
464 isl_multi_pw_aff_dump(expr->acc.index);
465 fprintf(stderr, "%*sread: %d\n", indent + 2,
466 "", expr->acc.read);
467 fprintf(stderr, "%*swrite: %d\n", indent + 2,
468 "", expr->acc.write);
469 for (i = 0; i < expr->n_arg; ++i)
470 expr_dump(expr->args[i], indent + 2);
471 break;
472 case pet_expr_unary:
473 fprintf(stderr, "%s\n", op_str[expr->op]);
474 expr_dump(expr->args[pet_un_arg], indent + 2);
475 break;
476 case pet_expr_binary:
477 fprintf(stderr, "%s\n", op_str[expr->op]);
478 expr_dump(expr->args[pet_bin_lhs], indent + 2);
479 expr_dump(expr->args[pet_bin_rhs], indent + 2);
480 break;
481 case pet_expr_ternary:
482 fprintf(stderr, "?:\n");
483 expr_dump(expr->args[pet_ter_cond], indent + 2);
484 expr_dump(expr->args[pet_ter_true], indent + 2);
485 expr_dump(expr->args[pet_ter_false], indent + 2);
486 break;
487 case pet_expr_call:
488 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
489 for (i = 0; i < expr->n_arg; ++i)
490 expr_dump(expr->args[i], indent + 2);
491 break;
492 case pet_expr_cast:
493 fprintf(stderr, "(%s)\n", expr->type_name);
494 for (i = 0; i < expr->n_arg; ++i)
495 expr_dump(expr->args[i], indent + 2);
496 break;
500 void pet_expr_dump(struct pet_expr *expr)
502 expr_dump(expr, 0);
505 /* Does "expr" represent an access to an unnamed space, i.e.,
506 * does it represent an affine expression?
508 int pet_expr_is_affine(struct pet_expr *expr)
510 int has_id;
512 if (!expr)
513 return -1;
514 if (expr->type != pet_expr_access)
515 return 0;
517 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
518 if (has_id < 0)
519 return -1;
521 return !has_id;
524 /* Return the identifier of the array accessed by "expr".
526 __isl_give isl_id *pet_expr_access_get_id(struct pet_expr *expr)
528 if (!expr)
529 return NULL;
530 if (expr->type != pet_expr_access)
531 return NULL;
532 return isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
535 /* Align the parameters of expr->acc.index and expr->acc.access.
537 struct pet_expr *pet_expr_access_align_params(struct pet_expr *expr)
539 if (!expr)
540 return NULL;
541 if (expr->type != pet_expr_access)
542 return pet_expr_free(expr);
544 expr->acc.access = isl_map_align_params(expr->acc.access,
545 isl_multi_pw_aff_get_space(expr->acc.index));
546 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
547 isl_map_get_space(expr->acc.access));
548 if (!expr->acc.access || !expr->acc.index)
549 return pet_expr_free(expr);
551 return expr;
554 /* Does "expr" represent an access to a scalar, i.e., zero-dimensional array?
556 int pet_expr_is_scalar_access(struct pet_expr *expr)
558 if (!expr)
559 return -1;
560 if (expr->type != pet_expr_access)
561 return 0;
563 return isl_map_dim(expr->acc.access, isl_dim_out) == 0;
566 /* Return 1 if the two pet_exprs are equivalent.
568 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
570 int i;
572 if (!expr1 || !expr2)
573 return 0;
575 if (expr1->type != expr2->type)
576 return 0;
577 if (expr1->n_arg != expr2->n_arg)
578 return 0;
579 for (i = 0; i < expr1->n_arg; ++i)
580 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
581 return 0;
582 switch (expr1->type) {
583 case pet_expr_double:
584 if (strcmp(expr1->d.s, expr2->d.s))
585 return 0;
586 if (expr1->d.val != expr2->d.val)
587 return 0;
588 break;
589 case pet_expr_access:
590 if (expr1->acc.read != expr2->acc.read)
591 return 0;
592 if (expr1->acc.write != expr2->acc.write)
593 return 0;
594 if (expr1->acc.ref_id != expr2->acc.ref_id)
595 return 0;
596 if (!expr1->acc.access || !expr2->acc.access)
597 return 0;
598 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
599 return 0;
600 if (!expr1->acc.index || !expr2->acc.index)
601 return 0;
602 if (!isl_multi_pw_aff_plain_is_equal(expr1->acc.index,
603 expr2->acc.index))
604 return 0;
605 break;
606 case pet_expr_unary:
607 case pet_expr_binary:
608 case pet_expr_ternary:
609 if (expr1->op != expr2->op)
610 return 0;
611 break;
612 case pet_expr_call:
613 if (strcmp(expr1->name, expr2->name))
614 return 0;
615 break;
616 case pet_expr_cast:
617 if (strcmp(expr1->type_name, expr2->type_name))
618 return 0;
619 break;
622 return 1;
625 /* Add extra conditions on the parameters to all access relations in "expr".
627 * The conditions are not added to the index expression. Instead, they
628 * are used to try and simplifty the index expression.
630 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
631 __isl_take isl_set *cond)
633 int i;
635 if (!expr)
636 goto error;
638 for (i = 0; i < expr->n_arg; ++i) {
639 expr->args[i] = pet_expr_restrict(expr->args[i],
640 isl_set_copy(cond));
641 if (!expr->args[i])
642 goto error;
645 if (expr->type == pet_expr_access) {
646 expr->acc.access = isl_map_intersect_params(expr->acc.access,
647 isl_set_copy(cond));
648 expr->acc.index = isl_multi_pw_aff_gist_params(
649 expr->acc.index, isl_set_copy(cond));
650 if (!expr->acc.access || !expr->acc.index)
651 goto error;
654 isl_set_free(cond);
655 return expr;
656 error:
657 isl_set_free(cond);
658 return pet_expr_free(expr);
661 /* Modify all expressions of type pet_expr_access in "expr"
662 * by calling "fn" on them.
664 struct pet_expr *pet_expr_map_access(struct pet_expr *expr,
665 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
666 void *user)
668 int i;
670 if (!expr)
671 return NULL;
673 for (i = 0; i < expr->n_arg; ++i) {
674 expr->args[i] = pet_expr_map_access(expr->args[i], fn, user);
675 if (!expr->args[i])
676 return pet_expr_free(expr);
679 if (expr->type == pet_expr_access)
680 expr = fn(expr, user);
682 return expr;
685 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
687 * Return -1 on error (where fn return a negative value is treated as an error).
688 * Otherwise return 0.
690 int pet_expr_foreach_access_expr(struct pet_expr *expr,
691 int (*fn)(struct pet_expr *expr, void *user), void *user)
693 int i;
695 if (!expr)
696 return -1;
698 for (i = 0; i < expr->n_arg; ++i)
699 if (pet_expr_foreach_access_expr(expr->args[i], fn, user) < 0)
700 return -1;
702 if (expr->type == pet_expr_access)
703 return fn(expr, user);
705 return 0;
708 /* Modify the access relation and index expression
709 * of the given access expression
710 * based on the given iteration space transformation.
711 * In particular, precompose the access relation and index expression
712 * with the update function.
714 * If the access has any arguments then the domain of the access relation
715 * is a wrapped mapping from the iteration space to the space of
716 * argument values. We only need to change the domain of this wrapped
717 * mapping, so we extend the input transformation with an identity mapping
718 * on the space of argument values.
720 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
722 isl_multi_pw_aff *update = user;
723 isl_space *space;
725 update = isl_multi_pw_aff_copy(update);
727 space = isl_map_get_space(expr->acc.access);
728 space = isl_space_domain(space);
729 if (!isl_space_is_wrapping(space))
730 isl_space_free(space);
731 else {
732 isl_multi_pw_aff *id;
733 space = isl_space_unwrap(space);
734 space = isl_space_range(space);
735 space = isl_space_map_from_set(space);
736 id = isl_multi_pw_aff_identity(space);
737 update = isl_multi_pw_aff_product(update, id);
740 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
741 expr->acc.access,
742 isl_multi_pw_aff_copy(update));
743 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
744 expr->acc.index, update);
745 if (!expr->acc.access || !expr->acc.index)
746 return pet_expr_free(expr);
748 return expr;
751 /* Modify all access relations in "expr" by precomposing them with
752 * the given iteration space transformation.
754 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
755 __isl_take isl_multi_pw_aff *update)
757 expr = pet_expr_map_access(expr, &update_domain, update);
758 isl_multi_pw_aff_free(update);
759 return expr;
762 /* Construct a pet_stmt with given line number and statement
763 * number from a pet_expr.
764 * The initial iteration domain is the zero-dimensional universe.
765 * The name of the domain is given by "label" if it is non-NULL.
766 * Otherwise, the name is constructed as S_<id>.
767 * The domains of all access relations are modified to refer
768 * to the statement iteration domain.
770 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
771 __isl_take isl_id *label, int id, struct pet_expr *expr)
773 struct pet_stmt *stmt;
774 isl_space *dim;
775 isl_set *dom;
776 isl_map *sched;
777 isl_multi_pw_aff *add_name;
778 char name[50];
780 if (!expr)
781 goto error;
783 stmt = isl_calloc_type(ctx, struct pet_stmt);
784 if (!stmt)
785 goto error;
787 dim = isl_space_set_alloc(ctx, 0, 0);
788 if (label)
789 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
790 else {
791 snprintf(name, sizeof(name), "S_%d", id);
792 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
794 dom = isl_set_universe(isl_space_copy(dim));
795 sched = isl_map_from_domain(isl_set_copy(dom));
797 dim = isl_space_from_domain(dim);
798 add_name = isl_multi_pw_aff_zero(dim);
799 expr = expr_update_domain(expr, add_name);
801 stmt->line = line;
802 stmt->domain = dom;
803 stmt->schedule = sched;
804 stmt->body = expr;
806 if (!stmt->domain || !stmt->schedule || !stmt->body)
807 return pet_stmt_free(stmt);
809 return stmt;
810 error:
811 isl_id_free(label);
812 pet_expr_free(expr);
813 return NULL;
816 void *pet_stmt_free(struct pet_stmt *stmt)
818 int i;
820 if (!stmt)
821 return NULL;
823 isl_set_free(stmt->domain);
824 isl_map_free(stmt->schedule);
825 pet_expr_free(stmt->body);
827 for (i = 0; i < stmt->n_arg; ++i)
828 pet_expr_free(stmt->args[i]);
829 free(stmt->args);
831 free(stmt);
832 return NULL;
835 static void stmt_dump(struct pet_stmt *stmt, int indent)
837 int i;
839 if (!stmt)
840 return;
842 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
843 fprintf(stderr, "%*s", indent, "");
844 isl_set_dump(stmt->domain);
845 fprintf(stderr, "%*s", indent, "");
846 isl_map_dump(stmt->schedule);
847 expr_dump(stmt->body, indent);
848 for (i = 0; i < stmt->n_arg; ++i)
849 expr_dump(stmt->args[i], indent + 2);
852 void pet_stmt_dump(struct pet_stmt *stmt)
854 stmt_dump(stmt, 0);
857 /* Allocate a new pet_type with the given "name" and "definition".
859 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
860 const char *definition)
862 struct pet_type *type;
864 type = isl_alloc_type(ctx, struct pet_type);
865 if (!type)
866 return NULL;
868 type->name = strdup(name);
869 type->definition = strdup(definition);
871 if (!type->name || !type->definition)
872 return pet_type_free(type);
874 return type;
877 /* Free "type" and return NULL.
879 struct pet_type *pet_type_free(struct pet_type *type)
881 if (!type)
882 return NULL;
884 free(type->name);
885 free(type->definition);
887 free(type);
888 return NULL;
891 struct pet_array *pet_array_free(struct pet_array *array)
893 if (!array)
894 return NULL;
896 isl_set_free(array->context);
897 isl_set_free(array->extent);
898 isl_set_free(array->value_bounds);
899 free(array->element_type);
901 free(array);
902 return NULL;
905 void pet_array_dump(struct pet_array *array)
907 if (!array)
908 return;
910 isl_set_dump(array->context);
911 isl_set_dump(array->extent);
912 isl_set_dump(array->value_bounds);
913 fprintf(stderr, "%s %s\n", array->element_type,
914 array->live_out ? "live-out" : "");
917 /* Alloc a pet_scop structure, with extra room for information that
918 * is only used during parsing.
920 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
922 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
925 /* Construct a pet_scop with room for n statements.
927 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
929 isl_space *space;
930 struct pet_scop *scop;
932 scop = pet_scop_alloc(ctx);
933 if (!scop)
934 return NULL;
936 space = isl_space_params_alloc(ctx, 0);
937 scop->context = isl_set_universe(isl_space_copy(space));
938 scop->context_value = isl_set_universe(space);
939 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
940 if (!scop->context || !scop->stmts)
941 return pet_scop_free(scop);
943 scop->n_stmt = n;
945 return scop;
948 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
950 return scop_alloc(ctx, 0);
953 /* Update "context" with respect to the valid parameter values for "access".
955 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
956 __isl_take isl_set *context)
958 context = isl_set_intersect(context,
959 isl_map_params(isl_map_copy(access)));
960 return context;
963 /* Update "context" with respect to the valid parameter values for "expr".
965 * If "expr" represents a ternary operator, then a parameter value
966 * needs to be valid for the condition and for at least one of the
967 * remaining two arguments.
968 * If the condition is an affine expression, then we can be a bit more specific.
969 * The parameter then has to be valid for the second argument for
970 * non-zero accesses and valid for the third argument for zero accesses.
972 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
973 __isl_take isl_set *context)
975 int i;
977 if (expr->type == pet_expr_ternary) {
978 int is_aff;
979 isl_set *context1, *context2;
981 is_aff = pet_expr_is_affine(expr->args[0]);
982 if (is_aff < 0)
983 goto error;
985 context = expr_extract_context(expr->args[0], context);
986 context1 = expr_extract_context(expr->args[1],
987 isl_set_copy(context));
988 context2 = expr_extract_context(expr->args[2], context);
990 if (is_aff) {
991 isl_map *access;
992 isl_set *zero_set;
994 access = isl_map_copy(expr->args[0]->acc.access);
995 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
996 zero_set = isl_map_params(access);
997 context1 = isl_set_subtract(context1,
998 isl_set_copy(zero_set));
999 context2 = isl_set_intersect(context2, zero_set);
1002 context = isl_set_union(context1, context2);
1003 context = isl_set_coalesce(context);
1005 return context;
1008 for (i = 0; i < expr->n_arg; ++i)
1009 context = expr_extract_context(expr->args[i], context);
1011 if (expr->type == pet_expr_access)
1012 context = access_extract_context(expr->acc.access, context);
1014 return context;
1015 error:
1016 isl_set_free(context);
1017 return NULL;
1020 /* Update "context" with respect to the valid parameter values for "stmt".
1022 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
1023 __isl_take isl_set *context)
1025 int i;
1027 for (i = 0; i < stmt->n_arg; ++i)
1028 context = expr_extract_context(stmt->args[i], context);
1030 context = expr_extract_context(stmt->body, context);
1032 return context;
1035 /* Construct a pet_scop that contains the given pet_stmt.
1037 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
1039 struct pet_scop *scop;
1041 if (!stmt)
1042 return NULL;
1044 scop = scop_alloc(ctx, 1);
1045 if (!scop)
1046 goto error;
1048 scop->context = stmt_extract_context(stmt, scop->context);
1049 if (!scop->context)
1050 goto error;
1052 scop->stmts[0] = stmt;
1054 return scop;
1055 error:
1056 pet_stmt_free(stmt);
1057 pet_scop_free(scop);
1058 return NULL;
1061 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
1062 * does it represent an affine expression?
1064 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
1066 int has_id;
1068 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
1069 if (has_id < 0)
1070 return -1;
1072 return !has_id;
1075 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
1077 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
1078 __isl_take isl_set *dom)
1080 isl_pw_aff *pa;
1081 pa = isl_set_indicator_function(set);
1082 pa = isl_pw_aff_intersect_domain(pa, dom);
1083 return pa;
1086 /* Return "lhs || rhs", defined on the shared definition domain.
1088 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
1089 __isl_take isl_pw_aff *rhs)
1091 isl_set *cond;
1092 isl_set *dom;
1094 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
1095 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
1096 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
1097 isl_pw_aff_non_zero_set(rhs));
1098 cond = isl_set_coalesce(cond);
1099 return indicator_function(cond, dom);
1102 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
1103 * ext may be equal to either ext1 or ext2.
1105 * The two skips that need to be combined are assumed to be affine expressions.
1107 * We need to skip in ext if we need to skip in either ext1 or ext2.
1108 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
1110 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
1111 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
1112 enum pet_skip type)
1114 isl_pw_aff *skip, *skip1, *skip2;
1116 if (!ext)
1117 return NULL;
1118 if (!ext1->skip[type] && !ext2->skip[type])
1119 return ext;
1120 if (!ext1->skip[type]) {
1121 if (ext == ext2)
1122 return ext;
1123 ext->skip[type] = ext2->skip[type];
1124 ext2->skip[type] = NULL;
1125 return ext;
1127 if (!ext2->skip[type]) {
1128 if (ext == ext1)
1129 return ext;
1130 ext->skip[type] = ext1->skip[type];
1131 ext1->skip[type] = NULL;
1132 return ext;
1135 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
1136 !multi_pw_aff_is_affine(ext2->skip[type]))
1137 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
1138 isl_error_internal, "can only combine affine skips",
1139 goto error);
1141 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
1142 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
1143 skip = pw_aff_or(skip1, skip2);
1144 isl_multi_pw_aff_free(ext1->skip[type]);
1145 ext1->skip[type] = NULL;
1146 isl_multi_pw_aff_free(ext2->skip[type]);
1147 ext2->skip[type] = NULL;
1148 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1149 if (!ext->skip[type])
1150 goto error;
1152 return ext;
1153 error:
1154 pet_scop_free(&ext->scop);
1155 return NULL;
1158 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
1159 * where type takes on the values pet_skip_now and pet_skip_later.
1160 * scop may be equal to either scop1 or scop2.
1162 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
1163 struct pet_scop *scop1, struct pet_scop *scop2)
1165 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1166 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
1167 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
1169 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
1170 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
1171 return &ext->scop;
1174 /* Update scop->start and scop->end to include the region from "start"
1175 * to "end". In particular, if scop->end == 0, then "scop" does not
1176 * have any offset information yet and we simply take the information
1177 * from "start" and "end". Otherwise, we update the fields if the
1178 * region from "start" to "end" is not already included.
1180 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
1181 unsigned start, unsigned end)
1183 if (!scop)
1184 return NULL;
1185 if (scop->end == 0) {
1186 scop->start = start;
1187 scop->end = end;
1188 } else {
1189 if (start < scop->start)
1190 scop->start = start;
1191 if (end > scop->end)
1192 scop->end = end;
1195 return scop;
1198 /* Does "implication" appear in the list of implications of "scop"?
1200 static int is_known_implication(struct pet_scop *scop,
1201 struct pet_implication *implication)
1203 int i;
1205 for (i = 0; i < scop->n_implication; ++i) {
1206 struct pet_implication *pi = scop->implications[i];
1207 int equal;
1209 if (pi->satisfied != implication->satisfied)
1210 continue;
1211 equal = isl_map_is_equal(pi->extension, implication->extension);
1212 if (equal < 0)
1213 return -1;
1214 if (equal)
1215 return 1;
1218 return 0;
1221 /* Store the concatenation of the impliciations of "scop1" and "scop2"
1222 * in "scop", removing duplicates (i.e., implications in "scop2" that
1223 * already appear in "scop1").
1225 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
1226 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
1228 int i, j;
1230 if (!scop)
1231 return NULL;
1233 if (scop2->n_implication == 0) {
1234 scop->n_implication = scop1->n_implication;
1235 scop->implications = scop1->implications;
1236 scop1->n_implication = 0;
1237 scop1->implications = NULL;
1238 return scop;
1241 if (scop1->n_implication == 0) {
1242 scop->n_implication = scop2->n_implication;
1243 scop->implications = scop2->implications;
1244 scop2->n_implication = 0;
1245 scop2->implications = NULL;
1246 return scop;
1249 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1250 scop1->n_implication + scop2->n_implication);
1251 if (!scop->implications)
1252 return pet_scop_free(scop);
1254 for (i = 0; i < scop1->n_implication; ++i) {
1255 scop->implications[i] = scop1->implications[i];
1256 scop1->implications[i] = NULL;
1259 scop->n_implication = scop1->n_implication;
1260 j = scop1->n_implication;
1261 for (i = 0; i < scop2->n_implication; ++i) {
1262 int known;
1264 known = is_known_implication(scop, scop2->implications[i]);
1265 if (known < 0)
1266 return pet_scop_free(scop);
1267 if (known)
1268 continue;
1269 scop->implications[j++] = scop2->implications[i];
1270 scop2->implications[i] = NULL;
1272 scop->n_implication = j;
1274 return scop;
1277 /* Combine the offset information of "scop1" and "scop2" into "scop".
1279 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
1280 struct pet_scop *scop1, struct pet_scop *scop2)
1282 if (scop1->end)
1283 scop = pet_scop_update_start_end(scop,
1284 scop1->start, scop1->end);
1285 if (scop2->end)
1286 scop = pet_scop_update_start_end(scop,
1287 scop2->start, scop2->end);
1288 return scop;
1291 /* Construct a pet_scop that contains the offset information,
1292 * arrays, statements and skip information in "scop1" and "scop2".
1294 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1295 struct pet_scop *scop2)
1297 int i;
1298 struct pet_scop *scop = NULL;
1300 if (!scop1 || !scop2)
1301 goto error;
1303 if (scop1->n_stmt == 0) {
1304 scop2 = scop_combine_skips(scop2, scop1, scop2);
1305 pet_scop_free(scop1);
1306 return scop2;
1309 if (scop2->n_stmt == 0) {
1310 scop1 = scop_combine_skips(scop1, scop1, scop2);
1311 pet_scop_free(scop2);
1312 return scop1;
1315 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1316 if (!scop)
1317 goto error;
1319 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1320 scop1->n_array + scop2->n_array);
1321 if (!scop->arrays)
1322 goto error;
1323 scop->n_array = scop1->n_array + scop2->n_array;
1325 for (i = 0; i < scop1->n_stmt; ++i) {
1326 scop->stmts[i] = scop1->stmts[i];
1327 scop1->stmts[i] = NULL;
1330 for (i = 0; i < scop2->n_stmt; ++i) {
1331 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1332 scop2->stmts[i] = NULL;
1335 for (i = 0; i < scop1->n_array; ++i) {
1336 scop->arrays[i] = scop1->arrays[i];
1337 scop1->arrays[i] = NULL;
1340 for (i = 0; i < scop2->n_array; ++i) {
1341 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1342 scop2->arrays[i] = NULL;
1345 scop = scop_collect_implications(ctx, scop, scop1, scop2);
1346 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1347 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1348 scop = scop_combine_skips(scop, scop1, scop2);
1349 scop = scop_combine_start_end(scop, scop1, scop2);
1351 pet_scop_free(scop1);
1352 pet_scop_free(scop2);
1353 return scop;
1354 error:
1355 pet_scop_free(scop1);
1356 pet_scop_free(scop2);
1357 pet_scop_free(scop);
1358 return NULL;
1361 /* Apply the skip condition "skip" to "scop".
1362 * That is, make sure "scop" is not executed when the condition holds.
1364 * If "skip" is an affine expression, we add the conditions under
1365 * which the expression is zero to the iteration domains.
1366 * Otherwise, we add a filter on the variable attaining the value zero.
1368 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1369 __isl_take isl_multi_pw_aff *skip)
1371 isl_set *zero;
1372 isl_pw_aff *pa;
1373 int is_aff;
1375 if (!scop || !skip)
1376 goto error;
1378 is_aff = multi_pw_aff_is_affine(skip);
1379 if (is_aff < 0)
1380 goto error;
1382 if (!is_aff)
1383 return pet_scop_filter(scop, skip, 0);
1385 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1386 isl_multi_pw_aff_free(skip);
1387 zero = isl_set_params(isl_pw_aff_zero_set(pa));
1388 scop = pet_scop_restrict(scop, zero);
1390 return scop;
1391 error:
1392 isl_multi_pw_aff_free(skip);
1393 return pet_scop_free(scop);
1396 /* Construct a pet_scop that contains the arrays, statements and
1397 * skip information in "scop1" and "scop2", where the two scops
1398 * are executed "in sequence". That is, breaks and continues
1399 * in scop1 have an effect on scop2.
1401 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1402 struct pet_scop *scop2)
1404 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1405 scop2 = restrict_skip(scop2,
1406 pet_scop_get_skip(scop1, pet_skip_now));
1407 return pet_scop_add(ctx, scop1, scop2);
1410 /* Construct a pet_scop that contains the arrays, statements and
1411 * skip information in "scop1" and "scop2", where the two scops
1412 * are executed "in parallel". That is, any break or continue
1413 * in scop1 has no effect on scop2.
1415 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1416 struct pet_scop *scop2)
1418 return pet_scop_add(ctx, scop1, scop2);
1421 void *pet_implication_free(struct pet_implication *implication)
1423 int i;
1425 if (!implication)
1426 return NULL;
1428 isl_map_free(implication->extension);
1430 free(implication);
1431 return NULL;
1434 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1436 int i;
1437 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1439 if (!scop)
1440 return NULL;
1441 isl_set_free(scop->context);
1442 isl_set_free(scop->context_value);
1443 if (scop->types)
1444 for (i = 0; i < scop->n_type; ++i)
1445 pet_type_free(scop->types[i]);
1446 free(scop->types);
1447 if (scop->arrays)
1448 for (i = 0; i < scop->n_array; ++i)
1449 pet_array_free(scop->arrays[i]);
1450 free(scop->arrays);
1451 if (scop->stmts)
1452 for (i = 0; i < scop->n_stmt; ++i)
1453 pet_stmt_free(scop->stmts[i]);
1454 free(scop->stmts);
1455 if (scop->implications)
1456 for (i = 0; i < scop->n_implication; ++i)
1457 pet_implication_free(scop->implications[i]);
1458 free(scop->implications);
1459 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1460 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1461 free(scop);
1462 return NULL;
1465 void pet_type_dump(struct pet_type *type)
1467 if (!type)
1468 return;
1470 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1473 void pet_implication_dump(struct pet_implication *implication)
1475 if (!implication)
1476 return;
1478 fprintf(stderr, "%d\n", implication->satisfied);
1479 isl_map_dump(implication->extension);
1482 void pet_scop_dump(struct pet_scop *scop)
1484 int i;
1485 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1487 if (!scop)
1488 return;
1490 isl_set_dump(scop->context);
1491 isl_set_dump(scop->context_value);
1492 for (i = 0; i < scop->n_type; ++i)
1493 pet_type_dump(scop->types[i]);
1494 for (i = 0; i < scop->n_array; ++i)
1495 pet_array_dump(scop->arrays[i]);
1496 for (i = 0; i < scop->n_stmt; ++i)
1497 pet_stmt_dump(scop->stmts[i]);
1498 for (i = 0; i < scop->n_implication; ++i)
1499 pet_implication_dump(scop->implications[i]);
1501 if (ext->skip[0]) {
1502 fprintf(stderr, "skip\n");
1503 isl_multi_pw_aff_dump(ext->skip[0]);
1504 isl_multi_pw_aff_dump(ext->skip[1]);
1508 /* Return 1 if the two pet_arrays are equivalent.
1510 * We don't compare element_size as this may be target dependent.
1512 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1514 if (!array1 || !array2)
1515 return 0;
1517 if (!isl_set_is_equal(array1->context, array2->context))
1518 return 0;
1519 if (!isl_set_is_equal(array1->extent, array2->extent))
1520 return 0;
1521 if (!!array1->value_bounds != !!array2->value_bounds)
1522 return 0;
1523 if (array1->value_bounds &&
1524 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1525 return 0;
1526 if (strcmp(array1->element_type, array2->element_type))
1527 return 0;
1528 if (array1->live_out != array2->live_out)
1529 return 0;
1530 if (array1->uniquely_defined != array2->uniquely_defined)
1531 return 0;
1532 if (array1->declared != array2->declared)
1533 return 0;
1534 if (array1->exposed != array2->exposed)
1535 return 0;
1537 return 1;
1540 /* Return 1 if the two pet_stmts are equivalent.
1542 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1544 int i;
1546 if (!stmt1 || !stmt2)
1547 return 0;
1549 if (stmt1->line != stmt2->line)
1550 return 0;
1551 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1552 return 0;
1553 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1554 return 0;
1555 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1556 return 0;
1557 if (stmt1->n_arg != stmt2->n_arg)
1558 return 0;
1559 for (i = 0; i < stmt1->n_arg; ++i) {
1560 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1561 return 0;
1564 return 1;
1567 /* Return 1 if the two pet_types are equivalent.
1569 * We only compare the names of the types since the exact representation
1570 * of the definition may depend on the version of clang being used.
1572 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1574 if (!type1 || !type2)
1575 return 0;
1577 if (strcmp(type1->name, type2->name))
1578 return 0;
1580 return 1;
1583 /* Return 1 if the two pet_implications are equivalent.
1585 int pet_implication_is_equal(struct pet_implication *implication1,
1586 struct pet_implication *implication2)
1588 if (!implication1 || !implication2)
1589 return 0;
1591 if (implication1->satisfied != implication2->satisfied)
1592 return 0;
1593 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1594 return 0;
1596 return 1;
1599 /* Return 1 if the two pet_scops are equivalent.
1601 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1603 int i;
1605 if (!scop1 || !scop2)
1606 return 0;
1608 if (!isl_set_is_equal(scop1->context, scop2->context))
1609 return 0;
1610 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1611 return 0;
1613 if (scop1->n_type != scop2->n_type)
1614 return 0;
1615 for (i = 0; i < scop1->n_type; ++i)
1616 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1617 return 0;
1619 if (scop1->n_array != scop2->n_array)
1620 return 0;
1621 for (i = 0; i < scop1->n_array; ++i)
1622 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1623 return 0;
1625 if (scop1->n_stmt != scop2->n_stmt)
1626 return 0;
1627 for (i = 0; i < scop1->n_stmt; ++i)
1628 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1629 return 0;
1631 if (scop1->n_implication != scop2->n_implication)
1632 return 0;
1633 for (i = 0; i < scop1->n_implication; ++i)
1634 if (!pet_implication_is_equal(scop1->implications[i],
1635 scop2->implications[i]))
1636 return 0;
1638 return 1;
1641 /* Prefix the schedule of "stmt" with an extra dimension with constant
1642 * value "pos".
1644 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1646 if (!stmt)
1647 return NULL;
1649 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1650 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1651 if (!stmt->schedule)
1652 return pet_stmt_free(stmt);
1654 return stmt;
1657 /* Prefix the schedules of all statements in "scop" with an extra
1658 * dimension with constant value "pos".
1660 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1662 int i;
1664 if (!scop)
1665 return NULL;
1667 for (i = 0; i < scop->n_stmt; ++i) {
1668 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1669 if (!scop->stmts[i])
1670 return pet_scop_free(scop);
1673 return scop;
1676 /* Given a set with a parameter at "param_pos" that refers to the
1677 * iterator, "move" the iterator to the first set dimension.
1678 * That is, essentially equate the parameter to the first set dimension
1679 * and then project it out.
1681 * The first set dimension may however refer to a virtual iterator,
1682 * while the parameter refers to the "real" iterator.
1683 * We therefore need to take into account the affine expression "iv_map", which
1684 * expresses the real iterator in terms of the virtual iterator.
1685 * In particular, we equate the set dimension to the input of the map
1686 * and the parameter to the output of the map and then project out
1687 * everything we don't need anymore.
1689 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1690 int param_pos, __isl_take isl_aff *iv_map)
1692 isl_map *map, *map2;
1693 map = isl_map_from_domain(set);
1694 map = isl_map_add_dims(map, isl_dim_out, 1);
1695 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1696 map2 = isl_map_from_aff(iv_map);
1697 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1698 map = isl_map_apply_range(map, map2);
1699 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1700 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1701 return isl_map_domain(map);
1704 /* Data used in embed_access.
1705 * extend adds an iterator to the iteration domain (through precomposition).
1706 * iv_map expresses the real iterator in terms of the virtual iterator
1707 * var_id represents the induction variable of the corresponding loop
1709 struct pet_embed_access {
1710 isl_multi_pw_aff *extend;
1711 isl_aff *iv_map;
1712 isl_id *var_id;
1715 /* Given an index expression, return an expression for the outer iterator.
1717 static __isl_give isl_aff *index_outer_iterator(
1718 __isl_take isl_multi_pw_aff *index)
1720 isl_space *space;
1721 isl_local_space *ls;
1723 space = isl_multi_pw_aff_get_domain_space(index);
1724 isl_multi_pw_aff_free(index);
1726 ls = isl_local_space_from_space(space);
1727 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1730 /* Replace an index expression that references the new (outer) iterator variable
1731 * by one that references the corresponding (real) iterator.
1733 * The input index expression is of the form
1735 * { S[i',...] -> i[] }
1737 * where i' refers to the virtual iterator.
1739 * iv_map is of the form
1741 * { [i'] -> [i] }
1743 * Return the index expression
1745 * { S[i',...] -> [i] }
1747 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1748 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1750 isl_space *space;
1751 isl_aff *aff;
1753 aff = index_outer_iterator(index);
1754 space = isl_aff_get_space(aff);
1755 iv_map = isl_aff_align_params(iv_map, space);
1756 aff = isl_aff_pullback_aff(iv_map, aff);
1758 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1761 /* Given an index expression "index" that refers to the (real) iterator
1762 * through the parameter at position "pos", plug in "iv_map", expressing
1763 * the real iterator in terms of the virtual (outer) iterator.
1765 * In particular, the index expression is of the form
1767 * [..., i, ...] -> { S[i',...] -> ... i ... }
1769 * where i refers to the real iterator and i' refers to the virtual iterator.
1771 * iv_map is of the form
1773 * { [i'] -> [i] }
1775 * Return the index expression
1777 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1780 * We first move the parameter to the input
1782 * [..., ...] -> { [i, i',...] -> ... i ... }
1784 * and construct
1786 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1788 * and then combine the two to obtain the desired result.
1790 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1791 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1793 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1794 isl_multi_aff *ma;
1796 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1797 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1798 isl_dim_param, pos, 1);
1800 space = isl_space_map_from_set(space);
1801 ma = isl_multi_aff_identity(isl_space_copy(space));
1802 iv_map = isl_aff_align_params(iv_map, space);
1803 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1804 ma = isl_multi_aff_flat_range_product(
1805 isl_multi_aff_from_aff(iv_map), ma);
1806 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1808 return index;
1811 /* Embed the given index expression in an extra outer loop.
1812 * The domain of the index expression has already been updated.
1814 * If the access refers to the induction variable, then it is
1815 * turned into an access to the set of integers with index (and value)
1816 * equal to the induction variable.
1818 * If the accessed array is a virtual array (with user
1819 * pointer equal to NULL), as created by create_test_index,
1820 * then it is extended along with the domain of the index expression.
1822 static __isl_give isl_multi_pw_aff *embed_index_expression(
1823 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1825 isl_id *array_id = NULL;
1826 int pos;
1828 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1829 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1830 if (array_id == data->var_id) {
1831 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1832 } else if (array_id && !isl_id_get_user(array_id)) {
1833 isl_aff *aff;
1834 isl_multi_pw_aff *mpa;
1836 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1837 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1838 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1839 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1840 isl_id_copy(array_id));
1842 isl_id_free(array_id);
1844 pos = isl_multi_pw_aff_find_dim_by_id(index,
1845 isl_dim_param, data->var_id);
1846 if (pos >= 0)
1847 index = index_internalize_iv(index, pos,
1848 isl_aff_copy(data->iv_map));
1849 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1850 isl_id_copy(data->var_id));
1852 return index;
1855 /* Embed the given access relation in an extra outer loop.
1856 * The domain of the access relation has already been updated.
1858 * If the access refers to the induction variable, then it is
1859 * turned into an access to the set of integers with index (and value)
1860 * equal to the induction variable.
1862 * If the induction variable appears in the constraints (as a parameter),
1863 * then the parameter is equated to the newly introduced iteration
1864 * domain dimension and subsequently projected out.
1866 * Similarly, if the accessed array is a virtual array (with user
1867 * pointer equal to NULL), as created by create_test_index,
1868 * then it is extended along with the domain of the access.
1870 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
1871 struct pet_embed_access *data)
1873 isl_id *array_id = NULL;
1874 int pos;
1876 if (isl_map_has_tuple_id(access, isl_dim_out))
1877 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1878 if (array_id == data->var_id ||
1879 (array_id && !isl_id_get_user(array_id))) {
1880 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1881 access = isl_map_equate(access,
1882 isl_dim_in, 0, isl_dim_out, 0);
1883 if (array_id == data->var_id)
1884 access = isl_map_apply_range(access,
1885 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1886 else
1887 access = isl_map_set_tuple_id(access, isl_dim_out,
1888 isl_id_copy(array_id));
1890 isl_id_free(array_id);
1892 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1893 if (pos >= 0) {
1894 isl_set *set = isl_map_wrap(access);
1895 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1896 access = isl_set_unwrap(set);
1898 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1899 isl_id_copy(data->var_id));
1901 return access;
1904 /* Given an access expression, embed the associated access relation and
1905 * index expression in an extra outer loop.
1907 * We first update the domains to insert the extra dimension and
1908 * then update the access relation and index expression to take
1909 * into account the mapping "iv_map" from virtual iterator
1910 * to real iterator.
1912 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
1914 int dim;
1915 struct pet_embed_access *data = user;
1917 expr = update_domain(expr, data->extend);
1918 if (!expr)
1919 return NULL;
1921 expr->acc.access = embed_access_relation(expr->acc.access, data);
1922 expr->acc.index = embed_index_expression(expr->acc.index, data);
1923 if (!expr->acc.access || !expr->acc.index)
1924 return pet_expr_free(expr);
1926 return expr;
1929 /* Embed all access subexpressions of "expr" in an extra loop.
1930 * "extend" inserts an outer loop iterator in the iteration domains
1931 * (through precomposition).
1932 * "iv_map" expresses the real iterator in terms of the virtual iterator
1933 * "var_id" represents the induction variable.
1935 static struct pet_expr *expr_embed(struct pet_expr *expr,
1936 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
1937 __isl_keep isl_id *var_id)
1939 struct pet_embed_access data =
1940 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1942 expr = pet_expr_map_access(expr, &embed_access, &data);
1943 isl_aff_free(iv_map);
1944 isl_multi_pw_aff_free(extend);
1945 return expr;
1948 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1949 * "dom" and schedule "sched". "var_id" represents the induction variable
1950 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1951 * That is, it expresses the iterator that some of the parameters in "stmt"
1952 * may refer to in terms of the iterator used in "dom" and
1953 * the domain of "sched".
1955 * The iteration domain and schedule of the statement are updated
1956 * according to the iteration domain and schedule of the new loop.
1957 * If stmt->domain is a wrapped map, then the iteration domain
1958 * is the domain of this map, so we need to be careful to adjust
1959 * this domain.
1961 * If the induction variable appears in the constraints (as a parameter)
1962 * of the current iteration domain or the schedule of the statement,
1963 * then the parameter is equated to the newly introduced iteration
1964 * domain dimension and subsequently projected out.
1966 * Finally, all access relations are updated based on the extra loop.
1968 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1969 __isl_take isl_set *dom, __isl_take isl_map *sched,
1970 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1972 int i;
1973 int pos;
1974 isl_id *stmt_id;
1975 isl_space *dim;
1976 isl_multi_pw_aff *extend;
1978 if (!stmt)
1979 goto error;
1981 if (isl_set_is_wrapping(stmt->domain)) {
1982 isl_map *map;
1983 isl_map *ext;
1984 isl_space *ran_dim;
1986 map = isl_set_unwrap(stmt->domain);
1987 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1988 ran_dim = isl_space_range(isl_map_get_space(map));
1989 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1990 isl_set_universe(ran_dim));
1991 map = isl_map_flat_domain_product(ext, map);
1992 map = isl_map_set_tuple_id(map, isl_dim_in,
1993 isl_id_copy(stmt_id));
1994 dim = isl_space_domain(isl_map_get_space(map));
1995 stmt->domain = isl_map_wrap(map);
1996 } else {
1997 stmt_id = isl_set_get_tuple_id(stmt->domain);
1998 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1999 stmt->domain);
2000 stmt->domain = isl_set_set_tuple_id(stmt->domain,
2001 isl_id_copy(stmt_id));
2002 dim = isl_set_get_space(stmt->domain);
2005 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
2006 if (pos >= 0)
2007 stmt->domain = internalize_iv(stmt->domain, pos,
2008 isl_aff_copy(iv_map));
2010 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
2011 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
2012 isl_dim_in, stmt_id);
2014 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
2015 if (pos >= 0) {
2016 isl_set *set = isl_map_wrap(stmt->schedule);
2017 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
2018 stmt->schedule = isl_set_unwrap(set);
2021 dim = isl_space_map_from_set(dim);
2022 extend = isl_multi_pw_aff_identity(dim);
2023 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
2024 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
2025 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
2026 for (i = 0; i < stmt->n_arg; ++i)
2027 stmt->args[i] = expr_embed(stmt->args[i],
2028 isl_multi_pw_aff_copy(extend),
2029 isl_aff_copy(iv_map), var_id);
2030 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
2032 isl_set_free(dom);
2033 isl_id_free(var_id);
2035 for (i = 0; i < stmt->n_arg; ++i)
2036 if (!stmt->args[i])
2037 return pet_stmt_free(stmt);
2038 if (!stmt->domain || !stmt->schedule || !stmt->body)
2039 return pet_stmt_free(stmt);
2040 return stmt;
2041 error:
2042 isl_set_free(dom);
2043 isl_map_free(sched);
2044 isl_aff_free(iv_map);
2045 isl_id_free(var_id);
2046 return NULL;
2049 /* Embed the given pet_array in an extra outer loop with iteration domain
2050 * "dom".
2051 * This embedding only has an effect on virtual arrays (those with
2052 * user pointer equal to NULL), which need to be extended along with
2053 * the iteration domain.
2055 static struct pet_array *pet_array_embed(struct pet_array *array,
2056 __isl_take isl_set *dom)
2058 isl_id *array_id = NULL;
2060 if (!array)
2061 goto error;
2063 if (isl_set_has_tuple_id(array->extent))
2064 array_id = isl_set_get_tuple_id(array->extent);
2066 if (array_id && !isl_id_get_user(array_id)) {
2067 array->extent = isl_set_flat_product(dom, array->extent);
2068 array->extent = isl_set_set_tuple_id(array->extent, array_id);
2069 if (!array->extent)
2070 return pet_array_free(array);
2071 } else {
2072 isl_set_free(dom);
2073 isl_id_free(array_id);
2076 return array;
2077 error:
2078 isl_set_free(dom);
2079 return NULL;
2082 /* Project out all unnamed parameters from "set" and return the result.
2084 static __isl_give isl_set *set_project_out_unnamed_params(
2085 __isl_take isl_set *set)
2087 int i, n;
2089 n = isl_set_dim(set, isl_dim_param);
2090 for (i = n - 1; i >= 0; --i) {
2091 if (isl_set_has_dim_name(set, isl_dim_param, i))
2092 continue;
2093 set = isl_set_project_out(set, isl_dim_param, i, 1);
2096 return set;
2099 /* Update the context with respect to an embedding into a loop
2100 * with iteration domain "dom" and induction variable "id".
2101 * "iv_map" expresses the real iterator (parameter "id") in terms
2102 * of a possibly virtual iterator (used in "dom").
2104 * If the current context is independent of "id", we don't need
2105 * to do anything.
2106 * Otherwise, a parameter value is invalid for the embedding if
2107 * any of the corresponding iterator values is invalid.
2108 * That is, a parameter value is valid only if all the corresponding
2109 * iterator values are valid.
2110 * We therefore compute the set of parameters
2112 * forall i in dom : valid (i)
2114 * or
2116 * not exists i in dom : not valid(i)
2118 * i.e.,
2120 * not exists i in dom \ valid(i)
2122 * Before we subtract valid(i) from dom, we first need to substitute
2123 * the real iterator for the virtual iterator.
2125 * If there are any unnamed parameters in "dom", then we consider
2126 * a parameter value to be valid if it is valid for any value of those
2127 * unnamed parameters. They are therefore projected out at the end.
2129 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
2130 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
2131 __isl_keep isl_id *id)
2133 int pos;
2134 isl_multi_aff *ma;
2136 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
2137 if (pos < 0)
2138 return context;
2140 context = isl_set_from_params(context);
2141 context = isl_set_add_dims(context, isl_dim_set, 1);
2142 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
2143 context = isl_set_project_out(context, isl_dim_param, pos, 1);
2144 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
2145 context = isl_set_preimage_multi_aff(context, ma);
2146 context = isl_set_subtract(isl_set_copy(dom), context);
2147 context = isl_set_params(context);
2148 context = isl_set_complement(context);
2149 context = set_project_out_unnamed_params(context);
2150 return context;
2153 /* Update the implication with respect to an embedding into a loop
2154 * with iteration domain "dom".
2156 * Since embed_access extends virtual arrays along with the domain
2157 * of the access, we need to do the same with domain and range
2158 * of the implication. Since the original implication is only valid
2159 * within a given iteration of the loop, the extended implication
2160 * maps the extra array dimension corresponding to the extra loop
2161 * to itself.
2163 static struct pet_implication *pet_implication_embed(
2164 struct pet_implication *implication, __isl_take isl_set *dom)
2166 isl_id *id;
2167 isl_map *map;
2169 if (!implication)
2170 goto error;
2172 map = isl_set_identity(dom);
2173 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
2174 map = isl_map_flat_product(map, implication->extension);
2175 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
2176 map = isl_map_set_tuple_id(map, isl_dim_out, id);
2177 implication->extension = map;
2178 if (!implication->extension)
2179 return pet_implication_free(implication);
2181 return implication;
2182 error:
2183 isl_set_free(dom);
2184 return NULL;
2187 /* Embed all statements and arrays in "scop" in an extra outer loop
2188 * with iteration domain "dom" and schedule "sched".
2189 * "id" represents the induction variable of the loop.
2190 * "iv_map" maps a possibly virtual iterator to the real iterator.
2191 * That is, it expresses the iterator that some of the parameters in "scop"
2192 * may refer to in terms of the iterator used in "dom" and
2193 * the domain of "sched".
2195 * Any skip conditions within the loop have no effect outside of the loop.
2196 * The caller is responsible for making sure skip[pet_skip_later] has been
2197 * taken into account.
2199 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
2200 __isl_take isl_map *sched, __isl_take isl_aff *iv_map,
2201 __isl_take isl_id *id)
2203 int i;
2205 if (!scop)
2206 goto error;
2208 pet_scop_reset_skip(scop, pet_skip_now);
2209 pet_scop_reset_skip(scop, pet_skip_later);
2211 scop->context = context_embed(scop->context, dom, iv_map, id);
2212 if (!scop->context)
2213 goto error;
2215 for (i = 0; i < scop->n_stmt; ++i) {
2216 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
2217 isl_set_copy(dom), isl_map_copy(sched),
2218 isl_aff_copy(iv_map), isl_id_copy(id));
2219 if (!scop->stmts[i])
2220 goto error;
2223 for (i = 0; i < scop->n_array; ++i) {
2224 scop->arrays[i] = pet_array_embed(scop->arrays[i],
2225 isl_set_copy(dom));
2226 if (!scop->arrays[i])
2227 goto error;
2230 for (i = 0; i < scop->n_implication; ++i) {
2231 scop->implications[i] =
2232 pet_implication_embed(scop->implications[i],
2233 isl_set_copy(dom));
2234 if (!scop->implications[i])
2235 goto error;
2238 isl_set_free(dom);
2239 isl_map_free(sched);
2240 isl_aff_free(iv_map);
2241 isl_id_free(id);
2242 return scop;
2243 error:
2244 isl_set_free(dom);
2245 isl_map_free(sched);
2246 isl_aff_free(iv_map);
2247 isl_id_free(id);
2248 return pet_scop_free(scop);
2251 /* Add extra conditions on the parameters to iteration domain of "stmt".
2253 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
2254 __isl_take isl_set *cond)
2256 if (!stmt)
2257 goto error;
2259 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
2261 return stmt;
2262 error:
2263 isl_set_free(cond);
2264 return pet_stmt_free(stmt);
2267 /* Add extra conditions to scop->skip[type].
2269 * The new skip condition only holds if it held before
2270 * and the condition is true. It does not hold if it did not hold
2271 * before or the condition is false.
2273 * The skip condition is assumed to be an affine expression.
2275 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
2276 enum pet_skip type, __isl_keep isl_set *cond)
2278 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2279 isl_pw_aff *skip;
2280 isl_set *dom;
2282 if (!scop)
2283 return NULL;
2284 if (!ext->skip[type])
2285 return scop;
2287 if (!multi_pw_aff_is_affine(ext->skip[type]))
2288 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
2289 isl_error_internal, "can only resrict affine skips",
2290 return pet_scop_free(scop));
2292 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2293 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
2294 cond = isl_set_copy(cond);
2295 cond = isl_set_from_params(cond);
2296 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
2297 skip = indicator_function(cond, dom);
2298 isl_multi_pw_aff_free(ext->skip[type]);
2299 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
2300 if (!ext->skip[type])
2301 return pet_scop_free(scop);
2303 return scop;
2306 /* Add extra conditions on the parameters to all iteration domains
2307 * and skip conditions.
2309 * A parameter value is valid for the result if it was valid
2310 * for the original scop and satisfies "cond" or if it does
2311 * not satisfy "cond" as in this case the scop is not executed
2312 * and the original constraints on the parameters are irrelevant.
2314 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
2315 __isl_take isl_set *cond)
2317 int i;
2319 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
2320 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
2322 if (!scop)
2323 goto error;
2325 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
2326 scop->context = isl_set_union(scop->context,
2327 isl_set_complement(isl_set_copy(cond)));
2328 scop->context = isl_set_coalesce(scop->context);
2329 scop->context = set_project_out_unnamed_params(scop->context);
2330 if (!scop->context)
2331 goto error;
2333 for (i = 0; i < scop->n_stmt; ++i) {
2334 scop->stmts[i] = stmt_restrict(scop->stmts[i],
2335 isl_set_copy(cond));
2336 if (!scop->stmts[i])
2337 goto error;
2340 isl_set_free(cond);
2341 return scop;
2342 error:
2343 isl_set_free(cond);
2344 return pet_scop_free(scop);
2347 /* Construct a function that (upon precomposition) inserts
2348 * a filter value with name "id" and value "satisfied"
2349 * in the list of filter values embedded in the set space "space".
2351 * If "space" does not contain any filter values yet, we first create
2352 * a function that inserts 0 filter values, i.e.,
2354 * [space -> []] -> space
2356 * We can now assume that space is of the form [dom -> [filters]]
2357 * We construct an identity mapping on dom and a mapping on filters
2358 * that (upon precomposition) inserts the new filter
2360 * dom -> dom
2361 * [satisfied, filters] -> [filters]
2363 * and then compute the cross product
2365 * [dom -> [satisfied, filters]] -> [dom -> [filters]]
2367 static __isl_give isl_pw_multi_aff *insert_filter_pma(
2368 __isl_take isl_space *space, __isl_take isl_id *id, int satisfied)
2370 isl_space *space2;
2371 isl_multi_aff *ma;
2372 isl_pw_multi_aff *pma0, *pma, *pma_dom, *pma_ran;
2373 isl_set *dom;
2375 if (isl_space_is_wrapping(space)) {
2376 space2 = isl_space_map_from_set(isl_space_copy(space));
2377 ma = isl_multi_aff_identity(space2);
2378 space = isl_space_unwrap(space);
2379 } else {
2380 space = isl_space_from_domain(space);
2381 ma = isl_multi_aff_domain_map(isl_space_copy(space));
2384 space2 = isl_space_domain(isl_space_copy(space));
2385 pma_dom = isl_pw_multi_aff_identity(isl_space_map_from_set(space2));
2386 space = isl_space_range(space);
2387 space = isl_space_insert_dims(space, isl_dim_set, 0, 1);
2388 pma_ran = isl_pw_multi_aff_project_out_map(space, isl_dim_set, 0, 1);
2389 pma_ran = isl_pw_multi_aff_set_dim_id(pma_ran, isl_dim_in, 0, id);
2390 pma_ran = isl_pw_multi_aff_fix_si(pma_ran, isl_dim_in, 0, satisfied);
2391 pma = isl_pw_multi_aff_product(pma_dom, pma_ran);
2393 pma0 = isl_pw_multi_aff_from_multi_aff(ma);
2394 pma = isl_pw_multi_aff_pullback_pw_multi_aff(pma0, pma);
2396 return pma;
2399 /* Insert an argument expression corresponding to "test" in front
2400 * of the list of arguments described by *n_arg and *args.
2402 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
2403 __isl_keep isl_multi_pw_aff *test)
2405 int i;
2406 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
2408 if (!test)
2409 return -1;
2411 if (!*args) {
2412 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
2413 if (!*args)
2414 return -1;
2415 } else {
2416 struct pet_expr **ext;
2417 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
2418 if (!ext)
2419 return -1;
2420 for (i = 0; i < *n_arg; ++i)
2421 ext[1 + i] = (*args)[i];
2422 free(*args);
2423 *args = ext;
2425 (*n_arg)++;
2426 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
2427 if (!(*args)[0])
2428 return -1;
2430 return 0;
2433 /* Make the expression "expr" depend on the value of "test"
2434 * being equal to "satisfied".
2436 * If "test" is an affine expression, we simply add the conditions
2437 * on the expression having the value "satisfied" to all access relations
2438 * and index expressions.
2440 * Otherwise, we add a filter to "expr" (which is then assumed to be
2441 * an access expression) corresponding to "test" being equal to "satisfied".
2443 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
2444 __isl_take isl_multi_pw_aff *test, int satisfied)
2446 isl_id *id;
2447 isl_ctx *ctx;
2448 isl_space *space;
2449 isl_pw_multi_aff *pma;
2451 if (!expr || !test)
2452 goto error;
2454 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
2455 isl_pw_aff *pa;
2456 isl_set *cond;
2458 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
2459 isl_multi_pw_aff_free(test);
2460 if (satisfied)
2461 cond = isl_pw_aff_non_zero_set(pa);
2462 else
2463 cond = isl_pw_aff_zero_set(pa);
2464 return pet_expr_restrict(expr, isl_set_params(cond));
2467 ctx = isl_multi_pw_aff_get_ctx(test);
2468 if (expr->type != pet_expr_access)
2469 isl_die(ctx, isl_error_invalid,
2470 "can only filter access expressions", goto error);
2472 space = isl_space_domain(isl_map_get_space(expr->acc.access));
2473 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2474 pma = insert_filter_pma(space, id, satisfied);
2476 expr->acc.access = isl_map_preimage_domain_pw_multi_aff(
2477 expr->acc.access,
2478 isl_pw_multi_aff_copy(pma));
2479 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
2480 expr->acc.index, pma);
2481 if (!expr->acc.access || !expr->acc.index)
2482 goto error;
2484 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
2485 goto error;
2487 isl_multi_pw_aff_free(test);
2488 return expr;
2489 error:
2490 isl_multi_pw_aff_free(test);
2491 return pet_expr_free(expr);
2494 /* Look through the applications in "scop" for any that can be
2495 * applied to the filter expressed by "map" and "satisified".
2496 * If there is any, then apply it to "map" and return the result.
2497 * Otherwise, return "map".
2498 * "id" is the identifier of the virtual array.
2500 * We only introduce at most one implication for any given virtual array,
2501 * so we can apply the implication and return as soon as we find one.
2503 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
2504 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
2506 int i;
2508 for (i = 0; i < scop->n_implication; ++i) {
2509 struct pet_implication *pi = scop->implications[i];
2510 isl_id *pi_id;
2512 if (pi->satisfied != satisfied)
2513 continue;
2514 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
2515 isl_id_free(pi_id);
2516 if (pi_id != id)
2517 continue;
2519 return isl_map_apply_range(map, isl_map_copy(pi->extension));
2522 return map;
2525 /* Is the filter expressed by "test" and "satisfied" implied
2526 * by filter "pos" on "domain", with filter "expr", taking into
2527 * account the implications of "scop"?
2529 * For filter on domain implying that expressed by "test" and "satisfied",
2530 * the filter needs to be an access to the same (virtual) array as "test" and
2531 * the filter value needs to be equal to "satisfied".
2532 * Moreover, the filter access relation, possibly extended by
2533 * the implications in "scop" needs to contain "test".
2535 static int implies_filter(struct pet_scop *scop,
2536 __isl_keep isl_map *domain, int pos, struct pet_expr *expr,
2537 __isl_keep isl_map *test, int satisfied)
2539 isl_id *test_id, *arg_id;
2540 isl_val *val;
2541 int is_int;
2542 int s;
2543 int is_subset;
2544 isl_map *implied;
2546 if (expr->type != pet_expr_access)
2547 return 0;
2548 test_id = isl_map_get_tuple_id(test, isl_dim_out);
2549 arg_id = pet_expr_access_get_id(expr);
2550 isl_id_free(arg_id);
2551 isl_id_free(test_id);
2552 if (test_id != arg_id)
2553 return 0;
2554 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
2555 is_int = isl_val_is_int(val);
2556 if (is_int)
2557 s = isl_val_get_num_si(val);
2558 isl_val_free(val);
2559 if (!val)
2560 return -1;
2561 if (!is_int)
2562 return 0;
2563 if (s != satisfied)
2564 return 0;
2566 implied = isl_map_copy(expr->acc.access);
2567 implied = apply_implications(scop, implied, test_id, satisfied);
2568 is_subset = isl_map_is_subset(test, implied);
2569 isl_map_free(implied);
2571 return is_subset;
2574 /* Is the filter expressed by "test" and "satisfied" implied
2575 * by any of the filters on the domain of "stmt", taking into
2576 * account the implications of "scop"?
2578 static int filter_implied(struct pet_scop *scop,
2579 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
2581 int i;
2582 int implied;
2583 isl_id *test_id;
2584 isl_map *domain;
2585 isl_map *test_map;
2587 if (!scop || !stmt || !test)
2588 return -1;
2589 if (scop->n_implication == 0)
2590 return 0;
2591 if (stmt->n_arg == 0)
2592 return 0;
2594 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
2595 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
2597 implied = 0;
2598 for (i = 0; i < stmt->n_arg; ++i) {
2599 implied = implies_filter(scop, domain, i, stmt->args[i],
2600 test_map, satisfied);
2601 if (implied < 0 || implied)
2602 break;
2605 isl_map_free(test_map);
2606 isl_map_free(domain);
2607 return implied;
2610 /* Make the statement "stmt" depend on the value of "test"
2611 * being equal to "satisfied" by adjusting stmt->domain.
2613 * The domain of "test" corresponds to the (zero or more) outer dimensions
2614 * of the iteration domain.
2616 * We first extend "test" to apply to the entire iteration domain and
2617 * then check if the filter that we are about to add is implied
2618 * by any of the current filters, possibly taking into account
2619 * the implications in "scop". If so, we leave "stmt" untouched and return.
2621 * Otherwise, we insert an argument corresponding to a read to "test"
2622 * from the iteration domain of "stmt" in front of the list of arguments.
2623 * We also insert a corresponding output dimension in the wrapped
2624 * map contained in stmt->domain, with value set to "satisfied".
2626 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
2627 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
2629 int i;
2630 int implied;
2631 isl_id *id;
2632 isl_ctx *ctx;
2633 isl_pw_multi_aff *pma;
2634 isl_multi_aff *add_dom;
2635 isl_space *space;
2636 isl_local_space *ls;
2637 int n_test_dom;
2639 if (!stmt || !test)
2640 goto error;
2642 space = isl_set_get_space(stmt->domain);
2643 if (isl_space_is_wrapping(space))
2644 space = isl_space_domain(isl_space_unwrap(space));
2645 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
2646 space = isl_space_from_domain(space);
2647 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
2648 add_dom = isl_multi_aff_zero(isl_space_copy(space));
2649 ls = isl_local_space_from_space(isl_space_domain(space));
2650 for (i = 0; i < n_test_dom; ++i) {
2651 isl_aff *aff;
2652 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2653 isl_dim_set, i);
2654 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
2656 isl_local_space_free(ls);
2657 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
2659 implied = filter_implied(scop, stmt, test, satisfied);
2660 if (implied < 0)
2661 goto error;
2662 if (implied) {
2663 isl_multi_pw_aff_free(test);
2664 return stmt;
2667 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2668 pma = insert_filter_pma(isl_set_get_space(stmt->domain), id, satisfied);
2669 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
2671 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
2672 goto error;
2674 isl_multi_pw_aff_free(test);
2675 return stmt;
2676 error:
2677 isl_multi_pw_aff_free(test);
2678 return pet_stmt_free(stmt);
2681 /* Does "scop" have a skip condition of the given "type"?
2683 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2685 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2687 if (!scop)
2688 return -1;
2689 return ext->skip[type] != NULL;
2692 /* Does "scop" have a skip condition of the given "type" that
2693 * is an affine expression?
2695 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2697 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2699 if (!scop)
2700 return -1;
2701 if (!ext->skip[type])
2702 return 0;
2703 return multi_pw_aff_is_affine(ext->skip[type]);
2706 /* Does "scop" have a skip condition of the given "type" that
2707 * is not an affine expression?
2709 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2711 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2712 int aff;
2714 if (!scop)
2715 return -1;
2716 if (!ext->skip[type])
2717 return 0;
2718 aff = multi_pw_aff_is_affine(ext->skip[type]);
2719 if (aff < 0)
2720 return -1;
2721 return !aff;
2724 /* Does "scop" have a skip condition of the given "type" that
2725 * is affine and holds on the entire domain?
2727 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2729 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2730 isl_pw_aff *pa;
2731 isl_set *set;
2732 int is_aff;
2733 int is_univ;
2735 is_aff = pet_scop_has_affine_skip(scop, type);
2736 if (is_aff < 0 || !is_aff)
2737 return is_aff;
2739 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2740 set = isl_pw_aff_non_zero_set(pa);
2741 is_univ = isl_set_plain_is_universe(set);
2742 isl_set_free(set);
2744 return is_univ;
2747 /* Replace scop->skip[type] by "skip".
2749 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2750 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2752 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2754 if (!scop || !skip)
2755 goto error;
2757 isl_multi_pw_aff_free(ext->skip[type]);
2758 ext->skip[type] = skip;
2760 return scop;
2761 error:
2762 isl_multi_pw_aff_free(skip);
2763 return pet_scop_free(scop);
2766 /* Return a copy of scop->skip[type].
2768 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2769 enum pet_skip type)
2771 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2773 if (!scop)
2774 return NULL;
2776 return isl_multi_pw_aff_copy(ext->skip[type]);
2779 /* Assuming scop->skip[type] is an affine expression,
2780 * return the constraints on the parameters for which the skip condition
2781 * holds.
2783 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2784 enum pet_skip type)
2786 isl_multi_pw_aff *skip;
2787 isl_pw_aff *pa;
2789 skip = pet_scop_get_skip(scop, type);
2790 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2791 isl_multi_pw_aff_free(skip);
2792 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2795 /* Return the identifier of the variable that is accessed by
2796 * the skip condition of the given type.
2798 * The skip condition is assumed not to be an affine condition.
2800 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2801 enum pet_skip type)
2803 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2805 if (!scop)
2806 return NULL;
2808 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2811 /* Return an access pet_expr corresponding to the skip condition
2812 * of the given type.
2814 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2815 enum pet_skip type)
2817 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2820 /* Drop the the skip condition scop->skip[type].
2822 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2824 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2826 if (!scop)
2827 return;
2829 isl_multi_pw_aff_free(ext->skip[type]);
2830 ext->skip[type] = NULL;
2833 /* Make the skip condition (if any) depend on the value of "test" being
2834 * equal to "satisfied".
2836 * We only support the case where the original skip condition is universal,
2837 * i.e., where skipping is unconditional, and where satisfied == 1.
2838 * In this case, the skip condition is changed to skip only when
2839 * "test" is equal to one.
2841 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2842 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2844 int is_univ = 0;
2846 if (!scop)
2847 return NULL;
2848 if (!pet_scop_has_skip(scop, type))
2849 return scop;
2851 if (satisfied)
2852 is_univ = pet_scop_has_universal_skip(scop, type);
2853 if (is_univ < 0)
2854 return pet_scop_free(scop);
2855 if (satisfied && is_univ) {
2856 isl_space *space = isl_multi_pw_aff_get_space(test);
2857 isl_multi_pw_aff *skip;
2858 skip = isl_multi_pw_aff_zero(space);
2859 scop = pet_scop_set_skip(scop, type, skip);
2860 if (!scop)
2861 return NULL;
2862 } else {
2863 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2864 "skip expression cannot be filtered",
2865 return pet_scop_free(scop));
2868 return scop;
2871 /* Make all statements in "scop" depend on the value of "test"
2872 * being equal to "satisfied" by adjusting their domains.
2874 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2875 __isl_take isl_multi_pw_aff *test, int satisfied)
2877 int i;
2879 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2880 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2882 if (!scop || !test)
2883 goto error;
2885 for (i = 0; i < scop->n_stmt; ++i) {
2886 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2887 isl_multi_pw_aff_copy(test), satisfied);
2888 if (!scop->stmts[i])
2889 goto error;
2892 isl_multi_pw_aff_free(test);
2893 return scop;
2894 error:
2895 isl_multi_pw_aff_free(test);
2896 return pet_scop_free(scop);
2899 /* Add all parameters in "expr" to "dim" and return the result.
2901 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
2902 __isl_take isl_space *dim)
2904 int i;
2906 if (!expr)
2907 goto error;
2908 for (i = 0; i < expr->n_arg; ++i)
2910 dim = expr_collect_params(expr->args[i], dim);
2912 if (expr->type == pet_expr_access)
2913 dim = isl_space_align_params(dim,
2914 isl_map_get_space(expr->acc.access));
2916 return dim;
2917 error:
2918 pet_expr_free(expr);
2919 return isl_space_free(dim);
2922 /* Add all parameters in "stmt" to "dim" and return the result.
2924 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2925 __isl_take isl_space *dim)
2927 if (!stmt)
2928 goto error;
2930 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
2931 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
2932 dim = expr_collect_params(stmt->body, dim);
2934 return dim;
2935 error:
2936 isl_space_free(dim);
2937 return pet_stmt_free(stmt);
2940 /* Add all parameters in "array" to "dim" and return the result.
2942 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2943 __isl_take isl_space *dim)
2945 if (!array)
2946 goto error;
2948 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
2949 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
2951 return dim;
2952 error:
2953 pet_array_free(array);
2954 return isl_space_free(dim);
2957 /* Add all parameters in "scop" to "dim" and return the result.
2959 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2960 __isl_take isl_space *dim)
2962 int i;
2964 if (!scop)
2965 goto error;
2967 for (i = 0; i < scop->n_array; ++i)
2968 dim = array_collect_params(scop->arrays[i], dim);
2970 for (i = 0; i < scop->n_stmt; ++i)
2971 dim = stmt_collect_params(scop->stmts[i], dim);
2973 return dim;
2974 error:
2975 isl_space_free(dim);
2976 pet_scop_free(scop);
2977 return NULL;
2980 /* Add all parameters in "dim" to all access relations and index expressions
2981 * in "expr".
2983 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
2984 __isl_take isl_space *dim)
2986 int i;
2988 if (!expr)
2989 goto error;
2991 for (i = 0; i < expr->n_arg; ++i) {
2992 expr->args[i] =
2993 expr_propagate_params(expr->args[i],
2994 isl_space_copy(dim));
2995 if (!expr->args[i])
2996 goto error;
2999 if (expr->type == pet_expr_access) {
3000 expr->acc.access = isl_map_align_params(expr->acc.access,
3001 isl_space_copy(dim));
3002 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
3003 isl_space_copy(dim));
3004 if (!expr->acc.access || !expr->acc.index)
3005 goto error;
3008 isl_space_free(dim);
3009 return expr;
3010 error:
3011 isl_space_free(dim);
3012 return pet_expr_free(expr);
3015 /* Add all parameters in "dim" to the domain, schedule and
3016 * all access relations in "stmt".
3018 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
3019 __isl_take isl_space *dim)
3021 if (!stmt)
3022 goto error;
3024 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
3025 stmt->schedule = isl_map_align_params(stmt->schedule,
3026 isl_space_copy(dim));
3027 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
3029 if (!stmt->domain || !stmt->schedule || !stmt->body)
3030 goto error;
3032 isl_space_free(dim);
3033 return stmt;
3034 error:
3035 isl_space_free(dim);
3036 return pet_stmt_free(stmt);
3039 /* Add all parameters in "dim" to "array".
3041 static struct pet_array *array_propagate_params(struct pet_array *array,
3042 __isl_take isl_space *dim)
3044 if (!array)
3045 goto error;
3047 array->context = isl_set_align_params(array->context,
3048 isl_space_copy(dim));
3049 array->extent = isl_set_align_params(array->extent,
3050 isl_space_copy(dim));
3051 if (array->value_bounds) {
3052 array->value_bounds = isl_set_align_params(array->value_bounds,
3053 isl_space_copy(dim));
3054 if (!array->value_bounds)
3055 goto error;
3058 if (!array->context || !array->extent)
3059 goto error;
3061 isl_space_free(dim);
3062 return array;
3063 error:
3064 isl_space_free(dim);
3065 return pet_array_free(array);
3068 /* Add all parameters in "dim" to "scop".
3070 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
3071 __isl_take isl_space *dim)
3073 int i;
3075 if (!scop)
3076 goto error;
3078 for (i = 0; i < scop->n_array; ++i) {
3079 scop->arrays[i] = array_propagate_params(scop->arrays[i],
3080 isl_space_copy(dim));
3081 if (!scop->arrays[i])
3082 goto error;
3085 for (i = 0; i < scop->n_stmt; ++i) {
3086 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
3087 isl_space_copy(dim));
3088 if (!scop->stmts[i])
3089 goto error;
3092 isl_space_free(dim);
3093 return scop;
3094 error:
3095 isl_space_free(dim);
3096 return pet_scop_free(scop);
3099 /* Update all isl_sets and isl_maps in "scop" such that they all
3100 * have the same parameters.
3102 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
3104 isl_space *dim;
3106 if (!scop)
3107 return NULL;
3109 dim = isl_set_get_space(scop->context);
3110 dim = scop_collect_params(scop, dim);
3112 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
3113 scop = scop_propagate_params(scop, dim);
3115 return scop;
3118 /* Check if the given index expression accesses a (0D) array that corresponds
3119 * to one of the parameters in "dim". If so, replace the array access
3120 * by an access to the set of integers with as index (and value)
3121 * that parameter.
3123 static __isl_give isl_multi_pw_aff *index_detect_parameter(
3124 __isl_take isl_multi_pw_aff *index, __isl_take isl_space *space)
3126 isl_local_space *ls;
3127 isl_id *array_id = NULL;
3128 isl_aff *aff;
3129 int pos = -1;
3131 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out)) {
3132 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
3133 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3135 isl_space_free(space);
3137 if (pos < 0) {
3138 isl_id_free(array_id);
3139 return index;
3142 space = isl_multi_pw_aff_get_domain_space(index);
3143 isl_multi_pw_aff_free(index);
3145 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3146 if (pos < 0) {
3147 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
3148 space = isl_space_set_dim_id(space, isl_dim_param, 0, array_id);
3149 pos = 0;
3150 } else
3151 isl_id_free(array_id);
3153 ls = isl_local_space_from_space(space);
3154 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
3155 index = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
3157 return index;
3160 /* Check if the given access relation accesses a (0D) array that corresponds
3161 * to one of the parameters in "dim". If so, replace the array access
3162 * by an access to the set of integers with as index (and value)
3163 * that parameter.
3165 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
3166 __isl_take isl_space *dim)
3168 isl_id *array_id = NULL;
3169 int pos = -1;
3171 if (isl_map_has_tuple_id(access, isl_dim_out)) {
3172 array_id = isl_map_get_tuple_id(access, isl_dim_out);
3173 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
3175 isl_space_free(dim);
3177 if (pos < 0) {
3178 isl_id_free(array_id);
3179 return access;
3182 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
3183 if (pos < 0) {
3184 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
3185 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
3186 pos = 0;
3187 } else
3188 isl_id_free(array_id);
3190 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
3191 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
3193 return access;
3196 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3197 * in "dim" by a value equal to the corresponding parameter.
3199 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
3200 __isl_take isl_space *dim)
3202 int i;
3204 if (!expr)
3205 goto error;
3207 for (i = 0; i < expr->n_arg; ++i) {
3208 expr->args[i] =
3209 expr_detect_parameter_accesses(expr->args[i],
3210 isl_space_copy(dim));
3211 if (!expr->args[i])
3212 goto error;
3215 if (expr->type == pet_expr_access) {
3216 expr->acc.access = access_detect_parameter(expr->acc.access,
3217 isl_space_copy(dim));
3218 expr->acc.index = index_detect_parameter(expr->acc.index,
3219 isl_space_copy(dim));
3220 if (!expr->acc.access || !expr->acc.index)
3221 goto error;
3224 isl_space_free(dim);
3225 return expr;
3226 error:
3227 isl_space_free(dim);
3228 return pet_expr_free(expr);
3231 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3232 * in "dim" by a value equal to the corresponding parameter.
3234 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
3235 __isl_take isl_space *dim)
3237 if (!stmt)
3238 goto error;
3240 stmt->body = expr_detect_parameter_accesses(stmt->body,
3241 isl_space_copy(dim));
3243 if (!stmt->domain || !stmt->schedule || !stmt->body)
3244 goto error;
3246 isl_space_free(dim);
3247 return stmt;
3248 error:
3249 isl_space_free(dim);
3250 return pet_stmt_free(stmt);
3253 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3254 * in "dim" by a value equal to the corresponding parameter.
3256 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
3257 __isl_take isl_space *dim)
3259 int i;
3261 if (!scop)
3262 goto error;
3264 for (i = 0; i < scop->n_stmt; ++i) {
3265 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
3266 isl_space_copy(dim));
3267 if (!scop->stmts[i])
3268 goto error;
3271 isl_space_free(dim);
3272 return scop;
3273 error:
3274 isl_space_free(dim);
3275 return pet_scop_free(scop);
3278 /* Replace all accesses to (0D) arrays that correspond to any of
3279 * the parameters used in "scop" by a value equal
3280 * to the corresponding parameter.
3282 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
3284 isl_space *dim;
3286 if (!scop)
3287 return NULL;
3289 dim = isl_set_get_space(scop->context);
3290 dim = scop_collect_params(scop, dim);
3292 scop = scop_detect_parameter_accesses(scop, dim);
3294 return scop;
3297 /* Return the relation mapping domain iterations to all possibly
3298 * accessed data elements.
3299 * In particular, take the access relation and project out the values
3300 * of the arguments, if any.
3302 static __isl_give isl_map *expr_access_get_may_access(struct pet_expr *expr)
3304 isl_map *access;
3305 isl_space *space;
3306 isl_map *map;
3308 if (!expr)
3309 return NULL;
3310 if (expr->type != pet_expr_access)
3311 return NULL;
3313 access = isl_map_copy(expr->acc.access);
3314 if (expr->n_arg == 0)
3315 return access;
3317 space = isl_space_domain(isl_map_get_space(access));
3318 map = isl_map_universe(isl_space_unwrap(space));
3319 map = isl_map_domain_map(map);
3320 access = isl_map_apply_domain(access, map);
3322 return access;
3325 /* Add all read access relations (if "read" is set) and/or all write
3326 * access relations (if "write" is set) to "accesses" and return the result.
3328 * If "must" is set, then we only add the accesses that are definitely
3329 * performed. Otherwise, we add all potential accesses.
3330 * In particular, if the access has any arguments, then if "must" is
3331 * set we currently skip the access completely. If "must" is not set,
3332 * we project out the values of the access arguments.
3334 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
3335 int read, int write, int must, __isl_take isl_union_map *accesses)
3337 int i;
3338 isl_id *id;
3339 isl_space *dim;
3341 if (!expr)
3342 return NULL;
3344 for (i = 0; i < expr->n_arg; ++i)
3345 accesses = expr_collect_accesses(expr->args[i],
3346 read, write, must, accesses);
3348 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
3349 ((read && expr->acc.read) || (write && expr->acc.write)) &&
3350 (!must || expr->n_arg == 0)) {
3351 isl_map *access;
3353 access = expr_access_get_may_access(expr);
3354 accesses = isl_union_map_add_map(accesses, access);
3357 return accesses;
3360 /* Collect and return all read access relations (if "read" is set)
3361 * and/or all write access relations (if "write" is set) in "stmt".
3363 * If "must" is set, then we only add the accesses that are definitely
3364 * performed. Otherwise, we add all potential accesses.
3365 * In particular, if the statement has any arguments, then if "must" is
3366 * set we currently skip the statement completely. If "must" is not set,
3367 * we project out the values of the statement arguments.
3369 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
3370 int read, int write, int must, __isl_take isl_space *dim)
3372 isl_union_map *accesses;
3373 isl_set *domain;
3375 if (!stmt)
3376 return NULL;
3378 accesses = isl_union_map_empty(dim);
3380 if (must && stmt->n_arg > 0)
3381 return accesses;
3383 domain = isl_set_copy(stmt->domain);
3384 if (isl_set_is_wrapping(domain))
3385 domain = isl_map_domain(isl_set_unwrap(domain));
3387 accesses = expr_collect_accesses(stmt->body,
3388 read, write, must, accesses);
3389 accesses = isl_union_map_intersect_domain(accesses,
3390 isl_union_set_from_set(domain));
3392 return accesses;
3395 /* Collect and return all read access relations (if "read" is set)
3396 * and/or all write access relations (if "write" is set) in "scop".
3397 * If "must" is set, then we only add the accesses that are definitely
3398 * performed. Otherwise, we add all potential accesses.
3400 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
3401 int read, int write, int must)
3403 int i;
3404 isl_union_map *accesses;
3405 isl_union_set *arrays;
3407 if (!scop)
3408 return NULL;
3410 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
3412 for (i = 0; i < scop->n_stmt; ++i) {
3413 isl_union_map *accesses_i;
3414 isl_space *dim = isl_set_get_space(scop->context);
3415 accesses_i = stmt_collect_accesses(scop->stmts[i],
3416 read, write, must, dim);
3417 accesses = isl_union_map_union(accesses, accesses_i);
3420 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
3421 for (i = 0; i < scop->n_array; ++i) {
3422 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
3423 arrays = isl_union_set_add_set(arrays, extent);
3425 accesses = isl_union_map_intersect_range(accesses, arrays);
3427 return accesses;
3430 /* Collect all potential read access relations.
3432 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
3434 return scop_collect_accesses(scop, 1, 0, 0);
3437 /* Collect all potential write access relations.
3439 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
3441 return scop_collect_accesses(scop, 0, 1, 0);
3444 /* Collect all definite write access relations.
3446 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
3448 return scop_collect_accesses(scop, 0, 1, 1);
3451 /* Collect and return the union of iteration domains in "scop".
3453 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
3455 int i;
3456 isl_set *domain_i;
3457 isl_union_set *domain;
3459 if (!scop)
3460 return NULL;
3462 domain = isl_union_set_empty(isl_set_get_space(scop->context));
3464 for (i = 0; i < scop->n_stmt; ++i) {
3465 domain_i = isl_set_copy(scop->stmts[i]->domain);
3466 domain = isl_union_set_add_set(domain, domain_i);
3469 return domain;
3472 /* Collect and return the schedules of the statements in "scop".
3473 * The range is normalized to the maximal number of scheduling
3474 * dimensions.
3476 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
3478 int i, j;
3479 isl_map *schedule_i;
3480 isl_union_map *schedule;
3481 int depth, max_depth = 0;
3483 if (!scop)
3484 return NULL;
3486 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
3488 for (i = 0; i < scop->n_stmt; ++i) {
3489 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
3490 if (depth > max_depth)
3491 max_depth = depth;
3494 for (i = 0; i < scop->n_stmt; ++i) {
3495 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
3496 depth = isl_map_dim(schedule_i, isl_dim_out);
3497 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
3498 max_depth - depth);
3499 for (j = depth; j < max_depth; ++j)
3500 schedule_i = isl_map_fix_si(schedule_i,
3501 isl_dim_out, j, 0);
3502 schedule = isl_union_map_add_map(schedule, schedule_i);
3505 return schedule;
3508 /* Does expression "expr" write to "id"?
3510 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
3512 int i;
3513 isl_id *write_id;
3515 for (i = 0; i < expr->n_arg; ++i) {
3516 int writes = expr_writes(expr->args[i], id);
3517 if (writes < 0 || writes)
3518 return writes;
3521 if (expr->type != pet_expr_access)
3522 return 0;
3523 if (!expr->acc.write)
3524 return 0;
3525 if (pet_expr_is_affine(expr))
3526 return 0;
3528 write_id = pet_expr_access_get_id(expr);
3529 isl_id_free(write_id);
3531 if (!write_id)
3532 return -1;
3534 return write_id == id;
3537 /* Does statement "stmt" write to "id"?
3539 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
3541 return expr_writes(stmt->body, id);
3544 /* Is there any write access in "scop" that accesses "id"?
3546 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
3548 int i;
3550 if (!scop)
3551 return -1;
3553 for (i = 0; i < scop->n_stmt; ++i) {
3554 int writes = stmt_writes(scop->stmts[i], id);
3555 if (writes < 0 || writes)
3556 return writes;
3559 return 0;
3562 /* Add a reference identifier to access expression "expr".
3563 * "user" points to an integer that contains the sequence number
3564 * of the next reference.
3566 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
3568 isl_ctx *ctx;
3569 char name[50];
3570 int *n_ref = user;
3572 if (!expr)
3573 return expr;
3575 ctx = isl_map_get_ctx(expr->acc.access);
3576 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
3577 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
3578 if (!expr->acc.ref_id)
3579 return pet_expr_free(expr);
3581 return expr;
3584 /* Add a reference identifier to all access expressions in "stmt".
3585 * "n_ref" points to an integer that contains the sequence number
3586 * of the next reference.
3588 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
3590 int i;
3592 if (!stmt)
3593 return NULL;
3595 for (i = 0; i < stmt->n_arg; ++i) {
3596 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3597 &access_add_ref_id, n_ref);
3598 if (!stmt->args[i])
3599 return pet_stmt_free(stmt);
3602 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
3603 if (!stmt->body)
3604 return pet_stmt_free(stmt);
3606 return stmt;
3609 /* Add a reference identifier to all access expressions in "scop".
3611 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
3613 int i;
3614 int n_ref;
3616 if (!scop)
3617 return NULL;
3619 n_ref = 0;
3620 for (i = 0; i < scop->n_stmt; ++i) {
3621 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
3622 if (!scop->stmts[i])
3623 return pet_scop_free(scop);
3626 return scop;
3629 /* Reset the user pointer on all parameter ids in "array".
3631 static struct pet_array *array_anonymize(struct pet_array *array)
3633 if (!array)
3634 return NULL;
3636 array->context = isl_set_reset_user(array->context);
3637 array->extent = isl_set_reset_user(array->extent);
3638 if (!array->context || !array->extent)
3639 return pet_array_free(array);
3641 return array;
3644 /* Reset the user pointer on all parameter and tuple ids in
3645 * the access relation and the index expressions
3646 * of the access expression "expr".
3648 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
3650 expr->acc.access = isl_map_reset_user(expr->acc.access);
3651 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
3652 if (!expr->acc.access || !expr->acc.index)
3653 return pet_expr_free(expr);
3655 return expr;
3658 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3660 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3662 int i;
3663 isl_space *space;
3664 isl_set *domain;
3666 if (!stmt)
3667 return NULL;
3669 stmt->domain = isl_set_reset_user(stmt->domain);
3670 stmt->schedule = isl_map_reset_user(stmt->schedule);
3671 if (!stmt->domain || !stmt->schedule)
3672 return pet_stmt_free(stmt);
3674 for (i = 0; i < stmt->n_arg; ++i) {
3675 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3676 &access_anonymize, NULL);
3677 if (!stmt->args[i])
3678 return pet_stmt_free(stmt);
3681 stmt->body = pet_expr_map_access(stmt->body,
3682 &access_anonymize, NULL);
3683 if (!stmt->body)
3684 return pet_stmt_free(stmt);
3686 return stmt;
3689 /* Reset the user pointer on the tuple ids and all parameter ids
3690 * in "implication".
3692 static struct pet_implication *implication_anonymize(
3693 struct pet_implication *implication)
3695 if (!implication)
3696 return NULL;
3698 implication->extension = isl_map_reset_user(implication->extension);
3699 if (!implication->extension)
3700 return pet_implication_free(implication);
3702 return implication;
3705 /* Reset the user pointer on all parameter and tuple ids in "scop".
3707 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
3709 int i;
3711 if (!scop)
3712 return NULL;
3714 scop->context = isl_set_reset_user(scop->context);
3715 scop->context_value = isl_set_reset_user(scop->context_value);
3716 if (!scop->context || !scop->context_value)
3717 return pet_scop_free(scop);
3719 for (i = 0; i < scop->n_array; ++i) {
3720 scop->arrays[i] = array_anonymize(scop->arrays[i]);
3721 if (!scop->arrays[i])
3722 return pet_scop_free(scop);
3725 for (i = 0; i < scop->n_stmt; ++i) {
3726 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
3727 if (!scop->stmts[i])
3728 return pet_scop_free(scop);
3731 for (i = 0; i < scop->n_implication; ++i) {
3732 scop->implications[i] =
3733 implication_anonymize(scop->implications[i]);
3734 if (!scop->implications[i])
3735 return pet_scop_free(scop);
3738 return scop;
3741 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
3742 * then intersect the range of "map" with the valid set of values.
3744 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
3745 struct pet_expr *arg, __isl_keep isl_union_map *value_bounds)
3747 isl_id *id;
3748 isl_map *vb;
3749 isl_space *space;
3750 isl_ctx *ctx = isl_map_get_ctx(map);
3752 id = pet_expr_access_get_id(arg);
3753 space = isl_space_alloc(ctx, 0, 0, 1);
3754 space = isl_space_set_tuple_id(space, isl_dim_in, id);
3755 vb = isl_union_map_extract_map(value_bounds, space);
3756 if (!isl_map_plain_is_empty(vb))
3757 map = isl_map_intersect_range(map, isl_map_range(vb));
3758 else
3759 isl_map_free(vb);
3761 return map;
3764 /* Given a set "domain", return a wrapped relation with the given set
3765 * as domain and a range of dimension "n_arg", where each coordinate
3766 * is either unbounded or, if the corresponding element of args is of
3767 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
3769 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
3770 unsigned n_arg, struct pet_expr **args,
3771 __isl_keep isl_union_map *value_bounds)
3773 int i;
3774 isl_map *map;
3775 isl_space *space;
3777 map = isl_map_from_domain(domain);
3778 space = isl_map_get_space(map);
3779 space = isl_space_add_dims(space, isl_dim_out, 1);
3781 for (i = 0; i < n_arg; ++i) {
3782 isl_map *map_i;
3783 struct pet_expr *arg = args[i];
3785 map_i = isl_map_universe(isl_space_copy(space));
3786 if (arg->type == pet_expr_access)
3787 map_i = access_apply_value_bounds(map_i, arg,
3788 value_bounds);
3789 map = isl_map_flat_range_product(map, map_i);
3791 isl_space_free(space);
3793 return isl_map_wrap(map);
3796 /* Data used in access_gist() callback.
3798 struct pet_access_gist_data {
3799 isl_set *domain;
3800 isl_union_map *value_bounds;
3803 /* Given an expression "expr" of type pet_expr_access, compute
3804 * the gist of the associated access relation and index expression
3805 * with respect to data->domain and the bounds on the values of the arguments
3806 * of the expression.
3808 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
3810 struct pet_access_gist_data *data = user;
3811 isl_set *domain;
3813 domain = isl_set_copy(data->domain);
3814 if (expr->n_arg > 0)
3815 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
3816 data->value_bounds);
3818 expr->acc.access = isl_map_gist_domain(expr->acc.access,
3819 isl_set_copy(domain));
3820 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
3821 if (!expr->acc.access || !expr->acc.index)
3822 return pet_expr_free(expr);
3824 return expr;
3827 /* Compute the gist of the iteration domain and all access relations
3828 * of "stmt" based on the constraints on the parameters specified by "context"
3829 * and the constraints on the values of nested accesses specified
3830 * by "value_bounds".
3832 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3833 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3835 int i;
3836 isl_space *space;
3837 isl_set *domain;
3838 struct pet_access_gist_data data;
3840 if (!stmt)
3841 return NULL;
3843 data.domain = isl_set_copy(stmt->domain);
3844 data.value_bounds = value_bounds;
3845 if (stmt->n_arg > 0)
3846 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
3848 data.domain = isl_set_intersect_params(data.domain,
3849 isl_set_copy(context));
3851 for (i = 0; i < stmt->n_arg; ++i) {
3852 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3853 &access_gist, &data);
3854 if (!stmt->args[i])
3855 goto error;
3858 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
3859 if (!stmt->body)
3860 goto error;
3862 isl_set_free(data.domain);
3864 space = isl_set_get_space(stmt->domain);
3865 if (isl_space_is_wrapping(space))
3866 space = isl_space_domain(isl_space_unwrap(space));
3867 domain = isl_set_universe(space);
3868 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3869 if (stmt->n_arg > 0)
3870 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
3871 value_bounds);
3872 stmt->domain = isl_set_gist(stmt->domain, domain);
3873 if (!stmt->domain)
3874 return pet_stmt_free(stmt);
3876 return stmt;
3877 error:
3878 isl_set_free(data.domain);
3879 return pet_stmt_free(stmt);
3882 /* Compute the gist of the extent of the array
3883 * based on the constraints on the parameters specified by "context".
3885 static struct pet_array *array_gist(struct pet_array *array,
3886 __isl_keep isl_set *context)
3888 if (!array)
3889 return NULL;
3891 array->extent = isl_set_gist_params(array->extent,
3892 isl_set_copy(context));
3893 if (!array->extent)
3894 return pet_array_free(array);
3896 return array;
3899 /* Compute the gist of all sets and relations in "scop"
3900 * based on the constraints on the parameters specified by "scop->context"
3901 * and the constraints on the values of nested accesses specified
3902 * by "value_bounds".
3904 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3905 __isl_keep isl_union_map *value_bounds)
3907 int i;
3909 if (!scop)
3910 return NULL;
3912 scop->context = isl_set_coalesce(scop->context);
3913 if (!scop->context)
3914 return pet_scop_free(scop);
3916 for (i = 0; i < scop->n_array; ++i) {
3917 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3918 if (!scop->arrays[i])
3919 return pet_scop_free(scop);
3922 for (i = 0; i < scop->n_stmt; ++i) {
3923 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3924 value_bounds);
3925 if (!scop->stmts[i])
3926 return pet_scop_free(scop);
3929 return scop;
3932 /* Intersect the context of "scop" with "context".
3933 * To ensure that we don't introduce any unnamed parameters in
3934 * the context of "scop", we first remove the unnamed parameters
3935 * from "context".
3937 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3938 __isl_take isl_set *context)
3940 if (!scop)
3941 goto error;
3943 context = set_project_out_unnamed_params(context);
3944 scop->context = isl_set_intersect(scop->context, context);
3945 if (!scop->context)
3946 return pet_scop_free(scop);
3948 return scop;
3949 error:
3950 isl_set_free(context);
3951 return pet_scop_free(scop);
3954 /* Drop the current context of "scop". That is, replace the context
3955 * by a universal set.
3957 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3959 isl_space *space;
3961 if (!scop)
3962 return NULL;
3964 space = isl_set_get_space(scop->context);
3965 isl_set_free(scop->context);
3966 scop->context = isl_set_universe(space);
3967 if (!scop->context)
3968 return pet_scop_free(scop);
3970 return scop;
3973 /* Append "array" to the arrays of "scop".
3975 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3976 struct pet_array *array)
3978 isl_ctx *ctx;
3979 struct pet_array **arrays;
3981 if (!array || !scop)
3982 goto error;
3984 ctx = isl_set_get_ctx(scop->context);
3985 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3986 scop->n_array + 1);
3987 if (!arrays)
3988 goto error;
3989 scop->arrays = arrays;
3990 scop->arrays[scop->n_array] = array;
3991 scop->n_array++;
3993 return scop;
3994 error:
3995 pet_array_free(array);
3996 return pet_scop_free(scop);
3999 /* Create and return an implication on filter values equal to "satisfied"
4000 * with extension "map".
4002 static struct pet_implication *new_implication(__isl_take isl_map *map,
4003 int satisfied)
4005 isl_ctx *ctx;
4006 struct pet_implication *implication;
4008 if (!map)
4009 return NULL;
4010 ctx = isl_map_get_ctx(map);
4011 implication = isl_alloc_type(ctx, struct pet_implication);
4012 if (!implication)
4013 goto error;
4015 implication->extension = map;
4016 implication->satisfied = satisfied;
4018 return implication;
4019 error:
4020 isl_map_free(map);
4021 return NULL;
4024 /* Add an implication on filter values equal to "satisfied"
4025 * with extension "map" to "scop".
4027 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
4028 __isl_take isl_map *map, int satisfied)
4030 isl_ctx *ctx;
4031 struct pet_implication *implication;
4032 struct pet_implication **implications;
4034 implication = new_implication(map, satisfied);
4035 if (!scop || !implication)
4036 goto error;
4038 ctx = isl_set_get_ctx(scop->context);
4039 implications = isl_realloc_array(ctx, scop->implications,
4040 struct pet_implication *,
4041 scop->n_implication + 1);
4042 if (!implications)
4043 goto error;
4044 scop->implications = implications;
4045 scop->implications[scop->n_implication] = implication;
4046 scop->n_implication++;
4048 return scop;
4049 error:
4050 pet_implication_free(implication);
4051 return pet_scop_free(scop);
4054 /* Given an access expression, check if it is data dependent.
4055 * If so, set *found and abort the search.
4057 static int is_data_dependent(struct pet_expr *expr, void *user)
4059 int *found = user;
4061 if (expr->n_arg) {
4062 *found = 1;
4063 return -1;
4066 return 0;
4069 /* Does "scop" contain any data dependent accesses?
4071 * Check the body of each statement for such accesses.
4073 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
4075 int i;
4076 int found = 0;
4078 if (!scop)
4079 return -1;
4081 for (i = 0; i < scop->n_stmt; ++i) {
4082 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
4083 &is_data_dependent, &found);
4084 if (r < 0 && !found)
4085 return -1;
4086 if (found)
4087 return found;
4090 return found;
4093 /* Does "scop" contain and data dependent conditions?
4095 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
4097 int i;
4099 if (!scop)
4100 return -1;
4102 for (i = 0; i < scop->n_stmt; ++i)
4103 if (scop->stmts[i]->n_arg > 0)
4104 return 1;
4106 return 0;
4109 /* Keep track of the "input" file inside the (extended) "scop".
4111 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
4113 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4115 if (!scop)
4116 return NULL;
4118 ext->input = input;
4120 return scop;
4123 /* Print the original code corresponding to "scop" to printer "p".
4125 * pet_scop_print_original can only be called from
4126 * a pet_transform_C_source callback. This means that the input
4127 * file is stored in the extended scop and that the printer prints
4128 * to a file.
4130 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
4131 __isl_take isl_printer *p)
4133 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4134 FILE *output;
4136 if (!scop || !p)
4137 return isl_printer_free(p);
4139 if (!ext->input)
4140 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
4141 "no input file stored in scop",
4142 return isl_printer_free(p));
4144 output = isl_printer_get_file(p);
4145 if (!output)
4146 return isl_printer_free(p);
4148 if (copy(ext->input, output, scop->start, scop->end) < 0)
4149 return isl_printer_free(p);
4151 return p;