pet_scop_from_pet_stmt: extract context constraints from statement expressions
[pet.git] / scop.c
blob6653f794f93dd27593dac4219b7063e92da4051a
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 /* Update "context" with respect to the valid parameter values for "access".
650 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
651 __isl_take isl_set *context)
653 context = isl_set_intersect(context,
654 isl_map_params(isl_map_copy(access)));
655 return context;
658 /* Update "context" with respect to the valid parameter values for "expr".
660 * If "expr" represents a ternary operator, then a parameter value
661 * needs to be valid for the condition and for at least one of the
662 * remaining two arguments.
663 * If the condition is an access, then we can be a bit more specific.
664 * The parameter then has to be valid for the second argument for
665 * non-zero accesses and valid for the third argument for zero accesses.
667 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
668 __isl_take isl_set *context)
670 int i;
672 if (expr->type == pet_expr_ternary) {
673 isl_set *context1, *context2;
675 context = expr_extract_context(expr->args[0], context);
676 context1 = expr_extract_context(expr->args[1],
677 isl_set_copy(context));
678 context2 = expr_extract_context(expr->args[2], context);
680 if (expr->args[0]->type == pet_expr_access &&
681 expr->args[0]->n_arg == 0) {
682 isl_map *access;
683 isl_set *zero_set;
685 access = isl_map_copy(expr->args[0]->acc.access);
686 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
687 zero_set = isl_map_params(access);
688 context1 = isl_set_subtract(context1,
689 isl_set_copy(zero_set));
690 context2 = isl_set_intersect(context2, zero_set);
693 context = isl_set_union(context1, context2);
694 context = isl_set_coalesce(context);
696 return context;
699 for (i = 0; i < expr->n_arg; ++i)
700 context = expr_extract_context(expr->args[i], context);
702 if (expr->type == pet_expr_access)
703 context = access_extract_context(expr->acc.access, context);
705 return context;
708 /* Update "context" with respect to the valid parameter values for "stmt".
710 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
711 __isl_take isl_set *context)
713 int i;
715 for (i = 0; i < stmt->n_arg; ++i)
716 context = expr_extract_context(stmt->args[i], context);
718 context = expr_extract_context(stmt->body, context);
720 return context;
723 /* Construct a pet_scop that contains the given pet_stmt.
725 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
727 struct pet_scop *scop;
729 if (!stmt)
730 return NULL;
732 scop = scop_alloc(ctx, 1);
734 scop->context = stmt_extract_context(stmt, scop->context);
735 if (!scop->context)
736 goto error;
738 scop->stmts[0] = stmt;
740 return scop;
741 error:
742 pet_stmt_free(stmt);
743 pet_scop_free(scop);
744 return NULL;
747 /* Construct a pet_scop that contains the arrays and the statements
748 * in "scop1" and "scop2".
750 struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
751 struct pet_scop *scop2)
753 int i;
754 struct pet_scop *scop;
756 if (!scop1 || !scop2)
757 goto error;
759 if (scop1->n_stmt == 0) {
760 pet_scop_free(scop1);
761 return scop2;
764 if (scop2->n_stmt == 0) {
765 pet_scop_free(scop2);
766 return scop1;
769 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
770 if (!scop)
771 goto error;
773 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
774 scop1->n_array + scop2->n_array);
775 if (!scop->arrays)
776 goto error;
777 scop->n_array = scop1->n_array + scop2->n_array;
779 for (i = 0; i < scop1->n_stmt; ++i) {
780 scop->stmts[i] = scop1->stmts[i];
781 scop1->stmts[i] = NULL;
784 for (i = 0; i < scop2->n_stmt; ++i) {
785 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
786 scop2->stmts[i] = NULL;
789 for (i = 0; i < scop1->n_array; ++i) {
790 scop->arrays[i] = scop1->arrays[i];
791 scop1->arrays[i] = NULL;
794 for (i = 0; i < scop2->n_array; ++i) {
795 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
796 scop2->arrays[i] = NULL;
799 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
800 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
802 pet_scop_free(scop1);
803 pet_scop_free(scop2);
804 return scop;
805 error:
806 pet_scop_free(scop1);
807 pet_scop_free(scop2);
808 return NULL;
811 void *pet_scop_free(struct pet_scop *scop)
813 int i;
815 if (!scop)
816 return NULL;
817 isl_set_free(scop->context);
818 isl_set_free(scop->context_value);
819 if (scop->arrays)
820 for (i = 0; i < scop->n_array; ++i)
821 pet_array_free(scop->arrays[i]);
822 free(scop->arrays);
823 if (scop->stmts)
824 for (i = 0; i < scop->n_stmt; ++i)
825 pet_stmt_free(scop->stmts[i]);
826 free(scop->stmts);
827 free(scop);
828 return NULL;
831 void pet_scop_dump(struct pet_scop *scop)
833 int i;
835 if (!scop)
836 return;
838 isl_set_dump(scop->context);
839 isl_set_dump(scop->context_value);
840 for (i = 0; i < scop->n_array; ++i)
841 pet_array_dump(scop->arrays[i]);
842 for (i = 0; i < scop->n_stmt; ++i)
843 pet_stmt_dump(scop->stmts[i]);
846 /* Return 1 if the two pet_arrays are equivalent.
848 * We don't compare element_size as this may be target dependent.
850 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
852 if (!array1 || !array2)
853 return 0;
855 if (!isl_set_is_equal(array1->context, array2->context))
856 return 0;
857 if (!isl_set_is_equal(array1->extent, array2->extent))
858 return 0;
859 if (!!array1->value_bounds != !!array2->value_bounds)
860 return 0;
861 if (array1->value_bounds &&
862 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
863 return 0;
864 if (strcmp(array1->element_type, array2->element_type))
865 return 0;
866 if (array1->live_out != array2->live_out)
867 return 0;
869 return 1;
872 /* Return 1 if the two pet_stmts are equivalent.
874 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
876 int i;
878 if (!stmt1 || !stmt2)
879 return 0;
881 if (stmt1->line != stmt2->line)
882 return 0;
883 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
884 return 0;
885 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
886 return 0;
887 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
888 return 0;
889 if (stmt1->n_arg != stmt2->n_arg)
890 return 0;
891 for (i = 0; i < stmt1->n_arg; ++i) {
892 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
893 return 0;
896 return 1;
899 /* Return 1 if the two pet_scops are equivalent.
901 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
903 int i;
905 if (!scop1 || !scop2)
906 return 0;
908 if (!isl_set_is_equal(scop1->context, scop2->context))
909 return 0;
910 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
911 return 0;
913 if (scop1->n_array != scop2->n_array)
914 return 0;
915 for (i = 0; i < scop1->n_array; ++i)
916 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
917 return 0;
919 if (scop1->n_stmt != scop2->n_stmt)
920 return 0;
921 for (i = 0; i < scop1->n_stmt; ++i)
922 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
923 return 0;
925 return 1;
928 /* Prefix the schedule of "stmt" with an extra dimension with constant
929 * value "pos".
931 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
933 if (!stmt)
934 return NULL;
936 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
937 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
938 if (!stmt->schedule)
939 return pet_stmt_free(stmt);
941 return stmt;
944 /* Prefix the schedules of all statements in "scop" with an extra
945 * dimension with constant value "pos".
947 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
949 int i;
951 if (!scop)
952 return NULL;
954 for (i = 0; i < scop->n_stmt; ++i) {
955 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
956 if (!scop->stmts[i])
957 return pet_scop_free(scop);
960 return scop;
963 /* Data used in embed_access.
964 * extend adds an iterator to the iteration domain
965 * var_id represents the induction variable of the corresponding loop
967 struct pet_embed_access {
968 isl_map *extend;
969 isl_id *var_id;
972 /* Embed the access relation in an extra outer loop.
974 * We first update the iteration domain to insert the extra dimension.
976 * If the access refers to the induction variable, then it is
977 * turned into an access to the set of integers with index (and value)
978 * equal to the induction variable.
980 * If the induction variable appears in the constraints (as a parameter),
981 * then the parameter is equated to the newly introduced iteration
982 * domain dimension and subsequently projected out.
984 * Similarly, if the accessed array is a virtual array (with user
985 * pointer equal to NULL), as created by create_test_access,
986 * then it is extended along with the domain of the access.
988 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
989 void *user)
991 struct pet_embed_access *data = user;
992 isl_id *array_id = NULL;
993 int pos;
995 access = update_domain(access, data->extend);
997 if (isl_map_has_tuple_id(access, isl_dim_out))
998 array_id = isl_map_get_tuple_id(access, isl_dim_out);
999 if (array_id == data->var_id ||
1000 (array_id && !isl_id_get_user(array_id))) {
1001 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1002 access = isl_map_equate(access,
1003 isl_dim_in, 0, isl_dim_out, 0);
1004 if (array_id != data->var_id)
1005 access = isl_map_set_tuple_id(access, isl_dim_out,
1006 isl_id_copy(array_id));
1008 isl_id_free(array_id);
1010 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1011 if (pos >= 0) {
1012 access = isl_map_equate(access,
1013 isl_dim_param, pos, isl_dim_in, 0);
1014 access = isl_map_project_out(access, isl_dim_param, pos, 1);
1016 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1017 isl_id_copy(data->var_id));
1019 return access;
1022 /* Embed all access relations in "expr" in an extra loop.
1023 * "extend" inserts an outer loop iterator in the iteration domains.
1024 * "var_id" represents the induction variable.
1026 static struct pet_expr *expr_embed(struct pet_expr *expr,
1027 __isl_take isl_map *extend, __isl_keep isl_id *var_id)
1029 struct pet_embed_access data = { .extend = extend, .var_id = var_id };
1031 expr = pet_expr_foreach_access(expr, &embed_access, &data);
1032 isl_map_free(extend);
1033 return expr;
1036 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1037 * "dom" and schedule "sched". "var_id" represents the induction variable
1038 * of the loop.
1040 * The iteration domain and schedule of the statement are updated
1041 * according to the iteration domain and schedule of the new loop.
1042 * If stmt->domain is a wrapped map, then the iteration domain
1043 * is the domain of this map, so we need to be careful to adjust
1044 * this domain.
1046 * If the induction variable appears in the constraints (as a parameter)
1047 * of the current iteration domain or the schedule of the statement,
1048 * then the parameter is equated to the newly introduced iteration
1049 * domain dimension and subsequently projected out.
1051 * Finally, all access relations are updated based on the extra loop.
1053 struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt, __isl_take isl_set *dom,
1054 __isl_take isl_map *sched, __isl_take isl_id *var_id)
1056 int i;
1057 int pos;
1058 isl_id *stmt_id;
1059 isl_space *dim;
1060 isl_map *extend;
1062 if (!stmt)
1063 goto error;
1065 if (isl_set_is_wrapping(stmt->domain)) {
1066 isl_map *map;
1067 isl_map *ext;
1068 isl_space *ran_dim;
1070 map = isl_set_unwrap(stmt->domain);
1071 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1072 ran_dim = isl_space_range(isl_map_get_space(map));
1073 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1074 isl_set_universe(ran_dim));
1075 map = isl_map_flat_domain_product(ext, map);
1076 map = isl_map_set_tuple_id(map, isl_dim_in,
1077 isl_id_copy(stmt_id));
1078 dim = isl_space_domain(isl_map_get_space(map));
1079 stmt->domain = isl_map_wrap(map);
1080 } else {
1081 stmt_id = isl_set_get_tuple_id(stmt->domain);
1082 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1083 stmt->domain);
1084 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1085 isl_id_copy(stmt_id));
1086 dim = isl_set_get_space(stmt->domain);
1089 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1090 if (pos >= 0) {
1091 stmt->domain = isl_set_equate(stmt->domain,
1092 isl_dim_param, pos, isl_dim_set, 0);
1093 stmt->domain = isl_set_project_out(stmt->domain,
1094 isl_dim_param, pos, 1);
1097 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1098 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1099 isl_dim_in, stmt_id);
1101 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1102 if (pos >= 0) {
1103 stmt->schedule = isl_map_equate(stmt->schedule,
1104 isl_dim_param, pos, isl_dim_in, 0);
1105 stmt->schedule = isl_map_project_out(stmt->schedule,
1106 isl_dim_param, pos, 1);
1109 dim = isl_space_map_from_set(dim);
1110 extend = isl_map_identity(dim);
1111 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1112 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1113 isl_map_get_tuple_id(extend, isl_dim_out));
1114 for (i = 0; i < stmt->n_arg; ++i)
1115 stmt->args[i] = expr_embed(stmt->args[i],
1116 isl_map_copy(extend), var_id);
1117 stmt->body = expr_embed(stmt->body, extend, var_id);
1119 isl_set_free(dom);
1120 isl_id_free(var_id);
1122 for (i = 0; i < stmt->n_arg; ++i)
1123 if (!stmt->args[i])
1124 return pet_stmt_free(stmt);
1125 if (!stmt->domain || !stmt->schedule || !stmt->body)
1126 return pet_stmt_free(stmt);
1127 return stmt;
1128 error:
1129 isl_set_free(dom);
1130 isl_map_free(sched);
1131 isl_id_free(var_id);
1132 return NULL;
1135 /* Embed the given pet_array in an extra outer loop with iteration domain
1136 * "dom".
1137 * This embedding only has an effect on virtual arrays (those with
1138 * user pointer equal to NULL), which need to be extended along with
1139 * the iteration domain.
1141 static struct pet_array *pet_array_embed(struct pet_array *array,
1142 __isl_take isl_set *dom)
1144 isl_id *array_id = NULL;
1146 if (!array)
1147 goto error;
1149 if (isl_set_has_tuple_id(array->extent))
1150 array_id = isl_set_get_tuple_id(array->extent);
1152 if (array_id && !isl_id_get_user(array_id)) {
1153 array->extent = isl_set_flat_product(dom, array->extent);
1154 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1155 } else {
1156 isl_set_free(dom);
1157 isl_id_free(array_id);
1160 return array;
1161 error:
1162 isl_set_free(dom);
1163 return NULL;
1166 /* Project out all unnamed parameters from "set" and return the result.
1168 static __isl_give isl_set *set_project_out_unnamed_params(
1169 __isl_take isl_set *set)
1171 int i, n;
1173 n = isl_set_dim(set, isl_dim_param);
1174 for (i = n - 1; i >= 0; --i) {
1175 if (isl_set_has_dim_name(set, isl_dim_param, i))
1176 continue;
1177 set = isl_set_project_out(set, isl_dim_param, i, 1);
1180 return set;
1183 /* Update the context with respect to an embedding into a loop
1184 * with iteration domain "dom" and induction variable "id".
1186 * If the current context is independent of "id", we don't need
1187 * to do anything.
1188 * Otherwise, a parameter value is invalid for the embedding if
1189 * any of the corresponding iterator values is invalid.
1190 * That is, a parameter value is valid only if all the corresponding
1191 * iterator values are valid.
1192 * We therefore compute the set of parameters
1194 * forall i in dom : valid (i)
1196 * or
1198 * not exists i in dom : not valid(i)
1200 * i.e.,
1202 * not exists i in dom \ valid(i)
1204 * If there are any unnamed parameters in "dom", then we consider
1205 * a parameter value to be valid if it is valid for any value of those
1206 * unnamed parameters. They are therefore projected out at the end.
1208 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1209 __isl_keep isl_set *dom, __isl_keep isl_id *id)
1211 int pos;
1213 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1214 if (pos < 0)
1215 return context;
1217 context = isl_set_from_params(context);
1218 context = isl_set_add_dims(context, isl_dim_set, 1);
1219 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1220 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1221 context = isl_set_subtract(isl_set_copy(dom), context);
1222 context = isl_set_params(context);
1223 context = isl_set_complement(context);
1224 context = set_project_out_unnamed_params(context);
1225 return context;
1228 /* Embed all statements and arrays in "scop" in an extra outer loop
1229 * with iteration domain "dom" and schedule "sched".
1230 * "id" represents the induction variable of the loop.
1232 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1233 __isl_take isl_map *sched, __isl_take isl_id *id)
1235 int i;
1237 if (!scop)
1238 goto error;
1240 scop->context = context_embed(scop->context, dom, id);
1241 if (!scop->context)
1242 goto error;
1244 for (i = 0; i < scop->n_stmt; ++i) {
1245 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1246 isl_set_copy(dom),
1247 isl_map_copy(sched), isl_id_copy(id));
1248 if (!scop->stmts[i])
1249 goto error;
1252 for (i = 0; i < scop->n_array; ++i) {
1253 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1254 isl_set_copy(dom));
1255 if (!scop->arrays[i])
1256 goto error;
1259 isl_set_free(dom);
1260 isl_map_free(sched);
1261 isl_id_free(id);
1262 return scop;
1263 error:
1264 isl_set_free(dom);
1265 isl_map_free(sched);
1266 isl_id_free(id);
1267 return pet_scop_free(scop);
1270 /* Add extra conditions on the parameters to iteration domain of "stmt".
1272 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1273 __isl_take isl_set *cond)
1275 if (!stmt)
1276 goto error;
1278 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1280 return stmt;
1281 error:
1282 isl_set_free(cond);
1283 return pet_stmt_free(stmt);
1286 /* Add extra conditions on the parameters to all iteration domains.
1288 * A parameter value is valid for the result if it was valid
1289 * for the original scop and satisfies "cond" or if it does
1290 * not satisfy "cond" as in this case the scop is not executed
1291 * and the original constraints on the parameters are irrelevant.
1293 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1294 __isl_take isl_set *cond)
1296 int i;
1298 if (!scop)
1299 goto error;
1301 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1302 scop->context = isl_set_union(scop->context,
1303 isl_set_complement(isl_set_copy(cond)));
1304 scop->context = isl_set_coalesce(scop->context);
1305 if (!scop->context)
1306 goto error;
1308 for (i = 0; i < scop->n_stmt; ++i) {
1309 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1310 isl_set_copy(cond));
1311 if (!scop->stmts[i])
1312 goto error;
1315 isl_set_free(cond);
1316 return scop;
1317 error:
1318 isl_set_free(cond);
1319 return pet_scop_free(scop);
1322 /* Make the statements "stmt" depend on the value of "test"
1323 * being equal to "satisfied" by adjusting stmt->domain.
1325 * We insert an argument corresponding to a read to "test"
1326 * from the iteration domain of "stmt" in front of the list of arguments.
1327 * We also insert a corresponding output dimension in the wrapped
1328 * map contained in stmt->domain, with value set to "satisfied".
1330 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1331 __isl_take isl_map *test, int satisfied)
1333 int i;
1334 isl_id *id;
1335 isl_ctx *ctx;
1336 isl_map *map;
1337 isl_set *dom;
1339 if (!stmt || !test)
1340 goto error;
1342 if (isl_set_is_wrapping(stmt->domain))
1343 map = isl_set_unwrap(stmt->domain);
1344 else
1345 map = isl_map_from_domain(stmt->domain);
1346 map = isl_map_insert_dims(map, isl_dim_out, 0, 1);
1347 id = isl_map_get_tuple_id(test, isl_dim_out);
1348 map = isl_map_set_dim_id(map, isl_dim_out, 0, id);
1349 map = isl_map_fix_si(map, isl_dim_out, 0, satisfied);
1350 dom = isl_set_universe(isl_space_domain(isl_map_get_space(map)));
1351 test = isl_map_apply_domain(test, isl_map_from_range(dom));
1353 stmt->domain = isl_map_wrap(map);
1355 ctx = isl_map_get_ctx(test);
1356 if (!stmt->args) {
1357 stmt->args = isl_calloc_array(ctx, struct pet_expr *, 1);
1358 if (!stmt->args)
1359 goto error;
1360 } else {
1361 struct pet_expr **args;
1362 args = isl_calloc_array(ctx, struct pet_expr *, 1 + stmt->n_arg);
1363 if (!args)
1364 goto error;
1365 for (i = 0; i < stmt->n_arg; ++i)
1366 args[1 + i] = stmt->args[i];
1367 free(stmt->args);
1368 stmt->args = args;
1370 stmt->n_arg++;
1371 stmt->args[0] = pet_expr_from_access(isl_map_copy(test));
1372 if (!stmt->args[0])
1373 goto error;
1375 isl_map_free(test);
1376 return stmt;
1377 error:
1378 isl_map_free(test);
1379 return pet_stmt_free(stmt);
1382 /* Make all statements in "scop" depend on the value of "test"
1383 * being equal to "satisfied" by adjusting their domains.
1385 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
1386 __isl_take isl_map *test, int satisfied)
1388 int i;
1390 if (!scop)
1391 goto error;
1393 for (i = 0; i < scop->n_stmt; ++i) {
1394 scop->stmts[i] = stmt_filter(scop->stmts[i],
1395 isl_map_copy(test), satisfied);
1396 if (!scop->stmts[i])
1397 goto error;
1400 isl_map_free(test);
1401 return scop;
1402 error:
1403 isl_map_free(test);
1404 return pet_scop_free(scop);
1407 /* Add all parameters in "expr" to "dim" and return the result.
1409 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
1410 __isl_take isl_space *dim)
1412 int i;
1414 if (!expr)
1415 goto error;
1416 for (i = 0; i < expr->n_arg; ++i)
1418 dim = expr_collect_params(expr->args[i], dim);
1420 if (expr->type == pet_expr_access)
1421 dim = isl_space_align_params(dim,
1422 isl_map_get_space(expr->acc.access));
1424 return dim;
1425 error:
1426 isl_space_free(dim);
1427 return pet_expr_free(expr);
1430 /* Add all parameters in "stmt" to "dim" and return the result.
1432 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
1433 __isl_take isl_space *dim)
1435 if (!stmt)
1436 goto error;
1438 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
1439 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
1440 dim = expr_collect_params(stmt->body, dim);
1442 return dim;
1443 error:
1444 isl_space_free(dim);
1445 return pet_stmt_free(stmt);
1448 /* Add all parameters in "array" to "dim" and return the result.
1450 static __isl_give isl_space *array_collect_params(struct pet_array *array,
1451 __isl_take isl_space *dim)
1453 if (!array)
1454 goto error;
1456 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
1457 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
1459 return dim;
1460 error:
1461 isl_space_free(dim);
1462 return pet_array_free(array);
1465 /* Add all parameters in "scop" to "dim" and return the result.
1467 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
1468 __isl_take isl_space *dim)
1470 int i;
1472 if (!scop)
1473 goto error;
1475 for (i = 0; i < scop->n_array; ++i)
1476 dim = array_collect_params(scop->arrays[i], dim);
1478 for (i = 0; i < scop->n_stmt; ++i)
1479 dim = stmt_collect_params(scop->stmts[i], dim);
1481 return dim;
1482 error:
1483 isl_space_free(dim);
1484 return pet_scop_free(scop);
1487 /* Add all parameters in "dim" to all access relations in "expr".
1489 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
1490 __isl_take isl_space *dim)
1492 int i;
1494 if (!expr)
1495 goto error;
1497 for (i = 0; i < expr->n_arg; ++i) {
1498 expr->args[i] =
1499 expr_propagate_params(expr->args[i],
1500 isl_space_copy(dim));
1501 if (!expr->args[i])
1502 goto error;
1505 if (expr->type == pet_expr_access) {
1506 expr->acc.access = isl_map_align_params(expr->acc.access,
1507 isl_space_copy(dim));
1508 if (!expr->acc.access)
1509 goto error;
1512 isl_space_free(dim);
1513 return expr;
1514 error:
1515 isl_space_free(dim);
1516 return pet_expr_free(expr);
1519 /* Add all parameters in "dim" to the domain, schedule and
1520 * all access relations in "stmt".
1522 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
1523 __isl_take isl_space *dim)
1525 if (!stmt)
1526 goto error;
1528 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
1529 stmt->schedule = isl_map_align_params(stmt->schedule,
1530 isl_space_copy(dim));
1531 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
1533 if (!stmt->domain || !stmt->schedule || !stmt->body)
1534 goto error;
1536 isl_space_free(dim);
1537 return stmt;
1538 error:
1539 isl_space_free(dim);
1540 return pet_stmt_free(stmt);
1543 /* Add all parameters in "dim" to "array".
1545 static struct pet_array *array_propagate_params(struct pet_array *array,
1546 __isl_take isl_space *dim)
1548 if (!array)
1549 goto error;
1551 array->context = isl_set_align_params(array->context,
1552 isl_space_copy(dim));
1553 array->extent = isl_set_align_params(array->extent,
1554 isl_space_copy(dim));
1555 if (array->value_bounds) {
1556 array->value_bounds = isl_set_align_params(array->value_bounds,
1557 isl_space_copy(dim));
1558 if (!array->value_bounds)
1559 goto error;
1562 if (!array->context || !array->extent)
1563 goto error;
1565 isl_space_free(dim);
1566 return array;
1567 error:
1568 isl_space_free(dim);
1569 return pet_array_free(array);
1572 /* Add all parameters in "dim" to "scop".
1574 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
1575 __isl_take isl_space *dim)
1577 int i;
1579 if (!scop)
1580 goto error;
1582 for (i = 0; i < scop->n_array; ++i) {
1583 scop->arrays[i] = array_propagate_params(scop->arrays[i],
1584 isl_space_copy(dim));
1585 if (!scop->arrays[i])
1586 goto error;
1589 for (i = 0; i < scop->n_stmt; ++i) {
1590 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
1591 isl_space_copy(dim));
1592 if (!scop->stmts[i])
1593 goto error;
1596 isl_space_free(dim);
1597 return scop;
1598 error:
1599 isl_space_free(dim);
1600 return pet_scop_free(scop);
1603 /* Update all isl_sets and isl_maps in "scop" such that they all
1604 * have the same parameters.
1606 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
1608 isl_space *dim;
1610 if (!scop)
1611 return NULL;
1613 dim = isl_set_get_space(scop->context);
1614 dim = scop_collect_params(scop, dim);
1616 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
1617 scop = scop_propagate_params(scop, dim);
1619 return scop;
1622 /* Check if the given access relation accesses a (0D) array that corresponds
1623 * to one of the parameters in "dim". If so, replace the array access
1624 * by an access to the set of integers with as index (and value)
1625 * that parameter.
1627 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
1628 __isl_take isl_space *dim)
1630 isl_id *array_id = NULL;
1631 int pos = -1;
1633 if (isl_map_has_tuple_id(access, isl_dim_out)) {
1634 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1635 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
1637 isl_space_free(dim);
1639 if (pos < 0) {
1640 isl_id_free(array_id);
1641 return access;
1644 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
1645 if (pos < 0) {
1646 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
1647 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
1648 pos = 0;
1649 } else
1650 isl_id_free(array_id);
1652 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1653 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
1655 return access;
1658 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1659 * in "dim" by a value equal to the corresponding parameter.
1661 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
1662 __isl_take isl_space *dim)
1664 int i;
1666 if (!expr)
1667 goto error;
1669 for (i = 0; i < expr->n_arg; ++i) {
1670 expr->args[i] =
1671 expr_detect_parameter_accesses(expr->args[i],
1672 isl_space_copy(dim));
1673 if (!expr->args[i])
1674 goto error;
1677 if (expr->type == pet_expr_access) {
1678 expr->acc.access = access_detect_parameter(expr->acc.access,
1679 isl_space_copy(dim));
1680 if (!expr->acc.access)
1681 goto error;
1684 isl_space_free(dim);
1685 return expr;
1686 error:
1687 isl_space_free(dim);
1688 return pet_expr_free(expr);
1691 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1692 * in "dim" by a value equal to the corresponding parameter.
1694 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
1695 __isl_take isl_space *dim)
1697 if (!stmt)
1698 goto error;
1700 stmt->body = expr_detect_parameter_accesses(stmt->body,
1701 isl_space_copy(dim));
1703 if (!stmt->domain || !stmt->schedule || !stmt->body)
1704 goto error;
1706 isl_space_free(dim);
1707 return stmt;
1708 error:
1709 isl_space_free(dim);
1710 return pet_stmt_free(stmt);
1713 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1714 * in "dim" by a value equal to the corresponding parameter.
1716 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
1717 __isl_take isl_space *dim)
1719 int i;
1721 if (!scop)
1722 goto error;
1724 for (i = 0; i < scop->n_stmt; ++i) {
1725 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
1726 isl_space_copy(dim));
1727 if (!scop->stmts[i])
1728 goto error;
1731 isl_space_free(dim);
1732 return scop;
1733 error:
1734 isl_space_free(dim);
1735 return pet_scop_free(scop);
1738 /* Replace all accesses to (0D) arrays that correspond to any of
1739 * the parameters used in "scop" by a value equal
1740 * to the corresponding parameter.
1742 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
1744 isl_space *dim;
1746 if (!scop)
1747 return NULL;
1749 dim = isl_set_get_space(scop->context);
1750 dim = scop_collect_params(scop, dim);
1752 scop = scop_detect_parameter_accesses(scop, dim);
1754 return scop;
1757 /* Add all read access relations (if "read" is set) and/or all write
1758 * access relations (if "write" is set) to "accesses" and return the result.
1760 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
1761 int read, int write, __isl_take isl_union_map *accesses)
1763 int i;
1764 isl_id *id;
1765 isl_space *dim;
1767 if (!expr)
1768 return NULL;
1770 for (i = 0; i < expr->n_arg; ++i)
1771 accesses = expr_collect_accesses(expr->args[i],
1772 read, write, accesses);
1774 if (expr->type == pet_expr_access &&
1775 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
1776 ((read && expr->acc.read) || (write && expr->acc.write)))
1777 accesses = isl_union_map_add_map(accesses,
1778 isl_map_copy(expr->acc.access));
1780 return accesses;
1783 /* Collect and return all read access relations (if "read" is set)
1784 * and/or all write * access relations (if "write" is set) in "stmt".
1786 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
1787 int read, int write, __isl_take isl_space *dim)
1789 isl_union_map *accesses;
1791 if (!stmt)
1792 return NULL;
1794 accesses = isl_union_map_empty(dim);
1795 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
1796 accesses = isl_union_map_intersect_domain(accesses,
1797 isl_union_set_from_set(isl_set_copy(stmt->domain)));
1799 return accesses;
1802 /* Collect and return all read access relations (if "read" is set)
1803 * and/or all write * access relations (if "write" is set) in "scop".
1805 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
1806 int read, int write)
1808 int i;
1809 isl_union_map *accesses;
1811 if (!scop)
1812 return NULL;
1814 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
1816 for (i = 0; i < scop->n_stmt; ++i) {
1817 isl_union_map *accesses_i;
1818 isl_space *dim = isl_set_get_space(scop->context);
1819 accesses_i = stmt_collect_accesses(scop->stmts[i],
1820 read, write, dim);
1821 accesses = isl_union_map_union(accesses, accesses_i);
1824 return accesses;
1827 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
1829 return scop_collect_accesses(scop, 1, 0);
1832 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
1834 return scop_collect_accesses(scop, 0, 1);
1837 /* Collect and return the union of iteration domains in "scop".
1839 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
1841 int i;
1842 isl_set *domain_i;
1843 isl_union_set *domain;
1845 if (!scop)
1846 return NULL;
1848 domain = isl_union_set_empty(isl_set_get_space(scop->context));
1850 for (i = 0; i < scop->n_stmt; ++i) {
1851 domain_i = isl_set_copy(scop->stmts[i]->domain);
1852 domain = isl_union_set_add_set(domain, domain_i);
1855 return domain;
1858 /* Collect and return the schedules of the statements in "scop".
1859 * The range is normalized to the maximal number of scheduling
1860 * dimensions.
1862 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
1864 int i, j;
1865 isl_map *schedule_i;
1866 isl_union_map *schedule;
1867 int depth, max_depth = 0;
1869 if (!scop)
1870 return NULL;
1872 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
1874 for (i = 0; i < scop->n_stmt; ++i) {
1875 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
1876 if (depth > max_depth)
1877 max_depth = depth;
1880 for (i = 0; i < scop->n_stmt; ++i) {
1881 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
1882 depth = isl_map_dim(schedule_i, isl_dim_out);
1883 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
1884 max_depth - depth);
1885 for (j = depth; j < max_depth; ++j)
1886 schedule_i = isl_map_fix_si(schedule_i,
1887 isl_dim_out, j, 0);
1888 schedule = isl_union_map_add_map(schedule, schedule_i);
1891 return schedule;
1894 /* Does expression "expr" write to "id"?
1896 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
1898 int i;
1899 isl_id *write_id;
1901 for (i = 0; i < expr->n_arg; ++i) {
1902 int writes = expr_writes(expr->args[i], id);
1903 if (writes < 0 || writes)
1904 return writes;
1907 if (expr->type != pet_expr_access)
1908 return 0;
1909 if (!expr->acc.write)
1910 return 0;
1911 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
1912 return 0;
1914 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
1915 isl_id_free(write_id);
1917 if (!write_id)
1918 return -1;
1920 return write_id == id;
1923 /* Does statement "stmt" write to "id"?
1925 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
1927 return expr_writes(stmt->body, id);
1930 /* Is there any write access in "scop" that accesses "id"?
1932 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
1934 int i;
1936 if (!scop)
1937 return -1;
1939 for (i = 0; i < scop->n_stmt; ++i) {
1940 int writes = stmt_writes(scop->stmts[i], id);
1941 if (writes < 0 || writes)
1942 return writes;
1945 return 0;
1948 /* Reset the user pointer on all parameter ids in "set".
1950 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
1952 int i, n;
1954 n = isl_set_dim(set, isl_dim_param);
1955 for (i = 0; i < n; ++i) {
1956 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
1957 const char *name = isl_id_get_name(id);
1958 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
1959 isl_id_free(id);
1962 return set;
1965 /* Reset the user pointer on all parameter ids in "map".
1967 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
1969 int i, n;
1971 n = isl_map_dim(map, isl_dim_param);
1972 for (i = 0; i < n; ++i) {
1973 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
1974 const char *name = isl_id_get_name(id);
1975 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
1976 isl_id_free(id);
1979 return map;
1982 /* Reset the user pointer on all parameter ids in "array".
1984 static struct pet_array *array_anonymize(struct pet_array *array)
1986 if (!array)
1987 return NULL;
1989 array->context = set_anonymize(array->context);
1990 array->extent = set_anonymize(array->extent);
1991 if (!array->context || !array->extent)
1992 return pet_array_free(array);
1994 return array;
1997 /* Reset the user pointer on all parameter ids in "access".
1999 static __isl_give isl_map *access_anonymize(__isl_take isl_map *access,
2000 void *user)
2002 access = map_anonymize(access);
2004 return access;
2007 /* Reset the user pointer on all parameter ids in "stmt".
2009 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2011 int i;
2012 isl_space *space;
2013 isl_set *domain;
2015 if (!stmt)
2016 return NULL;
2018 stmt->domain = set_anonymize(stmt->domain);
2019 stmt->schedule = map_anonymize(stmt->schedule);
2020 if (!stmt->domain || !stmt->schedule)
2021 return pet_stmt_free(stmt);
2023 for (i = 0; i < stmt->n_arg; ++i) {
2024 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2025 &access_anonymize, NULL);
2026 if (!stmt->args[i])
2027 return pet_stmt_free(stmt);
2030 stmt->body = pet_expr_foreach_access(stmt->body,
2031 &access_anonymize, NULL);
2032 if (!stmt->body)
2033 return pet_stmt_free(stmt);
2035 return stmt;
2038 /* Reset the user pointer on all parameter ids in "scop".
2040 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2042 int i;
2044 if (!scop)
2045 return NULL;
2047 scop->context = set_anonymize(scop->context);
2048 scop->context_value = set_anonymize(scop->context_value);
2049 if (!scop->context || !scop->context_value)
2050 return pet_scop_free(scop);
2052 for (i = 0; i < scop->n_array; ++i) {
2053 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2054 if (!scop->arrays[i])
2055 return pet_scop_free(scop);
2058 for (i = 0; i < scop->n_stmt; ++i) {
2059 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2060 if (!scop->stmts[i])
2061 return pet_scop_free(scop);
2064 return scop;
2067 /* Given a set "domain", return a wrapped relation with the given set
2068 * as domain and a range of dimension "n_arg", where each coordinate
2069 * is either unbounded or, if the corresponding element of args is of
2070 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2072 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2073 unsigned n_arg, struct pet_expr **args,
2074 __isl_keep isl_union_map *value_bounds)
2076 int i;
2077 isl_map *map;
2078 isl_space *space;
2079 isl_ctx *ctx = isl_set_get_ctx(domain);
2081 map = isl_map_from_domain(domain);
2082 space = isl_map_get_space(map);
2083 space = isl_space_add_dims(space, isl_dim_out, 1);
2085 for (i = 0; i < n_arg; ++i) {
2086 isl_map *map_i;
2087 struct pet_expr *arg = args[i];
2088 isl_id *id;
2089 isl_space *space2;
2091 map_i = isl_map_universe(isl_space_copy(space));
2092 if (arg->type == pet_expr_access) {
2093 isl_map *vb;
2094 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2095 space2 = isl_space_alloc(ctx, 0, 0, 1);
2096 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2097 vb = isl_union_map_extract_map(value_bounds, space2);
2098 if (!isl_map_plain_is_empty(vb))
2099 map_i = isl_map_intersect_range(map_i,
2100 isl_map_range(vb));
2101 else
2102 isl_map_free(vb);
2104 map = isl_map_flat_range_product(map, map_i);
2106 isl_space_free(space);
2108 return isl_map_wrap(map);
2111 /* Data used in access_gist() callback.
2113 struct pet_access_gist_data {
2114 isl_set *domain;
2115 isl_union_map *value_bounds;
2118 /* Given an expression "expr" of type pet_expr_access, compute
2119 * the gist of the associated access relation with respect to
2120 * data->domain and the bounds on the values of the arguments
2121 * of the expression.
2123 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
2125 struct pet_access_gist_data *data = user;
2126 isl_set *domain;
2128 domain = isl_set_copy(data->domain);
2129 if (expr->n_arg > 0)
2130 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
2131 data->value_bounds);
2133 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
2134 if (!expr->acc.access)
2135 return pet_expr_free(expr);
2137 return expr;
2140 /* Compute the gist of the iteration domain and all access relations
2141 * of "stmt" based on the constraints on the parameters specified by "context"
2142 * and the constraints on the values of nested accesses specified
2143 * by "value_bounds".
2145 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2146 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2148 int i;
2149 isl_space *space;
2150 isl_set *domain;
2151 struct pet_access_gist_data data;
2153 if (!stmt)
2154 return NULL;
2156 data.domain = isl_set_copy(stmt->domain);
2157 data.value_bounds = value_bounds;
2158 if (stmt->n_arg > 0)
2159 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
2161 data.domain = isl_set_intersect_params(data.domain,
2162 isl_set_copy(context));
2164 for (i = 0; i < stmt->n_arg; ++i) {
2165 stmt->args[i] = pet_expr_foreach_access_expr(stmt->args[i],
2166 &access_gist, &data);
2167 if (!stmt->args[i])
2168 goto error;
2171 stmt->body = pet_expr_foreach_access_expr(stmt->body,
2172 &access_gist, &data);
2173 if (!stmt->body)
2174 goto error;
2176 isl_set_free(data.domain);
2178 space = isl_set_get_space(stmt->domain);
2179 if (isl_space_is_wrapping(space))
2180 space = isl_space_domain(isl_space_unwrap(space));
2181 domain = isl_set_universe(space);
2182 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2183 if (stmt->n_arg > 0)
2184 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
2185 value_bounds);
2186 stmt->domain = isl_set_gist(stmt->domain, domain);
2187 if (!stmt->domain)
2188 return pet_stmt_free(stmt);
2190 return stmt;
2191 error:
2192 isl_set_free(data.domain);
2193 return pet_stmt_free(stmt);
2196 /* Compute the gist of the extent of the array
2197 * based on the constraints on the parameters specified by "context".
2199 static struct pet_array *array_gist(struct pet_array *array,
2200 __isl_keep isl_set *context)
2202 if (!array)
2203 return NULL;
2205 array->extent = isl_set_gist_params(array->extent,
2206 isl_set_copy(context));
2207 if (!array->extent)
2208 return pet_array_free(array);
2210 return array;
2213 /* Compute the gist of all sets and relations in "scop"
2214 * based on the constraints on the parameters specified by "scop->context"
2215 * and the constraints on the values of nested accesses specified
2216 * by "value_bounds".
2218 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
2219 __isl_keep isl_union_map *value_bounds)
2221 int i;
2223 if (!scop)
2224 return NULL;
2226 scop->context = isl_set_coalesce(scop->context);
2227 if (!scop->context)
2228 return pet_scop_free(scop);
2230 for (i = 0; i < scop->n_array; ++i) {
2231 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
2232 if (!scop->arrays[i])
2233 return pet_scop_free(scop);
2236 for (i = 0; i < scop->n_stmt; ++i) {
2237 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
2238 value_bounds);
2239 if (!scop->stmts[i])
2240 return pet_scop_free(scop);
2243 return scop;
2246 /* Intersect the context of "scop" with "context".
2247 * To ensure that we don't introduce any unnamed parameters in
2248 * the context of "scop", we first remove the unnamed parameters
2249 * from "context".
2251 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
2252 __isl_take isl_set *context)
2254 if (!scop)
2255 goto error;
2257 context = set_project_out_unnamed_params(context);
2258 scop->context = isl_set_intersect(scop->context, context);
2259 if (!scop->context)
2260 return pet_scop_free(scop);
2262 return scop;
2263 error:
2264 isl_set_free(context);
2265 return pet_scop_free(scop);