PetScan::extract_for: explicitly keep track of when iterator is virtual
[pet.git] / scop.c
blobb9c1cd1bea9e77f944f49fbab0944268719b0a7d
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012 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 <isl/constraint.h>
36 #include <isl/union_set.h>
38 #include "scop.h"
40 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
42 static char *type_str[] = {
43 [pet_expr_access] = "access",
44 [pet_expr_call] = "call",
45 [pet_expr_double] = "double",
46 [pet_expr_unary] = "unary",
47 [pet_expr_binary] = "binary",
48 [pet_expr_ternary] = "ternary"
51 static char *op_str[] = {
52 [pet_op_add_assign] = "+=",
53 [pet_op_sub_assign] = "-=",
54 [pet_op_mul_assign] = "*=",
55 [pet_op_div_assign] = "/=",
56 [pet_op_assign] = "=",
57 [pet_op_add] = "+",
58 [pet_op_sub] = "-",
59 [pet_op_mul] = "*",
60 [pet_op_div] = "/",
61 [pet_op_eq] = "==",
62 [pet_op_le] = "<=",
63 [pet_op_lt] = "<",
64 [pet_op_gt] = ">",
65 [pet_op_minus] = "-",
66 [pet_op_address_of] = "&"
69 const char *pet_op_str(enum pet_op_type op)
71 return op_str[op];
74 const char *pet_type_str(enum pet_expr_type type)
76 return type_str[type];
79 enum pet_op_type pet_str_op(const char *str)
81 int i;
83 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
84 if (!strcmp(op_str[i], str))
85 return i;
87 return -1;
90 enum pet_expr_type pet_str_type(const char *str)
92 int i;
94 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
95 if (!strcmp(type_str[i], str))
96 return i;
98 return -1;
101 /* Construct a pet_expr from an access relation.
102 * By default, it is considered to be a read access.
104 struct pet_expr *pet_expr_from_access(__isl_take isl_map *access)
106 isl_ctx *ctx = isl_map_get_ctx(access);
107 struct pet_expr *expr;
109 if (!access)
110 return NULL;
111 expr = isl_calloc_type(ctx, struct pet_expr);
112 if (!expr)
113 goto error;
115 expr->type = pet_expr_access;
116 expr->acc.access = access;
117 expr->acc.read = 1;
118 expr->acc.write = 0;
120 return expr;
121 error:
122 isl_map_free(access);
123 return NULL;
126 /* Construct a unary pet_expr that performs "op" on "arg".
128 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
129 struct pet_expr *arg)
131 struct pet_expr *expr;
133 if (!arg)
134 goto error;
135 expr = isl_alloc_type(ctx, struct pet_expr);
136 if (!expr)
137 goto error;
139 expr->type = pet_expr_unary;
140 expr->op = op;
141 expr->n_arg = 1;
142 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
143 if (!expr->args)
144 goto error;
145 expr->args[pet_un_arg] = arg;
147 return expr;
148 error:
149 pet_expr_free(arg);
150 return NULL;
153 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
155 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
156 struct pet_expr *lhs, struct pet_expr *rhs)
158 struct pet_expr *expr;
160 if (!lhs || !rhs)
161 goto error;
162 expr = isl_alloc_type(ctx, struct pet_expr);
163 if (!expr)
164 goto error;
166 expr->type = pet_expr_binary;
167 expr->op = op;
168 expr->n_arg = 2;
169 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
170 if (!expr->args)
171 goto error;
172 expr->args[pet_bin_lhs] = lhs;
173 expr->args[pet_bin_rhs] = rhs;
175 return expr;
176 error:
177 pet_expr_free(lhs);
178 pet_expr_free(rhs);
179 return NULL;
182 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
184 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
185 struct pet_expr *lhs, struct pet_expr *rhs)
187 struct pet_expr *expr;
189 if (!cond || !lhs || !rhs)
190 goto error;
191 expr = isl_alloc_type(ctx, struct pet_expr);
192 if (!expr)
193 goto error;
195 expr->type = pet_expr_ternary;
196 expr->n_arg = 3;
197 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
198 if (!expr->args)
199 goto error;
200 expr->args[pet_ter_cond] = cond;
201 expr->args[pet_ter_true] = lhs;
202 expr->args[pet_ter_false] = rhs;
204 return expr;
205 error:
206 pet_expr_free(cond);
207 pet_expr_free(lhs);
208 pet_expr_free(rhs);
209 return NULL;
212 /* Construct a call pet_expr that calls function "name" with "n_arg"
213 * arguments. The caller is responsible for filling in the arguments.
215 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
216 unsigned n_arg)
218 struct pet_expr *expr;
220 expr = isl_alloc_type(ctx, struct pet_expr);
221 if (!expr)
222 return NULL;
224 expr->type = pet_expr_call;
225 expr->n_arg = n_arg;
226 expr->name = strdup(name);
227 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
228 if (!expr->name || !expr->args)
229 return pet_expr_free(expr);
231 return expr;
234 /* Construct a pet_expr that represents the double "d".
236 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double d)
238 struct pet_expr *expr;
240 expr = isl_calloc_type(ctx, struct pet_expr);
241 if (!expr)
242 return NULL;
244 expr->type = pet_expr_double;
245 expr->d = d;
247 return expr;
250 void *pet_expr_free(struct pet_expr *expr)
252 int i;
254 if (!expr)
255 return NULL;
257 for (i = 0; i < expr->n_arg; ++i)
258 pet_expr_free(expr->args[i]);
259 free(expr->args);
261 switch (expr->type) {
262 case pet_expr_access:
263 isl_map_free(expr->acc.access);
264 break;
265 case pet_expr_call:
266 free(expr->name);
267 break;
268 case pet_expr_double:
269 case pet_expr_unary:
270 case pet_expr_binary:
271 case pet_expr_ternary:
272 break;
275 free(expr);
276 return NULL;
279 static void expr_dump(struct pet_expr *expr, int indent)
281 int i;
283 if (!expr)
284 return;
286 fprintf(stderr, "%*s", indent, "");
288 switch (expr->type) {
289 case pet_expr_double:
290 fprintf(stderr, "%g\n", expr->d);
291 break;
292 case pet_expr_access:
293 isl_map_dump(expr->acc.access);
294 fprintf(stderr, "%*sread: %d\n", indent + 2,
295 "", expr->acc.read);
296 fprintf(stderr, "%*swrite: %d\n", indent + 2,
297 "", expr->acc.write);
298 for (i = 0; i < expr->n_arg; ++i)
299 expr_dump(expr->args[i], indent + 2);
300 break;
301 case pet_expr_unary:
302 fprintf(stderr, "%s\n", op_str[expr->op]);
303 expr_dump(expr->args[pet_un_arg], indent + 2);
304 break;
305 case pet_expr_binary:
306 fprintf(stderr, "%s\n", op_str[expr->op]);
307 expr_dump(expr->args[pet_bin_lhs], indent + 2);
308 expr_dump(expr->args[pet_bin_rhs], indent + 2);
309 break;
310 case pet_expr_ternary:
311 fprintf(stderr, "?:\n");
312 expr_dump(expr->args[pet_ter_cond], indent + 2);
313 expr_dump(expr->args[pet_ter_true], indent + 2);
314 expr_dump(expr->args[pet_ter_false], indent + 2);
315 break;
316 case pet_expr_call:
317 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
318 for (i = 0; i < expr->n_arg; ++i)
319 expr_dump(expr->args[i], indent + 2);
320 break;
324 void pet_expr_dump(struct pet_expr *expr)
326 expr_dump(expr, 0);
329 /* Return 1 if the two pet_exprs are equivalent.
331 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
333 int i;
335 if (!expr1 || !expr2)
336 return 0;
338 if (expr1->type != expr2->type)
339 return 0;
340 if (expr1->n_arg != expr2->n_arg)
341 return 0;
342 for (i = 0; i < expr1->n_arg; ++i)
343 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
344 return 0;
345 switch (expr1->type) {
346 case pet_expr_double:
347 if (expr1->d != expr2->d)
348 return 0;
349 break;
350 case pet_expr_access:
351 if (expr1->acc.read != expr2->acc.read)
352 return 0;
353 if (expr1->acc.write != expr2->acc.write)
354 return 0;
355 if (!expr1->acc.access || !expr2->acc.access)
356 return 0;
357 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
358 return 0;
359 break;
360 case pet_expr_unary:
361 case pet_expr_binary:
362 case pet_expr_ternary:
363 if (expr1->op != expr2->op)
364 return 0;
365 break;
366 case pet_expr_call:
367 if (strcmp(expr1->name, expr2->name))
368 return 0;
369 break;
372 return 1;
375 /* Add extra conditions on the parameters to all access relations in "expr".
377 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
378 __isl_take isl_set *cond)
380 int i;
382 if (!expr)
383 goto error;
385 for (i = 0; i < expr->n_arg; ++i) {
386 expr->args[i] = pet_expr_restrict(expr->args[i],
387 isl_set_copy(cond));
388 if (!expr->args[i])
389 goto error;
392 if (expr->type == pet_expr_access) {
393 expr->acc.access = isl_map_intersect_params(expr->acc.access,
394 isl_set_copy(cond));
395 if (!expr->acc.access)
396 goto error;
399 isl_set_free(cond);
400 return expr;
401 error:
402 isl_set_free(cond);
403 return pet_expr_free(expr);
406 /* Modify all access relations in "expr" by calling "fn" on them.
408 struct pet_expr *pet_expr_foreach_access(struct pet_expr *expr,
409 __isl_give isl_map *(*fn)(__isl_take isl_map *access, void *user),
410 void *user)
412 int i;
414 if (!expr)
415 return NULL;
417 for (i = 0; i < expr->n_arg; ++i) {
418 expr->args[i] = pet_expr_foreach_access(expr->args[i], fn, user);
419 if (!expr->args[i])
420 return pet_expr_free(expr);
423 if (expr->type == pet_expr_access) {
424 expr->acc.access = fn(expr->acc.access, user);
425 if (!expr->acc.access)
426 return pet_expr_free(expr);
429 return expr;
432 /* Modify all expressions of type pet_expr_access in "expr"
433 * by calling "fn" on them.
435 struct pet_expr *pet_expr_foreach_access_expr(struct pet_expr *expr,
436 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
437 void *user)
439 int i;
441 if (!expr)
442 return NULL;
444 for (i = 0; i < expr->n_arg; ++i) {
445 expr->args[i] = pet_expr_foreach_access_expr(expr->args[i],
446 fn, user);
447 if (!expr->args[i])
448 return pet_expr_free(expr);
451 if (expr->type == pet_expr_access)
452 expr = fn(expr, user);
454 return expr;
457 /* Modify the given access relation based on the given iteration space
458 * transformation.
459 * If the access has any arguments then the domain of the access relation
460 * is a wrapped mapping from the iteration space to the space of
461 * argument values. We only need to change the domain of this wrapped
462 * mapping, so we extend the input transformation with an identity mapping
463 * on the space of argument values.
465 static __isl_give isl_map *update_domain(__isl_take isl_map *access,
466 void *user)
468 isl_map *update = user;
469 isl_space *dim;
471 update = isl_map_copy(update);
473 dim = isl_map_get_space(access);
474 dim = isl_space_domain(dim);
475 if (!isl_space_is_wrapping(dim))
476 isl_space_free(dim);
477 else {
478 isl_map *id;
479 dim = isl_space_unwrap(dim);
480 dim = isl_space_range(dim);
481 dim = isl_space_map_from_set(dim);
482 id = isl_map_identity(dim);
483 update = isl_map_product(update, id);
486 return isl_map_apply_domain(access, update);
489 /* Modify all access relations in "expr" based on the given iteration space
490 * transformation.
492 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
493 __isl_take isl_map *update)
495 expr = pet_expr_foreach_access(expr, &update_domain, update);
496 isl_map_free(update);
497 return expr;
500 /* Construct a pet_stmt with given line number and statement
501 * number from a pet_expr.
502 * The initial iteration domain is the zero-dimensional universe.
503 * The name of the domain is given by "label" if it is non-NULL.
504 * Otherwise, the name is constructed as S_<id>.
505 * The domains of all access relations are modified to refer
506 * to the statement iteration domain.
508 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
509 __isl_take isl_id *label, int id, struct pet_expr *expr)
511 struct pet_stmt *stmt;
512 isl_space *dim;
513 isl_set *dom;
514 isl_map *sched;
515 isl_map *add_name;
516 char name[50];
518 if (!expr)
519 goto error;
521 stmt = isl_calloc_type(ctx, struct pet_stmt);
522 if (!stmt)
523 goto error;
525 dim = isl_space_set_alloc(ctx, 0, 0);
526 if (label)
527 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
528 else {
529 snprintf(name, sizeof(name), "S_%d", id);
530 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
532 dom = isl_set_universe(isl_space_copy(dim));
533 sched = isl_map_from_domain(isl_set_copy(dom));
535 dim = isl_space_from_range(dim);
536 add_name = isl_map_universe(dim);
537 expr = expr_update_domain(expr, add_name);
539 stmt->line = line;
540 stmt->domain = dom;
541 stmt->schedule = sched;
542 stmt->body = expr;
544 if (!stmt->domain || !stmt->schedule || !stmt->body)
545 return pet_stmt_free(stmt);
547 return stmt;
548 error:
549 isl_id_free(label);
550 return pet_expr_free(expr);
553 void *pet_stmt_free(struct pet_stmt *stmt)
555 int i;
557 if (!stmt)
558 return NULL;
560 isl_set_free(stmt->domain);
561 isl_map_free(stmt->schedule);
562 pet_expr_free(stmt->body);
564 for (i = 0; i < stmt->n_arg; ++i)
565 pet_expr_free(stmt->args[i]);
566 free(stmt->args);
568 free(stmt);
569 return NULL;
572 static void stmt_dump(struct pet_stmt *stmt, int indent)
574 int i;
576 if (!stmt)
577 return;
579 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
580 fprintf(stderr, "%*s", indent, "");
581 isl_set_dump(stmt->domain);
582 fprintf(stderr, "%*s", indent, "");
583 isl_map_dump(stmt->schedule);
584 expr_dump(stmt->body, indent);
585 for (i = 0; i < stmt->n_arg; ++i)
586 expr_dump(stmt->args[i], indent + 2);
589 void pet_stmt_dump(struct pet_stmt *stmt)
591 stmt_dump(stmt, 0);
594 void *pet_array_free(struct pet_array *array)
596 if (!array)
597 return NULL;
599 isl_set_free(array->context);
600 isl_set_free(array->extent);
601 isl_set_free(array->value_bounds);
602 free(array->element_type);
604 free(array);
605 return NULL;
608 void pet_array_dump(struct pet_array *array)
610 if (!array)
611 return;
613 isl_set_dump(array->context);
614 isl_set_dump(array->extent);
615 isl_set_dump(array->value_bounds);
616 fprintf(stderr, "%s %s\n", array->element_type,
617 array->live_out ? "live-out" : "");
620 /* Construct a pet_scop with room for n statements.
622 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
624 isl_space *space;
625 struct pet_scop *scop;
627 scop = isl_calloc_type(ctx, struct pet_scop);
628 if (!scop)
629 return NULL;
631 space = isl_space_params_alloc(ctx, 0);
632 scop->context = isl_set_universe(isl_space_copy(space));
633 scop->context_value = isl_set_universe(space);
634 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
635 if (!scop->context || !scop->stmts)
636 return pet_scop_free(scop);
638 scop->n_stmt = n;
640 return scop;
643 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
645 return scop_alloc(ctx, 0);
648 /* Construct a pet_scop that contains the given pet_stmt.
650 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
652 struct pet_scop *scop;
654 if (!stmt)
655 return NULL;
657 scop = scop_alloc(ctx, 1);
659 scop->stmts[0] = stmt;
661 return scop;
662 error:
663 pet_stmt_free(stmt);
664 pet_scop_free(scop);
665 return NULL;
668 /* Construct a pet_scop that contains the arrays and the statements
669 * in "scop1" and "scop2".
671 struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
672 struct pet_scop *scop2)
674 int i;
675 struct pet_scop *scop;
677 if (!scop1 || !scop2)
678 goto error;
680 if (scop1->n_stmt == 0) {
681 pet_scop_free(scop1);
682 return scop2;
685 if (scop2->n_stmt == 0) {
686 pet_scop_free(scop2);
687 return scop1;
690 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
691 if (!scop)
692 goto error;
694 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
695 scop1->n_array + scop2->n_array);
696 if (!scop->arrays)
697 goto error;
698 scop->n_array = scop1->n_array + scop2->n_array;
700 for (i = 0; i < scop1->n_stmt; ++i) {
701 scop->stmts[i] = scop1->stmts[i];
702 scop1->stmts[i] = NULL;
705 for (i = 0; i < scop2->n_stmt; ++i) {
706 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
707 scop2->stmts[i] = NULL;
710 for (i = 0; i < scop1->n_array; ++i) {
711 scop->arrays[i] = scop1->arrays[i];
712 scop1->arrays[i] = NULL;
715 for (i = 0; i < scop2->n_array; ++i) {
716 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
717 scop2->arrays[i] = NULL;
720 pet_scop_free(scop1);
721 pet_scop_free(scop2);
722 return scop;
723 error:
724 pet_scop_free(scop1);
725 pet_scop_free(scop2);
726 return NULL;
729 void *pet_scop_free(struct pet_scop *scop)
731 int i;
733 if (!scop)
734 return NULL;
735 isl_set_free(scop->context);
736 isl_set_free(scop->context_value);
737 if (scop->arrays)
738 for (i = 0; i < scop->n_array; ++i)
739 pet_array_free(scop->arrays[i]);
740 free(scop->arrays);
741 if (scop->stmts)
742 for (i = 0; i < scop->n_stmt; ++i)
743 pet_stmt_free(scop->stmts[i]);
744 free(scop->stmts);
745 free(scop);
746 return NULL;
749 void pet_scop_dump(struct pet_scop *scop)
751 int i;
753 if (!scop)
754 return;
756 isl_set_dump(scop->context);
757 isl_set_dump(scop->context_value);
758 for (i = 0; i < scop->n_array; ++i)
759 pet_array_dump(scop->arrays[i]);
760 for (i = 0; i < scop->n_stmt; ++i)
761 pet_stmt_dump(scop->stmts[i]);
764 /* Return 1 if the two pet_arrays are equivalent.
766 * We don't compare element_size as this may be target dependent.
768 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
770 if (!array1 || !array2)
771 return 0;
773 if (!isl_set_is_equal(array1->context, array2->context))
774 return 0;
775 if (!isl_set_is_equal(array1->extent, array2->extent))
776 return 0;
777 if (!!array1->value_bounds != !!array2->value_bounds)
778 return 0;
779 if (array1->value_bounds &&
780 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
781 return 0;
782 if (strcmp(array1->element_type, array2->element_type))
783 return 0;
784 if (array1->live_out != array2->live_out)
785 return 0;
787 return 1;
790 /* Return 1 if the two pet_stmts are equivalent.
792 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
794 int i;
796 if (!stmt1 || !stmt2)
797 return 0;
799 if (stmt1->line != stmt2->line)
800 return 0;
801 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
802 return 0;
803 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
804 return 0;
805 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
806 return 0;
807 if (stmt1->n_arg != stmt2->n_arg)
808 return 0;
809 for (i = 0; i < stmt1->n_arg; ++i) {
810 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
811 return 0;
814 return 1;
817 /* Return 1 if the two pet_scops are equivalent.
819 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
821 int i;
823 if (!scop1 || !scop2)
824 return 0;
826 if (!isl_set_is_equal(scop1->context, scop2->context))
827 return 0;
828 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
829 return 0;
831 if (scop1->n_array != scop2->n_array)
832 return 0;
833 for (i = 0; i < scop1->n_array; ++i)
834 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
835 return 0;
837 if (scop1->n_stmt != scop2->n_stmt)
838 return 0;
839 for (i = 0; i < scop1->n_stmt; ++i)
840 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
841 return 0;
843 return 1;
846 /* Prefix the schedule of "stmt" with an extra dimension with constant
847 * value "pos".
849 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
851 if (!stmt)
852 return NULL;
854 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
855 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
856 if (!stmt->schedule)
857 return pet_stmt_free(stmt);
859 return stmt;
862 /* Prefix the schedules of all statements in "scop" with an extra
863 * dimension with constant value "pos".
865 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
867 int i;
869 if (!scop)
870 return NULL;
872 for (i = 0; i < scop->n_stmt; ++i) {
873 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
874 if (!scop->stmts[i])
875 return pet_scop_free(scop);
878 return scop;
881 /* Data used in embed_access.
882 * extend adds an iterator to the iteration domain
883 * var_id represents the induction variable of the corresponding loop
885 struct pet_embed_access {
886 isl_map *extend;
887 isl_id *var_id;
890 /* Embed the access relation in an extra outer loop.
892 * We first update the iteration domain to insert the extra dimension.
894 * If the access refers to the induction variable, then it is
895 * turned into an access to the set of integers with index (and value)
896 * equal to the induction variable.
898 * If the induction variable appears in the constraints (as a parameter),
899 * then the parameter is equated to the newly introduced iteration
900 * domain dimension and subsequently projected out.
902 * Similarly, if the accessed array is a virtual array (with user
903 * pointer equal to NULL), as created by create_test_access,
904 * then it is extended along with the domain of the access.
906 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
907 void *user)
909 struct pet_embed_access *data = user;
910 isl_id *array_id = NULL;
911 int pos;
913 access = update_domain(access, data->extend);
915 if (isl_map_has_tuple_id(access, isl_dim_out))
916 array_id = isl_map_get_tuple_id(access, isl_dim_out);
917 if (array_id == data->var_id ||
918 (array_id && !isl_id_get_user(array_id))) {
919 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
920 access = isl_map_equate(access,
921 isl_dim_in, 0, isl_dim_out, 0);
922 if (array_id != data->var_id)
923 access = isl_map_set_tuple_id(access, isl_dim_out,
924 isl_id_copy(array_id));
926 isl_id_free(array_id);
928 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
929 if (pos >= 0) {
930 access = isl_map_equate(access,
931 isl_dim_param, pos, isl_dim_in, 0);
932 access = isl_map_project_out(access, isl_dim_param, pos, 1);
934 access = isl_map_set_dim_id(access, isl_dim_in, 0,
935 isl_id_copy(data->var_id));
937 return access;
940 /* Embed all access relations in "expr" in an extra loop.
941 * "extend" inserts an outer loop iterator in the iteration domains.
942 * "var_id" represents the induction variable.
944 static struct pet_expr *expr_embed(struct pet_expr *expr,
945 __isl_take isl_map *extend, __isl_keep isl_id *var_id)
947 struct pet_embed_access data = { .extend = extend, .var_id = var_id };
949 expr = pet_expr_foreach_access(expr, &embed_access, &data);
950 isl_map_free(extend);
951 return expr;
954 /* Embed the given pet_stmt in an extra outer loop with iteration domain
955 * "dom" and schedule "sched". "var_id" represents the induction variable
956 * of the loop.
958 * The iteration domain and schedule of the statement are updated
959 * according to the iteration domain and schedule of the new loop.
960 * If stmt->domain is a wrapped map, then the iteration domain
961 * is the domain of this map, so we need to be careful to adjust
962 * this domain.
964 * If the induction variable appears in the constraints (as a parameter)
965 * of the current iteration domain or the schedule of the statement,
966 * then the parameter is equated to the newly introduced iteration
967 * domain dimension and subsequently projected out.
969 * Finally, all access relations are updated based on the extra loop.
971 struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt, __isl_take isl_set *dom,
972 __isl_take isl_map *sched, __isl_take isl_id *var_id)
974 int i;
975 int pos;
976 isl_id *stmt_id;
977 isl_space *dim;
978 isl_map *extend;
980 if (!stmt)
981 goto error;
983 if (isl_set_is_wrapping(stmt->domain)) {
984 isl_map *map;
985 isl_map *ext;
986 isl_space *ran_dim;
988 map = isl_set_unwrap(stmt->domain);
989 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
990 ran_dim = isl_space_range(isl_map_get_space(map));
991 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
992 isl_set_universe(ran_dim));
993 map = isl_map_flat_domain_product(ext, map);
994 map = isl_map_set_tuple_id(map, isl_dim_in,
995 isl_id_copy(stmt_id));
996 dim = isl_space_domain(isl_map_get_space(map));
997 stmt->domain = isl_map_wrap(map);
998 } else {
999 stmt_id = isl_set_get_tuple_id(stmt->domain);
1000 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1001 stmt->domain);
1002 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1003 isl_id_copy(stmt_id));
1004 dim = isl_set_get_space(stmt->domain);
1007 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1008 if (pos >= 0) {
1009 stmt->domain = isl_set_equate(stmt->domain,
1010 isl_dim_param, pos, isl_dim_set, 0);
1011 stmt->domain = isl_set_project_out(stmt->domain,
1012 isl_dim_param, pos, 1);
1015 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1016 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1017 isl_dim_in, stmt_id);
1019 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1020 if (pos >= 0) {
1021 stmt->schedule = isl_map_equate(stmt->schedule,
1022 isl_dim_param, pos, isl_dim_in, 0);
1023 stmt->schedule = isl_map_project_out(stmt->schedule,
1024 isl_dim_param, pos, 1);
1027 dim = isl_space_map_from_set(dim);
1028 extend = isl_map_identity(dim);
1029 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1030 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1031 isl_map_get_tuple_id(extend, isl_dim_out));
1032 for (i = 0; i < stmt->n_arg; ++i)
1033 stmt->args[i] = expr_embed(stmt->args[i],
1034 isl_map_copy(extend), var_id);
1035 stmt->body = expr_embed(stmt->body, extend, var_id);
1037 isl_set_free(dom);
1038 isl_id_free(var_id);
1040 for (i = 0; i < stmt->n_arg; ++i)
1041 if (!stmt->args[i])
1042 return pet_stmt_free(stmt);
1043 if (!stmt->domain || !stmt->schedule || !stmt->body)
1044 return pet_stmt_free(stmt);
1045 return stmt;
1046 error:
1047 isl_set_free(dom);
1048 isl_map_free(sched);
1049 isl_id_free(var_id);
1050 return NULL;
1053 /* Embed the given pet_array in an extra outer loop with iteration domain
1054 * "dom".
1055 * This embedding only has an effect on virtual arrays (those with
1056 * user pointer equal to NULL), which need to be extended along with
1057 * the iteration domain.
1059 static struct pet_array *pet_array_embed(struct pet_array *array,
1060 __isl_take isl_set *dom)
1062 isl_id *array_id = NULL;
1064 if (!array)
1065 goto error;
1067 if (isl_set_has_tuple_id(array->extent))
1068 array_id = isl_set_get_tuple_id(array->extent);
1070 if (array_id && !isl_id_get_user(array_id)) {
1071 array->extent = isl_set_flat_product(dom, array->extent);
1072 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1073 } else {
1074 isl_set_free(dom);
1075 isl_id_free(array_id);
1078 return array;
1079 error:
1080 isl_set_free(dom);
1081 return NULL;
1084 /* Embed all statements and arrays in "scop" in an extra outer loop
1085 * with iteration domain "dom" and schedule "sched".
1086 * "var_id" represents the induction variable of the loop.
1088 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1089 __isl_take isl_map *sched, __isl_take isl_id *id)
1091 int i;
1093 if (!scop)
1094 goto error;
1096 for (i = 0; i < scop->n_stmt; ++i) {
1097 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1098 isl_set_copy(dom),
1099 isl_map_copy(sched), isl_id_copy(id));
1100 if (!scop->stmts[i])
1101 goto error;
1104 for (i = 0; i < scop->n_array; ++i) {
1105 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1106 isl_set_copy(dom));
1107 if (!scop->arrays[i])
1108 goto error;
1111 isl_set_free(dom);
1112 isl_map_free(sched);
1113 isl_id_free(id);
1114 return scop;
1115 error:
1116 isl_set_free(dom);
1117 isl_map_free(sched);
1118 isl_id_free(id);
1119 return pet_scop_free(scop);
1122 /* Add extra conditions on the parameters to iteration domain of "stmt".
1124 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1125 __isl_take isl_set *cond)
1127 if (!stmt)
1128 goto error;
1130 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1132 return stmt;
1133 error:
1134 isl_set_free(cond);
1135 return pet_stmt_free(stmt);
1138 /* Add extra conditions on the parameters to all iteration domains.
1140 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1141 __isl_take isl_set *cond)
1143 int i;
1145 if (!scop)
1146 goto error;
1148 for (i = 0; i < scop->n_stmt; ++i) {
1149 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1150 isl_set_copy(cond));
1151 if (!scop->stmts[i])
1152 goto error;
1155 isl_set_free(cond);
1156 return scop;
1157 error:
1158 isl_set_free(cond);
1159 return pet_scop_free(scop);
1162 /* Make the statements "stmt" depend on the value of "test"
1163 * being equal to "satisfied" by adjusting stmt->domain.
1165 * We insert an argument corresponding to a read to "test"
1166 * from the iteration domain of "stmt" in front of the list of arguments.
1167 * We also insert a corresponding output dimension in the wrapped
1168 * map contained in stmt->domain, with value set to "satisfied".
1170 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1171 __isl_take isl_map *test, int satisfied)
1173 int i;
1174 isl_id *id;
1175 isl_ctx *ctx;
1176 isl_map *map;
1177 isl_set *dom;
1179 if (!stmt || !test)
1180 goto error;
1182 if (isl_set_is_wrapping(stmt->domain))
1183 map = isl_set_unwrap(stmt->domain);
1184 else
1185 map = isl_map_from_domain(stmt->domain);
1186 map = isl_map_insert_dims(map, isl_dim_out, 0, 1);
1187 id = isl_map_get_tuple_id(test, isl_dim_out);
1188 map = isl_map_set_dim_id(map, isl_dim_out, 0, id);
1189 map = isl_map_fix_si(map, isl_dim_out, 0, satisfied);
1190 dom = isl_set_universe(isl_space_domain(isl_map_get_space(map)));
1191 test = isl_map_apply_domain(test, isl_map_from_range(dom));
1193 stmt->domain = isl_map_wrap(map);
1195 ctx = isl_map_get_ctx(test);
1196 if (!stmt->args) {
1197 stmt->args = isl_calloc_array(ctx, struct pet_expr *, 1);
1198 if (!stmt->args)
1199 goto error;
1200 } else {
1201 struct pet_expr **args;
1202 args = isl_calloc_array(ctx, struct pet_expr *, 1 + stmt->n_arg);
1203 if (!args)
1204 goto error;
1205 for (i = 0; i < stmt->n_arg; ++i)
1206 args[1 + i] = stmt->args[i];
1207 free(stmt->args);
1208 stmt->args = args;
1210 stmt->n_arg++;
1211 stmt->args[0] = pet_expr_from_access(isl_map_copy(test));
1212 if (!stmt->args[0])
1213 goto error;
1215 isl_map_free(test);
1216 return stmt;
1217 error:
1218 isl_map_free(test);
1219 return pet_stmt_free(stmt);
1222 /* Make all statements in "scop" depend on the value of "test"
1223 * being equal to "satisfied" by adjusting their domains.
1225 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
1226 __isl_take isl_map *test, int satisfied)
1228 int i;
1230 if (!scop)
1231 goto error;
1233 for (i = 0; i < scop->n_stmt; ++i) {
1234 scop->stmts[i] = stmt_filter(scop->stmts[i],
1235 isl_map_copy(test), satisfied);
1236 if (!scop->stmts[i])
1237 goto error;
1240 isl_map_free(test);
1241 return scop;
1242 error:
1243 isl_map_free(test);
1244 return pet_scop_free(scop);
1247 /* Add all parameters in "expr" to "dim" and return the result.
1249 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
1250 __isl_take isl_space *dim)
1252 int i;
1254 if (!expr)
1255 goto error;
1256 for (i = 0; i < expr->n_arg; ++i)
1258 dim = expr_collect_params(expr->args[i], dim);
1260 if (expr->type == pet_expr_access)
1261 dim = isl_space_align_params(dim,
1262 isl_map_get_space(expr->acc.access));
1264 return dim;
1265 error:
1266 isl_space_free(dim);
1267 return pet_expr_free(expr);
1270 /* Add all parameters in "stmt" to "dim" and return the result.
1272 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
1273 __isl_take isl_space *dim)
1275 if (!stmt)
1276 goto error;
1278 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
1279 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
1280 dim = expr_collect_params(stmt->body, dim);
1282 return dim;
1283 error:
1284 isl_space_free(dim);
1285 return pet_stmt_free(stmt);
1288 /* Add all parameters in "array" to "dim" and return the result.
1290 static __isl_give isl_space *array_collect_params(struct pet_array *array,
1291 __isl_take isl_space *dim)
1293 if (!array)
1294 goto error;
1296 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
1297 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
1299 return dim;
1300 error:
1301 isl_space_free(dim);
1302 return pet_array_free(array);
1305 /* Add all parameters in "scop" to "dim" and return the result.
1307 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
1308 __isl_take isl_space *dim)
1310 int i;
1312 if (!scop)
1313 goto error;
1315 for (i = 0; i < scop->n_array; ++i)
1316 dim = array_collect_params(scop->arrays[i], dim);
1318 for (i = 0; i < scop->n_stmt; ++i)
1319 dim = stmt_collect_params(scop->stmts[i], dim);
1321 return dim;
1322 error:
1323 isl_space_free(dim);
1324 return pet_scop_free(scop);
1327 /* Add all parameters in "dim" to all access relations in "expr".
1329 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
1330 __isl_take isl_space *dim)
1332 int i;
1334 if (!expr)
1335 goto error;
1337 for (i = 0; i < expr->n_arg; ++i) {
1338 expr->args[i] =
1339 expr_propagate_params(expr->args[i],
1340 isl_space_copy(dim));
1341 if (!expr->args[i])
1342 goto error;
1345 if (expr->type == pet_expr_access) {
1346 expr->acc.access = isl_map_align_params(expr->acc.access,
1347 isl_space_copy(dim));
1348 if (!expr->acc.access)
1349 goto error;
1352 isl_space_free(dim);
1353 return expr;
1354 error:
1355 isl_space_free(dim);
1356 return pet_expr_free(expr);
1359 /* Add all parameters in "dim" to the domain, schedule and
1360 * all access relations in "stmt".
1362 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
1363 __isl_take isl_space *dim)
1365 if (!stmt)
1366 goto error;
1368 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
1369 stmt->schedule = isl_map_align_params(stmt->schedule,
1370 isl_space_copy(dim));
1371 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
1373 if (!stmt->domain || !stmt->schedule || !stmt->body)
1374 goto error;
1376 isl_space_free(dim);
1377 return stmt;
1378 error:
1379 isl_space_free(dim);
1380 return pet_stmt_free(stmt);
1383 /* Add all parameters in "dim" to "array".
1385 static struct pet_array *array_propagate_params(struct pet_array *array,
1386 __isl_take isl_space *dim)
1388 if (!array)
1389 goto error;
1391 array->context = isl_set_align_params(array->context,
1392 isl_space_copy(dim));
1393 array->extent = isl_set_align_params(array->extent,
1394 isl_space_copy(dim));
1395 if (array->value_bounds) {
1396 array->value_bounds = isl_set_align_params(array->value_bounds,
1397 isl_space_copy(dim));
1398 if (!array->value_bounds)
1399 goto error;
1402 if (!array->context || !array->extent)
1403 goto error;
1405 isl_space_free(dim);
1406 return array;
1407 error:
1408 isl_space_free(dim);
1409 return pet_array_free(array);
1412 /* Add all parameters in "dim" to "scop".
1414 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
1415 __isl_take isl_space *dim)
1417 int i;
1419 if (!scop)
1420 goto error;
1422 for (i = 0; i < scop->n_array; ++i) {
1423 scop->arrays[i] = array_propagate_params(scop->arrays[i],
1424 isl_space_copy(dim));
1425 if (!scop->arrays[i])
1426 goto error;
1429 for (i = 0; i < scop->n_stmt; ++i) {
1430 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
1431 isl_space_copy(dim));
1432 if (!scop->stmts[i])
1433 goto error;
1436 isl_space_free(dim);
1437 return scop;
1438 error:
1439 isl_space_free(dim);
1440 return pet_scop_free(scop);
1443 /* Update all isl_sets and isl_maps in "scop" such that they all
1444 * have the same parameters.
1446 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
1448 isl_space *dim;
1450 if (!scop)
1451 return NULL;
1453 dim = isl_set_get_space(scop->context);
1454 dim = scop_collect_params(scop, dim);
1456 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
1457 scop = scop_propagate_params(scop, dim);
1459 return scop;
1462 /* Check if the given access relation accesses a (0D) array that corresponds
1463 * to one of the parameters in "dim". If so, replace the array access
1464 * by an access to the set of integers with as index (and value)
1465 * that parameter.
1467 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
1468 __isl_take isl_space *dim)
1470 isl_id *array_id = NULL;
1471 int pos = -1;
1473 if (isl_map_has_tuple_id(access, isl_dim_out)) {
1474 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1475 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
1477 isl_space_free(dim);
1479 if (pos < 0) {
1480 isl_id_free(array_id);
1481 return access;
1484 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
1485 if (pos < 0) {
1486 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
1487 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
1488 pos = 0;
1489 } else
1490 isl_id_free(array_id);
1492 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1493 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
1495 return access;
1498 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1499 * in "dim" by a value equal to the corresponding parameter.
1501 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
1502 __isl_take isl_space *dim)
1504 int i;
1506 if (!expr)
1507 goto error;
1509 for (i = 0; i < expr->n_arg; ++i) {
1510 expr->args[i] =
1511 expr_detect_parameter_accesses(expr->args[i],
1512 isl_space_copy(dim));
1513 if (!expr->args[i])
1514 goto error;
1517 if (expr->type == pet_expr_access) {
1518 expr->acc.access = access_detect_parameter(expr->acc.access,
1519 isl_space_copy(dim));
1520 if (!expr->acc.access)
1521 goto error;
1524 isl_space_free(dim);
1525 return expr;
1526 error:
1527 isl_space_free(dim);
1528 return pet_expr_free(expr);
1531 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1532 * in "dim" by a value equal to the corresponding parameter.
1534 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
1535 __isl_take isl_space *dim)
1537 if (!stmt)
1538 goto error;
1540 stmt->body = expr_detect_parameter_accesses(stmt->body,
1541 isl_space_copy(dim));
1543 if (!stmt->domain || !stmt->schedule || !stmt->body)
1544 goto error;
1546 isl_space_free(dim);
1547 return stmt;
1548 error:
1549 isl_space_free(dim);
1550 return pet_stmt_free(stmt);
1553 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1554 * in "dim" by a value equal to the corresponding parameter.
1556 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
1557 __isl_take isl_space *dim)
1559 int i;
1561 if (!scop)
1562 goto error;
1564 for (i = 0; i < scop->n_stmt; ++i) {
1565 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
1566 isl_space_copy(dim));
1567 if (!scop->stmts[i])
1568 goto error;
1571 isl_space_free(dim);
1572 return scop;
1573 error:
1574 isl_space_free(dim);
1575 return pet_scop_free(scop);
1578 /* Replace all accesses to (0D) arrays that correspond to any of
1579 * the parameters used in "scop" by a value equal
1580 * to the corresponding parameter.
1582 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
1584 isl_space *dim;
1586 if (!scop)
1587 return NULL;
1589 dim = isl_set_get_space(scop->context);
1590 dim = scop_collect_params(scop, dim);
1592 scop = scop_detect_parameter_accesses(scop, dim);
1594 return scop;
1597 /* Add all read access relations (if "read" is set) and/or all write
1598 * access relations (if "write" is set) to "accesses" and return the result.
1600 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
1601 int read, int write, __isl_take isl_union_map *accesses)
1603 int i;
1604 isl_id *id;
1605 isl_space *dim;
1607 if (!expr)
1608 return NULL;
1610 for (i = 0; i < expr->n_arg; ++i)
1611 accesses = expr_collect_accesses(expr->args[i],
1612 read, write, accesses);
1614 if (expr->type == pet_expr_access &&
1615 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
1616 ((read && expr->acc.read) || (write && expr->acc.write)))
1617 accesses = isl_union_map_add_map(accesses,
1618 isl_map_copy(expr->acc.access));
1620 return accesses;
1623 /* Collect and return all read access relations (if "read" is set)
1624 * and/or all write * access relations (if "write" is set) in "stmt".
1626 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
1627 int read, int write, __isl_take isl_space *dim)
1629 isl_union_map *accesses;
1631 if (!stmt)
1632 return NULL;
1634 accesses = isl_union_map_empty(dim);
1635 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
1636 accesses = isl_union_map_intersect_domain(accesses,
1637 isl_union_set_from_set(isl_set_copy(stmt->domain)));
1639 return accesses;
1642 /* Collect and return all read access relations (if "read" is set)
1643 * and/or all write * access relations (if "write" is set) in "scop".
1645 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
1646 int read, int write)
1648 int i;
1649 isl_union_map *accesses;
1651 if (!scop)
1652 return NULL;
1654 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
1656 for (i = 0; i < scop->n_stmt; ++i) {
1657 isl_union_map *accesses_i;
1658 isl_space *dim = isl_set_get_space(scop->context);
1659 accesses_i = stmt_collect_accesses(scop->stmts[i],
1660 read, write, dim);
1661 accesses = isl_union_map_union(accesses, accesses_i);
1664 return accesses;
1667 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
1669 return scop_collect_accesses(scop, 1, 0);
1672 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
1674 return scop_collect_accesses(scop, 0, 1);
1677 /* Collect and return the union of iteration domains in "scop".
1679 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
1681 int i;
1682 isl_set *domain_i;
1683 isl_union_set *domain;
1685 if (!scop)
1686 return NULL;
1688 domain = isl_union_set_empty(isl_set_get_space(scop->context));
1690 for (i = 0; i < scop->n_stmt; ++i) {
1691 domain_i = isl_set_copy(scop->stmts[i]->domain);
1692 domain = isl_union_set_add_set(domain, domain_i);
1695 return domain;
1698 /* Collect and return the schedules of the statements in "scop".
1699 * The range is normalized to the maximal number of scheduling
1700 * dimensions.
1702 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
1704 int i, j;
1705 isl_map *schedule_i;
1706 isl_union_map *schedule;
1707 int depth, max_depth = 0;
1709 if (!scop)
1710 return NULL;
1712 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
1714 for (i = 0; i < scop->n_stmt; ++i) {
1715 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
1716 if (depth > max_depth)
1717 max_depth = depth;
1720 for (i = 0; i < scop->n_stmt; ++i) {
1721 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
1722 depth = isl_map_dim(schedule_i, isl_dim_out);
1723 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
1724 max_depth - depth);
1725 for (j = depth; j < max_depth; ++j)
1726 schedule_i = isl_map_fix_si(schedule_i,
1727 isl_dim_out, j, 0);
1728 schedule = isl_union_map_add_map(schedule, schedule_i);
1731 return schedule;
1734 /* Does expression "expr" write to "id"?
1736 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
1738 int i;
1739 isl_id *write_id;
1741 for (i = 0; i < expr->n_arg; ++i) {
1742 int writes = expr_writes(expr->args[i], id);
1743 if (writes < 0 || writes)
1744 return writes;
1747 if (expr->type != pet_expr_access)
1748 return 0;
1749 if (!expr->acc.write)
1750 return 0;
1751 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
1752 return 0;
1754 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
1755 isl_id_free(write_id);
1757 if (!write_id)
1758 return -1;
1760 return write_id == id;
1763 /* Does statement "stmt" write to "id"?
1765 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
1767 return expr_writes(stmt->body, id);
1770 /* Is there any write access in "scop" that accesses "id"?
1772 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
1774 int i;
1776 if (!scop)
1777 return -1;
1779 for (i = 0; i < scop->n_stmt; ++i) {
1780 int writes = stmt_writes(scop->stmts[i], id);
1781 if (writes < 0 || writes)
1782 return writes;
1785 return 0;
1788 /* Reset the user pointer on all parameter ids in "set".
1790 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
1792 int i, n;
1794 n = isl_set_dim(set, isl_dim_param);
1795 for (i = 0; i < n; ++i) {
1796 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
1797 const char *name = isl_id_get_name(id);
1798 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
1799 isl_id_free(id);
1802 return set;
1805 /* Reset the user pointer on all parameter ids in "map".
1807 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
1809 int i, n;
1811 n = isl_map_dim(map, isl_dim_param);
1812 for (i = 0; i < n; ++i) {
1813 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
1814 const char *name = isl_id_get_name(id);
1815 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
1816 isl_id_free(id);
1819 return map;
1822 /* Reset the user pointer on all parameter ids in "array".
1824 static struct pet_array *array_anonymize(struct pet_array *array)
1826 if (!array)
1827 return NULL;
1829 array->context = set_anonymize(array->context);
1830 array->extent = set_anonymize(array->extent);
1831 if (!array->context || !array->extent)
1832 return pet_array_free(array);
1834 return array;
1837 /* Reset the user pointer on all parameter ids in "access".
1839 static __isl_give isl_map *access_anonymize(__isl_take isl_map *access,
1840 void *user)
1842 access = map_anonymize(access);
1844 return access;
1847 /* Reset the user pointer on all parameter ids in "stmt".
1849 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
1851 int i;
1852 isl_space *space;
1853 isl_set *domain;
1855 if (!stmt)
1856 return NULL;
1858 stmt->domain = set_anonymize(stmt->domain);
1859 stmt->schedule = map_anonymize(stmt->schedule);
1860 if (!stmt->domain || !stmt->schedule)
1861 return pet_stmt_free(stmt);
1863 for (i = 0; i < stmt->n_arg; ++i) {
1864 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
1865 &access_anonymize, NULL);
1866 if (!stmt->args[i])
1867 return pet_stmt_free(stmt);
1870 stmt->body = pet_expr_foreach_access(stmt->body,
1871 &access_anonymize, NULL);
1872 if (!stmt->body)
1873 return pet_stmt_free(stmt);
1875 return stmt;
1878 /* Reset the user pointer on all parameter ids in "scop".
1880 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
1882 int i;
1884 if (!scop)
1885 return NULL;
1887 scop->context = set_anonymize(scop->context);
1888 scop->context_value = set_anonymize(scop->context_value);
1889 if (!scop->context || !scop->context_value)
1890 return pet_scop_free(scop);
1892 for (i = 0; i < scop->n_array; ++i) {
1893 scop->arrays[i] = array_anonymize(scop->arrays[i]);
1894 if (!scop->arrays[i])
1895 return pet_scop_free(scop);
1898 for (i = 0; i < scop->n_stmt; ++i) {
1899 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
1900 if (!scop->stmts[i])
1901 return pet_scop_free(scop);
1904 return scop;
1907 /* Given a set "domain", return a wrapped relation with the given set
1908 * as domain and a range of dimension "n_arg", where each coordinate
1909 * is either unbounded or, if the corresponding element of args is of
1910 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
1912 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
1913 unsigned n_arg, struct pet_expr **args,
1914 __isl_keep isl_union_map *value_bounds)
1916 int i;
1917 isl_map *map;
1918 isl_space *space;
1919 isl_ctx *ctx = isl_set_get_ctx(domain);
1921 map = isl_map_from_domain(domain);
1922 space = isl_map_get_space(map);
1923 space = isl_space_add_dims(space, isl_dim_out, 1);
1925 for (i = 0; i < n_arg; ++i) {
1926 isl_map *map_i;
1927 struct pet_expr *arg = args[i];
1928 isl_id *id;
1929 isl_space *space2;
1931 map_i = isl_map_universe(isl_space_copy(space));
1932 if (arg->type == pet_expr_access) {
1933 isl_map *vb;
1934 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
1935 space2 = isl_space_alloc(ctx, 0, 0, 1);
1936 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
1937 vb = isl_union_map_extract_map(value_bounds, space2);
1938 if (!isl_map_plain_is_empty(vb))
1939 map_i = isl_map_intersect_range(map_i,
1940 isl_map_range(vb));
1941 else
1942 isl_map_free(vb);
1944 map = isl_map_flat_range_product(map, map_i);
1946 isl_space_free(space);
1948 return isl_map_wrap(map);
1951 /* Data used in access_gist() callback.
1953 struct pet_access_gist_data {
1954 isl_set *domain;
1955 isl_union_map *value_bounds;
1958 /* Given an expression "expr" of type pet_expr_access, compute
1959 * the gist of the associated access relation with respect to
1960 * data->domain and the bounds on the values of the arguments
1961 * of the expression.
1963 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
1965 struct pet_access_gist_data *data = user;
1966 isl_set *domain;
1968 domain = isl_set_copy(data->domain);
1969 if (expr->n_arg > 0)
1970 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
1971 data->value_bounds);
1973 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
1974 if (!expr->acc.access)
1975 return pet_expr_free(expr);
1977 return expr;
1980 /* Compute the gist of the iteration domain and all access relations
1981 * of "stmt" based on the constraints on the parameters specified by "context"
1982 * and the constraints on the values of nested accesses specified
1983 * by "value_bounds".
1985 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
1986 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
1988 int i;
1989 isl_space *space;
1990 isl_set *domain;
1991 struct pet_access_gist_data data;
1993 if (!stmt)
1994 return NULL;
1996 data.domain = isl_set_copy(stmt->domain);
1997 data.value_bounds = value_bounds;
1998 if (stmt->n_arg > 0)
1999 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
2001 data.domain = isl_set_intersect_params(data.domain,
2002 isl_set_copy(context));
2004 for (i = 0; i < stmt->n_arg; ++i) {
2005 stmt->args[i] = pet_expr_foreach_access_expr(stmt->args[i],
2006 &access_gist, &data);
2007 if (!stmt->args[i])
2008 goto error;
2011 stmt->body = pet_expr_foreach_access_expr(stmt->body,
2012 &access_gist, &data);
2013 if (!stmt->body)
2014 goto error;
2016 isl_set_free(data.domain);
2018 space = isl_set_get_space(stmt->domain);
2019 if (isl_space_is_wrapping(space))
2020 space = isl_space_domain(isl_space_unwrap(space));
2021 domain = isl_set_universe(space);
2022 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2023 if (stmt->n_arg > 0)
2024 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
2025 value_bounds);
2026 stmt->domain = isl_set_gist(stmt->domain, domain);
2027 if (!stmt->domain)
2028 return pet_stmt_free(stmt);
2030 return stmt;
2031 error:
2032 isl_set_free(data.domain);
2033 return pet_stmt_free(stmt);
2036 /* Compute the gist of the extent of the array
2037 * based on the constraints on the parameters specified by "context".
2039 static struct pet_array *array_gist(struct pet_array *array,
2040 __isl_keep isl_set *context)
2042 if (!array)
2043 return NULL;
2045 array->extent = isl_set_gist_params(array->extent,
2046 isl_set_copy(context));
2047 if (!array->extent)
2048 return pet_array_free(array);
2050 return array;
2053 /* Compute the gist of all sets and relations in "scop"
2054 * based on the constraints on the parameters specified by "scop->context"
2055 * and the constraints on the values of nested accesses specified
2056 * by "value_bounds".
2058 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
2059 __isl_keep isl_union_map *value_bounds)
2061 int i;
2063 if (!scop)
2064 return NULL;
2066 scop->context = isl_set_coalesce(scop->context);
2067 if (!scop->context)
2068 return pet_scop_free(scop);
2070 for (i = 0; i < scop->n_array; ++i) {
2071 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
2072 if (!scop->arrays[i])
2073 return pet_scop_free(scop);
2076 for (i = 0; i < scop->n_stmt; ++i) {
2077 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
2078 value_bounds);
2079 if (!scop->stmts[i])
2080 return pet_scop_free(scop);
2083 return scop;