PetScan::extract_for: rename variable "dim" to "space"
[pet.git] / scop.c
blob4ae2b49fd83b1b19b777a845c939e87246a35de8
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_post_inc] = "++",
67 [pet_op_post_dec] = "--",
68 [pet_op_pre_inc] = "++",
69 [pet_op_pre_dec] = "--",
70 [pet_op_address_of] = "&"
73 const char *pet_op_str(enum pet_op_type op)
75 return op_str[op];
78 int pet_op_is_inc_dec(enum pet_op_type op)
80 return op == pet_op_post_inc || op == pet_op_post_dec ||
81 op == pet_op_pre_inc || op == pet_op_pre_dec;
84 const char *pet_type_str(enum pet_expr_type type)
86 return type_str[type];
89 enum pet_op_type pet_str_op(const char *str)
91 int i;
93 for (i = 0; i < ARRAY_SIZE(op_str); ++i)
94 if (!strcmp(op_str[i], str))
95 return i;
97 return -1;
100 enum pet_expr_type pet_str_type(const char *str)
102 int i;
104 for (i = 0; i < ARRAY_SIZE(type_str); ++i)
105 if (!strcmp(type_str[i], str))
106 return i;
108 return -1;
111 /* Construct a pet_expr from an access relation.
112 * By default, it is considered to be a read access.
114 struct pet_expr *pet_expr_from_access(__isl_take isl_map *access)
116 isl_ctx *ctx = isl_map_get_ctx(access);
117 struct pet_expr *expr;
119 if (!access)
120 return NULL;
121 expr = isl_calloc_type(ctx, struct pet_expr);
122 if (!expr)
123 goto error;
125 expr->type = pet_expr_access;
126 expr->acc.access = access;
127 expr->acc.read = 1;
128 expr->acc.write = 0;
130 return expr;
131 error:
132 isl_map_free(access);
133 return NULL;
136 /* Construct a unary pet_expr that performs "op" on "arg".
138 struct pet_expr *pet_expr_new_unary(isl_ctx *ctx, enum pet_op_type op,
139 struct pet_expr *arg)
141 struct pet_expr *expr;
143 if (!arg)
144 goto error;
145 expr = isl_alloc_type(ctx, struct pet_expr);
146 if (!expr)
147 goto error;
149 expr->type = pet_expr_unary;
150 expr->op = op;
151 expr->n_arg = 1;
152 expr->args = isl_calloc_array(ctx, struct pet_expr *, 1);
153 if (!expr->args)
154 goto error;
155 expr->args[pet_un_arg] = arg;
157 return expr;
158 error:
159 pet_expr_free(arg);
160 return NULL;
163 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
165 struct pet_expr *pet_expr_new_binary(isl_ctx *ctx, enum pet_op_type op,
166 struct pet_expr *lhs, struct pet_expr *rhs)
168 struct pet_expr *expr;
170 if (!lhs || !rhs)
171 goto error;
172 expr = isl_alloc_type(ctx, struct pet_expr);
173 if (!expr)
174 goto error;
176 expr->type = pet_expr_binary;
177 expr->op = op;
178 expr->n_arg = 2;
179 expr->args = isl_calloc_array(ctx, struct pet_expr *, 2);
180 if (!expr->args)
181 goto error;
182 expr->args[pet_bin_lhs] = lhs;
183 expr->args[pet_bin_rhs] = rhs;
185 return expr;
186 error:
187 pet_expr_free(lhs);
188 pet_expr_free(rhs);
189 return NULL;
192 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
194 struct pet_expr *pet_expr_new_ternary(isl_ctx *ctx, struct pet_expr *cond,
195 struct pet_expr *lhs, struct pet_expr *rhs)
197 struct pet_expr *expr;
199 if (!cond || !lhs || !rhs)
200 goto error;
201 expr = isl_alloc_type(ctx, struct pet_expr);
202 if (!expr)
203 goto error;
205 expr->type = pet_expr_ternary;
206 expr->n_arg = 3;
207 expr->args = isl_calloc_array(ctx, struct pet_expr *, 3);
208 if (!expr->args)
209 goto error;
210 expr->args[pet_ter_cond] = cond;
211 expr->args[pet_ter_true] = lhs;
212 expr->args[pet_ter_false] = rhs;
214 return expr;
215 error:
216 pet_expr_free(cond);
217 pet_expr_free(lhs);
218 pet_expr_free(rhs);
219 return NULL;
222 /* Construct a call pet_expr that calls function "name" with "n_arg"
223 * arguments. The caller is responsible for filling in the arguments.
225 struct pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
226 unsigned n_arg)
228 struct pet_expr *expr;
230 expr = isl_alloc_type(ctx, struct pet_expr);
231 if (!expr)
232 return NULL;
234 expr->type = pet_expr_call;
235 expr->n_arg = n_arg;
236 expr->name = strdup(name);
237 expr->args = isl_calloc_array(ctx, struct pet_expr *, n_arg);
238 if (!expr->name || !expr->args)
239 return pet_expr_free(expr);
241 return expr;
244 /* Construct a pet_expr that represents the double "d".
246 struct pet_expr *pet_expr_new_double(isl_ctx *ctx, double d)
248 struct pet_expr *expr;
250 expr = isl_calloc_type(ctx, struct pet_expr);
251 if (!expr)
252 return NULL;
254 expr->type = pet_expr_double;
255 expr->d = d;
257 return expr;
260 void *pet_expr_free(struct pet_expr *expr)
262 int i;
264 if (!expr)
265 return NULL;
267 for (i = 0; i < expr->n_arg; ++i)
268 pet_expr_free(expr->args[i]);
269 free(expr->args);
271 switch (expr->type) {
272 case pet_expr_access:
273 isl_map_free(expr->acc.access);
274 break;
275 case pet_expr_call:
276 free(expr->name);
277 break;
278 case pet_expr_double:
279 case pet_expr_unary:
280 case pet_expr_binary:
281 case pet_expr_ternary:
282 break;
285 free(expr);
286 return NULL;
289 static void expr_dump(struct pet_expr *expr, int indent)
291 int i;
293 if (!expr)
294 return;
296 fprintf(stderr, "%*s", indent, "");
298 switch (expr->type) {
299 case pet_expr_double:
300 fprintf(stderr, "%g\n", expr->d);
301 break;
302 case pet_expr_access:
303 isl_map_dump(expr->acc.access);
304 fprintf(stderr, "%*sread: %d\n", indent + 2,
305 "", expr->acc.read);
306 fprintf(stderr, "%*swrite: %d\n", indent + 2,
307 "", expr->acc.write);
308 for (i = 0; i < expr->n_arg; ++i)
309 expr_dump(expr->args[i], indent + 2);
310 break;
311 case pet_expr_unary:
312 fprintf(stderr, "%s\n", op_str[expr->op]);
313 expr_dump(expr->args[pet_un_arg], indent + 2);
314 break;
315 case pet_expr_binary:
316 fprintf(stderr, "%s\n", op_str[expr->op]);
317 expr_dump(expr->args[pet_bin_lhs], indent + 2);
318 expr_dump(expr->args[pet_bin_rhs], indent + 2);
319 break;
320 case pet_expr_ternary:
321 fprintf(stderr, "?:\n");
322 expr_dump(expr->args[pet_ter_cond], indent + 2);
323 expr_dump(expr->args[pet_ter_true], indent + 2);
324 expr_dump(expr->args[pet_ter_false], indent + 2);
325 break;
326 case pet_expr_call:
327 fprintf(stderr, "%s/%d\n", expr->name, expr->n_arg);
328 for (i = 0; i < expr->n_arg; ++i)
329 expr_dump(expr->args[i], indent + 2);
330 break;
334 void pet_expr_dump(struct pet_expr *expr)
336 expr_dump(expr, 0);
339 /* Does "expr" represent an access to an unnamed space, i.e.,
340 * does it represent an affine expression?
342 int pet_expr_is_affine(struct pet_expr *expr)
344 int has_id;
346 if (!expr)
347 return -1;
348 if (expr->type != pet_expr_access)
349 return 0;
351 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
352 if (has_id < 0)
353 return -1;
355 return !has_id;
358 /* Return 1 if the two pet_exprs are equivalent.
360 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
362 int i;
364 if (!expr1 || !expr2)
365 return 0;
367 if (expr1->type != expr2->type)
368 return 0;
369 if (expr1->n_arg != expr2->n_arg)
370 return 0;
371 for (i = 0; i < expr1->n_arg; ++i)
372 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
373 return 0;
374 switch (expr1->type) {
375 case pet_expr_double:
376 if (expr1->d != expr2->d)
377 return 0;
378 break;
379 case pet_expr_access:
380 if (expr1->acc.read != expr2->acc.read)
381 return 0;
382 if (expr1->acc.write != expr2->acc.write)
383 return 0;
384 if (!expr1->acc.access || !expr2->acc.access)
385 return 0;
386 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
387 return 0;
388 break;
389 case pet_expr_unary:
390 case pet_expr_binary:
391 case pet_expr_ternary:
392 if (expr1->op != expr2->op)
393 return 0;
394 break;
395 case pet_expr_call:
396 if (strcmp(expr1->name, expr2->name))
397 return 0;
398 break;
401 return 1;
404 /* Add extra conditions on the parameters to all access relations in "expr".
406 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
407 __isl_take isl_set *cond)
409 int i;
411 if (!expr)
412 goto error;
414 for (i = 0; i < expr->n_arg; ++i) {
415 expr->args[i] = pet_expr_restrict(expr->args[i],
416 isl_set_copy(cond));
417 if (!expr->args[i])
418 goto error;
421 if (expr->type == pet_expr_access) {
422 expr->acc.access = isl_map_intersect_params(expr->acc.access,
423 isl_set_copy(cond));
424 if (!expr->acc.access)
425 goto error;
428 isl_set_free(cond);
429 return expr;
430 error:
431 isl_set_free(cond);
432 return pet_expr_free(expr);
435 /* Modify all access relations in "expr" by calling "fn" on them.
437 struct pet_expr *pet_expr_foreach_access(struct pet_expr *expr,
438 __isl_give isl_map *(*fn)(__isl_take isl_map *access, void *user),
439 void *user)
441 int i;
443 if (!expr)
444 return NULL;
446 for (i = 0; i < expr->n_arg; ++i) {
447 expr->args[i] = pet_expr_foreach_access(expr->args[i], fn, user);
448 if (!expr->args[i])
449 return pet_expr_free(expr);
452 if (expr->type == pet_expr_access) {
453 expr->acc.access = fn(expr->acc.access, user);
454 if (!expr->acc.access)
455 return pet_expr_free(expr);
458 return expr;
461 /* Modify all expressions of type pet_expr_access in "expr"
462 * by calling "fn" on them.
464 struct pet_expr *pet_expr_foreach_access_expr(struct pet_expr *expr,
465 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
466 void *user)
468 int i;
470 if (!expr)
471 return NULL;
473 for (i = 0; i < expr->n_arg; ++i) {
474 expr->args[i] = pet_expr_foreach_access_expr(expr->args[i],
475 fn, user);
476 if (!expr->args[i])
477 return pet_expr_free(expr);
480 if (expr->type == pet_expr_access)
481 expr = fn(expr, user);
483 return expr;
486 /* Modify the given access relation based on the given iteration space
487 * transformation.
488 * If the access has any arguments then the domain of the access relation
489 * is a wrapped mapping from the iteration space to the space of
490 * argument values. We only need to change the domain of this wrapped
491 * mapping, so we extend the input transformation with an identity mapping
492 * on the space of argument values.
494 static __isl_give isl_map *update_domain(__isl_take isl_map *access,
495 void *user)
497 isl_map *update = user;
498 isl_space *dim;
500 update = isl_map_copy(update);
502 dim = isl_map_get_space(access);
503 dim = isl_space_domain(dim);
504 if (!isl_space_is_wrapping(dim))
505 isl_space_free(dim);
506 else {
507 isl_map *id;
508 dim = isl_space_unwrap(dim);
509 dim = isl_space_range(dim);
510 dim = isl_space_map_from_set(dim);
511 id = isl_map_identity(dim);
512 update = isl_map_product(update, id);
515 return isl_map_apply_domain(access, update);
518 /* Modify all access relations in "expr" based on the given iteration space
519 * transformation.
521 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
522 __isl_take isl_map *update)
524 expr = pet_expr_foreach_access(expr, &update_domain, update);
525 isl_map_free(update);
526 return expr;
529 /* Construct a pet_stmt with given line number and statement
530 * number from a pet_expr.
531 * The initial iteration domain is the zero-dimensional universe.
532 * The name of the domain is given by "label" if it is non-NULL.
533 * Otherwise, the name is constructed as S_<id>.
534 * The domains of all access relations are modified to refer
535 * to the statement iteration domain.
537 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
538 __isl_take isl_id *label, int id, struct pet_expr *expr)
540 struct pet_stmt *stmt;
541 isl_space *dim;
542 isl_set *dom;
543 isl_map *sched;
544 isl_map *add_name;
545 char name[50];
547 if (!expr)
548 goto error;
550 stmt = isl_calloc_type(ctx, struct pet_stmt);
551 if (!stmt)
552 goto error;
554 dim = isl_space_set_alloc(ctx, 0, 0);
555 if (label)
556 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
557 else {
558 snprintf(name, sizeof(name), "S_%d", id);
559 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
561 dom = isl_set_universe(isl_space_copy(dim));
562 sched = isl_map_from_domain(isl_set_copy(dom));
564 dim = isl_space_from_range(dim);
565 add_name = isl_map_universe(dim);
566 expr = expr_update_domain(expr, add_name);
568 stmt->line = line;
569 stmt->domain = dom;
570 stmt->schedule = sched;
571 stmt->body = expr;
573 if (!stmt->domain || !stmt->schedule || !stmt->body)
574 return pet_stmt_free(stmt);
576 return stmt;
577 error:
578 isl_id_free(label);
579 return pet_expr_free(expr);
582 void *pet_stmt_free(struct pet_stmt *stmt)
584 int i;
586 if (!stmt)
587 return NULL;
589 isl_set_free(stmt->domain);
590 isl_map_free(stmt->schedule);
591 pet_expr_free(stmt->body);
593 for (i = 0; i < stmt->n_arg; ++i)
594 pet_expr_free(stmt->args[i]);
595 free(stmt->args);
597 free(stmt);
598 return NULL;
601 static void stmt_dump(struct pet_stmt *stmt, int indent)
603 int i;
605 if (!stmt)
606 return;
608 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
609 fprintf(stderr, "%*s", indent, "");
610 isl_set_dump(stmt->domain);
611 fprintf(stderr, "%*s", indent, "");
612 isl_map_dump(stmt->schedule);
613 expr_dump(stmt->body, indent);
614 for (i = 0; i < stmt->n_arg; ++i)
615 expr_dump(stmt->args[i], indent + 2);
618 void pet_stmt_dump(struct pet_stmt *stmt)
620 stmt_dump(stmt, 0);
623 void *pet_array_free(struct pet_array *array)
625 if (!array)
626 return NULL;
628 isl_set_free(array->context);
629 isl_set_free(array->extent);
630 isl_set_free(array->value_bounds);
631 free(array->element_type);
633 free(array);
634 return NULL;
637 void pet_array_dump(struct pet_array *array)
639 if (!array)
640 return;
642 isl_set_dump(array->context);
643 isl_set_dump(array->extent);
644 isl_set_dump(array->value_bounds);
645 fprintf(stderr, "%s %s\n", array->element_type,
646 array->live_out ? "live-out" : "");
649 /* Construct a pet_scop with room for n statements.
651 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
653 isl_space *space;
654 struct pet_scop *scop;
656 scop = isl_calloc_type(ctx, struct pet_scop);
657 if (!scop)
658 return NULL;
660 space = isl_space_params_alloc(ctx, 0);
661 scop->context = isl_set_universe(isl_space_copy(space));
662 scop->context_value = isl_set_universe(space);
663 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
664 if (!scop->context || !scop->stmts)
665 return pet_scop_free(scop);
667 scop->n_stmt = n;
669 return scop;
672 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
674 return scop_alloc(ctx, 0);
677 /* Update "context" with respect to the valid parameter values for "access".
679 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
680 __isl_take isl_set *context)
682 context = isl_set_intersect(context,
683 isl_map_params(isl_map_copy(access)));
684 return context;
687 /* Update "context" with respect to the valid parameter values for "expr".
689 * If "expr" represents a ternary operator, then a parameter value
690 * needs to be valid for the condition and for at least one of the
691 * remaining two arguments.
692 * If the condition is an affine expression, then we can be a bit more specific.
693 * The parameter then has to be valid for the second argument for
694 * non-zero accesses and valid for the third argument for zero accesses.
696 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
697 __isl_take isl_set *context)
699 int i;
701 if (expr->type == pet_expr_ternary) {
702 int is_aff;
703 isl_set *context1, *context2;
705 is_aff = pet_expr_is_affine(expr->args[0]);
706 if (is_aff < 0)
707 goto error;
709 context = expr_extract_context(expr->args[0], context);
710 context1 = expr_extract_context(expr->args[1],
711 isl_set_copy(context));
712 context2 = expr_extract_context(expr->args[2], context);
714 if (is_aff) {
715 isl_map *access;
716 isl_set *zero_set;
718 access = isl_map_copy(expr->args[0]->acc.access);
719 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
720 zero_set = isl_map_params(access);
721 context1 = isl_set_subtract(context1,
722 isl_set_copy(zero_set));
723 context2 = isl_set_intersect(context2, zero_set);
726 context = isl_set_union(context1, context2);
727 context = isl_set_coalesce(context);
729 return context;
732 for (i = 0; i < expr->n_arg; ++i)
733 context = expr_extract_context(expr->args[i], context);
735 if (expr->type == pet_expr_access)
736 context = access_extract_context(expr->acc.access, context);
738 return context;
739 error:
740 isl_set_free(context);
741 return NULL;
744 /* Update "context" with respect to the valid parameter values for "stmt".
746 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
747 __isl_take isl_set *context)
749 int i;
751 for (i = 0; i < stmt->n_arg; ++i)
752 context = expr_extract_context(stmt->args[i], context);
754 context = expr_extract_context(stmt->body, context);
756 return context;
759 /* Construct a pet_scop that contains the given pet_stmt.
761 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
763 struct pet_scop *scop;
765 if (!stmt)
766 return NULL;
768 scop = scop_alloc(ctx, 1);
770 scop->context = stmt_extract_context(stmt, scop->context);
771 if (!scop->context)
772 goto error;
774 scop->stmts[0] = stmt;
776 return scop;
777 error:
778 pet_stmt_free(stmt);
779 pet_scop_free(scop);
780 return NULL;
783 /* Construct a pet_scop that contains the arrays and the statements
784 * in "scop1" and "scop2".
786 struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
787 struct pet_scop *scop2)
789 int i;
790 struct pet_scop *scop;
792 if (!scop1 || !scop2)
793 goto error;
795 if (scop1->n_stmt == 0) {
796 pet_scop_free(scop1);
797 return scop2;
800 if (scop2->n_stmt == 0) {
801 pet_scop_free(scop2);
802 return scop1;
805 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
806 if (!scop)
807 goto error;
809 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
810 scop1->n_array + scop2->n_array);
811 if (!scop->arrays)
812 goto error;
813 scop->n_array = scop1->n_array + scop2->n_array;
815 for (i = 0; i < scop1->n_stmt; ++i) {
816 scop->stmts[i] = scop1->stmts[i];
817 scop1->stmts[i] = NULL;
820 for (i = 0; i < scop2->n_stmt; ++i) {
821 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
822 scop2->stmts[i] = NULL;
825 for (i = 0; i < scop1->n_array; ++i) {
826 scop->arrays[i] = scop1->arrays[i];
827 scop1->arrays[i] = NULL;
830 for (i = 0; i < scop2->n_array; ++i) {
831 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
832 scop2->arrays[i] = NULL;
835 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
836 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
838 pet_scop_free(scop1);
839 pet_scop_free(scop2);
840 return scop;
841 error:
842 pet_scop_free(scop1);
843 pet_scop_free(scop2);
844 return NULL;
847 void *pet_scop_free(struct pet_scop *scop)
849 int i;
851 if (!scop)
852 return NULL;
853 isl_set_free(scop->context);
854 isl_set_free(scop->context_value);
855 if (scop->arrays)
856 for (i = 0; i < scop->n_array; ++i)
857 pet_array_free(scop->arrays[i]);
858 free(scop->arrays);
859 if (scop->stmts)
860 for (i = 0; i < scop->n_stmt; ++i)
861 pet_stmt_free(scop->stmts[i]);
862 free(scop->stmts);
863 free(scop);
864 return NULL;
867 void pet_scop_dump(struct pet_scop *scop)
869 int i;
871 if (!scop)
872 return;
874 isl_set_dump(scop->context);
875 isl_set_dump(scop->context_value);
876 for (i = 0; i < scop->n_array; ++i)
877 pet_array_dump(scop->arrays[i]);
878 for (i = 0; i < scop->n_stmt; ++i)
879 pet_stmt_dump(scop->stmts[i]);
882 /* Return 1 if the two pet_arrays are equivalent.
884 * We don't compare element_size as this may be target dependent.
886 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
888 if (!array1 || !array2)
889 return 0;
891 if (!isl_set_is_equal(array1->context, array2->context))
892 return 0;
893 if (!isl_set_is_equal(array1->extent, array2->extent))
894 return 0;
895 if (!!array1->value_bounds != !!array2->value_bounds)
896 return 0;
897 if (array1->value_bounds &&
898 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
899 return 0;
900 if (strcmp(array1->element_type, array2->element_type))
901 return 0;
902 if (array1->live_out != array2->live_out)
903 return 0;
904 if (array1->uniquely_defined != array2->uniquely_defined)
905 return 0;
907 return 1;
910 /* Return 1 if the two pet_stmts are equivalent.
912 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
914 int i;
916 if (!stmt1 || !stmt2)
917 return 0;
919 if (stmt1->line != stmt2->line)
920 return 0;
921 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
922 return 0;
923 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
924 return 0;
925 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
926 return 0;
927 if (stmt1->n_arg != stmt2->n_arg)
928 return 0;
929 for (i = 0; i < stmt1->n_arg; ++i) {
930 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
931 return 0;
934 return 1;
937 /* Return 1 if the two pet_scops are equivalent.
939 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
941 int i;
943 if (!scop1 || !scop2)
944 return 0;
946 if (!isl_set_is_equal(scop1->context, scop2->context))
947 return 0;
948 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
949 return 0;
951 if (scop1->n_array != scop2->n_array)
952 return 0;
953 for (i = 0; i < scop1->n_array; ++i)
954 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
955 return 0;
957 if (scop1->n_stmt != scop2->n_stmt)
958 return 0;
959 for (i = 0; i < scop1->n_stmt; ++i)
960 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
961 return 0;
963 return 1;
966 /* Prefix the schedule of "stmt" with an extra dimension with constant
967 * value "pos".
969 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
971 if (!stmt)
972 return NULL;
974 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
975 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
976 if (!stmt->schedule)
977 return pet_stmt_free(stmt);
979 return stmt;
982 /* Prefix the schedules of all statements in "scop" with an extra
983 * dimension with constant value "pos".
985 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
987 int i;
989 if (!scop)
990 return NULL;
992 for (i = 0; i < scop->n_stmt; ++i) {
993 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
994 if (!scop->stmts[i])
995 return pet_scop_free(scop);
998 return scop;
1001 /* Given a set with a parameter at "param_pos" that refers to the
1002 * iterator, "move" the iterator to the first set dimension.
1003 * That is, equate the parameter to the first set dimension and then
1004 * project it out.
1006 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1007 int param_pos)
1009 set = isl_set_equate(set, isl_dim_param, param_pos, isl_dim_set, 0);
1010 set = isl_set_project_out(set, isl_dim_param, param_pos, 1);
1011 return set;
1014 /* Data used in embed_access.
1015 * extend adds an iterator to the iteration domain
1016 * var_id represents the induction variable of the corresponding loop
1018 struct pet_embed_access {
1019 isl_map *extend;
1020 isl_id *var_id;
1023 /* Embed the access relation in an extra outer loop.
1025 * We first update the iteration domain to insert the extra dimension.
1027 * If the access refers to the induction variable, then it is
1028 * turned into an access to the set of integers with index (and value)
1029 * equal to the induction variable.
1031 * If the induction variable appears in the constraints (as a parameter),
1032 * then the parameter is equated to the newly introduced iteration
1033 * domain dimension and subsequently projected out.
1035 * Similarly, if the accessed array is a virtual array (with user
1036 * pointer equal to NULL), as created by create_test_access,
1037 * then it is extended along with the domain of the access.
1039 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
1040 void *user)
1042 struct pet_embed_access *data = user;
1043 isl_id *array_id = NULL;
1044 int pos;
1046 access = update_domain(access, data->extend);
1048 if (isl_map_has_tuple_id(access, isl_dim_out))
1049 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1050 if (array_id == data->var_id ||
1051 (array_id && !isl_id_get_user(array_id))) {
1052 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1053 access = isl_map_equate(access,
1054 isl_dim_in, 0, isl_dim_out, 0);
1055 if (array_id != data->var_id)
1056 access = isl_map_set_tuple_id(access, isl_dim_out,
1057 isl_id_copy(array_id));
1059 isl_id_free(array_id);
1061 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1062 if (pos >= 0) {
1063 isl_set *set = isl_map_wrap(access);
1064 set = internalize_iv(set, pos);
1065 access = isl_set_unwrap(set);
1067 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1068 isl_id_copy(data->var_id));
1070 return access;
1073 /* Embed all access relations in "expr" in an extra loop.
1074 * "extend" inserts an outer loop iterator in the iteration domains.
1075 * "var_id" represents the induction variable.
1077 static struct pet_expr *expr_embed(struct pet_expr *expr,
1078 __isl_take isl_map *extend, __isl_keep isl_id *var_id)
1080 struct pet_embed_access data = { .extend = extend, .var_id = var_id };
1082 expr = pet_expr_foreach_access(expr, &embed_access, &data);
1083 isl_map_free(extend);
1084 return expr;
1087 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1088 * "dom" and schedule "sched". "var_id" represents the induction variable
1089 * of the loop.
1091 * The iteration domain and schedule of the statement are updated
1092 * according to the iteration domain and schedule of the new loop.
1093 * If stmt->domain is a wrapped map, then the iteration domain
1094 * is the domain of this map, so we need to be careful to adjust
1095 * this domain.
1097 * If the induction variable appears in the constraints (as a parameter)
1098 * of the current iteration domain or the schedule of the statement,
1099 * then the parameter is equated to the newly introduced iteration
1100 * domain dimension and subsequently projected out.
1102 * Finally, all access relations are updated based on the extra loop.
1104 struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt, __isl_take isl_set *dom,
1105 __isl_take isl_map *sched, __isl_take isl_id *var_id)
1107 int i;
1108 int pos;
1109 isl_id *stmt_id;
1110 isl_space *dim;
1111 isl_map *extend;
1113 if (!stmt)
1114 goto error;
1116 if (isl_set_is_wrapping(stmt->domain)) {
1117 isl_map *map;
1118 isl_map *ext;
1119 isl_space *ran_dim;
1121 map = isl_set_unwrap(stmt->domain);
1122 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1123 ran_dim = isl_space_range(isl_map_get_space(map));
1124 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1125 isl_set_universe(ran_dim));
1126 map = isl_map_flat_domain_product(ext, map);
1127 map = isl_map_set_tuple_id(map, isl_dim_in,
1128 isl_id_copy(stmt_id));
1129 dim = isl_space_domain(isl_map_get_space(map));
1130 stmt->domain = isl_map_wrap(map);
1131 } else {
1132 stmt_id = isl_set_get_tuple_id(stmt->domain);
1133 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1134 stmt->domain);
1135 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1136 isl_id_copy(stmt_id));
1137 dim = isl_set_get_space(stmt->domain);
1140 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1141 if (pos >= 0)
1142 stmt->domain = internalize_iv(stmt->domain, pos);
1144 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1145 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1146 isl_dim_in, stmt_id);
1148 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1149 if (pos >= 0) {
1150 isl_set *set = isl_map_wrap(stmt->schedule);
1151 set = internalize_iv(set, pos);
1152 stmt->schedule = isl_set_unwrap(set);
1155 dim = isl_space_map_from_set(dim);
1156 extend = isl_map_identity(dim);
1157 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1158 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1159 isl_map_get_tuple_id(extend, isl_dim_out));
1160 for (i = 0; i < stmt->n_arg; ++i)
1161 stmt->args[i] = expr_embed(stmt->args[i],
1162 isl_map_copy(extend), var_id);
1163 stmt->body = expr_embed(stmt->body, extend, var_id);
1165 isl_set_free(dom);
1166 isl_id_free(var_id);
1168 for (i = 0; i < stmt->n_arg; ++i)
1169 if (!stmt->args[i])
1170 return pet_stmt_free(stmt);
1171 if (!stmt->domain || !stmt->schedule || !stmt->body)
1172 return pet_stmt_free(stmt);
1173 return stmt;
1174 error:
1175 isl_set_free(dom);
1176 isl_map_free(sched);
1177 isl_id_free(var_id);
1178 return NULL;
1181 /* Embed the given pet_array in an extra outer loop with iteration domain
1182 * "dom".
1183 * This embedding only has an effect on virtual arrays (those with
1184 * user pointer equal to NULL), which need to be extended along with
1185 * the iteration domain.
1187 static struct pet_array *pet_array_embed(struct pet_array *array,
1188 __isl_take isl_set *dom)
1190 isl_id *array_id = NULL;
1192 if (!array)
1193 goto error;
1195 if (isl_set_has_tuple_id(array->extent))
1196 array_id = isl_set_get_tuple_id(array->extent);
1198 if (array_id && !isl_id_get_user(array_id)) {
1199 array->extent = isl_set_flat_product(dom, array->extent);
1200 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1201 } else {
1202 isl_set_free(dom);
1203 isl_id_free(array_id);
1206 return array;
1207 error:
1208 isl_set_free(dom);
1209 return NULL;
1212 /* Project out all unnamed parameters from "set" and return the result.
1214 static __isl_give isl_set *set_project_out_unnamed_params(
1215 __isl_take isl_set *set)
1217 int i, n;
1219 n = isl_set_dim(set, isl_dim_param);
1220 for (i = n - 1; i >= 0; --i) {
1221 if (isl_set_has_dim_name(set, isl_dim_param, i))
1222 continue;
1223 set = isl_set_project_out(set, isl_dim_param, i, 1);
1226 return set;
1229 /* Update the context with respect to an embedding into a loop
1230 * with iteration domain "dom" and induction variable "id".
1232 * If the current context is independent of "id", we don't need
1233 * to do anything.
1234 * Otherwise, a parameter value is invalid for the embedding if
1235 * any of the corresponding iterator values is invalid.
1236 * That is, a parameter value is valid only if all the corresponding
1237 * iterator values are valid.
1238 * We therefore compute the set of parameters
1240 * forall i in dom : valid (i)
1242 * or
1244 * not exists i in dom : not valid(i)
1246 * i.e.,
1248 * not exists i in dom \ valid(i)
1250 * If there are any unnamed parameters in "dom", then we consider
1251 * a parameter value to be valid if it is valid for any value of those
1252 * unnamed parameters. They are therefore projected out at the end.
1254 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1255 __isl_keep isl_set *dom, __isl_keep isl_id *id)
1257 int pos;
1259 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1260 if (pos < 0)
1261 return context;
1263 context = isl_set_from_params(context);
1264 context = isl_set_add_dims(context, isl_dim_set, 1);
1265 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1266 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1267 context = isl_set_subtract(isl_set_copy(dom), context);
1268 context = isl_set_params(context);
1269 context = isl_set_complement(context);
1270 context = set_project_out_unnamed_params(context);
1271 return context;
1274 /* Embed all statements and arrays in "scop" in an extra outer loop
1275 * with iteration domain "dom" and schedule "sched".
1276 * "id" represents the induction variable of the loop.
1278 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1279 __isl_take isl_map *sched, __isl_take isl_id *id)
1281 int i;
1283 if (!scop)
1284 goto error;
1286 scop->context = context_embed(scop->context, dom, id);
1287 if (!scop->context)
1288 goto error;
1290 for (i = 0; i < scop->n_stmt; ++i) {
1291 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1292 isl_set_copy(dom),
1293 isl_map_copy(sched), isl_id_copy(id));
1294 if (!scop->stmts[i])
1295 goto error;
1298 for (i = 0; i < scop->n_array; ++i) {
1299 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1300 isl_set_copy(dom));
1301 if (!scop->arrays[i])
1302 goto error;
1305 isl_set_free(dom);
1306 isl_map_free(sched);
1307 isl_id_free(id);
1308 return scop;
1309 error:
1310 isl_set_free(dom);
1311 isl_map_free(sched);
1312 isl_id_free(id);
1313 return pet_scop_free(scop);
1316 /* Add extra conditions on the parameters to iteration domain of "stmt".
1318 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1319 __isl_take isl_set *cond)
1321 if (!stmt)
1322 goto error;
1324 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1326 return stmt;
1327 error:
1328 isl_set_free(cond);
1329 return pet_stmt_free(stmt);
1332 /* Add extra conditions on the parameters to all iteration domains.
1334 * A parameter value is valid for the result if it was valid
1335 * for the original scop and satisfies "cond" or if it does
1336 * not satisfy "cond" as in this case the scop is not executed
1337 * and the original constraints on the parameters are irrelevant.
1339 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1340 __isl_take isl_set *cond)
1342 int i;
1344 if (!scop)
1345 goto error;
1347 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1348 scop->context = isl_set_union(scop->context,
1349 isl_set_complement(isl_set_copy(cond)));
1350 scop->context = isl_set_coalesce(scop->context);
1351 scop->context = set_project_out_unnamed_params(scop->context);
1352 if (!scop->context)
1353 goto error;
1355 for (i = 0; i < scop->n_stmt; ++i) {
1356 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1357 isl_set_copy(cond));
1358 if (!scop->stmts[i])
1359 goto error;
1362 isl_set_free(cond);
1363 return scop;
1364 error:
1365 isl_set_free(cond);
1366 return pet_scop_free(scop);
1369 /* Make the statement "stmt" depend on the value of "test"
1370 * being equal to "satisfied" by adjusting stmt->domain.
1372 * The domain of "test" corresponds to the (zero or more) outer dimensions
1373 * of the iteration domain.
1375 * We insert an argument corresponding to a read to "test"
1376 * from the iteration domain of "stmt" in front of the list of arguments.
1377 * We also insert a corresponding output dimension in the wrapped
1378 * map contained in stmt->domain, with value set to "satisfied".
1380 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1381 __isl_take isl_map *test, int satisfied)
1383 int i;
1384 isl_id *id;
1385 isl_ctx *ctx;
1386 isl_map *map, *add_dom;
1387 isl_set *dom;
1388 int n_test_dom;
1390 if (!stmt || !test)
1391 goto error;
1393 if (isl_set_is_wrapping(stmt->domain))
1394 map = isl_set_unwrap(stmt->domain);
1395 else
1396 map = isl_map_from_domain(stmt->domain);
1397 map = isl_map_insert_dims(map, isl_dim_out, 0, 1);
1398 id = isl_map_get_tuple_id(test, isl_dim_out);
1399 map = isl_map_set_dim_id(map, isl_dim_out, 0, id);
1400 map = isl_map_fix_si(map, isl_dim_out, 0, satisfied);
1401 dom = isl_set_universe(isl_space_domain(isl_map_get_space(map)));
1402 n_test_dom = isl_map_dim(test, isl_dim_in);
1403 add_dom = isl_map_from_range(dom);
1404 add_dom = isl_map_add_dims(add_dom, isl_dim_in, n_test_dom);
1405 for (i = 0; i < n_test_dom; ++i)
1406 add_dom = isl_map_equate(add_dom, isl_dim_in, i,
1407 isl_dim_out, i);
1408 test = isl_map_apply_domain(test, add_dom);
1410 stmt->domain = isl_map_wrap(map);
1412 ctx = isl_map_get_ctx(test);
1413 if (!stmt->args) {
1414 stmt->args = isl_calloc_array(ctx, struct pet_expr *, 1);
1415 if (!stmt->args)
1416 goto error;
1417 } else {
1418 struct pet_expr **args;
1419 args = isl_calloc_array(ctx, struct pet_expr *, 1 + stmt->n_arg);
1420 if (!args)
1421 goto error;
1422 for (i = 0; i < stmt->n_arg; ++i)
1423 args[1 + i] = stmt->args[i];
1424 free(stmt->args);
1425 stmt->args = args;
1427 stmt->n_arg++;
1428 stmt->args[0] = pet_expr_from_access(isl_map_copy(test));
1429 if (!stmt->args[0])
1430 goto error;
1432 isl_map_free(test);
1433 return stmt;
1434 error:
1435 isl_map_free(test);
1436 return pet_stmt_free(stmt);
1439 /* Make all statements in "scop" depend on the value of "test"
1440 * being equal to "satisfied" by adjusting their domains.
1442 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
1443 __isl_take isl_map *test, int satisfied)
1445 int i;
1447 if (!scop)
1448 goto error;
1450 for (i = 0; i < scop->n_stmt; ++i) {
1451 scop->stmts[i] = stmt_filter(scop->stmts[i],
1452 isl_map_copy(test), satisfied);
1453 if (!scop->stmts[i])
1454 goto error;
1457 isl_map_free(test);
1458 return scop;
1459 error:
1460 isl_map_free(test);
1461 return pet_scop_free(scop);
1464 /* Add all parameters in "expr" to "dim" and return the result.
1466 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
1467 __isl_take isl_space *dim)
1469 int i;
1471 if (!expr)
1472 goto error;
1473 for (i = 0; i < expr->n_arg; ++i)
1475 dim = expr_collect_params(expr->args[i], dim);
1477 if (expr->type == pet_expr_access)
1478 dim = isl_space_align_params(dim,
1479 isl_map_get_space(expr->acc.access));
1481 return dim;
1482 error:
1483 isl_space_free(dim);
1484 return pet_expr_free(expr);
1487 /* Add all parameters in "stmt" to "dim" and return the result.
1489 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
1490 __isl_take isl_space *dim)
1492 if (!stmt)
1493 goto error;
1495 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
1496 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
1497 dim = expr_collect_params(stmt->body, dim);
1499 return dim;
1500 error:
1501 isl_space_free(dim);
1502 return pet_stmt_free(stmt);
1505 /* Add all parameters in "array" to "dim" and return the result.
1507 static __isl_give isl_space *array_collect_params(struct pet_array *array,
1508 __isl_take isl_space *dim)
1510 if (!array)
1511 goto error;
1513 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
1514 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
1516 return dim;
1517 error:
1518 isl_space_free(dim);
1519 return pet_array_free(array);
1522 /* Add all parameters in "scop" to "dim" and return the result.
1524 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
1525 __isl_take isl_space *dim)
1527 int i;
1529 if (!scop)
1530 goto error;
1532 for (i = 0; i < scop->n_array; ++i)
1533 dim = array_collect_params(scop->arrays[i], dim);
1535 for (i = 0; i < scop->n_stmt; ++i)
1536 dim = stmt_collect_params(scop->stmts[i], dim);
1538 return dim;
1539 error:
1540 isl_space_free(dim);
1541 return pet_scop_free(scop);
1544 /* Add all parameters in "dim" to all access relations in "expr".
1546 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
1547 __isl_take isl_space *dim)
1549 int i;
1551 if (!expr)
1552 goto error;
1554 for (i = 0; i < expr->n_arg; ++i) {
1555 expr->args[i] =
1556 expr_propagate_params(expr->args[i],
1557 isl_space_copy(dim));
1558 if (!expr->args[i])
1559 goto error;
1562 if (expr->type == pet_expr_access) {
1563 expr->acc.access = isl_map_align_params(expr->acc.access,
1564 isl_space_copy(dim));
1565 if (!expr->acc.access)
1566 goto error;
1569 isl_space_free(dim);
1570 return expr;
1571 error:
1572 isl_space_free(dim);
1573 return pet_expr_free(expr);
1576 /* Add all parameters in "dim" to the domain, schedule and
1577 * all access relations in "stmt".
1579 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
1580 __isl_take isl_space *dim)
1582 if (!stmt)
1583 goto error;
1585 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
1586 stmt->schedule = isl_map_align_params(stmt->schedule,
1587 isl_space_copy(dim));
1588 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
1590 if (!stmt->domain || !stmt->schedule || !stmt->body)
1591 goto error;
1593 isl_space_free(dim);
1594 return stmt;
1595 error:
1596 isl_space_free(dim);
1597 return pet_stmt_free(stmt);
1600 /* Add all parameters in "dim" to "array".
1602 static struct pet_array *array_propagate_params(struct pet_array *array,
1603 __isl_take isl_space *dim)
1605 if (!array)
1606 goto error;
1608 array->context = isl_set_align_params(array->context,
1609 isl_space_copy(dim));
1610 array->extent = isl_set_align_params(array->extent,
1611 isl_space_copy(dim));
1612 if (array->value_bounds) {
1613 array->value_bounds = isl_set_align_params(array->value_bounds,
1614 isl_space_copy(dim));
1615 if (!array->value_bounds)
1616 goto error;
1619 if (!array->context || !array->extent)
1620 goto error;
1622 isl_space_free(dim);
1623 return array;
1624 error:
1625 isl_space_free(dim);
1626 return pet_array_free(array);
1629 /* Add all parameters in "dim" to "scop".
1631 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
1632 __isl_take isl_space *dim)
1634 int i;
1636 if (!scop)
1637 goto error;
1639 for (i = 0; i < scop->n_array; ++i) {
1640 scop->arrays[i] = array_propagate_params(scop->arrays[i],
1641 isl_space_copy(dim));
1642 if (!scop->arrays[i])
1643 goto error;
1646 for (i = 0; i < scop->n_stmt; ++i) {
1647 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
1648 isl_space_copy(dim));
1649 if (!scop->stmts[i])
1650 goto error;
1653 isl_space_free(dim);
1654 return scop;
1655 error:
1656 isl_space_free(dim);
1657 return pet_scop_free(scop);
1660 /* Update all isl_sets and isl_maps in "scop" such that they all
1661 * have the same parameters.
1663 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
1665 isl_space *dim;
1667 if (!scop)
1668 return NULL;
1670 dim = isl_set_get_space(scop->context);
1671 dim = scop_collect_params(scop, dim);
1673 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
1674 scop = scop_propagate_params(scop, dim);
1676 return scop;
1679 /* Check if the given access relation accesses a (0D) array that corresponds
1680 * to one of the parameters in "dim". If so, replace the array access
1681 * by an access to the set of integers with as index (and value)
1682 * that parameter.
1684 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
1685 __isl_take isl_space *dim)
1687 isl_id *array_id = NULL;
1688 int pos = -1;
1690 if (isl_map_has_tuple_id(access, isl_dim_out)) {
1691 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1692 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
1694 isl_space_free(dim);
1696 if (pos < 0) {
1697 isl_id_free(array_id);
1698 return access;
1701 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
1702 if (pos < 0) {
1703 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
1704 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
1705 pos = 0;
1706 } else
1707 isl_id_free(array_id);
1709 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1710 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
1712 return access;
1715 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1716 * in "dim" by a value equal to the corresponding parameter.
1718 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
1719 __isl_take isl_space *dim)
1721 int i;
1723 if (!expr)
1724 goto error;
1726 for (i = 0; i < expr->n_arg; ++i) {
1727 expr->args[i] =
1728 expr_detect_parameter_accesses(expr->args[i],
1729 isl_space_copy(dim));
1730 if (!expr->args[i])
1731 goto error;
1734 if (expr->type == pet_expr_access) {
1735 expr->acc.access = access_detect_parameter(expr->acc.access,
1736 isl_space_copy(dim));
1737 if (!expr->acc.access)
1738 goto error;
1741 isl_space_free(dim);
1742 return expr;
1743 error:
1744 isl_space_free(dim);
1745 return pet_expr_free(expr);
1748 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1749 * in "dim" by a value equal to the corresponding parameter.
1751 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
1752 __isl_take isl_space *dim)
1754 if (!stmt)
1755 goto error;
1757 stmt->body = expr_detect_parameter_accesses(stmt->body,
1758 isl_space_copy(dim));
1760 if (!stmt->domain || !stmt->schedule || !stmt->body)
1761 goto error;
1763 isl_space_free(dim);
1764 return stmt;
1765 error:
1766 isl_space_free(dim);
1767 return pet_stmt_free(stmt);
1770 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1771 * in "dim" by a value equal to the corresponding parameter.
1773 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
1774 __isl_take isl_space *dim)
1776 int i;
1778 if (!scop)
1779 goto error;
1781 for (i = 0; i < scop->n_stmt; ++i) {
1782 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
1783 isl_space_copy(dim));
1784 if (!scop->stmts[i])
1785 goto error;
1788 isl_space_free(dim);
1789 return scop;
1790 error:
1791 isl_space_free(dim);
1792 return pet_scop_free(scop);
1795 /* Replace all accesses to (0D) arrays that correspond to any of
1796 * the parameters used in "scop" by a value equal
1797 * to the corresponding parameter.
1799 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
1801 isl_space *dim;
1803 if (!scop)
1804 return NULL;
1806 dim = isl_set_get_space(scop->context);
1807 dim = scop_collect_params(scop, dim);
1809 scop = scop_detect_parameter_accesses(scop, dim);
1811 return scop;
1814 /* Add all read access relations (if "read" is set) and/or all write
1815 * access relations (if "write" is set) to "accesses" and return the result.
1817 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
1818 int read, int write, __isl_take isl_union_map *accesses)
1820 int i;
1821 isl_id *id;
1822 isl_space *dim;
1824 if (!expr)
1825 return NULL;
1827 for (i = 0; i < expr->n_arg; ++i)
1828 accesses = expr_collect_accesses(expr->args[i],
1829 read, write, accesses);
1831 if (expr->type == pet_expr_access &&
1832 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
1833 ((read && expr->acc.read) || (write && expr->acc.write)))
1834 accesses = isl_union_map_add_map(accesses,
1835 isl_map_copy(expr->acc.access));
1837 return accesses;
1840 /* Collect and return all read access relations (if "read" is set)
1841 * and/or all write * access relations (if "write" is set) in "stmt".
1843 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
1844 int read, int write, __isl_take isl_space *dim)
1846 isl_union_map *accesses;
1848 if (!stmt)
1849 return NULL;
1851 accesses = isl_union_map_empty(dim);
1852 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
1853 accesses = isl_union_map_intersect_domain(accesses,
1854 isl_union_set_from_set(isl_set_copy(stmt->domain)));
1856 return accesses;
1859 /* Collect and return all read access relations (if "read" is set)
1860 * and/or all write * access relations (if "write" is set) in "scop".
1862 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
1863 int read, int write)
1865 int i;
1866 isl_union_map *accesses;
1868 if (!scop)
1869 return NULL;
1871 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
1873 for (i = 0; i < scop->n_stmt; ++i) {
1874 isl_union_map *accesses_i;
1875 isl_space *dim = isl_set_get_space(scop->context);
1876 accesses_i = stmt_collect_accesses(scop->stmts[i],
1877 read, write, dim);
1878 accesses = isl_union_map_union(accesses, accesses_i);
1881 return accesses;
1884 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
1886 return scop_collect_accesses(scop, 1, 0);
1889 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
1891 return scop_collect_accesses(scop, 0, 1);
1894 /* Collect and return the union of iteration domains in "scop".
1896 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
1898 int i;
1899 isl_set *domain_i;
1900 isl_union_set *domain;
1902 if (!scop)
1903 return NULL;
1905 domain = isl_union_set_empty(isl_set_get_space(scop->context));
1907 for (i = 0; i < scop->n_stmt; ++i) {
1908 domain_i = isl_set_copy(scop->stmts[i]->domain);
1909 domain = isl_union_set_add_set(domain, domain_i);
1912 return domain;
1915 /* Collect and return the schedules of the statements in "scop".
1916 * The range is normalized to the maximal number of scheduling
1917 * dimensions.
1919 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
1921 int i, j;
1922 isl_map *schedule_i;
1923 isl_union_map *schedule;
1924 int depth, max_depth = 0;
1926 if (!scop)
1927 return NULL;
1929 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
1931 for (i = 0; i < scop->n_stmt; ++i) {
1932 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
1933 if (depth > max_depth)
1934 max_depth = depth;
1937 for (i = 0; i < scop->n_stmt; ++i) {
1938 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
1939 depth = isl_map_dim(schedule_i, isl_dim_out);
1940 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
1941 max_depth - depth);
1942 for (j = depth; j < max_depth; ++j)
1943 schedule_i = isl_map_fix_si(schedule_i,
1944 isl_dim_out, j, 0);
1945 schedule = isl_union_map_add_map(schedule, schedule_i);
1948 return schedule;
1951 /* Does expression "expr" write to "id"?
1953 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
1955 int i;
1956 isl_id *write_id;
1958 for (i = 0; i < expr->n_arg; ++i) {
1959 int writes = expr_writes(expr->args[i], id);
1960 if (writes < 0 || writes)
1961 return writes;
1964 if (expr->type != pet_expr_access)
1965 return 0;
1966 if (!expr->acc.write)
1967 return 0;
1968 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
1969 return 0;
1971 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
1972 isl_id_free(write_id);
1974 if (!write_id)
1975 return -1;
1977 return write_id == id;
1980 /* Does statement "stmt" write to "id"?
1982 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
1984 return expr_writes(stmt->body, id);
1987 /* Is there any write access in "scop" that accesses "id"?
1989 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
1991 int i;
1993 if (!scop)
1994 return -1;
1996 for (i = 0; i < scop->n_stmt; ++i) {
1997 int writes = stmt_writes(scop->stmts[i], id);
1998 if (writes < 0 || writes)
1999 return writes;
2002 return 0;
2005 /* Reset the user pointer on all parameter ids in "set".
2007 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
2009 int i, n;
2011 n = isl_set_dim(set, isl_dim_param);
2012 for (i = 0; i < n; ++i) {
2013 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2014 const char *name = isl_id_get_name(id);
2015 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
2016 isl_id_free(id);
2019 return set;
2022 /* Reset the user pointer on all parameter ids in "map".
2024 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
2026 int i, n;
2028 n = isl_map_dim(map, isl_dim_param);
2029 for (i = 0; i < n; ++i) {
2030 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2031 const char *name = isl_id_get_name(id);
2032 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2033 isl_id_free(id);
2036 return map;
2039 /* Reset the user pointer on all parameter ids in "array".
2041 static struct pet_array *array_anonymize(struct pet_array *array)
2043 if (!array)
2044 return NULL;
2046 array->context = set_anonymize(array->context);
2047 array->extent = set_anonymize(array->extent);
2048 if (!array->context || !array->extent)
2049 return pet_array_free(array);
2051 return array;
2054 /* Reset the user pointer on all parameter ids in "access".
2056 static __isl_give isl_map *access_anonymize(__isl_take isl_map *access,
2057 void *user)
2059 access = map_anonymize(access);
2061 return access;
2064 /* Reset the user pointer on all parameter ids in "stmt".
2066 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2068 int i;
2069 isl_space *space;
2070 isl_set *domain;
2072 if (!stmt)
2073 return NULL;
2075 stmt->domain = set_anonymize(stmt->domain);
2076 stmt->schedule = map_anonymize(stmt->schedule);
2077 if (!stmt->domain || !stmt->schedule)
2078 return pet_stmt_free(stmt);
2080 for (i = 0; i < stmt->n_arg; ++i) {
2081 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2082 &access_anonymize, NULL);
2083 if (!stmt->args[i])
2084 return pet_stmt_free(stmt);
2087 stmt->body = pet_expr_foreach_access(stmt->body,
2088 &access_anonymize, NULL);
2089 if (!stmt->body)
2090 return pet_stmt_free(stmt);
2092 return stmt;
2095 /* Reset the user pointer on all parameter ids in "scop".
2097 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2099 int i;
2101 if (!scop)
2102 return NULL;
2104 scop->context = set_anonymize(scop->context);
2105 scop->context_value = set_anonymize(scop->context_value);
2106 if (!scop->context || !scop->context_value)
2107 return pet_scop_free(scop);
2109 for (i = 0; i < scop->n_array; ++i) {
2110 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2111 if (!scop->arrays[i])
2112 return pet_scop_free(scop);
2115 for (i = 0; i < scop->n_stmt; ++i) {
2116 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2117 if (!scop->stmts[i])
2118 return pet_scop_free(scop);
2121 return scop;
2124 /* Given a set "domain", return a wrapped relation with the given set
2125 * as domain and a range of dimension "n_arg", where each coordinate
2126 * is either unbounded or, if the corresponding element of args is of
2127 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2129 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2130 unsigned n_arg, struct pet_expr **args,
2131 __isl_keep isl_union_map *value_bounds)
2133 int i;
2134 isl_map *map;
2135 isl_space *space;
2136 isl_ctx *ctx = isl_set_get_ctx(domain);
2138 map = isl_map_from_domain(domain);
2139 space = isl_map_get_space(map);
2140 space = isl_space_add_dims(space, isl_dim_out, 1);
2142 for (i = 0; i < n_arg; ++i) {
2143 isl_map *map_i;
2144 struct pet_expr *arg = args[i];
2145 isl_id *id;
2146 isl_space *space2;
2148 map_i = isl_map_universe(isl_space_copy(space));
2149 if (arg->type == pet_expr_access) {
2150 isl_map *vb;
2151 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2152 space2 = isl_space_alloc(ctx, 0, 0, 1);
2153 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2154 vb = isl_union_map_extract_map(value_bounds, space2);
2155 if (!isl_map_plain_is_empty(vb))
2156 map_i = isl_map_intersect_range(map_i,
2157 isl_map_range(vb));
2158 else
2159 isl_map_free(vb);
2161 map = isl_map_flat_range_product(map, map_i);
2163 isl_space_free(space);
2165 return isl_map_wrap(map);
2168 /* Data used in access_gist() callback.
2170 struct pet_access_gist_data {
2171 isl_set *domain;
2172 isl_union_map *value_bounds;
2175 /* Given an expression "expr" of type pet_expr_access, compute
2176 * the gist of the associated access relation with respect to
2177 * data->domain and the bounds on the values of the arguments
2178 * of the expression.
2180 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
2182 struct pet_access_gist_data *data = user;
2183 isl_set *domain;
2185 domain = isl_set_copy(data->domain);
2186 if (expr->n_arg > 0)
2187 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
2188 data->value_bounds);
2190 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
2191 if (!expr->acc.access)
2192 return pet_expr_free(expr);
2194 return expr;
2197 /* Compute the gist of the iteration domain and all access relations
2198 * of "stmt" based on the constraints on the parameters specified by "context"
2199 * and the constraints on the values of nested accesses specified
2200 * by "value_bounds".
2202 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2203 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2205 int i;
2206 isl_space *space;
2207 isl_set *domain;
2208 struct pet_access_gist_data data;
2210 if (!stmt)
2211 return NULL;
2213 data.domain = isl_set_copy(stmt->domain);
2214 data.value_bounds = value_bounds;
2215 if (stmt->n_arg > 0)
2216 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
2218 data.domain = isl_set_intersect_params(data.domain,
2219 isl_set_copy(context));
2221 for (i = 0; i < stmt->n_arg; ++i) {
2222 stmt->args[i] = pet_expr_foreach_access_expr(stmt->args[i],
2223 &access_gist, &data);
2224 if (!stmt->args[i])
2225 goto error;
2228 stmt->body = pet_expr_foreach_access_expr(stmt->body,
2229 &access_gist, &data);
2230 if (!stmt->body)
2231 goto error;
2233 isl_set_free(data.domain);
2235 space = isl_set_get_space(stmt->domain);
2236 if (isl_space_is_wrapping(space))
2237 space = isl_space_domain(isl_space_unwrap(space));
2238 domain = isl_set_universe(space);
2239 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2240 if (stmt->n_arg > 0)
2241 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
2242 value_bounds);
2243 stmt->domain = isl_set_gist(stmt->domain, domain);
2244 if (!stmt->domain)
2245 return pet_stmt_free(stmt);
2247 return stmt;
2248 error:
2249 isl_set_free(data.domain);
2250 return pet_stmt_free(stmt);
2253 /* Compute the gist of the extent of the array
2254 * based on the constraints on the parameters specified by "context".
2256 static struct pet_array *array_gist(struct pet_array *array,
2257 __isl_keep isl_set *context)
2259 if (!array)
2260 return NULL;
2262 array->extent = isl_set_gist_params(array->extent,
2263 isl_set_copy(context));
2264 if (!array->extent)
2265 return pet_array_free(array);
2267 return array;
2270 /* Compute the gist of all sets and relations in "scop"
2271 * based on the constraints on the parameters specified by "scop->context"
2272 * and the constraints on the values of nested accesses specified
2273 * by "value_bounds".
2275 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
2276 __isl_keep isl_union_map *value_bounds)
2278 int i;
2280 if (!scop)
2281 return NULL;
2283 scop->context = isl_set_coalesce(scop->context);
2284 if (!scop->context)
2285 return pet_scop_free(scop);
2287 for (i = 0; i < scop->n_array; ++i) {
2288 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
2289 if (!scop->arrays[i])
2290 return pet_scop_free(scop);
2293 for (i = 0; i < scop->n_stmt; ++i) {
2294 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
2295 value_bounds);
2296 if (!scop->stmts[i])
2297 return pet_scop_free(scop);
2300 return scop;
2303 /* Intersect the context of "scop" with "context".
2304 * To ensure that we don't introduce any unnamed parameters in
2305 * the context of "scop", we first remove the unnamed parameters
2306 * from "context".
2308 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
2309 __isl_take isl_set *context)
2311 if (!scop)
2312 goto error;
2314 context = set_project_out_unnamed_params(context);
2315 scop->context = isl_set_intersect(scop->context, context);
2316 if (!scop->context)
2317 return pet_scop_free(scop);
2319 return scop;
2320 error:
2321 isl_set_free(context);
2322 return pet_scop_free(scop);
2325 /* Drop the current context of "scop". That is, replace the context
2326 * by a universal set.
2328 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
2330 isl_space *space;
2332 if (!scop)
2333 return NULL;
2335 space = isl_set_get_space(scop->context);
2336 isl_set_free(scop->context);
2337 scop->context = isl_set_universe(space);
2338 if (!scop->context)
2339 return pet_scop_free(scop);
2341 return scop;