scop.c: *_collect_params: fix error handling
[pet.git] / scop.c
blob0c50dafa79654eaf86f4350c760e4bbeea1c082d
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_shl] = "<<",
66 [pet_op_shr] = ">>",
67 [pet_op_eq] = "==",
68 [pet_op_ne] = "!=",
69 [pet_op_le] = "<=",
70 [pet_op_ge] = ">=",
71 [pet_op_lt] = "<",
72 [pet_op_gt] = ">",
73 [pet_op_minus] = "-",
74 [pet_op_post_inc] = "++",
75 [pet_op_post_dec] = "--",
76 [pet_op_pre_inc] = "++",
77 [pet_op_pre_dec] = "--",
78 [pet_op_address_of] = "&",
79 [pet_op_and] = "&",
80 [pet_op_xor] = "^",
81 [pet_op_or] = "|",
82 [pet_op_not] = "~",
83 [pet_op_assume] = "assume",
84 [pet_op_kill] = "kill"
87 /* pet_scop with extra information that is used during parsing and printing.
89 * In particular, we keep track of conditions under which we want
90 * to skip the rest of the current loop iteration (skip[pet_skip_now])
91 * and of conditions under which we want to skip subsequent
92 * loop iterations (skip[pet_skip_later]).
94 * The conditions are represented as index expressions defined
95 * over a zero-dimensiona domain. The index expression is either
96 * a boolean affine expression or an access to a variable, which
97 * is assumed to attain values zero and one. The condition holds
98 * if the variable has value one or if the affine expression
99 * has value one (typically for only part of the parameter space).
101 * A missing condition (skip[type] == NULL) means that we don't want
102 * to skip anything.
104 * Additionally, we keep track of the original input file
105 * inside pet_transform_C_source.
107 struct pet_scop_ext {
108 struct pet_scop scop;
110 isl_multi_pw_aff *skip[2];
111 FILE *input;
114 const char *pet_op_str(enum pet_op_type op)
116 return op_str[op];
119 int pet_op_is_inc_dec(enum pet_op_type op)
121 return op == pet_op_post_inc || op == pet_op_post_dec ||
122 op == pet_op_pre_inc || op == pet_op_pre_dec;
125 const char *pet_type_str(enum pet_expr_type type)
127 return type_str[type];
130 enum pet_op_type pet_str_op(const char *str)
132 int i;
134 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
135 if (!strcmp(op_str[i], str))
136 return i;
138 return -1;
141 enum pet_expr_type pet_str_type(const char *str)
143 int i;
145 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
146 if (!strcmp(type_str[i], str))
147 return i;
149 return -1;
152 /* Construct an access pet_expr from an access relation and an index expression.
153 * By default, it is considered to be a read access.
155 struct pet_expr *pet_expr_from_access_and_index( __isl_take isl_map *access,
156 __isl_take isl_multi_pw_aff *index)
158 isl_ctx *ctx = isl_map_get_ctx(access);
159 struct pet_expr *expr;
161 if (!index || !access)
162 goto error;
163 expr = isl_calloc_type(ctx, struct pet_expr);
164 if (!expr)
165 goto error;
167 expr->type = pet_expr_access;
168 expr->acc.access = access;
169 expr->acc.index = index;
170 expr->acc.read = 1;
171 expr->acc.write = 0;
173 return expr;
174 error:
175 isl_map_free(access);
176 isl_multi_pw_aff_free(index);
177 return NULL;
180 /* Construct an access pet_expr from an index expression.
181 * By default, the access is considered to be a read access.
183 struct pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
185 isl_map *access;
187 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
188 return pet_expr_from_access_and_index(access, index);
191 /* Extend the range of "access" with "n" dimensions, retaining
192 * the tuple identifier on this range.
194 * If "access" represents a member access, then extend the range
195 * of the member.
197 static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
199 isl_id *id;
201 id = isl_map_get_tuple_id(access, isl_dim_out);
203 if (!isl_map_range_is_wrapping(access)) {
204 access = isl_map_add_dims(access, isl_dim_out, n);
205 } else {
206 isl_map *domain;
208 domain = isl_map_copy(access);
209 domain = isl_map_range_factor_domain(domain);
210 access = isl_map_range_factor_range(access);
211 access = extend_range(access, n);
212 access = isl_map_range_product(domain, access);
215 access = isl_map_set_tuple_id(access, isl_dim_out, id);
217 return access;
220 /* Construct an access pet_expr from an index expression and
221 * the depth of the accessed array.
222 * By default, the access is considered to be a read access.
224 * If the number of indices is smaller than the depth of the array,
225 * then we assume that all elements of the remaining dimensions
226 * are accessed.
228 struct pet_expr *pet_expr_from_index_and_depth(
229 __isl_take isl_multi_pw_aff *index, int depth)
231 isl_map *access;
232 int dim;
234 access = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
235 if (!access)
236 goto error;
237 dim = isl_map_dim(access, isl_dim_out);
238 if (dim > depth)
239 isl_die(isl_map_get_ctx(access), isl_error_internal,
240 "number of indices greater than depth",
241 access = isl_map_free(access));
242 if (dim == depth)
243 return pet_expr_from_access_and_index(access, index);
245 access = extend_range(access, depth - dim);
247 return pet_expr_from_access_and_index(access, index);
248 error:
249 isl_multi_pw_aff_free(index);
250 return NULL;
253 /* Construct a pet_expr that kills the elements specified by
254 * the index expression "index" and the access relation "access".
256 struct pet_expr *pet_expr_kill_from_access_and_index(__isl_take isl_map *access,
257 __isl_take isl_multi_pw_aff *index)
259 isl_ctx *ctx;
260 struct pet_expr *expr;
262 if (!access || !index)
263 goto error;
265 ctx = isl_multi_pw_aff_get_ctx(index);
266 expr = pet_expr_from_access_and_index(access, index);
267 if (!expr)
268 return NULL;
269 expr->acc.read = 0;
270 return pet_expr_new_unary(ctx, pet_op_kill, expr);
271 error:
272 isl_map_free(access);
273 isl_multi_pw_aff_free(index);
274 return NULL;
277 /* Construct a unary pet_expr that performs "op" on "arg".
279 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
280 struct pet_expr *arg)
282 struct pet_expr *expr;
284 if (!arg)
285 goto error;
286 expr = isl_alloc_type(ctx, struct pet_expr);
287 if (!expr)
288 goto error;
290 expr->type = pet_expr_unary;
291 expr->op = op;
292 expr->n_arg = 1;
293 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
294 if (!expr->args)
295 goto error;
296 expr->args[pet_un_arg] = arg;
298 return expr;
299 error:
300 pet_expr_free(arg);
301 return NULL;
304 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
306 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
307 struct pet_expr *lhs, struct pet_expr *rhs)
309 struct pet_expr *expr;
311 if (!lhs || !rhs)
312 goto error;
313 expr = isl_alloc_type(ctx, struct pet_expr);
314 if (!expr)
315 goto error;
317 expr->type = pet_expr_binary;
318 expr->op = op;
319 expr->n_arg = 2;
320 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
321 if (!expr->args)
322 goto error;
323 expr->args[pet_bin_lhs] = lhs;
324 expr->args[pet_bin_rhs] = rhs;
326 return expr;
327 error:
328 pet_expr_free(lhs);
329 pet_expr_free(rhs);
330 return NULL;
333 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
335 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
336 struct pet_expr *lhs, struct pet_expr *rhs)
338 struct pet_expr *expr;
340 if (!cond || !lhs || !rhs)
341 goto error;
342 expr = isl_alloc_type(ctx, struct pet_expr);
343 if (!expr)
344 goto error;
346 expr->type = pet_expr_ternary;
347 expr->n_arg = 3;
348 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
349 if (!expr->args)
350 goto error;
351 expr->args[pet_ter_cond] = cond;
352 expr->args[pet_ter_true] = lhs;
353 expr->args[pet_ter_false] = rhs;
355 return expr;
356 error:
357 pet_expr_free(cond);
358 pet_expr_free(lhs);
359 pet_expr_free(rhs);
360 return NULL;
363 /* Construct a call pet_expr that calls function "name" with "n_arg"
364 * arguments. The caller is responsible for filling in the arguments.
366 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
367 unsigned n_arg)
369 struct pet_expr *expr;
371 expr = isl_alloc_type(ctx, struct pet_expr);
372 if (!expr)
373 return NULL;
375 expr->type = pet_expr_call;
376 expr->n_arg = n_arg;
377 expr->name = strdup(name);
378 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
379 if (!expr->name || !expr->args)
380 return pet_expr_free(expr);
382 return expr;
385 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
387 struct pet_expr *pet_expr_new_cast(isl_ctx *ctx, const char *type_name,
388 struct pet_expr *arg)
390 struct pet_expr *expr;
392 if (!arg)
393 return NULL;
395 expr = isl_alloc_type(ctx, struct pet_expr);
396 if (!expr)
397 goto error;
399 expr->type = pet_expr_cast;
400 expr->n_arg = 1;
401 expr->type_name = strdup(type_name);
402 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
403 if (!expr->type_name || !expr->args)
404 goto error;
406 expr->args[0] = arg;
408 return expr;
409 error:
410 pet_expr_free(arg);
411 pet_expr_free(expr);
412 return NULL;
415 /* Construct a pet_expr that represents the double "d".
417 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double val, const char *s)
419 struct pet_expr *expr;
421 expr = isl_calloc_type(ctx, struct pet_expr);
422 if (!expr)
423 return NULL;
425 expr->type = pet_expr_double;
426 expr->d.val = val;
427 expr->d.s = strdup(s);
428 if (!expr->d.s)
429 return pet_expr_free(expr);
431 return expr;
434 struct pet_expr *pet_expr_free(struct pet_expr *expr)
436 int i;
438 if (!expr)
439 return NULL;
441 for (i = 0; i < expr->n_arg; ++i)
442 pet_expr_free(expr->args[i]);
443 free(expr->args);
445 switch (expr->type) {
446 case pet_expr_access:
447 isl_id_free(expr->acc.ref_id);
448 isl_map_free(expr->acc.access);
449 isl_multi_pw_aff_free(expr->acc.index);
450 break;
451 case pet_expr_call:
452 free(expr->name);
453 break;
454 case pet_expr_cast:
455 free(expr->type_name);
456 break;
457 case pet_expr_double:
458 free(expr->d.s);
459 break;
460 case pet_expr_unary:
461 case pet_expr_binary:
462 case pet_expr_ternary:
463 break;
466 free(expr);
467 return NULL;
470 static void expr_dump(struct pet_expr *expr, int indent)
472 int i;
474 if (!expr)
475 return;
477 fprintf(stderr, "%*s", indent, "");
479 switch (expr->type) {
480 case pet_expr_double:
481 fprintf(stderr, "%s\n", expr->d.s);
482 break;
483 case pet_expr_access:
484 isl_id_dump(expr->acc.ref_id);
485 fprintf(stderr, "%*s", indent, "");
486 isl_map_dump(expr->acc.access);
487 fprintf(stderr, "%*s", indent, "");
488 isl_multi_pw_aff_dump(expr->acc.index);
489 fprintf(stderr, "%*sread: %d\n", indent + 2,
490 "", expr->acc.read);
491 fprintf(stderr, "%*swrite: %d\n", indent + 2,
492 "", expr->acc.write);
493 for (i = 0; i < expr->n_arg; ++i)
494 expr_dump(expr->args[i], indent + 2);
495 break;
496 case pet_expr_unary:
497 fprintf(stderr, "%s\n", op_str[expr->op]);
498 expr_dump(expr->args[pet_un_arg], indent + 2);
499 break;
500 case pet_expr_binary:
501 fprintf(stderr, "%s\n", op_str[expr->op]);
502 expr_dump(expr->args[pet_bin_lhs], indent + 2);
503 expr_dump(expr->args[pet_bin_rhs], indent + 2);
504 break;
505 case pet_expr_ternary:
506 fprintf(stderr, "?:\n");
507 expr_dump(expr->args[pet_ter_cond], indent + 2);
508 expr_dump(expr->args[pet_ter_true], indent + 2);
509 expr_dump(expr->args[pet_ter_false], indent + 2);
510 break;
511 case pet_expr_call:
512 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
513 for (i = 0; i < expr->n_arg; ++i)
514 expr_dump(expr->args[i], indent + 2);
515 break;
516 case pet_expr_cast:
517 fprintf(stderr, "(%s)\n", expr->type_name);
518 for (i = 0; i < expr->n_arg; ++i)
519 expr_dump(expr->args[i], indent + 2);
520 break;
524 void pet_expr_dump(struct pet_expr *expr)
526 expr_dump(expr, 0);
529 /* Does "expr" represent an access to an unnamed space, i.e.,
530 * does it represent an affine expression?
532 int pet_expr_is_affine(struct pet_expr *expr)
534 int has_id;
536 if (!expr)
537 return -1;
538 if (expr->type != pet_expr_access)
539 return 0;
541 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
542 if (has_id < 0)
543 return -1;
545 return !has_id;
548 /* Return the identifier of the array accessed by "expr".
550 * If "expr" represents a member access, then return the identifier
551 * of the outer structure array.
553 __isl_give isl_id *pet_expr_access_get_id(struct pet_expr *expr)
555 if (!expr)
556 return NULL;
557 if (expr->type != pet_expr_access)
558 return NULL;
560 if (isl_map_range_is_wrapping(expr->acc.access)) {
561 isl_space *space;
562 isl_id *id;
564 space = isl_map_get_space(expr->acc.access);
565 space = isl_space_range(space);
566 while (space && isl_space_is_wrapping(space))
567 space = isl_space_domain(isl_space_unwrap(space));
568 id = isl_space_get_tuple_id(space, isl_dim_set);
569 isl_space_free(space);
571 return id;
574 return isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
577 /* Align the parameters of expr->acc.index and expr->acc.access.
579 struct pet_expr *pet_expr_access_align_params(struct pet_expr *expr)
581 if (!expr)
582 return NULL;
583 if (expr->type != pet_expr_access)
584 return pet_expr_free(expr);
586 expr->acc.access = isl_map_align_params(expr->acc.access,
587 isl_multi_pw_aff_get_space(expr->acc.index));
588 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
589 isl_map_get_space(expr->acc.access));
590 if (!expr->acc.access || !expr->acc.index)
591 return pet_expr_free(expr);
593 return expr;
596 /* Does "expr" represent an access to a scalar, i.e., zero-dimensional array?
598 int pet_expr_is_scalar_access(struct pet_expr *expr)
600 if (!expr)
601 return -1;
602 if (expr->type != pet_expr_access)
603 return 0;
605 return isl_map_dim(expr->acc.access, isl_dim_out) == 0;
608 /* Return 1 if the two pet_exprs are equivalent.
610 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
612 int i;
614 if (!expr1 || !expr2)
615 return 0;
617 if (expr1->type != expr2->type)
618 return 0;
619 if (expr1->n_arg != expr2->n_arg)
620 return 0;
621 for (i = 0; i < expr1->n_arg; ++i)
622 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
623 return 0;
624 switch (expr1->type) {
625 case pet_expr_double:
626 if (strcmp(expr1->d.s, expr2->d.s))
627 return 0;
628 if (expr1->d.val != expr2->d.val)
629 return 0;
630 break;
631 case pet_expr_access:
632 if (expr1->acc.read != expr2->acc.read)
633 return 0;
634 if (expr1->acc.write != expr2->acc.write)
635 return 0;
636 if (expr1->acc.ref_id != expr2->acc.ref_id)
637 return 0;
638 if (!expr1->acc.access || !expr2->acc.access)
639 return 0;
640 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
641 return 0;
642 if (!expr1->acc.index || !expr2->acc.index)
643 return 0;
644 if (!isl_multi_pw_aff_plain_is_equal(expr1->acc.index,
645 expr2->acc.index))
646 return 0;
647 break;
648 case pet_expr_unary:
649 case pet_expr_binary:
650 case pet_expr_ternary:
651 if (expr1->op != expr2->op)
652 return 0;
653 break;
654 case pet_expr_call:
655 if (strcmp(expr1->name, expr2->name))
656 return 0;
657 break;
658 case pet_expr_cast:
659 if (strcmp(expr1->type_name, expr2->type_name))
660 return 0;
661 break;
664 return 1;
667 /* Add extra conditions on the parameters to all access relations in "expr".
669 * The conditions are not added to the index expression. Instead, they
670 * are used to try and simplifty the index expression.
672 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
673 __isl_take isl_set *cond)
675 int i;
677 if (!expr)
678 goto error;
680 for (i = 0; i < expr->n_arg; ++i) {
681 expr->args[i] = pet_expr_restrict(expr->args[i],
682 isl_set_copy(cond));
683 if (!expr->args[i])
684 goto error;
687 if (expr->type == pet_expr_access) {
688 expr->acc.access = isl_map_intersect_params(expr->acc.access,
689 isl_set_copy(cond));
690 expr->acc.index = isl_multi_pw_aff_gist_params(
691 expr->acc.index, isl_set_copy(cond));
692 if (!expr->acc.access || !expr->acc.index)
693 goto error;
696 isl_set_free(cond);
697 return expr;
698 error:
699 isl_set_free(cond);
700 return pet_expr_free(expr);
703 /* Tag the access relation "access" with "id".
704 * That is, insert the id as the range of a wrapped relation
705 * in the domain of "access".
707 * If "access" is of the form
709 * D[i] -> A[a]
711 * then the result is of the form
713 * [D[i] -> id[]] -> A[a]
715 static __isl_give isl_map *tag_access(__isl_take isl_map *access,
716 __isl_take isl_id *id)
718 isl_space *space;
719 isl_map *add_tag;
721 space = isl_space_range(isl_map_get_space(access));
722 space = isl_space_from_range(space);
723 space = isl_space_set_tuple_id(space, isl_dim_in, id);
724 add_tag = isl_map_universe(space);
725 access = isl_map_domain_product(access, add_tag);
727 return access;
730 /* Modify all expressions of type pet_expr_access in "expr"
731 * by calling "fn" on them.
733 struct pet_expr *pet_expr_map_access(struct pet_expr *expr,
734 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
735 void *user)
737 int i;
739 if (!expr)
740 return NULL;
742 for (i = 0; i < expr->n_arg; ++i) {
743 expr->args[i] = pet_expr_map_access(expr->args[i], fn, user);
744 if (!expr->args[i])
745 return pet_expr_free(expr);
748 if (expr->type == pet_expr_access)
749 expr = fn(expr, user);
751 return expr;
754 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access.
756 * Return -1 on error (where fn return a negative value is treated as an error).
757 * Otherwise return 0.
759 int pet_expr_foreach_access_expr(struct pet_expr *expr,
760 int (*fn)(struct pet_expr *expr, void *user), void *user)
762 int i;
764 if (!expr)
765 return -1;
767 for (i = 0; i < expr->n_arg; ++i)
768 if (pet_expr_foreach_access_expr(expr->args[i], fn, user) < 0)
769 return -1;
771 if (expr->type == pet_expr_access)
772 return fn(expr, user);
774 return 0;
777 /* Modify the access relation and index expression
778 * of the given access expression
779 * based on the given iteration space transformation.
780 * In particular, precompose the access relation and index expression
781 * with the update function.
783 * If the access has any arguments then the domain of the access relation
784 * is a wrapped mapping from the iteration space to the space of
785 * argument values. We only need to change the domain of this wrapped
786 * mapping, so we extend the input transformation with an identity mapping
787 * on the space of argument values.
789 static struct pet_expr *update_domain(struct pet_expr *expr, void *user)
791 isl_multi_pw_aff *update = user;
792 isl_space *space;
794 update = isl_multi_pw_aff_copy(update);
796 space = isl_map_get_space(expr->acc.access);
797 space = isl_space_domain(space);
798 if (!isl_space_is_wrapping(space))
799 isl_space_free(space);
800 else {
801 isl_multi_pw_aff *id;
802 space = isl_space_unwrap(space);
803 space = isl_space_range(space);
804 space = isl_space_map_from_set(space);
805 id = isl_multi_pw_aff_identity(space);
806 update = isl_multi_pw_aff_product(update, id);
809 expr->acc.access = isl_map_preimage_domain_multi_pw_aff(
810 expr->acc.access,
811 isl_multi_pw_aff_copy(update));
812 expr->acc.index = isl_multi_pw_aff_pullback_multi_pw_aff(
813 expr->acc.index, update);
814 if (!expr->acc.access || !expr->acc.index)
815 return pet_expr_free(expr);
817 return expr;
820 /* Modify all access relations in "expr" by precomposing them with
821 * the given iteration space transformation.
823 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
824 __isl_take isl_multi_pw_aff *update)
826 expr = pet_expr_map_access(expr, &update_domain, update);
827 isl_multi_pw_aff_free(update);
828 return expr;
831 /* Construct a pet_stmt with given line number and statement
832 * number from a pet_expr.
833 * The initial iteration domain is the zero-dimensional universe.
834 * The name of the domain is given by "label" if it is non-NULL.
835 * Otherwise, the name is constructed as S_<id>.
836 * The domains of all access relations are modified to refer
837 * to the statement iteration domain.
839 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
840 __isl_take isl_id *label, int id, struct pet_expr *expr)
842 struct pet_stmt *stmt;
843 isl_space *dim;
844 isl_set *dom;
845 isl_map *sched;
846 isl_multi_pw_aff *add_name;
847 char name[50];
849 if (!expr)
850 goto error;
852 stmt = isl_calloc_type(ctx, struct pet_stmt);
853 if (!stmt)
854 goto error;
856 dim = isl_space_set_alloc(ctx, 0, 0);
857 if (label)
858 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
859 else {
860 snprintf(name, sizeof(name), "S_%d", id);
861 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
863 dom = isl_set_universe(isl_space_copy(dim));
864 sched = isl_map_from_domain(isl_set_copy(dom));
866 dim = isl_space_from_domain(dim);
867 add_name = isl_multi_pw_aff_zero(dim);
868 expr = expr_update_domain(expr, add_name);
870 stmt->line = line;
871 stmt->domain = dom;
872 stmt->schedule = sched;
873 stmt->body = expr;
875 if (!stmt->domain || !stmt->schedule || !stmt->body)
876 return pet_stmt_free(stmt);
878 return stmt;
879 error:
880 isl_id_free(label);
881 pet_expr_free(expr);
882 return NULL;
885 void *pet_stmt_free(struct pet_stmt *stmt)
887 int i;
889 if (!stmt)
890 return NULL;
892 isl_set_free(stmt->domain);
893 isl_map_free(stmt->schedule);
894 pet_expr_free(stmt->body);
896 for (i = 0; i < stmt->n_arg; ++i)
897 pet_expr_free(stmt->args[i]);
898 free(stmt->args);
900 free(stmt);
901 return NULL;
904 static void stmt_dump(struct pet_stmt *stmt, int indent)
906 int i;
908 if (!stmt)
909 return;
911 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
912 fprintf(stderr, "%*s", indent, "");
913 isl_set_dump(stmt->domain);
914 fprintf(stderr, "%*s", indent, "");
915 isl_map_dump(stmt->schedule);
916 expr_dump(stmt->body, indent);
917 for (i = 0; i < stmt->n_arg; ++i)
918 expr_dump(stmt->args[i], indent + 2);
921 void pet_stmt_dump(struct pet_stmt *stmt)
923 stmt_dump(stmt, 0);
926 /* Allocate a new pet_type with the given "name" and "definition".
928 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
929 const char *definition)
931 struct pet_type *type;
933 type = isl_alloc_type(ctx, struct pet_type);
934 if (!type)
935 return NULL;
937 type->name = strdup(name);
938 type->definition = strdup(definition);
940 if (!type->name || !type->definition)
941 return pet_type_free(type);
943 return type;
946 /* Free "type" and return NULL.
948 struct pet_type *pet_type_free(struct pet_type *type)
950 if (!type)
951 return NULL;
953 free(type->name);
954 free(type->definition);
956 free(type);
957 return NULL;
960 struct pet_array *pet_array_free(struct pet_array *array)
962 if (!array)
963 return NULL;
965 isl_set_free(array->context);
966 isl_set_free(array->extent);
967 isl_set_free(array->value_bounds);
968 free(array->element_type);
970 free(array);
971 return NULL;
974 void pet_array_dump(struct pet_array *array)
976 if (!array)
977 return;
979 isl_set_dump(array->context);
980 isl_set_dump(array->extent);
981 isl_set_dump(array->value_bounds);
982 fprintf(stderr, "%s%s%s\n", array->element_type,
983 array->element_is_record ? " element-is-record" : "",
984 array->live_out ? " live-out" : "");
987 /* Alloc a pet_scop structure, with extra room for information that
988 * is only used during parsing.
990 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
992 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
995 /* Construct a pet_scop with room for n statements.
997 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
999 isl_space *space;
1000 struct pet_scop *scop;
1002 scop = pet_scop_alloc(ctx);
1003 if (!scop)
1004 return NULL;
1006 space = isl_space_params_alloc(ctx, 0);
1007 scop->context = isl_set_universe(isl_space_copy(space));
1008 scop->context_value = isl_set_universe(space);
1009 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
1010 if (!scop->context || !scop->stmts)
1011 return pet_scop_free(scop);
1013 scop->n_stmt = n;
1015 return scop;
1018 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
1020 return scop_alloc(ctx, 0);
1023 /* Update "context" with respect to the valid parameter values for "access".
1025 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
1026 __isl_take isl_set *context)
1028 context = isl_set_intersect(context,
1029 isl_map_params(isl_map_copy(access)));
1030 return context;
1033 /* Update "context" with respect to the valid parameter values for "expr".
1035 * If "expr" represents a ternary operator, then a parameter value
1036 * needs to be valid for the condition and for at least one of the
1037 * remaining two arguments.
1038 * If the condition is an affine expression, then we can be a bit more specific.
1039 * The parameter then has to be valid for the second argument for
1040 * non-zero accesses and valid for the third argument for zero accesses.
1042 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
1043 __isl_take isl_set *context)
1045 int i;
1047 if (expr->type == pet_expr_ternary) {
1048 int is_aff;
1049 isl_set *context1, *context2;
1051 is_aff = pet_expr_is_affine(expr->args[0]);
1052 if (is_aff < 0)
1053 goto error;
1055 context = expr_extract_context(expr->args[0], context);
1056 context1 = expr_extract_context(expr->args[1],
1057 isl_set_copy(context));
1058 context2 = expr_extract_context(expr->args[2], context);
1060 if (is_aff) {
1061 isl_map *access;
1062 isl_set *zero_set;
1064 access = isl_map_copy(expr->args[0]->acc.access);
1065 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
1066 zero_set = isl_map_params(access);
1067 context1 = isl_set_subtract(context1,
1068 isl_set_copy(zero_set));
1069 context2 = isl_set_intersect(context2, zero_set);
1072 context = isl_set_union(context1, context2);
1073 context = isl_set_coalesce(context);
1075 return context;
1078 for (i = 0; i < expr->n_arg; ++i)
1079 context = expr_extract_context(expr->args[i], context);
1081 if (expr->type == pet_expr_access)
1082 context = access_extract_context(expr->acc.access, context);
1084 return context;
1085 error:
1086 isl_set_free(context);
1087 return NULL;
1090 /* Update "context" with respect to the valid parameter values for "stmt".
1092 * If the statement is an assume statement with an affine expression,
1093 * then intersect "context" with that expression.
1094 * Otherwise, intersect "context" with the contexts of the expressions
1095 * inside "stmt".
1097 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
1098 __isl_take isl_set *context)
1100 int i;
1102 if (pet_stmt_is_assume(stmt) &&
1103 pet_expr_is_affine(stmt->body->args[0])) {
1104 isl_multi_pw_aff *index;
1105 isl_pw_aff *pa;
1106 isl_set *cond;
1108 index = stmt->body->args[0]->acc.index;
1109 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
1110 cond = isl_set_params(isl_pw_aff_non_zero_set(pa));
1111 return isl_set_intersect(context, cond);
1114 for (i = 0; i < stmt->n_arg; ++i)
1115 context = expr_extract_context(stmt->args[i], context);
1117 context = expr_extract_context(stmt->body, context);
1119 return context;
1122 /* Construct a pet_scop that contains the given pet_stmt.
1124 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
1126 struct pet_scop *scop;
1128 if (!stmt)
1129 return NULL;
1131 scop = scop_alloc(ctx, 1);
1132 if (!scop)
1133 goto error;
1135 scop->context = stmt_extract_context(stmt, scop->context);
1136 if (!scop->context)
1137 goto error;
1139 scop->stmts[0] = stmt;
1141 return scop;
1142 error:
1143 pet_stmt_free(stmt);
1144 pet_scop_free(scop);
1145 return NULL;
1148 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
1149 * does it represent an affine expression?
1151 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
1153 int has_id;
1155 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
1156 if (has_id < 0)
1157 return -1;
1159 return !has_id;
1162 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
1164 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
1165 __isl_take isl_set *dom)
1167 isl_pw_aff *pa;
1168 pa = isl_set_indicator_function(set);
1169 pa = isl_pw_aff_intersect_domain(pa, dom);
1170 return pa;
1173 /* Return "lhs || rhs", defined on the shared definition domain.
1175 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
1176 __isl_take isl_pw_aff *rhs)
1178 isl_set *cond;
1179 isl_set *dom;
1181 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
1182 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
1183 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
1184 isl_pw_aff_non_zero_set(rhs));
1185 cond = isl_set_coalesce(cond);
1186 return indicator_function(cond, dom);
1189 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
1190 * ext may be equal to either ext1 or ext2.
1192 * The two skips that need to be combined are assumed to be affine expressions.
1194 * We need to skip in ext if we need to skip in either ext1 or ext2.
1195 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
1197 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
1198 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
1199 enum pet_skip type)
1201 isl_pw_aff *skip, *skip1, *skip2;
1203 if (!ext)
1204 return NULL;
1205 if (!ext1->skip[type] && !ext2->skip[type])
1206 return ext;
1207 if (!ext1->skip[type]) {
1208 if (ext == ext2)
1209 return ext;
1210 ext->skip[type] = ext2->skip[type];
1211 ext2->skip[type] = NULL;
1212 return ext;
1214 if (!ext2->skip[type]) {
1215 if (ext == ext1)
1216 return ext;
1217 ext->skip[type] = ext1->skip[type];
1218 ext1->skip[type] = NULL;
1219 return ext;
1222 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
1223 !multi_pw_aff_is_affine(ext2->skip[type]))
1224 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
1225 isl_error_internal, "can only combine affine skips",
1226 goto error);
1228 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
1229 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
1230 skip = pw_aff_or(skip1, skip2);
1231 isl_multi_pw_aff_free(ext1->skip[type]);
1232 ext1->skip[type] = NULL;
1233 isl_multi_pw_aff_free(ext2->skip[type]);
1234 ext2->skip[type] = NULL;
1235 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1236 if (!ext->skip[type])
1237 goto error;
1239 return ext;
1240 error:
1241 pet_scop_free(&ext->scop);
1242 return NULL;
1245 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
1246 * where type takes on the values pet_skip_now and pet_skip_later.
1247 * scop may be equal to either scop1 or scop2.
1249 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
1250 struct pet_scop *scop1, struct pet_scop *scop2)
1252 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1253 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
1254 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
1256 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
1257 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
1258 return &ext->scop;
1261 /* Update scop->start and scop->end to include the region from "start"
1262 * to "end". In particular, if scop->end == 0, then "scop" does not
1263 * have any offset information yet and we simply take the information
1264 * from "start" and "end". Otherwise, we update the fields if the
1265 * region from "start" to "end" is not already included.
1267 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
1268 unsigned start, unsigned end)
1270 if (!scop)
1271 return NULL;
1272 if (scop->end == 0) {
1273 scop->start = start;
1274 scop->end = end;
1275 } else {
1276 if (start < scop->start)
1277 scop->start = start;
1278 if (end > scop->end)
1279 scop->end = end;
1282 return scop;
1285 /* Does "implication" appear in the list of implications of "scop"?
1287 static int is_known_implication(struct pet_scop *scop,
1288 struct pet_implication *implication)
1290 int i;
1292 for (i = 0; i < scop->n_implication; ++i) {
1293 struct pet_implication *pi = scop->implications[i];
1294 int equal;
1296 if (pi->satisfied != implication->satisfied)
1297 continue;
1298 equal = isl_map_is_equal(pi->extension, implication->extension);
1299 if (equal < 0)
1300 return -1;
1301 if (equal)
1302 return 1;
1305 return 0;
1308 /* Store the concatenation of the impliciations of "scop1" and "scop2"
1309 * in "scop", removing duplicates (i.e., implications in "scop2" that
1310 * already appear in "scop1").
1312 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
1313 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
1315 int i, j;
1317 if (!scop)
1318 return NULL;
1320 if (scop2->n_implication == 0) {
1321 scop->n_implication = scop1->n_implication;
1322 scop->implications = scop1->implications;
1323 scop1->n_implication = 0;
1324 scop1->implications = NULL;
1325 return scop;
1328 if (scop1->n_implication == 0) {
1329 scop->n_implication = scop2->n_implication;
1330 scop->implications = scop2->implications;
1331 scop2->n_implication = 0;
1332 scop2->implications = NULL;
1333 return scop;
1336 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1337 scop1->n_implication + scop2->n_implication);
1338 if (!scop->implications)
1339 return pet_scop_free(scop);
1341 for (i = 0; i < scop1->n_implication; ++i) {
1342 scop->implications[i] = scop1->implications[i];
1343 scop1->implications[i] = NULL;
1346 scop->n_implication = scop1->n_implication;
1347 j = scop1->n_implication;
1348 for (i = 0; i < scop2->n_implication; ++i) {
1349 int known;
1351 known = is_known_implication(scop, scop2->implications[i]);
1352 if (known < 0)
1353 return pet_scop_free(scop);
1354 if (known)
1355 continue;
1356 scop->implications[j++] = scop2->implications[i];
1357 scop2->implications[i] = NULL;
1359 scop->n_implication = j;
1361 return scop;
1364 /* Combine the offset information of "scop1" and "scop2" into "scop".
1366 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
1367 struct pet_scop *scop1, struct pet_scop *scop2)
1369 if (scop1->end)
1370 scop = pet_scop_update_start_end(scop,
1371 scop1->start, scop1->end);
1372 if (scop2->end)
1373 scop = pet_scop_update_start_end(scop,
1374 scop2->start, scop2->end);
1375 return scop;
1378 /* Construct a pet_scop that contains the offset information,
1379 * arrays, statements and skip information in "scop1" and "scop2".
1381 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
1382 struct pet_scop *scop2)
1384 int i;
1385 struct pet_scop *scop = NULL;
1387 if (!scop1 || !scop2)
1388 goto error;
1390 if (scop1->n_stmt == 0) {
1391 scop2 = scop_combine_skips(scop2, scop1, scop2);
1392 pet_scop_free(scop1);
1393 return scop2;
1396 if (scop2->n_stmt == 0) {
1397 scop1 = scop_combine_skips(scop1, scop1, scop2);
1398 pet_scop_free(scop2);
1399 return scop1;
1402 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
1403 if (!scop)
1404 goto error;
1406 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
1407 scop1->n_array + scop2->n_array);
1408 if (!scop->arrays)
1409 goto error;
1410 scop->n_array = scop1->n_array + scop2->n_array;
1412 for (i = 0; i < scop1->n_stmt; ++i) {
1413 scop->stmts[i] = scop1->stmts[i];
1414 scop1->stmts[i] = NULL;
1417 for (i = 0; i < scop2->n_stmt; ++i) {
1418 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
1419 scop2->stmts[i] = NULL;
1422 for (i = 0; i < scop1->n_array; ++i) {
1423 scop->arrays[i] = scop1->arrays[i];
1424 scop1->arrays[i] = NULL;
1427 for (i = 0; i < scop2->n_array; ++i) {
1428 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
1429 scop2->arrays[i] = NULL;
1432 scop = scop_collect_implications(ctx, scop, scop1, scop2);
1433 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
1434 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
1435 scop = scop_combine_skips(scop, scop1, scop2);
1436 scop = scop_combine_start_end(scop, scop1, scop2);
1438 pet_scop_free(scop1);
1439 pet_scop_free(scop2);
1440 return scop;
1441 error:
1442 pet_scop_free(scop1);
1443 pet_scop_free(scop2);
1444 pet_scop_free(scop);
1445 return NULL;
1448 /* Apply the skip condition "skip" to "scop".
1449 * That is, make sure "scop" is not executed when the condition holds.
1451 * If "skip" is an affine expression, we add the conditions under
1452 * which the expression is zero to the iteration domains.
1453 * Otherwise, we add a filter on the variable attaining the value zero.
1455 static struct pet_scop *restrict_skip(struct pet_scop *scop,
1456 __isl_take isl_multi_pw_aff *skip)
1458 isl_set *zero;
1459 isl_pw_aff *pa;
1460 int is_aff;
1462 if (!scop || !skip)
1463 goto error;
1465 is_aff = multi_pw_aff_is_affine(skip);
1466 if (is_aff < 0)
1467 goto error;
1469 if (!is_aff)
1470 return pet_scop_filter(scop, skip, 0);
1472 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1473 isl_multi_pw_aff_free(skip);
1474 zero = isl_set_params(isl_pw_aff_zero_set(pa));
1475 scop = pet_scop_restrict(scop, zero);
1477 return scop;
1478 error:
1479 isl_multi_pw_aff_free(skip);
1480 return pet_scop_free(scop);
1483 /* Construct a pet_scop that contains the arrays, statements and
1484 * skip information in "scop1" and "scop2", where the two scops
1485 * are executed "in sequence". That is, breaks and continues
1486 * in scop1 have an effect on scop2.
1488 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1489 struct pet_scop *scop2)
1491 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1492 scop2 = restrict_skip(scop2,
1493 pet_scop_get_skip(scop1, pet_skip_now));
1494 return pet_scop_add(ctx, scop1, scop2);
1497 /* Construct a pet_scop that contains the arrays, statements and
1498 * skip information in "scop1" and "scop2", where the two scops
1499 * are executed "in parallel". That is, any break or continue
1500 * in scop1 has no effect on scop2.
1502 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1503 struct pet_scop *scop2)
1505 return pet_scop_add(ctx, scop1, scop2);
1508 void *pet_implication_free(struct pet_implication *implication)
1510 int i;
1512 if (!implication)
1513 return NULL;
1515 isl_map_free(implication->extension);
1517 free(implication);
1518 return NULL;
1521 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1523 int i;
1524 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1526 if (!scop)
1527 return NULL;
1528 isl_set_free(scop->context);
1529 isl_set_free(scop->context_value);
1530 if (scop->types)
1531 for (i = 0; i < scop->n_type; ++i)
1532 pet_type_free(scop->types[i]);
1533 free(scop->types);
1534 if (scop->arrays)
1535 for (i = 0; i < scop->n_array; ++i)
1536 pet_array_free(scop->arrays[i]);
1537 free(scop->arrays);
1538 if (scop->stmts)
1539 for (i = 0; i < scop->n_stmt; ++i)
1540 pet_stmt_free(scop->stmts[i]);
1541 free(scop->stmts);
1542 if (scop->implications)
1543 for (i = 0; i < scop->n_implication; ++i)
1544 pet_implication_free(scop->implications[i]);
1545 free(scop->implications);
1546 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1547 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1548 free(scop);
1549 return NULL;
1552 void pet_type_dump(struct pet_type *type)
1554 if (!type)
1555 return;
1557 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1560 void pet_implication_dump(struct pet_implication *implication)
1562 if (!implication)
1563 return;
1565 fprintf(stderr, "%d\n", implication->satisfied);
1566 isl_map_dump(implication->extension);
1569 void pet_scop_dump(struct pet_scop *scop)
1571 int i;
1572 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1574 if (!scop)
1575 return;
1577 isl_set_dump(scop->context);
1578 isl_set_dump(scop->context_value);
1579 for (i = 0; i < scop->n_type; ++i)
1580 pet_type_dump(scop->types[i]);
1581 for (i = 0; i < scop->n_array; ++i)
1582 pet_array_dump(scop->arrays[i]);
1583 for (i = 0; i < scop->n_stmt; ++i)
1584 pet_stmt_dump(scop->stmts[i]);
1585 for (i = 0; i < scop->n_implication; ++i)
1586 pet_implication_dump(scop->implications[i]);
1588 if (ext->skip[0]) {
1589 fprintf(stderr, "skip\n");
1590 isl_multi_pw_aff_dump(ext->skip[0]);
1591 isl_multi_pw_aff_dump(ext->skip[1]);
1595 /* Return 1 if the two pet_arrays are equivalent.
1597 * We don't compare element_size as this may be target dependent.
1599 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1601 if (!array1 || !array2)
1602 return 0;
1604 if (!isl_set_is_equal(array1->context, array2->context))
1605 return 0;
1606 if (!isl_set_is_equal(array1->extent, array2->extent))
1607 return 0;
1608 if (!!array1->value_bounds != !!array2->value_bounds)
1609 return 0;
1610 if (array1->value_bounds &&
1611 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1612 return 0;
1613 if (strcmp(array1->element_type, array2->element_type))
1614 return 0;
1615 if (array1->element_is_record != array2->element_is_record)
1616 return 0;
1617 if (array1->live_out != array2->live_out)
1618 return 0;
1619 if (array1->uniquely_defined != array2->uniquely_defined)
1620 return 0;
1621 if (array1->declared != array2->declared)
1622 return 0;
1623 if (array1->exposed != array2->exposed)
1624 return 0;
1626 return 1;
1629 /* Return 1 if the two pet_stmts are equivalent.
1631 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1633 int i;
1635 if (!stmt1 || !stmt2)
1636 return 0;
1638 if (stmt1->line != stmt2->line)
1639 return 0;
1640 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1641 return 0;
1642 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1643 return 0;
1644 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
1645 return 0;
1646 if (stmt1->n_arg != stmt2->n_arg)
1647 return 0;
1648 for (i = 0; i < stmt1->n_arg; ++i) {
1649 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1650 return 0;
1653 return 1;
1656 /* Return 1 if the two pet_types are equivalent.
1658 * We only compare the names of the types since the exact representation
1659 * of the definition may depend on the version of clang being used.
1661 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1663 if (!type1 || !type2)
1664 return 0;
1666 if (strcmp(type1->name, type2->name))
1667 return 0;
1669 return 1;
1672 /* Return 1 if the two pet_implications are equivalent.
1674 int pet_implication_is_equal(struct pet_implication *implication1,
1675 struct pet_implication *implication2)
1677 if (!implication1 || !implication2)
1678 return 0;
1680 if (implication1->satisfied != implication2->satisfied)
1681 return 0;
1682 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1683 return 0;
1685 return 1;
1688 /* Return 1 if the two pet_scops are equivalent.
1690 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1692 int i;
1694 if (!scop1 || !scop2)
1695 return 0;
1697 if (!isl_set_is_equal(scop1->context, scop2->context))
1698 return 0;
1699 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1700 return 0;
1702 if (scop1->n_type != scop2->n_type)
1703 return 0;
1704 for (i = 0; i < scop1->n_type; ++i)
1705 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1706 return 0;
1708 if (scop1->n_array != scop2->n_array)
1709 return 0;
1710 for (i = 0; i < scop1->n_array; ++i)
1711 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1712 return 0;
1714 if (scop1->n_stmt != scop2->n_stmt)
1715 return 0;
1716 for (i = 0; i < scop1->n_stmt; ++i)
1717 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1718 return 0;
1720 if (scop1->n_implication != scop2->n_implication)
1721 return 0;
1722 for (i = 0; i < scop1->n_implication; ++i)
1723 if (!pet_implication_is_equal(scop1->implications[i],
1724 scop2->implications[i]))
1725 return 0;
1727 return 1;
1730 /* Prefix the schedule of "stmt" with an extra dimension with constant
1731 * value "pos".
1733 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1735 if (!stmt)
1736 return NULL;
1738 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1739 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1740 if (!stmt->schedule)
1741 return pet_stmt_free(stmt);
1743 return stmt;
1746 /* Prefix the schedules of all statements in "scop" with an extra
1747 * dimension with constant value "pos".
1749 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1751 int i;
1753 if (!scop)
1754 return NULL;
1756 for (i = 0; i < scop->n_stmt; ++i) {
1757 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1758 if (!scop->stmts[i])
1759 return pet_scop_free(scop);
1762 return scop;
1765 /* Given a set with a parameter at "param_pos" that refers to the
1766 * iterator, "move" the iterator to the first set dimension.
1767 * That is, essentially equate the parameter to the first set dimension
1768 * and then project it out.
1770 * The first set dimension may however refer to a virtual iterator,
1771 * while the parameter refers to the "real" iterator.
1772 * We therefore need to take into account the affine expression "iv_map", which
1773 * expresses the real iterator in terms of the virtual iterator.
1774 * In particular, we equate the set dimension to the input of the map
1775 * and the parameter to the output of the map and then project out
1776 * everything we don't need anymore.
1778 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1779 int param_pos, __isl_take isl_aff *iv_map)
1781 isl_map *map, *map2;
1782 map = isl_map_from_domain(set);
1783 map = isl_map_add_dims(map, isl_dim_out, 1);
1784 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1785 map2 = isl_map_from_aff(iv_map);
1786 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1787 map = isl_map_apply_range(map, map2);
1788 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1789 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1790 return isl_map_domain(map);
1793 /* Data used in embed_access.
1794 * extend adds an iterator to the iteration domain (through precomposition).
1795 * iv_map expresses the real iterator in terms of the virtual iterator
1796 * var_id represents the induction variable of the corresponding loop
1798 struct pet_embed_access {
1799 isl_multi_pw_aff *extend;
1800 isl_aff *iv_map;
1801 isl_id *var_id;
1804 /* Given an index expression, return an expression for the outer iterator.
1806 static __isl_give isl_aff *index_outer_iterator(
1807 __isl_take isl_multi_pw_aff *index)
1809 isl_space *space;
1810 isl_local_space *ls;
1812 space = isl_multi_pw_aff_get_domain_space(index);
1813 isl_multi_pw_aff_free(index);
1815 ls = isl_local_space_from_space(space);
1816 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1819 /* Replace an index expression that references the new (outer) iterator variable
1820 * by one that references the corresponding (real) iterator.
1822 * The input index expression is of the form
1824 * { S[i',...] -> i[] }
1826 * where i' refers to the virtual iterator.
1828 * iv_map is of the form
1830 * { [i'] -> [i] }
1832 * Return the index expression
1834 * { S[i',...] -> [i] }
1836 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1837 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1839 isl_space *space;
1840 isl_aff *aff;
1842 aff = index_outer_iterator(index);
1843 space = isl_aff_get_space(aff);
1844 iv_map = isl_aff_align_params(iv_map, space);
1845 aff = isl_aff_pullback_aff(iv_map, aff);
1847 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1850 /* Given an index expression "index" that refers to the (real) iterator
1851 * through the parameter at position "pos", plug in "iv_map", expressing
1852 * the real iterator in terms of the virtual (outer) iterator.
1854 * In particular, the index expression is of the form
1856 * [..., i, ...] -> { S[i',...] -> ... i ... }
1858 * where i refers to the real iterator and i' refers to the virtual iterator.
1860 * iv_map is of the form
1862 * { [i'] -> [i] }
1864 * Return the index expression
1866 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1869 * We first move the parameter to the input
1871 * [..., ...] -> { [i, i',...] -> ... i ... }
1873 * and construct
1875 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1877 * and then combine the two to obtain the desired result.
1879 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1880 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1882 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1883 isl_multi_aff *ma;
1885 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1886 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1887 isl_dim_param, pos, 1);
1889 space = isl_space_map_from_set(space);
1890 ma = isl_multi_aff_identity(isl_space_copy(space));
1891 iv_map = isl_aff_align_params(iv_map, space);
1892 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1893 ma = isl_multi_aff_flat_range_product(
1894 isl_multi_aff_from_aff(iv_map), ma);
1895 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1897 return index;
1900 /* Does the index expression "index" reference a virtual array, i.e.,
1901 * one with user pointer equal to NULL?
1902 * A virtual array does not have any members.
1904 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff *index)
1906 isl_id *id;
1907 int is_virtual;
1909 if (!isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1910 return 0;
1911 if (isl_multi_pw_aff_range_is_wrapping(index))
1912 return 0;
1913 id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1914 is_virtual = !isl_id_get_user(id);
1915 isl_id_free(id);
1917 return is_virtual;
1920 /* Does the access relation "access" reference a virtual array, i.e.,
1921 * one with user pointer equal to NULL?
1922 * A virtual array does not have any members.
1924 static int access_is_virtual_array(__isl_keep isl_map *access)
1926 isl_id *id;
1927 int is_virtual;
1929 if (!isl_map_has_tuple_id(access, isl_dim_out))
1930 return 0;
1931 if (isl_map_range_is_wrapping(access))
1932 return 0;
1933 id = isl_map_get_tuple_id(access, isl_dim_out);
1934 is_virtual = !isl_id_get_user(id);
1935 isl_id_free(id);
1937 return is_virtual;
1940 /* Embed the given index expression in an extra outer loop.
1941 * The domain of the index expression has already been updated.
1943 * If the access refers to the induction variable, then it is
1944 * turned into an access to the set of integers with index (and value)
1945 * equal to the induction variable.
1947 * If the accessed array is a virtual array (with user
1948 * pointer equal to NULL), as created by create_test_index,
1949 * then it is extended along with the domain of the index expression.
1951 static __isl_give isl_multi_pw_aff *embed_index_expression(
1952 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1954 isl_id *array_id = NULL;
1955 int pos;
1957 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1958 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1959 if (array_id == data->var_id) {
1960 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1961 } else if (index_is_virtual_array(index)) {
1962 isl_aff *aff;
1963 isl_multi_pw_aff *mpa;
1965 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1966 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1967 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1968 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1969 isl_id_copy(array_id));
1971 isl_id_free(array_id);
1973 pos = isl_multi_pw_aff_find_dim_by_id(index,
1974 isl_dim_param, data->var_id);
1975 if (pos >= 0)
1976 index = index_internalize_iv(index, pos,
1977 isl_aff_copy(data->iv_map));
1978 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1979 isl_id_copy(data->var_id));
1981 return index;
1984 /* Embed the given access relation in an extra outer loop.
1985 * The domain of the access relation has already been updated.
1987 * If the access refers to the induction variable, then it is
1988 * turned into an access to the set of integers with index (and value)
1989 * equal to the induction variable.
1991 * If the induction variable appears in the constraints (as a parameter),
1992 * then the parameter is equated to the newly introduced iteration
1993 * domain dimension and subsequently projected out.
1995 * Similarly, if the accessed array is a virtual array (with user
1996 * pointer equal to NULL), as created by create_test_index,
1997 * then it is extended along with the domain of the access.
1999 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
2000 struct pet_embed_access *data)
2002 isl_id *array_id = NULL;
2003 int pos;
2005 if (isl_map_has_tuple_id(access, isl_dim_out))
2006 array_id = isl_map_get_tuple_id(access, isl_dim_out);
2007 if (array_id == data->var_id || access_is_virtual_array(access)) {
2008 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
2009 access = isl_map_equate(access,
2010 isl_dim_in, 0, isl_dim_out, 0);
2011 if (array_id == data->var_id)
2012 access = isl_map_apply_range(access,
2013 isl_map_from_aff(isl_aff_copy(data->iv_map)));
2014 else
2015 access = isl_map_set_tuple_id(access, isl_dim_out,
2016 isl_id_copy(array_id));
2018 isl_id_free(array_id);
2020 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
2021 if (pos >= 0) {
2022 isl_set *set = isl_map_wrap(access);
2023 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
2024 access = isl_set_unwrap(set);
2026 access = isl_map_set_dim_id(access, isl_dim_in, 0,
2027 isl_id_copy(data->var_id));
2029 return access;
2032 /* Given an access expression, embed the associated access relation and
2033 * index expression in an extra outer loop.
2035 * We first update the domains to insert the extra dimension and
2036 * then update the access relation and index expression to take
2037 * into account the mapping "iv_map" from virtual iterator
2038 * to real iterator.
2040 static struct pet_expr *embed_access(struct pet_expr *expr, void *user)
2042 int dim;
2043 struct pet_embed_access *data = user;
2045 expr = update_domain(expr, data->extend);
2046 if (!expr)
2047 return NULL;
2049 expr->acc.access = embed_access_relation(expr->acc.access, data);
2050 expr->acc.index = embed_index_expression(expr->acc.index, data);
2051 if (!expr->acc.access || !expr->acc.index)
2052 return pet_expr_free(expr);
2054 return expr;
2057 /* Embed all access subexpressions of "expr" in an extra loop.
2058 * "extend" inserts an outer loop iterator in the iteration domains
2059 * (through precomposition).
2060 * "iv_map" expresses the real iterator in terms of the virtual iterator
2061 * "var_id" represents the induction variable.
2063 static struct pet_expr *expr_embed(struct pet_expr *expr,
2064 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
2065 __isl_keep isl_id *var_id)
2067 struct pet_embed_access data =
2068 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
2070 expr = pet_expr_map_access(expr, &embed_access, &data);
2071 isl_aff_free(iv_map);
2072 isl_multi_pw_aff_free(extend);
2073 return expr;
2076 /* Embed the given pet_stmt in an extra outer loop with iteration domain
2077 * "dom" and schedule "sched". "var_id" represents the induction variable
2078 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
2079 * That is, it expresses the iterator that some of the parameters in "stmt"
2080 * may refer to in terms of the iterator used in "dom" and
2081 * the domain of "sched".
2083 * The iteration domain and schedule of the statement are updated
2084 * according to the iteration domain and schedule of the new loop.
2085 * If stmt->domain is a wrapped map, then the iteration domain
2086 * is the domain of this map, so we need to be careful to adjust
2087 * this domain.
2089 * If the induction variable appears in the constraints (as a parameter)
2090 * of the current iteration domain or the schedule of the statement,
2091 * then the parameter is equated to the newly introduced iteration
2092 * domain dimension and subsequently projected out.
2094 * Finally, all access relations are updated based on the extra loop.
2096 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
2097 __isl_take isl_set *dom, __isl_take isl_map *sched,
2098 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
2100 int i;
2101 int pos;
2102 isl_id *stmt_id;
2103 isl_space *dim;
2104 isl_multi_pw_aff *extend;
2106 if (!stmt)
2107 goto error;
2109 if (isl_set_is_wrapping(stmt->domain)) {
2110 isl_map *map;
2111 isl_map *ext;
2112 isl_space *ran_dim;
2114 map = isl_set_unwrap(stmt->domain);
2115 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
2116 ran_dim = isl_space_range(isl_map_get_space(map));
2117 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
2118 isl_set_universe(ran_dim));
2119 map = isl_map_flat_domain_product(ext, map);
2120 map = isl_map_set_tuple_id(map, isl_dim_in,
2121 isl_id_copy(stmt_id));
2122 dim = isl_space_domain(isl_map_get_space(map));
2123 stmt->domain = isl_map_wrap(map);
2124 } else {
2125 stmt_id = isl_set_get_tuple_id(stmt->domain);
2126 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
2127 stmt->domain);
2128 stmt->domain = isl_set_set_tuple_id(stmt->domain,
2129 isl_id_copy(stmt_id));
2130 dim = isl_set_get_space(stmt->domain);
2133 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
2134 if (pos >= 0)
2135 stmt->domain = internalize_iv(stmt->domain, pos,
2136 isl_aff_copy(iv_map));
2138 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
2139 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
2140 isl_dim_in, stmt_id);
2142 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
2143 if (pos >= 0) {
2144 isl_set *set = isl_map_wrap(stmt->schedule);
2145 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
2146 stmt->schedule = isl_set_unwrap(set);
2149 dim = isl_space_map_from_set(dim);
2150 extend = isl_multi_pw_aff_identity(dim);
2151 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
2152 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
2153 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
2154 for (i = 0; i < stmt->n_arg; ++i)
2155 stmt->args[i] = expr_embed(stmt->args[i],
2156 isl_multi_pw_aff_copy(extend),
2157 isl_aff_copy(iv_map), var_id);
2158 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
2160 isl_set_free(dom);
2161 isl_id_free(var_id);
2163 for (i = 0; i < stmt->n_arg; ++i)
2164 if (!stmt->args[i])
2165 return pet_stmt_free(stmt);
2166 if (!stmt->domain || !stmt->schedule || !stmt->body)
2167 return pet_stmt_free(stmt);
2168 return stmt;
2169 error:
2170 isl_set_free(dom);
2171 isl_map_free(sched);
2172 isl_aff_free(iv_map);
2173 isl_id_free(var_id);
2174 return NULL;
2177 /* Embed the given pet_array in an extra outer loop with iteration domain
2178 * "dom".
2179 * This embedding only has an effect on virtual arrays (those with
2180 * user pointer equal to NULL), which need to be extended along with
2181 * the iteration domain.
2183 static struct pet_array *pet_array_embed(struct pet_array *array,
2184 __isl_take isl_set *dom)
2186 isl_id *array_id = NULL;
2188 if (!array)
2189 goto error;
2191 if (isl_set_has_tuple_id(array->extent))
2192 array_id = isl_set_get_tuple_id(array->extent);
2194 if (array_id && !isl_id_get_user(array_id)) {
2195 array->extent = isl_set_flat_product(dom, array->extent);
2196 array->extent = isl_set_set_tuple_id(array->extent, array_id);
2197 if (!array->extent)
2198 return pet_array_free(array);
2199 } else {
2200 isl_set_free(dom);
2201 isl_id_free(array_id);
2204 return array;
2205 error:
2206 isl_set_free(dom);
2207 return NULL;
2210 /* Project out all unnamed parameters from "set" and return the result.
2212 static __isl_give isl_set *set_project_out_unnamed_params(
2213 __isl_take isl_set *set)
2215 int i, n;
2217 n = isl_set_dim(set, isl_dim_param);
2218 for (i = n - 1; i >= 0; --i) {
2219 if (isl_set_has_dim_name(set, isl_dim_param, i))
2220 continue;
2221 set = isl_set_project_out(set, isl_dim_param, i, 1);
2224 return set;
2227 /* Update the context with respect to an embedding into a loop
2228 * with iteration domain "dom" and induction variable "id".
2229 * "iv_map" expresses the real iterator (parameter "id") in terms
2230 * of a possibly virtual iterator (used in "dom").
2232 * If the current context is independent of "id", we don't need
2233 * to do anything.
2234 * Otherwise, a parameter value is invalid for the embedding if
2235 * any of the corresponding iterator values is invalid.
2236 * That is, a parameter value is valid only if all the corresponding
2237 * iterator values are valid.
2238 * We therefore compute the set of parameters
2240 * forall i in dom : valid (i)
2242 * or
2244 * not exists i in dom : not valid(i)
2246 * i.e.,
2248 * not exists i in dom \ valid(i)
2250 * Before we subtract valid(i) from dom, we first need to substitute
2251 * the real iterator for the virtual iterator.
2253 * If there are any unnamed parameters in "dom", then we consider
2254 * a parameter value to be valid if it is valid for any value of those
2255 * unnamed parameters. They are therefore projected out at the end.
2257 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
2258 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
2259 __isl_keep isl_id *id)
2261 int pos;
2262 isl_multi_aff *ma;
2264 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
2265 if (pos < 0)
2266 return context;
2268 context = isl_set_from_params(context);
2269 context = isl_set_add_dims(context, isl_dim_set, 1);
2270 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
2271 context = isl_set_project_out(context, isl_dim_param, pos, 1);
2272 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
2273 context = isl_set_preimage_multi_aff(context, ma);
2274 context = isl_set_subtract(isl_set_copy(dom), context);
2275 context = isl_set_params(context);
2276 context = isl_set_complement(context);
2277 context = set_project_out_unnamed_params(context);
2278 return context;
2281 /* Update the implication with respect to an embedding into a loop
2282 * with iteration domain "dom".
2284 * Since embed_access extends virtual arrays along with the domain
2285 * of the access, we need to do the same with domain and range
2286 * of the implication. Since the original implication is only valid
2287 * within a given iteration of the loop, the extended implication
2288 * maps the extra array dimension corresponding to the extra loop
2289 * to itself.
2291 static struct pet_implication *pet_implication_embed(
2292 struct pet_implication *implication, __isl_take isl_set *dom)
2294 isl_id *id;
2295 isl_map *map;
2297 if (!implication)
2298 goto error;
2300 map = isl_set_identity(dom);
2301 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
2302 map = isl_map_flat_product(map, implication->extension);
2303 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
2304 map = isl_map_set_tuple_id(map, isl_dim_out, id);
2305 implication->extension = map;
2306 if (!implication->extension)
2307 return pet_implication_free(implication);
2309 return implication;
2310 error:
2311 isl_set_free(dom);
2312 return NULL;
2315 /* Embed all statements and arrays in "scop" in an extra outer loop
2316 * with iteration domain "dom" and schedule "sched".
2317 * "id" represents the induction variable of the loop.
2318 * "iv_map" maps a possibly virtual iterator to the real iterator.
2319 * That is, it expresses the iterator that some of the parameters in "scop"
2320 * may refer to in terms of the iterator used in "dom" and
2321 * the domain of "sched".
2323 * Any skip conditions within the loop have no effect outside of the loop.
2324 * The caller is responsible for making sure skip[pet_skip_later] has been
2325 * taken into account.
2327 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
2328 __isl_take isl_map *sched, __isl_take isl_aff *iv_map,
2329 __isl_take isl_id *id)
2331 int i;
2333 if (!scop)
2334 goto error;
2336 pet_scop_reset_skip(scop, pet_skip_now);
2337 pet_scop_reset_skip(scop, pet_skip_later);
2339 scop->context = context_embed(scop->context, dom, iv_map, id);
2340 if (!scop->context)
2341 goto error;
2343 for (i = 0; i < scop->n_stmt; ++i) {
2344 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
2345 isl_set_copy(dom), isl_map_copy(sched),
2346 isl_aff_copy(iv_map), isl_id_copy(id));
2347 if (!scop->stmts[i])
2348 goto error;
2351 for (i = 0; i < scop->n_array; ++i) {
2352 scop->arrays[i] = pet_array_embed(scop->arrays[i],
2353 isl_set_copy(dom));
2354 if (!scop->arrays[i])
2355 goto error;
2358 for (i = 0; i < scop->n_implication; ++i) {
2359 scop->implications[i] =
2360 pet_implication_embed(scop->implications[i],
2361 isl_set_copy(dom));
2362 if (!scop->implications[i])
2363 goto error;
2366 isl_set_free(dom);
2367 isl_map_free(sched);
2368 isl_aff_free(iv_map);
2369 isl_id_free(id);
2370 return scop;
2371 error:
2372 isl_set_free(dom);
2373 isl_map_free(sched);
2374 isl_aff_free(iv_map);
2375 isl_id_free(id);
2376 return pet_scop_free(scop);
2379 /* Add extra conditions on the parameters to iteration domain of "stmt".
2381 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
2382 __isl_take isl_set *cond)
2384 if (!stmt)
2385 goto error;
2387 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
2389 return stmt;
2390 error:
2391 isl_set_free(cond);
2392 return pet_stmt_free(stmt);
2395 /* Add extra conditions to scop->skip[type].
2397 * The new skip condition only holds if it held before
2398 * and the condition is true. It does not hold if it did not hold
2399 * before or the condition is false.
2401 * The skip condition is assumed to be an affine expression.
2403 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
2404 enum pet_skip type, __isl_keep isl_set *cond)
2406 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2407 isl_pw_aff *skip;
2408 isl_set *dom;
2410 if (!scop)
2411 return NULL;
2412 if (!ext->skip[type])
2413 return scop;
2415 if (!multi_pw_aff_is_affine(ext->skip[type]))
2416 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
2417 isl_error_internal, "can only resrict affine skips",
2418 return pet_scop_free(scop));
2420 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2421 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
2422 cond = isl_set_copy(cond);
2423 cond = isl_set_from_params(cond);
2424 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
2425 skip = indicator_function(cond, dom);
2426 isl_multi_pw_aff_free(ext->skip[type]);
2427 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
2428 if (!ext->skip[type])
2429 return pet_scop_free(scop);
2431 return scop;
2434 /* Add extra conditions on the parameters to all iteration domains
2435 * and skip conditions.
2437 * A parameter value is valid for the result if it was valid
2438 * for the original scop and satisfies "cond" or if it does
2439 * not satisfy "cond" as in this case the scop is not executed
2440 * and the original constraints on the parameters are irrelevant.
2442 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
2443 __isl_take isl_set *cond)
2445 int i;
2447 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
2448 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
2450 if (!scop)
2451 goto error;
2453 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
2454 scop->context = isl_set_union(scop->context,
2455 isl_set_complement(isl_set_copy(cond)));
2456 scop->context = isl_set_coalesce(scop->context);
2457 scop->context = set_project_out_unnamed_params(scop->context);
2458 if (!scop->context)
2459 goto error;
2461 for (i = 0; i < scop->n_stmt; ++i) {
2462 scop->stmts[i] = stmt_restrict(scop->stmts[i],
2463 isl_set_copy(cond));
2464 if (!scop->stmts[i])
2465 goto error;
2468 isl_set_free(cond);
2469 return scop;
2470 error:
2471 isl_set_free(cond);
2472 return pet_scop_free(scop);
2475 /* Construct a function that (upon precomposition) inserts
2476 * a filter value with name "id" and value "satisfied"
2477 * in the list of filter values embedded in the set space "space".
2479 * If "space" does not contain any filter values yet, we first create
2480 * a function that inserts 0 filter values, i.e.,
2482 * [space -> []] -> space
2484 * We can now assume that space is of the form [dom -> [filters]]
2485 * We construct an identity mapping on dom and a mapping on filters
2486 * that (upon precomposition) inserts the new filter
2488 * dom -> dom
2489 * [satisfied, filters] -> [filters]
2491 * and then compute the cross product
2493 * [dom -> [satisfied, filters]] -> [dom -> [filters]]
2495 static __isl_give isl_pw_multi_aff *insert_filter_pma(
2496 __isl_take isl_space *space, __isl_take isl_id *id, int satisfied)
2498 isl_space *space2;
2499 isl_multi_aff *ma;
2500 isl_pw_multi_aff *pma0, *pma, *pma_dom, *pma_ran;
2501 isl_set *dom;
2503 if (isl_space_is_wrapping(space)) {
2504 space2 = isl_space_map_from_set(isl_space_copy(space));
2505 ma = isl_multi_aff_identity(space2);
2506 space = isl_space_unwrap(space);
2507 } else {
2508 space = isl_space_from_domain(space);
2509 ma = isl_multi_aff_domain_map(isl_space_copy(space));
2512 space2 = isl_space_domain(isl_space_copy(space));
2513 pma_dom = isl_pw_multi_aff_identity(isl_space_map_from_set(space2));
2514 space = isl_space_range(space);
2515 space = isl_space_insert_dims(space, isl_dim_set, 0, 1);
2516 pma_ran = isl_pw_multi_aff_project_out_map(space, isl_dim_set, 0, 1);
2517 pma_ran = isl_pw_multi_aff_set_dim_id(pma_ran, isl_dim_in, 0, id);
2518 pma_ran = isl_pw_multi_aff_fix_si(pma_ran, isl_dim_in, 0, satisfied);
2519 pma = isl_pw_multi_aff_product(pma_dom, pma_ran);
2521 pma0 = isl_pw_multi_aff_from_multi_aff(ma);
2522 pma = isl_pw_multi_aff_pullback_pw_multi_aff(pma0, pma);
2524 return pma;
2527 /* Insert an argument expression corresponding to "test" in front
2528 * of the list of arguments described by *n_arg and *args.
2530 static int args_insert_access(unsigned *n_arg, struct pet_expr ***args,
2531 __isl_keep isl_multi_pw_aff *test)
2533 int i;
2534 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
2536 if (!test)
2537 return -1;
2539 if (!*args) {
2540 *args = isl_calloc_array(ctx, struct pet_expr *, 1);
2541 if (!*args)
2542 return -1;
2543 } else {
2544 struct pet_expr **ext;
2545 ext = isl_calloc_array(ctx, struct pet_expr *, 1 + *n_arg);
2546 if (!ext)
2547 return -1;
2548 for (i = 0; i < *n_arg; ++i)
2549 ext[1 + i] = (*args)[i];
2550 free(*args);
2551 *args = ext;
2553 (*n_arg)++;
2554 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
2555 if (!(*args)[0])
2556 return -1;
2558 return 0;
2561 /* Make the expression "expr" depend on the value of "test"
2562 * being equal to "satisfied".
2564 * If "test" is an affine expression, we simply add the conditions
2565 * on the expression having the value "satisfied" to all access relations
2566 * and index expressions.
2568 * Otherwise, we add a filter to "expr" (which is then assumed to be
2569 * an access expression) corresponding to "test" being equal to "satisfied".
2571 struct pet_expr *pet_expr_filter(struct pet_expr *expr,
2572 __isl_take isl_multi_pw_aff *test, int satisfied)
2574 isl_id *id;
2575 isl_ctx *ctx;
2576 isl_space *space;
2577 isl_pw_multi_aff *pma;
2579 if (!expr || !test)
2580 goto error;
2582 if (!isl_multi_pw_aff_has_tuple_id(test, isl_dim_out)) {
2583 isl_pw_aff *pa;
2584 isl_set *cond;
2586 pa = isl_multi_pw_aff_get_pw_aff(test, 0);
2587 isl_multi_pw_aff_free(test);
2588 if (satisfied)
2589 cond = isl_pw_aff_non_zero_set(pa);
2590 else
2591 cond = isl_pw_aff_zero_set(pa);
2592 return pet_expr_restrict(expr, isl_set_params(cond));
2595 ctx = isl_multi_pw_aff_get_ctx(test);
2596 if (expr->type != pet_expr_access)
2597 isl_die(ctx, isl_error_invalid,
2598 "can only filter access expressions", goto error);
2600 space = isl_space_domain(isl_map_get_space(expr->acc.access));
2601 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2602 pma = insert_filter_pma(space, id, satisfied);
2604 expr->acc.access = isl_map_preimage_domain_pw_multi_aff(
2605 expr->acc.access,
2606 isl_pw_multi_aff_copy(pma));
2607 expr->acc.index = isl_multi_pw_aff_pullback_pw_multi_aff(
2608 expr->acc.index, pma);
2609 if (!expr->acc.access || !expr->acc.index)
2610 goto error;
2612 if (args_insert_access(&expr->n_arg, &expr->args, test) < 0)
2613 goto error;
2615 isl_multi_pw_aff_free(test);
2616 return expr;
2617 error:
2618 isl_multi_pw_aff_free(test);
2619 return pet_expr_free(expr);
2622 /* Look through the applications in "scop" for any that can be
2623 * applied to the filter expressed by "map" and "satisified".
2624 * If there is any, then apply it to "map" and return the result.
2625 * Otherwise, return "map".
2626 * "id" is the identifier of the virtual array.
2628 * We only introduce at most one implication for any given virtual array,
2629 * so we can apply the implication and return as soon as we find one.
2631 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
2632 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
2634 int i;
2636 for (i = 0; i < scop->n_implication; ++i) {
2637 struct pet_implication *pi = scop->implications[i];
2638 isl_id *pi_id;
2640 if (pi->satisfied != satisfied)
2641 continue;
2642 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
2643 isl_id_free(pi_id);
2644 if (pi_id != id)
2645 continue;
2647 return isl_map_apply_range(map, isl_map_copy(pi->extension));
2650 return map;
2653 /* Is the filter expressed by "test" and "satisfied" implied
2654 * by filter "pos" on "domain", with filter "expr", taking into
2655 * account the implications of "scop"?
2657 * For filter on domain implying that expressed by "test" and "satisfied",
2658 * the filter needs to be an access to the same (virtual) array as "test" and
2659 * the filter value needs to be equal to "satisfied".
2660 * Moreover, the filter access relation, possibly extended by
2661 * the implications in "scop" needs to contain "test".
2663 static int implies_filter(struct pet_scop *scop,
2664 __isl_keep isl_map *domain, int pos, struct pet_expr *expr,
2665 __isl_keep isl_map *test, int satisfied)
2667 isl_id *test_id, *arg_id;
2668 isl_val *val;
2669 int is_int;
2670 int s;
2671 int is_subset;
2672 isl_map *implied;
2674 if (expr->type != pet_expr_access)
2675 return 0;
2676 test_id = isl_map_get_tuple_id(test, isl_dim_out);
2677 arg_id = pet_expr_access_get_id(expr);
2678 isl_id_free(arg_id);
2679 isl_id_free(test_id);
2680 if (test_id != arg_id)
2681 return 0;
2682 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
2683 is_int = isl_val_is_int(val);
2684 if (is_int)
2685 s = isl_val_get_num_si(val);
2686 isl_val_free(val);
2687 if (!val)
2688 return -1;
2689 if (!is_int)
2690 return 0;
2691 if (s != satisfied)
2692 return 0;
2694 implied = isl_map_copy(expr->acc.access);
2695 implied = apply_implications(scop, implied, test_id, satisfied);
2696 is_subset = isl_map_is_subset(test, implied);
2697 isl_map_free(implied);
2699 return is_subset;
2702 /* Is the filter expressed by "test" and "satisfied" implied
2703 * by any of the filters on the domain of "stmt", taking into
2704 * account the implications of "scop"?
2706 static int filter_implied(struct pet_scop *scop,
2707 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
2709 int i;
2710 int implied;
2711 isl_id *test_id;
2712 isl_map *domain;
2713 isl_map *test_map;
2715 if (!scop || !stmt || !test)
2716 return -1;
2717 if (scop->n_implication == 0)
2718 return 0;
2719 if (stmt->n_arg == 0)
2720 return 0;
2722 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
2723 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
2725 implied = 0;
2726 for (i = 0; i < stmt->n_arg; ++i) {
2727 implied = implies_filter(scop, domain, i, stmt->args[i],
2728 test_map, satisfied);
2729 if (implied < 0 || implied)
2730 break;
2733 isl_map_free(test_map);
2734 isl_map_free(domain);
2735 return implied;
2738 /* Make the statement "stmt" depend on the value of "test"
2739 * being equal to "satisfied" by adjusting stmt->domain.
2741 * The domain of "test" corresponds to the (zero or more) outer dimensions
2742 * of the iteration domain.
2744 * We first extend "test" to apply to the entire iteration domain and
2745 * then check if the filter that we are about to add is implied
2746 * by any of the current filters, possibly taking into account
2747 * the implications in "scop". If so, we leave "stmt" untouched and return.
2749 * Otherwise, we insert an argument corresponding to a read to "test"
2750 * from the iteration domain of "stmt" in front of the list of arguments.
2751 * We also insert a corresponding output dimension in the wrapped
2752 * map contained in stmt->domain, with value set to "satisfied".
2754 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
2755 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
2757 int i;
2758 int implied;
2759 isl_id *id;
2760 isl_ctx *ctx;
2761 isl_pw_multi_aff *pma;
2762 isl_multi_aff *add_dom;
2763 isl_space *space;
2764 isl_local_space *ls;
2765 int n_test_dom;
2767 if (!stmt || !test)
2768 goto error;
2770 space = isl_set_get_space(stmt->domain);
2771 if (isl_space_is_wrapping(space))
2772 space = isl_space_domain(isl_space_unwrap(space));
2773 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
2774 space = isl_space_from_domain(space);
2775 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
2776 add_dom = isl_multi_aff_zero(isl_space_copy(space));
2777 ls = isl_local_space_from_space(isl_space_domain(space));
2778 for (i = 0; i < n_test_dom; ++i) {
2779 isl_aff *aff;
2780 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2781 isl_dim_set, i);
2782 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
2784 isl_local_space_free(ls);
2785 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
2787 implied = filter_implied(scop, stmt, test, satisfied);
2788 if (implied < 0)
2789 goto error;
2790 if (implied) {
2791 isl_multi_pw_aff_free(test);
2792 return stmt;
2795 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2796 pma = insert_filter_pma(isl_set_get_space(stmt->domain), id, satisfied);
2797 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
2799 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
2800 goto error;
2802 isl_multi_pw_aff_free(test);
2803 return stmt;
2804 error:
2805 isl_multi_pw_aff_free(test);
2806 return pet_stmt_free(stmt);
2809 /* Does "scop" have a skip condition of the given "type"?
2811 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2813 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2815 if (!scop)
2816 return -1;
2817 return ext->skip[type] != NULL;
2820 /* Does "scop" have a skip condition of the given "type" that
2821 * is an affine expression?
2823 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2825 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2827 if (!scop)
2828 return -1;
2829 if (!ext->skip[type])
2830 return 0;
2831 return multi_pw_aff_is_affine(ext->skip[type]);
2834 /* Does "scop" have a skip condition of the given "type" that
2835 * is not an affine expression?
2837 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2839 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2840 int aff;
2842 if (!scop)
2843 return -1;
2844 if (!ext->skip[type])
2845 return 0;
2846 aff = multi_pw_aff_is_affine(ext->skip[type]);
2847 if (aff < 0)
2848 return -1;
2849 return !aff;
2852 /* Does "scop" have a skip condition of the given "type" that
2853 * is affine and holds on the entire domain?
2855 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2857 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2858 isl_pw_aff *pa;
2859 isl_set *set;
2860 int is_aff;
2861 int is_univ;
2863 is_aff = pet_scop_has_affine_skip(scop, type);
2864 if (is_aff < 0 || !is_aff)
2865 return is_aff;
2867 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2868 set = isl_pw_aff_non_zero_set(pa);
2869 is_univ = isl_set_plain_is_universe(set);
2870 isl_set_free(set);
2872 return is_univ;
2875 /* Replace scop->skip[type] by "skip".
2877 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2878 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2880 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2882 if (!scop || !skip)
2883 goto error;
2885 isl_multi_pw_aff_free(ext->skip[type]);
2886 ext->skip[type] = skip;
2888 return scop;
2889 error:
2890 isl_multi_pw_aff_free(skip);
2891 return pet_scop_free(scop);
2894 /* Return a copy of scop->skip[type].
2896 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2897 enum pet_skip type)
2899 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2901 if (!scop)
2902 return NULL;
2904 return isl_multi_pw_aff_copy(ext->skip[type]);
2907 /* Assuming scop->skip[type] is an affine expression,
2908 * return the constraints on the parameters for which the skip condition
2909 * holds.
2911 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2912 enum pet_skip type)
2914 isl_multi_pw_aff *skip;
2915 isl_pw_aff *pa;
2917 skip = pet_scop_get_skip(scop, type);
2918 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2919 isl_multi_pw_aff_free(skip);
2920 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2923 /* Return the identifier of the variable that is accessed by
2924 * the skip condition of the given type.
2926 * The skip condition is assumed not to be an affine condition.
2928 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2929 enum pet_skip type)
2931 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2933 if (!scop)
2934 return NULL;
2936 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2939 /* Return an access pet_expr corresponding to the skip condition
2940 * of the given type.
2942 struct pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2943 enum pet_skip type)
2945 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2948 /* Drop the the skip condition scop->skip[type].
2950 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2952 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2954 if (!scop)
2955 return;
2957 isl_multi_pw_aff_free(ext->skip[type]);
2958 ext->skip[type] = NULL;
2961 /* Make the skip condition (if any) depend on the value of "test" being
2962 * equal to "satisfied".
2964 * We only support the case where the original skip condition is universal,
2965 * i.e., where skipping is unconditional, and where satisfied == 1.
2966 * In this case, the skip condition is changed to skip only when
2967 * "test" is equal to one.
2969 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2970 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2972 int is_univ = 0;
2974 if (!scop)
2975 return NULL;
2976 if (!pet_scop_has_skip(scop, type))
2977 return scop;
2979 if (satisfied)
2980 is_univ = pet_scop_has_universal_skip(scop, type);
2981 if (is_univ < 0)
2982 return pet_scop_free(scop);
2983 if (satisfied && is_univ) {
2984 isl_space *space = isl_multi_pw_aff_get_space(test);
2985 isl_multi_pw_aff *skip;
2986 skip = isl_multi_pw_aff_zero(space);
2987 scop = pet_scop_set_skip(scop, type, skip);
2988 if (!scop)
2989 return NULL;
2990 } else {
2991 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2992 "skip expression cannot be filtered",
2993 return pet_scop_free(scop));
2996 return scop;
2999 /* Make all statements in "scop" depend on the value of "test"
3000 * being equal to "satisfied" by adjusting their domains.
3002 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
3003 __isl_take isl_multi_pw_aff *test, int satisfied)
3005 int i;
3007 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
3008 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
3010 if (!scop || !test)
3011 goto error;
3013 for (i = 0; i < scop->n_stmt; ++i) {
3014 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
3015 isl_multi_pw_aff_copy(test), satisfied);
3016 if (!scop->stmts[i])
3017 goto error;
3020 isl_multi_pw_aff_free(test);
3021 return scop;
3022 error:
3023 isl_multi_pw_aff_free(test);
3024 return pet_scop_free(scop);
3027 /* Add all parameters in "expr" to "space" and return the result.
3029 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
3030 __isl_take isl_space *space)
3032 int i;
3034 if (!expr)
3035 goto error;
3036 for (i = 0; i < expr->n_arg; ++i)
3037 space = expr_collect_params(expr->args[i], space);
3039 if (expr->type == pet_expr_access)
3040 space = isl_space_align_params(space,
3041 isl_map_get_space(expr->acc.access));
3043 return space;
3044 error:
3045 pet_expr_free(expr);
3046 return isl_space_free(space);
3049 /* Add all parameters in "stmt" to "space" and return the result.
3051 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
3052 __isl_take isl_space *space)
3054 if (!stmt)
3055 return isl_space_free(space);
3057 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
3058 space = isl_space_align_params(space,
3059 isl_map_get_space(stmt->schedule));
3060 space = expr_collect_params(stmt->body, space);
3062 return space;
3065 /* Add all parameters in "array" to "space" and return the result.
3067 static __isl_give isl_space *array_collect_params(struct pet_array *array,
3068 __isl_take isl_space *space)
3070 if (!array)
3071 return isl_space_free(space);
3073 space = isl_space_align_params(space,
3074 isl_set_get_space(array->context));
3075 space = isl_space_align_params(space, isl_set_get_space(array->extent));
3077 return space;
3080 /* Add all parameters in "scop" to "space" and return the result.
3082 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
3083 __isl_take isl_space *space)
3085 int i;
3087 if (!scop)
3088 return isl_space_free(space);
3090 for (i = 0; i < scop->n_array; ++i)
3091 space = array_collect_params(scop->arrays[i], space);
3093 for (i = 0; i < scop->n_stmt; ++i)
3094 space = stmt_collect_params(scop->stmts[i], space);
3096 return space;
3099 /* Add all parameters in "space" to all access relations and index expressions
3100 * in "expr".
3102 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
3103 __isl_take isl_space *space)
3105 int i;
3107 if (!expr)
3108 goto error;
3110 for (i = 0; i < expr->n_arg; ++i) {
3111 expr->args[i] =
3112 expr_propagate_params(expr->args[i],
3113 isl_space_copy(space));
3114 if (!expr->args[i])
3115 goto error;
3118 if (expr->type == pet_expr_access) {
3119 expr->acc.access = isl_map_align_params(expr->acc.access,
3120 isl_space_copy(space));
3121 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
3122 isl_space_copy(space));
3123 if (!expr->acc.access || !expr->acc.index)
3124 goto error;
3127 isl_space_free(space);
3128 return expr;
3129 error:
3130 isl_space_free(space);
3131 return pet_expr_free(expr);
3134 /* Add all parameters in "space" to the domain, schedule and
3135 * all access relations in "stmt".
3137 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
3138 __isl_take isl_space *space)
3140 if (!stmt)
3141 goto error;
3143 stmt->domain = isl_set_align_params(stmt->domain,
3144 isl_space_copy(space));
3145 stmt->schedule = isl_map_align_params(stmt->schedule,
3146 isl_space_copy(space));
3147 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(space));
3149 if (!stmt->domain || !stmt->schedule || !stmt->body)
3150 goto error;
3152 isl_space_free(space);
3153 return stmt;
3154 error:
3155 isl_space_free(space);
3156 return pet_stmt_free(stmt);
3159 /* Add all parameters in "space" to "array".
3161 static struct pet_array *array_propagate_params(struct pet_array *array,
3162 __isl_take isl_space *space)
3164 if (!array)
3165 goto error;
3167 array->context = isl_set_align_params(array->context,
3168 isl_space_copy(space));
3169 array->extent = isl_set_align_params(array->extent,
3170 isl_space_copy(space));
3171 if (array->value_bounds) {
3172 array->value_bounds = isl_set_align_params(array->value_bounds,
3173 isl_space_copy(space));
3174 if (!array->value_bounds)
3175 goto error;
3178 if (!array->context || !array->extent)
3179 goto error;
3181 isl_space_free(space);
3182 return array;
3183 error:
3184 isl_space_free(space);
3185 return pet_array_free(array);
3188 /* Add all parameters in "space" to "scop".
3190 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
3191 __isl_take isl_space *space)
3193 int i;
3195 if (!scop)
3196 goto error;
3198 for (i = 0; i < scop->n_array; ++i) {
3199 scop->arrays[i] = array_propagate_params(scop->arrays[i],
3200 isl_space_copy(space));
3201 if (!scop->arrays[i])
3202 goto error;
3205 for (i = 0; i < scop->n_stmt; ++i) {
3206 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
3207 isl_space_copy(space));
3208 if (!scop->stmts[i])
3209 goto error;
3212 isl_space_free(space);
3213 return scop;
3214 error:
3215 isl_space_free(space);
3216 return pet_scop_free(scop);
3219 /* Update all isl_sets and isl_maps in "scop" such that they all
3220 * have the same parameters.
3222 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
3224 isl_space *space;
3226 if (!scop)
3227 return NULL;
3229 space = isl_set_get_space(scop->context);
3230 space = scop_collect_params(scop, space);
3232 scop->context = isl_set_align_params(scop->context,
3233 isl_space_copy(space));
3234 scop = scop_propagate_params(scop, space);
3236 if (scop && !scop->context)
3237 return pet_scop_free(scop);
3239 return scop;
3242 /* Check if the given index expression accesses a (0D) array that corresponds
3243 * to one of the parameters in "dim". If so, replace the array access
3244 * by an access to the set of integers with as index (and value)
3245 * that parameter.
3247 static __isl_give isl_multi_pw_aff *index_detect_parameter(
3248 __isl_take isl_multi_pw_aff *index, __isl_take isl_space *space)
3250 isl_local_space *ls;
3251 isl_id *array_id = NULL;
3252 isl_aff *aff;
3253 int pos = -1;
3255 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out)) {
3256 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
3257 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3259 isl_space_free(space);
3261 if (pos < 0) {
3262 isl_id_free(array_id);
3263 return index;
3266 space = isl_multi_pw_aff_get_domain_space(index);
3267 isl_multi_pw_aff_free(index);
3269 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3270 if (pos < 0) {
3271 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
3272 space = isl_space_set_dim_id(space, isl_dim_param, 0, array_id);
3273 pos = 0;
3274 } else
3275 isl_id_free(array_id);
3277 ls = isl_local_space_from_space(space);
3278 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
3279 index = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
3281 return index;
3284 /* Check if the given access relation accesses a (0D) array that corresponds
3285 * to one of the parameters in "dim". If so, replace the array access
3286 * by an access to the set of integers with as index (and value)
3287 * that parameter.
3289 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
3290 __isl_take isl_space *dim)
3292 isl_id *array_id = NULL;
3293 int pos = -1;
3295 if (isl_map_has_tuple_id(access, isl_dim_out)) {
3296 array_id = isl_map_get_tuple_id(access, isl_dim_out);
3297 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
3299 isl_space_free(dim);
3301 if (pos < 0) {
3302 isl_id_free(array_id);
3303 return access;
3306 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
3307 if (pos < 0) {
3308 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
3309 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
3310 pos = 0;
3311 } else
3312 isl_id_free(array_id);
3314 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
3315 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
3317 return access;
3320 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3321 * in "dim" by a value equal to the corresponding parameter.
3323 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
3324 __isl_take isl_space *dim)
3326 int i;
3328 if (!expr)
3329 goto error;
3331 for (i = 0; i < expr->n_arg; ++i) {
3332 expr->args[i] =
3333 expr_detect_parameter_accesses(expr->args[i],
3334 isl_space_copy(dim));
3335 if (!expr->args[i])
3336 goto error;
3339 if (expr->type == pet_expr_access) {
3340 expr->acc.access = access_detect_parameter(expr->acc.access,
3341 isl_space_copy(dim));
3342 expr->acc.index = index_detect_parameter(expr->acc.index,
3343 isl_space_copy(dim));
3344 if (!expr->acc.access || !expr->acc.index)
3345 goto error;
3348 isl_space_free(dim);
3349 return expr;
3350 error:
3351 isl_space_free(dim);
3352 return pet_expr_free(expr);
3355 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3356 * in "dim" by a value equal to the corresponding parameter.
3358 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
3359 __isl_take isl_space *dim)
3361 if (!stmt)
3362 goto error;
3364 stmt->body = expr_detect_parameter_accesses(stmt->body,
3365 isl_space_copy(dim));
3367 if (!stmt->domain || !stmt->schedule || !stmt->body)
3368 goto error;
3370 isl_space_free(dim);
3371 return stmt;
3372 error:
3373 isl_space_free(dim);
3374 return pet_stmt_free(stmt);
3377 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3378 * in "dim" by a value equal to the corresponding parameter.
3380 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
3381 __isl_take isl_space *dim)
3383 int i;
3385 if (!scop)
3386 goto error;
3388 for (i = 0; i < scop->n_stmt; ++i) {
3389 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
3390 isl_space_copy(dim));
3391 if (!scop->stmts[i])
3392 goto error;
3395 isl_space_free(dim);
3396 return scop;
3397 error:
3398 isl_space_free(dim);
3399 return pet_scop_free(scop);
3402 /* Replace all accesses to (0D) arrays that correspond to any of
3403 * the parameters used in "scop" by a value equal
3404 * to the corresponding parameter.
3406 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
3408 isl_space *dim;
3410 if (!scop)
3411 return NULL;
3413 dim = isl_set_get_space(scop->context);
3414 dim = scop_collect_params(scop, dim);
3416 scop = scop_detect_parameter_accesses(scop, dim);
3418 return scop;
3421 /* Return the relation mapping domain iterations to all possibly
3422 * accessed data elements.
3423 * In particular, take the access relation and project out the values
3424 * of the arguments, if any.
3426 __isl_give isl_map *pet_expr_access_get_may_access(struct pet_expr *expr)
3428 isl_map *access;
3429 isl_space *space;
3430 isl_map *map;
3432 if (!expr)
3433 return NULL;
3434 if (expr->type != pet_expr_access)
3435 return NULL;
3437 access = isl_map_copy(expr->acc.access);
3438 if (expr->n_arg == 0)
3439 return access;
3441 space = isl_space_domain(isl_map_get_space(access));
3442 map = isl_map_universe(isl_space_unwrap(space));
3443 map = isl_map_domain_map(map);
3444 access = isl_map_apply_domain(access, map);
3446 return access;
3449 /* Return the relation mapping domain iterations to all possibly
3450 * accessed data elements, with its domain tagged with the reference
3451 * identifier.
3453 __isl_give isl_map *pet_expr_access_get_tagged_may_access(
3454 struct pet_expr *expr)
3456 isl_map *access;
3458 if (!expr)
3459 return NULL;
3461 access = pet_expr_access_get_may_access(expr);
3462 access = tag_access(access, isl_id_copy(expr->acc.ref_id));
3464 return access;
3467 /* Add the access relation of the access expression "expr" to "accesses" and
3468 * return the result.
3469 * The domain of the access relation is intersected with "domain".
3470 * If "tag" is set, then the access relation is tagged with
3471 * the corresponding reference identifier.
3473 static __isl_give isl_union_map *expr_collect_access(struct pet_expr *expr,
3474 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
3476 isl_map *access;
3478 access = pet_expr_access_get_may_access(expr);
3479 access = isl_map_intersect_domain(access, isl_set_copy(domain));
3480 if (tag)
3481 access = tag_access(access, isl_id_copy(expr->acc.ref_id));
3482 return isl_union_map_add_map(accesses, access);
3485 /* Add all read access relations (if "read" is set) and/or all write
3486 * access relations (if "write" is set) to "accesses" and return the result.
3487 * The domains of the access relations are intersected with "domain".
3488 * If "tag" is set, then the access relations are tagged with
3489 * the corresponding reference identifiers.
3491 * If "must" is set, then we only add the accesses that are definitely
3492 * performed. Otherwise, we add all potential accesses.
3493 * In particular, if the access has any arguments, then if "must" is
3494 * set we currently skip the access completely. If "must" is not set,
3495 * we project out the values of the access arguments.
3497 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
3498 int read, int write, int must, int tag,
3499 __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
3501 int i;
3502 isl_id *id;
3503 isl_space *dim;
3505 if (!expr)
3506 return isl_union_map_free(accesses);
3508 for (i = 0; i < expr->n_arg; ++i)
3509 accesses = expr_collect_accesses(expr->args[i],
3510 read, write, must, tag, accesses, domain);
3512 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
3513 ((read && expr->acc.read) || (write && expr->acc.write)) &&
3514 (!must || expr->n_arg == 0)) {
3515 accesses = expr_collect_access(expr, tag, accesses, domain);
3518 return accesses;
3521 /* Collect and return all read access relations (if "read" is set)
3522 * and/or all write access relations (if "write" is set) in "stmt".
3523 * If "tag" is set, then the access relations are tagged with
3524 * the corresponding reference identifiers.
3525 * If "kill" is set, then "stmt" is a kill statement and we simply
3526 * add the argument of the kill operation.
3528 * If "must" is set, then we only add the accesses that are definitely
3529 * performed. Otherwise, we add all potential accesses.
3530 * In particular, if the statement has any arguments, then if "must" is
3531 * set we currently skip the statement completely. If "must" is not set,
3532 * we project out the values of the statement arguments.
3534 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
3535 int read, int write, int kill, int must, int tag,
3536 __isl_take isl_space *dim)
3538 isl_union_map *accesses;
3539 isl_set *domain;
3541 if (!stmt)
3542 return NULL;
3544 accesses = isl_union_map_empty(dim);
3546 if (must && stmt->n_arg > 0)
3547 return accesses;
3549 domain = isl_set_copy(stmt->domain);
3550 if (isl_set_is_wrapping(domain))
3551 domain = isl_map_domain(isl_set_unwrap(domain));
3553 if (kill)
3554 accesses = expr_collect_access(stmt->body->args[0], tag,
3555 accesses, domain);
3556 else
3557 accesses = expr_collect_accesses(stmt->body, read, write,
3558 must, tag, accesses, domain);
3559 isl_set_free(domain);
3561 return accesses;
3564 /* Is "stmt" a kill statement?
3566 static int is_kill(struct pet_stmt *stmt)
3568 if (stmt->body->type != pet_expr_unary)
3569 return 0;
3570 return stmt->body->op == pet_op_kill;
3573 /* Is "stmt" an assume statement?
3575 int pet_stmt_is_assume(struct pet_stmt *stmt)
3577 if (stmt->body->type != pet_expr_unary)
3578 return 0;
3579 return stmt->body->op == pet_op_assume;
3582 /* Compute a mapping from all arrays (of structs) in scop
3583 * to their innermost arrays.
3585 * In particular, for each array of a primitive type, the result
3586 * contains the identity mapping on that array.
3587 * For each array involving member accesses, the result
3588 * contains a mapping from the elements of any intermediate array of structs
3589 * to all corresponding elements of the innermost nested arrays.
3591 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
3593 int i;
3594 isl_union_map *to_inner;
3596 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
3598 for (i = 0; i < scop->n_array; ++i) {
3599 struct pet_array *array = scop->arrays[i];
3600 isl_set *set;
3601 isl_map *map, *gist;
3603 if (array->element_is_record)
3604 continue;
3606 map = isl_set_identity(isl_set_copy(array->extent));
3608 set = isl_map_domain(isl_map_copy(map));
3609 gist = isl_map_copy(map);
3610 gist = isl_map_gist_domain(gist, isl_set_copy(set));
3611 to_inner = isl_union_map_add_map(to_inner, gist);
3613 while (set && isl_set_is_wrapping(set)) {
3614 isl_id *id;
3615 isl_map *wrapped;
3617 id = isl_set_get_tuple_id(set);
3618 wrapped = isl_set_unwrap(set);
3619 wrapped = isl_map_domain_map(wrapped);
3620 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
3621 map = isl_map_apply_domain(map, wrapped);
3622 set = isl_map_domain(isl_map_copy(map));
3623 gist = isl_map_copy(map);
3624 gist = isl_map_gist_domain(gist, isl_set_copy(set));
3625 to_inner = isl_union_map_add_map(to_inner, gist);
3628 isl_set_free(set);
3629 isl_map_free(map);
3632 return to_inner;
3635 /* Collect and return all read access relations (if "read" is set)
3636 * and/or all write access relations (if "write" is set) in "scop".
3637 * If "kill" is set, then we only add the arguments of kill operations.
3638 * If "must" is set, then we only add the accesses that are definitely
3639 * performed. Otherwise, we add all potential accesses.
3640 * If "tag" is set, then the access relations are tagged with
3641 * the corresponding reference identifiers.
3642 * For accesses to structures, the returned access relation accesses
3643 * all individual fields in the structures.
3645 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
3646 int read, int write, int kill, int must, int tag)
3648 int i;
3649 isl_union_map *accesses;
3650 isl_union_set *arrays;
3651 isl_union_map *to_inner;
3653 if (!scop)
3654 return NULL;
3656 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
3658 for (i = 0; i < scop->n_stmt; ++i) {
3659 struct pet_stmt *stmt = scop->stmts[i];
3660 isl_union_map *accesses_i;
3661 isl_space *space;
3663 if (kill && !is_kill(stmt))
3664 continue;
3666 space = isl_set_get_space(scop->context);
3667 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
3668 must, tag, space);
3669 accesses = isl_union_map_union(accesses, accesses_i);
3672 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
3673 for (i = 0; i < scop->n_array; ++i) {
3674 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
3675 arrays = isl_union_set_add_set(arrays, extent);
3677 accesses = isl_union_map_intersect_range(accesses, arrays);
3679 to_inner = compute_to_inner(scop);
3680 accesses = isl_union_map_apply_range(accesses, to_inner);
3682 return accesses;
3685 /* Collect all potential read access relations.
3687 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
3689 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
3692 /* Collect all potential write access relations.
3694 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
3696 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
3699 /* Collect all definite write access relations.
3701 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
3703 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
3706 /* Collect all definite kill access relations.
3708 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
3710 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
3713 /* Collect all tagged potential read access relations.
3715 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
3716 struct pet_scop *scop)
3718 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
3721 /* Collect all tagged potential write access relations.
3723 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
3724 struct pet_scop *scop)
3726 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
3729 /* Collect all tagged definite write access relations.
3731 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
3732 struct pet_scop *scop)
3734 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
3737 /* Collect all tagged definite kill access relations.
3739 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
3740 struct pet_scop *scop)
3742 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
3745 /* Collect and return the union of iteration domains in "scop".
3747 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
3749 int i;
3750 isl_set *domain_i;
3751 isl_union_set *domain;
3753 if (!scop)
3754 return NULL;
3756 domain = isl_union_set_empty(isl_set_get_space(scop->context));
3758 for (i = 0; i < scop->n_stmt; ++i) {
3759 domain_i = isl_set_copy(scop->stmts[i]->domain);
3760 domain = isl_union_set_add_set(domain, domain_i);
3763 return domain;
3766 /* Collect and return the schedules of the statements in "scop".
3767 * The range is normalized to the maximal number of scheduling
3768 * dimensions.
3770 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
3772 int i, j;
3773 isl_map *schedule_i;
3774 isl_union_map *schedule;
3775 int depth, max_depth = 0;
3777 if (!scop)
3778 return NULL;
3780 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
3782 for (i = 0; i < scop->n_stmt; ++i) {
3783 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
3784 if (depth > max_depth)
3785 max_depth = depth;
3788 for (i = 0; i < scop->n_stmt; ++i) {
3789 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
3790 depth = isl_map_dim(schedule_i, isl_dim_out);
3791 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
3792 max_depth - depth);
3793 for (j = depth; j < max_depth; ++j)
3794 schedule_i = isl_map_fix_si(schedule_i,
3795 isl_dim_out, j, 0);
3796 schedule = isl_union_map_add_map(schedule, schedule_i);
3799 return schedule;
3802 /* Does expression "expr" write to "id"?
3804 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
3806 int i;
3807 isl_id *write_id;
3809 for (i = 0; i < expr->n_arg; ++i) {
3810 int writes = expr_writes(expr->args[i], id);
3811 if (writes < 0 || writes)
3812 return writes;
3815 if (expr->type != pet_expr_access)
3816 return 0;
3817 if (!expr->acc.write)
3818 return 0;
3819 if (pet_expr_is_affine(expr))
3820 return 0;
3822 write_id = pet_expr_access_get_id(expr);
3823 isl_id_free(write_id);
3825 if (!write_id)
3826 return -1;
3828 return write_id == id;
3831 /* Does statement "stmt" write to "id"?
3833 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
3835 return expr_writes(stmt->body, id);
3838 /* Is there any write access in "scop" that accesses "id"?
3840 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
3842 int i;
3844 if (!scop)
3845 return -1;
3847 for (i = 0; i < scop->n_stmt; ++i) {
3848 int writes = stmt_writes(scop->stmts[i], id);
3849 if (writes < 0 || writes)
3850 return writes;
3853 return 0;
3856 /* Add a reference identifier to access expression "expr".
3857 * "user" points to an integer that contains the sequence number
3858 * of the next reference.
3860 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
3862 isl_ctx *ctx;
3863 char name[50];
3864 int *n_ref = user;
3866 if (!expr)
3867 return expr;
3869 ctx = isl_map_get_ctx(expr->acc.access);
3870 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
3871 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
3872 if (!expr->acc.ref_id)
3873 return pet_expr_free(expr);
3875 return expr;
3878 /* Add a reference identifier to all access expressions in "stmt".
3879 * "n_ref" points to an integer that contains the sequence number
3880 * of the next reference.
3882 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
3884 int i;
3886 if (!stmt)
3887 return NULL;
3889 for (i = 0; i < stmt->n_arg; ++i) {
3890 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3891 &access_add_ref_id, n_ref);
3892 if (!stmt->args[i])
3893 return pet_stmt_free(stmt);
3896 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
3897 if (!stmt->body)
3898 return pet_stmt_free(stmt);
3900 return stmt;
3903 /* Add a reference identifier to all access expressions in "scop".
3905 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
3907 int i;
3908 int n_ref;
3910 if (!scop)
3911 return NULL;
3913 n_ref = 0;
3914 for (i = 0; i < scop->n_stmt; ++i) {
3915 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
3916 if (!scop->stmts[i])
3917 return pet_scop_free(scop);
3920 return scop;
3923 /* Reset the user pointer on all parameter ids in "array".
3925 static struct pet_array *array_anonymize(struct pet_array *array)
3927 if (!array)
3928 return NULL;
3930 array->context = isl_set_reset_user(array->context);
3931 array->extent = isl_set_reset_user(array->extent);
3932 if (!array->context || !array->extent)
3933 return pet_array_free(array);
3935 return array;
3938 /* Reset the user pointer on all parameter and tuple ids in
3939 * the access relation and the index expressions
3940 * of the access expression "expr".
3942 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
3944 expr->acc.access = isl_map_reset_user(expr->acc.access);
3945 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
3946 if (!expr->acc.access || !expr->acc.index)
3947 return pet_expr_free(expr);
3949 return expr;
3952 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3954 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3956 int i;
3957 isl_space *space;
3958 isl_set *domain;
3960 if (!stmt)
3961 return NULL;
3963 stmt->domain = isl_set_reset_user(stmt->domain);
3964 stmt->schedule = isl_map_reset_user(stmt->schedule);
3965 if (!stmt->domain || !stmt->schedule)
3966 return pet_stmt_free(stmt);
3968 for (i = 0; i < stmt->n_arg; ++i) {
3969 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3970 &access_anonymize, NULL);
3971 if (!stmt->args[i])
3972 return pet_stmt_free(stmt);
3975 stmt->body = pet_expr_map_access(stmt->body,
3976 &access_anonymize, NULL);
3977 if (!stmt->body)
3978 return pet_stmt_free(stmt);
3980 return stmt;
3983 /* Reset the user pointer on the tuple ids and all parameter ids
3984 * in "implication".
3986 static struct pet_implication *implication_anonymize(
3987 struct pet_implication *implication)
3989 if (!implication)
3990 return NULL;
3992 implication->extension = isl_map_reset_user(implication->extension);
3993 if (!implication->extension)
3994 return pet_implication_free(implication);
3996 return implication;
3999 /* Reset the user pointer on all parameter and tuple ids in "scop".
4001 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
4003 int i;
4005 if (!scop)
4006 return NULL;
4008 scop->context = isl_set_reset_user(scop->context);
4009 scop->context_value = isl_set_reset_user(scop->context_value);
4010 if (!scop->context || !scop->context_value)
4011 return pet_scop_free(scop);
4013 for (i = 0; i < scop->n_array; ++i) {
4014 scop->arrays[i] = array_anonymize(scop->arrays[i]);
4015 if (!scop->arrays[i])
4016 return pet_scop_free(scop);
4019 for (i = 0; i < scop->n_stmt; ++i) {
4020 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
4021 if (!scop->stmts[i])
4022 return pet_scop_free(scop);
4025 for (i = 0; i < scop->n_implication; ++i) {
4026 scop->implications[i] =
4027 implication_anonymize(scop->implications[i]);
4028 if (!scop->implications[i])
4029 return pet_scop_free(scop);
4032 return scop;
4035 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
4036 * then intersect the range of "map" with the valid set of values.
4038 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
4039 struct pet_expr *arg, __isl_keep isl_union_map *value_bounds)
4041 isl_id *id;
4042 isl_map *vb;
4043 isl_space *space;
4044 isl_ctx *ctx = isl_map_get_ctx(map);
4046 id = pet_expr_access_get_id(arg);
4047 space = isl_space_alloc(ctx, 0, 0, 1);
4048 space = isl_space_set_tuple_id(space, isl_dim_in, id);
4049 vb = isl_union_map_extract_map(value_bounds, space);
4050 if (!isl_map_plain_is_empty(vb))
4051 map = isl_map_intersect_range(map, isl_map_range(vb));
4052 else
4053 isl_map_free(vb);
4055 return map;
4058 /* Given a set "domain", return a wrapped relation with the given set
4059 * as domain and a range of dimension "n_arg", where each coordinate
4060 * is either unbounded or, if the corresponding element of args is of
4061 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
4063 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
4064 unsigned n_arg, struct pet_expr **args,
4065 __isl_keep isl_union_map *value_bounds)
4067 int i;
4068 isl_map *map;
4069 isl_space *space;
4071 map = isl_map_from_domain(domain);
4072 space = isl_map_get_space(map);
4073 space = isl_space_add_dims(space, isl_dim_out, 1);
4075 for (i = 0; i < n_arg; ++i) {
4076 isl_map *map_i;
4077 struct pet_expr *arg = args[i];
4079 map_i = isl_map_universe(isl_space_copy(space));
4080 if (arg->type == pet_expr_access)
4081 map_i = access_apply_value_bounds(map_i, arg,
4082 value_bounds);
4083 map = isl_map_flat_range_product(map, map_i);
4085 isl_space_free(space);
4087 return isl_map_wrap(map);
4090 /* Data used in access_gist() callback.
4092 struct pet_access_gist_data {
4093 isl_set *domain;
4094 isl_union_map *value_bounds;
4097 /* Given an expression "expr" of type pet_expr_access, compute
4098 * the gist of the associated access relation and index expression
4099 * with respect to data->domain and the bounds on the values of the arguments
4100 * of the expression.
4102 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
4104 struct pet_access_gist_data *data = user;
4105 isl_set *domain;
4107 domain = isl_set_copy(data->domain);
4108 if (expr->n_arg > 0)
4109 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
4110 data->value_bounds);
4112 expr->acc.access = isl_map_gist_domain(expr->acc.access,
4113 isl_set_copy(domain));
4114 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
4115 if (!expr->acc.access || !expr->acc.index)
4116 return pet_expr_free(expr);
4118 return expr;
4121 /* Compute the gist of the iteration domain and all access relations
4122 * of "stmt" based on the constraints on the parameters specified by "context"
4123 * and the constraints on the values of nested accesses specified
4124 * by "value_bounds".
4126 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
4127 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
4129 int i;
4130 isl_space *space;
4131 isl_set *domain;
4132 struct pet_access_gist_data data;
4134 if (!stmt)
4135 return NULL;
4137 data.domain = isl_set_copy(stmt->domain);
4138 data.value_bounds = value_bounds;
4139 if (stmt->n_arg > 0)
4140 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
4142 data.domain = isl_set_intersect_params(data.domain,
4143 isl_set_copy(context));
4145 for (i = 0; i < stmt->n_arg; ++i) {
4146 stmt->args[i] = pet_expr_map_access(stmt->args[i],
4147 &access_gist, &data);
4148 if (!stmt->args[i])
4149 goto error;
4152 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
4153 if (!stmt->body)
4154 goto error;
4156 isl_set_free(data.domain);
4158 space = isl_set_get_space(stmt->domain);
4159 if (isl_space_is_wrapping(space))
4160 space = isl_space_domain(isl_space_unwrap(space));
4161 domain = isl_set_universe(space);
4162 domain = isl_set_intersect_params(domain, isl_set_copy(context));
4163 if (stmt->n_arg > 0)
4164 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
4165 value_bounds);
4166 stmt->domain = isl_set_gist(stmt->domain, domain);
4167 if (!stmt->domain)
4168 return pet_stmt_free(stmt);
4170 return stmt;
4171 error:
4172 isl_set_free(data.domain);
4173 return pet_stmt_free(stmt);
4176 /* Compute the gist of the extent of the array
4177 * based on the constraints on the parameters specified by "context".
4179 static struct pet_array *array_gist(struct pet_array *array,
4180 __isl_keep isl_set *context)
4182 if (!array)
4183 return NULL;
4185 array->extent = isl_set_gist_params(array->extent,
4186 isl_set_copy(context));
4187 if (!array->extent)
4188 return pet_array_free(array);
4190 return array;
4193 /* Compute the gist of all sets and relations in "scop"
4194 * based on the constraints on the parameters specified by "scop->context"
4195 * and the constraints on the values of nested accesses specified
4196 * by "value_bounds".
4198 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
4199 __isl_keep isl_union_map *value_bounds)
4201 int i;
4203 if (!scop)
4204 return NULL;
4206 scop->context = isl_set_coalesce(scop->context);
4207 if (!scop->context)
4208 return pet_scop_free(scop);
4210 for (i = 0; i < scop->n_array; ++i) {
4211 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
4212 if (!scop->arrays[i])
4213 return pet_scop_free(scop);
4216 for (i = 0; i < scop->n_stmt; ++i) {
4217 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
4218 value_bounds);
4219 if (!scop->stmts[i])
4220 return pet_scop_free(scop);
4223 return scop;
4226 /* Intersect the context of "scop" with "context".
4227 * To ensure that we don't introduce any unnamed parameters in
4228 * the context of "scop", we first remove the unnamed parameters
4229 * from "context".
4231 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
4232 __isl_take isl_set *context)
4234 if (!scop)
4235 goto error;
4237 context = set_project_out_unnamed_params(context);
4238 scop->context = isl_set_intersect(scop->context, context);
4239 if (!scop->context)
4240 return pet_scop_free(scop);
4242 return scop;
4243 error:
4244 isl_set_free(context);
4245 return pet_scop_free(scop);
4248 /* Drop the current context of "scop". That is, replace the context
4249 * by a universal set.
4251 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
4253 isl_space *space;
4255 if (!scop)
4256 return NULL;
4258 space = isl_set_get_space(scop->context);
4259 isl_set_free(scop->context);
4260 scop->context = isl_set_universe(space);
4261 if (!scop->context)
4262 return pet_scop_free(scop);
4264 return scop;
4267 /* Append "array" to the arrays of "scop".
4269 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
4270 struct pet_array *array)
4272 isl_ctx *ctx;
4273 struct pet_array **arrays;
4275 if (!array || !scop)
4276 goto error;
4278 ctx = isl_set_get_ctx(scop->context);
4279 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
4280 scop->n_array + 1);
4281 if (!arrays)
4282 goto error;
4283 scop->arrays = arrays;
4284 scop->arrays[scop->n_array] = array;
4285 scop->n_array++;
4287 return scop;
4288 error:
4289 pet_array_free(array);
4290 return pet_scop_free(scop);
4293 /* Create and return an implication on filter values equal to "satisfied"
4294 * with extension "map".
4296 static struct pet_implication *new_implication(__isl_take isl_map *map,
4297 int satisfied)
4299 isl_ctx *ctx;
4300 struct pet_implication *implication;
4302 if (!map)
4303 return NULL;
4304 ctx = isl_map_get_ctx(map);
4305 implication = isl_alloc_type(ctx, struct pet_implication);
4306 if (!implication)
4307 goto error;
4309 implication->extension = map;
4310 implication->satisfied = satisfied;
4312 return implication;
4313 error:
4314 isl_map_free(map);
4315 return NULL;
4318 /* Add an implication on filter values equal to "satisfied"
4319 * with extension "map" to "scop".
4321 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
4322 __isl_take isl_map *map, int satisfied)
4324 isl_ctx *ctx;
4325 struct pet_implication *implication;
4326 struct pet_implication **implications;
4328 implication = new_implication(map, satisfied);
4329 if (!scop || !implication)
4330 goto error;
4332 ctx = isl_set_get_ctx(scop->context);
4333 implications = isl_realloc_array(ctx, scop->implications,
4334 struct pet_implication *,
4335 scop->n_implication + 1);
4336 if (!implications)
4337 goto error;
4338 scop->implications = implications;
4339 scop->implications[scop->n_implication] = implication;
4340 scop->n_implication++;
4342 return scop;
4343 error:
4344 pet_implication_free(implication);
4345 return pet_scop_free(scop);
4348 /* Given an access expression, check if it is data dependent.
4349 * If so, set *found and abort the search.
4351 static int is_data_dependent(struct pet_expr *expr, void *user)
4353 int *found = user;
4355 if (expr->n_arg) {
4356 *found = 1;
4357 return -1;
4360 return 0;
4363 /* Does "scop" contain any data dependent accesses?
4365 * Check the body of each statement for such accesses.
4367 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
4369 int i;
4370 int found = 0;
4372 if (!scop)
4373 return -1;
4375 for (i = 0; i < scop->n_stmt; ++i) {
4376 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
4377 &is_data_dependent, &found);
4378 if (r < 0 && !found)
4379 return -1;
4380 if (found)
4381 return found;
4384 return found;
4387 /* Does "scop" contain and data dependent conditions?
4389 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
4391 int i;
4393 if (!scop)
4394 return -1;
4396 for (i = 0; i < scop->n_stmt; ++i)
4397 if (scop->stmts[i]->n_arg > 0)
4398 return 1;
4400 return 0;
4403 /* Keep track of the "input" file inside the (extended) "scop".
4405 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
4407 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4409 if (!scop)
4410 return NULL;
4412 ext->input = input;
4414 return scop;
4417 /* Print the original code corresponding to "scop" to printer "p".
4419 * pet_scop_print_original can only be called from
4420 * a pet_transform_C_source callback. This means that the input
4421 * file is stored in the extended scop and that the printer prints
4422 * to a file.
4424 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
4425 __isl_take isl_printer *p)
4427 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4428 FILE *output;
4430 if (!scop || !p)
4431 return isl_printer_free(p);
4433 if (!ext->input)
4434 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
4435 "no input file stored in scop",
4436 return isl_printer_free(p));
4438 output = isl_printer_get_file(p);
4439 if (!output)
4440 return isl_printer_free(p);
4442 if (copy(ext->input, output, scop->start, scop->end) < 0)
4443 return isl_printer_free(p);
4445 return p;