PetScan::extract_non_affine_condition: take statement number as argument
[pet.git] / scop.c
blob021bc770564811d4451c1374dd7ed8964d2d5da1
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 "dim" and return the result.
3029 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
3030 __isl_take isl_space *dim)
3032 int i;
3034 if (!expr)
3035 goto error;
3036 for (i = 0; i < expr->n_arg; ++i)
3038 dim = expr_collect_params(expr->args[i], dim);
3040 if (expr->type == pet_expr_access)
3041 dim = isl_space_align_params(dim,
3042 isl_map_get_space(expr->acc.access));
3044 return dim;
3045 error:
3046 pet_expr_free(expr);
3047 return isl_space_free(dim);
3050 /* Add all parameters in "stmt" to "dim" and return the result.
3052 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
3053 __isl_take isl_space *dim)
3055 if (!stmt)
3056 goto error;
3058 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
3059 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
3060 dim = expr_collect_params(stmt->body, dim);
3062 return dim;
3063 error:
3064 isl_space_free(dim);
3065 return pet_stmt_free(stmt);
3068 /* Add all parameters in "array" to "dim" and return the result.
3070 static __isl_give isl_space *array_collect_params(struct pet_array *array,
3071 __isl_take isl_space *dim)
3073 if (!array)
3074 goto error;
3076 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
3077 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
3079 return dim;
3080 error:
3081 pet_array_free(array);
3082 return isl_space_free(dim);
3085 /* Add all parameters in "scop" to "dim" and return the result.
3087 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
3088 __isl_take isl_space *dim)
3090 int i;
3092 if (!scop)
3093 goto error;
3095 for (i = 0; i < scop->n_array; ++i)
3096 dim = array_collect_params(scop->arrays[i], dim);
3098 for (i = 0; i < scop->n_stmt; ++i)
3099 dim = stmt_collect_params(scop->stmts[i], dim);
3101 return dim;
3102 error:
3103 isl_space_free(dim);
3104 pet_scop_free(scop);
3105 return NULL;
3108 /* Add all parameters in "dim" to all access relations and index expressions
3109 * in "expr".
3111 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
3112 __isl_take isl_space *dim)
3114 int i;
3116 if (!expr)
3117 goto error;
3119 for (i = 0; i < expr->n_arg; ++i) {
3120 expr->args[i] =
3121 expr_propagate_params(expr->args[i],
3122 isl_space_copy(dim));
3123 if (!expr->args[i])
3124 goto error;
3127 if (expr->type == pet_expr_access) {
3128 expr->acc.access = isl_map_align_params(expr->acc.access,
3129 isl_space_copy(dim));
3130 expr->acc.index = isl_multi_pw_aff_align_params(expr->acc.index,
3131 isl_space_copy(dim));
3132 if (!expr->acc.access || !expr->acc.index)
3133 goto error;
3136 isl_space_free(dim);
3137 return expr;
3138 error:
3139 isl_space_free(dim);
3140 return pet_expr_free(expr);
3143 /* Add all parameters in "dim" to the domain, schedule and
3144 * all access relations in "stmt".
3146 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
3147 __isl_take isl_space *dim)
3149 if (!stmt)
3150 goto error;
3152 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
3153 stmt->schedule = isl_map_align_params(stmt->schedule,
3154 isl_space_copy(dim));
3155 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
3157 if (!stmt->domain || !stmt->schedule || !stmt->body)
3158 goto error;
3160 isl_space_free(dim);
3161 return stmt;
3162 error:
3163 isl_space_free(dim);
3164 return pet_stmt_free(stmt);
3167 /* Add all parameters in "dim" to "array".
3169 static struct pet_array *array_propagate_params(struct pet_array *array,
3170 __isl_take isl_space *dim)
3172 if (!array)
3173 goto error;
3175 array->context = isl_set_align_params(array->context,
3176 isl_space_copy(dim));
3177 array->extent = isl_set_align_params(array->extent,
3178 isl_space_copy(dim));
3179 if (array->value_bounds) {
3180 array->value_bounds = isl_set_align_params(array->value_bounds,
3181 isl_space_copy(dim));
3182 if (!array->value_bounds)
3183 goto error;
3186 if (!array->context || !array->extent)
3187 goto error;
3189 isl_space_free(dim);
3190 return array;
3191 error:
3192 isl_space_free(dim);
3193 return pet_array_free(array);
3196 /* Add all parameters in "dim" to "scop".
3198 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
3199 __isl_take isl_space *dim)
3201 int i;
3203 if (!scop)
3204 goto error;
3206 for (i = 0; i < scop->n_array; ++i) {
3207 scop->arrays[i] = array_propagate_params(scop->arrays[i],
3208 isl_space_copy(dim));
3209 if (!scop->arrays[i])
3210 goto error;
3213 for (i = 0; i < scop->n_stmt; ++i) {
3214 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
3215 isl_space_copy(dim));
3216 if (!scop->stmts[i])
3217 goto error;
3220 isl_space_free(dim);
3221 return scop;
3222 error:
3223 isl_space_free(dim);
3224 return pet_scop_free(scop);
3227 /* Update all isl_sets and isl_maps in "scop" such that they all
3228 * have the same parameters.
3230 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
3232 isl_space *dim;
3234 if (!scop)
3235 return NULL;
3237 dim = isl_set_get_space(scop->context);
3238 dim = scop_collect_params(scop, dim);
3240 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
3241 scop = scop_propagate_params(scop, dim);
3243 return scop;
3246 /* Check if the given index expression accesses a (0D) array that corresponds
3247 * to one of the parameters in "dim". If so, replace the array access
3248 * by an access to the set of integers with as index (and value)
3249 * that parameter.
3251 static __isl_give isl_multi_pw_aff *index_detect_parameter(
3252 __isl_take isl_multi_pw_aff *index, __isl_take isl_space *space)
3254 isl_local_space *ls;
3255 isl_id *array_id = NULL;
3256 isl_aff *aff;
3257 int pos = -1;
3259 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out)) {
3260 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
3261 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3263 isl_space_free(space);
3265 if (pos < 0) {
3266 isl_id_free(array_id);
3267 return index;
3270 space = isl_multi_pw_aff_get_domain_space(index);
3271 isl_multi_pw_aff_free(index);
3273 pos = isl_space_find_dim_by_id(space, isl_dim_param, array_id);
3274 if (pos < 0) {
3275 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
3276 space = isl_space_set_dim_id(space, isl_dim_param, 0, array_id);
3277 pos = 0;
3278 } else
3279 isl_id_free(array_id);
3281 ls = isl_local_space_from_space(space);
3282 aff = isl_aff_var_on_domain(ls, isl_dim_param, pos);
3283 index = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
3285 return index;
3288 /* Check if the given access relation accesses a (0D) array that corresponds
3289 * to one of the parameters in "dim". If so, replace the array access
3290 * by an access to the set of integers with as index (and value)
3291 * that parameter.
3293 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
3294 __isl_take isl_space *dim)
3296 isl_id *array_id = NULL;
3297 int pos = -1;
3299 if (isl_map_has_tuple_id(access, isl_dim_out)) {
3300 array_id = isl_map_get_tuple_id(access, isl_dim_out);
3301 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
3303 isl_space_free(dim);
3305 if (pos < 0) {
3306 isl_id_free(array_id);
3307 return access;
3310 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
3311 if (pos < 0) {
3312 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
3313 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
3314 pos = 0;
3315 } else
3316 isl_id_free(array_id);
3318 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
3319 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
3321 return access;
3324 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3325 * in "dim" by a value equal to the corresponding parameter.
3327 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
3328 __isl_take isl_space *dim)
3330 int i;
3332 if (!expr)
3333 goto error;
3335 for (i = 0; i < expr->n_arg; ++i) {
3336 expr->args[i] =
3337 expr_detect_parameter_accesses(expr->args[i],
3338 isl_space_copy(dim));
3339 if (!expr->args[i])
3340 goto error;
3343 if (expr->type == pet_expr_access) {
3344 expr->acc.access = access_detect_parameter(expr->acc.access,
3345 isl_space_copy(dim));
3346 expr->acc.index = index_detect_parameter(expr->acc.index,
3347 isl_space_copy(dim));
3348 if (!expr->acc.access || !expr->acc.index)
3349 goto error;
3352 isl_space_free(dim);
3353 return expr;
3354 error:
3355 isl_space_free(dim);
3356 return pet_expr_free(expr);
3359 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3360 * in "dim" by a value equal to the corresponding parameter.
3362 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
3363 __isl_take isl_space *dim)
3365 if (!stmt)
3366 goto error;
3368 stmt->body = expr_detect_parameter_accesses(stmt->body,
3369 isl_space_copy(dim));
3371 if (!stmt->domain || !stmt->schedule || !stmt->body)
3372 goto error;
3374 isl_space_free(dim);
3375 return stmt;
3376 error:
3377 isl_space_free(dim);
3378 return pet_stmt_free(stmt);
3381 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
3382 * in "dim" by a value equal to the corresponding parameter.
3384 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
3385 __isl_take isl_space *dim)
3387 int i;
3389 if (!scop)
3390 goto error;
3392 for (i = 0; i < scop->n_stmt; ++i) {
3393 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
3394 isl_space_copy(dim));
3395 if (!scop->stmts[i])
3396 goto error;
3399 isl_space_free(dim);
3400 return scop;
3401 error:
3402 isl_space_free(dim);
3403 return pet_scop_free(scop);
3406 /* Replace all accesses to (0D) arrays that correspond to any of
3407 * the parameters used in "scop" by a value equal
3408 * to the corresponding parameter.
3410 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
3412 isl_space *dim;
3414 if (!scop)
3415 return NULL;
3417 dim = isl_set_get_space(scop->context);
3418 dim = scop_collect_params(scop, dim);
3420 scop = scop_detect_parameter_accesses(scop, dim);
3422 return scop;
3425 /* Return the relation mapping domain iterations to all possibly
3426 * accessed data elements.
3427 * In particular, take the access relation and project out the values
3428 * of the arguments, if any.
3430 __isl_give isl_map *pet_expr_access_get_may_access(struct pet_expr *expr)
3432 isl_map *access;
3433 isl_space *space;
3434 isl_map *map;
3436 if (!expr)
3437 return NULL;
3438 if (expr->type != pet_expr_access)
3439 return NULL;
3441 access = isl_map_copy(expr->acc.access);
3442 if (expr->n_arg == 0)
3443 return access;
3445 space = isl_space_domain(isl_map_get_space(access));
3446 map = isl_map_universe(isl_space_unwrap(space));
3447 map = isl_map_domain_map(map);
3448 access = isl_map_apply_domain(access, map);
3450 return access;
3453 /* Return the relation mapping domain iterations to all possibly
3454 * accessed data elements, with its domain tagged with the reference
3455 * identifier.
3457 __isl_give isl_map *pet_expr_access_get_tagged_may_access(
3458 struct pet_expr *expr)
3460 isl_map *access;
3462 if (!expr)
3463 return NULL;
3465 access = pet_expr_access_get_may_access(expr);
3466 access = tag_access(access, isl_id_copy(expr->acc.ref_id));
3468 return access;
3471 /* Add the access relation of the access expression "expr" to "accesses" and
3472 * return the result.
3473 * The domain of the access relation is intersected with "domain".
3474 * If "tag" is set, then the access relation is tagged with
3475 * the corresponding reference identifier.
3477 static __isl_give isl_union_map *expr_collect_access(struct pet_expr *expr,
3478 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
3480 isl_map *access;
3482 access = pet_expr_access_get_may_access(expr);
3483 access = isl_map_intersect_domain(access, isl_set_copy(domain));
3484 if (tag)
3485 access = tag_access(access, isl_id_copy(expr->acc.ref_id));
3486 return isl_union_map_add_map(accesses, access);
3489 /* Add all read access relations (if "read" is set) and/or all write
3490 * access relations (if "write" is set) to "accesses" and return the result.
3491 * The domains of the access relations are intersected with "domain".
3492 * If "tag" is set, then the access relations are tagged with
3493 * the corresponding reference identifiers.
3495 * If "must" is set, then we only add the accesses that are definitely
3496 * performed. Otherwise, we add all potential accesses.
3497 * In particular, if the access has any arguments, then if "must" is
3498 * set we currently skip the access completely. If "must" is not set,
3499 * we project out the values of the access arguments.
3501 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
3502 int read, int write, int must, int tag,
3503 __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
3505 int i;
3506 isl_id *id;
3507 isl_space *dim;
3509 if (!expr)
3510 return isl_union_map_free(accesses);
3512 for (i = 0; i < expr->n_arg; ++i)
3513 accesses = expr_collect_accesses(expr->args[i],
3514 read, write, must, tag, accesses, domain);
3516 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
3517 ((read && expr->acc.read) || (write && expr->acc.write)) &&
3518 (!must || expr->n_arg == 0)) {
3519 accesses = expr_collect_access(expr, tag, accesses, domain);
3522 return accesses;
3525 /* Collect and return all read access relations (if "read" is set)
3526 * and/or all write access relations (if "write" is set) in "stmt".
3527 * If "tag" is set, then the access relations are tagged with
3528 * the corresponding reference identifiers.
3529 * If "kill" is set, then "stmt" is a kill statement and we simply
3530 * add the argument of the kill operation.
3532 * If "must" is set, then we only add the accesses that are definitely
3533 * performed. Otherwise, we add all potential accesses.
3534 * In particular, if the statement has any arguments, then if "must" is
3535 * set we currently skip the statement completely. If "must" is not set,
3536 * we project out the values of the statement arguments.
3538 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
3539 int read, int write, int kill, int must, int tag,
3540 __isl_take isl_space *dim)
3542 isl_union_map *accesses;
3543 isl_set *domain;
3545 if (!stmt)
3546 return NULL;
3548 accesses = isl_union_map_empty(dim);
3550 if (must && stmt->n_arg > 0)
3551 return accesses;
3553 domain = isl_set_copy(stmt->domain);
3554 if (isl_set_is_wrapping(domain))
3555 domain = isl_map_domain(isl_set_unwrap(domain));
3557 if (kill)
3558 accesses = expr_collect_access(stmt->body->args[0], tag,
3559 accesses, domain);
3560 else
3561 accesses = expr_collect_accesses(stmt->body, read, write,
3562 must, tag, accesses, domain);
3563 isl_set_free(domain);
3565 return accesses;
3568 /* Is "stmt" a kill statement?
3570 static int is_kill(struct pet_stmt *stmt)
3572 if (stmt->body->type != pet_expr_unary)
3573 return 0;
3574 return stmt->body->op == pet_op_kill;
3577 /* Is "stmt" an assume statement?
3579 int pet_stmt_is_assume(struct pet_stmt *stmt)
3581 if (stmt->body->type != pet_expr_unary)
3582 return 0;
3583 return stmt->body->op == pet_op_assume;
3586 /* Compute a mapping from all arrays (of structs) in scop
3587 * to their innermost arrays.
3589 * In particular, for each array of a primitive type, the result
3590 * contains the identity mapping on that array.
3591 * For each array involving member accesses, the result
3592 * contains a mapping from the elements of any intermediate array of structs
3593 * to all corresponding elements of the innermost nested arrays.
3595 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
3597 int i;
3598 isl_union_map *to_inner;
3600 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
3602 for (i = 0; i < scop->n_array; ++i) {
3603 struct pet_array *array = scop->arrays[i];
3604 isl_set *set;
3605 isl_map *map, *gist;
3607 if (array->element_is_record)
3608 continue;
3610 map = isl_set_identity(isl_set_copy(array->extent));
3612 set = isl_map_domain(isl_map_copy(map));
3613 gist = isl_map_copy(map);
3614 gist = isl_map_gist_domain(gist, isl_set_copy(set));
3615 to_inner = isl_union_map_add_map(to_inner, gist);
3617 while (set && isl_set_is_wrapping(set)) {
3618 isl_id *id;
3619 isl_map *wrapped;
3621 id = isl_set_get_tuple_id(set);
3622 wrapped = isl_set_unwrap(set);
3623 wrapped = isl_map_domain_map(wrapped);
3624 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
3625 map = isl_map_apply_domain(map, wrapped);
3626 set = isl_map_domain(isl_map_copy(map));
3627 gist = isl_map_copy(map);
3628 gist = isl_map_gist_domain(gist, isl_set_copy(set));
3629 to_inner = isl_union_map_add_map(to_inner, gist);
3632 isl_set_free(set);
3633 isl_map_free(map);
3636 return to_inner;
3639 /* Collect and return all read access relations (if "read" is set)
3640 * and/or all write access relations (if "write" is set) in "scop".
3641 * If "kill" is set, then we only add the arguments of kill operations.
3642 * If "must" is set, then we only add the accesses that are definitely
3643 * performed. Otherwise, we add all potential accesses.
3644 * If "tag" is set, then the access relations are tagged with
3645 * the corresponding reference identifiers.
3646 * For accesses to structures, the returned access relation accesses
3647 * all individual fields in the structures.
3649 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
3650 int read, int write, int kill, int must, int tag)
3652 int i;
3653 isl_union_map *accesses;
3654 isl_union_set *arrays;
3655 isl_union_map *to_inner;
3657 if (!scop)
3658 return NULL;
3660 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
3662 for (i = 0; i < scop->n_stmt; ++i) {
3663 struct pet_stmt *stmt = scop->stmts[i];
3664 isl_union_map *accesses_i;
3665 isl_space *space;
3667 if (kill && !is_kill(stmt))
3668 continue;
3670 space = isl_set_get_space(scop->context);
3671 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
3672 must, tag, space);
3673 accesses = isl_union_map_union(accesses, accesses_i);
3676 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
3677 for (i = 0; i < scop->n_array; ++i) {
3678 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
3679 arrays = isl_union_set_add_set(arrays, extent);
3681 accesses = isl_union_map_intersect_range(accesses, arrays);
3683 to_inner = compute_to_inner(scop);
3684 accesses = isl_union_map_apply_range(accesses, to_inner);
3686 return accesses;
3689 /* Collect all potential read access relations.
3691 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
3693 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
3696 /* Collect all potential write access relations.
3698 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
3700 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
3703 /* Collect all definite write access relations.
3705 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
3707 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
3710 /* Collect all definite kill access relations.
3712 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
3714 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
3717 /* Collect all tagged potential read access relations.
3719 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
3720 struct pet_scop *scop)
3722 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
3725 /* Collect all tagged potential write access relations.
3727 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
3728 struct pet_scop *scop)
3730 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
3733 /* Collect all tagged definite write access relations.
3735 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
3736 struct pet_scop *scop)
3738 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
3741 /* Collect all tagged definite kill access relations.
3743 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
3744 struct pet_scop *scop)
3746 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
3749 /* Collect and return the union of iteration domains in "scop".
3751 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
3753 int i;
3754 isl_set *domain_i;
3755 isl_union_set *domain;
3757 if (!scop)
3758 return NULL;
3760 domain = isl_union_set_empty(isl_set_get_space(scop->context));
3762 for (i = 0; i < scop->n_stmt; ++i) {
3763 domain_i = isl_set_copy(scop->stmts[i]->domain);
3764 domain = isl_union_set_add_set(domain, domain_i);
3767 return domain;
3770 /* Collect and return the schedules of the statements in "scop".
3771 * The range is normalized to the maximal number of scheduling
3772 * dimensions.
3774 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
3776 int i, j;
3777 isl_map *schedule_i;
3778 isl_union_map *schedule;
3779 int depth, max_depth = 0;
3781 if (!scop)
3782 return NULL;
3784 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
3786 for (i = 0; i < scop->n_stmt; ++i) {
3787 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
3788 if (depth > max_depth)
3789 max_depth = depth;
3792 for (i = 0; i < scop->n_stmt; ++i) {
3793 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
3794 depth = isl_map_dim(schedule_i, isl_dim_out);
3795 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
3796 max_depth - depth);
3797 for (j = depth; j < max_depth; ++j)
3798 schedule_i = isl_map_fix_si(schedule_i,
3799 isl_dim_out, j, 0);
3800 schedule = isl_union_map_add_map(schedule, schedule_i);
3803 return schedule;
3806 /* Does expression "expr" write to "id"?
3808 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
3810 int i;
3811 isl_id *write_id;
3813 for (i = 0; i < expr->n_arg; ++i) {
3814 int writes = expr_writes(expr->args[i], id);
3815 if (writes < 0 || writes)
3816 return writes;
3819 if (expr->type != pet_expr_access)
3820 return 0;
3821 if (!expr->acc.write)
3822 return 0;
3823 if (pet_expr_is_affine(expr))
3824 return 0;
3826 write_id = pet_expr_access_get_id(expr);
3827 isl_id_free(write_id);
3829 if (!write_id)
3830 return -1;
3832 return write_id == id;
3835 /* Does statement "stmt" write to "id"?
3837 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
3839 return expr_writes(stmt->body, id);
3842 /* Is there any write access in "scop" that accesses "id"?
3844 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
3846 int i;
3848 if (!scop)
3849 return -1;
3851 for (i = 0; i < scop->n_stmt; ++i) {
3852 int writes = stmt_writes(scop->stmts[i], id);
3853 if (writes < 0 || writes)
3854 return writes;
3857 return 0;
3860 /* Add a reference identifier to access expression "expr".
3861 * "user" points to an integer that contains the sequence number
3862 * of the next reference.
3864 static struct pet_expr *access_add_ref_id(struct pet_expr *expr, void *user)
3866 isl_ctx *ctx;
3867 char name[50];
3868 int *n_ref = user;
3870 if (!expr)
3871 return expr;
3873 ctx = isl_map_get_ctx(expr->acc.access);
3874 snprintf(name, sizeof(name), "__pet_ref_%d", (*n_ref)++);
3875 expr->acc.ref_id = isl_id_alloc(ctx, name, NULL);
3876 if (!expr->acc.ref_id)
3877 return pet_expr_free(expr);
3879 return expr;
3882 /* Add a reference identifier to all access expressions in "stmt".
3883 * "n_ref" points to an integer that contains the sequence number
3884 * of the next reference.
3886 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
3888 int i;
3890 if (!stmt)
3891 return NULL;
3893 for (i = 0; i < stmt->n_arg; ++i) {
3894 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3895 &access_add_ref_id, n_ref);
3896 if (!stmt->args[i])
3897 return pet_stmt_free(stmt);
3900 stmt->body = pet_expr_map_access(stmt->body, &access_add_ref_id, n_ref);
3901 if (!stmt->body)
3902 return pet_stmt_free(stmt);
3904 return stmt;
3907 /* Add a reference identifier to all access expressions in "scop".
3909 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
3911 int i;
3912 int n_ref;
3914 if (!scop)
3915 return NULL;
3917 n_ref = 0;
3918 for (i = 0; i < scop->n_stmt; ++i) {
3919 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
3920 if (!scop->stmts[i])
3921 return pet_scop_free(scop);
3924 return scop;
3927 /* Reset the user pointer on all parameter ids in "array".
3929 static struct pet_array *array_anonymize(struct pet_array *array)
3931 if (!array)
3932 return NULL;
3934 array->context = isl_set_reset_user(array->context);
3935 array->extent = isl_set_reset_user(array->extent);
3936 if (!array->context || !array->extent)
3937 return pet_array_free(array);
3939 return array;
3942 /* Reset the user pointer on all parameter and tuple ids in
3943 * the access relation and the index expressions
3944 * of the access expression "expr".
3946 static struct pet_expr *access_anonymize(struct pet_expr *expr, void *user)
3948 expr->acc.access = isl_map_reset_user(expr->acc.access);
3949 expr->acc.index = isl_multi_pw_aff_reset_user(expr->acc.index);
3950 if (!expr->acc.access || !expr->acc.index)
3951 return pet_expr_free(expr);
3953 return expr;
3956 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3958 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3960 int i;
3961 isl_space *space;
3962 isl_set *domain;
3964 if (!stmt)
3965 return NULL;
3967 stmt->domain = isl_set_reset_user(stmt->domain);
3968 stmt->schedule = isl_map_reset_user(stmt->schedule);
3969 if (!stmt->domain || !stmt->schedule)
3970 return pet_stmt_free(stmt);
3972 for (i = 0; i < stmt->n_arg; ++i) {
3973 stmt->args[i] = pet_expr_map_access(stmt->args[i],
3974 &access_anonymize, NULL);
3975 if (!stmt->args[i])
3976 return pet_stmt_free(stmt);
3979 stmt->body = pet_expr_map_access(stmt->body,
3980 &access_anonymize, NULL);
3981 if (!stmt->body)
3982 return pet_stmt_free(stmt);
3984 return stmt;
3987 /* Reset the user pointer on the tuple ids and all parameter ids
3988 * in "implication".
3990 static struct pet_implication *implication_anonymize(
3991 struct pet_implication *implication)
3993 if (!implication)
3994 return NULL;
3996 implication->extension = isl_map_reset_user(implication->extension);
3997 if (!implication->extension)
3998 return pet_implication_free(implication);
4000 return implication;
4003 /* Reset the user pointer on all parameter and tuple ids in "scop".
4005 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
4007 int i;
4009 if (!scop)
4010 return NULL;
4012 scop->context = isl_set_reset_user(scop->context);
4013 scop->context_value = isl_set_reset_user(scop->context_value);
4014 if (!scop->context || !scop->context_value)
4015 return pet_scop_free(scop);
4017 for (i = 0; i < scop->n_array; ++i) {
4018 scop->arrays[i] = array_anonymize(scop->arrays[i]);
4019 if (!scop->arrays[i])
4020 return pet_scop_free(scop);
4023 for (i = 0; i < scop->n_stmt; ++i) {
4024 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
4025 if (!scop->stmts[i])
4026 return pet_scop_free(scop);
4029 for (i = 0; i < scop->n_implication; ++i) {
4030 scop->implications[i] =
4031 implication_anonymize(scop->implications[i]);
4032 if (!scop->implications[i])
4033 return pet_scop_free(scop);
4036 return scop;
4039 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
4040 * then intersect the range of "map" with the valid set of values.
4042 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
4043 struct pet_expr *arg, __isl_keep isl_union_map *value_bounds)
4045 isl_id *id;
4046 isl_map *vb;
4047 isl_space *space;
4048 isl_ctx *ctx = isl_map_get_ctx(map);
4050 id = pet_expr_access_get_id(arg);
4051 space = isl_space_alloc(ctx, 0, 0, 1);
4052 space = isl_space_set_tuple_id(space, isl_dim_in, id);
4053 vb = isl_union_map_extract_map(value_bounds, space);
4054 if (!isl_map_plain_is_empty(vb))
4055 map = isl_map_intersect_range(map, isl_map_range(vb));
4056 else
4057 isl_map_free(vb);
4059 return map;
4062 /* Given a set "domain", return a wrapped relation with the given set
4063 * as domain and a range of dimension "n_arg", where each coordinate
4064 * is either unbounded or, if the corresponding element of args is of
4065 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
4067 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
4068 unsigned n_arg, struct pet_expr **args,
4069 __isl_keep isl_union_map *value_bounds)
4071 int i;
4072 isl_map *map;
4073 isl_space *space;
4075 map = isl_map_from_domain(domain);
4076 space = isl_map_get_space(map);
4077 space = isl_space_add_dims(space, isl_dim_out, 1);
4079 for (i = 0; i < n_arg; ++i) {
4080 isl_map *map_i;
4081 struct pet_expr *arg = args[i];
4083 map_i = isl_map_universe(isl_space_copy(space));
4084 if (arg->type == pet_expr_access)
4085 map_i = access_apply_value_bounds(map_i, arg,
4086 value_bounds);
4087 map = isl_map_flat_range_product(map, map_i);
4089 isl_space_free(space);
4091 return isl_map_wrap(map);
4094 /* Data used in access_gist() callback.
4096 struct pet_access_gist_data {
4097 isl_set *domain;
4098 isl_union_map *value_bounds;
4101 /* Given an expression "expr" of type pet_expr_access, compute
4102 * the gist of the associated access relation and index expression
4103 * with respect to data->domain and the bounds on the values of the arguments
4104 * of the expression.
4106 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
4108 struct pet_access_gist_data *data = user;
4109 isl_set *domain;
4111 domain = isl_set_copy(data->domain);
4112 if (expr->n_arg > 0)
4113 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
4114 data->value_bounds);
4116 expr->acc.access = isl_map_gist_domain(expr->acc.access,
4117 isl_set_copy(domain));
4118 expr->acc.index = isl_multi_pw_aff_gist(expr->acc.index, domain);
4119 if (!expr->acc.access || !expr->acc.index)
4120 return pet_expr_free(expr);
4122 return expr;
4125 /* Compute the gist of the iteration domain and all access relations
4126 * of "stmt" based on the constraints on the parameters specified by "context"
4127 * and the constraints on the values of nested accesses specified
4128 * by "value_bounds".
4130 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
4131 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
4133 int i;
4134 isl_space *space;
4135 isl_set *domain;
4136 struct pet_access_gist_data data;
4138 if (!stmt)
4139 return NULL;
4141 data.domain = isl_set_copy(stmt->domain);
4142 data.value_bounds = value_bounds;
4143 if (stmt->n_arg > 0)
4144 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
4146 data.domain = isl_set_intersect_params(data.domain,
4147 isl_set_copy(context));
4149 for (i = 0; i < stmt->n_arg; ++i) {
4150 stmt->args[i] = pet_expr_map_access(stmt->args[i],
4151 &access_gist, &data);
4152 if (!stmt->args[i])
4153 goto error;
4156 stmt->body = pet_expr_map_access(stmt->body, &access_gist, &data);
4157 if (!stmt->body)
4158 goto error;
4160 isl_set_free(data.domain);
4162 space = isl_set_get_space(stmt->domain);
4163 if (isl_space_is_wrapping(space))
4164 space = isl_space_domain(isl_space_unwrap(space));
4165 domain = isl_set_universe(space);
4166 domain = isl_set_intersect_params(domain, isl_set_copy(context));
4167 if (stmt->n_arg > 0)
4168 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
4169 value_bounds);
4170 stmt->domain = isl_set_gist(stmt->domain, domain);
4171 if (!stmt->domain)
4172 return pet_stmt_free(stmt);
4174 return stmt;
4175 error:
4176 isl_set_free(data.domain);
4177 return pet_stmt_free(stmt);
4180 /* Compute the gist of the extent of the array
4181 * based on the constraints on the parameters specified by "context".
4183 static struct pet_array *array_gist(struct pet_array *array,
4184 __isl_keep isl_set *context)
4186 if (!array)
4187 return NULL;
4189 array->extent = isl_set_gist_params(array->extent,
4190 isl_set_copy(context));
4191 if (!array->extent)
4192 return pet_array_free(array);
4194 return array;
4197 /* Compute the gist of all sets and relations in "scop"
4198 * based on the constraints on the parameters specified by "scop->context"
4199 * and the constraints on the values of nested accesses specified
4200 * by "value_bounds".
4202 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
4203 __isl_keep isl_union_map *value_bounds)
4205 int i;
4207 if (!scop)
4208 return NULL;
4210 scop->context = isl_set_coalesce(scop->context);
4211 if (!scop->context)
4212 return pet_scop_free(scop);
4214 for (i = 0; i < scop->n_array; ++i) {
4215 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
4216 if (!scop->arrays[i])
4217 return pet_scop_free(scop);
4220 for (i = 0; i < scop->n_stmt; ++i) {
4221 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
4222 value_bounds);
4223 if (!scop->stmts[i])
4224 return pet_scop_free(scop);
4227 return scop;
4230 /* Intersect the context of "scop" with "context".
4231 * To ensure that we don't introduce any unnamed parameters in
4232 * the context of "scop", we first remove the unnamed parameters
4233 * from "context".
4235 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
4236 __isl_take isl_set *context)
4238 if (!scop)
4239 goto error;
4241 context = set_project_out_unnamed_params(context);
4242 scop->context = isl_set_intersect(scop->context, context);
4243 if (!scop->context)
4244 return pet_scop_free(scop);
4246 return scop;
4247 error:
4248 isl_set_free(context);
4249 return pet_scop_free(scop);
4252 /* Drop the current context of "scop". That is, replace the context
4253 * by a universal set.
4255 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
4257 isl_space *space;
4259 if (!scop)
4260 return NULL;
4262 space = isl_set_get_space(scop->context);
4263 isl_set_free(scop->context);
4264 scop->context = isl_set_universe(space);
4265 if (!scop->context)
4266 return pet_scop_free(scop);
4268 return scop;
4271 /* Append "array" to the arrays of "scop".
4273 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
4274 struct pet_array *array)
4276 isl_ctx *ctx;
4277 struct pet_array **arrays;
4279 if (!array || !scop)
4280 goto error;
4282 ctx = isl_set_get_ctx(scop->context);
4283 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
4284 scop->n_array + 1);
4285 if (!arrays)
4286 goto error;
4287 scop->arrays = arrays;
4288 scop->arrays[scop->n_array] = array;
4289 scop->n_array++;
4291 return scop;
4292 error:
4293 pet_array_free(array);
4294 return pet_scop_free(scop);
4297 /* Create and return an implication on filter values equal to "satisfied"
4298 * with extension "map".
4300 static struct pet_implication *new_implication(__isl_take isl_map *map,
4301 int satisfied)
4303 isl_ctx *ctx;
4304 struct pet_implication *implication;
4306 if (!map)
4307 return NULL;
4308 ctx = isl_map_get_ctx(map);
4309 implication = isl_alloc_type(ctx, struct pet_implication);
4310 if (!implication)
4311 goto error;
4313 implication->extension = map;
4314 implication->satisfied = satisfied;
4316 return implication;
4317 error:
4318 isl_map_free(map);
4319 return NULL;
4322 /* Add an implication on filter values equal to "satisfied"
4323 * with extension "map" to "scop".
4325 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
4326 __isl_take isl_map *map, int satisfied)
4328 isl_ctx *ctx;
4329 struct pet_implication *implication;
4330 struct pet_implication **implications;
4332 implication = new_implication(map, satisfied);
4333 if (!scop || !implication)
4334 goto error;
4336 ctx = isl_set_get_ctx(scop->context);
4337 implications = isl_realloc_array(ctx, scop->implications,
4338 struct pet_implication *,
4339 scop->n_implication + 1);
4340 if (!implications)
4341 goto error;
4342 scop->implications = implications;
4343 scop->implications[scop->n_implication] = implication;
4344 scop->n_implication++;
4346 return scop;
4347 error:
4348 pet_implication_free(implication);
4349 return pet_scop_free(scop);
4352 /* Given an access expression, check if it is data dependent.
4353 * If so, set *found and abort the search.
4355 static int is_data_dependent(struct pet_expr *expr, void *user)
4357 int *found = user;
4359 if (expr->n_arg) {
4360 *found = 1;
4361 return -1;
4364 return 0;
4367 /* Does "scop" contain any data dependent accesses?
4369 * Check the body of each statement for such accesses.
4371 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
4373 int i;
4374 int found = 0;
4376 if (!scop)
4377 return -1;
4379 for (i = 0; i < scop->n_stmt; ++i) {
4380 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
4381 &is_data_dependent, &found);
4382 if (r < 0 && !found)
4383 return -1;
4384 if (found)
4385 return found;
4388 return found;
4391 /* Does "scop" contain and data dependent conditions?
4393 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
4395 int i;
4397 if (!scop)
4398 return -1;
4400 for (i = 0; i < scop->n_stmt; ++i)
4401 if (scop->stmts[i]->n_arg > 0)
4402 return 1;
4404 return 0;
4407 /* Keep track of the "input" file inside the (extended) "scop".
4409 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
4411 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4413 if (!scop)
4414 return NULL;
4416 ext->input = input;
4418 return scop;
4421 /* Print the original code corresponding to "scop" to printer "p".
4423 * pet_scop_print_original can only be called from
4424 * a pet_transform_C_source callback. This means that the input
4425 * file is stored in the extended scop and that the printer prints
4426 * to a file.
4428 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
4429 __isl_take isl_printer *p)
4431 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
4432 FILE *output;
4434 if (!scop || !p)
4435 return isl_printer_free(p);
4437 if (!ext->input)
4438 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
4439 "no input file stored in scop",
4440 return isl_printer_free(p));
4442 output = isl_printer_get_file(p);
4443 if (!output)
4444 return isl_printer_free(p);
4446 if (copy(ext->input, output, scop->start, scop->end) < 0)
4447 return isl_printer_free(p);
4449 return p;