PetScan::extract_nested: insert outer filters in earlier positions
[pet.git] / scop.c
blobd68a0592e96efca2f632227acf0eb565dcd25fdf
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 /* Does "expr" represent an access to an unnamed space, i.e.,
330 * does it represent an affine expression?
332 int pet_expr_is_affine(struct pet_expr *expr)
334 int has_id;
336 if (!expr)
337 return -1;
338 if (expr->type != pet_expr_access)
339 return 0;
341 has_id = isl_map_has_tuple_id(expr->acc.access, isl_dim_out);
342 if (has_id < 0)
343 return -1;
345 return !has_id;
348 /* Return 1 if the two pet_exprs are equivalent.
350 int pet_expr_is_equal(struct pet_expr *expr1, struct pet_expr *expr2)
352 int i;
354 if (!expr1 || !expr2)
355 return 0;
357 if (expr1->type != expr2->type)
358 return 0;
359 if (expr1->n_arg != expr2->n_arg)
360 return 0;
361 for (i = 0; i < expr1->n_arg; ++i)
362 if (!pet_expr_is_equal(expr1->args[i], expr2->args[i]))
363 return 0;
364 switch (expr1->type) {
365 case pet_expr_double:
366 if (expr1->d != expr2->d)
367 return 0;
368 break;
369 case pet_expr_access:
370 if (expr1->acc.read != expr2->acc.read)
371 return 0;
372 if (expr1->acc.write != expr2->acc.write)
373 return 0;
374 if (!expr1->acc.access || !expr2->acc.access)
375 return 0;
376 if (!isl_map_is_equal(expr1->acc.access, expr2->acc.access))
377 return 0;
378 break;
379 case pet_expr_unary:
380 case pet_expr_binary:
381 case pet_expr_ternary:
382 if (expr1->op != expr2->op)
383 return 0;
384 break;
385 case pet_expr_call:
386 if (strcmp(expr1->name, expr2->name))
387 return 0;
388 break;
391 return 1;
394 /* Add extra conditions on the parameters to all access relations in "expr".
396 struct pet_expr *pet_expr_restrict(struct pet_expr *expr,
397 __isl_take isl_set *cond)
399 int i;
401 if (!expr)
402 goto error;
404 for (i = 0; i < expr->n_arg; ++i) {
405 expr->args[i] = pet_expr_restrict(expr->args[i],
406 isl_set_copy(cond));
407 if (!expr->args[i])
408 goto error;
411 if (expr->type == pet_expr_access) {
412 expr->acc.access = isl_map_intersect_params(expr->acc.access,
413 isl_set_copy(cond));
414 if (!expr->acc.access)
415 goto error;
418 isl_set_free(cond);
419 return expr;
420 error:
421 isl_set_free(cond);
422 return pet_expr_free(expr);
425 /* Modify all access relations in "expr" by calling "fn" on them.
427 struct pet_expr *pet_expr_foreach_access(struct pet_expr *expr,
428 __isl_give isl_map *(*fn)(__isl_take isl_map *access, void *user),
429 void *user)
431 int i;
433 if (!expr)
434 return NULL;
436 for (i = 0; i < expr->n_arg; ++i) {
437 expr->args[i] = pet_expr_foreach_access(expr->args[i], fn, user);
438 if (!expr->args[i])
439 return pet_expr_free(expr);
442 if (expr->type == pet_expr_access) {
443 expr->acc.access = fn(expr->acc.access, user);
444 if (!expr->acc.access)
445 return pet_expr_free(expr);
448 return expr;
451 /* Modify all expressions of type pet_expr_access in "expr"
452 * by calling "fn" on them.
454 struct pet_expr *pet_expr_foreach_access_expr(struct pet_expr *expr,
455 struct pet_expr *(*fn)(struct pet_expr *expr, void *user),
456 void *user)
458 int i;
460 if (!expr)
461 return NULL;
463 for (i = 0; i < expr->n_arg; ++i) {
464 expr->args[i] = pet_expr_foreach_access_expr(expr->args[i],
465 fn, user);
466 if (!expr->args[i])
467 return pet_expr_free(expr);
470 if (expr->type == pet_expr_access)
471 expr = fn(expr, user);
473 return expr;
476 /* Modify the given access relation based on the given iteration space
477 * transformation.
478 * If the access has any arguments then the domain of the access relation
479 * is a wrapped mapping from the iteration space to the space of
480 * argument values. We only need to change the domain of this wrapped
481 * mapping, so we extend the input transformation with an identity mapping
482 * on the space of argument values.
484 static __isl_give isl_map *update_domain(__isl_take isl_map *access,
485 void *user)
487 isl_map *update = user;
488 isl_space *dim;
490 update = isl_map_copy(update);
492 dim = isl_map_get_space(access);
493 dim = isl_space_domain(dim);
494 if (!isl_space_is_wrapping(dim))
495 isl_space_free(dim);
496 else {
497 isl_map *id;
498 dim = isl_space_unwrap(dim);
499 dim = isl_space_range(dim);
500 dim = isl_space_map_from_set(dim);
501 id = isl_map_identity(dim);
502 update = isl_map_product(update, id);
505 return isl_map_apply_domain(access, update);
508 /* Modify all access relations in "expr" based on the given iteration space
509 * transformation.
511 static struct pet_expr *expr_update_domain(struct pet_expr *expr,
512 __isl_take isl_map *update)
514 expr = pet_expr_foreach_access(expr, &update_domain, update);
515 isl_map_free(update);
516 return expr;
519 /* Construct a pet_stmt with given line number and statement
520 * number from a pet_expr.
521 * The initial iteration domain is the zero-dimensional universe.
522 * The name of the domain is given by "label" if it is non-NULL.
523 * Otherwise, the name is constructed as S_<id>.
524 * The domains of all access relations are modified to refer
525 * to the statement iteration domain.
527 struct pet_stmt *pet_stmt_from_pet_expr(isl_ctx *ctx, int line,
528 __isl_take isl_id *label, int id, struct pet_expr *expr)
530 struct pet_stmt *stmt;
531 isl_space *dim;
532 isl_set *dom;
533 isl_map *sched;
534 isl_map *add_name;
535 char name[50];
537 if (!expr)
538 goto error;
540 stmt = isl_calloc_type(ctx, struct pet_stmt);
541 if (!stmt)
542 goto error;
544 dim = isl_space_set_alloc(ctx, 0, 0);
545 if (label)
546 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
547 else {
548 snprintf(name, sizeof(name), "S_%d", id);
549 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
551 dom = isl_set_universe(isl_space_copy(dim));
552 sched = isl_map_from_domain(isl_set_copy(dom));
554 dim = isl_space_from_range(dim);
555 add_name = isl_map_universe(dim);
556 expr = expr_update_domain(expr, add_name);
558 stmt->line = line;
559 stmt->domain = dom;
560 stmt->schedule = sched;
561 stmt->body = expr;
563 if (!stmt->domain || !stmt->schedule || !stmt->body)
564 return pet_stmt_free(stmt);
566 return stmt;
567 error:
568 isl_id_free(label);
569 return pet_expr_free(expr);
572 void *pet_stmt_free(struct pet_stmt *stmt)
574 int i;
576 if (!stmt)
577 return NULL;
579 isl_set_free(stmt->domain);
580 isl_map_free(stmt->schedule);
581 pet_expr_free(stmt->body);
583 for (i = 0; i < stmt->n_arg; ++i)
584 pet_expr_free(stmt->args[i]);
585 free(stmt->args);
587 free(stmt);
588 return NULL;
591 static void stmt_dump(struct pet_stmt *stmt, int indent)
593 int i;
595 if (!stmt)
596 return;
598 fprintf(stderr, "%*s%d\n", indent, "", stmt->line);
599 fprintf(stderr, "%*s", indent, "");
600 isl_set_dump(stmt->domain);
601 fprintf(stderr, "%*s", indent, "");
602 isl_map_dump(stmt->schedule);
603 expr_dump(stmt->body, indent);
604 for (i = 0; i < stmt->n_arg; ++i)
605 expr_dump(stmt->args[i], indent + 2);
608 void pet_stmt_dump(struct pet_stmt *stmt)
610 stmt_dump(stmt, 0);
613 void *pet_array_free(struct pet_array *array)
615 if (!array)
616 return NULL;
618 isl_set_free(array->context);
619 isl_set_free(array->extent);
620 isl_set_free(array->value_bounds);
621 free(array->element_type);
623 free(array);
624 return NULL;
627 void pet_array_dump(struct pet_array *array)
629 if (!array)
630 return;
632 isl_set_dump(array->context);
633 isl_set_dump(array->extent);
634 isl_set_dump(array->value_bounds);
635 fprintf(stderr, "%s %s\n", array->element_type,
636 array->live_out ? "live-out" : "");
639 /* Construct a pet_scop with room for n statements.
641 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
643 isl_space *space;
644 struct pet_scop *scop;
646 scop = isl_calloc_type(ctx, struct pet_scop);
647 if (!scop)
648 return NULL;
650 space = isl_space_params_alloc(ctx, 0);
651 scop->context = isl_set_universe(isl_space_copy(space));
652 scop->context_value = isl_set_universe(space);
653 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
654 if (!scop->context || !scop->stmts)
655 return pet_scop_free(scop);
657 scop->n_stmt = n;
659 return scop;
662 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
664 return scop_alloc(ctx, 0);
667 /* Update "context" with respect to the valid parameter values for "access".
669 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
670 __isl_take isl_set *context)
672 context = isl_set_intersect(context,
673 isl_map_params(isl_map_copy(access)));
674 return context;
677 /* Update "context" with respect to the valid parameter values for "expr".
679 * If "expr" represents a ternary operator, then a parameter value
680 * needs to be valid for the condition and for at least one of the
681 * remaining two arguments.
682 * If the condition is an affine expression, then we can be a bit more specific.
683 * The parameter then has to be valid for the second argument for
684 * non-zero accesses and valid for the third argument for zero accesses.
686 static __isl_give isl_set *expr_extract_context(struct pet_expr *expr,
687 __isl_take isl_set *context)
689 int i;
691 if (expr->type == pet_expr_ternary) {
692 int is_aff;
693 isl_set *context1, *context2;
695 is_aff = pet_expr_is_affine(expr->args[0]);
696 if (is_aff < 0)
697 goto error;
699 context = expr_extract_context(expr->args[0], context);
700 context1 = expr_extract_context(expr->args[1],
701 isl_set_copy(context));
702 context2 = expr_extract_context(expr->args[2], context);
704 if (is_aff) {
705 isl_map *access;
706 isl_set *zero_set;
708 access = isl_map_copy(expr->args[0]->acc.access);
709 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
710 zero_set = isl_map_params(access);
711 context1 = isl_set_subtract(context1,
712 isl_set_copy(zero_set));
713 context2 = isl_set_intersect(context2, zero_set);
716 context = isl_set_union(context1, context2);
717 context = isl_set_coalesce(context);
719 return context;
722 for (i = 0; i < expr->n_arg; ++i)
723 context = expr_extract_context(expr->args[i], context);
725 if (expr->type == pet_expr_access)
726 context = access_extract_context(expr->acc.access, context);
728 return context;
729 error:
730 isl_set_free(context);
731 return NULL;
734 /* Update "context" with respect to the valid parameter values for "stmt".
736 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
737 __isl_take isl_set *context)
739 int i;
741 for (i = 0; i < stmt->n_arg; ++i)
742 context = expr_extract_context(stmt->args[i], context);
744 context = expr_extract_context(stmt->body, context);
746 return context;
749 /* Construct a pet_scop that contains the given pet_stmt.
751 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
753 struct pet_scop *scop;
755 if (!stmt)
756 return NULL;
758 scop = scop_alloc(ctx, 1);
760 scop->context = stmt_extract_context(stmt, scop->context);
761 if (!scop->context)
762 goto error;
764 scop->stmts[0] = stmt;
766 return scop;
767 error:
768 pet_stmt_free(stmt);
769 pet_scop_free(scop);
770 return NULL;
773 /* Construct a pet_scop that contains the arrays and the statements
774 * in "scop1" and "scop2".
776 struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
777 struct pet_scop *scop2)
779 int i;
780 struct pet_scop *scop;
782 if (!scop1 || !scop2)
783 goto error;
785 if (scop1->n_stmt == 0) {
786 pet_scop_free(scop1);
787 return scop2;
790 if (scop2->n_stmt == 0) {
791 pet_scop_free(scop2);
792 return scop1;
795 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
796 if (!scop)
797 goto error;
799 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
800 scop1->n_array + scop2->n_array);
801 if (!scop->arrays)
802 goto error;
803 scop->n_array = scop1->n_array + scop2->n_array;
805 for (i = 0; i < scop1->n_stmt; ++i) {
806 scop->stmts[i] = scop1->stmts[i];
807 scop1->stmts[i] = NULL;
810 for (i = 0; i < scop2->n_stmt; ++i) {
811 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
812 scop2->stmts[i] = NULL;
815 for (i = 0; i < scop1->n_array; ++i) {
816 scop->arrays[i] = scop1->arrays[i];
817 scop1->arrays[i] = NULL;
820 for (i = 0; i < scop2->n_array; ++i) {
821 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
822 scop2->arrays[i] = NULL;
825 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
826 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
828 pet_scop_free(scop1);
829 pet_scop_free(scop2);
830 return scop;
831 error:
832 pet_scop_free(scop1);
833 pet_scop_free(scop2);
834 return NULL;
837 void *pet_scop_free(struct pet_scop *scop)
839 int i;
841 if (!scop)
842 return NULL;
843 isl_set_free(scop->context);
844 isl_set_free(scop->context_value);
845 if (scop->arrays)
846 for (i = 0; i < scop->n_array; ++i)
847 pet_array_free(scop->arrays[i]);
848 free(scop->arrays);
849 if (scop->stmts)
850 for (i = 0; i < scop->n_stmt; ++i)
851 pet_stmt_free(scop->stmts[i]);
852 free(scop->stmts);
853 free(scop);
854 return NULL;
857 void pet_scop_dump(struct pet_scop *scop)
859 int i;
861 if (!scop)
862 return;
864 isl_set_dump(scop->context);
865 isl_set_dump(scop->context_value);
866 for (i = 0; i < scop->n_array; ++i)
867 pet_array_dump(scop->arrays[i]);
868 for (i = 0; i < scop->n_stmt; ++i)
869 pet_stmt_dump(scop->stmts[i]);
872 /* Return 1 if the two pet_arrays are equivalent.
874 * We don't compare element_size as this may be target dependent.
876 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
878 if (!array1 || !array2)
879 return 0;
881 if (!isl_set_is_equal(array1->context, array2->context))
882 return 0;
883 if (!isl_set_is_equal(array1->extent, array2->extent))
884 return 0;
885 if (!!array1->value_bounds != !!array2->value_bounds)
886 return 0;
887 if (array1->value_bounds &&
888 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
889 return 0;
890 if (strcmp(array1->element_type, array2->element_type))
891 return 0;
892 if (array1->live_out != array2->live_out)
893 return 0;
894 if (array1->uniquely_defined != array2->uniquely_defined)
895 return 0;
897 return 1;
900 /* Return 1 if the two pet_stmts are equivalent.
902 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
904 int i;
906 if (!stmt1 || !stmt2)
907 return 0;
909 if (stmt1->line != stmt2->line)
910 return 0;
911 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
912 return 0;
913 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
914 return 0;
915 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
916 return 0;
917 if (stmt1->n_arg != stmt2->n_arg)
918 return 0;
919 for (i = 0; i < stmt1->n_arg; ++i) {
920 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
921 return 0;
924 return 1;
927 /* Return 1 if the two pet_scops are equivalent.
929 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
931 int i;
933 if (!scop1 || !scop2)
934 return 0;
936 if (!isl_set_is_equal(scop1->context, scop2->context))
937 return 0;
938 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
939 return 0;
941 if (scop1->n_array != scop2->n_array)
942 return 0;
943 for (i = 0; i < scop1->n_array; ++i)
944 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
945 return 0;
947 if (scop1->n_stmt != scop2->n_stmt)
948 return 0;
949 for (i = 0; i < scop1->n_stmt; ++i)
950 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
951 return 0;
953 return 1;
956 /* Prefix the schedule of "stmt" with an extra dimension with constant
957 * value "pos".
959 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
961 if (!stmt)
962 return NULL;
964 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
965 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
966 if (!stmt->schedule)
967 return pet_stmt_free(stmt);
969 return stmt;
972 /* Prefix the schedules of all statements in "scop" with an extra
973 * dimension with constant value "pos".
975 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
977 int i;
979 if (!scop)
980 return NULL;
982 for (i = 0; i < scop->n_stmt; ++i) {
983 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
984 if (!scop->stmts[i])
985 return pet_scop_free(scop);
988 return scop;
991 /* Data used in embed_access.
992 * extend adds an iterator to the iteration domain
993 * var_id represents the induction variable of the corresponding loop
995 struct pet_embed_access {
996 isl_map *extend;
997 isl_id *var_id;
1000 /* Embed the access relation in an extra outer loop.
1002 * We first update the iteration domain to insert the extra dimension.
1004 * If the access refers to the induction variable, then it is
1005 * turned into an access to the set of integers with index (and value)
1006 * equal to the induction variable.
1008 * If the induction variable appears in the constraints (as a parameter),
1009 * then the parameter is equated to the newly introduced iteration
1010 * domain dimension and subsequently projected out.
1012 * Similarly, if the accessed array is a virtual array (with user
1013 * pointer equal to NULL), as created by create_test_access,
1014 * then it is extended along with the domain of the access.
1016 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
1017 void *user)
1019 struct pet_embed_access *data = user;
1020 isl_id *array_id = NULL;
1021 int pos;
1023 access = update_domain(access, data->extend);
1025 if (isl_map_has_tuple_id(access, isl_dim_out))
1026 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1027 if (array_id == data->var_id ||
1028 (array_id && !isl_id_get_user(array_id))) {
1029 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1030 access = isl_map_equate(access,
1031 isl_dim_in, 0, isl_dim_out, 0);
1032 if (array_id != data->var_id)
1033 access = isl_map_set_tuple_id(access, isl_dim_out,
1034 isl_id_copy(array_id));
1036 isl_id_free(array_id);
1038 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1039 if (pos >= 0) {
1040 access = isl_map_equate(access,
1041 isl_dim_param, pos, isl_dim_in, 0);
1042 access = isl_map_project_out(access, isl_dim_param, pos, 1);
1044 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1045 isl_id_copy(data->var_id));
1047 return access;
1050 /* Embed all access relations in "expr" in an extra loop.
1051 * "extend" inserts an outer loop iterator in the iteration domains.
1052 * "var_id" represents the induction variable.
1054 static struct pet_expr *expr_embed(struct pet_expr *expr,
1055 __isl_take isl_map *extend, __isl_keep isl_id *var_id)
1057 struct pet_embed_access data = { .extend = extend, .var_id = var_id };
1059 expr = pet_expr_foreach_access(expr, &embed_access, &data);
1060 isl_map_free(extend);
1061 return expr;
1064 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1065 * "dom" and schedule "sched". "var_id" represents the induction variable
1066 * of the loop.
1068 * The iteration domain and schedule of the statement are updated
1069 * according to the iteration domain and schedule of the new loop.
1070 * If stmt->domain is a wrapped map, then the iteration domain
1071 * is the domain of this map, so we need to be careful to adjust
1072 * this domain.
1074 * If the induction variable appears in the constraints (as a parameter)
1075 * of the current iteration domain or the schedule of the statement,
1076 * then the parameter is equated to the newly introduced iteration
1077 * domain dimension and subsequently projected out.
1079 * Finally, all access relations are updated based on the extra loop.
1081 struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt, __isl_take isl_set *dom,
1082 __isl_take isl_map *sched, __isl_take isl_id *var_id)
1084 int i;
1085 int pos;
1086 isl_id *stmt_id;
1087 isl_space *dim;
1088 isl_map *extend;
1090 if (!stmt)
1091 goto error;
1093 if (isl_set_is_wrapping(stmt->domain)) {
1094 isl_map *map;
1095 isl_map *ext;
1096 isl_space *ran_dim;
1098 map = isl_set_unwrap(stmt->domain);
1099 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1100 ran_dim = isl_space_range(isl_map_get_space(map));
1101 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1102 isl_set_universe(ran_dim));
1103 map = isl_map_flat_domain_product(ext, map);
1104 map = isl_map_set_tuple_id(map, isl_dim_in,
1105 isl_id_copy(stmt_id));
1106 dim = isl_space_domain(isl_map_get_space(map));
1107 stmt->domain = isl_map_wrap(map);
1108 } else {
1109 stmt_id = isl_set_get_tuple_id(stmt->domain);
1110 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1111 stmt->domain);
1112 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1113 isl_id_copy(stmt_id));
1114 dim = isl_set_get_space(stmt->domain);
1117 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1118 if (pos >= 0) {
1119 stmt->domain = isl_set_equate(stmt->domain,
1120 isl_dim_param, pos, isl_dim_set, 0);
1121 stmt->domain = isl_set_project_out(stmt->domain,
1122 isl_dim_param, pos, 1);
1125 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1126 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1127 isl_dim_in, stmt_id);
1129 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1130 if (pos >= 0) {
1131 stmt->schedule = isl_map_equate(stmt->schedule,
1132 isl_dim_param, pos, isl_dim_in, 0);
1133 stmt->schedule = isl_map_project_out(stmt->schedule,
1134 isl_dim_param, pos, 1);
1137 dim = isl_space_map_from_set(dim);
1138 extend = isl_map_identity(dim);
1139 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1140 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1141 isl_map_get_tuple_id(extend, isl_dim_out));
1142 for (i = 0; i < stmt->n_arg; ++i)
1143 stmt->args[i] = expr_embed(stmt->args[i],
1144 isl_map_copy(extend), var_id);
1145 stmt->body = expr_embed(stmt->body, extend, var_id);
1147 isl_set_free(dom);
1148 isl_id_free(var_id);
1150 for (i = 0; i < stmt->n_arg; ++i)
1151 if (!stmt->args[i])
1152 return pet_stmt_free(stmt);
1153 if (!stmt->domain || !stmt->schedule || !stmt->body)
1154 return pet_stmt_free(stmt);
1155 return stmt;
1156 error:
1157 isl_set_free(dom);
1158 isl_map_free(sched);
1159 isl_id_free(var_id);
1160 return NULL;
1163 /* Embed the given pet_array in an extra outer loop with iteration domain
1164 * "dom".
1165 * This embedding only has an effect on virtual arrays (those with
1166 * user pointer equal to NULL), which need to be extended along with
1167 * the iteration domain.
1169 static struct pet_array *pet_array_embed(struct pet_array *array,
1170 __isl_take isl_set *dom)
1172 isl_id *array_id = NULL;
1174 if (!array)
1175 goto error;
1177 if (isl_set_has_tuple_id(array->extent))
1178 array_id = isl_set_get_tuple_id(array->extent);
1180 if (array_id && !isl_id_get_user(array_id)) {
1181 array->extent = isl_set_flat_product(dom, array->extent);
1182 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1183 } else {
1184 isl_set_free(dom);
1185 isl_id_free(array_id);
1188 return array;
1189 error:
1190 isl_set_free(dom);
1191 return NULL;
1194 /* Project out all unnamed parameters from "set" and return the result.
1196 static __isl_give isl_set *set_project_out_unnamed_params(
1197 __isl_take isl_set *set)
1199 int i, n;
1201 n = isl_set_dim(set, isl_dim_param);
1202 for (i = n - 1; i >= 0; --i) {
1203 if (isl_set_has_dim_name(set, isl_dim_param, i))
1204 continue;
1205 set = isl_set_project_out(set, isl_dim_param, i, 1);
1208 return set;
1211 /* Update the context with respect to an embedding into a loop
1212 * with iteration domain "dom" and induction variable "id".
1214 * If the current context is independent of "id", we don't need
1215 * to do anything.
1216 * Otherwise, a parameter value is invalid for the embedding if
1217 * any of the corresponding iterator values is invalid.
1218 * That is, a parameter value is valid only if all the corresponding
1219 * iterator values are valid.
1220 * We therefore compute the set of parameters
1222 * forall i in dom : valid (i)
1224 * or
1226 * not exists i in dom : not valid(i)
1228 * i.e.,
1230 * not exists i in dom \ valid(i)
1232 * If there are any unnamed parameters in "dom", then we consider
1233 * a parameter value to be valid if it is valid for any value of those
1234 * unnamed parameters. They are therefore projected out at the end.
1236 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1237 __isl_keep isl_set *dom, __isl_keep isl_id *id)
1239 int pos;
1241 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1242 if (pos < 0)
1243 return context;
1245 context = isl_set_from_params(context);
1246 context = isl_set_add_dims(context, isl_dim_set, 1);
1247 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1248 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1249 context = isl_set_subtract(isl_set_copy(dom), context);
1250 context = isl_set_params(context);
1251 context = isl_set_complement(context);
1252 context = set_project_out_unnamed_params(context);
1253 return context;
1256 /* Embed all statements and arrays in "scop" in an extra outer loop
1257 * with iteration domain "dom" and schedule "sched".
1258 * "id" represents the induction variable of the loop.
1260 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1261 __isl_take isl_map *sched, __isl_take isl_id *id)
1263 int i;
1265 if (!scop)
1266 goto error;
1268 scop->context = context_embed(scop->context, dom, id);
1269 if (!scop->context)
1270 goto error;
1272 for (i = 0; i < scop->n_stmt; ++i) {
1273 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1274 isl_set_copy(dom),
1275 isl_map_copy(sched), isl_id_copy(id));
1276 if (!scop->stmts[i])
1277 goto error;
1280 for (i = 0; i < scop->n_array; ++i) {
1281 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1282 isl_set_copy(dom));
1283 if (!scop->arrays[i])
1284 goto error;
1287 isl_set_free(dom);
1288 isl_map_free(sched);
1289 isl_id_free(id);
1290 return scop;
1291 error:
1292 isl_set_free(dom);
1293 isl_map_free(sched);
1294 isl_id_free(id);
1295 return pet_scop_free(scop);
1298 /* Add extra conditions on the parameters to iteration domain of "stmt".
1300 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1301 __isl_take isl_set *cond)
1303 if (!stmt)
1304 goto error;
1306 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1308 return stmt;
1309 error:
1310 isl_set_free(cond);
1311 return pet_stmt_free(stmt);
1314 /* Add extra conditions on the parameters to all iteration domains.
1316 * A parameter value is valid for the result if it was valid
1317 * for the original scop and satisfies "cond" or if it does
1318 * not satisfy "cond" as in this case the scop is not executed
1319 * and the original constraints on the parameters are irrelevant.
1321 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1322 __isl_take isl_set *cond)
1324 int i;
1326 if (!scop)
1327 goto error;
1329 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1330 scop->context = isl_set_union(scop->context,
1331 isl_set_complement(isl_set_copy(cond)));
1332 scop->context = isl_set_coalesce(scop->context);
1333 if (!scop->context)
1334 goto error;
1336 for (i = 0; i < scop->n_stmt; ++i) {
1337 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1338 isl_set_copy(cond));
1339 if (!scop->stmts[i])
1340 goto error;
1343 isl_set_free(cond);
1344 return scop;
1345 error:
1346 isl_set_free(cond);
1347 return pet_scop_free(scop);
1350 /* Make the statements "stmt" depend on the value of "test"
1351 * being equal to "satisfied" by adjusting stmt->domain.
1353 * We insert an argument corresponding to a read to "test"
1354 * from the iteration domain of "stmt" in front of the list of arguments.
1355 * We also insert a corresponding output dimension in the wrapped
1356 * map contained in stmt->domain, with value set to "satisfied".
1358 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1359 __isl_take isl_map *test, int satisfied)
1361 int i;
1362 isl_id *id;
1363 isl_ctx *ctx;
1364 isl_map *map;
1365 isl_set *dom;
1367 if (!stmt || !test)
1368 goto error;
1370 if (isl_set_is_wrapping(stmt->domain))
1371 map = isl_set_unwrap(stmt->domain);
1372 else
1373 map = isl_map_from_domain(stmt->domain);
1374 map = isl_map_insert_dims(map, isl_dim_out, 0, 1);
1375 id = isl_map_get_tuple_id(test, isl_dim_out);
1376 map = isl_map_set_dim_id(map, isl_dim_out, 0, id);
1377 map = isl_map_fix_si(map, isl_dim_out, 0, satisfied);
1378 dom = isl_set_universe(isl_space_domain(isl_map_get_space(map)));
1379 test = isl_map_apply_domain(test, isl_map_from_range(dom));
1381 stmt->domain = isl_map_wrap(map);
1383 ctx = isl_map_get_ctx(test);
1384 if (!stmt->args) {
1385 stmt->args = isl_calloc_array(ctx, struct pet_expr *, 1);
1386 if (!stmt->args)
1387 goto error;
1388 } else {
1389 struct pet_expr **args;
1390 args = isl_calloc_array(ctx, struct pet_expr *, 1 + stmt->n_arg);
1391 if (!args)
1392 goto error;
1393 for (i = 0; i < stmt->n_arg; ++i)
1394 args[1 + i] = stmt->args[i];
1395 free(stmt->args);
1396 stmt->args = args;
1398 stmt->n_arg++;
1399 stmt->args[0] = pet_expr_from_access(isl_map_copy(test));
1400 if (!stmt->args[0])
1401 goto error;
1403 isl_map_free(test);
1404 return stmt;
1405 error:
1406 isl_map_free(test);
1407 return pet_stmt_free(stmt);
1410 /* Make all statements in "scop" depend on the value of "test"
1411 * being equal to "satisfied" by adjusting their domains.
1413 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
1414 __isl_take isl_map *test, int satisfied)
1416 int i;
1418 if (!scop)
1419 goto error;
1421 for (i = 0; i < scop->n_stmt; ++i) {
1422 scop->stmts[i] = stmt_filter(scop->stmts[i],
1423 isl_map_copy(test), satisfied);
1424 if (!scop->stmts[i])
1425 goto error;
1428 isl_map_free(test);
1429 return scop;
1430 error:
1431 isl_map_free(test);
1432 return pet_scop_free(scop);
1435 /* Add all parameters in "expr" to "dim" and return the result.
1437 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
1438 __isl_take isl_space *dim)
1440 int i;
1442 if (!expr)
1443 goto error;
1444 for (i = 0; i < expr->n_arg; ++i)
1446 dim = expr_collect_params(expr->args[i], dim);
1448 if (expr->type == pet_expr_access)
1449 dim = isl_space_align_params(dim,
1450 isl_map_get_space(expr->acc.access));
1452 return dim;
1453 error:
1454 isl_space_free(dim);
1455 return pet_expr_free(expr);
1458 /* Add all parameters in "stmt" to "dim" and return the result.
1460 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
1461 __isl_take isl_space *dim)
1463 if (!stmt)
1464 goto error;
1466 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
1467 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
1468 dim = expr_collect_params(stmt->body, dim);
1470 return dim;
1471 error:
1472 isl_space_free(dim);
1473 return pet_stmt_free(stmt);
1476 /* Add all parameters in "array" to "dim" and return the result.
1478 static __isl_give isl_space *array_collect_params(struct pet_array *array,
1479 __isl_take isl_space *dim)
1481 if (!array)
1482 goto error;
1484 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
1485 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
1487 return dim;
1488 error:
1489 isl_space_free(dim);
1490 return pet_array_free(array);
1493 /* Add all parameters in "scop" to "dim" and return the result.
1495 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
1496 __isl_take isl_space *dim)
1498 int i;
1500 if (!scop)
1501 goto error;
1503 for (i = 0; i < scop->n_array; ++i)
1504 dim = array_collect_params(scop->arrays[i], dim);
1506 for (i = 0; i < scop->n_stmt; ++i)
1507 dim = stmt_collect_params(scop->stmts[i], dim);
1509 return dim;
1510 error:
1511 isl_space_free(dim);
1512 return pet_scop_free(scop);
1515 /* Add all parameters in "dim" to all access relations in "expr".
1517 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
1518 __isl_take isl_space *dim)
1520 int i;
1522 if (!expr)
1523 goto error;
1525 for (i = 0; i < expr->n_arg; ++i) {
1526 expr->args[i] =
1527 expr_propagate_params(expr->args[i],
1528 isl_space_copy(dim));
1529 if (!expr->args[i])
1530 goto error;
1533 if (expr->type == pet_expr_access) {
1534 expr->acc.access = isl_map_align_params(expr->acc.access,
1535 isl_space_copy(dim));
1536 if (!expr->acc.access)
1537 goto error;
1540 isl_space_free(dim);
1541 return expr;
1542 error:
1543 isl_space_free(dim);
1544 return pet_expr_free(expr);
1547 /* Add all parameters in "dim" to the domain, schedule and
1548 * all access relations in "stmt".
1550 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
1551 __isl_take isl_space *dim)
1553 if (!stmt)
1554 goto error;
1556 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
1557 stmt->schedule = isl_map_align_params(stmt->schedule,
1558 isl_space_copy(dim));
1559 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
1561 if (!stmt->domain || !stmt->schedule || !stmt->body)
1562 goto error;
1564 isl_space_free(dim);
1565 return stmt;
1566 error:
1567 isl_space_free(dim);
1568 return pet_stmt_free(stmt);
1571 /* Add all parameters in "dim" to "array".
1573 static struct pet_array *array_propagate_params(struct pet_array *array,
1574 __isl_take isl_space *dim)
1576 if (!array)
1577 goto error;
1579 array->context = isl_set_align_params(array->context,
1580 isl_space_copy(dim));
1581 array->extent = isl_set_align_params(array->extent,
1582 isl_space_copy(dim));
1583 if (array->value_bounds) {
1584 array->value_bounds = isl_set_align_params(array->value_bounds,
1585 isl_space_copy(dim));
1586 if (!array->value_bounds)
1587 goto error;
1590 if (!array->context || !array->extent)
1591 goto error;
1593 isl_space_free(dim);
1594 return array;
1595 error:
1596 isl_space_free(dim);
1597 return pet_array_free(array);
1600 /* Add all parameters in "dim" to "scop".
1602 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
1603 __isl_take isl_space *dim)
1605 int i;
1607 if (!scop)
1608 goto error;
1610 for (i = 0; i < scop->n_array; ++i) {
1611 scop->arrays[i] = array_propagate_params(scop->arrays[i],
1612 isl_space_copy(dim));
1613 if (!scop->arrays[i])
1614 goto error;
1617 for (i = 0; i < scop->n_stmt; ++i) {
1618 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
1619 isl_space_copy(dim));
1620 if (!scop->stmts[i])
1621 goto error;
1624 isl_space_free(dim);
1625 return scop;
1626 error:
1627 isl_space_free(dim);
1628 return pet_scop_free(scop);
1631 /* Update all isl_sets and isl_maps in "scop" such that they all
1632 * have the same parameters.
1634 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
1636 isl_space *dim;
1638 if (!scop)
1639 return NULL;
1641 dim = isl_set_get_space(scop->context);
1642 dim = scop_collect_params(scop, dim);
1644 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
1645 scop = scop_propagate_params(scop, dim);
1647 return scop;
1650 /* Check if the given access relation accesses a (0D) array that corresponds
1651 * to one of the parameters in "dim". If so, replace the array access
1652 * by an access to the set of integers with as index (and value)
1653 * that parameter.
1655 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
1656 __isl_take isl_space *dim)
1658 isl_id *array_id = NULL;
1659 int pos = -1;
1661 if (isl_map_has_tuple_id(access, isl_dim_out)) {
1662 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1663 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
1665 isl_space_free(dim);
1667 if (pos < 0) {
1668 isl_id_free(array_id);
1669 return access;
1672 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
1673 if (pos < 0) {
1674 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
1675 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
1676 pos = 0;
1677 } else
1678 isl_id_free(array_id);
1680 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1681 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
1683 return access;
1686 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1687 * in "dim" by a value equal to the corresponding parameter.
1689 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
1690 __isl_take isl_space *dim)
1692 int i;
1694 if (!expr)
1695 goto error;
1697 for (i = 0; i < expr->n_arg; ++i) {
1698 expr->args[i] =
1699 expr_detect_parameter_accesses(expr->args[i],
1700 isl_space_copy(dim));
1701 if (!expr->args[i])
1702 goto error;
1705 if (expr->type == pet_expr_access) {
1706 expr->acc.access = access_detect_parameter(expr->acc.access,
1707 isl_space_copy(dim));
1708 if (!expr->acc.access)
1709 goto error;
1712 isl_space_free(dim);
1713 return expr;
1714 error:
1715 isl_space_free(dim);
1716 return pet_expr_free(expr);
1719 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1720 * in "dim" by a value equal to the corresponding parameter.
1722 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
1723 __isl_take isl_space *dim)
1725 if (!stmt)
1726 goto error;
1728 stmt->body = expr_detect_parameter_accesses(stmt->body,
1729 isl_space_copy(dim));
1731 if (!stmt->domain || !stmt->schedule || !stmt->body)
1732 goto error;
1734 isl_space_free(dim);
1735 return stmt;
1736 error:
1737 isl_space_free(dim);
1738 return pet_stmt_free(stmt);
1741 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1742 * in "dim" by a value equal to the corresponding parameter.
1744 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
1745 __isl_take isl_space *dim)
1747 int i;
1749 if (!scop)
1750 goto error;
1752 for (i = 0; i < scop->n_stmt; ++i) {
1753 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
1754 isl_space_copy(dim));
1755 if (!scop->stmts[i])
1756 goto error;
1759 isl_space_free(dim);
1760 return scop;
1761 error:
1762 isl_space_free(dim);
1763 return pet_scop_free(scop);
1766 /* Replace all accesses to (0D) arrays that correspond to any of
1767 * the parameters used in "scop" by a value equal
1768 * to the corresponding parameter.
1770 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
1772 isl_space *dim;
1774 if (!scop)
1775 return NULL;
1777 dim = isl_set_get_space(scop->context);
1778 dim = scop_collect_params(scop, dim);
1780 scop = scop_detect_parameter_accesses(scop, dim);
1782 return scop;
1785 /* Add all read access relations (if "read" is set) and/or all write
1786 * access relations (if "write" is set) to "accesses" and return the result.
1788 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
1789 int read, int write, __isl_take isl_union_map *accesses)
1791 int i;
1792 isl_id *id;
1793 isl_space *dim;
1795 if (!expr)
1796 return NULL;
1798 for (i = 0; i < expr->n_arg; ++i)
1799 accesses = expr_collect_accesses(expr->args[i],
1800 read, write, accesses);
1802 if (expr->type == pet_expr_access &&
1803 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
1804 ((read && expr->acc.read) || (write && expr->acc.write)))
1805 accesses = isl_union_map_add_map(accesses,
1806 isl_map_copy(expr->acc.access));
1808 return accesses;
1811 /* Collect and return all read access relations (if "read" is set)
1812 * and/or all write * access relations (if "write" is set) in "stmt".
1814 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
1815 int read, int write, __isl_take isl_space *dim)
1817 isl_union_map *accesses;
1819 if (!stmt)
1820 return NULL;
1822 accesses = isl_union_map_empty(dim);
1823 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
1824 accesses = isl_union_map_intersect_domain(accesses,
1825 isl_union_set_from_set(isl_set_copy(stmt->domain)));
1827 return accesses;
1830 /* Collect and return all read access relations (if "read" is set)
1831 * and/or all write * access relations (if "write" is set) in "scop".
1833 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
1834 int read, int write)
1836 int i;
1837 isl_union_map *accesses;
1839 if (!scop)
1840 return NULL;
1842 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
1844 for (i = 0; i < scop->n_stmt; ++i) {
1845 isl_union_map *accesses_i;
1846 isl_space *dim = isl_set_get_space(scop->context);
1847 accesses_i = stmt_collect_accesses(scop->stmts[i],
1848 read, write, dim);
1849 accesses = isl_union_map_union(accesses, accesses_i);
1852 return accesses;
1855 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
1857 return scop_collect_accesses(scop, 1, 0);
1860 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
1862 return scop_collect_accesses(scop, 0, 1);
1865 /* Collect and return the union of iteration domains in "scop".
1867 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
1869 int i;
1870 isl_set *domain_i;
1871 isl_union_set *domain;
1873 if (!scop)
1874 return NULL;
1876 domain = isl_union_set_empty(isl_set_get_space(scop->context));
1878 for (i = 0; i < scop->n_stmt; ++i) {
1879 domain_i = isl_set_copy(scop->stmts[i]->domain);
1880 domain = isl_union_set_add_set(domain, domain_i);
1883 return domain;
1886 /* Collect and return the schedules of the statements in "scop".
1887 * The range is normalized to the maximal number of scheduling
1888 * dimensions.
1890 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
1892 int i, j;
1893 isl_map *schedule_i;
1894 isl_union_map *schedule;
1895 int depth, max_depth = 0;
1897 if (!scop)
1898 return NULL;
1900 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
1902 for (i = 0; i < scop->n_stmt; ++i) {
1903 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
1904 if (depth > max_depth)
1905 max_depth = depth;
1908 for (i = 0; i < scop->n_stmt; ++i) {
1909 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
1910 depth = isl_map_dim(schedule_i, isl_dim_out);
1911 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
1912 max_depth - depth);
1913 for (j = depth; j < max_depth; ++j)
1914 schedule_i = isl_map_fix_si(schedule_i,
1915 isl_dim_out, j, 0);
1916 schedule = isl_union_map_add_map(schedule, schedule_i);
1919 return schedule;
1922 /* Does expression "expr" write to "id"?
1924 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
1926 int i;
1927 isl_id *write_id;
1929 for (i = 0; i < expr->n_arg; ++i) {
1930 int writes = expr_writes(expr->args[i], id);
1931 if (writes < 0 || writes)
1932 return writes;
1935 if (expr->type != pet_expr_access)
1936 return 0;
1937 if (!expr->acc.write)
1938 return 0;
1939 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
1940 return 0;
1942 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
1943 isl_id_free(write_id);
1945 if (!write_id)
1946 return -1;
1948 return write_id == id;
1951 /* Does statement "stmt" write to "id"?
1953 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
1955 return expr_writes(stmt->body, id);
1958 /* Is there any write access in "scop" that accesses "id"?
1960 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
1962 int i;
1964 if (!scop)
1965 return -1;
1967 for (i = 0; i < scop->n_stmt; ++i) {
1968 int writes = stmt_writes(scop->stmts[i], id);
1969 if (writes < 0 || writes)
1970 return writes;
1973 return 0;
1976 /* Reset the user pointer on all parameter ids in "set".
1978 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
1980 int i, n;
1982 n = isl_set_dim(set, isl_dim_param);
1983 for (i = 0; i < n; ++i) {
1984 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
1985 const char *name = isl_id_get_name(id);
1986 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
1987 isl_id_free(id);
1990 return set;
1993 /* Reset the user pointer on all parameter ids in "map".
1995 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
1997 int i, n;
1999 n = isl_map_dim(map, isl_dim_param);
2000 for (i = 0; i < n; ++i) {
2001 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2002 const char *name = isl_id_get_name(id);
2003 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2004 isl_id_free(id);
2007 return map;
2010 /* Reset the user pointer on all parameter ids in "array".
2012 static struct pet_array *array_anonymize(struct pet_array *array)
2014 if (!array)
2015 return NULL;
2017 array->context = set_anonymize(array->context);
2018 array->extent = set_anonymize(array->extent);
2019 if (!array->context || !array->extent)
2020 return pet_array_free(array);
2022 return array;
2025 /* Reset the user pointer on all parameter ids in "access".
2027 static __isl_give isl_map *access_anonymize(__isl_take isl_map *access,
2028 void *user)
2030 access = map_anonymize(access);
2032 return access;
2035 /* Reset the user pointer on all parameter ids in "stmt".
2037 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2039 int i;
2040 isl_space *space;
2041 isl_set *domain;
2043 if (!stmt)
2044 return NULL;
2046 stmt->domain = set_anonymize(stmt->domain);
2047 stmt->schedule = map_anonymize(stmt->schedule);
2048 if (!stmt->domain || !stmt->schedule)
2049 return pet_stmt_free(stmt);
2051 for (i = 0; i < stmt->n_arg; ++i) {
2052 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2053 &access_anonymize, NULL);
2054 if (!stmt->args[i])
2055 return pet_stmt_free(stmt);
2058 stmt->body = pet_expr_foreach_access(stmt->body,
2059 &access_anonymize, NULL);
2060 if (!stmt->body)
2061 return pet_stmt_free(stmt);
2063 return stmt;
2066 /* Reset the user pointer on all parameter ids in "scop".
2068 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2070 int i;
2072 if (!scop)
2073 return NULL;
2075 scop->context = set_anonymize(scop->context);
2076 scop->context_value = set_anonymize(scop->context_value);
2077 if (!scop->context || !scop->context_value)
2078 return pet_scop_free(scop);
2080 for (i = 0; i < scop->n_array; ++i) {
2081 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2082 if (!scop->arrays[i])
2083 return pet_scop_free(scop);
2086 for (i = 0; i < scop->n_stmt; ++i) {
2087 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2088 if (!scop->stmts[i])
2089 return pet_scop_free(scop);
2092 return scop;
2095 /* Given a set "domain", return a wrapped relation with the given set
2096 * as domain and a range of dimension "n_arg", where each coordinate
2097 * is either unbounded or, if the corresponding element of args is of
2098 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2100 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2101 unsigned n_arg, struct pet_expr **args,
2102 __isl_keep isl_union_map *value_bounds)
2104 int i;
2105 isl_map *map;
2106 isl_space *space;
2107 isl_ctx *ctx = isl_set_get_ctx(domain);
2109 map = isl_map_from_domain(domain);
2110 space = isl_map_get_space(map);
2111 space = isl_space_add_dims(space, isl_dim_out, 1);
2113 for (i = 0; i < n_arg; ++i) {
2114 isl_map *map_i;
2115 struct pet_expr *arg = args[i];
2116 isl_id *id;
2117 isl_space *space2;
2119 map_i = isl_map_universe(isl_space_copy(space));
2120 if (arg->type == pet_expr_access) {
2121 isl_map *vb;
2122 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2123 space2 = isl_space_alloc(ctx, 0, 0, 1);
2124 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2125 vb = isl_union_map_extract_map(value_bounds, space2);
2126 if (!isl_map_plain_is_empty(vb))
2127 map_i = isl_map_intersect_range(map_i,
2128 isl_map_range(vb));
2129 else
2130 isl_map_free(vb);
2132 map = isl_map_flat_range_product(map, map_i);
2134 isl_space_free(space);
2136 return isl_map_wrap(map);
2139 /* Data used in access_gist() callback.
2141 struct pet_access_gist_data {
2142 isl_set *domain;
2143 isl_union_map *value_bounds;
2146 /* Given an expression "expr" of type pet_expr_access, compute
2147 * the gist of the associated access relation with respect to
2148 * data->domain and the bounds on the values of the arguments
2149 * of the expression.
2151 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
2153 struct pet_access_gist_data *data = user;
2154 isl_set *domain;
2156 domain = isl_set_copy(data->domain);
2157 if (expr->n_arg > 0)
2158 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
2159 data->value_bounds);
2161 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
2162 if (!expr->acc.access)
2163 return pet_expr_free(expr);
2165 return expr;
2168 /* Compute the gist of the iteration domain and all access relations
2169 * of "stmt" based on the constraints on the parameters specified by "context"
2170 * and the constraints on the values of nested accesses specified
2171 * by "value_bounds".
2173 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2174 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2176 int i;
2177 isl_space *space;
2178 isl_set *domain;
2179 struct pet_access_gist_data data;
2181 if (!stmt)
2182 return NULL;
2184 data.domain = isl_set_copy(stmt->domain);
2185 data.value_bounds = value_bounds;
2186 if (stmt->n_arg > 0)
2187 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
2189 data.domain = isl_set_intersect_params(data.domain,
2190 isl_set_copy(context));
2192 for (i = 0; i < stmt->n_arg; ++i) {
2193 stmt->args[i] = pet_expr_foreach_access_expr(stmt->args[i],
2194 &access_gist, &data);
2195 if (!stmt->args[i])
2196 goto error;
2199 stmt->body = pet_expr_foreach_access_expr(stmt->body,
2200 &access_gist, &data);
2201 if (!stmt->body)
2202 goto error;
2204 isl_set_free(data.domain);
2206 space = isl_set_get_space(stmt->domain);
2207 if (isl_space_is_wrapping(space))
2208 space = isl_space_domain(isl_space_unwrap(space));
2209 domain = isl_set_universe(space);
2210 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2211 if (stmt->n_arg > 0)
2212 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
2213 value_bounds);
2214 stmt->domain = isl_set_gist(stmt->domain, domain);
2215 if (!stmt->domain)
2216 return pet_stmt_free(stmt);
2218 return stmt;
2219 error:
2220 isl_set_free(data.domain);
2221 return pet_stmt_free(stmt);
2224 /* Compute the gist of the extent of the array
2225 * based on the constraints on the parameters specified by "context".
2227 static struct pet_array *array_gist(struct pet_array *array,
2228 __isl_keep isl_set *context)
2230 if (!array)
2231 return NULL;
2233 array->extent = isl_set_gist_params(array->extent,
2234 isl_set_copy(context));
2235 if (!array->extent)
2236 return pet_array_free(array);
2238 return array;
2241 /* Compute the gist of all sets and relations in "scop"
2242 * based on the constraints on the parameters specified by "scop->context"
2243 * and the constraints on the values of nested accesses specified
2244 * by "value_bounds".
2246 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
2247 __isl_keep isl_union_map *value_bounds)
2249 int i;
2251 if (!scop)
2252 return NULL;
2254 scop->context = isl_set_coalesce(scop->context);
2255 if (!scop->context)
2256 return pet_scop_free(scop);
2258 for (i = 0; i < scop->n_array; ++i) {
2259 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
2260 if (!scop->arrays[i])
2261 return pet_scop_free(scop);
2264 for (i = 0; i < scop->n_stmt; ++i) {
2265 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
2266 value_bounds);
2267 if (!scop->stmts[i])
2268 return pet_scop_free(scop);
2271 return scop;
2274 /* Intersect the context of "scop" with "context".
2275 * To ensure that we don't introduce any unnamed parameters in
2276 * the context of "scop", we first remove the unnamed parameters
2277 * from "context".
2279 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
2280 __isl_take isl_set *context)
2282 if (!scop)
2283 goto error;
2285 context = set_project_out_unnamed_params(context);
2286 scop->context = isl_set_intersect(scop->context, context);
2287 if (!scop->context)
2288 return pet_scop_free(scop);
2290 return scop;
2291 error:
2292 isl_set_free(context);
2293 return pet_scop_free(scop);